更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_signal.h
浏览该文件的文档.
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
43extern "C" {
44#endif /* __cplusplus */
45#endif /* __cplusplus */
46/**
47 * @file los_signal.h
48 * @brief
49 * @verbatim
50 信号是Linux系统中用于进程间互相通信或者操作的一种机制,信号可以在任何时候发给某一进程,而无需知道该进程的状态。
51 如果该进程当前并未处于执行状态,则该信号就由内核保存起来,直到该进程被调度执行并传递给它为止。
52 如果一个信号被进程设置为阻塞,则该信号的传递被延迟,直到其阻塞被取消时才被传递给进程。
53
54 软中断信号(signal,又简称为信号)用来通知进程发生了异步事件。在软件层次上是对中断机制的一种模拟,
55 在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的。信号是进程间通信机制中异步通信机制,
56 一个进程不必通过任何操作来等待信号的到达,事实上,进程也不知道信号到底什么时候到达。
57 进程之间可以互相通过系统调用kill发送软中断信号。内核也可以因为内部事件而给进程发送信号,通知进程
58 发生了某个事件。信号机制除了基本通知功能外,还可以传递附加信息。
59
60 信号量定义如下: 见于..\third_party\musl\arch\aarch64\bits\signal.h
61 #define SIGHUP 1 //终端挂起或者控制进程终止
62 #define SIGINT 2 //键盘中断(如break键被按下)
63 #define SIGQUIT 3 //键盘的退出键被按下
64 #define SIGILL 4 //非法指令
65 #define SIGTRAP 5 //跟踪陷阱(trace trap),启动进程,跟踪代码的执行
66 #define SIGABRT 6 //由abort(3)发出的退出指令
67 #define SIGIOT SIGABRT //abort发出的信号
68 #define SIGBUS 7 //总线错误
69 #define SIGFPE 8 //浮点异常
70 #define SIGKILL 9 //常用的命令 kill 9 123 | 不能被忽略、处理和阻塞
71 #define SIGUSR1 10 //用户自定义信号1
72 #define SIGSEGV 11 //无效的内存引用, 段违例(segmentation violation),进程试图去访问其虚地址空间以外的位置
73 #define SIGUSR2 12 //用户自定义信号2
74 #define SIGPIPE 13 //向某个非读管道中写入数据
75 #define SIGALRM 14 //由alarm(2)发出的信号,默认行为为进程终止
76 #define SIGTERM 15 //终止信号, kill不带参数时默认发送这个信号
77 #define SIGSTKFLT 16 //栈溢出
78 #define SIGCHLD 17 //子进程结束信号
79 #define SIGCONT 18 //进程继续(曾被停止的进程)
80 #define SIGSTOP 19 //终止进程 | 不能被忽略、处理和阻塞
81 #define SIGTSTP 20 //控制终端(tty)上 按下停止键
82 #define SIGTTIN 21 //进程停止,后台进程企图从控制终端读
83 #define SIGTTOU 22 //进程停止,后台进程企图从控制终端写
84 #define SIGURG 23 //I/O有紧急数据到达当前进程
85 #define SIGXCPU 24 //进程的CPU时间片到期
86 #define SIGXFSZ 25 //文件大小的超出上限
87 #define SIGVTALRM 26 //虚拟时钟超时
88 #define SIGPROF 27 //profile时钟超时
89 #define SIGWINCH 28 //窗口大小改变
90 #define SIGIO 29 //I/O相关
91 #define SIGPOLL 29 //
92 #define SIGPWR 30 //电源故障,关机
93 #define SIGSYS 31 //系统调用中参数错,如系统调用号非法
94 #define SIGUNUSED SIGSYS //系统调用异常
95
96 #define _NSIG 65 //信号范围,不超过_NSIG
97
98 https://www.cnblogs.com/hoys/archive/2012/08/19/2646377.html
99
100 Musl官网 http://musl.libc.org/
101 musl是构建在Linux系统调用API之上的C标准库的实现,包括在基本语言标准POSIX中定义的接口,
102 以及广泛认可的扩展。musl是轻量级的,快速的,简单的,自由的.
103 * @endverbatim
104 * @param pathname
105 * @return int
106 */
107
108#define LOS_BIT_SET(val, bit) ((val) = (val) | (1ULL << (UINT32)(bit))) ///< 按位设置
109#define LOS_BIT_CLR(val, bit) ((val) = (val) & ~(1ULL << (UINT32)(bit))) ///< 按位清除
110#define LOS_IS_BIT_SET(val, bit) (bool)((((val) >> (UINT32)(bit)) & 1ULL)) ///< 位是否设置为1
111
112#define SIG_STOP_VISIT 1
113
114#define OS_KERNEL_KILL_PERMISSION 0U ///< 内核态 kill 权限
115#define OS_USER_KILL_PERMISSION 3U ///< 用户态 kill 权限
116
117#define OS_RETURN_IF(expr, errcode) \
118 if ((expr)) { \
119 return errcode; \
120 }
121
122#define OS_RETURN_IF_VOID(expr) \
123 if ((expr)) { \
124 return; \
125 }
126#define OS_GOTO_EXIT_IF(expr, errcode) \
127 if (expr) { \
128 ret = errcode; \
129 goto EXIT; \
130 }
131#define OS_GOTO_EXIT_IF_ONLY(expr) \
132 if (expr) { \
133 goto EXIT; \
134 }
135
136#define OS_RETURN_VOID_IF_NULL(pPara) \
137 if (NULL == (pPara)) { \
138 return; \
139 }
140#define OS_RETURN_IF_NULL(pPara) \
141 if (NULL == (pPara)) { \
142 return (-EINVAL); \
143 }
144
145#define OS_GOTO_EXIT_IF_NULL(pPara) \
146 if (NULL == (pPara)) { \
147 ret = -EINVAL; \
148 goto EXIT; \
149 }
150
152typedef void (*sa_siginfoaction_t)(int, siginfo_t *, void *);
153
154#define SIGNO2SET(s) ((sigset_t)1ULL << (s))
155#define NULL_SIGNAL_SET ((sigset_t)0ULL) ///< 信号集全部清0
156#define FULL_SIGNAL_SET ((sigset_t)~0ULL) ///< 信号集全部置1
157///信号ID是否有效
158static inline int GOOD_SIGNO(unsigned int sig)
159{
160 return (sig < _NSIG) ? 1 : 0;//
161}
162
163#define MAX_SIG_ARRAY_IN_MUSL 128 ///< 128个信号
164
165typedef struct {
166 unsigned long sig[MAX_SIG_ARRAY_IN_MUSL / sizeof(unsigned long)];
167} sigset_t_l;
168
169/***************************************************
170struct sigaction {
171 union {
172 void (*sa_handler)(int); //信号处理函数——普通版
173 void (*sa_sigaction)(int, siginfo_t *, void *);//信号处理函数——高级版
174 } __sa_handler;
175 sigset_t sa_mask;//指定信号处理程序执行过程中需要阻塞的信号;
176 int sa_flags; //标示位
177 // SA_RESTART:使被信号打断的syscall重新发起。
178 // SA_NOCLDSTOP:使父进程在它的子进程暂停或继续运行时不会收到 SIGCHLD 信号。
179 // SA_NOCLDWAIT:使父进程在它的子进程退出时不会收到SIGCHLD信号,这时子进程如果退出也不会成为僵 尸进程。
180 // SA_NODEFER:使对信号的屏蔽无效,即在信号处理函数执行期间仍能发出这个信号。
181 // SA_RESETHAND:信号处理之后重新设置为默认的处理方式。
182 // SA_SIGINFO:使用sa_sigaction成员而不是sa_handler作为信号处理函数。
183 void (*sa_restorer)(void);
184};
185****************************************************/
186typedef struct sigaction sigaction_t;
187
188struct sigactq {
189 struct sigactq *flink; /* Forward link */
190 sigaction_t act; /* Sigaction data */
191 uint8_t signo; /* Signal associated with action */
192};
193typedef struct sigactq sigactq_t;
194
197};
198typedef struct sq_entry_s sq_entry_t;
199
200struct sigpendq {
201 struct sigpendq *flink; /* Forward link */
202 siginfo_t info; /* Signal information */
203 uint8_t type; /* (Used to manage allocations) */
204};
205typedef struct sigpendq sigpendq_t;
206
207struct sq_queue_s {//信号队列
210};
211typedef struct sq_queue_s sq_queue_t;
212
213typedef struct SigInfoListNode {
215 siginfo_t info;
217/**
218 * @brief 信号控制块(描述符)
219 */
220typedef struct {
221 sigset_t sigFlag; ///< 不屏蔽的信号集
222 sigset_t sigPendFlag; ///< 信号阻塞标签集,记录那些信号来过,任务依然阻塞的集合.即:这些信号不能唤醒任务
223 sigset_t sigprocmask; ///< Signals that are blocked | 任务屏蔽了哪些信号
224 sq_queue_t sigactionq; ///< 信号捕捉队列
225 LOS_DL_LIST waitList; ///< 待链表,上面挂的是等待信号到来的任务, 请查找 OsTaskWait(&sigcb->waitList, timeout, TRUE) 理解
226 sigset_t sigwaitmask; /*! Waiting for pending signals | 任务在等待哪些信号的到来 */
227 siginfo_t sigunbinfo; /*! Signal info when task unblocked | 任务解锁时的信号信息 */
228 SigInfoListNode *tmpInfoListHead; /*! Signal info List */
229 unsigned int sigIntLock;///< 信号中断锁
230 void *sigContext; ///< 信号上下文
231 unsigned int count;///< 信号数量
232} sig_cb;
233
234#define SIGEV_THREAD_ID 4
235
236int sys_sigqueue(pid_t, int, const union sigval);
237int sys_sigpending(sigset_t *);
238int sys_rt_sigtimedwait(const sigset_t *mask, siginfo_t *si, const struct timespec *ts, size_t sigsetsize);
239int sys_sigsuspend(const sigset_t *);
240int OsKillLock(pid_t pid, int sig);
241int OsSigAction(int sig, const sigaction_t *act, sigaction_t *oact);
242int OsSigprocMask(int how, const sigset_t_l *set, sigset_t_l *oldset);
243int OsPthreadKill(UINT32 tid, int signo);
244int OsSigEmptySet(sigset_t *);
245int OsSigAddSet(sigset_t *, int);
246int OsSigIsMember(const sigset_t *, int);
247int OsKill(pid_t pid, int sig, int permission);
248int OsDispatch(pid_t pid, siginfo_t *info, int permission);
249int OsSigTimedWait(sigset_t *set, siginfo_t *info, unsigned int timeout);
250int OsPause(void);
251int OsSigPending(sigset_t *set);
252int OsSigSuspend(const sigset_t *set);
253VOID OsSigIntLock(VOID);
254VOID OsSigIntUnlock(VOID);
255INT32 OsTaskKillUnsafe(UINT32 taskID, INT32 signo);
256VOID OsClearSigInfoTmpList(sig_cb *sigcb);
257
258#ifdef __cplusplus
259#if __cplusplus
260}
261#endif /* __cplusplus */
262#endif /* __cplusplus */
263
264#endif /* _LOS_SIGNAL_H */
struct SigInfoListNode SigInfoListNode
int OsPause(void)
通过信号挂起当前任务
Definition: los_signal.c:617
int sys_sigpending(sigset_t *)
int OsSigAction(int sig, const sigaction_t *act, sigaction_t *oact)
Definition: los_signal.c:677
int OsSigprocMask(int how, const sigset_t_l *set, sigset_t_l *oldset)
Definition: los_signal.c:233
VOID OsSigIntUnlock(VOID)
Definition: los_signal.c:712
VOID OsClearSigInfoTmpList(sig_cb *sigcb)
Definition: los_signal.c:123
void(* sa_sighandler_t)(int)
Definition: los_signal.h:151
int OsSigAddSet(sigset_t *, int)
向信号集中加入signo信号
Definition: los_signal.c:527
int OsPthreadKill(UINT32 tid, int signo)
发送信号
Definition: los_signal.c:509
int sys_rt_sigtimedwait(const sigset_t *mask, siginfo_t *si, const struct timespec *ts, size_t sigsetsize)
int OsSigIsMember(const sigset_t *, int)
判定信号signo是否存在信号集中。如果信号集里已有该信号则返回1,否则返回0。如果有错误则返回-1
Definition: los_signal.c:62
int OsDispatch(pid_t pid, siginfo_t *info, int permission)
信号分发,发送信号权限/进程组过滤.
Definition: los_signal.c:412
int OsKillLock(pid_t pid, int sig)
给发送信号过程加锁
Definition: los_signal.c:480
int OsSigPending(sigset_t *set)
获取阻塞当前任务的信号集
Definition: los_signal.c:541
struct sigaction sigaction_t
Definition: los_signal.h:186
VOID OsSigIntLock(VOID)
Definition: los_signal.c:704
int sys_sigqueue(pid_t, int, const union sigval)
int OsKill(pid_t pid, int sig, int permission)
Definition: los_signal.c:450
int OsSigEmptySet(sigset_t *)
信号集全部清0
Definition: los_signal.c:392
int OsSigSuspend(const sigset_t *set)
用参数set代替进程的原有掩码,并暂停进程执行,直到收到信号再恢复原有掩码并继续执行进程。
Definition: los_signal.c:627
void(* sa_siginfoaction_t)(int, siginfo_t *, void *)
Definition: los_signal.h:152
INT32 OsTaskKillUnsafe(UINT32 taskID, INT32 signo)
Definition: los_signal.c:490
int OsSigTimedWait(sigset_t *set, siginfo_t *info, unsigned int timeout)
让当前任务等待的信号
Definition: los_signal.c:604
static int GOOD_SIGNO(unsigned int sig)
信号ID是否有效
Definition: los_signal.h:158
int sys_sigsuspend(const sigset_t *)
signed int INT32
Definition: los_typedef.h:60
unsigned int UINT32
Definition: los_typedef.h:57
siginfo_t info
Definition: los_signal.h:215
struct SigInfoListNode * next
Definition: los_signal.h:214
信号控制块(描述符)
Definition: los_signal.h:220
unsigned int sigIntLock
信号中断锁
Definition: los_signal.h:229
unsigned int count
信号数量
Definition: los_signal.h:231
sq_queue_t sigactionq
信号捕捉队列
Definition: los_signal.h:224
SigInfoListNode * tmpInfoListHead
Definition: los_signal.h:228
sigset_t sigwaitmask
Definition: los_signal.h:226
sigset_t sigprocmask
Signals that are blocked | 任务屏蔽了哪些信号
Definition: los_signal.h:223
LOS_DL_LIST waitList
待链表,上面挂的是等待信号到来的任务, 请查找 OsTaskWait(&sigcb->waitList, timeout, TRUE) 理解
Definition: los_signal.h:225
sigset_t sigPendFlag
信号阻塞标签集,记录那些信号来过,任务依然阻塞的集合.即:这些信号不能唤醒任务
Definition: los_signal.h:222
siginfo_t sigunbinfo
Definition: los_signal.h:227
void * sigContext
信号上下文
Definition: los_signal.h:230
sigset_t sigFlag
不屏蔽的信号集
Definition: los_signal.h:221
uint8_t signo
Definition: los_signal.h:191
struct sigactq * flink
Definition: los_signal.h:189
sigaction_t act
Definition: los_signal.h:190
siginfo_t info
Definition: los_signal.h:202
uint8_t type
Definition: los_signal.h:203
struct sigpendq * flink
Definition: los_signal.h:201
Definition: los_signal.h:195
struct sq_entry_s * flink
Definition: los_signal.h:196
sq_entry_t * tail
Definition: los_signal.h:209
sq_entry_t * head
Definition: los_signal.h:208
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 ARG_NUM_0 ARG_NUM_2 ARG_NUM_1 ARG_NUM_2 ARG_NUM_3 ARG_NUM_7 ARG_NUM_2 ARG_NUM_3 ARG_NUM_2 ARG_NUM_4 ARG_NUM_5 ARG_NUM_6 ARG_NUM_3 ARG_NUM_5 ARG_NUM_7 ARG_NUM_1 ARG_NUM_4 ARG_NUM_5 ARG_NUM_4 ARG_NUM_7 ARG_NUM_2 ARG_NUM_3 ARG_NUM_7 ARG_NUM_7 ARG_NUM_3 ARG_NUM_3 ARG_NUM_3 ARG_NUM_7 ARG_NUM_3 ARG_NUM_2 char ARG_NUM_2 ARG_NUM_1 ARG_NUM_0 ARG_NUM_0 ARG_NUM_3 void ARG_NUM_1 ARG_NUM_0 unsigned ARG_NUM_0 ARG_NUM_2 ARG_NUM_3 ARG_NUM_2 ARG_NUM_5 ARG_NUM_3 ARG_NUM_3 ARG_NUM_4 ARG_NUM_1 ARG_NUM_1 ARG_NUM_3 ARG_NUM_2 ARG_NUM_1 ARG_NUM_4 ARG_NUM_4 ARG_NUM_5 ARG_NUM_3 ARG_NUM_2 void ARG_NUM_6 unsigned ARG_NUM_0 unsigned ARG_NUM_0 ARG_NUM_3 ARG_NUM_3 ARG_NUM_2 ARG_NUM_2 ARG_NUM_1 ARG_NUM_2 ARG_NUM_1 char ARG_NUM_0 ARG_NUM_4 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_4 ARG_NUM_5 ARG_NUM_2 ARG_NUM_3 ARG_NUM_3 ARG_NUM_3 ARG_NUM_3 ARG_NUM_6 ARG_NUM_6 ARG_NUM_5 ARG_NUM_3 void ARG_NUM_3 ARG_NUM_3 ARG_NUM_5 ARG_NUM_1 unsigned ARG_NUM_3 long
ARG_NUM_3 int
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void