1 .\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" starting from a version by Davide Libenzi <davidel@xmailserver.org>
4 .\" This program is free software; you can redistribute it and/or modify
5 .\" it under the terms of the GNU General Public License as published by
6 .\" the Free Software Foundation; either version 2 of the License, or
7 .\" (at your option) any later version.
9 .\" This program is distributed in the hope that it will be useful,
10 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
11 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 .\" GNU General Public License for more details.
14 .\" You should have received a copy of the GNU General Public License
15 .\" along with this program; if not, write to the Free Software
16 .\" Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19 .TH SIGNALFD 2 2009-01-13 Linux "Linux Programmer's Manual"
21 signalfd \- create a file descriptor for accepting signals
23 .B #include <sys/signalfd.h>
25 .BI "int signalfd(int " fd ", const sigset_t *" mask ", int " flags );
28 creates a file descriptor that can be used to accept signals
29 targeted at the caller.
30 This provides an alternative to the use of a signal handler or
32 and has the advantage that the file descriptor may be monitored by
40 argument specifies the set of signals that the caller
41 wishes to accept via the file descriptor.
42 This argument is a signal set whose contents can be initialized
43 using the macros described in
45 Normally, the set of signals to be received via the
46 file descriptor should be blocked using
48 to prevent the signals being handled according to their default
50 It is not possible to receive
54 signals via a signalfd file descriptor;
55 these signals are silently ignored if specified in
61 then the call creates a new file descriptor and associates the
62 signal set specified in
68 then it must specify a valid existing signalfd file descriptor, and
70 is used to replace the signal set associated with that descriptor.
72 Starting with Linux 2.6.27, the following values may be bitwise ORed in
74 to change the behaviour of
80 file status flag on the new open file description.
81 Using this flag saves extra calls to
83 to achieve the same result.
88 flag on the new file descriptor.
89 See the description of the
93 for reasons why this may be useful.
95 In Linux up to version 2.6.26, the
97 argument is unused, and must be specified as zero.
100 returns a file descriptor that supports the following operations:
103 If one or more of the signals specified in
105 is pending for the process, then the buffer supplied to
107 is used to return one or more
109 structures (see below) that describe the signals.
112 returns information for as many signals as are pending and will
113 fit in the supplied buffer.
114 The buffer must be at least
115 .I "sizeof(struct signalfd_siginfo)"
117 The return value of the
119 is the total number of bytes read.
121 As a consequence of the
123 the signals are consumed,
124 so that they are no longer pending for the process
125 (i.e., will not be caught by signal handlers,
126 and cannot be accepted using
127 .BR sigwaitinfo (2)).
129 If none of the signals in
131 is pending for the process, then the
133 either blocks until one of the signals in
135 is generated for the process,
136 or fails with the error
138 if the file descriptor has been made nonblocking.
140 .BR poll "(2), " select "(2) (and similar)"
141 The file descriptor is readable
149 if one or more of the signals in
151 is pending for the process.
153 The signalfd file descriptor also supports the other file-descriptor
161 When the file descriptor is no longer required it should be closed.
162 When all file descriptors associated with the same signalfd object
163 have been closed, the resources for object are freed by the kernel.
164 .SS The signalfd_siginfo structure
167 structure(s) returned by
169 from a signalfd file descriptor is as follows:
173 struct signalfd_siginfo {
174 uint32_t ssi_signo; /* Signal number */
175 int32_t ssi_errno; /* Error number (unused) */
176 int32_t ssi_code; /* Signal code */
177 uint32_t ssi_pid; /* PID of sender */
178 uint32_t ssi_uid; /* Real UID of sender */
179 int32_t ssi_fd; /* File descriptor (SIGIO) */
180 uint32_t ssi_tid; /* Kernel timer ID (POSIX timers)
181 uint32_t ssi_band; /* Band event (SIGIO) */
182 uint32_t ssi_overrun; /* POSIX timer overrun count */
183 uint32_t ssi_trapno; /* Trap number that caused signal */
184 .\" ssi_trapno is unused on most arches
185 int32_t ssi_status; /* Exit status or signal (SIGCHLD) */
186 int32_t ssi_int; /* Integer sent by sigqueue(2) */
187 uint64_t ssi_ptr; /* Pointer sent by sigqueue(2) */
188 uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */
189 uint64_t ssi_stime; /* System CPU time consumed (SIGCHLD) */
190 uint64_t ssi_addr; /* Address that generated signal
191 (for hardware-generated signals) */
192 uint8_t pad[\fIX\fP]; /* Pad size to 128 bytes (allow for
193 additional fields in the future) */
198 Each of the fields in this structure
199 is analogous to the similarly named field in the
204 structure is described in
206 Not all fields in the returned
208 structure will be valid for a specific signal;
209 the set of valid fields can be determined from the value returned in the
212 This field is the analog of the
218 .SS fork(2) semantics
221 the child inherits a copy of the signalfd file descriptor.
224 from the file descriptor in the child will return information
225 about signals queued to the child.
226 .SS execve(2) semantics
227 Just like any other file descriptor,
228 a signalfd file descriptor remains open across an
230 unless it has been marked for close-on-exec (see
232 Any signals that were available for reading before the
234 remain available to the newly loaded program.
235 (This is analogous to traditional signal semantics,
236 where a blocked signal that is pending remains pending across an
239 The semantics of signalfd file descriptors in a multithreaded program
240 mirror the standard semantics for signals.
242 when a thread reads from a signalfd file descriptor,
243 it will read the signals that are directed to the thread
244 itself and the signals that are directed to the process
245 (i.e., the entire thread group).
246 (A thread will not be able to read signals that are directed
247 to other threads in the process.)
251 returns a signalfd file descriptor;
252 this is either a new file descriptor (if
258 was a valid signalfd file descriptor.
259 On error, \-1 is returned and
261 is set to indicate the error.
267 file descriptor is not a valid file descriptor.
271 is not a valid signalfd file descriptor.
274 .\" argument is not equal to
275 .\" .IR sizeof(sigset_t) ;
280 or, in Linux 2.6.26 or earlier,
285 The per-process limit of open file descriptors has been reached.
288 The system-wide limit on the total number of open files has been
292 Could not mount (internal) anonymous inode device.
295 There was insufficient memory to create a new signalfd file descriptor.
298 is available on Linux since kernel 2.6.22.
299 Working support is provided in glibc since version 2.8.
300 .\" signalfd() is in glibc 2.7, but reportedly does not build
303 system call (see NOTES) is available on Linux since kernel 2.6.27.
310 The underlying Linux system call requires an additional argument,
311 .IR "size_t sizemask" ,
312 which specifies the size of the
317 wrapper function does not include this argument,
318 since it provides the required value for the underlying system call.
320 A process can create multiple signalfd file descriptors.
321 This makes it possible to accept different signals
322 on different file descriptors.
323 (This may be useful if monitoring the file descriptors using
328 the arrival of different signals will make different descriptors ready.)
329 If a signal appears in the
331 of more than one of the file descriptors, then occurrences
332 of that signal can be read (once) from any one of the descriptors.
333 .SS Underlying Linux system calls
334 There are two underlying Linux system calls:
338 The former system call does not implement a
341 The latter system call implements the
343 values described above.
344 Starting with glibc 2.9, the
346 wrapper function will use
348 where it is available.
350 In kernels before 2.6.25, the
354 fields are not filled in with the data accompanying a signal sent by
356 .\" The fix also was put into 2.6.24.5
358 The program below accepts the signals
362 via a signalfd file descriptor.
363 The program terminates after accepting a
366 The following shell session demonstrates the use of the program:
370 .RB "$" " ./signalfd_demo"
371 .BR "^C" " # Control\-C generates SIGINT"
375 \fB^\\\fP # Control\-\\ generates SIGQUIT
383 #include <sys/signalfd.h>
389 #define handle_error(msg) \\
390 do { perror(msg); exit(EXIT_FAILURE); } while (0)
393 main(int argc, char *argv[])
397 struct signalfd_siginfo fdsi;
401 sigaddset(&mask, SIGINT);
402 sigaddset(&mask, SIGQUIT);
404 /* Block signals so that they aren\(aqt handled
405 according to their default dispositions */
407 if (sigprocmask(SIG_BLOCK, &mask, NULL) == \-1)
408 handle_error("sigprocmask");
410 sfd = signalfd(\-1, &mask, 0);
412 handle_error("signalfd");
415 s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
416 if (s != sizeof(struct signalfd_siginfo))
417 handle_error("read");
419 if (fdsi.ssi_signo == SIGINT) {
420 printf("Got SIGINT\\n");
421 } else if (fdsi.ssi_signo == SIGQUIT) {
422 printf("Got SIGQUIT\\n");
425 printf("Read unexpected signal\\n");
438 .BR timerfd_create (2),