]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/signalfd.2
eventfd.2, mprotect.2, signalfd.2: ffix
[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 .\" 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.
8 .\"
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.
13 .\"
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,
17 .\" MA 02111-1307 USA
18 .\"
19 .TH SIGNALFD 2 2008-10-20 Linux "Linux Programmer's Manual"
20 .SH NAME
21 signalfd \- create a file descriptor for accepting signals
22 .SH SYNOPSIS
23 .B #include <sys/signalfd.h>
24 .sp
25 .BI "int signalfd(int " fd ", const sigset_t *" mask ", int " flags );
26 .SH DESCRIPTION
27 .BR signalfd ()
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
31 .BR sigwaitinfo (2),
32 and has the advantage that the file descriptor may be monitored by
33 .BR select (2),
34 .BR poll (2),
35 and
36 .BR epoll (7).
37
38 The
39 .I mask
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
44 .BR sigsetops (3).
45 Normally, the set of signals to be received via the
46 file descriptor should be blocked using
47 .BR sigprocmask (2),
48 to prevent the signals being handled according to their default
49 dispositions.
50 It is not possible to receive
51 .B SIGKILL
52 or
53 .B SIGSTOP
54 signals via a signalfd file descriptor;
55 these signals are silently ignored if specified in
56 .IR mask .
57
58 If the
59 .I fd
60 argument is \-1,
61 then the call creates a new file descriptor and associates the
62 signal set specified in
63 .I mask
64 with that descriptor.
65 If
66 .I fd
67 is not \-1,
68 then it must specify a valid existing signalfd file descriptor, and
69 .I mask
70 is used to replace the signal set associated with that descriptor.
71
72 Starting with Linux 2.6.27, the following values may be bitwise ORed in
73 .IR flags
74 to change the behaviour of
75 .BR signalfd ():
76 .TP 14
77 .B SFD_NONBLOCK
78 Set the
79 .BR O_NONBLOCK
80 file status flag on the new open file description.
81 Using this flag saves extra calls to
82 .BR fcntl (2)
83 to achieve the same result.
84 .TP
85 .B SFD_CLOEXEC
86 Set the close-on-exec
87 .RB ( FD_CLOEXEC )
88 flag on the new file descriptor.
89 See the description of the
90 .B O_CLOEXEC
91 flag in
92 .BR open (2)
93 for reasons why this may be useful.
94 .PP
95 In Linux up to version 2.6.26, the
96 .I flags
97 argument is unused, and must be specified as zero.
98
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 non-blocking.
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(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) */
195 };
196
197 .fi
198 .in
199 Each of the fields in this structure
200 is analogous to the similarly named field in the
201 .I siginfo_t
202 structure.
203 The
204 .I siginfo_t
205 structure is described in
206 .BR sigaction (2).
207 Not all fields in the returned
208 .I signalfd_siginfo
209 structure will be valid for a specific signal;
210 the set of valid fields can be determined from the value returned in the
211 .I ssi_code
212 field.
213 This field is the analog of the
214 .I siginfo_t
215 .I si_code
216 field; see
217 .BR sigaction (2)
218 for details.
219 .SS fork(2) semantics
220 After a
221 .BR fork (2),
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,
225 and
226 .BR read (2)s
227 in the child will return information about signals generated
228 for the parent
229 (the process that created the object using
230 .BR signalfd ()).
231 .SS execve(2) semantics
232 Just like any other file descriptor,
233 a signalfd file descriptor remains open across an
234 .BR execve (2),
235 unless it has been marked for close-on-exec (see
236 .BR fcntl (2)).
237 Any signals that were available for reading before the
238 .BR execve (2)
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
242 .BR execve (2).)
243 .SS Thread semantics
244 The semantics of signalfd file descriptors in a multithreaded program
245 mirror the standard semantics for signals.
246 In other words,
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.)
253 .SH "RETURN VALUE"
254 On success,
255 .BR signalfd ()
256 returns a signalfd file descriptor;
257 this is either a new file descriptor (if
258 .I fd
259 was \-1), or
260 .I fd
261 if
262 .I fd
263 was a valid signalfd file descriptor.
264 On error, \-1 is returned and
265 .I errno
266 is set to indicate the error.
267 .SH ERRORS
268 .TP
269 .B EBADF
270 The
271 .I fd
272 file descriptor is not a valid file descriptor.
273 .TP
274 .B EINVAL
275 .I fd
276 is not a valid signalfd file descriptor.
277 .\" or, the
278 .\" .I sizemask
279 .\" argument is not equal to
280 .\" .IR sizeof(sigset_t) ;
281 .TP
282 .B EINVAL
283 .I flags
284 is invalid;
285 or, in Linux 2.6.26 or earlier,
286 .I flags
287 is non-zero.
288 .TP
289 .B EMFILE
290 The per-process limit of open file descriptors has been reached.
291 .TP
292 .B ENFILE
293 The system-wide limit on the total number of open files has been
294 reached.
295 .TP
296 .B ENODEV
297 Could not mount (internal) anonymous inode device.
298 .TP
299 .B ENOMEM
300 There was insufficient memory to create a new signalfd file descriptor.
301 .SH VERSIONS
302 .BR signalfd ()
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
306 The
307 .BR signalfd4 ()
308 system call (see NOTES) is available on Linux since kernel 2.6.27.
309 .SH CONFORMING TO
310 .BR signalfd ()
311 and
312 .BR signalfd4 ()
313 are Linux-specific.
314 .SH NOTES
315 The underlying Linux system call requires an additional argument,
316 .IR "size_t sizemask" ,
317 which specifies the size of the
318 .I mask
319 argument.
320 The glibc
321 .BR signalfd ()
322 wrapper function does not include this argument,
323 since it provides the required value for the underlying system call.
324
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
329 .BR select (2),
330 .BR poll (2),
331 or
332 .BR epoll (7):
333 the arrival of different signals will make different descriptors ready.)
334 If a signal appears in the
335 .I mask
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:
340 .BR signalfd ()
341 and the more recent
342 .BR signalfd4 ().
343 The former system call does not implement a
344 .I flags
345 argument.
346 The latter system call implements the
347 .I flags
348 values described above.
349 Starting with glibc 2.9, the
350 .BR signalfd ()
351 wrapper function will use
352 .BR signalfd4 ()
353 where it is available.
354 .SH BUGS
355 In kernels before 2.6.25, the
356 .I ssi_ptr
357 and
358 .I ssi_int
359 fields are not filled in with the data accompanying a signal sent by
360 .BR sigqueue (2).
361 .\" The fix also was put into 2.6.24.5
362 .SH EXAMPLE
363 The program below accepts the signals
364 .B SIGINT
365 and
366 .B SIGQUIT
367 via a signalfd file descriptor.
368 The program terminates after accepting a
369 .B SIGQUIT
370 signal.
371 The following shell session demonstrates the use of the program:
372 .in +4n
373 .nf
374
375 .RB "$" " ./signalfd_demo"
376 .BR "^C" " # Control\-C generates SIGINT"
377 Got SIGINT
378 .B ^C
379 Got SIGINT
380 \fB^\\\fP # Control\-\\ generates SIGQUIT
381 Got SIGQUIT
382 $
383 .fi
384 .in
385 .SS Program source
386 .R " "
387 .nf
388 #include <sys/signalfd.h>
389 #include <signal.h>
390 #include <unistd.h>
391 #include <stdlib.h>
392 #include <stdio.h>
393
394 #define handle_error(msg) \\
395 do { perror(msg); exit(EXIT_FAILURE); } while (0)
396
397 int
398 main(int argc, char *argv[])
399 {
400 sigset_t mask;
401 int sfd;
402 struct signalfd_siginfo fdsi;
403 ssize_t s;
404
405 sigemptyset(&mask);
406 sigaddset(&mask, SIGINT);
407 sigaddset(&mask, SIGQUIT);
408
409 /* Block signals so that they aren\(aqt handled
410 according to their default dispositions */
411
412 if (sigprocmask(SIG_BLOCK, &mask, NULL) == \-1)
413 handle_error("sigprocmask");
414
415 sfd = signalfd(\-1, &mask, 0);
416 if (sfd == \-1)
417 handle_error("signalfd");
418
419 for (;;) {
420 s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
421 if (s != sizeof(struct signalfd_siginfo))
422 handle_error("read");
423
424 if (fdsi.ssi_signo == SIGINT) {
425 printf("Got SIGINT\\n");
426 } else if (fdsi.ssi_signo == SIGQUIT) {
427 printf("Got SIGQUIT\\n");
428 exit(EXIT_SUCCESS);
429 } else {
430 printf("Read unexpected signal\\n");
431 }
432 }
433 }
434 .fi
435 .SH "SEE ALSO"
436 .BR eventfd (2),
437 .BR poll (2),
438 .BR read (2),
439 .BR select (2),
440 .BR sigaction (2),
441 .BR sigprocmask (2),
442 .BR sigwaitinfo (2),
443 .BR timerfd_create (2),
444 .BR sigsetops (3),
445 .BR sigwait (3),
446 .BR epoll (7),
447 .BR signal (7)