]>
Commit | Line | Data |
---|---|---|
733192cb | 1 | .\" Copyright (C) 2006, 2019 Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da | 2 | .\" |
5fbde956 | 3 | .\" SPDX-License-Identifier: Linux-man-pages-copyleft |
fea681da MK |
4 | .\" |
5 | .\" Additions from Richard Gooch <rgooch@atnf.CSIRO.AU> and aeb, 971207 | |
e755a587 | 6 | .\" 2006-03-13, mtk, Added ppoll() + various other rewordings |
23cb3cb5 MK |
7 | .\" 2006-07-01, mtk, Added POLLRDHUP + various other wording and |
8 | .\" formatting changes. | |
4b9d4c1a | 9 | .\" |
4c1c5274 | 10 | .TH poll 2 (date) "Linux man-pages (unreleased)" |
fea681da | 11 | .SH NAME |
e755a587 | 12 | poll, ppoll \- wait for some event on a file descriptor |
c29343b4 AC |
13 | .SH LIBRARY |
14 | Standard C library | |
8fc3b2cf | 15 | .RI ( libc ", " \-lc ) |
fea681da | 16 | .SH SYNOPSIS |
e755a587 | 17 | .nf |
23cb3cb5 | 18 | .B #include <poll.h> |
c6d039a3 | 19 | .P |
e755a587 | 20 | .BI "int poll(struct pollfd *" fds ", nfds_t " nfds ", int " timeout ); |
c6d039a3 | 21 | .P |
b80f966b | 22 | .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */" |
23cb3cb5 | 23 | .B #include <poll.h> |
c6d039a3 | 24 | .P |
77ca5b1d | 25 | .BI "int ppoll(struct pollfd *" fds ", nfds_t " nfds , |
5490a42f AC |
26 | .BI " const struct timespec *_Nullable " tmo_p , |
27 | .BI " const sigset_t *_Nullable " sigmask ); | |
e755a587 | 28 | .fi |
fea681da | 29 | .SH DESCRIPTION |
96296ef0 | 30 | .BR poll () |
e755a587 MK |
31 | performs a similar task to |
32 | .BR select (2): | |
33 | it waits for one of a set of file descriptors to become ready | |
34 | to perform I/O. | |
83bb822c MK |
35 | The Linux-specific |
36 | .BR epoll (7) | |
37 | API performs a similar task, but offers features beyond those found in | |
a36d5a35 | 38 | .BR poll (). |
c6d039a3 | 39 | .P |
e755a587 MK |
40 | The set of file descriptors to be monitored is specified in the |
41 | .I fds | |
d8dfbff9 | 42 | argument, which is an array of structures of the following form: |
c6d039a3 | 43 | .P |
088a639b | 44 | .in +4n |
e646a1ba | 45 | .EX |
a08ea57c MK |
46 | struct pollfd { |
47 | int fd; /* file descriptor */ | |
48 | short events; /* requested events */ | |
49 | short revents; /* returned events */ | |
50 | }; | |
b8302363 | 51 | .EE |
e646a1ba | 52 | .in |
c6d039a3 | 53 | .P |
d8dfbff9 MK |
54 | The caller should specify the number of items in the |
55 | .I fds | |
56 | array in | |
57 | .IR nfds . | |
c6d039a3 | 58 | .P |
fea681da MK |
59 | The field |
60 | .I fd | |
61 | contains a file descriptor for an open file. | |
096ca08a MK |
62 | If this field is negative, then the corresponding |
63 | .I events | |
64 | field is ignored and the | |
65 | .I revents | |
66 | field returns zero. | |
67 | (This provides an easy way of ignoring a | |
68 | file descriptor for a single | |
69 | .BR poll () | |
17411a0f | 70 | call: simply set the |
096ca08a | 71 | .I fd |
17411a0f | 72 | field to its bitwise complement.) |
c6d039a3 | 73 | .P |
fea681da MK |
74 | The field |
75 | .I events | |
10f5f294 | 76 | is an input parameter, a bit mask specifying the events the application |
b857fda5 MK |
77 | is interested in for the file descriptor |
78 | .IR fd . | |
210cded6 MK |
79 | This field may be specified as zero, |
80 | in which case the only events that can be returned in | |
b857fda5 | 81 | .I revents |
210cded6 MK |
82 | are |
83 | .BR POLLHUP , | |
84 | .BR POLLERR , | |
85 | and | |
86 | .B POLLNVAL | |
87 | (see below). | |
c6d039a3 | 88 | .P |
fea681da MK |
89 | The field |
90 | .I revents | |
91 | is an output parameter, filled by the kernel with the events that | |
e755a587 MK |
92 | actually occurred. |
93 | The bits returned in | |
94 | .I revents | |
c13182ef | 95 | can include any of those specified in |
e755a587 MK |
96 | .IR events , |
97 | or one of the values | |
98 | .BR POLLERR , | |
99 | .BR POLLHUP , | |
fea681da MK |
100 | or |
101 | .BR POLLNVAL . | |
102 | (These three bits are meaningless in the | |
103 | .I events | |
104 | field, and will be set in the | |
105 | .I revents | |
106 | field whenever the corresponding condition is true.) | |
c6d039a3 | 107 | .P |
fea681da | 108 | If none of the events requested (and no error) has occurred for any |
e755a587 MK |
109 | of the file descriptors, then |
110 | .BR poll () | |
111 | blocks until one of the events occurs. | |
c6d039a3 | 112 | .P |
c13182ef | 113 | The |
e755a587 | 114 | .I timeout |
0d9101c4 | 115 | argument specifies the number of milliseconds that |
e755a587 | 116 | .BR poll () |
0d9101c4 | 117 | should block waiting for a file descriptor to become ready. |
40df3d00 | 118 | The call will block until either: |
cdede5cd | 119 | .IP \[bu] 3 |
40df3d00 | 120 | a file descriptor becomes ready; |
cdede5cd | 121 | .IP \[bu] |
40df3d00 | 122 | the call is interrupted by a signal handler; or |
cdede5cd | 123 | .IP \[bu] |
216b9e61 | 124 | the timeout expires. |
c6d039a3 | 125 | .P |
f9f86b3d | 126 | Being "ready" means that the requested operation will not block; thus, |
127 | .BR poll ()ing | |
128 | regular files, | |
129 | block devices, | |
130 | and other files with no reasonable polling semantic | |
131 | .I always | |
132 | returns instantly as ready to read and write. | |
c6d039a3 | 133 | .P |
40df3d00 MK |
134 | Note that the |
135 | .I timeout | |
136 | interval will be rounded up to the system clock granularity, | |
19257d8f | 137 | and kernel scheduling delays mean that the blocking interval |
0d9101c4 | 138 | may overrun by a small amount. |
e755a587 MK |
139 | Specifying a negative value in |
140 | .I timeout | |
141 | means an infinite timeout. | |
77e74cf1 MK |
142 | Specifying a |
143 | .I timeout | |
144 | of zero causes | |
145 | .BR poll () | |
01d8b73f | 146 | to return immediately, even if no file descriptors are ready. |
c6d039a3 | 147 | .P |
23cb3cb5 MK |
148 | The bits that may be set/returned in |
149 | .I events | |
150 | and | |
151 | .I revents | |
c84371c6 | 152 | are defined in \fI<poll.h>\fP: |
23cb3cb5 MK |
153 | .TP |
154 | .B POLLIN | |
155 | There is data to read. | |
156 | .TP | |
157 | .B POLLPRI | |
e629dd78 MK |
158 | There is some exceptional condition on the file descriptor. |
159 | Possibilities include: | |
160 | .RS | |
cdede5cd | 161 | .IP \[bu] 3 |
e629dd78 MK |
162 | There is out-of-band data on a TCP socket (see |
163 | .BR tcp (7)). | |
cdede5cd | 164 | .IP \[bu] |
e629dd78 MK |
165 | A pseudoterminal master in packet mode has seen a state change on the slave |
166 | (see | |
167 | .BR ioctl_tty (2)). | |
cdede5cd | 168 | .IP \[bu] |
e629dd78 MK |
169 | A |
170 | .I cgroup.events | |
171 | file has been modified (see | |
172 | .BR cgroups (7)). | |
173 | .RE | |
23cb3cb5 MK |
174 | .TP |
175 | .B POLLOUT | |
aeaf667e | 176 | Writing is now possible, though a write larger than the available space |
d2e7d1bb RR |
177 | in a socket or pipe will still block (unless |
178 | .B O_NONBLOCK | |
179 | is set). | |
23cb3cb5 MK |
180 | .TP |
181 | .BR POLLRDHUP " (since Linux 2.6.17)" | |
c13182ef | 182 | Stream socket peer closed connection, |
23cb3cb5 | 183 | or shut down writing half of connection. |
c13182ef | 184 | The |
23cb3cb5 | 185 | .B _GNU_SOURCE |
e417acb0 MK |
186 | feature test macro must be defined |
187 | (before including | |
188 | .I any | |
189 | header files) | |
190 | in order to obtain this definition. | |
23cb3cb5 MK |
191 | .TP |
192 | .B POLLERR | |
b09322c4 MK |
193 | Error condition (only returned in |
194 | .IR revents ; | |
195 | ignored in | |
196 | .IR events ). | |
b3da9249 MK |
197 | This bit is also set for a file descriptor referring |
198 | to the write end of a pipe when the read end has been closed. | |
23cb3cb5 MK |
199 | .TP |
200 | .B POLLHUP | |
b09322c4 MK |
201 | Hang up (only returned in |
202 | .IR revents ; | |
203 | ignored in | |
204 | .IR events ). | |
7e8924ba MK |
205 | Note that when reading from a channel such as a pipe or a stream socket, |
206 | this event merely indicates that the peer closed its end of the channel. | |
207 | Subsequent reads from the channel will return 0 (end of file) | |
208 | only after all outstanding data in the channel has been consumed. | |
23cb3cb5 MK |
209 | .TP |
210 | .B POLLNVAL | |
c13182ef | 211 | Invalid request: |
23cb3cb5 | 212 | .I fd |
b09322c4 MK |
213 | not open (only returned in |
214 | .IR revents ; | |
215 | ignored in | |
216 | .IR events ). | |
c6d039a3 | 217 | .P |
c13182ef | 218 | When compiling with |
23cb3cb5 | 219 | .B _XOPEN_SOURCE |
c13182ef | 220 | defined, one also has the following, |
23cb3cb5 | 221 | which convey no further information beyond the bits listed above: |
23cb3cb5 MK |
222 | .TP |
223 | .B POLLRDNORM | |
224 | Equivalent to | |
225 | .BR POLLIN . | |
226 | .TP | |
227 | .B POLLRDBAND | |
228 | Priority band data can be read (generally unused on Linux). | |
229 | .\" POLLRDBAND is used in the DECnet protocol. | |
230 | .TP | |
231 | .B POLLWRNORM | |
232 | Equivalent to | |
233 | .BR POLLOUT . | |
234 | .TP | |
235 | .B POLLWRBAND | |
236 | Priority data may be written. | |
c6d039a3 | 237 | .P |
c13182ef | 238 | Linux also knows about, but does not use |
23cb3cb5 | 239 | .BR POLLMSG . |
e755a587 | 240 | .SS ppoll() |
c13182ef | 241 | The relationship between |
e755a587 | 242 | .BR poll () |
c13182ef MK |
243 | and |
244 | .BR ppoll () | |
e755a587 | 245 | is analogous to the relationship between |
0bfa087b | 246 | .BR select (2) |
e755a587 | 247 | and |
0bfa087b | 248 | .BR pselect (2): |
e755a587 | 249 | like |
0bfa087b | 250 | .BR pselect (2), |
e755a587 MK |
251 | .BR ppoll () |
252 | allows an application to safely wait until either a file descriptor | |
253 | becomes ready or until a signal is caught. | |
c6d039a3 | 254 | .P |
b8c599cb | 255 | Other than the difference in the precision of the |
39c05c26 YK |
256 | .I timeout |
257 | argument, the following | |
e755a587 MK |
258 | .BR ppoll () |
259 | call: | |
c6d039a3 | 260 | .P |
408731d4 MK |
261 | .in +4n |
262 | .EX | |
263 | ready = ppoll(&fds, nfds, tmo_p, &sigmask); | |
264 | .EE | |
265 | .in | |
c6d039a3 | 266 | .P |
0bdda5d0 | 267 | is nearly equivalent to |
e755a587 MK |
268 | .I atomically |
269 | executing the following calls: | |
c6d039a3 | 270 | .P |
408731d4 MK |
271 | .in +4n |
272 | .EX | |
273 | sigset_t origmask; | |
274 | int timeout; | |
fe5dba13 | 275 | \& |
408731d4 MK |
276 | timeout = (tmo_p == NULL) ? \-1 : |
277 | (tmo_p\->tv_sec * 1000 + tmo_p\->tv_nsec / 1000000); | |
278 | pthread_sigmask(SIG_SETMASK, &sigmask, &origmask); | |
279 | ready = poll(&fds, nfds, timeout); | |
280 | pthread_sigmask(SIG_SETMASK, &origmask, NULL); | |
281 | .EE | |
282 | .in | |
c6d039a3 | 283 | .P |
0bdda5d0 MK |
284 | The above code segment is described as |
285 | .I nearly | |
286 | equivalent because whereas a negative | |
287 | .I timeout | |
288 | value for | |
289 | .BR poll () | |
290 | is interpreted as an infinite timeout, a negative value expressed in | |
1ae6b2c7 | 291 | .I *tmo_p |
0bdda5d0 MK |
292 | results in an error from |
293 | .BR ppoll (). | |
c6d039a3 | 294 | .P |
e755a587 MK |
295 | See the description of |
296 | .BR pselect (2) | |
297 | for an explanation of why | |
298 | .BR ppoll () | |
299 | is necessary. | |
c6d039a3 | 300 | .P |
3ee89512 MK |
301 | If the |
302 | .I sigmask | |
303 | argument is specified as NULL, then | |
304 | no signal mask manipulation is performed | |
305 | (and thus | |
306 | .BR ppoll () | |
307 | differs from | |
308 | .BR poll () | |
79fd50e6 YK |
309 | only in the precision of the |
310 | .I timeout | |
311 | argument). | |
c6d039a3 | 312 | .P |
e755a587 | 313 | The |
0bf502fc | 314 | .I tmo_p |
c13182ef | 315 | argument specifies an upper limit on the amount of time that |
e755a587 MK |
316 | .BR ppoll () |
317 | will block. | |
e97e048a | 318 | This argument is a pointer to a |
319 | .BR timespec (3) | |
320 | structure. | |
c6d039a3 | 321 | .P |
e755a587 | 322 | If |
0bf502fc | 323 | .I tmo_p |
c13182ef | 324 | is specified as NULL, then |
e755a587 MK |
325 | .BR ppoll () |
326 | can block indefinitely. | |
47297adb | 327 | .SH RETURN VALUE |
8bcfd688 MK |
328 | On success, |
329 | .BR poll () | |
330 | returns a nonnegative value which is the number of elements in the | |
331 | .I pollfds | |
332 | whose | |
fea681da | 333 | .I revents |
8bcfd688 MK |
334 | fields have been set to a nonzero value (indicating an event or an error). |
335 | A return value of zero indicates that the system call timed out | |
acb3399a | 336 | before any file descriptors became ready. |
c6d039a3 | 337 | .P |
c13182ef | 338 | On error, \-1 is returned, and |
fea681da | 339 | .I errno |
855d489a | 340 | is set to indicate the error. |
fea681da MK |
341 | .SH ERRORS |
342 | .TP | |
fea681da | 343 | .B EFAULT |
342819c8 MK |
344 | .I fds |
345 | points outside the process's accessible address space. | |
fea681da MK |
346 | The array given as argument was not contained in the calling program's |
347 | address space. | |
348 | .TP | |
349 | .B EINTR | |
01538d0d MK |
350 | A signal occurred before any requested event; see |
351 | .BR signal (7). | |
fea681da MK |
352 | .TP |
353 | .B EINVAL | |
354 | The | |
355 | .I nfds | |
682edefb MK |
356 | value exceeds the |
357 | .B RLIMIT_NOFILE | |
358 | value. | |
fea681da | 359 | .TP |
0bdda5d0 MK |
360 | .B EINVAL |
361 | .RB ( ppoll ()) | |
362 | The timeout value expressed in | |
03ea37c5 | 363 | .I *tmo_p |
0bdda5d0 MK |
364 | is invalid (negative). |
365 | .TP | |
fea681da | 366 | .B ENOMEM |
8bcfd688 | 367 | Unable to allocate memory for kernel data structures. |
e755a587 | 368 | .SH VERSIONS |
f1463757 MK |
369 | On some other UNIX systems, |
370 | .\" Darwin, according to a report by Jeremy Sequoia, relayed by Josh Triplett | |
371 | .BR poll () | |
372 | can fail with the error | |
373 | .B EAGAIN | |
374 | if the system fails to allocate kernel-internal resources, rather than | |
375 | .B ENOMEM | |
376 | as Linux does. | |
377 | POSIX permits this behavior. | |
378 | Portable programs may wish to check for | |
379 | .B EAGAIN | |
380 | and loop, just as with | |
381 | .BR EINTR . | |
c6d039a3 | 382 | .P |
c8f2dd47 | 383 | Some implementations define the nonstandard constant |
c1164764 MK |
384 | .B INFTIM |
385 | with the value \-1 for use as a | |
1ae6b2c7 | 386 | .I timeout |
b8c599cb | 387 | for |
ba2e3e9a | 388 | .BR poll (). |
c1164764 | 389 | This constant is not provided in glibc. |
0722a578 | 390 | .SS C library/kernel differences |
4fb31341 MK |
391 | The Linux |
392 | .BR ppoll () | |
393 | system call modifies its | |
0bf502fc | 394 | .I tmo_p |
4fb31341 | 395 | argument. |
d9bfdb9c | 396 | However, the glibc wrapper function hides this behavior |
4fb31341 MK |
397 | by using a local variable for the timeout argument that |
398 | is passed to the system call. | |
399 | Thus, the glibc | |
400 | .BR ppoll () | |
401 | function does not modify its | |
0bf502fc | 402 | .I tmo_p |
4fb31341 | 403 | argument. |
c6d039a3 | 404 | .P |
361ec688 MK |
405 | The raw |
406 | .BR ppoll () | |
407 | system call has a fifth argument, | |
408 | .IR "size_t sigsetsize" , | |
409 | which specifies the size in bytes of the | |
1ae6b2c7 | 410 | .I sigmask |
361ec688 MK |
411 | argument. |
412 | The glibc | |
413 | .BR ppoll () | |
414 | wrapper function specifies this argument as a fixed value | |
415 | (equal to | |
f8dcca84 MK |
416 | .IR sizeof(kernel_sigset_t) ). |
417 | See | |
418 | .BR sigprocmask (2) | |
f25ea51b N |
419 | for a discussion on the differences between the kernel and the libc |
420 | notion of the sigset. | |
4131356c AC |
421 | .SH STANDARDS |
422 | .TP | |
423 | .BR poll () | |
424 | POSIX.1-2008. | |
425 | .TP | |
426 | .BR ppoll () | |
427 | Linux. | |
428 | .\" FIXME . | |
429 | .\" ppoll() is proposed for inclusion in POSIX: | |
430 | .\" https://www.austingroupbugs.net/view.php?id=1263 | |
431 | .\" NetBSD 3.0 has a pollts() which is like Linux ppoll(). | |
432 | .SH HISTORY | |
433 | .TP | |
434 | .BR poll () | |
435 | POSIX.1-2001. | |
436 | Linux 2.1.23. | |
437 | .IP | |
438 | On older kernels that lack this system call, | |
439 | the glibc | |
440 | .BR poll () | |
441 | wrapper function provides emulation using | |
442 | .BR select (2). | |
443 | .TP | |
444 | .BR ppoll () | |
445 | Linux 2.6.16, | |
446 | glibc 2.4. | |
447 | .SH NOTES | |
448 | The operation of | |
449 | .BR poll () | |
450 | and | |
451 | .BR ppoll () | |
452 | is not affected by the | |
453 | .B O_NONBLOCK | |
454 | flag. | |
c6d039a3 | 455 | .P |
4131356c AC |
456 | For a discussion of what may happen if a file descriptor being monitored by |
457 | .BR poll () | |
458 | is closed in another thread, see | |
459 | .BR select (2). | |
a1d5f77c MK |
460 | .SH BUGS |
461 | See the discussion of spurious readiness notifications under the | |
462 | BUGS section of | |
463 | .BR select (2). | |
a14af333 | 464 | .SH EXAMPLES |
836efc18 MK |
465 | The program below opens each of the files named in its command-line |
466 | arguments and monitors the resulting file descriptors for readiness to read | |
467 | .RB ( POLLIN ). | |
468 | The program loops, repeatedly using | |
469 | .BR poll () | |
470 | to monitor the file descriptors, | |
471 | printing the number of ready file descriptors on return. | |
472 | For each ready file descriptor, the program: | |
cdede5cd | 473 | .IP \[bu] 3 |
836efc18 MK |
474 | displays the returned |
475 | .I revents | |
476 | field in a human-readable form; | |
cdede5cd | 477 | .IP \[bu] |
836efc18 MK |
478 | if the file descriptor is readable, reads some data from it, |
479 | and displays that data on standard output; and | |
cdede5cd | 480 | .IP \[bu] |
03ea37c5 | 481 | if the file descriptor was not readable, |
836efc18 MK |
482 | but some other event occurred (presumably |
483 | .BR POLLHUP ), | |
484 | closes the file descriptor. | |
c6d039a3 | 485 | .P |
836efc18 | 486 | Suppose we run the program in one terminal, asking it to open a FIFO: |
c6d039a3 | 487 | .P |
836efc18 MK |
488 | .in +4n |
489 | .EX | |
490 | $ \fBmkfifo myfifo\fP | |
491 | $ \fB./poll_input myfifo\fP | |
492 | .EE | |
493 | .in | |
c6d039a3 | 494 | .P |
836efc18 MK |
495 | In a second terminal window, we then open the FIFO for writing, |
496 | write some data to it, and close the FIFO: | |
c6d039a3 | 497 | .P |
836efc18 MK |
498 | .in +4n |
499 | .EX | |
500 | $ \fBecho aaaaabbbbbccccc > myfifo\fP | |
501 | .EE | |
502 | .in | |
c6d039a3 | 503 | .P |
836efc18 | 504 | In the terminal where we are running the program, we would then see: |
c6d039a3 | 505 | .P |
836efc18 MK |
506 | .in +4n |
507 | .EX | |
508 | Opened "myfifo" on fd 3 | |
509 | About to poll() | |
510 | Ready: 1 | |
511 | fd=3; events: POLLIN POLLHUP | |
512 | read 10 bytes: aaaaabbbbb | |
513 | About to poll() | |
514 | Ready: 1 | |
515 | fd=3; events: POLLIN POLLHUP | |
516 | read 6 bytes: ccccc | |
fe5dba13 | 517 | \& |
836efc18 MK |
518 | About to poll() |
519 | Ready: 1 | |
520 | fd=3; events: POLLHUP | |
521 | closing fd 3 | |
522 | All file descriptors closed; bye | |
523 | .EE | |
524 | .in | |
c6d039a3 | 525 | .P |
836efc18 MK |
526 | In the above output, we see that |
527 | .BR poll () | |
528 | returned three times: | |
cdede5cd | 529 | .IP \[bu] 3 |
836efc18 MK |
530 | On the first return, the bits returned in the |
531 | .I revents | |
532 | field were | |
533 | .BR POLLIN , | |
534 | indicating that the file descriptor is readable, and | |
535 | .BR POLLHUP , | |
536 | indicating that the other end of the FIFO has been closed. | |
537 | The program then consumed some of the available input. | |
cdede5cd | 538 | .IP \[bu] |
836efc18 MK |
539 | The second return from |
540 | .BR poll () | |
541 | also indicated | |
1ae6b2c7 | 542 | .B POLLIN |
836efc18 MK |
543 | and |
544 | .BR POLLHUP ; | |
545 | the program then consumed the last of the available input. | |
cdede5cd | 546 | .IP \[bu] |
836efc18 MK |
547 | On the final return, |
548 | .BR poll () | |
549 | indicated only | |
1ae6b2c7 | 550 | .B POLLHUP |
836efc18 MK |
551 | on the FIFO, |
552 | at which point the file descriptor was closed and the program terminated. | |
553 | .\" | |
554 | .SS Program source | |
555 | \& | |
33857069 | 556 | .\" SRC BEGIN (poll_input.c) |
30dd9882 | 557 | .EX |
78391fde | 558 | /* poll_input.c |
fe5dba13 | 559 | \& |
78391fde MK |
560 | Licensed under GNU General Public License v2 or later. |
561 | */ | |
836efc18 | 562 | #include <fcntl.h> |
47b94bbd | 563 | #include <poll.h> |
836efc18 MK |
564 | #include <stdio.h> |
565 | #include <stdlib.h> | |
566 | #include <unistd.h> | |
fe5dba13 | 567 | \& |
836efc18 MK |
568 | #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e |
569 | } while (0) | |
fe5dba13 | 570 | \& |
836efc18 MK |
571 | int |
572 | main(int argc, char *argv[]) | |
573 | { | |
b42296e4 | 574 | int ready; |
041a155c | 575 | char buf[10]; |
b42296e4 | 576 | nfds_t num_open_fds, nfds; |
041a155c AC |
577 | ssize_t s; |
578 | struct pollfd *pfds; | |
fe5dba13 | 579 | \& |
836efc18 MK |
580 | if (argc < 2) { |
581 | fprintf(stderr, "Usage: %s file...\en", argv[0]); | |
582 | exit(EXIT_FAILURE); | |
583 | } | |
fe5dba13 | 584 | \& |
836efc18 MK |
585 | num_open_fds = nfds = argc \- 1; |
586 | pfds = calloc(nfds, sizeof(struct pollfd)); | |
587 | if (pfds == NULL) | |
588 | errExit("malloc"); | |
fe5dba13 | 589 | \& |
b957f81f | 590 | /* Open each file on command line, and add it to \[aq]pfds\[aq] array. */ |
fe5dba13 | 591 | \& |
b42296e4 | 592 | for (nfds_t j = 0; j < nfds; j++) { |
836efc18 MK |
593 | pfds[j].fd = open(argv[j + 1], O_RDONLY); |
594 | if (pfds[j].fd == \-1) | |
595 | errExit("open"); | |
fe5dba13 | 596 | \& |
836efc18 | 597 | printf("Opened \e"%s\e" on fd %d\en", argv[j + 1], pfds[j].fd); |
fe5dba13 | 598 | \& |
836efc18 MK |
599 | pfds[j].events = POLLIN; |
600 | } | |
fe5dba13 | 601 | \& |
836efc18 | 602 | /* Keep calling poll() as long as at least one file descriptor is |
46b20ca1 | 603 | open. */ |
fe5dba13 | 604 | \& |
836efc18 | 605 | while (num_open_fds > 0) { |
836efc18 MK |
606 | printf("About to poll()\en"); |
607 | ready = poll(pfds, nfds, \-1); | |
608 | if (ready == \-1) | |
609 | errExit("poll"); | |
fe5dba13 | 610 | \& |
836efc18 | 611 | printf("Ready: %d\en", ready); |
fe5dba13 | 612 | \& |
46b20ca1 | 613 | /* Deal with array returned by poll(). */ |
fe5dba13 | 614 | \& |
b42296e4 | 615 | for (nfds_t j = 0; j < nfds; j++) { |
836efc18 MK |
616 | if (pfds[j].revents != 0) { |
617 | printf(" fd=%d; events: %s%s%s\en", pfds[j].fd, | |
4687ab0e AC |
618 | (pfds[j].revents & POLLIN) ? "POLLIN " : "", |
619 | (pfds[j].revents & POLLHUP) ? "POLLHUP " : "", | |
620 | (pfds[j].revents & POLLERR) ? "POLLERR " : ""); | |
fe5dba13 | 621 | \& |
836efc18 | 622 | if (pfds[j].revents & POLLIN) { |
041a155c | 623 | s = read(pfds[j].fd, buf, sizeof(buf)); |
836efc18 MK |
624 | if (s == \-1) |
625 | errExit("read"); | |
626 | printf(" read %zd bytes: %.*s\en", | |
4687ab0e | 627 | s, (int) s, buf); |
836efc18 MK |
628 | } else { /* POLLERR | POLLHUP */ |
629 | printf(" closing fd %d\en", pfds[j].fd); | |
630 | if (close(pfds[j].fd) == \-1) | |
631 | errExit("close"); | |
632 | num_open_fds\-\-; | |
633 | } | |
634 | } | |
635 | } | |
636 | } | |
fe5dba13 | 637 | \& |
836efc18 MK |
638 | printf("All file descriptors closed; bye\en"); |
639 | exit(EXIT_SUCCESS); | |
640 | } | |
30dd9882 | 641 | .EE |
33857069 | 642 | .\" SRC END |
47297adb | 643 | .SH SEE ALSO |
d806bc05 | 644 | .BR restart_syscall (2), |
fea681da | 645 | .BR select (2), |
50e5322c | 646 | .BR select_tut (2), |
e97e048a | 647 | .BR timespec (3), |
9755c034 | 648 | .BR epoll (7), |
1d7c4d16 | 649 | .BR time (7) |