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