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  descriptor.
30       If  fd  is  not -1, then it must specify a valid existing signalfd file
31       descriptor, and mask is used to replace the signal set associated  with
32       that descriptor.
33
34       Starting with Linux 2.6.27, the following values may be bitwise ORed in
35       flags to change the behaviour of signalfd():
36
37       SFD_NONBLOCK  Set the O_NONBLOCK file status flag on the new open  file
38                     description.   Using  this  flag  saves  extra  calls  to
39                     fcntl(2) to achieve the same result.
40
41       SFD_CLOEXEC   Set the close-on-exec (FD_CLOEXEC) flag on the  new  file
42                     descriptor.  See the description of the O_CLOEXEC flag in
43                     open(2) for reasons why this may be useful.
44
45       In Linux up to version 2.6.26, the flags argument is unused,  and  must
46       be specified as zero.
47
48       signalfd() returns a file descriptor that supports the following opera‐
49       tions:
50
51       read(2)
52              If one or more of the signals specified in mask is  pending  for
53              the  process,  then  the  buffer  supplied to read(2) is used to
54              return one or more signalfd_siginfo structures (see below)  that
55              describe  the  signals.   The read(2) returns information for as
56              many signals as are pending and will fit in the supplied buffer.
57              The  buffer  must  be  at  least sizeof(struct signalfd_siginfo)
58              bytes.  The return value of the read(2) is the total  number  of
59              bytes read.
60
61              As  a  consequence  of the read(2), the signals are consumed, so
62              that they are no longer pending for the process (i.e., will  not
63              be  caught by signal handlers, and cannot be accepted using sig‐
64              waitinfo(2)).
65
66              If none of the signals in mask is pending for the process,  then
67              the  read(2)  either  blocks until one of the signals in mask is
68              generated for the process, or fails with the error EAGAIN if the
69              file descriptor has been made non-blocking.
70
71       poll(2), select(2) (and similar)
72              The file descriptor is readable (the select(2) readfds argument;
73              the poll(2) POLLIN flag) if one or more of the signals  in  mask
74              is pending for the process.
75
76              The  signalfd  file  descriptor  also  supports  the other file-
77              descriptor  multiplexing   APIs:   pselect(2),   ppoll(2),   and
78              epoll(7).
79
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.
85
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:
89
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(2) */
103               uint64_t ssi_ptr;     /* Pointer sent by sigqueue(2) */
104               uint64_t ssi_utime;   /* User CPU time consumed (SIGCHLD) */
105               uint64_t ssi_stime;   /* System CPU time consumed (SIGCHLD) */
106               uint64_t ssi_addr;    /* Address that generated signal
107                                        (for hardware-generated signals) */
108               uint8_t  pad[X];      /* Pad size to 128 bytes (allow for
109                                         additional fields in the future) */
110           };
111
112       Each  of  the  fields  in  this structure is analogous to the similarly
113       named field in the siginfo_t structure.   The  siginfo_t  structure  is
114       described  in  sigaction(2).   Not  all  fields  in  the  returned sig‐
115       nalfd_siginfo structure will be valid for a specific signal; the set of
116       valid  fields can be determined from the value returned in the ssi_code
117       field.  This field is the analog of the siginfo_t  si_code  field;  see
118       sigaction(2) for details.
119
120   fork(2) semantics
121       After  a  fork(2),  the  child  inherits  a  copy  of the signalfd file
122       descriptor.  A read(2) from the  file  descriptor  in  the  child  will
123       return information about signals queued to the child.
124
125   execve(2) semantics
126       Just like any other file descriptor, a signalfd file descriptor remains
127       open across an execve(2), unless it has been marked  for  close-on-exec
128       (see fcntl(2)).  Any signals that were available for reading before the
129       execve(2) remain available to the newly loaded program.  (This is anal‐
130       ogous  to  traditional signal semantics, where a blocked signal that is
131       pending remains pending across an execve(2).)
132
133   Thread semantics
134       The semantics of signalfd file descriptors in a  multithreaded  program
135       mirror  the  standard  semantics  for  signals.  In other words, when a
136       thread reads from a signalfd file descriptor, it will read the  signals
137       that  are  directed  to  the  thread  itself  and  the signals that are
138       directed to the process (i.e., the entire  thread  group).   (A  thread
139       will  not be able to read signals that are directed to other threads in
140       the process.)
141

RETURN VALUE

143       On success, signalfd() returns a  signalfd  file  descriptor;  this  is
144       either  a  new  file descriptor (if fd was -1), or fd if fd was a valid
145       signalfd file descriptor.  On error, -1 is returned and errno is set to
146       indicate the error.
147

ERRORS

149       EBADF  The fd file descriptor is not a valid file descriptor.
150
151       EINVAL fd is not a valid signalfd file descriptor.
152
153       EINVAL flags  is invalid; or, in Linux 2.6.26 or earlier, flags is non-
154              zero.
155
156       EMFILE The per-process limit of open file descriptors has been reached.
157
158       ENFILE The system-wide limit on the total number of open files has been
159              reached.
160
161       ENODEV Could not mount (internal) anonymous inode device.
162
163       ENOMEM There  was  insufficient  memory  to  create a new signalfd file
164              descriptor.
165

VERSIONS

167       signalfd() is available on Linux since kernel 2.6.22.  Working  support
168       is  provided  in  glibc since version 2.8.  The signalfd4() system call
169       (see NOTES) is available on Linux since kernel 2.6.27.
170

CONFORMING TO

172       signalfd() and signalfd4() are Linux-specific.
173

NOTES

175       The underlying Linux  system  call  requires  an  additional  argument,
176       size_t  sizemask,  which  specifies the size of the mask argument.  The
177       glibc signalfd() wrapper function does not include this argument, since
178       it provides the required value for the underlying system call.
179
180       A process can create multiple signalfd file descriptors.  This makes it
181       possible to accept different signals  on  different  file  descriptors.
182       (This may be useful if monitoring the file descriptors using select(2),
183       poll(2), or epoll(7): the arrival of different signals will  make  dif‐
184       ferent  descriptors  ready.)   If  a signal appears in the mask of more
185       than one of the file descriptors, then occurrences of that  signal  can
186       be read (once) from any one of the descriptors.
187
188   Underlying Linux system calls
189       There  are  two  underlying Linux system calls: signalfd() and the more
190       recent signalfd4().  The former system call does not implement a  flags
191       argument.  The latter system call implements the flags values described
192       above.  Starting with glibc 2.9, the signalfd() wrapper  function  will
193       use signalfd4() where it is available.
194

BUGS

196       In kernels before 2.6.25, the ssi_ptr and ssi_int fields are not filled
197       in with the data accompanying a signal sent by sigqueue(2).
198

EXAMPLE

200       The program below accepts the signals SIGINT and SIGQUIT via a signalfd
201       file descriptor.  The program terminates after accepting a SIGQUIT sig‐
202       nal.  The following shell session demonstrates the use of the program:
203
204           $ ./signalfd_demo
205           ^C                   # Control-C generates SIGINT
206           Got SIGINT
207           ^C
208           Got SIGINT
209           ^\                    # Control-\ generates SIGQUIT
210           Got SIGQUIT
211           $
212
213   Program source
214
215       #include <sys/signalfd.h>
216       #include <signal.h>
217       #include <unistd.h>
218       #include <stdlib.h>
219       #include <stdio.h>
220
221       #define handle_error(msg) \
222           do { perror(msg); exit(EXIT_FAILURE); } while (0)
223
224       int
225       main(int argc, char *argv[])
226       {
227           sigset_t mask;
228           int sfd;
229           struct signalfd_siginfo fdsi;
230           ssize_t s;
231
232           sigemptyset(&mask);
233           sigaddset(&mask, SIGINT);
234           sigaddset(&mask, SIGQUIT);
235
236           /* Block signals so that they aren't handled
237              according to their default dispositions */
238
239           if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
240               handle_error("sigprocmask");
241
242           sfd = signalfd(-1, &mask, 0);
243           if (sfd == -1)
244               handle_error("signalfd");
245
246           for (;;) {
247               s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
248               if (s != sizeof(struct signalfd_siginfo))
249                   handle_error("read");
250
251               if (fdsi.ssi_signo == SIGINT) {
252                   printf("Got SIGINT\n");
253               } else if (fdsi.ssi_signo == SIGQUIT) {
254                   printf("Got SIGQUIT\n");
255                   exit(EXIT_SUCCESS);
256               } else {
257                   printf("Read unexpected signal\n");
258               }
259           }
260       }
261

SEE ALSO

263       eventfd(2), poll(2), read(2), select(2), sigaction(2),  sigprocmask(2),
264       sigwaitinfo(2),  timerfd_create(2), sigsetops(3), sigwait(3), epoll(7),
265       signal(7)
266

COLOPHON

268       This page is part of release 3.22 of the Linux  man-pages  project.   A
269       description  of  the project, and information about reporting bugs, can
270       be found at http://www.kernel.org/doc/man-pages/.
271
272
273
274Linux                             2009-01-13                       SIGNALFD(2)
Impressum