• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "syscall_pub.h"
33 #include "mqueue.h"
34 #include <errno.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include "time_posix.h"
38 #include "user_copy.h"
39 #include "los_signal.h"
40 #include "los_process_pri.h"
41 #include "los_strncpy_from_user.h"
42 #include "fs/file.h"
43 
44 #define MQUEUE_FD_U2K(id) \
45     do { \
46         int sysFd = GetAssociatedSystemFd((INTPTR)(id)); \
47         (id) = (mqd_t)sysFd; \
48     } while (0)
49 #define MQUEUE_FD_K2U(id) \
50     do { \
51         int procFd = AllocAndAssocProcessFd((INTPTR)(id), MIN_START_FD); \
52         if (procFd == -1) { \
53             mq_close(id); \
54             set_errno(EMFILE); \
55             (id) = (mqd_t)(-EMFILE); \
56         } else { \
57             (id) = (mqd_t)procFd; \
58         } \
59     } while (0)
60 
SysMqOpen(const char * mqName,int openFlag,mode_t mode,struct mq_attr * attr)61 mqd_t SysMqOpen(const char *mqName, int openFlag, mode_t mode, struct mq_attr *attr)
62 {
63     mqd_t ret;
64     int retValue;
65     char kMqName[PATH_MAX + 1] = { 0 };
66 
67     retValue = LOS_StrncpyFromUser(kMqName, mqName, PATH_MAX);
68     if (retValue < 0) {
69         return retValue;
70     }
71     ret = mq_open(kMqName, openFlag, mode, attr);
72     if (ret == -1) {
73         return (mqd_t)-get_errno();
74     }
75     /* SysFd to procFd */
76     MQUEUE_FD_K2U(ret);
77     return ret;
78 }
79 
SysMqClose(mqd_t personal)80 int SysMqClose(mqd_t personal)
81 {
82     int ret;
83     int ufd = (INTPTR)personal;
84 
85     MQUEUE_FD_U2K(personal);
86     ret = mq_close(personal);
87     if (ret < 0) {
88         return -get_errno();
89     }
90     FreeProcessFd(ufd);
91     return ret;
92 }
93 
SysMqNotify(mqd_t personal,const struct sigevent * sigev)94 int SysMqNotify(mqd_t personal, const struct sigevent *sigev)
95 {
96     int ret;
97 
98     MQUEUE_FD_U2K(personal);
99     ret = OsMqNotify(personal, sigev);
100     if (ret < 0) {
101         return -get_errno();
102     }
103     return ret;
104 }
105 
SysMqGetSetAttr(mqd_t mqd,const struct mq_attr * new,struct mq_attr * old)106 int SysMqGetSetAttr(mqd_t mqd, const struct mq_attr *new, struct mq_attr *old)
107 {
108     int ret;
109     struct mq_attr knew, kold;
110 
111     if (new != NULL) {
112         ret = LOS_ArchCopyFromUser(&knew, new, sizeof(struct mq_attr));
113         if (ret != 0) {
114             return -EFAULT;
115         }
116     }
117     MQUEUE_FD_U2K(mqd);
118     ret = mq_getsetattr(mqd, new ? &knew : NULL, old ? &kold : NULL);
119     if (ret < 0) {
120         return -get_errno();
121     }
122     if (old != NULL) {
123         ret = LOS_ArchCopyToUser(old, &kold, sizeof(struct mq_attr));
124         if (ret != 0) {
125             return -EFAULT;
126         }
127     }
128     return ret;
129 }
130 
SysMqUnlink(const char * mqName)131 int SysMqUnlink(const char *mqName)
132 {
133     int ret;
134     int retValue;
135     char kMqName[PATH_MAX + 1] = { 0 };
136 
137     retValue = LOS_StrncpyFromUser(kMqName, mqName, PATH_MAX);
138     if (retValue < 0) {
139         return retValue;
140     }
141 
142     ret = mq_unlink(kMqName);
143     if (ret < 0) {
144         return -get_errno();
145     }
146     return ret;
147 }
148 
SysMqTimedSend(mqd_t personal,const char * msg,size_t msgLen,unsigned int msgPrio,const struct timespec * absTimeout)149 int SysMqTimedSend(mqd_t personal, const char *msg, size_t msgLen, unsigned int msgPrio,
150                    const struct timespec *absTimeout)
151 {
152     int ret;
153     struct timespec timeout;
154     char *msgIntr = NULL;
155 
156     if (absTimeout != NULL) {
157         ret = LOS_ArchCopyFromUser(&timeout, absTimeout, sizeof(struct timespec));
158         if (ret != 0) {
159             return -EFAULT;
160         }
161     }
162     if (msgLen == 0) {
163         return -EINVAL;
164     }
165     msgIntr = (char *)malloc(msgLen);
166     if (msgIntr == NULL) {
167         return -ENOMEM;
168     }
169     ret = LOS_ArchCopyFromUser(msgIntr, msg, msgLen);
170     if (ret != 0) {
171         free(msgIntr);
172         return -EFAULT;
173     }
174     MQUEUE_FD_U2K(personal);
175     ret = mq_timedsend(personal, msgIntr, msgLen, msgPrio, absTimeout ? &timeout : NULL);
176     free(msgIntr);
177     if (ret < 0) {
178         return -get_errno();
179     }
180     return ret;
181 }
182 
SysMqTimedReceive(mqd_t personal,char * msg,size_t msgLen,unsigned int * msgPrio,const struct timespec * absTimeout)183 ssize_t SysMqTimedReceive(mqd_t personal, char *msg, size_t msgLen, unsigned int *msgPrio,
184                           const struct timespec *absTimeout)
185 {
186     int ret, receiveLen;
187     struct timespec timeout;
188     char *msgIntr = NULL;
189     unsigned int kMsgPrio;
190 
191     if (absTimeout != NULL) {
192         ret = LOS_ArchCopyFromUser(&timeout, absTimeout, sizeof(struct timespec));
193         if (ret != 0) {
194             return -EFAULT;
195         }
196     }
197     if (msgLen == 0) {
198         return -EINVAL;
199     }
200     msgIntr = (char *)malloc(msgLen);
201     if (msgIntr == NULL) {
202         return -ENOMEM;
203     }
204     MQUEUE_FD_U2K(personal);
205     receiveLen = mq_timedreceive(personal, msgIntr, msgLen, &kMsgPrio, absTimeout ? &timeout : NULL);
206     if (receiveLen < 0) {
207         free(msgIntr);
208         return -get_errno();
209     }
210 
211     if (msgPrio != NULL) {
212         ret = LOS_ArchCopyToUser(msgPrio, &kMsgPrio, sizeof(unsigned int));
213         if (ret != 0) {
214             free(msgIntr);
215             return -EFAULT;
216         }
217     }
218 
219     ret = LOS_ArchCopyToUser(msg, msgIntr, receiveLen);
220     free(msgIntr);
221     if (ret != 0) {
222         return -EFAULT;
223     }
224     return receiveLen;
225 }
226 
SysSigAction(int sig,const sigaction_t * restrict sa,sigaction_t * restrict old,size_t sigsetsize)227 int SysSigAction(int sig, const sigaction_t *restrict sa, sigaction_t *restrict old, size_t sigsetsize)
228 {
229     return OsSigAction(sig, sa, old);
230 }
231 
SysSigprocMask(int how,const sigset_t_l * restrict setl,sigset_t_l * restrict oldl,size_t sigsetsize)232 int SysSigprocMask(int how, const sigset_t_l *restrict setl, sigset_t_l *restrict oldl, size_t sigsetsize)
233 {
234     CHECK_ASPACE(setl, sizeof(sigset_t_l));
235     CHECK_ASPACE(oldl, sizeof(sigset_t_l));
236     CPY_FROM_USER(setl);
237     CPY_FROM_USER(oldl);
238     /* Let OsSigprocMask do all of the work */
239     int ret = OsSigprocMask(how, setl, oldl);
240     CPY_TO_USER(oldl);
241     return ret;
242 }
243 
SysKill(pid_t pid,int sig)244 int SysKill(pid_t pid, int sig)
245 {
246     return OsKillLock(pid, sig);
247 }
248 
SysPthreadKill(pid_t pid,int sig)249 int SysPthreadKill(pid_t pid, int sig)
250 {
251     return OsPthreadKill(pid, sig);
252 }
253 
SysSigTimedWait(const sigset_t_l * setl,siginfo_t * info,const struct timespec * timeout,size_t sigsetsize)254 int SysSigTimedWait(const sigset_t_l *setl, siginfo_t *info, const struct timespec *timeout, size_t sigsetsize)
255 {
256     sigset_t set;
257     unsigned int tick;
258     int retVal, ret;
259     siginfo_t infoIntr;
260     struct timespec timeoutIntr;
261 
262     retVal = LOS_ArchCopyFromUser(&set, &(setl->sig[0]), sizeof(sigset_t));
263     if (retVal != 0) {
264         return -EFAULT;
265     }
266 
267     if (timeout == NULL) {
268         tick = LOS_WAIT_FOREVER;
269     } else {
270         retVal = LOS_ArchCopyFromUser(&timeoutIntr, timeout, sizeof(struct timespec));
271         if (retVal != 0) {
272             return -EFAULT;
273         }
274         if (!ValidTimeSpec(&timeoutIntr)) {
275             return -EINVAL;
276         }
277         tick = OsTimeSpec2Tick(&timeoutIntr);
278     }
279     ret = OsSigTimedWait(&set, &infoIntr, tick);
280     if (ret < 0) {
281         return ret;
282     }
283     if (info != NULL) {
284         retVal = LOS_ArchCopyToUser(info, &infoIntr, sizeof(siginfo_t));
285         if (retVal != 0) {
286             return -EFAULT;
287         }
288     }
289     return (ret == 0 ? infoIntr.si_signo : ret);
290 }
291 
SysPause(void)292 int SysPause(void)
293 {
294     return OsPause();
295 }
296 
SysSigPending(sigset_t_l * setl)297 int SysSigPending(sigset_t_l *setl)
298 {
299     sigset_t set;
300     int ret;
301 
302     ret = LOS_ArchCopyFromUser(&set, &(setl->sig[0]), sizeof(sigset_t));
303     if (ret != 0) {
304         return -EFAULT;
305     }
306     ret = OsSigPending(&set);
307     if (ret != LOS_OK) {
308         return ret;
309     }
310     ret = LOS_ArchCopyToUser(&(setl->sig[0]), &set, sizeof(sigset_t));
311     if (ret != LOS_OK) {
312         return -EFAULT;
313     }
314     return ret;
315 }
316 
SysSigSuspend(sigset_t_l * setl)317 int SysSigSuspend(sigset_t_l *setl)
318 {
319     sigset_t set;
320     int retVal;
321 
322     retVal = LOS_ArchCopyFromUser(&set, &(setl->sig[0]), sizeof(sigset_t));
323     if (retVal != 0) {
324         return -EFAULT;
325     }
326 
327     return OsSigSuspend(&set);
328 }
329 
330 #ifdef LOSCFG_KERNEL_PIPE
SysMkFifo(const char * pathName,mode_t mode)331 int SysMkFifo(const char *pathName, mode_t mode)
332 {
333     int retValue;
334     char kPathName[PATH_MAX + 1] = { 0 };
335 
336     retValue = LOS_StrncpyFromUser(kPathName, pathName, PATH_MAX);
337     if (retValue < 0) {
338         return retValue;
339     }
340     return mkfifo(kPathName, mode);
341 }
342 #endif
343