Debian Codegraph @

upstream/debian-trixie/man2/signalfd.2

raw
1 .\" Copyright (C) 2008 Michael Kerrisk <[email protected]>
2 .\" starting from a version by Davide Libenzi <[email protected]>
3 .\"
4 .\" SPDX-License-Identifier: GPL-2.0-or-later
5 .\"
6 .TH signalfd 2 2024-06-15 "Linux man-pages 6.9.1"
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 .P
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 .P
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 .P
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 .P
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 .P
89 Up to Linux 2.6.26, the
90 .I flags
91 argument is unused, and must be specified as zero.
92 .P
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)
135 .TQ
136 .BR select (2)
137 .TQ
138 (and similar)
139 The file descriptor is readable
140 (the
141 .BR select (2)
142 .I readfds
143 argument; the
144 .BR poll (2)
145 .B POLLIN
146 flag)
147 if one or more of the signals in
148 .I mask
149 is pending for the process.
150 .IP
151 The signalfd file descriptor also supports the other file-descriptor
152 multiplexing APIs:
153 .BR pselect (2),
154 .BR ppoll (2),
155 and
156 .BR epoll (7).
157 .TP
158 .BR close (2)
159 When the file descriptor is no longer required it should be closed.
160 When all file descriptors associated with the same signalfd object
161 have been closed, the resources for object are freed by the kernel.
162 .SS The signalfd_siginfo structure
163 The format of the
164 .I signalfd_siginfo
165 structure(s) returned by
166 .BR read (2)s
167 from a signalfd file descriptor is as follows:
168 .P
169 .in +4n
170 .EX
171 struct signalfd_siginfo {
172 uint32_t ssi_signo; /* Signal number */
173 int32_t ssi_errno; /* Error number (unused) */
174 int32_t ssi_code; /* Signal code */
175 uint32_t ssi_pid; /* PID of sender */
176 uint32_t ssi_uid; /* Real UID of sender */
177 int32_t ssi_fd; /* File descriptor (SIGIO) */
178 uint32_t ssi_tid; /* Kernel timer ID (POSIX timers)
179 uint32_t ssi_band; /* Band event (SIGIO) */
180 uint32_t ssi_overrun; /* POSIX timer overrun count */
181 uint32_t ssi_trapno; /* Trap number that caused signal */
182 .\" ssi_trapno is unused on most arches
183 int32_t ssi_status; /* Exit status or signal (SIGCHLD) */
184 int32_t ssi_int; /* Integer sent by sigqueue(3) */
185 uint64_t ssi_ptr; /* Pointer sent by sigqueue(3) */
186 uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */
187 uint64_t ssi_stime; /* System CPU time consumed
188 (SIGCHLD) */
189 uint64_t ssi_addr; /* Address that generated signal
190 (for hardware\-generated signals) */
191 uint16_t ssi_addr_lsb; /* Least significant bit of address
192 (SIGBUS; since Linux 2.6.37) */
193 .\" ssi_addr_lsb: commit b8aeec34175fc8fe8b0d40efea4846dfc1ba663e
194 uint8_t pad[\fIX\fP]; /* Pad size to 128 bytes (allow for
195 additional fields in the future) */
196 };
197 .EE
198 .in
199 .P
200 Each of the fields in this structure
201 is analogous to the similarly named field in the
202 .I siginfo_t
203 structure.
204 The
205 .I siginfo_t
206 structure is described in
207 .BR sigaction (2).
208 Not all fields in the returned
209 .I signalfd_siginfo
210 structure will be valid for a specific signal;
211 the set of valid fields can be determined from the value returned in the
212 .I ssi_code
213 field.
214 This field is the analog of the
215 .I siginfo_t
216 .I si_code
217 field; see
218 .BR sigaction (2)
219 for details.
220 .SS fork(2) semantics
221 After a
222 .BR fork (2),
223 the child inherits a copy of the signalfd file descriptor.
224 A
225 .BR read (2)
226 from the file descriptor in the child will return information
227 about signals queued to the child.
228 .SS Semantics of file descriptor passing
229 As with other file descriptors,
230 signalfd file descriptors can be passed to another process
231 via a UNIX domain socket (see
232 .BR unix (7)).
233 In the receiving process, a
234 .BR read (2)
235 from the received file descriptor will return information
236 about signals queued to that process.
237 .SS execve(2) semantics
238 Just like any other file descriptor,
239 a signalfd file descriptor remains open across an
240 .BR execve (2),
241 unless it has been marked for close-on-exec (see
242 .BR fcntl (2)).
243 Any signals that were available for reading before the
244 .BR execve (2)
245 remain available to the newly loaded program.
246 (This is analogous to traditional signal semantics,
247 where a blocked signal that is pending remains pending across an
248 .BR execve (2).)
249 .SS Thread semantics
250 The semantics of signalfd file descriptors in a multithreaded program
251 mirror the standard semantics for signals.
252 In other words,
253 when a thread reads from a signalfd file descriptor,
254 it will read the signals that are directed to the thread
255 itself and the signals that are directed to the process
256 (i.e., the entire thread group).
257 (A thread will not be able to read signals that are directed
258 to other threads in the process.)
259 .\"
260 .SS epoll(7) semantics
261 If a process adds (via
262 .BR epoll_ctl (2))
263 a signalfd file descriptor to an
264 .BR epoll (7)
265 instance, then
266 .BR epoll_wait (2)
267 returns events only for signals sent to that process.
268 In particular, if the process then uses
269 .BR fork (2)
270 to create a child process, then the child will be able to
271 .BR read (2)
272 signals that are sent to it using the signalfd file descriptor, but
273 .BR epoll_wait (2)
274 will
275 .B not
276 indicate that the signalfd file descriptor is ready.
277 In this scenario, a possible workaround is that after the
278 .BR fork (2),
279 the child process can close the signalfd file descriptor that it inherited
280 from the parent process and then create another signalfd file descriptor
281 and add it to the epoll instance.
282 Alternatively, the parent and the child could delay creating their
283 (separate) signalfd file descriptors and adding them to the
284 epoll instance until after the call to
285 .BR fork (2).
286 .SH RETURN VALUE
287 On success,
288 .BR signalfd ()
289 returns a signalfd file descriptor;
290 this is either a new file descriptor (if
291 .I fd
292 was \-1), or
293 .I fd
294 if
295 .I fd
296 was a valid signalfd file descriptor.
297 On error, \-1 is returned and
298 .I errno
299 is set to indicate the error.
300 .SH ERRORS
301 .TP
302 .B EBADF
303 The
304 .I fd
305 file descriptor is not a valid file descriptor.
306 .TP
307 .B EINVAL
308 .I fd
309 is not a valid signalfd file descriptor.
310 .\" or, the
311 .\" .I sizemask
312 .\" argument is not equal to
313 .\" .IR sizeof(sigset_t) ;
314 .TP
315 .B EINVAL
316 .I flags
317 is invalid;
318 or, in Linux 2.6.26 or earlier,
319 .I flags
320 is nonzero.
321 .TP
322 .B EMFILE
323 The per-process limit on the number of open file descriptors has been reached.
324 .TP
325 .B ENFILE
326 The system-wide limit on the total number of open files has been
327 reached.
328 .TP
329 .B ENODEV
330 Could not mount (internal) anonymous inode device.
331 .TP
332 .B ENOMEM
333 There was insufficient memory to create a new signalfd file descriptor.
334 .SH VERSIONS
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 .P
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 STANDARDS
362 Linux.
363 .SH HISTORY
364 .TP
365 .BR signalfd ()
366 Linux 2.6.22,
367 glibc 2.8.
368 .\" signalfd() is in glibc 2.7, but reportedly does not build
369 .TP
370 .BR signalfd4 ()
371 Linux 2.6.27.
372 .SH NOTES
373 A process can create multiple signalfd file descriptors.
374 This makes it possible to accept different signals
375 on different file descriptors.
376 (This may be useful if monitoring the file descriptors using
377 .BR select (2),
378 .BR poll (2),
379 or
380 .BR epoll (7):
381 the arrival of different signals will make different file descriptors ready.)
382 If a signal appears in the
383 .I mask
384 of more than one of the file descriptors, then occurrences
385 of that signal can be read (once) from any one of the file descriptors.
386 .P
387 Attempts to include
388 .B SIGKILL
389 and
390 .B SIGSTOP
391 in
392 .I mask
393 are silently ignored.
394 .P
395 The signal mask employed by a signalfd file descriptor can be viewed
396 via the entry for the corresponding file descriptor in the process's
397 .IR /proc/ pid /fdinfo
398 directory.
399 See
400 .BR proc (5)
401 for further details.
402 .\"
403 .SS Limitations
404 The signalfd mechanism can't be used to receive signals that
405 are synchronously generated, such as the
406 .B SIGSEGV
407 signal that results from accessing an invalid memory address
408 or the
409 .B SIGFPE
410 signal that results from an arithmetic error.
411 Such signals can be caught only via signal handler.
412 .P
413 As described above,
414 in normal usage one blocks the signals that will be accepted via
415 .BR signalfd ().
416 If spawning a child process to execute a helper program
417 (that does not need the signalfd file descriptor),
418 then, after the call to
419 .BR fork (2),
420 you will normally want to unblock those signals before calling
421 .BR execve (2),
422 so that the helper program can see any signals that it expects to see.
423 Be aware, however,
424 that this won't be possible in the case of a helper program spawned
425 behind the scenes by any library function that the program may call.
426 In such cases, one must fall back to using a traditional signal
427 handler that writes to a file descriptor monitored by
428 .BR select (2),
429 .BR poll (2),
430 or
431 .BR epoll (7).
432 .SH BUGS
433 Before Linux 2.6.25, the
434 .I ssi_ptr
435 and
436 .I ssi_int
437 fields are not filled in with the data accompanying a signal sent by
438 .BR sigqueue (3).
439 .\" The fix also was put into Linux 2.6.24.5
440 .SH EXAMPLES
441 The program below accepts the signals
442 .B SIGINT
443 and
444 .B SIGQUIT
445 via a signalfd file descriptor.
446 The program terminates after accepting a
447 .B SIGQUIT
448 signal.
449 The following shell session demonstrates the use of the program:
450 .P
451 .in +4n
452 .EX
453 .RB "$" " ./signalfd_demo"
454 .BR "\[ha]C" " # Control\-C generates SIGINT"
455 Got SIGINT
456 .B \[ha]C
457 Got SIGINT
458 \fB\[ha]\[rs]\fP # Control\-\[rs] generates SIGQUIT
459 Got SIGQUIT
460 $
461 .EE
462 .in
463 .SS Program source
464 \&
465 .\" SRC BEGIN (signalfd.c)
466 .EX
467 #include <err.h>
468 #include <signal.h>
469 #include <stdio.h>
470 #include <stdlib.h>
471 #include <sys/signalfd.h>
472 #include <sys/types.h>
473 #include <unistd.h>
474 \&
475 int
476 main(void)
477 {
478 int sfd;
479 ssize_t s;
480 sigset_t mask;
481 struct signalfd_siginfo fdsi;
482 \&
483 sigemptyset(&mask);
484 sigaddset(&mask, SIGINT);
485 sigaddset(&mask, SIGQUIT);
486 \&
487 /* Block signals so that they aren\[aq]t handled
488 according to their default dispositions. */
489 \&
490 if (sigprocmask(SIG_BLOCK, &mask, NULL) == \-1)
491 err(EXIT_FAILURE, "sigprocmask");
492 \&
493 sfd = signalfd(\-1, &mask, 0);
494 if (sfd == \-1)
495 err(EXIT_FAILURE, "signalfd");
496 \&
497 for (;;) {
498 s = read(sfd, &fdsi, sizeof(fdsi));
499 if (s != sizeof(fdsi))
500 err(EXIT_FAILURE, "read");
501 \&
502 if (fdsi.ssi_signo == SIGINT) {
503 printf("Got SIGINT\[rs]n");
504 } else if (fdsi.ssi_signo == SIGQUIT) {
505 printf("Got SIGQUIT\[rs]n");
506 exit(EXIT_SUCCESS);
507 } else {
508 printf("Read unexpected signal\[rs]n");
509 }
510 }
511 }
512 .EE
513 .\" SRC END
514 .SH SEE ALSO
515 .BR eventfd (2),
516 .BR poll (2),
517 .BR read (2),
518 .BR select (2),
519 .BR sigaction (2),
520 .BR sigprocmask (2),
521 .BR sigwaitinfo (2),
522 .BR timerfd_create (2),
523 .BR sigsetops (3),
524 .BR sigwait (3),
525 .BR epoll (7),
526 .BR signal (7)