1SIGNAL(2) Linux Programmer's Manual SIGNAL(2)
2
3
4
6 signal - ANSI C signal handling
7
9 #include <signal.h>
10
11 typedef void (*sighandler_t)(int);
12
13 sighandler_t signal(int signum, sighandler_t handler);
14
16 The signal() system call installs a new signal handler for the signal
17 with number signum. The signal handler is set to handler which may be
18 a user specified function, or either SIG_IGN or SIG_DFL.
19
20 Upon arrival of a signal with number signum the following happens. If
21 the corresponding handler is set to SIG_IGN, then the signal is
22 ignored. If the handler is set to SIG_DFL, then the default action
23 associated with the signal (see signal(7)) occurs. Finally, if the
24 handler is set to a function handler then first either the handler is
25 reset to SIG_DFL or an implementation-dependent blocking of the signal
26 is performed, and then handler is called with argument signum.
27
28 Using a signal handler function for a signal is called "catching the
29 signal". The signals SIGKILL and SIGSTOP cannot be caught or ignored.
30
32 The signal() function returns the previous value of the signal handler,
33 or SIG_ERR on error.
34
36 The original Unix signal() would reset the handler to SIG_DFL, and Sys‐
37 tem V (and the Linux kernel and libc4,5) does the same. On the other
38 hand, BSD does not reset the handler, but blocks new instances of this
39 signal from occurring during a call of the handler. The glibc2 library
40 follows the BSD behaviour.
41
42 If one on a libc5 system includes <bsd/signal.h> instead of <signal.h>
43 then signal() is redefined as __bsd_signal and signal has the BSD
44 semantics. This is not recommended.
45
46 If one on a glibc2 system defines a feature test macro such as
47 _XOPEN_SOURCE or uses a separate sysv_signal function, one obtains
48 classical behaviour. This is not recommended.
49
50 Trying to change the semantics of this call using defines and includes
51 is not a good idea. It is better to avoid signal() altogether, and use
52 sigaction(2) instead.
53
55 The effects of this call in a multi-threaded process are unspecified.
56
57 The routine handler must be very careful, since processing elsewhere
58 was interrupted at some arbitrary point. POSIX has the concept of "safe
59 function". If a signal interrupts an unsafe function, and handler
60 calls an unsafe function, then the behavior is undefined. Safe func‐
61 tions are listed explicitly in the various standards. The POSIX.1-2003
62 list is
63
64 _Exit() _exit() abort() accept() access() aio_error() aio_return()
65 aio_suspend() alarm() bind() cfgetispeed() cfgetospeed() cfsetispeed()
66 cfsetospeed() chdir() chmod() chown() clock_gettime() close() connect()
67 creat() dup() dup2() execle() execve() fchmod() fchown() fcntl() fdata‐
68 sync() fork() fpathconf() fstat() fsync() ftruncate() getegid()
69 geteuid() getgid() getgroups() getpeername() getpgrp() getpid() getp‐
70 pid() getsockname() getsockopt() getuid() kill() link() listen()
71 lseek() lstat() mkdir() mkfifo() open() pathconf() pause() pipe()
72 poll() posix_trace_event() pselect() raise() read() readlink() recv()
73 recvfrom() recvmsg() rename() rmdir() select() sem_post() send()
74 sendmsg() sendto() setgid() setpgid() setsid() setsockopt() setuid()
75 shutdown() sigaction() sigaddset() sigdelset() sigemptyset() sig‐
76 fillset() sigismember() signal() sigpause() sigpending() sigprocmask()
77 sigqueue() sigset() sigsuspend() sleep() socket() socketpair() stat()
78 symlink() sysconf() tcdrain() tcflow() tcflush() tcgetattr() tcgetp‐
79 grp() tcsendbreak() tcsetattr() tcsetpgrp() time() timer_getoverrun()
80 timer_gettime() timer_settime() times() umask() uname() unlink()
81 utime() wait() waitpid() write().
82
83 According to POSIX, the behaviour of a process is undefined after it
84 ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by
85 the kill(2) or the raise(3) functions. Integer division by zero has
86 undefined result. On some architectures it will generate a SIGFPE sig‐
87 nal. (Also dividing the most negative integer by -1 may generate
88 SIGFPE.) Ignoring this signal might lead to an endless loop.
89
90 See sigaction(2) for details on what happens when SIGCHLD is set to
91 SIG_IGN.
92
93 The use of sighandler_t is a GNU extension. Various versions of libc
94 predefine this type; libc4 and libc5 define SignalHandler, glibc
95 defines sig_t and, when _GNU_SOURCE is defined, also sighandler_t.
96
98 C89, C99, POSIX.1-2001.
99
101 kill(1), alarm(2), kill(2), pause(2), sigaction(2), sigpending(2), sig‐
102 procmask(2), sigqueue(2), sigsuspend(2), killpg(3), raise(3), sigse‐
103 tops(3), sigvec(3), feature_test_macros(7), signal(7)
104
105
106
107Linux 2.2 2000-04-28 SIGNAL(2)