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

NAME

6       poll, ppoll - wait for some event on a file descriptor
7

SYNOPSIS

9       #include <poll.h>
10
11       int poll(struct pollfd *fds, nfds_t nfds, int timeout);
12
13       #define _GNU_SOURCE         /* See feature_test_macros(7) */
14       #include <signal.h>
15       #include <poll.h>
16
17       int ppoll(struct pollfd *fds, nfds_t nfds,
18               const struct timespec *tmo_p, const sigset_t *sigmask);
19

DESCRIPTION

21       poll()  performs a similar task to select(2): it waits for one of a set
22       of file descriptors to become ready to perform I/O.
23
24       The set of file descriptors to be monitored is  specified  in  the  fds
25       argument, which is an array of structures of the following form:
26
27           struct pollfd {
28               int   fd;         /* file descriptor */
29               short events;     /* requested events */
30               short revents;    /* returned events */
31           };
32
33       The caller should specify the number of items in the fds array in nfds.
34
35       The  field  fd  contains  a  file descriptor for an open file.  If this
36       field is negative, then the corresponding events field is  ignored  and
37       the revents field returns zero.  (This provides an easy way of ignoring
38       a file descriptor for a single poll() call: simply negate the fd field.
39       Note,  however,  that  this  technique  can't  be  used  to ignore file
40       descriptor 0.)
41
42       The field events is an input  parameter,  a  bit  mask  specifying  the
43       events  the  application  is  interested in for the file descriptor fd.
44       This field may be specified as zero, in which case the only events that
45       can  be  returned  in  revents  are POLLHUP, POLLERR, and POLLNVAL (see
46       below).
47
48       The field revents is an output parameter, filled by the kernel with the
49       events  that  actually  occurred.   The  bits  returned  in revents can
50       include any of those specified in events, or one of the values POLLERR,
51       POLLHUP,  or POLLNVAL.  (These three bits are meaningless in the events
52       field, and will be set in the revents field whenever the  corresponding
53       condition is true.)
54
55       If  none of the events requested (and no error) has occurred for any of
56       the file descriptors, then  poll()  blocks  until  one  of  the  events
57       occurs.
58
59       The  timeout  argument specifies the number of milliseconds that poll()
60       should block waiting for a file descriptor to become ready.   The  call
61       will block until either:
62
63       *  a file descriptor becomes ready;
64
65       *  the call is interrupted by a signal handler; or
66
67       *  the timeout expires.
68
69       Note  that  the timeout interval will be rounded up to the system clock
70       granularity, and kernel scheduling delays mean that the blocking inter‐
71       val  may  overrun  by  a  small amount.  Specifying a negative value in
72       timeout means an infinite timeout.  Specifying a timeout of zero causes
73       poll() to return immediately, even if no file descriptors are ready.
74
75       The  bits that may be set/returned in events and revents are defined in
76       <poll.h>:
77
78       POLLIN There is data to read.
79
80       POLLPRI
81              There is some exceptional  condition  on  the  file  descriptor.
82              Possibilities include:
83
84              *  There is out-of-band data on a TCP socket (see tcp(7)).
85
86              *  A  pseudoterminal  master  in  packet  mode  has seen a state
87                 change on the slave (see ioctl_tty(2)).
88
89              *  A cgroup.events file has been modified (see cgroups(7)).
90
91       POLLOUT
92              Writing is now possible, though a write larger that  the  avail‐
93              able  space  in a socket or pipe will still block (unless O_NON‐
94              BLOCK is set).
95
96       POLLRDHUP (since Linux 2.6.17)
97              Stream socket peer closed connection, or shut down writing  half
98              of  connection.   The  _GNU_SOURCE  feature  test  macro must be
99              defined (before including any header files) in order  to  obtain
100              this definition.
101
102       POLLERR
103              Error  condition  (only returned in revents; ignored in events).
104              This bit is also set for a  file  descriptor  referring  to  the
105              write end of a pipe when the read end has been closed.
106
107       POLLHUP
108              Hang  up  (only  returned  in revents; ignored in events).  Note
109              that when reading from a channel such as  a  pipe  or  a  stream
110              socket, this event merely indicates that the peer closed its end
111              of the channel.  Subsequent reads from the channel will return 0
112              (end of file) only after all outstanding data in the channel has
113              been consumed.
114
115       POLLNVAL
116              Invalid request: fd not open (only returned in revents;  ignored
117              in events).
118
119       When  compiling with _XOPEN_SOURCE defined, one also has the following,
120       which convey no further information beyond the bits listed above:
121
122       POLLRDNORM
123              Equivalent to POLLIN.
124
125       POLLRDBAND
126              Priority band data can be read (generally unused on Linux).
127
128       POLLWRNORM
129              Equivalent to POLLOUT.
130
131       POLLWRBAND
132              Priority data may be written.
133
134       Linux also knows about, but does not use POLLMSG.
135
136   ppoll()
137       The relationship between poll() and ppoll() is analogous to  the  rela‐
138       tionship  between  select(2)  and  pselect(2): like pselect(2), ppoll()
139       allows an application to safely wait until  either  a  file  descriptor
140       becomes ready or until a signal is caught.
141
142       Other than the difference in the precision of the timeout argument, the
143       following ppoll() call:
144
145           ready = ppoll(&fds, nfds, tmo_p, &sigmask);
146
147       is nearly equivalent to atomically executing the following calls:
148
149           sigset_t origmask;
150           int timeout;
151
152           timeout = (tmo_p == NULL) ? -1 :
153                     (tmo_p->tv_sec * 1000 + tmo_p->tv_nsec / 1000000);
154           pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
155           ready = poll(&fds, nfds, timeout);
156           pthread_sigmask(SIG_SETMASK, &origmask, NULL);
157
158       The above code  segment  is  described  as  nearly  equivalent  because
159       whereas  a negative timeout value for poll() is interpreted as an infi‐
160       nite timeout, a negative value expressed in *tmo_p results in an  error
161       from ppoll().
162
163       See  the description of pselect(2) for an explanation of why ppoll() is
164       necessary.
165
166       If the sigmask argument is specified  as  NULL,  then  no  signal  mask
167       manipulation is performed (and thus ppoll() differs from poll() only in
168       the precision of the timeout argument).
169
170       The tmo_p argument specifies an upper limit on the amount of time  that
171       ppoll()  will  block.  This argument is a pointer to a structure of the
172       following form:
173
174           struct timespec {
175               long    tv_sec;         /* seconds */
176               long    tv_nsec;        /* nanoseconds */
177           };
178
179       If tmo_p is specified as NULL, then ppoll() can block indefinitely.
180

RETURN VALUE

182       On success, a positive number is returned; this is the number of struc‐
183       tures which have nonzero revents fields (in other words, those descrip‐
184       tors with events or errors reported).  A value of 0 indicates that  the
185       call  timed  out  and  no file descriptors were ready.  On error, -1 is
186       returned, and errno is set appropriately.
187

ERRORS

189       EFAULT The array given as argument was not  contained  in  the  calling
190              program's address space.
191
192       EINTR  A signal occurred before any requested event; see signal(7).
193
194       EINVAL The nfds value exceeds the RLIMIT_NOFILE value.
195
196       EINVAL (ppoll())  The  timeout value expressed in *ip is invalid (nega‐
197              tive).
198
199       ENOMEM There was no space to allocate file descriptor tables.
200

VERSIONS

202       The poll() system call was introduced in Linux 2.1.23.  On  older  ker‐
203       nels  that  lack  this  system call, the glibc (and the old Linux libc)
204       poll() wrapper function provides emulation using select(2).
205
206       The ppoll() system call was added  to  Linux  in  kernel  2.6.16.   The
207       ppoll() library call was added in glibc 2.4.
208

CONFORMING TO

210       poll()  conforms  to  POSIX.1-2001 and POSIX.1-2008.  ppoll() is Linux-
211       specific.
212

NOTES

214       The operation of poll() and ppoll() is not affected by  the  O_NONBLOCK
215       flag.
216
217       On  some  other  UNIX systems, poll() can fail with the error EAGAIN if
218       the system fails to allocate  kernel-internal  resources,  rather  than
219       ENOMEM  as Linux does.  POSIX permits this behavior.  Portable programs
220       may wish to check for EAGAIN and loop, just as with EINTR.
221
222       Some implementations define the nonstandard constant  INFTIM  with  the
223       value  -1  for  use as a timeout for poll().  This constant is not pro‐
224       vided in glibc.
225
226       For a discussion of what may happen if a file  descriptor  being  moni‐
227       tored by poll() is closed in another thread, see select(2).
228
229   C library/kernel differences
230       The  Linux  ppoll()  system call modifies its tmo_p argument.  However,
231       the glibc wrapper function hides this behavior by using a  local  vari‐
232       able for the timeout argument that is passed to the system call.  Thus,
233       the glibc ppoll() function does not modify its tmo_p argument.
234
235       The raw ppoll() system call has a fifth  argument,  size_t  sigsetsize,
236       which  specifies  the size in bytes of the sigmask argument.  The glibc
237       ppoll() wrapper function specifies  this  argument  as  a  fixed  value
238       (equal  to  sizeof(kernel_sigset_t)).  See sigprocmask(2) for a discus‐
239       sion on the differences between the kernel and the libc notion  of  the
240       sigset.
241

BUGS

243       See  the  discussion of spurious readiness notifications under the BUGS
244       section of select(2).
245

SEE ALSO

247       restart_syscall(2), select(2), select_tut(2), epoll(7), time(7)
248

COLOPHON

250       This page is part of release 5.04 of the Linux  man-pages  project.   A
251       description  of  the project, information about reporting bugs, and the
252       latest    version    of    this    page,    can     be     found     at
253       https://www.kernel.org/doc/man-pages/.
254
255
256
257Linux                             2019-08-02                           POLL(2)
Impressum