]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/signalfd.2
accept.2, dup.2, eventfd.2, execve.2, fcntl.2, memfd_create.2, open.2, perf_event_ope...
[thirdparty/man-pages.git] / man2 / signalfd.2
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 2015-12-05 Linux "Linux Programmer's Manual"
21 .SH NAME
22 signalfd \- create a file descriptor for accepting signals
23 .SH SYNOPSIS
24 .B #include <sys/signalfd.h>
25 .sp
26 .BI "int signalfd(int " fd ", const sigset_t *" mask ", int " flags );
27 .SH DESCRIPTION
28 .BR signalfd ()
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
32 .BR sigwaitinfo (2),
33 and has the advantage that the file descriptor may be monitored by
34 .BR select (2),
35 .BR poll (2),
36 and
37 .BR epoll (7).
38
39 The
40 .I mask
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
45 .BR sigsetops (3).
46 Normally, the set of signals to be received via the
47 file descriptor should be blocked using
48 .BR sigprocmask (2),
49 to prevent the signals being handled according to their default
50 dispositions.
51 It is not possible to receive
52 .B SIGKILL
53 or
54 .B SIGSTOP
55 signals via a signalfd file descriptor;
56 these signals are silently ignored if specified in
57 .IR mask .
58
59 If the
60 .I fd
61 argument is \-1,
62 then the call creates a new file descriptor and associates the
63 signal set specified in
64 .I mask
65 with that descriptor.
66 If
67 .I fd
68 is not \-1,
69 then it must specify a valid existing signalfd file descriptor, and
70 .I mask
71 is used to replace the signal set associated with that descriptor.
72
73 Starting with Linux 2.6.27, the following values may be bitwise ORed in
74 .IR flags
75 to change the behavior of
76 .BR signalfd ():
77 .TP 14
78 .B SFD_NONBLOCK
79 Set the
80 .BR O_NONBLOCK
81 file status flag on the new open file description.
82 Using this flag saves extra calls to
83 .BR fcntl (2)
84 to achieve the same result.
85 .TP
86 .B SFD_CLOEXEC
87 Set the close-on-exec
88 .RB ( FD_CLOEXEC )
89 flag on the new file descriptor.
90 See the description of the
91 .B O_CLOEXEC
92 flag in
93 .BR open (2)
94 for reasons why this may be useful.
95 .PP
96 In Linux up to version 2.6.26, the
97 .I flags
98 argument is unused, and must be specified as zero.
99
100 .BR signalfd ()
101 returns a file descriptor that supports the following operations:
102 .TP
103 .BR read (2)
104 If one or more of the signals specified in
105 .I mask
106 is pending for the process, then the buffer supplied to
107 .BR read (2)
108 is used to return one or more
109 .I signalfd_siginfo
110 structures (see below) that describe the signals.
111 The
112 .BR read (2)
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)"
117 bytes.
118 The return value of the
119 .BR read (2)
120 is the total number of bytes read.
121 .IP
122 As a consequence of the
123 .BR read (2),
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)).
129 .IP
130 If none of the signals in
131 .I mask
132 is pending for the process, then the
133 .BR read (2)
134 either blocks until one of the signals in
135 .I mask
136 is generated for the process,
137 or fails with the error
138 .B EAGAIN
139 if the file descriptor has been made nonblocking.
140 .TP
141 .BR poll "(2), " select "(2) (and similar)"
142 The file descriptor is readable
143 (the
144 .BR select (2)
145 .I readfds
146 argument; the
147 .BR poll (2)
148 .B POLLIN
149 flag)
150 if one or more of the signals in
151 .I mask
152 is pending for the process.
153 .IP
154 The signalfd file descriptor also supports the other file-descriptor
155 multiplexing APIs:
156 .BR pselect (2),
157 .BR ppoll (2),
158 and
159 .BR epoll (7).
160 .TP
161 .BR close (2)
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
166 The format of the
167 .I signalfd_siginfo
168 structure(s) returned by
169 .BR read (2)s
170 from a signalfd file descriptor is as follows:
171 .in +4n
172 .nf
173
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) */
197 };
198
199 .fi
200 .in
201 Each of the fields in this structure
202 is analogous to the similarly named field in the
203 .I siginfo_t
204 structure.
205 The
206 .I siginfo_t
207 structure is described in
208 .BR sigaction (2).
209 Not all fields in the returned
210 .I signalfd_siginfo
211 structure will be valid for a specific signal;
212 the set of valid fields can be determined from the value returned in the
213 .I ssi_code
214 field.
215 This field is the analog of the
216 .I siginfo_t
217 .I si_code
218 field; see
219 .BR sigaction (2)
220 for details.
221 .SS fork(2) semantics
222 After a
223 .BR fork (2),
224 the child inherits a copy of the signalfd file descriptor.
225 A
226 .BR read (2)
227 from the file descriptor in the child will return information
228 about signals queued to the child.
229 .SS Semantics of file descriptor passing
230 As with other file descriptors,
231 signalfd file descriptors can be passed to another process
232 via a UNIX domain socket (see
233 .BR unix (7)).
234 In the receiving process, a
235 .BR read (2)
236 from the received file descriptor will return information
237 about signals queued to that process.
238 .SS execve(2) semantics
239 Just like any other file descriptor,
240 a signalfd file descriptor remains open across an
241 .BR execve (2),
242 unless it has been marked for close-on-exec (see
243 .BR fcntl (2)).
244 Any signals that were available for reading before the
245 .BR execve (2)
246 remain available to the newly loaded program.
247 (This is analogous to traditional signal semantics,
248 where a blocked signal that is pending remains pending across an
249 .BR execve (2).)
250 .SS Thread semantics
251 The semantics of signalfd file descriptors in a multithreaded program
252 mirror the standard semantics for signals.
253 In other words,
254 when a thread reads from a signalfd file descriptor,
255 it will read the signals that are directed to the thread
256 itself and the signals that are directed to the process
257 (i.e., the entire thread group).
258 (A thread will not be able to read signals that are directed
259 to other threads in the process.)
260 .SH RETURN VALUE
261 On success,
262 .BR signalfd ()
263 returns a signalfd file descriptor;
264 this is either a new file descriptor (if
265 .I fd
266 was \-1), or
267 .I fd
268 if
269 .I fd
270 was a valid signalfd file descriptor.
271 On error, \-1 is returned and
272 .I errno
273 is set to indicate the error.
274 .SH ERRORS
275 .TP
276 .B EBADF
277 The
278 .I fd
279 file descriptor is not a valid file descriptor.
280 .TP
281 .B EINVAL
282 .I fd
283 is not a valid signalfd file descriptor.
284 .\" or, the
285 .\" .I sizemask
286 .\" argument is not equal to
287 .\" .IR sizeof(sigset_t) ;
288 .TP
289 .B EINVAL
290 .I flags
291 is invalid;
292 or, in Linux 2.6.26 or earlier,
293 .I flags
294 is nonzero.
295 .TP
296 .B EMFILE
297 The per-process limit on the number of open file descriptors has been reached.
298 .TP
299 .B ENFILE
300 The system-wide limit on the total number of open files has been
301 reached.
302 .TP
303 .B ENODEV
304 Could not mount (internal) anonymous inode device.
305 .TP
306 .B ENOMEM
307 There was insufficient memory to create a new signalfd file descriptor.
308 .SH VERSIONS
309 .BR signalfd ()
310 is available on Linux since kernel 2.6.22.
311 Working support is provided in glibc since version 2.8.
312 .\" signalfd() is in glibc 2.7, but reportedly does not build
313 The
314 .BR signalfd4 ()
315 system call (see NOTES) is available on Linux since kernel 2.6.27.
316 .SH CONFORMING TO
317 .BR signalfd ()
318 and
319 .BR signalfd4 ()
320 are Linux-specific.
321 .SH NOTES
322 A process can create multiple signalfd file descriptors.
323 This makes it possible to accept different signals
324 on different file descriptors.
325 (This may be useful if monitoring the file descriptors using
326 .BR select (2),
327 .BR poll (2),
328 or
329 .BR epoll (7):
330 the arrival of different signals will make different descriptors ready.)
331 If a signal appears in the
332 .I mask
333 of more than one of the file descriptors, then occurrences
334 of that signal can be read (once) from any one of the descriptors.
335 .SS C library/kernel differences
336 The underlying Linux system call requires an additional argument,
337 .IR "size_t sizemask" ,
338 which specifies the size of the
339 .I mask
340 argument.
341 The glibc
342 .BR signalfd ()
343 wrapper function does not include this argument,
344 since it provides the required value for the underlying system call.
345
346 There are two underlying Linux system calls:
347 .BR signalfd ()
348 and the more recent
349 .BR signalfd4 ().
350 The former system call does not implement a
351 .I flags
352 argument.
353 The latter system call implements the
354 .I flags
355 values described above.
356 Starting with glibc 2.9, the
357 .BR signalfd ()
358 wrapper function will use
359 .BR signalfd4 ()
360 where it is available.
361 .SH BUGS
362 In kernels before 2.6.25, the
363 .I ssi_ptr
364 and
365 .I ssi_int
366 fields are not filled in with the data accompanying a signal sent by
367 .BR sigqueue (3).
368 .\" The fix also was put into 2.6.24.5
369 .SH EXAMPLE
370 The program below accepts the signals
371 .B SIGINT
372 and
373 .B SIGQUIT
374 via a signalfd file descriptor.
375 The program terminates after accepting a
376 .B SIGQUIT
377 signal.
378 The following shell session demonstrates the use of the program:
379 .in +4n
380 .nf
381
382 .RB "$" " ./signalfd_demo"
383 .BR "^C" " # Control\-C generates SIGINT"
384 Got SIGINT
385 .B ^C
386 Got SIGINT
387 \fB^\\\fP # Control\-\\ generates SIGQUIT
388 Got SIGQUIT
389 $
390 .fi
391 .in
392 .SS Program source
393 \&
394 .nf
395 #include <sys/signalfd.h>
396 #include <signal.h>
397 #include <unistd.h>
398 #include <stdlib.h>
399 #include <stdio.h>
400
401 #define handle_error(msg) \\
402 do { perror(msg); exit(EXIT_FAILURE); } while (0)
403
404 int
405 main(int argc, char *argv[])
406 {
407 sigset_t mask;
408 int sfd;
409 struct signalfd_siginfo fdsi;
410 ssize_t s;
411
412 sigemptyset(&mask);
413 sigaddset(&mask, SIGINT);
414 sigaddset(&mask, SIGQUIT);
415
416 /* Block signals so that they aren\(aqt handled
417 according to their default dispositions */
418
419 if (sigprocmask(SIG_BLOCK, &mask, NULL) == \-1)
420 handle_error("sigprocmask");
421
422 sfd = signalfd(\-1, &mask, 0);
423 if (sfd == \-1)
424 handle_error("signalfd");
425
426 for (;;) {
427 s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
428 if (s != sizeof(struct signalfd_siginfo))
429 handle_error("read");
430
431 if (fdsi.ssi_signo == SIGINT) {
432 printf("Got SIGINT\\n");
433 } else if (fdsi.ssi_signo == SIGQUIT) {
434 printf("Got SIGQUIT\\n");
435 exit(EXIT_SUCCESS);
436 } else {
437 printf("Read unexpected signal\\n");
438 }
439 }
440 }
441 .fi
442 .SH SEE ALSO
443 .BR eventfd (2),
444 .BR poll (2),
445 .BR read (2),
446 .BR select (2),
447 .BR sigaction (2),
448 .BR sigprocmask (2),
449 .BR sigwaitinfo (2),
450 .BR timerfd_create (2),
451 .BR sigsetops (3),
452 .BR sigwait (3),
453 .BR epoll (7),
454 .BR signal (7)