namespaces.7: ffix
[man-pages.git] / man2 / signalfd.2
blob102679c0d25009493e7f946f9ac82148eb4c3483
1 .\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" starting from a version by Davide Libenzi <davidel@xmailserver.org>
3 .\"
4 .\" %%%LICENSE_START(GPLv2+_SW_3_PARA)
5 .\" This program is free software; you can redistribute it and/or modify
6 .\" it under the terms of the GNU General Public License as published by
7 .\" the Free Software Foundation; either version 2 of the License, or
8 .\" (at your option) any later version.
9 .\"
10 .\" This program is distributed in the hope that it will be useful,
11 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
12 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 .\" GNU General Public License for more details.
14 .\"
15 .\" You should have received a copy of the GNU General Public
16 .\" License along with this manual; if not, see
17 .\" <http://www.gnu.org/licenses/>.
18 .\" %%%LICENSE_END
19 .\"
20 .TH SIGNALFD 2 2021-03-22 Linux "Linux Programmer's Manual"
21 .SH NAME
22 signalfd \- create a file descriptor for accepting signals
23 .SH SYNOPSIS
24 .nf
25 .B #include <sys/signalfd.h>
26 .PP
27 .BI "int signalfd(int " fd ", const sigset_t *" mask ", int " flags );
28 .fi
29 .SH DESCRIPTION
30 .BR signalfd ()
31 creates a file descriptor that can be used to accept signals
32 targeted at the caller.
33 This provides an alternative to the use of a signal handler or
34 .BR sigwaitinfo (2),
35 and has the advantage that the file descriptor may be monitored by
36 .BR select (2),
37 .BR poll (2),
38 and
39 .BR epoll (7).
40 .PP
41 The
42 .I mask
43 argument specifies the set of signals that the caller
44 wishes to accept via the file descriptor.
45 This argument is a signal set whose contents can be initialized
46 using the macros described in
47 .BR sigsetops (3).
48 Normally, the set of signals to be received via the
49 file descriptor should be blocked using
50 .BR sigprocmask (2),
51 to prevent the signals being handled according to their default
52 dispositions.
53 It is not possible to receive
54 .B SIGKILL
56 .B SIGSTOP
57 signals via a signalfd file descriptor;
58 these signals are silently ignored if specified in
59 .IR mask .
60 .PP
61 If the
62 .I fd
63 argument is \-1,
64 then the call creates a new file descriptor and associates the
65 signal set specified in
66 .I mask
67 with that file descriptor.
69 .I fd
70 is not \-1,
71 then it must specify a valid existing signalfd file descriptor, and
72 .I mask
73 is used to replace the signal set associated with that file descriptor.
74 .PP
75 Starting with Linux 2.6.27, the following values may be bitwise ORed in
76 .IR flags
77 to change the behavior of
78 .BR signalfd ():
79 .TP 14
80 .B SFD_NONBLOCK
81 Set the
82 .BR O_NONBLOCK
83 file status flag on the open file description (see
84 .BR open (2))
85 referred to by the new file descriptor.
86 Using this flag saves extra calls to
87 .BR fcntl (2)
88 to achieve the same result.
89 .TP
90 .B SFD_CLOEXEC
91 Set the close-on-exec
92 .RB ( FD_CLOEXEC )
93 flag on the new file descriptor.
94 See the description of the
95 .B O_CLOEXEC
96 flag in
97 .BR open (2)
98 for reasons why this may be useful.
99 .PP
100 In Linux up to version 2.6.26, the
101 .I flags
102 argument is unused, and must be specified as zero.
104 .BR signalfd ()
105 returns a file descriptor that supports the following operations:
107 .BR read (2)
108 If one or more of the signals specified in
109 .I mask
110 is pending for the process, then the buffer supplied to
111 .BR read (2)
112 is used to return one or more
113 .I signalfd_siginfo
114 structures (see below) that describe the signals.
116 .BR read (2)
117 returns information for as many signals as are pending and will
118 fit in the supplied buffer.
119 The buffer must be at least
120 .I "sizeof(struct signalfd_siginfo)"
121 bytes.
122 The return value of the
123 .BR read (2)
124 is the total number of bytes read.
126 As a consequence of the
127 .BR read (2),
128 the signals are consumed,
129 so that they are no longer pending for the process
130 (i.e., will not be caught by signal handlers,
131 and cannot be accepted using
132 .BR sigwaitinfo (2)).
134 If none of the signals in
135 .I mask
136 is pending for the process, then the
137 .BR read (2)
138 either blocks until one of the signals in
139 .I mask
140 is generated for the process,
141 or fails with the error
142 .B EAGAIN
143 if the file descriptor has been made nonblocking.
145 .BR poll "(2), " select "(2) (and similar)"
146 The file descriptor is readable
147 (the
148 .BR select (2)
149 .I readfds
150 argument; the
151 .BR poll (2)
152 .B POLLIN
153 flag)
154 if one or more of the signals in
155 .I mask
156 is pending for the process.
158 The signalfd file descriptor also supports the other file-descriptor
159 multiplexing APIs:
160 .BR pselect (2),
161 .BR ppoll (2),
163 .BR epoll (7).
165 .BR close (2)
166 When the file descriptor is no longer required it should be closed.
167 When all file descriptors associated with the same signalfd object
168 have been closed, the resources for object are freed by the kernel.
169 .SS The signalfd_siginfo structure
170 The format of the
171 .I signalfd_siginfo
172 structure(s) returned by
173 .BR read (2)s
174 from a signalfd file descriptor is as follows:
176 .in +4n
178 struct signalfd_siginfo {
179     uint32_t ssi_signo;    /* Signal number */
180     int32_t  ssi_errno;    /* Error number (unused) */
181     int32_t  ssi_code;     /* Signal code */
182     uint32_t ssi_pid;      /* PID of sender */
183     uint32_t ssi_uid;      /* Real UID of sender */
184     int32_t  ssi_fd;       /* File descriptor (SIGIO) */
185     uint32_t ssi_tid;      /* Kernel timer ID (POSIX timers)
186     uint32_t ssi_band;     /* Band event (SIGIO) */
187     uint32_t ssi_overrun;  /* POSIX timer overrun count */
188     uint32_t ssi_trapno;   /* Trap number that caused signal */
189 .\" ssi_trapno is unused on most arches
190     int32_t  ssi_status;   /* Exit status or signal (SIGCHLD) */
191     int32_t  ssi_int;      /* Integer sent by sigqueue(3) */
192     uint64_t ssi_ptr;      /* Pointer sent by sigqueue(3) */
193     uint64_t ssi_utime;    /* User CPU time consumed (SIGCHLD) */
194     uint64_t ssi_stime;    /* System CPU time consumed
195                               (SIGCHLD) */
196     uint64_t ssi_addr;     /* Address that generated signal
197                               (for hardware\-generated signals) */
198     uint16_t ssi_addr_lsb; /* Least significant bit of address
199                               (SIGBUS; since Linux 2.6.37) */
200 .\" ssi_addr_lsb: commit b8aeec34175fc8fe8b0d40efea4846dfc1ba663e
201     uint8_t  pad[\fIX\fP];       /* Pad size to 128 bytes (allow for
202                               additional fields in the future) */
207 Each of the fields in this structure
208 is analogous to the similarly named field in the
209 .I siginfo_t
210 structure.
212 .I siginfo_t
213 structure is described in
214 .BR sigaction (2).
215 Not all fields in the returned
216 .I signalfd_siginfo
217 structure will be valid for a specific signal;
218 the set of valid fields can be determined from the value returned in the
219 .I ssi_code
220 field.
221 This field is the analog of the
222 .I siginfo_t
223 .I si_code
224 field; see
225 .BR sigaction (2)
226 for details.
227 .SS fork(2) semantics
228 After a
229 .BR fork (2),
230 the child inherits a copy of the signalfd file descriptor.
232 .BR read (2)
233 from the file descriptor in the child will return information
234 about signals queued to the child.
235 .SS Semantics of file descriptor passing
236 As with other file descriptors,
237 signalfd file descriptors can be passed to another process
238 via a UNIX domain socket (see
239 .BR unix (7)).
240 In the receiving process, a
241 .BR read (2)
242 from the received file descriptor will return information
243 about signals queued to that process.
244 .SS execve(2) semantics
245 Just like any other file descriptor,
246 a signalfd file descriptor remains open across an
247 .BR execve (2),
248 unless it has been marked for close-on-exec (see
249 .BR fcntl (2)).
250 Any signals that were available for reading before the
251 .BR execve (2)
252 remain available to the newly loaded program.
253 (This is analogous to traditional signal semantics,
254 where a blocked signal that is pending remains pending across an
255 .BR execve (2).)
256 .SS Thread semantics
257 The semantics of signalfd file descriptors in a multithreaded program
258 mirror the standard semantics for signals.
259 In other words,
260 when a thread reads from a signalfd file descriptor,
261 it will read the signals that are directed to the thread
262 itself and the signals that are directed to the process
263 (i.e., the entire thread group).
264 (A thread will not be able to read signals that are directed
265 to other threads in the process.)
267 .SS epoll(7) semantics
268 If a process adds (via
269 .BR epoll_ctl (2))
270 a signalfd file descriptor to an
271 .BR epoll (7)
272 instance, then
273 .BR epoll_wait (2)
274 returns events only for signals sent to that process.
275 In particular, if the process then uses
276 .BR fork (2)
277 to create a child process, then the child will be able to
278 .BR read (2)
279 signals that are sent to it using the signalfd file descriptor, but
280 .BR epoll_wait (2)
281 will
282 .B not
283 indicate that the signalfd file descriptor is ready.
284 In this scenario, a possible workaround is that after the
285 .BR fork (2),
286 the child process can close the signalfd file descriptor that it inherited
287 from the parent process and then create another signalfd file descriptor
288 and add it to the epoll instance.
289 Alternatively, the parent and the child could delay creating their
290 (separate) signalfd file descriptors and adding them to the
291 epoll instance until after the call to
292 .BR fork (2).
293 .SH RETURN VALUE
294 On success,
295 .BR signalfd ()
296 returns a signalfd file descriptor;
297 this is either a new file descriptor (if
298 .I fd
299 was \-1), or
300 .I fd
302 .I fd
303 was a valid signalfd file descriptor.
304 On error, \-1 is returned and
305 .I errno
306 is set to indicate the error.
307 .SH ERRORS
309 .B EBADF
311 .I fd
312 file descriptor is not a valid file descriptor.
314 .B EINVAL
315 .I fd
316 is not a valid signalfd file descriptor.
317 .\" or, the
318 .\" .I sizemask
319 .\" argument is not equal to
320 .\" .IR sizeof(sigset_t) ;
322 .B EINVAL
323 .I flags
324 is invalid;
325 or, in Linux 2.6.26 or earlier,
326 .I flags
327 is nonzero.
329 .B EMFILE
330 The per-process limit on the number of open file descriptors has been reached.
332 .B ENFILE
333 The system-wide limit on the total number of open files has been
334 reached.
336 .B ENODEV
337 Could not mount (internal) anonymous inode device.
339 .B ENOMEM
340 There was insufficient memory to create a new signalfd file descriptor.
341 .SH VERSIONS
342 .BR signalfd ()
343 is available on Linux since kernel 2.6.22.
344 Working support is provided in glibc since version 2.8.
345 .\" signalfd() is in glibc 2.7, but reportedly does not build
347 .BR signalfd4 ()
348 system call (see NOTES) is available on Linux since kernel 2.6.27.
349 .SH CONFORMING TO
350 .BR signalfd ()
352 .BR signalfd4 ()
353 are Linux-specific.
354 .SH NOTES
355 A process can create multiple signalfd file descriptors.
356 This makes it possible to accept different signals
357 on different file descriptors.
358 (This may be useful if monitoring the file descriptors using
359 .BR select (2),
360 .BR poll (2),
362 .BR epoll (7):
363 the arrival of different signals will make different file descriptors ready.)
364 If a signal appears in the
365 .I mask
366 of more than one of the file descriptors, then occurrences
367 of that signal can be read (once) from any one of the file descriptors.
369 Attempts to include
370 .B SIGKILL
372 .B SIGSTOP
374 .I mask
375 are silently ignored.
377 The signal mask employed by a signalfd file descriptor can be viewed
378 via the entry for the corresponding file descriptor in the process's
379 .IR /proc/[pid]/fdinfo
380 directory.
382 .BR proc (5)
383 for further details.
385 .SS Limitations
386 The signalfd mechanism can't be used to receive signals that
387 are synchronously generated, such as the
388 .BR SIGSEGV
389 signal that results from accessing an invalid memory address
390 or the
391 .BR SIGFPE
392 signal that results from an arithmetic error.
393 Such signals can be caught only via signal handler.
395 As described above,
396 in normal usage one blocks the signals that will be accepted via
397 .BR signalfd ().
398 If spawning a child process to execute a helper program
399 (that does not need the signalfd file descriptor),
400 then, after the call to
401 .BR fork (2),
402 you will normally want to unblock those signals before calling
403 .BR execve (2),
404 so that the helper program can see any signals that it expects to see.
405 Be aware, however,
406 that this won't be possible in the case of a helper program spawned
407 behind the scenes by any library function that the program may call.
408 In such cases, one must fall back to using a traditional signal
409 handler that writes to a file descriptor monitored by
410 .BR select (2),
411 .BR poll (2),
413 .BR epoll (7).
415 .SS C library/kernel differences
416 The underlying Linux system call requires an additional argument,
417 .IR "size_t sizemask" ,
418 which specifies the size of the
419 .I mask
420 argument.
421 The glibc
422 .BR signalfd ()
423 wrapper function does not include this argument,
424 since it provides the required value for the underlying system call.
426 There are two underlying Linux system calls:
427 .BR signalfd ()
428 and the more recent
429 .BR signalfd4 ().
430 The former system call does not implement a
431 .I flags
432 argument.
433 The latter system call implements the
434 .I flags
435 values described above.
436 Starting with glibc 2.9, the
437 .BR signalfd ()
438 wrapper function will use
439 .BR signalfd4 ()
440 where it is available.
441 .SH BUGS
442 In kernels before 2.6.25, the
443 .I ssi_ptr
445 .I ssi_int
446 fields are not filled in with the data accompanying a signal sent by
447 .BR sigqueue (3).
448 .\" The fix also was put into 2.6.24.5
449 .SH EXAMPLES
450 The program below accepts the signals
451 .B SIGINT
453 .B SIGQUIT
454 via a signalfd file descriptor.
455 The program terminates after accepting a
456 .B SIGQUIT
457 signal.
458 The following shell session demonstrates the use of the program:
460 .in +4n
462 .RB "$" " ./signalfd_demo"
463 .BR "\(haC" "                   # Control\-C generates SIGINT"
464 Got SIGINT
465 .B \(haC
466 Got SIGINT
467 \fB\(ha\e\fP                    # Control\-\e generates SIGQUIT
468 Got SIGQUIT
472 .SS Program source
475 #include <sys/signalfd.h>
476 #include <signal.h>
477 #include <unistd.h>
478 #include <stdlib.h>
479 #include <stdio.h>
481 #define handle_error(msg) \e
482     do { perror(msg); exit(EXIT_FAILURE); } while (0)
485 main(int argc, char *argv[])
487     sigset_t mask;
488     int sfd;
489     struct signalfd_siginfo fdsi;
490     ssize_t s;
492     sigemptyset(&mask);
493     sigaddset(&mask, SIGINT);
494     sigaddset(&mask, SIGQUIT);
496     /* Block signals so that they aren\(aqt handled
497        according to their default dispositions. */
499     if (sigprocmask(SIG_BLOCK, &mask, NULL) == \-1)
500         handle_error("sigprocmask");
502     sfd = signalfd(\-1, &mask, 0);
503     if (sfd == \-1)
504         handle_error("signalfd");
506     for (;;) {
507         s = read(sfd, &fdsi, sizeof(fdsi));
508         if (s != sizeof(fdsi))
509             handle_error("read");
511         if (fdsi.ssi_signo == SIGINT) {
512             printf("Got SIGINT\en");
513         } else if (fdsi.ssi_signo == SIGQUIT) {
514             printf("Got SIGQUIT\en");
515             exit(EXIT_SUCCESS);
516         } else {
517             printf("Read unexpected signal\en");
518         }
519     }
522 .SH SEE ALSO
523 .BR eventfd (2),
524 .BR poll (2),
525 .BR read (2),
526 .BR select (2),
527 .BR sigaction (2),
528 .BR sigprocmask (2),
529 .BR sigwaitinfo (2),
530 .BR timerfd_create (2),
531 .BR sigsetops (3),
532 .BR sigwait (3),
533 .BR epoll (7),
534 .BR signal (7)