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