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 2008-10-20 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.
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 non-blocking.
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(2) */
188 uint64_t ssi_ptr; /* Pointer sent by sigqueue(2) */
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 uint8_t pad[\fIX\fP]; /* Pad size to 128 bytes (allow for
194 additional fields in the future) */
199 Each of the fields in this structure
200 is analogous to the similarly named field in the
205 structure is described in
207 Not all fields in the returned
209 structure will be valid for a specific signal;
210 the set of valid fields can be determined from the value returned in the
213 This field is the analog of the
219 .SS fork(2) semantics
222 the child inherits a copy of the signalfd file descriptor.
223 The file descriptor refers to the same underlying
224 file object as the corresponding descriptor in the parent,
227 in the child will return information about signals generated
229 (the process that created the object using
231 .SS execve(2) semantics
232 Just like any other file descriptor,
233 a signalfd file descriptor remains open across an
235 unless it has been marked for close-on-exec (see
237 Any signals that were available for reading before the
239 remain available to the newly loaded program.
240 (This is analogous to traditional signal semantics,
241 where a blocked signal that is pending remains pending across an
244 The semantics of signalfd file descriptors in a multithreaded program
245 mirror the standard semantics for signals.
247 when a thread reads from a signalfd file descriptor,
248 it will read the signals that are directed to the thread
249 itself and the signals that are directed to the process
250 (i.e., the entire thread group).
251 (A thread will not be able to read signals that are directed
252 to other threads in the process.)
256 returns a signalfd file descriptor;
257 this is either a new file descriptor (if
263 was a valid signalfd file descriptor.
264 On error, \-1 is returned and
266 is set to indicate the error.
272 file descriptor is not a valid file descriptor.
276 is not a valid signalfd file descriptor.
279 .\" argument is not equal to
280 .\" .IR sizeof(sigset_t) ;
285 or, in Linux 2.6.26 or earlier,
290 The per-process limit of open file descriptors has been reached.
293 The system-wide limit on the total number of open files has been
297 Could not mount (internal) anonymous inode device.
300 There was insufficient memory to create a new signalfd file descriptor.
303 is available on Linux since kernel 2.6.22.
304 Working support is provided in glibc since version 2.8.
305 .\" signalfd() is in glibc 2.7, but reportedly does not build
308 system call (see NOTES) is available on Linux since kernel 2.6.27.
315 The underlying Linux system call requires an additional argument,
316 .IR "size_t sizemask" ,
317 which specifies the size of the
322 wrapper function does not include this argument,
323 since it provides the required value for the underlying system call.
325 A process can create multiple signalfd file descriptors.
326 This makes it possible to accept different signals
327 on different file descriptors.
328 (This may be useful if monitoring the file descriptors using
333 the arrival of different signals will make different descriptors ready.)
334 If a signal appears in the
336 of more than one of the file descriptors, then occurrences
337 of that signal can be read (once) from any one of the descriptors.
338 .SS Underlying Linux system calls
339 There are two underlying Linux system calls:
343 The former system call does not implement a
346 The latter system call implements the
348 values described above.
349 Starting with glibc 2.9, the
351 wrapper function will use
353 where it is available.
355 In kernels before 2.6.25, the
359 fields are not filled in with the data accompanying a signal sent by
361 .\" The fix also was put into 2.6.24.5
363 The program below accepts the signals
367 via a signalfd file descriptor.
368 The program terminates after accepting a
371 The following shell session demonstrates the use of the program:
375 .RB "$" " ./signalfd_demo"
376 .BR "^C" " # Control\-C generates SIGINT"
380 \fB^\\\fP # Control\-\\ generates SIGQUIT
388 #include <sys/signalfd.h>
394 #define handle_error(msg) \\
395 do { perror(msg); exit(EXIT_FAILURE); } while (0)
398 main(int argc, char *argv[])
402 struct signalfd_siginfo fdsi;
406 sigaddset(&mask, SIGINT);
407 sigaddset(&mask, SIGQUIT);
409 /* Block signals so that they aren\(aqt handled
410 according to their default dispositions */
412 if (sigprocmask(SIG_BLOCK, &mask, NULL) == \-1)
413 handle_error("sigprocmask");
415 sfd = signalfd(\-1, &mask, 0);
417 handle_error("signalfd");
420 s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
421 if (s != sizeof(struct signalfd_siginfo))
422 handle_error("read");
424 if (fdsi.ssi_signo == SIGINT) {
425 printf("Got SIGINT\\n");
426 } else if (fdsi.ssi_signo == SIGQUIT) {
427 printf("Got SIGQUIT\\n");
430 printf("Read unexpected signal\\n");
443 .BR timerfd_create (2),