• 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 #ifndef _LOS_SIGNAL_H
33 #define _LOS_SIGNAL_H
34 
35 #include <stddef.h>
36 #include <limits.h>
37 #include <sys/types.h>
38 #include <signal.h>
39 #include "los_event.h"
40 
41 #ifdef __cplusplus
42 #if __cplusplus
43 extern "C" {
44 #endif /* __cplusplus */
45 #endif /* __cplusplus */
46 
47 #define LOS_BIT_SET(val, bit) ((val) = (val) | (1ULL << (UINT32)(bit)))
48 #define LOS_BIT_CLR(val, bit) ((val) = (val) & ~(1ULL << (UINT32)(bit)))
49 #define LOS_IS_BIT_SET(val, bit) (bool)((((val) >> (UINT32)(bit)) & 1ULL))
50 
51 #define SIG_STOP_VISIT 1
52 
53 #define OS_KERNEL_KILL_PERMISSION 0U
54 #define OS_USER_KILL_PERMISSION   3U
55 
56 #define OS_RETURN_IF(expr, errcode) \
57     if ((expr)) {                   \
58         return errcode;             \
59     }
60 
61 #define OS_RETURN_IF_VOID(expr) \
62     if ((expr)) {               \
63         return;                 \
64     }
65 #define OS_GOTO_EXIT_IF(expr, errcode) \
66     if (expr) {                        \
67         ret = errcode;                 \
68         goto EXIT;                     \
69     }
70 #define OS_GOTO_EXIT_IF_ONLY(expr) \
71     if (expr) {                    \
72         goto EXIT;                 \
73     }
74 
75 #define OS_RETURN_VOID_IF_NULL(pPara) \
76     if (NULL == (pPara)) {            \
77         return;                       \
78     }
79 #define OS_RETURN_IF_NULL(pPara) \
80     if (NULL == (pPara)) {       \
81         return (-EINVAL);          \
82     }
83 
84 #define OS_GOTO_EXIT_IF_NULL(pPara) \
85     if (NULL == (pPara)) {          \
86         ret = -EINVAL;              \
87         goto EXIT;                  \
88     }
89 
90 typedef void (*sa_sighandler_t)(int);
91 typedef void (*sa_siginfoaction_t)(int, siginfo_t *, void *);
92 
93 #define SIGNO2SET(s) ((sigset_t)1ULL << (s))
94 #define NULL_SIGNAL_SET ((sigset_t)0ULL)
95 #define FULL_SIGNAL_SET ((sigset_t)~0ULL)
96 
GOOD_SIGNO(unsigned int sig)97 static inline int GOOD_SIGNO(unsigned int sig)
98 {
99     return (sig < _NSIG) ? 1 : 0;
100 }
101 
102 #define MAX_SIG_ARRAY_IN_MUSL 128
103 
104 typedef struct {
105     unsigned long sig[MAX_SIG_ARRAY_IN_MUSL / sizeof(unsigned long)];
106 } sigset_t_l;
107 
108 typedef struct sigaction sigaction_t;
109 
110 struct sigactq {
111     struct sigactq *flink; /* Forward link */
112     sigaction_t act;       /* Sigaction data */
113     uint8_t signo;         /* Signal associated with action */
114 };
115 typedef struct sigactq sigactq_t;
116 
117 struct sq_entry_s {
118     struct sq_entry_s *flink;
119 };
120 typedef struct sq_entry_s sq_entry_t;
121 
122 struct sigpendq {
123     struct sigpendq *flink; /* Forward link */
124     siginfo_t info;         /* Signal information */
125     uint8_t type;           /* (Used to manage allocations) */
126 };
127 typedef struct sigpendq sigpendq_t;
128 
129 struct sq_queue_s {
130     sq_entry_t *head;
131     sq_entry_t *tail;
132 };
133 typedef struct sq_queue_s sq_queue_t;
134 
135 typedef struct SigInfoListNode {
136     struct SigInfoListNode *next;
137     siginfo_t info;
138 } SigInfoListNode;
139 
140 typedef struct {
141     sigset_t sigFlag;
142     sigset_t sigPendFlag;
143     sigset_t sigprocmask; /* Signals that are blocked */
144     sq_queue_t sigactionq;
145     LOS_DL_LIST waitList;
146     sigset_t sigwaitmask; /* Waiting for pending signals */
147     siginfo_t sigunbinfo; /* Signal info when task unblocked */
148     SigInfoListNode *tmpInfoListHead; /* Signal info List */
149     unsigned int sigIntLock;
150     void *sigContext;
151     unsigned int count;
152 } sig_cb;
153 
154 #define SIGEV_THREAD_ID 4
155 
156 int sys_sigqueue(pid_t, int, const union sigval);
157 int sys_sigpending(sigset_t *);
158 int sys_rt_sigtimedwait(const sigset_t *mask, siginfo_t *si, const struct timespec *ts, size_t sigsetsize);
159 int sys_sigsuspend(const sigset_t *);
160 int OsKillLock(pid_t pid, int sig);
161 int OsSigAction(int sig, const sigaction_t *act, sigaction_t *oact);
162 int OsSigprocMask(int how, const sigset_t_l *set, sigset_t_l *oldset);
163 int OsPthreadKill(UINT32 tid, int signo);
164 int OsSigEmptySet(sigset_t *);
165 int OsSigAddSet(sigset_t *, int);
166 int OsSigIsMember(const sigset_t *, int);
167 int OsKill(pid_t pid, int sig, int permission);
168 int OsDispatch(pid_t pid, siginfo_t *info, int permission);
169 int OsSigTimedWait(sigset_t *set, siginfo_t *info, unsigned int timeout);
170 int OsPause(void);
171 int OsSigPending(sigset_t *set);
172 int OsSigSuspend(const sigset_t *set);
173 VOID OsSigIntLock(VOID);
174 VOID OsSigIntUnlock(VOID);
175 INT32 OsTaskKillUnsafe(UINT32 taskID, INT32 signo);
176 VOID OsClearSigInfoTmpList(sig_cb *sigcb);
177 
178 #ifdef __cplusplus
179 #if __cplusplus
180 }
181 #endif /* __cplusplus */
182 #endif /* __cplusplus */
183 
184 #endif /* _LOS_SIGNAL_H */
185