1POLL(2) Linux Programmer's Manual POLL(2)
2
3
4
6 poll, ppoll - wait for some event on a file descriptor
7
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
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 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 See the description of pselect(2) for an explanation of why ppoll() is
159 necessary.
160
161 If the sigmask argument is specified as NULL, then no signal mask
162 manipulation is performed (and thus ppoll() differs from poll() only in
163 the precision of the timeout argument).
164
165 The tmo_p argument specifies an upper limit on the amount of time that
166 ppoll() will block. This argument is a pointer to a structure of the
167 following form:
168
169 struct timespec {
170 long tv_sec; /* seconds */
171 long tv_nsec; /* nanoseconds */
172 };
173
174 If tmo_p is specified as NULL, then ppoll() can block indefinitely.
175
177 On success, a positive number is returned; this is the number of struc‐
178 tures which have nonzero revents fields (in other words, those descrip‐
179 tors with events or errors reported). A value of 0 indicates that the
180 call timed out and no file descriptors were ready. On error, -1 is
181 returned, and errno is set appropriately.
182
184 EFAULT The array given as argument was not contained in the calling
185 program's address space.
186
187 EINTR A signal occurred before any requested event; see signal(7).
188
189 EINVAL The nfds value exceeds the RLIMIT_NOFILE value.
190
191 ENOMEM There was no space to allocate file descriptor tables.
192
194 The poll() system call was introduced in Linux 2.1.23. On older ker‐
195 nels that lack this system call, the glibc (and the old Linux libc)
196 poll() wrapper function provides emulation using select(2).
197
198 The ppoll() system call was added to Linux in kernel 2.6.16. The
199 ppoll() library call was added in glibc 2.4.
200
202 poll() conforms to POSIX.1-2001 and POSIX.1-2008. ppoll() is Linux-
203 specific.
204
206 On some other UNIX systems, poll() can fail with the error EAGAIN if
207 the system fails to allocate kernel-internal resources, rather than
208 ENOMEM as Linux does. POSIX permits this behavior. Portable programs
209 may wish to check for EAGAIN and loop, just as with EINTR.
210
211 Some implementations define the nonstandard constant INFTIM with the
212 value -1 for use as a timeout for poll(). This constant is not pro‐
213 vided in glibc.
214
215 For a discussion of what may happen if a file descriptor being moni‐
216 tored by poll() is closed in another thread, see select(2).
217
218 C library/kernel differences
219 The Linux ppoll() system call modifies its tmo_p argument. However,
220 the glibc wrapper function hides this behavior by using a local vari‐
221 able for the timeout argument that is passed to the system call. Thus,
222 the glibc ppoll() function does not modify its tmo_p argument.
223
224 The raw ppoll() system call has a fifth argument, size_t sigsetsize,
225 which specifies the size in bytes of the sigmask argument. The glibc
226 ppoll() wrapper function specifies this argument as a fixed value
227 (equal to sizeof(kernel_sigset_t)). See sigprocmask(2) for a discus‐
228 sion on the differences between the kernel and the libc notion of the
229 sigset.
230
232 See the discussion of spurious readiness notifications under the BUGS
233 section of select(2).
234
236 restart_syscall(2), select(2), select_tut(2), epoll(7), time(7)
237
239 This page is part of release 4.16 of the Linux man-pages project. A
240 description of the project, information about reporting bugs, and the
241 latest version of this page, can be found at
242 https://www.kernel.org/doc/man-pages/.
243
244
245
246Linux 2017-09-15 POLL(2)