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


6       signalfd - create a file descriptor for accepting signals


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


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).
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.
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.
34       Starting with Linux 2.6.27, the following values may be bitwise ORed in
35       flags to change the behavior of signalfd():
37       SFD_NONBLOCK  Set the O_NONBLOCK file status flag on the open file  de‐
38                     scription  (see  open(2)) referred to by the new file de‐
39                     scriptor.  Using this flag saves extra calls to  fcntl(2)
40                     to achieve the same result.
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.
46       In  Linux  up to version 2.6.26, the flags argument is unused, and must
47       be specified as zero.
49       signalfd() returns a file descriptor that supports the following opera‐
50       tions:
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  re‐
55              turn  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.
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)).
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.
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.
77              The signalfd file descriptor also supports  the  other  file-de‐
78              scriptor multiplexing APIs: pselect(2), ppoll(2), and epoll(7).
80       close(2)
81              When  the  file  descriptor  is  no longer required it should be
82              closed.  When all file descriptors associated with the same sig‐
83              nalfd  object  have  been  closed,  the resources for object are
84              freed by the kernel.
86   The signalfd_siginfo structure
87       The format of the signalfd_siginfo structure(s)  returned  by  read(2)s
88       from a signalfd file descriptor is as follows:
90           struct signalfd_siginfo {
91               uint32_t ssi_signo;    /* Signal number */
92               int32_t  ssi_errno;    /* Error number (unused) */
93               int32_t  ssi_code;     /* Signal code */
94               uint32_t ssi_pid;      /* PID of sender */
95               uint32_t ssi_uid;      /* Real UID of sender */
96               int32_t  ssi_fd;       /* File descriptor (SIGIO) */
97               uint32_t ssi_tid;      /* Kernel timer ID (POSIX timers)
98               uint32_t ssi_band;     /* Band event (SIGIO) */
99               uint32_t ssi_overrun;  /* POSIX timer overrun count */
100               uint32_t ssi_trapno;   /* Trap number that caused signal */
101               int32_t  ssi_status;   /* Exit status or signal (SIGCHLD) */
102               int32_t  ssi_int;      /* Integer sent by sigqueue(3) */
103               uint64_t ssi_ptr;      /* Pointer sent by sigqueue(3) */
104               uint64_t ssi_utime;    /* User CPU time consumed (SIGCHLD) */
105               uint64_t ssi_stime;    /* System CPU time consumed
106                                         (SIGCHLD) */
107               uint64_t ssi_addr;     /* Address that generated signal
108                                         (for hardware-generated signals) */
109               uint16_t ssi_addr_lsb; /* Least significant bit of address
110                                         (SIGBUS; since Linux 2.6.37) */
111               uint8_t  pad[X];       /* Pad size to 128 bytes (allow for
112                                         additional fields in the future) */
113           };
115       Each  of  the  fields  in  this structure is analogous to the similarly
116       named field in the siginfo_t structure.  The siginfo_t structure is de‐
117       scribed  in sigaction(2).  Not all fields in the returned signalfd_sig‐
118       info structure will be valid for a specific signal; the  set  of  valid
119       fields can be determined from the value returned in the ssi_code field.
120       This field is the analog of the siginfo_t  si_code  field;  see  sigac‐
121       tion(2) for details.
123   fork(2) semantics
124       After  a  fork(2),  the  child inherits a copy of the signalfd file de‐
125       scriptor.  A read(2) from the file descriptor in the child will  return
126       information about signals queued to the child.
128   Semantics of file descriptor passing
129       As with other file descriptors, signalfd file descriptors can be passed
130       to another process via a UNIX domain socket (see unix(7)).  In the  re‐
131       ceiving  process,  a read(2) from the received file descriptor will re‐
132       turn information about signals queued to that process.
134   execve(2) semantics
135       Just like any other file descriptor, a signalfd file descriptor remains
136       open  across  an execve(2), unless it has been marked for close-on-exec
137       (see fcntl(2)).  Any signals that were available for reading before the
138       execve(2) remain available to the newly loaded program.  (This is anal‐
139       ogous to traditional signal semantics, where a blocked signal  that  is
140       pending remains pending across an execve(2).)
142   Thread semantics
143       The  semantics  of signalfd file descriptors in a multithreaded program
144       mirror the standard semantics for signals.   In  other  words,  when  a
145       thread  reads from a signalfd file descriptor, it will read the signals
146       that are directed to the thread itself and the  signals  that  are  di‐
147       rected  to the process (i.e., the entire thread group).  (A thread will
148       not be able to read signals that are directed to other threads  in  the
149       process.)
151   epoll(7) semantics
152       If  a  process adds (via epoll_ctl(2)) a signalfd file descriptor to an
153       epoll(7) instance, then epoll_wait(2) returns events only  for  signals
154       sent  to that process.  In particular, if the process then uses fork(2)
155       to create a child process, then the child will be able to read(2)  sig‐
156       nals  that  are  sent  to  it  using  the signalfd file descriptor, but
157       epoll_wait(2) will not indicate that the signalfd  file  descriptor  is
158       ready.   In  this  scenario,  a  possible  workaround is that after the
159       fork(2), the child process can close the signalfd file descriptor  that
160       it  inherited  from the parent process and then create another signalfd
161       file descriptor and add it to the epoll instance.   Alternatively,  the
162       parent  and  the  child  could delay creating their (separate) signalfd
163       file descriptors and adding them to the epoll instance until after  the
164       call to fork(2).


167       On  success, signalfd() returns a signalfd file descriptor; this is ei‐
168       ther a new file descriptor (if fd was -1), or fd if fd was a valid sig‐
169       nalfd  file  descriptor.   On error, -1 is returned and errno is set to
170       indicate the error.


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


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


197       signalfd() and signalfd4() are Linux-specific.


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


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


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


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


317       This  page  is  part of release 5.13 of the Linux man-pages project.  A
318       description of the project, information about reporting bugs,  and  the
319       latest     version     of     this    page,    can    be    found    at
320       https://www.kernel.org/doc/man-pages/.
324Linux                             2021-03-22                       SIGNALFD(2)