1signal(2) System Calls Manual signal(2)
2
3
4
6 signal - ANSI C signal handling
7
9 Standard C library (libc, -lc)
10
12 #include <signal.h>
13
14 typedef void (*sighandler_t)(int);
15
16 sighandler_t signal(int signum, sighandler_t handler);
17
19 WARNING: the behavior of signal() varies across UNIX versions, and has
20 also varied historically across different versions of Linux. Avoid its
21 use: use sigaction(2) instead. See Portability below.
22
23 signal() sets the disposition of the signal signum to handler, which is
24 either SIG_IGN, SIG_DFL, or the address of a programmer-defined func‐
25 tion (a "signal handler").
26
27 If the signal signum is delivered to the process, then one of the fol‐
28 lowing happens:
29
30 * If the disposition is set to SIG_IGN, then the signal is ignored.
31
32 * If the disposition is set to SIG_DFL, then the default action asso‐
33 ciated with the signal (see signal(7)) occurs.
34
35 * If the disposition is set to a function, then first either the dis‐
36 position is reset to SIG_DFL, or the signal is blocked (see Porta‐
37 bility below), and then handler is called with argument signum. If
38 invocation of the handler caused the signal to be blocked, then the
39 signal is unblocked upon return from the handler.
40
41 The signals SIGKILL and SIGSTOP cannot be caught or ignored.
42
44 signal() returns the previous value of the signal handler. On failure,
45 it returns SIG_ERR, and errno is set to indicate the error.
46
48 EINVAL signum is invalid.
49
51 The use of sighandler_t is a GNU extension, exposed if _GNU_SOURCE is
52 defined; glibc also defines (the BSD-derived) sig_t if _BSD_SOURCE
53 (glibc 2.19 and earlier) or _DEFAULT_SOURCE (glibc 2.19 and later) is
54 defined. Without use of such a type, the declaration of signal() is
55 the somewhat harder to read:
56
57 void ( *signal(int signum, void (*handler)(int)) ) (int);
58
59 Portability
60 The only portable use of signal() is to set a signal's disposition to
61 SIG_DFL or SIG_IGN. The semantics when using signal() to establish a
62 signal handler vary across systems (and POSIX.1 explicitly permits this
63 variation); do not use it for this purpose.
64
65 POSIX.1 solved the portability mess by specifying sigaction(2), which
66 provides explicit control of the semantics when a signal handler is in‐
67 voked; use that interface instead of signal().
68
70 C11, POSIX.1-2008.
71
73 C89, POSIX.1-2001.
74
75 In the original UNIX systems, when a handler that was established using
76 signal() was invoked by the delivery of a signal, the disposition of
77 the signal would be reset to SIG_DFL, and the system did not block de‐
78 livery of further instances of the signal. This is equivalent to call‐
79 ing sigaction(2) with the following flags:
80
81 sa.sa_flags = SA_RESETHAND | SA_NODEFER;
82
83 System V also provides these semantics for signal(). This was bad be‐
84 cause the signal might be delivered again before the handler had a
85 chance to reestablish itself. Furthermore, rapid deliveries of the
86 same signal could result in recursive invocations of the handler.
87
88 BSD improved on this situation, but unfortunately also changed the se‐
89 mantics of the existing signal() interface while doing so. On BSD,
90 when a signal handler is invoked, the signal disposition is not reset,
91 and further instances of the signal are blocked from being delivered
92 while the handler is executing. Furthermore, certain blocking system
93 calls are automatically restarted if interrupted by a signal handler
94 (see signal(7)). The BSD semantics are equivalent to calling sigac‐
95 tion(2) with the following flags:
96
97 sa.sa_flags = SA_RESTART;
98
99 The situation on Linux is as follows:
100
101 • The kernel's signal() system call provides System V semantics.
102
103 • By default, in glibc 2 and later, the signal() wrapper function does
104 not invoke the kernel system call. Instead, it calls sigaction(2)
105 using flags that supply BSD semantics. This default behavior is
106 provided as long as a suitable feature test macro is defined:
107 _BSD_SOURCE on glibc 2.19 and earlier or _DEFAULT_SOURCE in glibc
108 2.19 and later. (By default, these macros are defined; see fea‐
109 ture_test_macros(7) for details.) If such a feature test macro is
110 not defined, then signal() provides System V semantics.
111
113 The effects of signal() in a multithreaded process are unspecified.
114
115 According to POSIX, the behavior of a process is undefined after it ig‐
116 nores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by
117 kill(2) or raise(3). Integer division by zero has undefined result.
118 On some architectures it will generate a SIGFPE signal. (Also dividing
119 the most negative integer by -1 may generate SIGFPE.) Ignoring this
120 signal might lead to an endless loop.
121
122 See sigaction(2) for details on what happens when the disposition
123 SIGCHLD is set to SIG_IGN.
124
125 See signal-safety(7) for a list of the async-signal-safe functions that
126 can be safely called from inside a signal handler.
127
129 kill(1), alarm(2), kill(2), pause(2), sigaction(2), signalfd(2), sig‐
130 pending(2), sigprocmask(2), sigsuspend(2), bsd_signal(3), killpg(3),
131 raise(3), siginterrupt(3), sigqueue(3), sigsetops(3), sigvec(3),
132 sysv_signal(3), signal(7)
133
134
135
136Linux man-pages 6.05 2023-03-30 signal(2)