1SIGNALFD(2)                Linux Programmer's Manual               SIGNALFD(2)
2
3
4

NAME

6       signalfd - create a file descriptor for accepting signals
7

SYNOPSIS

9       #include <sys/signalfd.h>
10
11       int signalfd(int fd, const sigset_t *mask, int flags);
12

DESCRIPTION

14       signalfd() creates a file descriptor that can be used to accept signals
15       targeted at the caller.  This provides an alternative to the use  of  a
16       signal  handler  or sigwaitinfo(2), and has the advantage that the file
17       descriptor may be monitored by select(2), poll(2), and epoll(7).
18
19       The mask argument specifies the set of signals that the  caller  wishes
20       to accept via the file descriptor.  This argument is a signal set whose
21       contents can be initialized using the macros described in sigsetops(3).
22       Normally,  the  set  of  signals to be received via the file descriptor
23       should be blocked using sigprocmask(2), to prevent  the  signals  being
24       handled according to their default dispositions.  It is not possible to
25       receive SIGKILL or SIGSTOP signals  via  a  signalfd  file  descriptor;
26       these signals are silently ignored if specified in mask.
27
28       If  the  fd argument is -1, then the call creates a new file descriptor
29       and associates the signal set specified in mask with that file descrip‐
30       tor.   If  fd is not -1, then it must specify a valid existing signalfd
31       file descriptor, and mask is used to replace the signal set  associated
32       with that file descriptor.
33
34       Starting with Linux 2.6.27, the following values may be bitwise ORed in
35       flags to change the behavior of signalfd():
36
37       SFD_NONBLOCK  Set the O_NONBLOCK file status  flag  on  the  open  file
38                     description  (see  open(2))  referred  to by the new file
39                     descriptor.   Using  this  flag  saves  extra  calls   to
40                     fcntl(2) to achieve the same result.
41
42       SFD_CLOEXEC   Set  the  close-on-exec (FD_CLOEXEC) flag on the new file
43                     descriptor.  See the description of the O_CLOEXEC flag in
44                     open(2) for reasons why this may be useful.
45
46       In  Linux  up to version 2.6.26, the flags argument is unused, and must
47       be specified as zero.
48
49       signalfd() returns a file descriptor that supports the following opera‐
50       tions:
51
52       read(2)
53              If  one  or more of the signals specified in mask is pending for
54              the process, then the buffer supplied  to  read(2)  is  used  to
55              return  one or more signalfd_siginfo structures (see below) that
56              describe the signals.  The read(2) returns  information  for  as
57              many signals as are pending and will fit in the supplied buffer.
58              The buffer must  be  at  least  sizeof(struct  signalfd_siginfo)
59              bytes.   The  return value of the read(2) is the total number of
60              bytes read.
61
62              As a consequence of the read(2), the signals  are  consumed,  so
63              that  they are no longer pending for the process (i.e., will not
64              be caught by signal handlers, and cannot be accepted using  sig‐
65              waitinfo(2)).
66
67              If  none of the signals in mask is pending for the process, then
68              the read(2) either blocks until one of the signals  in  mask  is
69              generated for the process, or fails with the error EAGAIN if the
70              file descriptor has been made nonblocking.
71
72       poll(2), select(2) (and similar)
73              The file descriptor is readable (the select(2) readfds argument;
74              the  poll(2)  POLLIN flag) if one or more of the signals in mask
75              is pending for the process.
76
77              The signalfd file  descriptor  also  supports  the  other  file-
78              descriptor   multiplexing   APIs:   pselect(2),   ppoll(2),  and
79              epoll(7).
80
81       close(2)
82              When the file descriptor is no  longer  required  it  should  be
83              closed.  When all file descriptors associated with the same sig‐
84              nalfd object have been closed,  the  resources  for  object  are
85              freed by the kernel.
86
87   The signalfd_siginfo structure
88       The  format  of  the signalfd_siginfo structure(s) returned by read(2)s
89       from a signalfd file descriptor is as follows:
90
91           struct signalfd_siginfo {
92               uint32_t ssi_signo;    /* Signal number */
93               int32_t  ssi_errno;    /* Error number (unused) */
94               int32_t  ssi_code;     /* Signal code */
95               uint32_t ssi_pid;      /* PID of sender */
96               uint32_t ssi_uid;      /* Real UID of sender */
97               int32_t  ssi_fd;       /* File descriptor (SIGIO) */
98               uint32_t ssi_tid;      /* Kernel timer ID (POSIX timers)
99               uint32_t ssi_band;     /* Band event (SIGIO) */
100               uint32_t ssi_overrun;  /* POSIX timer overrun count */
101               uint32_t ssi_trapno;   /* Trap number that caused signal */
102               int32_t  ssi_status;   /* Exit status or signal (SIGCHLD) */
103               int32_t  ssi_int;      /* Integer sent by sigqueue(3) */
104               uint64_t ssi_ptr;      /* Pointer sent by sigqueue(3) */
105               uint64_t ssi_utime;    /* User CPU time consumed (SIGCHLD) */
106               uint64_t ssi_stime;    /* System CPU time consumed
107                                         (SIGCHLD) */
108               uint64_t ssi_addr;     /* Address that generated signal
109                                         (for hardware-generated signals) */
110               uint16_t ssi_addr_lsb; /* Least significant bit of address
111                                         (SIGBUS; since Linux 2.6.37)
112               uint8_t  pad[X];       /* Pad size to 128 bytes (allow for
113                                         additional fields in the future) */
114           };
115
116       Each of the fields in this structure  is  analogous  to  the  similarly
117       named  field  in  the  siginfo_t structure.  The siginfo_t structure is
118       described in  sigaction(2).   Not  all  fields  in  the  returned  sig‐
119       nalfd_siginfo structure will be valid for a specific signal; the set of
120       valid fields can be determined from the value returned in the  ssi_code
121       field.   This  field  is the analog of the siginfo_t si_code field; see
122       sigaction(2) for details.
123
124   fork(2) semantics
125       After a fork(2), the  child  inherits  a  copy  of  the  signalfd  file
126       descriptor.   A  read(2)  from  the  file  descriptor in the child will
127       return information about signals queued to the child.
128
129   Semantics of file descriptor passing
130       As with other file descriptors, signalfd file descriptors can be passed
131       to  another  process  via  a  UNIX domain socket (see unix(7)).  In the
132       receiving process, a read(2) from the  received  file  descriptor  will
133       return information about signals queued to that process.
134
135   execve(2) semantics
136       Just like any other file descriptor, a signalfd file descriptor remains
137       open across an execve(2), unless it has been marked  for  close-on-exec
138       (see fcntl(2)).  Any signals that were available for reading before the
139       execve(2) remain available to the newly loaded program.  (This is anal‐
140       ogous  to  traditional signal semantics, where a blocked signal that is
141       pending remains pending across an execve(2).)
142
143   Thread semantics
144       The semantics of signalfd file descriptors in a  multithreaded  program
145       mirror  the  standard  semantics  for  signals.  In other words, when a
146       thread reads from a signalfd file descriptor, it will read the  signals
147       that  are  directed  to  the  thread  itself  and  the signals that are
148       directed to the process (i.e., the entire  thread  group).   (A  thread
149       will  not be able to read signals that are directed to other threads in
150       the process.)
151
152   epoll(7) semantics
153       If a process adds (via epoll_ctl(2)) a signalfd file descriptor  to  an
154       epoll(7)  instance,  then epoll_wait(2) returns events only for signals
155       sent to that process.  In particular, if the process then uses  fork(2)
156       to  create a child process, then the child will be able to read(2) sig‐
157       nals that are sent to  it  using  the  signalfd  file  descriptor,  but
158       epoll_wait(2)  will  not  indicate that the signalfd file descriptor is
159       ready.  In this scenario, a  possible  workaround  is  that  after  the
160       fork(2),  the child process can close the signalfd file descriptor that
161       it inherited from the parent process and then create  another  signalfd
162       file  descriptor  and add it to the epoll instance.  Alternatively, the
163       parent and the child could delay  creating  their  (separate)  signalfd
164       file  descriptors and adding them to the epoll instance until after the
165       call to fork(2).
166

RETURN VALUE

168       On success, signalfd() returns a  signalfd  file  descriptor;  this  is
169       either  a  new  file descriptor (if fd was -1), or fd if fd was a valid
170       signalfd file descriptor.  On error, -1 is returned and errno is set to
171       indicate the error.
172

ERRORS

174       EBADF  The fd file descriptor is not a valid file descriptor.
175
176       EINVAL fd is not a valid signalfd file descriptor.
177
178       EINVAL flags  is  invalid;  or,  in  Linux  2.6.26 or earlier, flags is
179              nonzero.
180
181       EMFILE The per-process limit on the number of open file descriptors has
182              been reached.
183
184       ENFILE The system-wide limit on the total number of open files has been
185              reached.
186
187       ENODEV Could not mount (internal) anonymous inode device.
188
189       ENOMEM There was insufficient memory to  create  a  new  signalfd  file
190              descriptor.
191

VERSIONS

193       signalfd()  is available on Linux since kernel 2.6.22.  Working support
194       is provided in glibc since version 2.8.  The  signalfd4()  system  call
195       (see NOTES) is available on Linux since kernel 2.6.27.
196

CONFORMING TO

198       signalfd() and signalfd4() are Linux-specific.
199

NOTES

201       A process can create multiple signalfd file descriptors.  This makes it
202       possible to accept different signals  on  different  file  descriptors.
203       (This may be useful if monitoring the file descriptors using select(2),
204       poll(2), or epoll(7): the arrival of different signals will  make  dif‐
205       ferent  file  descriptors  ready.)   If a signal appears in the mask of
206       more than one of the file descriptors, then occurrences of that  signal
207       can be read (once) from any one of the file descriptors.
208
209       Attempts to include SIGKILL and SIGSTOP in mask are silently ignored.
210
211       The  signal  mask  employed by a signalfd file descriptor can be viewed
212       via the entry for the corresponding file descriptor  in  the  process's
213       /proc/[pid]/fdinfo directory.  See proc(5) for further details.
214
215   Limitations
216       The  signalfd  mechanism can't be used to receive signals that are syn‐
217       chronously generated, such as the  SIGSEGV  signal  that  results  from
218       accessing  an  invalid memory address or the SIGFPE signal that results
219       from an arithmetic error.  Such signals can be caught only  via  signal
220       handler.
221
222       As described above, in normal usage one blocks the signals that will be
223       accepted via signalfd().  If spawning a  child  process  to  execute  a
224       helper program (that does not need the signalfd file descriptor), then,
225       after the call to fork(2), you will normally want to unblock those sig‐
226       nals  before  calling execve(2), so that the helper program can see any
227       signals that it expects to see.  Be aware, however, that this won't  be
228       possible  in  the case of a helper program spawned behind the scenes by
229       any library function that the program may call.   In  such  cases,  one
230       must  fall  back to using a traditional signal handler that writes to a
231       file descriptor monitored by select(2), poll(2), or epoll(7).
232
233   C library/kernel differences
234       The underlying Linux  system  call  requires  an  additional  argument,
235       size_t  sizemask,  which  specifies the size of the mask argument.  The
236       glibc signalfd() wrapper function does not include this argument, since
237       it provides the required value for the underlying system call.
238
239       There  are  two  underlying Linux system calls: signalfd() and the more
240       recent signalfd4().  The former system call does not implement a  flags
241       argument.  The latter system call implements the flags values described
242       above.  Starting with glibc 2.9, the signalfd() wrapper  function  will
243       use signalfd4() where it is available.
244

BUGS

246       In kernels before 2.6.25, the ssi_ptr and ssi_int fields are not filled
247       in with the data accompanying a signal sent by sigqueue(3).
248

EXAMPLES

250       The program below accepts the signals SIGINT and SIGQUIT via a signalfd
251       file descriptor.  The program terminates after accepting a SIGQUIT sig‐
252       nal.  The following shell session demonstrates the use of the program:
253
254           $ ./signalfd_demo
255           ^C                   # Control-C generates SIGINT
256           Got SIGINT
257           ^C
258           Got SIGINT
259           ^\                    # Control-\ generates SIGQUIT
260           Got SIGQUIT
261           $
262
263   Program source
264
265       #include <sys/signalfd.h>
266       #include <signal.h>
267       #include <unistd.h>
268       #include <stdlib.h>
269       #include <stdio.h>
270
271       #define handle_error(msg) \
272           do { perror(msg); exit(EXIT_FAILURE); } while (0)
273
274       int
275       main(int argc, char *argv[])
276       {
277           sigset_t mask;
278           int sfd;
279           struct signalfd_siginfo fdsi;
280           ssize_t s;
281
282           sigemptyset(&mask);
283           sigaddset(&mask, SIGINT);
284           sigaddset(&mask, SIGQUIT);
285
286           /* Block signals so that they aren't handled
287              according to their default dispositions */
288
289           if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
290               handle_error("sigprocmask");
291
292           sfd = signalfd(-1, &mask, 0);
293           if (sfd == -1)
294               handle_error("signalfd");
295
296           for (;;) {
297               s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
298               if (s != sizeof(struct signalfd_siginfo))
299                   handle_error("read");
300
301               if (fdsi.ssi_signo == SIGINT) {
302                   printf("Got SIGINT\n");
303               } else if (fdsi.ssi_signo == SIGQUIT) {
304                   printf("Got SIGQUIT\n");
305                   exit(EXIT_SUCCESS);
306               } else {
307                   printf("Read unexpected signal\n");
308               }
309           }
310       }
311

SEE ALSO

313       eventfd(2), poll(2), read(2), select(2), sigaction(2),  sigprocmask(2),
314       sigwaitinfo(2),  timerfd_create(2), sigsetops(3), sigwait(3), epoll(7),
315       signal(7)
316

COLOPHON

318       This page is part of release 5.07 of the Linux  man-pages  project.   A
319       description  of  the project, information about reporting bugs, and the
320       latest    version    of    this    page,    can     be     found     at
321       https://www.kernel.org/doc/man-pages/.
322
323
324
325Linux                             2020-06-09                       SIGNALFD(2)
Impressum