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, or more precisely the SA_SIGINFO flag. To do 29 // this properly we need real-time signal support. Both are commented out for 30 // the time being to not make "configure" scripts think we do support them. 31 #if 0 32 typedef struct { 33 int si_signo; /* signal number */ 34 int si_code; /* signal code */ 35 int si_errno; /* if non zero, an error number associated with this signal */ 36 pid_t si_pid; /* sending process ID */ 37 uid_t si_uid; /* real user ID of sending process */ 38 void *si_addr; /* address of faulting instruction */ 39 int si_status; /* exit value or signal */ 40 long si_band; /* band event for SIGPOLL */ 41 } siginfo_t; 42 #endif /* 0 */ 43 44 /* 45 * structure used by sigaction() 46 * 47 * Note: the 'sa_userdata' field is a non-POSIX extension. 48 * See the documentation for more info on this. 49 */ 50 struct sigaction { 51 sighandler_t sa_handler; 52 sigset_t sa_mask; 53 int sa_flags; 54 void *sa_userdata; /* will be passed to the signal handler */ 55 }; 56 57 /* values for sa_flags */ 58 #define SA_NOCLDSTOP 0x01 59 #define SA_NOCLDWAIT 0x02 60 #define SA_RESETHAND 0x04 61 #define SA_NODEFER 0x08 62 #define SA_RESTART 0x10 63 #define SA_ONSTACK 0x20 64 //#define SA_SIGINFO 0x40 65 #define SA_NOMASK SA_NODEFER 66 #define SA_STACK SA_ONSTACK 67 #define SA_ONESHOT SA_RESETHAND 68 69 /* values for ss_flags */ 70 #define SS_ONSTACK 0x1 71 #define SS_DISABLE 0x2 72 73 #define MINSIGSTKSZ 4096 74 #define SIGSTKSZ 16384 75 76 /* 77 * for signals using an alternate stack 78 */ 79 typedef struct stack_t { 80 void *ss_sp; 81 size_t ss_size; 82 int ss_flags; 83 } stack_t; 84 85 typedef struct sigstack { 86 int ss_onstack; 87 void *ss_sp; 88 } sigstack; 89 90 /* for the 'how' arg of sigprocmask() */ 91 #define SIG_BLOCK 1 92 #define SIG_UNBLOCK 2 93 #define SIG_SETMASK 3 94 95 /* 96 * The list of all defined signals: 97 * 98 * The numbering of signals for Haiku attempts to maintain 99 * some consistency with UN*X conventions so that things 100 * like "kill -9" do what you expect. 101 */ 102 #define SIGHUP 1 /* hangup -- tty is gone! */ 103 #define SIGINT 2 /* interrupt */ 104 #define SIGQUIT 3 /* `quit' special character typed in tty */ 105 #define SIGILL 4 /* illegal instruction */ 106 #define SIGCHLD 5 /* child process exited */ 107 #define SIGABRT 6 /* abort() called, dont' catch */ 108 #define SIGPIPE 7 /* write to a pipe w/no readers */ 109 #define SIGFPE 8 /* floating point exception */ 110 #define SIGKILL 9 /* kill a team (not catchable) */ 111 #define SIGSTOP 10 /* suspend a thread (not catchable) */ 112 #define SIGSEGV 11 /* segmentation violation (read: invalid pointer) */ 113 #define SIGCONT 12 /* continue execution if suspended */ 114 #define SIGTSTP 13 /* `stop' special character typed in tty */ 115 #define SIGALRM 14 /* an alarm has gone off (see alarm()) */ 116 #define SIGTERM 15 /* termination requested */ 117 #define SIGTTIN 16 /* read of tty from bg process */ 118 #define SIGTTOU 17 /* write to tty from bg process */ 119 #define SIGUSR1 18 /* app defined signal 1 */ 120 #define SIGUSR2 19 /* app defined signal 2 */ 121 #define SIGWINCH 20 /* tty window size changed */ 122 #define SIGKILLTHR 21 /* be specific: kill just the thread, not team */ 123 #define SIGTRAP 22 /* Trace/breakpoint trap */ 124 #define SIGPOLL 23 /* Pollable event */ 125 #define SIGPROF 24 /* Profiling timer expired */ 126 #define SIGSYS 25 /* Bad system call */ 127 #define SIGURG 26 /* High bandwidth data is available at socket */ 128 #define SIGVTALRM 27 /* Virtual timer expired */ 129 #define SIGXCPU 28 /* CPU time limit exceeded */ 130 #define SIGXFSZ 29 /* File size limit exceeded */ 131 132 #define SIGBUS SIGSEGV /* for old style code */ 133 134 /* 135 * Signal numbers 30-32 are currently free but may be used in future 136 * releases. Use them at your own peril (if you do use them, at least 137 * be smart and use them backwards from signal 32). 138 */ 139 #define MAX_SIGNO 32 /* the most signals that a single thread can reference */ 140 #define __signal_max 29 /* the largest signal number that is actually defined */ 141 #define NSIG (__signal_max+1) 142 /* the number of defined signals */ 143 144 145 /* the global table of text strings containing descriptions for each signal */ 146 extern const char * const sys_siglist[NSIG]; 147 148 149 #ifdef __cplusplus 150 extern "C" { 151 #endif 152 153 sighandler_t signal(int sig, sighandler_t signalHandler); 154 sighandler_t sigset(int sig, sighandler_t signalHandler); 155 int raise(int sig); 156 int kill(pid_t pid, int sig); 157 int send_signal(pid_t tid, unsigned int sig); 158 int killpg(pid_t processGroupID, int sig); 159 160 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact); 161 int sigprocmask(int how, const sigset_t *set, sigset_t *oset); 162 int sigpending(sigset_t *set); 163 int sigsuspend(const sigset_t *mask); 164 int sigwait(const sigset_t *set, int *sig); 165 166 int sigemptyset(sigset_t *set); 167 int sigfillset(sigset_t *set); 168 int sigaddset(sigset_t *set, int signo); 169 int sigdelset(sigset_t *set, int signo); 170 int sigismember(const sigset_t *set, int signo); 171 int sigignore(int signo); 172 int sighold(int signo); 173 int sigrelse(int signo); 174 175 const char *strsignal(int sig); 176 177 void set_signal_stack(void *ptr, size_t size); 178 int sigaltstack(const stack_t *ss, stack_t *oss); 179 180 /* pthread extension : equivalent of sigprocmask() */ 181 int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset); 182 183 #ifdef __cplusplus 184 } 185 #endif 186 187 /* TODO: move this into the documentation! 188 * ================================================== 189 * !!! SPECIAL NOTES CONCERNING NON-POSIX EXTENSIONS: 190 * ================================================== 191 * 192 * The standard Posix interface for signal handlers is not as useful 193 * as it could be. The handler can define only one single argument 194 * (the signal number). For example: 195 * void 196 * my_signal_handler(int sig) 197 * { 198 * . . . 199 * } 200 * 201 * // install the handler 202 * signal(SIGINT, &my_signal_handler); 203 * 204 * The sigaction() function allows finer grained control of the signal 205 * handling. It also allows an opportunity, via the 'sigaction' struct, to 206 * enable additional data to be passed to the handler. For example: 207 * void 208 * my_signal_handler(int sig, char *userData, vregs regs) 209 * { 210 * . . . 211 * } 212 * 213 * struct sigaction sa; 214 * char data_buffer[32]; 215 * 216 * sa.sa_handler = (sighandler_t)my_signal_handler; 217 * sigemptyset(&sa.sa_mask); 218 * sa.sa_userdata = userData; 219 * 220 * // install the handler 221 * sigaction(SIGINT, &sa, NULL); 222 * 223 * The two additional arguments available to the signal handler are extensions 224 * to the Posix standard. This feature was introduced by the BeOS and retained 225 * by Haiku. However, to remain compatible with Posix and ANSI C, the type 226 * of the sa_handler field is defined as 'sighandler_t'. This requires the handler 227 * to be cast when assigned to the sa_handler field, as in the example above. 228 * 229 * The 3 arguments that Haiku provides to signal handlers are as follows: 230 * 1) The first argument is the (usual) signal number. 231 * 232 * 2) The second argument is whatever value is put in the sa_userdata field 233 * of the sigaction struct. 234 * 235 * 3) The third argument is a pointer to a vregs struct (defined below). 236 * The vregs struct contains the contents of the volatile registers at 237 * the time the signal was delivered to your thread. You can change the fields 238 * of the structure. After your signal handler completes, the OS uses this struct 239 * to reload the registers for your thread (privileged registers are not loaded 240 * of course). The vregs struct is of course terribly machine dependent. 241 */ 242 243 /* 244 * the vregs struct: 245 * 246 * signal handlers get this as the last argument 247 */ 248 249 typedef struct vregs vregs; 250 251 // include architecture specific definitions 252 #ifdef __INTEL__ 253 #include <arch/x86/signal.h> 254 #elif __POWERPC__ 255 #include <arch/ppc/signal.h> 256 #elif __M68K__ 257 #include <arch/m68k/signal.h> 258 #else 259 #error #include <arch/<cpu>/signal.h> 260 #endif 261 262 263 #endif /* _SIGNAL_H_ */ 264