xref: /haiku/headers/posix/signal.h (revision db10640de90f7f9519ba2da9577b7c1af3c64f6b)
1 #ifndef _SIGNAL_H_
2 #define _SIGNAL_H_
3 /*
4 ** Distributed under the terms of the OpenBeOS License.
5 */
6 
7 
8 #include <sys/types.h>
9 
10 
11 typedef int	sig_atomic_t;
12 typedef long sigset_t;
13 
14 typedef void (*sig_func_t)(int);
15 typedef void (*__signal_func_ptr)(int);  /* deprecated, for compatibility with BeOS only */
16 
17 
18 // ToDo: actually make use of this structure!
19 typedef struct {
20 	int		si_signo;	/* signal number */
21 	int		si_code;	/* signal code */
22 	int		si_errno;	/* if non zero, an error number associated with this signal */
23 	pid_t	si_pid;		/* sending process ID */
24 	uid_t	si_uid;		/* real user ID of sending process */
25 	void	*si_addr;	/* address of faulting instruction */
26 	int		si_status;	/* exit value or signal */
27 	long	si_band;	/* band event for SIGPOLL */
28 } siginfo_t;
29 
30 
31 /*
32  * macros defining the standard signal handling behavior
33  */
34 #define SIG_DFL	((sig_func_t) 0)   /* the signal was treated in the "default" manner */
35 #define SIG_IGN	((sig_func_t) 1)   /* the signal was ignored */
36 #define SIG_ERR	((sig_func_t)-1)   /* an error ocurred during signal processing */
37 
38 
39 /*
40  * structure used by sigaction()
41  *
42  * Note: the 'sa_userdata' field is a non-Posix extension.
43  * See the SPECIAL NOTES below for an explanation of this.
44  *
45  */
46 struct sigaction {
47 	sig_func_t	sa_handler;
48 	sigset_t	sa_mask;
49 	int			sa_flags;
50 	void		*sa_userdata;  /* will be passed to the signal handler */
51 };
52 
53 
54 /*
55  * values for sa_flags
56  */
57 #define SA_NOCLDSTOP  0x01
58 #define SA_ONESHOT    0x02
59 #define SA_NOMASK     0x04
60 #define SA_NODEFER    SA_NOMASK
61 #define SA_RESTART    0x08
62 #define SA_STACK      0x10
63 
64 
65 /*
66  * for signals using an alternate stack
67  */
68 typedef struct stack_t {
69 	void	*ss_sp;
70 	size_t	ss_size;
71 	int		ss_flags;
72 } stack_t;
73 
74 
75 /*
76  * for the 'how' arg of sigprocmask()
77  */
78 #define SIG_BLOCK    1
79 #define SIG_UNBLOCK  2
80 #define SIG_SETMASK  3
81 
82 
83 /*
84  * The list of all defined signals:
85  *
86  * The numbering of signals for OpenBeOS attempts to maintain
87  * some consistency with UN*X conventions so that things
88  * like "kill -9" do what you expect.
89  */
90 #define	SIGHUP      1      /* hangup -- tty is gone! */
91 #define SIGINT      2      /* interrupt */
92 #define SIGQUIT     3      /* `quit' special character typed in tty  */
93 #define SIGILL	    4      /* illegal instruction */
94 #define SIGCHLD     5      /* child process exited */
95 #define SIGABRT	    6      /* abort() called, dont' catch */
96 #define SIGPIPE	    7      /* write to a pipe w/no readers */
97 #define SIGFPE	    8      /* floating point exception */
98 #define SIGKILL	    9      /* kill a team (not catchable) */
99 #define SIGSTOP	   10      /* suspend a thread (not catchable) */
100 #define SIGSEGV	   11      /* segmentation violation (read: invalid pointer) */
101 #define SIGCONT    12      /* continue execution if suspended */
102 #define SIGTSTP	   13      /* `stop' special character typed in tty */
103 #define SIGALRM    14      /* an alarm has gone off (see alarm()) */
104 #define SIGTERM	   15      /* termination requested */
105 #define SIGTTIN	   16      /* read of tty from bg process */
106 #define SIGTTOU	   17      /* write to tty from bg process */
107 #define SIGUSR1	   18      /* app defined signal 1 */
108 #define SIGUSR2	   19      /* app defined signal 2 */
109 #define SIGWINCH   20      /* tty window size changed */
110 #define SIGKILLTHR 21      /* be specific: kill just the thread, not team */
111 #define SIGTRAP	   22
112 
113 #define SIGBUS     SIGSEGV /* for old style code */
114 
115 
116 /*
117  * Signal numbers 23-32 are currently free but may be used in future
118  * releases.  Use them at your own peril (if you do use them, at least
119  * be smart and use them backwards from signal 32).
120  */
121 #define MAX_SIGNO     32       /* the most signals that a single thread can reference */
122 #define __signal_max  22       /* the largest signal number that is actually defined */
123 #define NSIG (__signal_max+1)  /* the number of defined signals */
124 
125 
126 /* the global table of text strings containing descriptions for each signal */
127 extern const char * const sys_siglist[NSIG];
128 
129 
130 #ifdef __cplusplus
131 extern "C" {
132 #endif
133 
134 sig_func_t signal(int sig, sig_func_t signal_handler);
135 int        raise(int sig);
136 int        kill(pid_t pid, int sig);
137 int        send_signal(pid_t tid, uint sig);
138 
139 int        sigaction(int sig, const struct sigaction *act, struct sigaction *oact);
140 int        sigprocmask(int how, const sigset_t *set, sigset_t *oset);
141 int        sigpending(sigset_t *set);
142 int        sigsuspend(const sigset_t *mask);
143 
144 int        sigemptyset(sigset_t *set);
145 int        sigfillset(sigset_t *set);
146 static int sigaddset(sigset_t *set, int signo);
147 static int sigdelset(sigset_t *set, int signo);
148 static int sigismember(const sigset_t *set, int signo);
149 
150 const char *strsignal(int sig);
151 
152 const void  set_signal_stack(void *ptr, size_t size);
153 int         sigaltstack(const stack_t *ss, stack_t *oss);         /* XXXdbg */
154 
155 static inline int
156 sigismember(const sigset_t *set, int sig)
157 {
158 	sigset_t mask = (((sigset_t) 1) << (( sig ) - 1)) ;
159 	return   (*set & mask) ? 1 : 0 ;
160 }
161 
162 static inline int
163 sigaddset(sigset_t *set, int sig)
164 {
165 	sigset_t mask = (((sigset_t) 1) << (( sig ) - 1)) ;
166 	return   ((*set |= mask), 0) ;
167 }
168 
169 static inline int
170 sigdelset(sigset_t *set, int sig)
171 {
172 	sigset_t mask = (((sigset_t) 1) << (( sig ) - 1)) ;
173 	return   ((*set &= ~mask), 0) ;
174 }
175 
176 #ifdef __cplusplus
177 }
178 #endif
179 
180 /*
181  * ==================================================
182  * !!! SPECIAL NOTES CONCERNING NON-POSIX EXTENSIONS:
183  * ==================================================
184  *
185  * The standard Posix interface for signal handlers is not as useful
186  * as it could be. The handler can define only one single argument
187  * (the signal number). For example:
188  *    void
189  *    my_signal_handler(int sig)
190  *    {
191  *    . . .
192  *    }
193  *
194  *    // install the handler
195  *    signal(SIGINT, &my_signal_handler);
196  *
197  * The sigaction() function allows finer grained control of the signal
198  * handling. It also allows an opportunity, via the 'sigaction' struct, to
199  * enable additional data to be passed to the handler. For example:
200  *    void
201  *    my_signal_handler(int sig, char *somedata, vregs regs)
202  *    {
203  *    . . .
204  *    }
205  *
206  *    struct sigaction sa;
207  *    char data_buffer[32];
208  *
209  *    sa.sa_handler = (sig_func_t) my_signal_handler;
210  *    sigemptyset(&sa.sa_mask);
211  *    sigaddset(&sa.sa_mask, SIGINT);
212  *    sa.sa_userdata = data_buffer;
213  *
214  *    // install the handler
215  *    sigaction(SIGINT, &sa, NULL);
216  *
217  * The two additional arguments available to the signal handler are extensions
218  * to the Posix standard. This feature was introduced by the BeOS and retained
219  * by OpenBeOS. However, to remain compatible with Posix and ANSI C, the type
220  * of the sa_handler field is defined as 'sig_func_t'. This requires the handler
221  * to be cast when assigned to the sa_handler field, as in the example above.
222  *
223  * NOTE: C++ member functions can not be signal handlers!
224  * This is because they expect a "this" pointer as the first argument.
225  *
226  *
227  * The 3 arguments that OpenBeOS provides to signal handlers are as follows:
228  *
229  *  - The first argument is the (usual) signal number.
230  *
231  *  - The second argument is whatever value is put in the sa_userdata field
232  *    of the sigaction struct.
233  *
234  *  - The third argument is a pointer to a vregs struct (defined below).
235  *    The vregs struct contains the contents of the volatile registers at
236  *    the time the signal was delivered to your thread. You can change the fields
237  *    of the structure. After your signal handler completes, the OS uses this struct
238  *    to reload the registers for your thread (privileged registers are not loaded
239  *    of course). The vregs struct is of course terribly machine dependent.
240  *    If you use it, you should expect to have to re-work your code when new
241  *    processors come out. Nonetheless, the ability to change the registers does
242  *    open some interesting programming possibilities.
243  */
244 
245 /*
246  * the vregs struct:
247  *
248  * signal handlers get this as the last argument
249  */
250 
251 typedef struct vregs vregs;
252 
253 #if __POWERPC__
254 struct vregs
255 {
256 	ulong pc,                                         /* program counter */
257 	      r0,                                         /* scratch */
258 	      r1,                                         /* stack ptr */
259 	      r2,                                         /* TOC */
260 	      r3,r4,r5,r6,r7,r8,r9,r10,                   /* volatile regs */
261 	      r11,r12;                                    /* scratch regs */
262 
263    double f0,                                         /* fp scratch */
264 	      f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13; /* fp volatile regs */
265 
266 	ulong filler1,                                    /* place holder */
267 	      fpscr,                                      /* fp condition codes */
268 	      ctr, xer, cr, msr, lr;                      /* misc. status */
269 };
270 #endif /* __POWERPC__ */
271 
272 #if __INTEL__
273 
274 typedef struct packed_fp_stack {
275 	unsigned char	st0[10];
276 	unsigned char	st1[10];
277 	unsigned char	st2[10];
278 	unsigned char	st3[10];
279 	unsigned char	st4[10];
280 	unsigned char	st5[10];
281 	unsigned char	st6[10];
282 	unsigned char	st7[10];
283 } packed_fp_stack;
284 
285 typedef struct packed_mmx_regs {
286 	unsigned char	mm0[10];
287 	unsigned char	mm1[10];
288 	unsigned char	mm2[10];
289 	unsigned char	mm3[10];
290 	unsigned char	mm4[10];
291 	unsigned char	mm5[10];
292 	unsigned char	mm6[10];
293 	unsigned char	mm7[10];
294 } packed_mmx_regs;
295 
296 typedef struct old_extended_regs {
297 	unsigned short	fp_control;
298 	unsigned short	_reserved1;
299 	unsigned short	fp_status;
300 	unsigned short	_reserved2;
301 	unsigned short	fp_tag;
302 	unsigned short	_reserved3;
303 	unsigned long	fp_eip;
304 	unsigned short	fp_cs;
305 	unsigned short	fp_opcode;
306 	unsigned long	fp_datap;
307 	unsigned short	fp_ds;
308 	unsigned short	_reserved4;
309 	union {
310 		packed_fp_stack	fp;
311 		packed_mmx_regs	mmx;
312 	} fp_mmx;
313 } old_extended_regs;
314 
315 typedef struct fp_stack {
316 	unsigned char	st0[10];
317 	unsigned char	_reserved_42_47[6];
318 	unsigned char	st1[10];
319 	unsigned char	_reserved_58_63[6];
320 	unsigned char	st2[10];
321 	unsigned char	_reserved_74_79[6];
322 	unsigned char	st3[10];
323 	unsigned char	_reserved_90_95[6];
324 	unsigned char	st4[10];
325 	unsigned char	_reserved_106_111[6];
326 	unsigned char	st5[10];
327 	unsigned char	_reserved_122_127[6];
328 	unsigned char	st6[10];
329 	unsigned char	_reserved_138_143[6];
330 	unsigned char	st7[10];
331 	unsigned char	_reserved_154_159[6];
332 } fp_stack;
333 
334 typedef struct mmx_regs {
335 	unsigned char	mm0[10];
336 	unsigned char	_reserved_42_47[6];
337 	unsigned char	mm1[10];
338 	unsigned char	_reserved_58_63[6];
339 	unsigned char	mm2[10];
340 	unsigned char	_reserved_74_79[6];
341 	unsigned char	mm3[10];
342 	unsigned char	_reserved_90_95[6];
343 	unsigned char	mm4[10];
344 	unsigned char	_reserved_106_111[6];
345 	unsigned char	mm5[10];
346 	unsigned char	_reserved_122_127[6];
347 	unsigned char	mm6[10];
348 	unsigned char	_reserved_138_143[6];
349 	unsigned char	mm7[10];
350 	unsigned char	_reserved_154_159[6];
351 } mmx_regs;
352 
353 typedef struct xmmx_regs {
354 	unsigned char	xmm0[16];
355 	unsigned char	xmm1[16];
356 	unsigned char	xmm2[16];
357 	unsigned char	xmm3[16];
358 	unsigned char	xmm4[16];
359 	unsigned char	xmm5[16];
360 	unsigned char	xmm6[16];
361 	unsigned char	xmm7[16];
362 } xmmx_regs;
363 
364 typedef struct new_extended_regs {
365 	unsigned short	fp_control;
366 	unsigned short	fp_status;
367 	unsigned short	fp_tag;
368 	unsigned short	fp_opcode;
369 	unsigned long	fp_eip;
370 	unsigned short	fp_cs;
371 	unsigned short	res_14_15;
372 	unsigned long	fp_datap;
373 	unsigned short	fp_ds;
374 	unsigned short	_reserved_22_23;
375 	unsigned long	mxcsr;
376 	unsigned long	_reserved_28_31;
377 	union {
378 		fp_stack fp;
379 		mmx_regs mmx;
380 	} fp_mmx;
381 	xmmx_regs xmmx;
382 	unsigned char	_reserved_288_511[224];
383 } new_extended_regs;
384 
385 typedef struct extended_regs {
386 	union {
387 		old_extended_regs	old_format;
388 		new_extended_regs	new_format;
389 	} state;
390 	unsigned long	format;
391 } extended_regs;
392 
393 struct vregs {
394 	unsigned long			eip;
395 	unsigned long			eflags;
396 	unsigned long			eax;
397 	unsigned long			ecx;
398 	unsigned long			edx;
399 	unsigned long			esp;
400 	unsigned long			ebp;
401 	unsigned long			_reserved_1;
402 	extended_regs	xregs;
403 	unsigned long			_reserved_2[3];
404 };
405 
406 #endif /* __INTEL__ */
407 
408 #endif /* _SIGNAL_H_ */
409