1 /*
2  * Copyright (c) 2006-2022, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2017-09-12     Bernard      The first version
9  * 2021-07-21     Meco Man     move to libc/common
10  */
11 
12 #ifndef __SYS_SIGNAL_H__
13 #define __SYS_SIGNAL_H__
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif /* __cplusplus */
18 
19 #ifdef RT_USING_MUSLLIBC
20 /* this is required for musl <signal.h> */
21 #ifndef _POSIX_SOURCE
22 #define _POSIX_SOURCE
23 #include <signal.h>
24 /* limiting influence of _POSIX_SOURCE */
25 #undef _POSIX_SOURCE
26 
27 #else /* ndef _POSIX_SOURCE */
28 #include <signal.h>
29 #endif
30 
31 #else
32 
33 #include <stdint.h>
34 #include <sys/types.h>
35 
36 /* sigev_notify values
37    NOTE: P1003.1c/D10, p. 34 adds SIGEV_THREAD.  */
38 
39 #define SIGEV_NONE   1  /* No asynchronous notification shall be delivered */
40                         /*   when the event of interest occurs. */
41 #define SIGEV_SIGNAL 0  /* A queued signal, with an application defined */
42                         /*  value, shall be delivered when the event of */
43                         /*  interest occurs. */
44 #define SIGEV_THREAD 2  /* A notification function shall be called to */
45                         /*   perform notification. */
46 
47 /*  Signal Generation and Delivery, P1003.1b-1993, p. 63
48     NOTE: P1003.1c/D10, p. 34 adds sigev_notify_function and
49           sigev_notify_attributes to the sigevent structure.  */
50 union sigval
51 {
52     int    sival_int;    /* Integer signal value */
53     void  *sival_ptr;    /* Pointer signal value */
54 };
55 
56 struct sigevent
57 {
58     union sigval sigev_value;
59     int          sigev_signo;                /* Signal number */
60     int          sigev_notify;               /* Notification type */
61     void         (*sigev_notify_function)( union sigval );
62                                              /* Notification function */
63     void         *sigev_notify_attributes;   /* Notification Attributes, really pthread_attr_t */
64 };
65 
66 struct siginfo
67 {
68     uint16_t si_signo;
69     uint16_t si_code;
70     union sigval si_value;
71 };
72 typedef struct siginfo siginfo_t;
73 
74 #define SI_USER     0x01    /* Signal sent by kill(). */
75 #define SI_QUEUE    0x02    /* Signal sent by sigqueue(). */
76 #define SI_TIMER    0x03    /* Signal generated by expiration of a timer set by timer_settime(). */
77 #define SI_ASYNCIO  0x04    /* Signal generated by completion of an asynchronous I/O request. */
78 #define SI_MESGQ    0x05    /* Signal generated by arrival of a message on an empty message queue. */
79 
80 typedef void (*_sig_func_ptr)(int);
81 typedef unsigned long sigset_t;
82 
83 struct sigaction
84 {
85     _sig_func_ptr sa_handler;
86     sigset_t sa_mask;
87     int sa_flags;
88 };
89 
90 /*
91  * Structure used in sigaltstack call.
92  */
93 typedef struct sigaltstack
94 {
95   void     *ss_sp;    /* Stack base or pointer.  */
96   int       ss_flags; /* Flags.  */
97   size_t    ss_size;  /* Stack size.  */
98 } stack_t;
99 
100 #define SIG_SETMASK 0   /* set mask with sigprocmask() */
101 #define SIG_BLOCK   1   /* set of signals to block */
102 #define SIG_UNBLOCK 2   /* set of signals to, well, unblock */
103 
104 #define sigaddset(what,sig) (*(what) |= (1<<(sig)), 0)
105 #define sigdelset(what,sig) (*(what) &= ~(1<<(sig)), 0)
106 #define sigemptyset(what)   (*(what) = 0, 0)
107 #define sigfillset(what)    (*(what) = ~(0), 0)
108 #define sigismember(what,sig) (((*(what)) & (1<<(sig))) != 0)
109 
110 int sigprocmask (int how, const sigset_t *set, sigset_t *oset);
111 int sigpending (sigset_t *set);
112 int sigsuspend (const sigset_t *set);
113 
114 #include "time.h"
115 int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout);
116 int sigwait(const sigset_t *set, int *sig);
117 int sigwaitinfo(const sigset_t *set, siginfo_t *info);
118 int raise(int sig);
119 int sigqueue(pid_t pid, int signo, const union sigval value);
120 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
121 
122 #ifdef __ARMCC_VERSION
123 #define SIGHUP       1
124 /* #define SIGINT       2 */
125 #define SIGQUIT      3
126 /* #define SIGILL       4 */
127 #define SIGTRAP      5
128 /* #define SIGABRT      6 */
129 #define SIGEMT       7
130 /* #define SIGFPE       8 */
131 #define SIGKILL      9
132 #define SIGBUS      10
133 /* #define SIGSEGV     11 */
134 #define SIGSYS      12
135 #define SIGPIPE     13
136 #define SIGALRM     14
137 /* #define SIGTERM     15 */
138 #define SIGURG      16
139 #define SIGSTOP     17
140 #define SIGTSTP     18
141 #define SIGCONT     19
142 #define SIGCHLD     20
143 #define SIGTTIN     21
144 #define SIGTTOU     22
145 #define SIGPOLL     23
146 #define SIGWINCH    24
147 #define SIGXCPU     24  /* exceeded CPU time limit */
148 #define SIGXFSZ     25  /* exceeded file size limit */
149 #define SIGVTALRM   26  /* virtual time alarm */
150 /* #define SIGUSR1     25 */
151 /* #define SIGUSR2     26 */
152 #define SIGRTMIN    27
153 #define SIGRTMAX    31
154 #define NSIG        32
155 
156 #elif defined(__IAR_SYSTEMS_ICC__)
157 #define SIGHUP       1
158 #define SIGINT       2
159 #define SIGQUIT      3
160 #define SIGILL       4
161 #define SIGTRAP      5
162 /* #define SIGABRT      6 */
163 #define SIGEMT       7
164 #define SIGFPE       8
165 #define SIGKILL      9
166 #define SIGBUS      10
167 #define SIGSEGV     11
168 #define SIGSYS      12
169 #define SIGPIPE     13
170 #define SIGALRM     14
171 #define SIGTERM     15
172 #define SIGURG      16
173 #define SIGSTOP     17
174 #define SIGTSTP     18
175 #define SIGCONT     19
176 #define SIGCHLD     20
177 #define SIGTTIN     21
178 #define SIGTTOU     22
179 #define SIGPOLL     23
180 #define SIGWINCH    24
181 #define SIGXCPU     24  /* exceeded CPU time limit */
182 #define SIGXFSZ     25  /* exceeded file size limit */
183 #define SIGVTALRM   26  /* virtual time alarm */
184 #define SIGUSR1     25
185 #define SIGUSR2     26
186 #define SIGRTMIN    27
187 #define SIGRTMAX    31
188 #define NSIG        32
189 
190 #elif defined(__GNUC__)
191 #define SIGHUP  1   /* hangup */
192 #define SIGINT  2   /* interrupt */
193 #define SIGQUIT 3   /* quit */
194 #define SIGILL  4   /* illegal instruction (not reset when caught) */
195 #define SIGTRAP 5   /* trace trap (not reset when caught) */
196 #define SIGIOT  6   /* IOT instruction */
197 #define SIGABRT 6   /* used by abort, replace SIGIOT in the future */
198 #define SIGEMT  7   /* EMT instruction */
199 #define SIGFPE  8   /* floating point exception */
200 #define SIGKILL 9   /* kill (cannot be caught or ignored) */
201 #define SIGBUS  10  /* bus error */
202 #define SIGSEGV 11  /* segmentation violation */
203 #define SIGSYS  12  /* bad argument to system call */
204 #define SIGPIPE 13  /* write on a pipe with no one to read it */
205 #define SIGALRM 14  /* alarm clock */
206 #define SIGTERM 15  /* software termination signal from kill */
207 #define SIGURG  16  /* urgent condition on IO channel */
208 #define SIGSTOP 17  /* sendable stop signal not from tty */
209 #define SIGTSTP 18  /* stop signal from tty */
210 #define SIGCONT 19  /* continue a stopped process */
211 #define SIGCHLD 20  /* to parent on child stop or exit */
212 #define SIGCLD  20  /* System V name for SIGCHLD */
213 #define SIGTTIN 21  /* to readers pgrp upon background tty read */
214 #define SIGTTOU 22  /* like TTIN for output if (tp->t_local&LTOSTOP) */
215 #define SIGIO   23  /* input/output possible signal */
216 #define SIGPOLL SIGIO   /* System V name for SIGIO */
217 #define SIGXCPU 24  /* exceeded CPU time limit */
218 #define SIGXFSZ 25  /* exceeded file size limit */
219 #define SIGVTALRM 26    /* virtual time alarm */
220 #define SIGPROF 27  /* profiling time alarm */
221 #define SIGWINCH 28 /* window changed */
222 #define SIGLOST 29  /* resource lost (eg, record-lock lost) */
223 #define SIGUSR1 30  /* user defined signal 1 */
224 #define SIGUSR2 31  /* user defined signal 2 */
225 #define NSIG    32      /* signal 0 implied */
226 #endif /* __ARMCC_VERSION */
227 
228 /* Some applications take advantage of the fact that <sys/signal.h>
229  * and <signal.h> are equivalent in glibc.  Allow for that here.  */
230 #include <signal.h>
231 
232 #ifndef SIG_ERR
233 #define SIG_ERR  ((void (*)(int))-1)
234 #endif
235 
236 #ifndef SIG_DFL
237 #define SIG_DFL  ((void (*)(int)) 0)
238 #endif
239 
240 #ifndef SIG_IGN
241 #define SIG_IGN  ((void (*)(int)) 1)
242 #endif
243 
244 #endif /* RT_USING_MUSLLIBC */
245 
246 #ifdef __cplusplus
247 }
248 #endif /* __cplusplus */
249 
250 #endif /* __SYS_SIGNAL_H__ */
251 
252