• 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;
110     struct mq_attr kold = { 0 };
111 
112     if (new != NULL) {
113         ret = LOS_ArchCopyFromUser(&knew, new, sizeof(struct mq_attr));
114         if (ret != 0) {
115             return -EFAULT;
116         }
117     }
118     MQUEUE_FD_U2K(mqd);
119     ret = mq_getsetattr(mqd, new ? &knew : NULL, old ? &kold : NULL);
120     if (ret < 0) {
121         return -get_errno();
122     }
123     if (old != NULL) {
124         ret = LOS_ArchCopyToUser(old, &kold, sizeof(struct mq_attr));
125         if (ret != 0) {
126             return -EFAULT;
127         }
128     }
129     return ret;
130 }
131 
SysMqUnlink(const char * mqName)132 int SysMqUnlink(const char *mqName)
133 {
134     int ret;
135     int retValue;
136     char kMqName[PATH_MAX + 1] = { 0 };
137 
138     retValue = LOS_StrncpyFromUser(kMqName, mqName, PATH_MAX);
139     if (retValue < 0) {
140         return retValue;
141     }
142 
143     ret = mq_unlink(kMqName);
144     if (ret < 0) {
145         return -get_errno();
146     }
147     return ret;
148 }
149 
SysMqTimedSend(mqd_t personal,const char * msg,size_t msgLen,unsigned int msgPrio,const struct timespec * absTimeout)150 int SysMqTimedSend(mqd_t personal, const char *msg, size_t msgLen, unsigned int msgPrio,
151                    const struct timespec *absTimeout)
152 {
153     int ret;
154     struct timespec timeout;
155     char *msgIntr = NULL;
156 
157     if (absTimeout != NULL) {
158         ret = LOS_ArchCopyFromUser(&timeout, absTimeout, sizeof(struct timespec));
159         if (ret != 0) {
160             return -EFAULT;
161         }
162     }
163     if (msgLen == 0) {
164         return -EINVAL;
165     }
166     msgIntr = (char *)malloc(msgLen);
167     if (msgIntr == NULL) {
168         return -ENOMEM;
169     }
170     ret = LOS_ArchCopyFromUser(msgIntr, msg, msgLen);
171     if (ret != 0) {
172         free(msgIntr);
173         return -EFAULT;
174     }
175     MQUEUE_FD_U2K(personal);
176     ret = mq_timedsend(personal, msgIntr, msgLen, msgPrio, absTimeout ? &timeout : NULL);
177     free(msgIntr);
178     if (ret < 0) {
179         return -get_errno();
180     }
181     return ret;
182 }
183 
SysMqTimedReceive(mqd_t personal,char * msg,size_t msgLen,unsigned int * msgPrio,const struct timespec * absTimeout)184 ssize_t SysMqTimedReceive(mqd_t personal, char *msg, size_t msgLen, unsigned int *msgPrio,
185                           const struct timespec *absTimeout)
186 {
187     int ret, receiveLen;
188     struct timespec timeout;
189     char *msgIntr = NULL;
190     unsigned int kMsgPrio;
191 
192     if (absTimeout != NULL) {
193         ret = LOS_ArchCopyFromUser(&timeout, absTimeout, sizeof(struct timespec));
194         if (ret != 0) {
195             return -EFAULT;
196         }
197     }
198     if (msgLen == 0) {
199         return -EINVAL;
200     }
201     msgIntr = (char *)malloc(msgLen);
202     if (msgIntr == NULL) {
203         return -ENOMEM;
204     }
205     MQUEUE_FD_U2K(personal);
206     receiveLen = mq_timedreceive(personal, msgIntr, msgLen, &kMsgPrio, absTimeout ? &timeout : NULL);
207     if (receiveLen < 0) {
208         free(msgIntr);
209         return -get_errno();
210     }
211 
212     if (msgPrio != NULL) {
213         ret = LOS_ArchCopyToUser(msgPrio, &kMsgPrio, sizeof(unsigned int));
214         if (ret != 0) {
215             free(msgIntr);
216             return -EFAULT;
217         }
218     }
219 
220     ret = LOS_ArchCopyToUser(msg, msgIntr, receiveLen);
221     free(msgIntr);
222     if (ret != 0) {
223         return -EFAULT;
224     }
225     return receiveLen;
226 }
227 
SysSigAction(int sig,const sigaction_t * restrict sa,sigaction_t * restrict old,size_t sigsetsize)228 int SysSigAction(int sig, const sigaction_t *restrict sa, sigaction_t *restrict old, size_t sigsetsize)
229 {
230     return OsSigAction(sig, sa, old);
231 }
232 
SysSigprocMask(int how,const sigset_t_l * restrict setl,sigset_t_l * restrict oldl,size_t sigsetsize)233 int SysSigprocMask(int how, const sigset_t_l *restrict setl, sigset_t_l *restrict oldl, size_t sigsetsize)
234 {
235     CHECK_ASPACE(setl, sizeof(sigset_t_l));
236     CHECK_ASPACE(oldl, sizeof(sigset_t_l));
237     CPY_FROM_USER(setl);
238     CPY_FROM_USER(oldl);
239     /* Let OsSigprocMask do all of the work */
240     int ret = OsSigprocMask(how, setl, oldl);
241     CPY_TO_USER(oldl);
242     return ret;
243 }
244 
SysKill(pid_t pid,int sig)245 int SysKill(pid_t pid, int sig)
246 {
247     return OsKillLock(pid, sig);
248 }
249 
SysPthreadKill(pid_t pid,int sig)250 int SysPthreadKill(pid_t pid, int sig)
251 {
252     return OsPthreadKill(pid, sig);
253 }
254 
SysSigTimedWait(const sigset_t_l * setl,siginfo_t * info,const struct timespec * timeout,size_t sigsetsize)255 int SysSigTimedWait(const sigset_t_l *setl, siginfo_t *info, const struct timespec *timeout, size_t sigsetsize)
256 {
257     sigset_t set;
258     unsigned int tick;
259     int retVal, ret;
260     siginfo_t infoIntr = { 0 };
261     struct timespec timeoutIntr;
262 
263     retVal = LOS_ArchCopyFromUser(&set, &(setl->sig[0]), sizeof(sigset_t));
264     if (retVal != 0) {
265         return -EFAULT;
266     }
267 
268     if (timeout == NULL) {
269         tick = LOS_WAIT_FOREVER;
270     } else {
271         retVal = LOS_ArchCopyFromUser(&timeoutIntr, timeout, sizeof(struct timespec));
272         if (retVal != 0) {
273             return -EFAULT;
274         }
275         if (!ValidTimeSpec(&timeoutIntr)) {
276             return -EINVAL;
277         }
278         tick = OsTimeSpec2Tick(&timeoutIntr);
279     }
280     ret = OsSigTimedWait(&set, &infoIntr, tick);
281     if (ret < 0) {
282         return ret;
283     }
284     if (info != NULL) {
285         retVal = LOS_ArchCopyToUser(info, &infoIntr, sizeof(siginfo_t));
286         if (retVal != 0) {
287             return -EFAULT;
288         }
289     }
290     return (ret == 0 ? infoIntr.si_signo : ret);
291 }
292 
SysPause(void)293 int SysPause(void)
294 {
295     return OsPause();
296 }
297 
SysSigPending(sigset_t_l * setl)298 int SysSigPending(sigset_t_l *setl)
299 {
300     sigset_t set;
301     int ret;
302 
303     ret = LOS_ArchCopyFromUser(&set, &(setl->sig[0]), sizeof(sigset_t));
304     if (ret != 0) {
305         return -EFAULT;
306     }
307     ret = OsSigPending(&set);
308     if (ret != LOS_OK) {
309         return ret;
310     }
311     ret = LOS_ArchCopyToUser(&(setl->sig[0]), &set, sizeof(sigset_t));
312     if (ret != LOS_OK) {
313         return -EFAULT;
314     }
315     return ret;
316 }
317 
SysSigSuspend(sigset_t_l * setl)318 int SysSigSuspend(sigset_t_l *setl)
319 {
320     sigset_t set;
321     int retVal;
322 
323     retVal = LOS_ArchCopyFromUser(&set, &(setl->sig[0]), sizeof(sigset_t));
324     if (retVal != 0) {
325         return -EFAULT;
326     }
327 
328     return OsSigSuspend(&set);
329 }
330 
331 #ifdef LOSCFG_KERNEL_PIPE
SysMkFifo(const char * pathName,mode_t mode)332 int SysMkFifo(const char *pathName, mode_t mode)
333 {
334     int retValue;
335     char kPathName[PATH_MAX + 1] = { 0 };
336 
337     retValue = LOS_StrncpyFromUser(kPathName, pathName, PATH_MAX);
338     if (retValue < 0) {
339         return retValue;
340     }
341     return mkfifo(kPathName, mode);
342 }
343 #endif
344