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 2014-07-08 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
69 then it must specify a valid existing signalfd file descriptor, and
71 is used to replace the signal set associated with that 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 new open file description.
82 Using this flag saves extra calls to
84 to achieve the same result.
89 flag on the new file descriptor.
90 See the description of the
94 for reasons why this may be useful.
96 In Linux up to version 2.6.26, the
98 argument is unused, and must be specified as zero.
101 returns a file descriptor that supports the following operations:
104 If one or more of the signals specified in
106 is pending for the process, then the buffer supplied to
108 is used to return one or more
110 structures (see below) that describe the signals.
113 returns information for as many signals as are pending and will
114 fit in the supplied buffer.
115 The buffer must be at least
116 .I "sizeof(struct signalfd_siginfo)"
118 The return value of the
120 is the total number of bytes read.
122 As a consequence of the
124 the signals are consumed,
125 so that they are no longer pending for the process
126 (i.e., will not be caught by signal handlers,
127 and cannot be accepted using
128 .BR sigwaitinfo (2)).
130 If none of the signals in
132 is pending for the process, then the
134 either blocks until one of the signals in
136 is generated for the process,
137 or fails with the error
139 if the file descriptor has been made nonblocking.
141 .BR poll "(2), " select "(2) (and similar)"
142 The file descriptor is readable
150 if one or more of the signals in
152 is pending for the process.
154 The signalfd file descriptor also supports the other file-descriptor
162 When the file descriptor is no longer required it should be closed.
163 When all file descriptors associated with the same signalfd object
164 have been closed, the resources for object are freed by the kernel.
165 .SS The signalfd_siginfo structure
168 structure(s) returned by
170 from a signalfd file descriptor is as follows:
174 struct signalfd_siginfo {
175 uint32_t ssi_signo; /* Signal number */
176 int32_t ssi_errno; /* Error number (unused) */
177 int32_t ssi_code; /* Signal code */
178 uint32_t ssi_pid; /* PID of sender */
179 uint32_t ssi_uid; /* Real UID of sender */
180 int32_t ssi_fd; /* File descriptor (SIGIO) */
181 uint32_t ssi_tid; /* Kernel timer ID (POSIX timers)
182 uint32_t ssi_band; /* Band event (SIGIO) */
183 uint32_t ssi_overrun; /* POSIX timer overrun count */
184 uint32_t ssi_trapno; /* Trap number that caused signal */
185 .\" ssi_trapno is unused on most arches
186 int32_t ssi_status; /* Exit status or signal (SIGCHLD) */
187 int32_t ssi_int; /* Integer sent by sigqueue(3) */
188 uint64_t ssi_ptr; /* Pointer sent by sigqueue(3) */
189 uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */
190 uint64_t ssi_stime; /* System CPU time consumed (SIGCHLD) */
191 uint64_t ssi_addr; /* Address that generated signal
192 (for hardware-generated signals) */
193 .\" FIXME Since Linux 2.6.37 there is 'uint16_t ssi_addr_lsb'
194 .\" in the signalfd_siginfo structure. This needs to be documented.
195 uint8_t pad[\fIX\fP]; /* Pad size to 128 bytes (allow for
196 additional fields in the future) */
201 Each of the fields in this structure
202 is analogous to the similarly named field in the
207 structure is described in
209 Not all fields in the returned
211 structure will be valid for a specific signal;
212 the set of valid fields can be determined from the value returned in the
215 This field is the analog of the
221 .SS fork(2) semantics
224 the child inherits a copy of the signalfd file descriptor.
227 from the file descriptor in the child will return information
228 about signals queued to the child.
229 .SS execve(2) semantics
230 Just like any other file descriptor,
231 a signalfd file descriptor remains open across an
233 unless it has been marked for close-on-exec (see
235 Any signals that were available for reading before the
237 remain available to the newly loaded program.
238 (This is analogous to traditional signal semantics,
239 where a blocked signal that is pending remains pending across an
242 The semantics of signalfd file descriptors in a multithreaded program
243 mirror the standard semantics for signals.
245 when a thread reads from a signalfd file descriptor,
246 it will read the signals that are directed to the thread
247 itself and the signals that are directed to the process
248 (i.e., the entire thread group).
249 (A thread will not be able to read signals that are directed
250 to other threads in the process.)
254 returns a signalfd file descriptor;
255 this is either a new file descriptor (if
261 was a valid signalfd file descriptor.
262 On error, \-1 is returned and
264 is set to indicate the error.
270 file descriptor is not a valid file descriptor.
274 is not a valid signalfd file descriptor.
277 .\" argument is not equal to
278 .\" .IR sizeof(sigset_t) ;
283 or, in Linux 2.6.26 or earlier,
288 The per-process limit of open file descriptors has been reached.
291 The system-wide limit on the total number of open files has been
295 Could not mount (internal) anonymous inode device.
298 There was insufficient memory to create a new signalfd file descriptor.
301 is available on Linux since kernel 2.6.22.
302 Working support is provided in glibc since version 2.8.
303 .\" signalfd() is in glibc 2.7, but reportedly does not build
306 system call (see NOTES) is available on Linux since kernel 2.6.27.
313 A process can create multiple signalfd file descriptors.
314 This makes it possible to accept different signals
315 on different file descriptors.
316 (This may be useful if monitoring the file descriptors using
321 the arrival of different signals will make different descriptors ready.)
322 If a signal appears in the
324 of more than one of the file descriptors, then occurrences
325 of that signal can be read (once) from any one of the descriptors.
326 .SS C library/kernel ABI differences
327 The underlying Linux system call requires an additional argument,
328 .IR "size_t sizemask" ,
329 which specifies the size of the
334 wrapper function does not include this argument,
335 since it provides the required value for the underlying system call.
337 There are two underlying Linux system calls:
341 The former system call does not implement a
344 The latter system call implements the
346 values described above.
347 Starting with glibc 2.9, the
349 wrapper function will use
351 where it is available.
353 In kernels before 2.6.25, the
357 fields are not filled in with the data accompanying a signal sent by
359 .\" The fix also was put into 2.6.24.5
361 The program below accepts the signals
365 via a signalfd file descriptor.
366 The program terminates after accepting a
369 The following shell session demonstrates the use of the program:
373 .RB "$" " ./signalfd_demo"
374 .BR "^C" " # Control\-C generates SIGINT"
378 \fB^\\\fP # Control\-\\ generates SIGQUIT
386 #include <sys/signalfd.h>
392 #define handle_error(msg) \\
393 do { perror(msg); exit(EXIT_FAILURE); } while (0)
396 main(int argc, char *argv[])
400 struct signalfd_siginfo fdsi;
404 sigaddset(&mask, SIGINT);
405 sigaddset(&mask, SIGQUIT);
407 /* Block signals so that they aren\(aqt handled
408 according to their default dispositions */
410 if (sigprocmask(SIG_BLOCK, &mask, NULL) == \-1)
411 handle_error("sigprocmask");
413 sfd = signalfd(\-1, &mask, 0);
415 handle_error("signalfd");
418 s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
419 if (s != sizeof(struct signalfd_siginfo))
420 handle_error("read");
422 if (fdsi.ssi_signo == SIGINT) {
423 printf("Got SIGINT\\n");
424 } else if (fdsi.ssi_signo == SIGQUIT) {
425 printf("Got SIGQUIT\\n");
428 printf("Read unexpected signal\\n");
441 .BR timerfd_create (2),
447 This page is part of release 3.79 of the Linux
450 A description of the project,
451 information about reporting bugs,
452 and the latest version of this page,
454 \%http://www.kernel.org/doc/man\-pages/.