]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/signalfd.2
Many pages: Use correct letter case in page titles (TH)
[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 .\" SPDX-License-Identifier: GPL-2.0-or-later
5 .\"
6 .TH signalfd 2 (date) "Linux man-pages (unreleased)"
7 .SH NAME
8 signalfd \- create a file descriptor for accepting signals
9 .SH LIBRARY
10 Standard C library
11 .RI ( libc ", " \-lc )
12 .SH SYNOPSIS
13 .nf
14 .B #include <sys/signalfd.h>
15 .PP
16 .BI "int signalfd(int " fd ", const sigset_t *" mask ", int " flags );
17 .fi
18 .SH DESCRIPTION
19 .BR signalfd ()
20 creates a file descriptor that can be used to accept signals
21 targeted at the caller.
22 This provides an alternative to the use of a signal handler or
23 .BR sigwaitinfo (2),
24 and has the advantage that the file descriptor may be monitored by
25 .BR select (2),
26 .BR poll (2),
27 and
28 .BR epoll (7).
29 .PP
30 The
31 .I mask
32 argument specifies the set of signals that the caller
33 wishes to accept via the file descriptor.
34 This argument is a signal set whose contents can be initialized
35 using the macros described in
36 .BR sigsetops (3).
37 Normally, the set of signals to be received via the
38 file descriptor should be blocked using
39 .BR sigprocmask (2),
40 to prevent the signals being handled according to their default
41 dispositions.
42 It is not possible to receive
43 .B SIGKILL
44 or
45 .B SIGSTOP
46 signals via a signalfd file descriptor;
47 these signals are silently ignored if specified in
48 .IR mask .
49 .PP
50 If the
51 .I fd
52 argument is \-1,
53 then the call creates a new file descriptor and associates the
54 signal set specified in
55 .I mask
56 with that file descriptor.
57 If
58 .I fd
59 is not \-1,
60 then it must specify a valid existing signalfd file descriptor, and
61 .I mask
62 is used to replace the signal set associated with that file descriptor.
63 .PP
64 Starting with Linux 2.6.27, the following values may be bitwise ORed in
65 .I flags
66 to change the behavior of
67 .BR signalfd ():
68 .TP 14
69 .B SFD_NONBLOCK
70 Set the
71 .B O_NONBLOCK
72 file status flag on the open file description (see
73 .BR open (2))
74 referred to by the new file descriptor.
75 Using this flag saves extra calls to
76 .BR fcntl (2)
77 to achieve the same result.
78 .TP
79 .B SFD_CLOEXEC
80 Set the close-on-exec
81 .RB ( FD_CLOEXEC )
82 flag on the new file descriptor.
83 See the description of the
84 .B O_CLOEXEC
85 flag in
86 .BR open (2)
87 for reasons why this may be useful.
88 .PP
89 In Linux up to version 2.6.26, the
90 .I flags
91 argument is unused, and must be specified as zero.
92 .PP
93 .BR signalfd ()
94 returns a file descriptor that supports the following operations:
95 .TP
96 .BR read (2)
97 If one or more of the signals specified in
98 .I mask
99 is pending for the process, then the buffer supplied to
100 .BR read (2)
101 is used to return one or more
102 .I signalfd_siginfo
103 structures (see below) that describe the signals.
104 The
105 .BR read (2)
106 returns information for as many signals as are pending and will
107 fit in the supplied buffer.
108 The buffer must be at least
109 .I "sizeof(struct signalfd_siginfo)"
110 bytes.
111 The return value of the
112 .BR read (2)
113 is the total number of bytes read.
114 .IP
115 As a consequence of the
116 .BR read (2),
117 the signals are consumed,
118 so that they are no longer pending for the process
119 (i.e., will not be caught by signal handlers,
120 and cannot be accepted using
121 .BR sigwaitinfo (2)).
122 .IP
123 If none of the signals in
124 .I mask
125 is pending for the process, then the
126 .BR read (2)
127 either blocks until one of the signals in
128 .I mask
129 is generated for the process,
130 or fails with the error
131 .B EAGAIN
132 if the file descriptor has been made nonblocking.
133 .TP
134 .BR poll "(2), " select "(2) (and similar)"
135 The file descriptor is readable
136 (the
137 .BR select (2)
138 .I readfds
139 argument; the
140 .BR poll (2)
141 .B POLLIN
142 flag)
143 if one or more of the signals in
144 .I mask
145 is pending for the process.
146 .IP
147 The signalfd file descriptor also supports the other file-descriptor
148 multiplexing APIs:
149 .BR pselect (2),
150 .BR ppoll (2),
151 and
152 .BR epoll (7).
153 .TP
154 .BR close (2)
155 When the file descriptor is no longer required it should be closed.
156 When all file descriptors associated with the same signalfd object
157 have been closed, the resources for object are freed by the kernel.
158 .SS The signalfd_siginfo structure
159 The format of the
160 .I signalfd_siginfo
161 structure(s) returned by
162 .BR read (2)s
163 from a signalfd file descriptor is as follows:
164 .PP
165 .in +4n
166 .EX
167 struct signalfd_siginfo {
168 uint32_t ssi_signo; /* Signal number */
169 int32_t ssi_errno; /* Error number (unused) */
170 int32_t ssi_code; /* Signal code */
171 uint32_t ssi_pid; /* PID of sender */
172 uint32_t ssi_uid; /* Real UID of sender */
173 int32_t ssi_fd; /* File descriptor (SIGIO) */
174 uint32_t ssi_tid; /* Kernel timer ID (POSIX timers)
175 uint32_t ssi_band; /* Band event (SIGIO) */
176 uint32_t ssi_overrun; /* POSIX timer overrun count */
177 uint32_t ssi_trapno; /* Trap number that caused signal */
178 .\" ssi_trapno is unused on most arches
179 int32_t ssi_status; /* Exit status or signal (SIGCHLD) */
180 int32_t ssi_int; /* Integer sent by sigqueue(3) */
181 uint64_t ssi_ptr; /* Pointer sent by sigqueue(3) */
182 uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */
183 uint64_t ssi_stime; /* System CPU time consumed
184 (SIGCHLD) */
185 uint64_t ssi_addr; /* Address that generated signal
186 (for hardware\-generated signals) */
187 uint16_t ssi_addr_lsb; /* Least significant bit of address
188 (SIGBUS; since Linux 2.6.37) */
189 .\" ssi_addr_lsb: commit b8aeec34175fc8fe8b0d40efea4846dfc1ba663e
190 uint8_t pad[\fIX\fP]; /* Pad size to 128 bytes (allow for
191 additional fields in the future) */
192 };
193 .EE
194 .in
195 .PP
196 Each of the fields in this structure
197 is analogous to the similarly named field in the
198 .I siginfo_t
199 structure.
200 The
201 .I siginfo_t
202 structure is described in
203 .BR sigaction (2).
204 Not all fields in the returned
205 .I signalfd_siginfo
206 structure will be valid for a specific signal;
207 the set of valid fields can be determined from the value returned in the
208 .I ssi_code
209 field.
210 This field is the analog of the
211 .I siginfo_t
212 .I si_code
213 field; see
214 .BR sigaction (2)
215 for details.
216 .SS fork(2) semantics
217 After a
218 .BR fork (2),
219 the child inherits a copy of the signalfd file descriptor.
220 A
221 .BR read (2)
222 from the file descriptor in the child will return information
223 about signals queued to the child.
224 .SS Semantics of file descriptor passing
225 As with other file descriptors,
226 signalfd file descriptors can be passed to another process
227 via a UNIX domain socket (see
228 .BR unix (7)).
229 In the receiving process, a
230 .BR read (2)
231 from the received file descriptor will return information
232 about signals queued to that process.
233 .SS execve(2) semantics
234 Just like any other file descriptor,
235 a signalfd file descriptor remains open across an
236 .BR execve (2),
237 unless it has been marked for close-on-exec (see
238 .BR fcntl (2)).
239 Any signals that were available for reading before the
240 .BR execve (2)
241 remain available to the newly loaded program.
242 (This is analogous to traditional signal semantics,
243 where a blocked signal that is pending remains pending across an
244 .BR execve (2).)
245 .SS Thread semantics
246 The semantics of signalfd file descriptors in a multithreaded program
247 mirror the standard semantics for signals.
248 In other words,
249 when a thread reads from a signalfd file descriptor,
250 it will read the signals that are directed to the thread
251 itself and the signals that are directed to the process
252 (i.e., the entire thread group).
253 (A thread will not be able to read signals that are directed
254 to other threads in the process.)
255 .\"
256 .SS epoll(7) semantics
257 If a process adds (via
258 .BR epoll_ctl (2))
259 a signalfd file descriptor to an
260 .BR epoll (7)
261 instance, then
262 .BR epoll_wait (2)
263 returns events only for signals sent to that process.
264 In particular, if the process then uses
265 .BR fork (2)
266 to create a child process, then the child will be able to
267 .BR read (2)
268 signals that are sent to it using the signalfd file descriptor, but
269 .BR epoll_wait (2)
270 will
271 .B not
272 indicate that the signalfd file descriptor is ready.
273 In this scenario, a possible workaround is that after the
274 .BR fork (2),
275 the child process can close the signalfd file descriptor that it inherited
276 from the parent process and then create another signalfd file descriptor
277 and add it to the epoll instance.
278 Alternatively, the parent and the child could delay creating their
279 (separate) signalfd file descriptors and adding them to the
280 epoll instance until after the call to
281 .BR fork (2).
282 .SH RETURN VALUE
283 On success,
284 .BR signalfd ()
285 returns a signalfd file descriptor;
286 this is either a new file descriptor (if
287 .I fd
288 was \-1), or
289 .I fd
290 if
291 .I fd
292 was a valid signalfd file descriptor.
293 On error, \-1 is returned and
294 .I errno
295 is set to indicate the error.
296 .SH ERRORS
297 .TP
298 .B EBADF
299 The
300 .I fd
301 file descriptor is not a valid file descriptor.
302 .TP
303 .B EINVAL
304 .I fd
305 is not a valid signalfd file descriptor.
306 .\" or, the
307 .\" .I sizemask
308 .\" argument is not equal to
309 .\" .IR sizeof(sigset_t) ;
310 .TP
311 .B EINVAL
312 .I flags
313 is invalid;
314 or, in Linux 2.6.26 or earlier,
315 .I flags
316 is nonzero.
317 .TP
318 .B EMFILE
319 The per-process limit on the number of open file descriptors has been reached.
320 .TP
321 .B ENFILE
322 The system-wide limit on the total number of open files has been
323 reached.
324 .TP
325 .B ENODEV
326 Could not mount (internal) anonymous inode device.
327 .TP
328 .B ENOMEM
329 There was insufficient memory to create a new signalfd file descriptor.
330 .SH VERSIONS
331 .BR signalfd ()
332 is available on Linux since kernel 2.6.22.
333 Working support is provided in glibc since version 2.8.
334 .\" signalfd() is in glibc 2.7, but reportedly does not build
335 The
336 .BR signalfd4 ()
337 system call (see NOTES) is available on Linux since kernel 2.6.27.
338 .SH STANDARDS
339 .BR signalfd ()
340 and
341 .BR signalfd4 ()
342 are Linux-specific.
343 .SH NOTES
344 A process can create multiple signalfd file descriptors.
345 This makes it possible to accept different signals
346 on different file descriptors.
347 (This may be useful if monitoring the file descriptors using
348 .BR select (2),
349 .BR poll (2),
350 or
351 .BR epoll (7):
352 the arrival of different signals will make different file descriptors ready.)
353 If a signal appears in the
354 .I mask
355 of more than one of the file descriptors, then occurrences
356 of that signal can be read (once) from any one of the file descriptors.
357 .PP
358 Attempts to include
359 .B SIGKILL
360 and
361 .B SIGSTOP
362 in
363 .I mask
364 are silently ignored.
365 .PP
366 The signal mask employed by a signalfd file descriptor can be viewed
367 via the entry for the corresponding file descriptor in the process's
368 .IR /proc/ pid /fdinfo
369 directory.
370 See
371 .BR proc (5)
372 for further details.
373 .\"
374 .SS Limitations
375 The signalfd mechanism can't be used to receive signals that
376 are synchronously generated, such as the
377 .B SIGSEGV
378 signal that results from accessing an invalid memory address
379 or the
380 .B SIGFPE
381 signal that results from an arithmetic error.
382 Such signals can be caught only via signal handler.
383 .PP
384 As described above,
385 in normal usage one blocks the signals that will be accepted via
386 .BR signalfd ().
387 If spawning a child process to execute a helper program
388 (that does not need the signalfd file descriptor),
389 then, after the call to
390 .BR fork (2),
391 you will normally want to unblock those signals before calling
392 .BR execve (2),
393 so that the helper program can see any signals that it expects to see.
394 Be aware, however,
395 that this won't be possible in the case of a helper program spawned
396 behind the scenes by any library function that the program may call.
397 In such cases, one must fall back to using a traditional signal
398 handler that writes to a file descriptor monitored by
399 .BR select (2),
400 .BR poll (2),
401 or
402 .BR epoll (7).
403 .\"
404 .SS C library/kernel differences
405 The underlying Linux system call requires an additional argument,
406 .IR "size_t sizemask" ,
407 which specifies the size of the
408 .I mask
409 argument.
410 The glibc
411 .BR signalfd ()
412 wrapper function does not include this argument,
413 since it provides the required value for the underlying system call.
414 .PP
415 There are two underlying Linux system calls:
416 .BR signalfd ()
417 and the more recent
418 .BR signalfd4 ().
419 The former system call does not implement a
420 .I flags
421 argument.
422 The latter system call implements the
423 .I flags
424 values described above.
425 Starting with glibc 2.9, the
426 .BR signalfd ()
427 wrapper function will use
428 .BR signalfd4 ()
429 where it is available.
430 .SH BUGS
431 In kernels before 2.6.25, the
432 .I ssi_ptr
433 and
434 .I ssi_int
435 fields are not filled in with the data accompanying a signal sent by
436 .BR sigqueue (3).
437 .\" The fix also was put into 2.6.24.5
438 .SH EXAMPLES
439 The program below accepts the signals
440 .B SIGINT
441 and
442 .B SIGQUIT
443 via a signalfd file descriptor.
444 The program terminates after accepting a
445 .B SIGQUIT
446 signal.
447 The following shell session demonstrates the use of the program:
448 .PP
449 .in +4n
450 .EX
451 .RB "$" " ./signalfd_demo"
452 .BR "\(haC" " # Control\-C generates SIGINT"
453 Got SIGINT
454 .B \(haC
455 Got SIGINT
456 \fB\(ha\e\fP # Control\-\e generates SIGQUIT
457 Got SIGQUIT
458 $
459 .EE
460 .in
461 .SS Program source
462 \&
463 .\" SRC BEGIN (signalfd.c)
464 .EX
465 #include <err.h>
466 #include <signal.h>
467 #include <stdio.h>
468 #include <stdlib.h>
469 #include <sys/signalfd.h>
470 #include <unistd.h>
471
472 int
473 main(void)
474 {
475 int sfd;
476 ssize_t s;
477 sigset_t mask;
478 struct signalfd_siginfo fdsi;
479
480 sigemptyset(&mask);
481 sigaddset(&mask, SIGINT);
482 sigaddset(&mask, SIGQUIT);
483
484 /* Block signals so that they aren\(aqt handled
485 according to their default dispositions. */
486
487 if (sigprocmask(SIG_BLOCK, &mask, NULL) == \-1)
488 err(EXIT_FAILURE, "sigprocmask");
489
490 sfd = signalfd(\-1, &mask, 0);
491 if (sfd == \-1)
492 err(EXIT_FAILURE, "signalfd");
493
494 for (;;) {
495 s = read(sfd, &fdsi, sizeof(fdsi));
496 if (s != sizeof(fdsi))
497 err(EXIT_FAILURE, "read");
498
499 if (fdsi.ssi_signo == SIGINT) {
500 printf("Got SIGINT\en");
501 } else if (fdsi.ssi_signo == SIGQUIT) {
502 printf("Got SIGQUIT\en");
503 exit(EXIT_SUCCESS);
504 } else {
505 printf("Read unexpected signal\en");
506 }
507 }
508 }
509 .EE
510 .\" SRC END
511 .SH SEE ALSO
512 .BR eventfd (2),
513 .BR poll (2),
514 .BR read (2),
515 .BR select (2),
516 .BR sigaction (2),
517 .BR sigprocmask (2),
518 .BR sigwaitinfo (2),
519 .BR timerfd_create (2),
520 .BR sigsetops (3),
521 .BR sigwait (3),
522 .BR epoll (7),
523 .BR signal (7)