1SIGNALFD(2) Linux Programmer's Manual SIGNALFD(2)
2
3
4
6 signalfd - create a file descriptor for accepting signals
7
9 #include <sys/signalfd.h>
10
11 int signalfd(int fd, const sigset_t *mask, int flags);
12
14 signalfd() creates a file descriptor that can be used to accept signals
15 targeted at the caller. This provides an alternative to the use of a
16 signal handler or sigwaitinfo(2), and has the advantage that the file
17 descriptor may be monitored by select(2), poll(2), and epoll(7).
18
19 The mask argument specifies the set of signals that the caller wishes
20 to accept via the file descriptor. This argument is a signal set whose
21 contents can be initialized using the macros described in sigsetops(3).
22 Normally, the set of signals to be received via the file descriptor
23 should be blocked using sigprocmask(2), to prevent the signals being
24 handled according to their default dispositions. It is not possible to
25 receive SIGKILL or SIGSTOP signals via a signalfd file descriptor;
26 these signals are silently ignored if specified in mask.
27
28 If the fd argument is -1, then the call creates a new file descriptor
29 and associates the signal set specified in mask with that file descrip‐
30 tor. If fd is not -1, then it must specify a valid existing signalfd
31 file descriptor, and mask is used to replace the signal set associated
32 with that file descriptor.
33
34 Starting with Linux 2.6.27, the following values may be bitwise ORed in
35 flags to change the behavior of signalfd():
36
37 SFD_NONBLOCK Set the O_NONBLOCK file status flag on the new open file
38 description. Using this flag saves extra calls to
39 fcntl(2) to achieve the same result.
40
41 SFD_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the new file
42 descriptor. See the description of the O_CLOEXEC flag in
43 open(2) for reasons why this may be useful.
44
45 In Linux up to version 2.6.26, the flags argument is unused, and must
46 be specified as zero.
47
48 signalfd() returns a file descriptor that supports the following opera‐
49 tions:
50
51 read(2)
52 If one or more of the signals specified in mask is pending for
53 the process, then the buffer supplied to read(2) is used to
54 return one or more signalfd_siginfo structures (see below) that
55 describe the signals. The read(2) returns information for as
56 many signals as are pending and will fit in the supplied buffer.
57 The buffer must be at least sizeof(struct signalfd_siginfo)
58 bytes. The return value of the read(2) is the total number of
59 bytes read.
60
61 As a consequence of the read(2), the signals are consumed, so
62 that they are no longer pending for the process (i.e., will not
63 be caught by signal handlers, and cannot be accepted using sig‐
64 waitinfo(2)).
65
66 If none of the signals in mask is pending for the process, then
67 the read(2) either blocks until one of the signals in mask is
68 generated for the process, or fails with the error EAGAIN if the
69 file descriptor has been made nonblocking.
70
71 poll(2), select(2) (and similar)
72 The file descriptor is readable (the select(2) readfds argument;
73 the poll(2) POLLIN flag) if one or more of the signals in mask
74 is pending for the process.
75
76 The signalfd file descriptor also supports the other file-
77 descriptor multiplexing APIs: pselect(2), ppoll(2), and
78 epoll(7).
79
80 close(2)
81 When the file descriptor is no longer required it should be
82 closed. When all file descriptors associated with the same sig‐
83 nalfd object have been closed, the resources for object are
84 freed by the kernel.
85
86 The signalfd_siginfo structure
87 The format of the signalfd_siginfo structure(s) returned by read(2)s
88 from a signalfd file descriptor is as follows:
89
90 struct signalfd_siginfo {
91 uint32_t ssi_signo; /* Signal number */
92 int32_t ssi_errno; /* Error number (unused) */
93 int32_t ssi_code; /* Signal code */
94 uint32_t ssi_pid; /* PID of sender */
95 uint32_t ssi_uid; /* Real UID of sender */
96 int32_t ssi_fd; /* File descriptor (SIGIO) */
97 uint32_t ssi_tid; /* Kernel timer ID (POSIX timers)
98 uint32_t ssi_band; /* Band event (SIGIO) */
99 uint32_t ssi_overrun; /* POSIX timer overrun count */
100 uint32_t ssi_trapno; /* Trap number that caused signal */
101 int32_t ssi_status; /* Exit status or signal (SIGCHLD) */
102 int32_t ssi_int; /* Integer sent by sigqueue(3) */
103 uint64_t ssi_ptr; /* Pointer sent by sigqueue(3) */
104 uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */
105 uint64_t ssi_stime; /* System CPU time consumed
106 (SIGCHLD) */
107 uint64_t ssi_addr; /* Address that generated signal
108 (for hardware-generated signals) */
109 uint16_t ssi_addr_lsb; /* Least significant bit of address
110 (SIGBUS; since Linux 2.6.37)
111 uint8_t pad[X]; /* Pad size to 128 bytes (allow for
112 additional fields in the future) */
113 };
114
115 Each of the fields in this structure is analogous to the similarly
116 named field in the siginfo_t structure. The siginfo_t structure is
117 described in sigaction(2). Not all fields in the returned sig‐
118 nalfd_siginfo structure will be valid for a specific signal; the set of
119 valid fields can be determined from the value returned in the ssi_code
120 field. This field is the analog of the siginfo_t si_code field; see
121 sigaction(2) for details.
122
123 fork(2) semantics
124 After a fork(2), the child inherits a copy of the signalfd file
125 descriptor. A read(2) from the file descriptor in the child will
126 return information about signals queued to the child.
127
128 Semantics of file descriptor passing
129 As with other file descriptors, signalfd file descriptors can be passed
130 to another process via a UNIX domain socket (see unix(7)). In the
131 receiving process, a read(2) from the received file descriptor will
132 return information about signals queued to that process.
133
134 execve(2) semantics
135 Just like any other file descriptor, a signalfd file descriptor remains
136 open across an execve(2), unless it has been marked for close-on-exec
137 (see fcntl(2)). Any signals that were available for reading before the
138 execve(2) remain available to the newly loaded program. (This is anal‐
139 ogous to traditional signal semantics, where a blocked signal that is
140 pending remains pending across an execve(2).)
141
142 Thread semantics
143 The semantics of signalfd file descriptors in a multithreaded program
144 mirror the standard semantics for signals. In other words, when a
145 thread reads from a signalfd file descriptor, it will read the signals
146 that are directed to the thread itself and the signals that are
147 directed to the process (i.e., the entire thread group). (A thread
148 will not be able to read signals that are directed to other threads in
149 the process.)
150
152 On success, signalfd() returns a signalfd file descriptor; this is
153 either a new file descriptor (if fd was -1), or fd if fd was a valid
154 signalfd file descriptor. On error, -1 is returned and errno is set to
155 indicate the error.
156
158 EBADF The fd file descriptor is not a valid file descriptor.
159
160 EINVAL fd is not a valid signalfd file descriptor.
161
162 EINVAL flags is invalid; or, in Linux 2.6.26 or earlier, flags is
163 nonzero.
164
165 EMFILE The per-process limit on the number of open file descriptors has
166 been reached.
167
168 ENFILE The system-wide limit on the total number of open files has been
169 reached.
170
171 ENODEV Could not mount (internal) anonymous inode device.
172
173 ENOMEM There was insufficient memory to create a new signalfd file
174 descriptor.
175
177 signalfd() is available on Linux since kernel 2.6.22. Working support
178 is provided in glibc since version 2.8. The signalfd4() system call
179 (see NOTES) is available on Linux since kernel 2.6.27.
180
182 signalfd() and signalfd4() are Linux-specific.
183
185 A process can create multiple signalfd file descriptors. This makes it
186 possible to accept different signals on different file descriptors.
187 (This may be useful if monitoring the file descriptors using select(2),
188 poll(2), or epoll(7): the arrival of different signals will make dif‐
189 ferent file descriptors ready.) If a signal appears in the mask of
190 more than one of the file descriptors, then occurrences of that signal
191 can be read (once) from any one of the file descriptors.
192
193 Attempts to include SIGKILL and SIGSTOP in mask are silently ignored.
194
195 The signal mask employed by a signalfd file descriptor can be viewed
196 via the entry for the corresponding file descriptor in the process's
197 /proc/[pid]/fdinfo directory. See proc(5) for further details.
198
199 Limitations
200 The signalfd mechanism can't be used to receive signals that are syn‐
201 chronously generated, such as the SIGSEGV signal that results from
202 accessing an invalid memory address or the SIGFPE signal that results
203 from an arithmetic error. Such signals can be caught only via signal
204 handler.
205
206 As described above, in normal usage one blocks the signals that will be
207 accepted via signalfd(). If spawning a child process to execute a
208 helper program (that does not need the signalfd file descriptor), then,
209 after the call to fork(2), you will normally want to unblock those sig‐
210 nals before calling execve(2), so that the helper program can see any
211 signals that it expects to see. Be aware, however, that this won't be
212 possible in the case of a helper program spawned behind the scenes by
213 any library function that the program may call. In such cases, one
214 must fall back to using a traditional signal handler that writes to a
215 file descriptor monitored by select(2), poll(2), or epoll(7),
216
217 C library/kernel differences
218 The underlying Linux system call requires an additional argument,
219 size_t sizemask, which specifies the size of the mask argument. The
220 glibc signalfd() wrapper function does not include this argument, since
221 it provides the required value for the underlying system call.
222
223 There are two underlying Linux system calls: signalfd() and the more
224 recent signalfd4(). The former system call does not implement a flags
225 argument. The latter system call implements the flags values described
226 above. Starting with glibc 2.9, the signalfd() wrapper function will
227 use signalfd4() where it is available.
228
230 In kernels before 2.6.25, the ssi_ptr and ssi_int fields are not filled
231 in with the data accompanying a signal sent by sigqueue(3).
232
234 The program below accepts the signals SIGINT and SIGQUIT via a signalfd
235 file descriptor. The program terminates after accepting a SIGQUIT sig‐
236 nal. The following shell session demonstrates the use of the program:
237
238 $ ./signalfd_demo
239 ^C # Control-C generates SIGINT
240 Got SIGINT
241 ^C
242 Got SIGINT
243 ^\ # Control-\ generates SIGQUIT
244 Got SIGQUIT
245 $
246
247 Program source
248
249 #include <sys/signalfd.h>
250 #include <signal.h>
251 #include <unistd.h>
252 #include <stdlib.h>
253 #include <stdio.h>
254
255 #define handle_error(msg) \
256 do { perror(msg); exit(EXIT_FAILURE); } while (0)
257
258 int
259 main(int argc, char *argv[])
260 {
261 sigset_t mask;
262 int sfd;
263 struct signalfd_siginfo fdsi;
264 ssize_t s;
265
266 sigemptyset(&mask);
267 sigaddset(&mask, SIGINT);
268 sigaddset(&mask, SIGQUIT);
269
270 /* Block signals so that they aren't handled
271 according to their default dispositions */
272
273 if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
274 handle_error("sigprocmask");
275
276 sfd = signalfd(-1, &mask, 0);
277 if (sfd == -1)
278 handle_error("signalfd");
279
280 for (;;) {
281 s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
282 if (s != sizeof(struct signalfd_siginfo))
283 handle_error("read");
284
285 if (fdsi.ssi_signo == SIGINT) {
286 printf("Got SIGINT\n");
287 } else if (fdsi.ssi_signo == SIGQUIT) {
288 printf("Got SIGQUIT\n");
289 exit(EXIT_SUCCESS);
290 } else {
291 printf("Read unexpected signal\n");
292 }
293 }
294 }
295
297 eventfd(2), poll(2), read(2), select(2), sigaction(2), sigprocmask(2),
298 sigwaitinfo(2), timerfd_create(2), sigsetops(3), sigwait(3), epoll(7),
299 signal(7)
300
302 This page is part of release 4.16 of the Linux man-pages project. A
303 description of the project, information about reporting bugs, and the
304 latest version of this page, can be found at
305 https://www.kernel.org/doc/man-pages/.
306
307
308
309Linux 2017-05-03 SIGNALFD(2)