1 .\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" starting from a version by Davide Libenzi <davidel@xmailserver.org>
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.
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.
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/>.
20 .TH SIGNALFD 2 2019-03-06 Linux "Linux Programmer's Manual"
22 signalfd \- create a file descriptor for accepting signals
24 .B #include <sys/signalfd.h>
26 .BI "int signalfd(int " fd ", const sigset_t *" mask ", int " flags );
29 creates a file descriptor that can be used to accept signals
30 targeted at the caller.
31 This provides an alternative to the use of a signal handler or
33 and has the advantage that the file descriptor may be monitored by
41 argument specifies the set of signals that the caller
42 wishes to accept via the file descriptor.
43 This argument is a signal set whose contents can be initialized
44 using the macros described in
46 Normally, the set of signals to be received via the
47 file descriptor should be blocked using
49 to prevent the signals being handled according to their default
51 It is not possible to receive
55 signals via a signalfd file descriptor;
56 these signals are silently ignored if specified in
62 then the call creates a new file descriptor and associates the
63 signal set specified in
65 with that file descriptor.
69 then it must specify a valid existing signalfd file descriptor, and
71 is used to replace the signal set associated with that file descriptor.
73 Starting with Linux 2.6.27, the following values may be bitwise ORed in
75 to change the behavior of
81 file status flag on the open file description (see
83 referred to by the new file descriptor.
84 Using this flag saves extra calls to
86 to achieve the same result.
91 flag on the new file descriptor.
92 See the description of the
96 for reasons why this may be useful.
98 In Linux up to version 2.6.26, the
100 argument is unused, and must be specified as zero.
103 returns a file descriptor that supports the following operations:
106 If one or more of the signals specified in
108 is pending for the process, then the buffer supplied to
110 is used to return one or more
112 structures (see below) that describe the signals.
115 returns information for as many signals as are pending and will
116 fit in the supplied buffer.
117 The buffer must be at least
118 .I "sizeof(struct signalfd_siginfo)"
120 The return value of the
122 is the total number of bytes read.
124 As a consequence of the
126 the signals are consumed,
127 so that they are no longer pending for the process
128 (i.e., will not be caught by signal handlers,
129 and cannot be accepted using
130 .BR sigwaitinfo (2)).
132 If none of the signals in
134 is pending for the process, then the
136 either blocks until one of the signals in
138 is generated for the process,
139 or fails with the error
141 if the file descriptor has been made nonblocking.
143 .BR poll "(2), " select "(2) (and similar)"
144 The file descriptor is readable
152 if one or more of the signals in
154 is pending for the process.
156 The signalfd file descriptor also supports the other file-descriptor
164 When the file descriptor is no longer required it should be closed.
165 When all file descriptors associated with the same signalfd object
166 have been closed, the resources for object are freed by the kernel.
167 .SS The signalfd_siginfo structure
170 structure(s) returned by
172 from a signalfd file descriptor is as follows:
176 struct signalfd_siginfo {
177 uint32_t ssi_signo; /* Signal number */
178 int32_t ssi_errno; /* Error number (unused) */
179 int32_t ssi_code; /* Signal code */
180 uint32_t ssi_pid; /* PID of sender */
181 uint32_t ssi_uid; /* Real UID of sender */
182 int32_t ssi_fd; /* File descriptor (SIGIO) */
183 uint32_t ssi_tid; /* Kernel timer ID (POSIX timers)
184 uint32_t ssi_band; /* Band event (SIGIO) */
185 uint32_t ssi_overrun; /* POSIX timer overrun count */
186 uint32_t ssi_trapno; /* Trap number that caused signal */
187 .\" ssi_trapno is unused on most arches
188 int32_t ssi_status; /* Exit status or signal (SIGCHLD) */
189 int32_t ssi_int; /* Integer sent by sigqueue(3) */
190 uint64_t ssi_ptr; /* Pointer sent by sigqueue(3) */
191 uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */
192 uint64_t ssi_stime; /* System CPU time consumed
194 uint64_t ssi_addr; /* Address that generated signal
195 (for hardware-generated signals) */
196 uint16_t ssi_addr_lsb; /* Least significant bit of address
197 (SIGBUS; since Linux 2.6.37)
198 .\" ssi_addr_lsb: commit b8aeec34175fc8fe8b0d40efea4846dfc1ba663e
199 uint8_t pad[\fIX\fP]; /* Pad size to 128 bytes (allow for
200 additional fields in the future) */
205 Each of the fields in this structure
206 is analogous to the similarly named field in the
211 structure is described in
213 Not all fields in the returned
215 structure will be valid for a specific signal;
216 the set of valid fields can be determined from the value returned in the
219 This field is the analog of the
225 .SS fork(2) semantics
228 the child inherits a copy of the signalfd file descriptor.
231 from the file descriptor in the child will return information
232 about signals queued to the child.
233 .SS Semantics of file descriptor passing
234 As with other file descriptors,
235 signalfd file descriptors can be passed to another process
236 via a UNIX domain socket (see
238 In the receiving process, a
240 from the received file descriptor will return information
241 about signals queued to that process.
242 .SS execve(2) semantics
243 Just like any other file descriptor,
244 a signalfd file descriptor remains open across an
246 unless it has been marked for close-on-exec (see
248 Any signals that were available for reading before the
250 remain available to the newly loaded program.
251 (This is analogous to traditional signal semantics,
252 where a blocked signal that is pending remains pending across an
255 The semantics of signalfd file descriptors in a multithreaded program
256 mirror the standard semantics for signals.
258 when a thread reads from a signalfd file descriptor,
259 it will read the signals that are directed to the thread
260 itself and the signals that are directed to the process
261 (i.e., the entire thread group).
262 (A thread will not be able to read signals that are directed
263 to other threads in the process.)
267 returns a signalfd file descriptor;
268 this is either a new file descriptor (if
274 was a valid signalfd file descriptor.
275 On error, \-1 is returned and
277 is set to indicate the error.
283 file descriptor is not a valid file descriptor.
287 is not a valid signalfd file descriptor.
290 .\" argument is not equal to
291 .\" .IR sizeof(sigset_t) ;
296 or, in Linux 2.6.26 or earlier,
301 The per-process limit on the number of open file descriptors has been reached.
304 The system-wide limit on the total number of open files has been
308 Could not mount (internal) anonymous inode device.
311 There was insufficient memory to create a new signalfd file descriptor.
314 is available on Linux since kernel 2.6.22.
315 Working support is provided in glibc since version 2.8.
316 .\" signalfd() is in glibc 2.7, but reportedly does not build
319 system call (see NOTES) is available on Linux since kernel 2.6.27.
326 A process can create multiple signalfd file descriptors.
327 This makes it possible to accept different signals
328 on different file descriptors.
329 (This may be useful if monitoring the file descriptors using
334 the arrival of different signals will make different file descriptors ready.)
335 If a signal appears in the
337 of more than one of the file descriptors, then occurrences
338 of that signal can be read (once) from any one of the file descriptors.
346 are silently ignored.
348 The signal mask employed by a signalfd file descriptor can be viewed
349 via the entry for the corresponding file descriptor in the process's
350 .IR /proc/[pid]/fdinfo
357 The signalfd mechanism can't be used to receive signals that
358 are synchronously generated, such as the
360 signal that results from accessing an invalid memory address
363 signal that results from an arithmetic error.
364 Such signals can be caught only via signal handler.
367 in normal usage one blocks the signals that will be accepted via
369 If spawning a child process to execute a helper program
370 (that does not need the signalfd file descriptor),
371 then, after the call to
373 you will normally want to unblock those signals before calling
375 so that the helper program can see any signals that it expects to see.
377 that this won't be possible in the case of a helper program spawned
378 behind the scenes by any library function that the program may call.
379 In such cases, one must fall back to using a traditional signal
380 handler that writes to a file descriptor monitored by
386 .SS C library/kernel differences
387 The underlying Linux system call requires an additional argument,
388 .IR "size_t sizemask" ,
389 which specifies the size of the
394 wrapper function does not include this argument,
395 since it provides the required value for the underlying system call.
397 There are two underlying Linux system calls:
401 The former system call does not implement a
404 The latter system call implements the
406 values described above.
407 Starting with glibc 2.9, the
409 wrapper function will use
411 where it is available.
413 In kernels before 2.6.25, the
417 fields are not filled in with the data accompanying a signal sent by
419 .\" The fix also was put into 2.6.24.5
421 The program below accepts the signals
425 via a signalfd file descriptor.
426 The program terminates after accepting a
429 The following shell session demonstrates the use of the program:
433 .RB "$" " ./signalfd_demo"
434 .BR "^C" " # Control\-C generates SIGINT"
438 \fB^\e\fP # Control\-\e generates SIGQUIT
446 #include <sys/signalfd.h>
452 #define handle_error(msg) \e
453 do { perror(msg); exit(EXIT_FAILURE); } while (0)
456 main(int argc, char *argv[])
460 struct signalfd_siginfo fdsi;
464 sigaddset(&mask, SIGINT);
465 sigaddset(&mask, SIGQUIT);
467 /* Block signals so that they aren\(aqt handled
468 according to their default dispositions */
470 if (sigprocmask(SIG_BLOCK, &mask, NULL) == \-1)
471 handle_error("sigprocmask");
473 sfd = signalfd(\-1, &mask, 0);
475 handle_error("signalfd");
478 s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
479 if (s != sizeof(struct signalfd_siginfo))
480 handle_error("read");
482 if (fdsi.ssi_signo == SIGINT) {
483 printf("Got SIGINT\en");
484 } else if (fdsi.ssi_signo == SIGQUIT) {
485 printf("Got SIGQUIT\en");
488 printf("Read unexpected signal\en");
501 .BR timerfd_create (2),