xref: /haiku/headers/posix/signal.h (revision e6b30aee0fd7a23d6a6baab9f3718945a0cd838a)
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 sighandler_t sigset(int sig, sighandler_t signalHandler);
151 int     raise(int sig);
152 int     kill(pid_t pid, int sig);
153 int     send_signal(pid_t tid, unsigned int sig);
154 int		killpg(pid_t processGroupID, int sig);
155 
156 int     sigaction(int sig, const struct sigaction *act, struct sigaction *oact);
157 int     sigprocmask(int how, const sigset_t *set, sigset_t *oset);
158 int     sigpending(sigset_t *set);
159 int     sigsuspend(const sigset_t *mask);
160 int 	sigwait(const sigset_t *set, int *sig);
161 
162 int     sigemptyset(sigset_t *set);
163 int     sigfillset(sigset_t *set);
164 int 	sigaddset(sigset_t *set, int signo);
165 int 	sigdelset(sigset_t *set, int signo);
166 int 	sigismember(const sigset_t *set, int signo);
167 int		sigignore(int signo);
168 int		sighold(int signo);
169 int		sigrelse(int signo);
170 
171 const char *strsignal(int sig);
172 
173 void	set_signal_stack(void *ptr, size_t size);
174 int		sigaltstack(const stack_t *ss, stack_t *oss);
175 
176 /* pthread extension : equivalent of sigprocmask()  */
177 int		pthread_sigmask(int how, const sigset_t *set, sigset_t *oset);
178 
179 #ifdef __cplusplus
180 }
181 #endif
182 
183 /* TODO: move this into the documentation!
184  * ==================================================
185  * !!! SPECIAL NOTES CONCERNING NON-POSIX EXTENSIONS:
186  * ==================================================
187  *
188  * The standard Posix interface for signal handlers is not as useful
189  * as it could be. The handler can define only one single argument
190  * (the signal number). For example:
191  *    void
192  *    my_signal_handler(int sig)
193  *    {
194  *    . . .
195  *    }
196  *
197  *    // install the handler
198  *    signal(SIGINT, &my_signal_handler);
199  *
200  * The sigaction() function allows finer grained control of the signal
201  * handling. It also allows an opportunity, via the 'sigaction' struct, to
202  * enable additional data to be passed to the handler. For example:
203  *    void
204  *    my_signal_handler(int sig, char *userData, vregs regs)
205  *    {
206  *    . . .
207  *    }
208  *
209  *    struct sigaction sa;
210  *    char data_buffer[32];
211  *
212  *    sa.sa_handler = (sighandler_t)my_signal_handler;
213  *    sigemptyset(&sa.sa_mask);
214  *    sa.sa_userdata = userData;
215  *
216  *    // install the handler
217  *    sigaction(SIGINT, &sa, NULL);
218  *
219  * The two additional arguments available to the signal handler are extensions
220  * to the Posix standard. This feature was introduced by the BeOS and retained
221  * by Haiku. However, to remain compatible with Posix and ANSI C, the type
222  * of the sa_handler field is defined as 'sighandler_t'. This requires the handler
223  * to be cast when assigned to the sa_handler field, as in the example above.
224  *
225  * The 3 arguments that Haiku provides to signal handlers are as follows:
226  * 1) The first argument is the (usual) signal number.
227  *
228  * 2) The second argument is whatever value is put in the sa_userdata field
229  *    of the sigaction struct.
230  *
231  * 3) The third argument is a pointer to a vregs struct (defined below).
232  *    The vregs struct contains the contents of the volatile registers at
233  *    the time the signal was delivered to your thread. You can change the fields
234  *    of the structure. After your signal handler completes, the OS uses this struct
235  *    to reload the registers for your thread (privileged registers are not loaded
236  *    of course). The vregs struct is of course terribly machine dependent.
237  */
238 
239 /*
240  * the vregs struct:
241  *
242  * signal handlers get this as the last argument
243  */
244 
245 typedef struct vregs vregs;
246 
247 // include architecture specific definitions
248 #ifdef __INTEL__
249         #include <arch/x86/signal.h>
250 #elif __POWERPC__
251         #include <arch/ppc/signal.h>
252 #elif __M68K__
253         #include <arch/m68k/signal.h>
254 #else
255         #error #include <arch/<cpu>/signal.h>
256 #endif
257 
258 
259 #endif /* _SIGNAL_H_ */
260