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