• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2022 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 "los_signal.h"
33 #include "pthread.h"
34 #include "los_process_pri.h"
35 #include "los_sched_pri.h"
36 #include "los_hw_pri.h"
37 #include "user_copy.h"
38 #ifdef LOSCFG_SECURITY_CAPABILITY
39 #include "capability_api.h"
40 #endif
41 #include "los_atomic.h"
42 
43 #ifdef LOSCFG_KERNEL_VM
44 
raise(int sig)45 int raise(int sig)
46 {
47     (VOID)sig;
48     PRINT_ERR("%s NOT SUPPORT\n", __FUNCTION__);
49     errno = ENOSYS;
50     return -1;
51 }
52 
53 #define GETUNMASKSET(procmask, pendFlag) ((~(procmask)) & (sigset_t)(pendFlag))
54 #define UINT64_BIT_SIZE 64
55 
OsSigIsMember(const sigset_t * set,int signo)56 int OsSigIsMember(const sigset_t *set, int signo)
57 {
58     int ret = LOS_NOK;
59     /* In musl, sig No bits 00000100 present sig No 3, but  1<< 3 = 00001000, so signo needs minus 1 */
60     signo -= 1;
61     /* Verify the signal */
62     if (GOOD_SIGNO(signo)) {
63         /* Check if the signal is in the set */
64         ret = ((*set & SIGNO2SET((unsigned int)signo)) != 0);
65     }
66 
67     return ret;
68 }
69 
OsMoveTmpInfoToUnbInfo(sig_cb * sigcb,INT32 signo)70 STATIC VOID OsMoveTmpInfoToUnbInfo(sig_cb *sigcb, INT32 signo)
71 {
72     SigInfoListNode *tmpInfoNode = sigcb->tmpInfoListHead;
73     SigInfoListNode **prevHook = &sigcb->tmpInfoListHead;
74     while (tmpInfoNode != NULL) {
75         if (tmpInfoNode->info.si_signo == signo) {
76             /* copy tmpinfo to unbinfo. */
77             (VOID)memcpy_s(&sigcb->sigunbinfo, sizeof(siginfo_t), &tmpInfoNode->info, sizeof(siginfo_t));
78             /* delete tmpinfo from tmpList. */
79             *prevHook = tmpInfoNode->next;
80             (VOID)LOS_MemFree(m_aucSysMem0, tmpInfoNode);
81             break;
82         }
83         prevHook = &tmpInfoNode->next;
84         tmpInfoNode = tmpInfoNode->next;
85     }
86 }
87 
OsAddSigInfoToTmpList(sig_cb * sigcb,siginfo_t * info)88 STATIC INT32 OsAddSigInfoToTmpList(sig_cb *sigcb, siginfo_t *info)
89 {
90     /* try to find the old siginfo */
91     SigInfoListNode *tmp = sigcb->tmpInfoListHead;
92     while (tmp != NULL) {
93         if (tmp->info.si_signo == info->si_signo) {
94             /* found it, break. */
95             break;
96         }
97         tmp = tmp->next;
98     }
99 
100     if (tmp == NULL) {
101         /* none, alloc new one */
102         tmp = (SigInfoListNode *)LOS_MemAlloc(m_aucSysMem0, sizeof(SigInfoListNode));
103         if (tmp == NULL) {
104             return LOS_NOK;
105         }
106         tmp->next = sigcb->tmpInfoListHead;
107         sigcb->tmpInfoListHead = tmp;
108     }
109 
110     (VOID)memcpy_s(&tmp->info, sizeof(siginfo_t), info, sizeof(siginfo_t));
111 
112     return LOS_OK;
113 }
114 
OsClearSigInfoTmpList(sig_cb * sigcb)115 VOID OsClearSigInfoTmpList(sig_cb *sigcb)
116 {
117     while (sigcb->tmpInfoListHead != NULL) {
118         SigInfoListNode *tmpInfoNode = sigcb->tmpInfoListHead;
119         sigcb->tmpInfoListHead = sigcb->tmpInfoListHead->next;
120         (VOID)LOS_MemFree(m_aucSysMem0, tmpInfoNode);
121     }
122 }
123 
OsSigWaitTaskWake(LosTaskCB * taskCB,INT32 signo)124 STATIC INLINE VOID OsSigWaitTaskWake(LosTaskCB *taskCB, INT32 signo)
125 {
126     sig_cb *sigcb = &taskCB->sig;
127 
128     if (!LOS_ListEmpty(&sigcb->waitList) && OsSigIsMember(&sigcb->sigwaitmask, signo)) {
129         OsMoveTmpInfoToUnbInfo(sigcb, signo);
130         OsTaskWakeClearPendMask(taskCB);
131         taskCB->ops->wake(taskCB);
132         OsSigEmptySet(&sigcb->sigwaitmask);
133     }
134 }
135 
OsPendingTaskWake(LosTaskCB * taskCB,INT32 signo)136 STATIC UINT32 OsPendingTaskWake(LosTaskCB *taskCB, INT32 signo)
137 {
138     if (!OsTaskIsPending(taskCB) || !OsProcessIsUserMode(OS_PCB_FROM_PID(taskCB->processID))) {
139         return 0;
140     }
141 
142     if ((signo != SIGKILL) && (taskCB->waitFlag != OS_TASK_WAIT_SIGNAL)) {
143         return 0;
144     }
145 
146     switch (taskCB->waitFlag) {
147         case OS_TASK_WAIT_PROCESS:
148         case OS_TASK_WAIT_GID:
149         case OS_TASK_WAIT_ANYPROCESS:
150             OsWaitWakeTask(taskCB, OS_INVALID_VALUE);
151             break;
152         case OS_TASK_WAIT_JOIN:
153             OsTaskWakeClearPendMask(taskCB);
154             taskCB->ops->wake(taskCB);
155             break;
156         case OS_TASK_WAIT_SIGNAL:
157             OsSigWaitTaskWake(taskCB, signo);
158             break;
159         case OS_TASK_WAIT_LITEIPC:
160             OsTaskWakeClearPendMask(taskCB);
161             taskCB->ops->wake(taskCB);
162             break;
163         case OS_TASK_WAIT_FUTEX:
164             OsFutexNodeDeleteFromFutexHash(&taskCB->futex, TRUE, NULL, NULL);
165             OsTaskWakeClearPendMask(taskCB);
166             taskCB->ops->wake(taskCB);
167             break;
168         default:
169             break;
170     }
171 
172     return 0;
173 }
174 
OsTcbDispatch(LosTaskCB * stcb,siginfo_t * info)175 int OsTcbDispatch(LosTaskCB *stcb, siginfo_t *info)
176 {
177     bool masked = FALSE;
178     sig_cb *sigcb = &stcb->sig;
179 
180     OS_RETURN_IF_NULL(sigcb);
181     /* If signo is 0, not send signal, just check process or pthread exist */
182     if (info->si_signo == 0) {
183         return 0;
184     }
185     masked = (bool)OsSigIsMember(&sigcb->sigprocmask, info->si_signo);
186     if (masked) {
187         /* If signal is in wait list and mask list, need unblock it */
188         if (LOS_ListEmpty(&sigcb->waitList)  ||
189             (!LOS_ListEmpty(&sigcb->waitList) && !OsSigIsMember(&sigcb->sigwaitmask, info->si_signo))) {
190             OsSigAddSet(&sigcb->sigPendFlag, info->si_signo);
191         }
192     } else {
193         /* unmasked signal actions */
194         OsSigAddSet(&sigcb->sigFlag, info->si_signo);
195     }
196 
197     if (OsAddSigInfoToTmpList(sigcb, info) == LOS_NOK) {
198         return -ENOMEM;
199     }
200 
201     return OsPendingTaskWake(stcb, info->si_signo);
202 }
203 
OsSigMaskSwitch(LosTaskCB * const rtcb,sigset_t set)204 void OsSigMaskSwitch(LosTaskCB * const rtcb, sigset_t set)
205 {
206     sigset_t unmaskset;
207 
208     rtcb->sig.sigprocmask = set;
209     unmaskset = GETUNMASKSET(rtcb->sig.sigprocmask, rtcb->sig.sigPendFlag);
210     if (unmaskset != NULL_SIGNAL_SET) {
211         /* pendlist do */
212         rtcb->sig.sigFlag |= unmaskset;
213         rtcb->sig.sigPendFlag ^= unmaskset;
214     }
215 }
216 
OsSigprocMask(int how,const sigset_t_l * setl,sigset_t_l * oldsetl)217 int OsSigprocMask(int how, const sigset_t_l *setl, sigset_t_l *oldsetl)
218 {
219     LosTaskCB *spcb = NULL;
220     int ret = LOS_OK;
221     unsigned int intSave;
222     sigset_t set;
223 
224     SCHEDULER_LOCK(intSave);
225     spcb = OsCurrTaskGet();
226     /* If requested, copy the old mask to user. */
227     if (oldsetl != NULL) {
228         *(sigset_t *)oldsetl = spcb->sig.sigprocmask;
229     }
230     /* If requested, modify the current signal mask. */
231     if (setl != NULL) {
232         set = *(sigset_t *)setl;
233         /* Okay, determine what we are supposed to do */
234         switch (how) {
235             /* Set the union of the current set and the signal
236              * set pointed to by set as the new sigprocmask.
237              */
238             case SIG_BLOCK:
239                 spcb->sig.sigprocmask |= set;
240                 break;
241             /* Set the intersection of the current set and the
242              * signal set pointed to by set as the new sigprocmask.
243              */
244             case SIG_UNBLOCK:
245                 spcb->sig.sigprocmask &= ~(set);
246                 break;
247             /* Set the signal set pointed to by set as the new sigprocmask. */
248             case SIG_SETMASK:
249                 spcb->sig.sigprocmask = set;
250                 break;
251             default:
252                 ret = -EINVAL;
253                 break;
254         }
255         /* If pending mask not in sigmask, need set sigflag. */
256         OsSigMaskSwitch(spcb, spcb->sig.sigprocmask);
257     }
258     SCHEDULER_UNLOCK(intSave);
259 
260     return ret;
261 }
262 
OsSigProcessForeachChild(LosProcessCB * spcb,ForEachTaskCB handler,void * arg)263 int OsSigProcessForeachChild(LosProcessCB *spcb, ForEachTaskCB handler, void *arg)
264 {
265     int ret;
266 
267     /* Visit the main thread last (if present) */
268     LosTaskCB *taskCB = NULL;
269     LOS_DL_LIST_FOR_EACH_ENTRY(taskCB, &(spcb->threadSiblingList), LosTaskCB, threadList) {
270         ret = handler(taskCB, arg);
271         OS_RETURN_IF(ret != 0, ret);
272     }
273     return LOS_OK;
274 }
275 
SigProcessSignalHandler(LosTaskCB * tcb,void * arg)276 static int SigProcessSignalHandler(LosTaskCB *tcb, void *arg)
277 {
278     struct ProcessSignalInfo *info = (struct ProcessSignalInfo *)arg;
279     int ret;
280     int isMember;
281 
282     if (tcb == NULL) {
283         return 0;
284     }
285 
286     /* If the default tcb is not set, then set this one as default. */
287     if (!info->defaultTcb) {
288         info->defaultTcb = tcb;
289     }
290 
291     isMember = OsSigIsMember(&tcb->sig.sigwaitmask, info->sigInfo->si_signo);
292     if (isMember && (!info->awakenedTcb)) {
293         /* This means the task is waiting for this signal. Stop looking for it and use this tcb.
294          * The requirement is: if more than one task in this task group is waiting for the signal,
295          * then only one indeterminate task in the group will receive the signal.
296          */
297         ret = OsTcbDispatch(tcb, info->sigInfo);
298         OS_RETURN_IF(ret < 0, ret);
299 
300         /* set this tcb as awakenedTcb */
301         info->awakenedTcb = tcb;
302         OS_RETURN_IF(info->receivedTcb != NULL, SIG_STOP_VISIT); /* Stop search */
303     }
304     /* Is this signal unblocked on this thread? */
305     isMember = OsSigIsMember(&tcb->sig.sigprocmask, info->sigInfo->si_signo);
306     if ((!isMember) && (!info->receivedTcb) && (tcb != info->awakenedTcb)) {
307         /* if unblockedTcb of this signal is not set, then set it. */
308         if (!info->unblockedTcb) {
309             info->unblockedTcb = tcb;
310         }
311 
312         ret = OsTcbDispatch(tcb, info->sigInfo);
313         OS_RETURN_IF(ret < 0, ret);
314         /* set this tcb as receivedTcb */
315         info->receivedTcb = tcb;
316         OS_RETURN_IF(info->awakenedTcb != NULL, SIG_STOP_VISIT); /* Stop search */
317     }
318     return 0; /* Keep searching */
319 }
320 
SigProcessKillSigHandler(LosTaskCB * tcb,void * arg)321 static int SigProcessKillSigHandler(LosTaskCB *tcb, void *arg)
322 {
323     struct ProcessSignalInfo *info = (struct ProcessSignalInfo *)arg;
324 
325     return OsPendingTaskWake(tcb, info->sigInfo->si_signo);
326 }
327 
SigProcessLoadTcb(struct ProcessSignalInfo * info,siginfo_t * sigInfo)328 static void SigProcessLoadTcb(struct ProcessSignalInfo *info, siginfo_t *sigInfo)
329 {
330     LosTaskCB *tcb = NULL;
331 
332     if (info->awakenedTcb == NULL && info->receivedTcb == NULL) {
333         if (info->unblockedTcb) {
334             tcb = info->unblockedTcb;
335         } else if (info->defaultTcb) {
336             tcb = info->defaultTcb;
337         } else {
338             return;
339         }
340         /* Deliver the signal to the selected task */
341         (void)OsTcbDispatch(tcb, sigInfo);
342     }
343 }
344 
OsSigProcessSend(LosProcessCB * spcb,siginfo_t * sigInfo)345 int OsSigProcessSend(LosProcessCB *spcb, siginfo_t *sigInfo)
346 {
347     int ret;
348     struct ProcessSignalInfo info = {
349         .sigInfo = sigInfo,
350         .defaultTcb = NULL,
351         .unblockedTcb = NULL,
352         .awakenedTcb = NULL,
353         .receivedTcb = NULL
354     };
355 
356     if (info.sigInfo == NULL) {
357         return -EFAULT;
358     }
359 
360     /* visit all taskcb and dispatch signal */
361     if (info.sigInfo->si_signo == SIGKILL) {
362         OsSigAddSet(&spcb->sigShare, info.sigInfo->si_signo);
363         (void)OsSigProcessForeachChild(spcb, SigProcessKillSigHandler, &info);
364         return 0;
365     } else {
366         ret = OsSigProcessForeachChild(spcb, SigProcessSignalHandler, &info);
367     }
368     if (ret < 0) {
369         return ret;
370     }
371     SigProcessLoadTcb(&info, sigInfo);
372     return 0;
373 }
374 
OsSigEmptySet(sigset_t * set)375 int OsSigEmptySet(sigset_t *set)
376 {
377     *set = NULL_SIGNAL_SET;
378     return 0;
379 }
380 
381 /* Privilege process can't send to kernel and privilege process */
OsSignalPermissionToCheck(const LosProcessCB * spcb)382 static int OsSignalPermissionToCheck(const LosProcessCB *spcb)
383 {
384     UINT32 gid = spcb->group->groupID;
385 
386     if (gid == OS_KERNEL_PROCESS_GROUP) {
387         return -EPERM;
388     } else if (gid == OS_USER_PRIVILEGE_PROCESS_GROUP) {
389         return -EPERM;
390     }
391 
392     return 0;
393 }
394 
OsDispatch(pid_t pid,siginfo_t * info,int permission)395 int OsDispatch(pid_t pid, siginfo_t *info, int permission)
396 {
397     if (OsProcessIDUserCheckInvalid(pid) || pid < 0) {
398         return -ESRCH;
399     }
400 
401     LosProcessCB *spcb = OS_PCB_FROM_PID(pid);
402     if (OsProcessIsUnused(spcb)) {
403         return -ESRCH;
404     }
405 
406     /* If the process you want to kill had been inactive, but still exist. should return LOS_OK */
407     if (OsProcessIsInactive(spcb)) {
408         return LOS_OK;
409     }
410 
411 #ifdef LOSCFG_SECURITY_CAPABILITY
412     LosProcessCB *current = OsCurrProcessGet();
413     /* Kernel process always has kill permission and user process should check permission */
414     if (OsProcessIsUserMode(current) && !(current->processStatus & OS_PROCESS_FLAG_EXIT)) {
415         if ((current != spcb) && (!IsCapPermit(CAP_KILL)) && (current->user->userID != spcb->user->userID)) {
416             return -EPERM;
417         }
418     }
419 #endif
420     if ((permission == OS_USER_KILL_PERMISSION) && (OsSignalPermissionToCheck(spcb) < 0)) {
421         return -EPERM;
422     }
423     return OsSigProcessSend(spcb, info);
424 }
425 
OsKill(pid_t pid,int sig,int permission)426 int OsKill(pid_t pid, int sig, int permission)
427 {
428     siginfo_t info;
429     int ret;
430 
431     /* Make sure that the para is valid */
432     if (!GOOD_SIGNO(sig)) {
433         return -EINVAL;
434     }
435 
436     /* Create the siginfo structure */
437     info.si_signo = sig;
438     info.si_code = SI_USER;
439     info.si_value.sival_ptr = NULL;
440 
441     if (pid > 0) {
442         /* Send the signal to the specify process */
443         ret = OsDispatch(pid, &info, permission);
444     } else if (pid == -1) {
445         /* Send SIG to all processes */
446         ret = OsSendSignalToAllProcess(&info, permission);
447     } else {
448         /* Send SIG to all processes in process group PGRP.
449            If PGRP is zero, send SIG to all processes in
450            the current process's process group. */
451         ret = OsSendSignalToProcessGroup(pid, &info, permission);
452     }
453     return ret;
454 }
455 
OsKillLock(pid_t pid,int sig)456 int OsKillLock(pid_t pid, int sig)
457 {
458     int ret;
459     unsigned int intSave;
460 
461     SCHEDULER_LOCK(intSave);
462     ret = OsKill(pid, sig, OS_USER_KILL_PERMISSION);
463     SCHEDULER_UNLOCK(intSave);
464     return ret;
465 }
466 
OsTaskKillUnsafe(UINT32 taskID,INT32 signo)467 INT32 OsTaskKillUnsafe(UINT32 taskID, INT32 signo)
468 {
469     siginfo_t info;
470     LosTaskCB *taskCB = OsGetTaskCB(taskID);
471     INT32 ret = OsUserTaskOperatePermissionsCheck(taskCB);
472     if (ret != LOS_OK) {
473         return -ret;
474     }
475 
476     /* Create the siginfo structure */
477     info.si_signo = signo;
478     info.si_code = SI_USER;
479     info.si_value.sival_ptr = NULL;
480 
481     /* Dispatch the signal to thread, bypassing normal task group thread
482      * dispatch rules. */
483     return OsTcbDispatch(taskCB, &info);
484 }
485 
OsPthreadKill(UINT32 tid,int signo)486 int OsPthreadKill(UINT32 tid, int signo)
487 {
488     int ret;
489     UINT32 intSave;
490 
491     /* Make sure that the signal is valid */
492     OS_RETURN_IF(!GOOD_SIGNO(signo), -EINVAL);
493     if (OS_TID_CHECK_INVALID(tid)) {
494         return -ESRCH;
495     }
496 
497     /* Keep things stationary through the following */
498     SCHEDULER_LOCK(intSave);
499     ret = OsTaskKillUnsafe(tid, signo);
500     SCHEDULER_UNLOCK(intSave);
501     return ret;
502 }
503 
OsSigAddSet(sigset_t * set,int signo)504 int OsSigAddSet(sigset_t *set, int signo)
505 {
506     /* Verify the signal */
507     if (!GOOD_SIGNO(signo)) {
508         return -EINVAL;
509     } else {
510         /* In musl, sig No bits 00000100 present sig No 3, but  1<< 3 = 00001000, so signo needs minus 1 */
511         signo -= 1;
512         /* Add the signal to the set */
513         *set |= SIGNO2SET((unsigned int)signo);
514         return LOS_OK;
515     }
516 }
517 
OsSigPending(sigset_t * set)518 int OsSigPending(sigset_t *set)
519 {
520     LosTaskCB *tcb = NULL;
521     unsigned int intSave;
522 
523     if (set == NULL) {
524         return -EFAULT;
525     }
526 
527     SCHEDULER_LOCK(intSave);
528     tcb = OsCurrTaskGet();
529     *set = tcb->sig.sigPendFlag;
530     SCHEDULER_UNLOCK(intSave);
531     return LOS_OK;
532 }
533 
FindFirstSetedBit(UINT64 n)534 STATIC int FindFirstSetedBit(UINT64 n)
535 {
536     int count;
537 
538     if (n == 0) {
539         return -1;
540     }
541     for (count = 0; (count < UINT64_BIT_SIZE) && (n ^ 1ULL); n >>= 1, count++) {}
542     return (count < UINT64_BIT_SIZE) ? count : (-1);
543 }
544 
OsSigTimedWaitNoLock(sigset_t * set,siginfo_t * info,unsigned int timeout)545 int OsSigTimedWaitNoLock(sigset_t *set, siginfo_t *info, unsigned int timeout)
546 {
547     LosTaskCB *task = NULL;
548     sig_cb *sigcb = NULL;
549     int ret;
550 
551     task = OsCurrTaskGet();
552     sigcb = &task->sig;
553 
554     if (sigcb->waitList.pstNext == NULL) {
555         LOS_ListInit(&sigcb->waitList);
556     }
557     /* If pendingflag & set > 0, should clear pending flag */
558     sigset_t clear = sigcb->sigPendFlag & *set;
559     if (clear) {
560         sigcb->sigPendFlag ^= clear;
561         ret = FindFirstSetedBit((UINT64)clear) + 1;
562         OsMoveTmpInfoToUnbInfo(sigcb, ret);
563     } else {
564         OsSigAddSet(set, SIGKILL);
565         OsSigAddSet(set, SIGSTOP);
566 
567         sigcb->sigwaitmask |= *set;
568         OsTaskWaitSetPendMask(OS_TASK_WAIT_SIGNAL, sigcb->sigwaitmask, timeout);
569         ret = task->ops->wait(task, &sigcb->waitList, timeout);
570         if (ret == LOS_ERRNO_TSK_TIMEOUT) {
571             ret = -EAGAIN;
572         }
573         sigcb->sigwaitmask = NULL_SIGNAL_SET;
574     }
575     if (info != NULL) {
576         (VOID)memcpy_s(info, sizeof(siginfo_t), &sigcb->sigunbinfo, sizeof(siginfo_t));
577     }
578     return ret;
579 }
580 
OsSigTimedWait(sigset_t * set,siginfo_t * info,unsigned int timeout)581 int OsSigTimedWait(sigset_t *set, siginfo_t *info, unsigned int timeout)
582 {
583     int ret;
584     unsigned int intSave;
585 
586     SCHEDULER_LOCK(intSave);
587 
588     ret = OsSigTimedWaitNoLock(set, info, timeout);
589 
590     SCHEDULER_UNLOCK(intSave);
591     return ret;
592 }
593 
OsPause(void)594 int OsPause(void)
595 {
596     LosTaskCB *spcb = NULL;
597     sigset_t oldSigprocmask;
598 
599     spcb = OsCurrTaskGet();
600     oldSigprocmask = spcb->sig.sigprocmask;
601     return OsSigSuspend(&oldSigprocmask);
602 }
603 
OsSigSuspend(const sigset_t * set)604 int OsSigSuspend(const sigset_t *set)
605 {
606     unsigned int intSave;
607     LosTaskCB *rtcb = NULL;
608     sigset_t setSuspend;
609     int ret;
610 
611     if (set == NULL) {
612         return -EINVAL;
613     }
614     SCHEDULER_LOCK(intSave);
615     rtcb = OsCurrTaskGet();
616 
617     /* Wait signal calc */
618     setSuspend = FULL_SIGNAL_SET & (~(*set));
619 
620     /* If pending mask not in sigmask, need set sigflag */
621     OsSigMaskSwitch(rtcb, *set);
622 
623     if (rtcb->sig.sigFlag > 0) {
624         SCHEDULER_UNLOCK(intSave);
625 
626         /*
627          * If rtcb->sig.sigFlag > 0, it means that some signal have been
628          * received, and we need to do schedule to handle the signal directly.
629          */
630         LOS_Schedule();
631         return -EINTR;
632     } else {
633         ret = OsSigTimedWaitNoLock(&setSuspend, NULL, LOS_WAIT_FOREVER);
634         if (ret < 0) {
635             PRINT_ERR("FUNC %s LINE = %d, ret = %x\n", __FUNCTION__, __LINE__, ret);
636         }
637     }
638 
639     SCHEDULER_UNLOCK(intSave);
640     return -EINTR;
641 }
642 
OsSigAction(int sig,const sigaction_t * act,sigaction_t * oact)643 int OsSigAction(int sig, const sigaction_t *act, sigaction_t *oact)
644 {
645     UINTPTR addr;
646     sigaction_t action;
647 
648     if (!GOOD_SIGNO(sig) || sig < 1 || act == NULL) {
649         return -EINVAL;
650     }
651     if (LOS_ArchCopyFromUser(&action, act, sizeof(sigaction_t)) != LOS_OK) {
652         return -EFAULT;
653     }
654 
655     if (sig == SIGSYS) {
656         addr = OsGetSigHandler();
657         if (addr == 0) {
658             OsSetSigHandler((unsigned long)(UINTPTR)action.sa_handler);
659             return LOS_OK;
660         }
661         return -EINVAL;
662     }
663 
664     return LOS_OK;
665 }
666 
OsSigIntLock(VOID)667 VOID OsSigIntLock(VOID)
668 {
669     LosTaskCB *task = OsCurrTaskGet();
670     sig_cb *sigcb = &task->sig;
671 
672     (VOID)LOS_AtomicAdd((Atomic *)&sigcb->sigIntLock, 1);
673 }
674 
OsSigIntUnlock(VOID)675 VOID OsSigIntUnlock(VOID)
676 {
677     LosTaskCB *task = OsCurrTaskGet();
678     sig_cb *sigcb = &task->sig;
679 
680     (VOID)LOS_AtomicSub((Atomic *)&sigcb->sigIntLock, 1);
681 }
682 
OsSaveSignalContext(VOID * sp,VOID * newSp)683 VOID *OsSaveSignalContext(VOID *sp, VOID *newSp)
684 {
685     UINTPTR sigHandler;
686     UINT32 intSave;
687 
688     LosTaskCB *task = OsCurrTaskGet();
689     LosProcessCB *process = OsCurrProcessGet();
690     sig_cb *sigcb = &task->sig;
691 
692     /* A thread is not allowed to interrupt the processing of its signals during a system call */
693     if (sigcb->sigIntLock > 0) {
694         return sp;
695     }
696 
697     if (OsTaskIsKilled(task)) {
698         OsRunningTaskToExit(task, 0);
699         return sp;
700     }
701 
702     SCHEDULER_LOCK(intSave);
703     if ((sigcb->count == 0) && ((sigcb->sigFlag != 0) || (process->sigShare != 0))) {
704         sigHandler = OsGetSigHandler();
705         if (sigHandler == 0) {
706             sigcb->sigFlag = 0;
707             process->sigShare = 0;
708             SCHEDULER_UNLOCK(intSave);
709             PRINT_ERR("The signal processing function for the current process pid =%d is NULL!\n", task->processID);
710             return sp;
711         }
712         /* One pthread do the share signal */
713         sigcb->sigFlag |= process->sigShare;
714         UINT32 signo = (UINT32)FindFirstSetedBit(sigcb->sigFlag) + 1;
715         UINT32 sigVal = (UINT32)(UINTPTR)(sigcb->sigunbinfo.si_value.sival_ptr);
716         OsMoveTmpInfoToUnbInfo(sigcb, signo);
717         OsProcessExitCodeSignalSet(process, signo);
718         sigcb->sigContext = sp;
719 
720         OsInitSignalContext(sp, newSp, sigHandler, signo, sigVal);
721 
722         /* sig No bits 00000100 present sig No 3, but  1<< 3 = 00001000, so signo needs minus 1 */
723         sigcb->sigFlag ^= 1ULL << (signo - 1);
724         sigcb->count++;
725         SCHEDULER_UNLOCK(intSave);
726         return newSp;
727     }
728 
729     SCHEDULER_UNLOCK(intSave);
730     return sp;
731 }
732 
OsRestorSignalContext(VOID * sp)733 VOID *OsRestorSignalContext(VOID *sp)
734 {
735     UINT32 intSave;
736 
737     LosTaskCB *task = OsCurrTaskGet();
738     sig_cb *sigcb = &task->sig;
739 
740     SCHEDULER_LOCK(intSave);
741     if (sigcb->count != 1) {
742         SCHEDULER_UNLOCK(intSave);
743         PRINT_ERR("sig error count : %d\n", sigcb->count);
744         return sp;
745     }
746 
747     LosProcessCB *process = OsCurrProcessGet();
748     VOID *saveContext = sigcb->sigContext;
749     sigcb->sigContext = NULL;
750     sigcb->count--;
751     process->sigShare = 0;
752     OsProcessExitCodeSignalClear(process);
753     SCHEDULER_UNLOCK(intSave);
754     return saveContext;
755 }
756 
757 #endif
758