1poll(2) System Calls Manual poll(2)
2
3
4
6 poll, ppoll - wait for some event on a file descriptor
7
9 Standard C library (libc, -lc)
10
12 #include <poll.h>
13
14 int poll(struct pollfd *fds, nfds_t nfds, int timeout);
15
16 #define _GNU_SOURCE /* See feature_test_macros(7) */
17 #include <poll.h>
18
19 int ppoll(struct pollfd *fds, nfds_t nfds,
20 const struct timespec *_Nullable tmo_p,
21 const sigset_t *_Nullable sigmask);
22
24 poll() performs a similar task to select(2): it waits for one of a set
25 of file descriptors to become ready to perform I/O. The Linux-specific
26 epoll(7) API performs a similar task, but offers features beyond those
27 found in poll().
28
29 The set of file descriptors to be monitored is specified in the fds ar‐
30 gument, which is an array of structures of the following form:
31
32 struct pollfd {
33 int fd; /* file descriptor */
34 short events; /* requested events */
35 short revents; /* returned events */
36 };
37
38 The caller should specify the number of items in the fds array in nfds.
39
40 The field fd contains a file descriptor for an open file. If this
41 field is negative, then the corresponding events field is ignored and
42 the revents field returns zero. (This provides an easy way of ignoring
43 a file descriptor for a single poll() call: simply set the fd field to
44 its bitwise complement.)
45
46 The field events is an input parameter, a bit mask specifying the
47 events the application is interested in for the file descriptor fd.
48 This field may be specified as zero, in which case the only events that
49 can be returned in revents are POLLHUP, POLLERR, and POLLNVAL (see be‐
50 low).
51
52 The field revents is an output parameter, filled by the kernel with the
53 events that actually occurred. The bits returned in revents can in‐
54 clude any of those specified in events, or one of the values POLLERR,
55 POLLHUP, or POLLNVAL. (These three bits are meaningless in the events
56 field, and will be set in the revents field whenever the corresponding
57 condition is true.)
58
59 If none of the events requested (and no error) has occurred for any of
60 the file descriptors, then poll() blocks until one of the events oc‐
61 curs.
62
63 The timeout argument specifies the number of milliseconds that poll()
64 should block waiting for a file descriptor to become ready. The call
65 will block until either:
66
67 • a file descriptor becomes ready;
68
69 • the call is interrupted by a signal handler; or
70
71 • the timeout expires.
72
73 Note that the timeout interval will be rounded up to the system clock
74 granularity, and kernel scheduling delays mean that the blocking inter‐
75 val may overrun by a small amount. Specifying a negative value in
76 timeout means an infinite timeout. Specifying a timeout of zero causes
77 poll() to return immediately, even if no file descriptors are ready.
78
79 The bits that may be set/returned in events and revents are defined in
80 <poll.h>:
81
82 POLLIN There is data to read.
83
84 POLLPRI
85 There is some exceptional condition on the file descriptor.
86 Possibilities include:
87
88 • There is out-of-band data on a TCP socket (see tcp(7)).
89
90 • A pseudoterminal master in packet mode has seen a state
91 change on the slave (see ioctl_tty(2)).
92
93 • A cgroup.events file has been modified (see cgroups(7)).
94
95 POLLOUT
96 Writing is now possible, though a write larger than the avail‐
97 able space in a socket or pipe will still block (unless O_NON‐
98 BLOCK is set).
99
100 POLLRDHUP (since Linux 2.6.17)
101 Stream socket peer closed connection, or shut down writing half
102 of connection. The _GNU_SOURCE feature test macro must be de‐
103 fined (before including any header files) in order to obtain
104 this definition.
105
106 POLLERR
107 Error condition (only returned in revents; ignored in events).
108 This bit is also set for a file descriptor referring to the
109 write end of a pipe when the read end has been closed.
110
111 POLLHUP
112 Hang up (only returned in revents; ignored in events). Note
113 that when reading from a channel such as a pipe or a stream
114 socket, this event merely indicates that the peer closed its end
115 of the channel. Subsequent reads from the channel will return 0
116 (end of file) only after all outstanding data in the channel has
117 been consumed.
118
119 POLLNVAL
120 Invalid request: fd not open (only returned in revents; ignored
121 in events).
122
123 When compiling with _XOPEN_SOURCE defined, one also has the following,
124 which convey no further information beyond the bits listed above:
125
126 POLLRDNORM
127 Equivalent to POLLIN.
128
129 POLLRDBAND
130 Priority band data can be read (generally unused on Linux).
131
132 POLLWRNORM
133 Equivalent to POLLOUT.
134
135 POLLWRBAND
136 Priority data may be written.
137
138 Linux also knows about, but does not use POLLMSG.
139
140 ppoll()
141 The relationship between poll() and ppoll() is analogous to the rela‐
142 tionship between select(2) and pselect(2): like pselect(2), ppoll() al‐
143 lows an application to safely wait until either a file descriptor be‐
144 comes ready or until a signal is caught.
145
146 Other than the difference in the precision of the timeout argument, the
147 following ppoll() call:
148
149 ready = ppoll(&fds, nfds, tmo_p, &sigmask);
150
151 is nearly equivalent to atomically executing the following calls:
152
153 sigset_t origmask;
154 int timeout;
155
156 timeout = (tmo_p == NULL) ? -1 :
157 (tmo_p->tv_sec * 1000 + tmo_p->tv_nsec / 1000000);
158 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
159 ready = poll(&fds, nfds, timeout);
160 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
161
162 The above code segment is described as nearly equivalent because
163 whereas a negative timeout value for poll() is interpreted as an infi‐
164 nite timeout, a negative value expressed in *tmo_p results in an error
165 from ppoll().
166
167 See the description of pselect(2) for an explanation of why ppoll() is
168 necessary.
169
170 If the sigmask argument is specified as NULL, then no signal mask ma‐
171 nipulation is performed (and thus ppoll() differs from poll() only in
172 the precision of the timeout argument).
173
174 The tmo_p argument specifies an upper limit on the amount of time that
175 ppoll() will block. This argument is a pointer to a timespec(3) struc‐
176 ture.
177
178 If tmo_p is specified as NULL, then ppoll() can block indefinitely.
179
181 On success, poll() returns a nonnegative value which is the number of
182 elements in the pollfds whose revents fields have been set to a nonzero
183 value (indicating an event or an error). A return value of zero indi‐
184 cates that the system call timed out before any file descriptors became
185 ready.
186
187 On error, -1 is returned, and errno is set to indicate the error.
188
190 EFAULT fds points outside the process's accessible address space. The
191 array given as argument was not contained in the calling pro‐
192 gram's address space.
193
194 EINTR A signal occurred before any requested event; see signal(7).
195
196 EINVAL The nfds value exceeds the RLIMIT_NOFILE value.
197
198 EINVAL (ppoll()) The timeout value expressed in *tmo_p is invalid (neg‐
199 ative).
200
201 ENOMEM Unable to allocate memory for kernel data structures.
202
204 On some other UNIX systems, poll() can fail with the error EAGAIN if
205 the system fails to allocate kernel-internal resources, rather than
206 ENOMEM as Linux does. POSIX permits this behavior. Portable programs
207 may wish to check for EAGAIN and loop, just as with EINTR.
208
209 Some implementations define the nonstandard constant INFTIM with the
210 value -1 for use as a timeout for poll(). This constant is not pro‐
211 vided in glibc.
212
213 C library/kernel differences
214 The Linux ppoll() system call modifies its tmo_p argument. However,
215 the glibc wrapper function hides this behavior by using a local vari‐
216 able for the timeout argument that is passed to the system call. Thus,
217 the glibc ppoll() function does not modify its tmo_p argument.
218
219 The raw ppoll() system call has a fifth argument, size_t sigsetsize,
220 which specifies the size in bytes of the sigmask argument. The glibc
221 ppoll() wrapper function specifies this argument as a fixed value
222 (equal to sizeof(kernel_sigset_t)). See sigprocmask(2) for a discus‐
223 sion on the differences between the kernel and the libc notion of the
224 sigset.
225
227 poll() POSIX.1-2008.
228
229 ppoll()
230 Linux.
231
233 poll() POSIX.1-2001. Linux 2.1.23.
234
235 On older kernels that lack this system call, the glibc poll()
236 wrapper function provides emulation using select(2).
237
238 ppoll()
239 Linux 2.6.16, glibc 2.4.
240
242 The operation of poll() and ppoll() is not affected by the O_NONBLOCK
243 flag.
244
245 For a discussion of what may happen if a file descriptor being moni‐
246 tored by poll() is closed in another thread, see select(2).
247
249 See the discussion of spurious readiness notifications under the BUGS
250 section of select(2).
251
253 The program below opens each of the files named in its command-line ar‐
254 guments and monitors the resulting file descriptors for readiness to
255 read (POLLIN). The program loops, repeatedly using poll() to monitor
256 the file descriptors, printing the number of ready file descriptors on
257 return. For each ready file descriptor, the program:
258
259 • displays the returned revents field in a human-readable form;
260
261 • if the file descriptor is readable, reads some data from it, and
262 displays that data on standard output; and
263
264 • if the file descriptor was not readable, but some other event oc‐
265 curred (presumably POLLHUP), closes the file descriptor.
266
267 Suppose we run the program in one terminal, asking it to open a FIFO:
268
269 $ mkfifo myfifo
270 $ ./poll_input myfifo
271
272 In a second terminal window, we then open the FIFO for writing, write
273 some data to it, and close the FIFO:
274
275 $ echo aaaaabbbbbccccc > myfifo
276
277 In the terminal where we are running the program, we would then see:
278
279 Opened "myfifo" on fd 3
280 About to poll()
281 Ready: 1
282 fd=3; events: POLLIN POLLHUP
283 read 10 bytes: aaaaabbbbb
284 About to poll()
285 Ready: 1
286 fd=3; events: POLLIN POLLHUP
287 read 6 bytes: ccccc
288
289 About to poll()
290 Ready: 1
291 fd=3; events: POLLHUP
292 closing fd 3
293 All file descriptors closed; bye
294
295 In the above output, we see that poll() returned three times:
296
297 • On the first return, the bits returned in the revents field were
298 POLLIN, indicating that the file descriptor is readable, and POLL‐
299 HUP, indicating that the other end of the FIFO has been closed. The
300 program then consumed some of the available input.
301
302 • The second return from poll() also indicated POLLIN and POLLHUP; the
303 program then consumed the last of the available input.
304
305 • On the final return, poll() indicated only POLLHUP on the FIFO, at
306 which point the file descriptor was closed and the program termi‐
307 nated.
308
309 Program source
310
311 /* poll_input.c
312
313 Licensed under GNU General Public License v2 or later.
314 */
315 #include <fcntl.h>
316 #include <poll.h>
317 #include <stdio.h>
318 #include <stdlib.h>
319 #include <unistd.h>
320
321 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
322 } while (0)
323
324 int
325 main(int argc, char *argv[])
326 {
327 int ready;
328 char buf[10];
329 nfds_t num_open_fds, nfds;
330 ssize_t s;
331 struct pollfd *pfds;
332
333 if (argc < 2) {
334 fprintf(stderr, "Usage: %s file...\n", argv[0]);
335 exit(EXIT_FAILURE);
336 }
337
338 num_open_fds = nfds = argc - 1;
339 pfds = calloc(nfds, sizeof(struct pollfd));
340 if (pfds == NULL)
341 errExit("malloc");
342
343 /* Open each file on command line, and add it to 'pfds' array. */
344
345 for (nfds_t j = 0; j < nfds; j++) {
346 pfds[j].fd = open(argv[j + 1], O_RDONLY);
347 if (pfds[j].fd == -1)
348 errExit("open");
349
350 printf("Opened \"%s\" on fd %d\n", argv[j + 1], pfds[j].fd);
351
352 pfds[j].events = POLLIN;
353 }
354
355 /* Keep calling poll() as long as at least one file descriptor is
356 open. */
357
358 while (num_open_fds > 0) {
359 printf("About to poll()\n");
360 ready = poll(pfds, nfds, -1);
361 if (ready == -1)
362 errExit("poll");
363
364 printf("Ready: %d\n", ready);
365
366 /* Deal with array returned by poll(). */
367
368 for (nfds_t j = 0; j < nfds; j++) {
369 if (pfds[j].revents != 0) {
370 printf(" fd=%d; events: %s%s%s\n", pfds[j].fd,
371 (pfds[j].revents & POLLIN) ? "POLLIN " : "",
372 (pfds[j].revents & POLLHUP) ? "POLLHUP " : "",
373 (pfds[j].revents & POLLERR) ? "POLLERR " : "");
374
375 if (pfds[j].revents & POLLIN) {
376 s = read(pfds[j].fd, buf, sizeof(buf));
377 if (s == -1)
378 errExit("read");
379 printf(" read %zd bytes: %.*s\n",
380 s, (int) s, buf);
381 } else { /* POLLERR | POLLHUP */
382 printf(" closing fd %d\n", pfds[j].fd);
383 if (close(pfds[j].fd) == -1)
384 errExit("close");
385 num_open_fds--;
386 }
387 }
388 }
389 }
390
391 printf("All file descriptors closed; bye\n");
392 exit(EXIT_SUCCESS);
393 }
394
396 restart_syscall(2), select(2), select_tut(2), timespec(3), epoll(7),
397 time(7)
398
399
400
401Linux man-pages 6.04 2023-03-30 poll(2)