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 <poll.h>
15
16 int ppoll(struct pollfd *fds, nfds_t nfds,
17 const struct timespec *timeout_ts, const sigset_t *sigmask);
18
20 poll() performs a similar task to select(2): it waits for one of a set
21 of file descriptors to become ready to perform I/O.
22
23 The set of file descriptors to be monitored is specified in the fds
24 argument, which is an array of structures of the following form:
25
26 struct pollfd {
27 int fd; /* file descriptor */
28 short events; /* requested events */
29 short revents; /* returned events */
30 };
31
32 The caller should specify the number of items in the fds array in nfds.
33
34 The field fd contains a file descriptor for an open file. If this
35 field is negative, then the corresponding events field is ignored and
36 the revents field returns zero. (This provides an easy way of ignoring
37 a file descriptor for a single poll() call: simply negate the fd
38 field.)
39
40 The field events is an input parameter, a bit mask specifying the
41 events the application is interested in for the file descriptor fd. If
42 this field is specified as zero, then all events are ignored for fd and
43 revents returns zero.
44
45 The field revents is an output parameter, filled by the kernel with the
46 events that actually occurred. The bits returned in revents can
47 include any of those specified in events, or one of the values POLLERR,
48 POLLHUP, or POLLNVAL. (These three bits are meaningless in the events
49 field, and will be set in the revents field whenever the corresponding
50 condition is true.)
51
52 If none of the events requested (and no error) has occurred for any of
53 the file descriptors, then poll() blocks until one of the events
54 occurs.
55
56 The timeout argument specifies the minimum number of milliseconds that
57 poll() will block. (This interval will be rounded up to the system
58 clock granularity, and kernel scheduling delays mean that the blocking
59 interval may overrun by a small amount.) Specifying a negative value
60 in timeout means an infinite timeout. Specifying a timeout of zero
61 causes poll() to return immediately, even if no file descriptors are
62 ready.
63
64 The bits that may be set/returned in events and revents are defined in
65 <poll.h>:
66
67 POLLIN There is data to read.
68
69 POLLPRI
70 There is urgent data to read (e.g., out-of-band data on
71 TCP socket; pseudoterminal master in packet mode has seen
72 state change in slave).
73
74 POLLOUT
75 Writing now will not block.
76
77 POLLRDHUP (since Linux 2.6.17)
78 Stream socket peer closed connection, or shut down writ‐
79 ing half of connection. The _GNU_SOURCE feature test
80 macro must be defined (before including any header files)
81 in order to obtain this definition.
82
83 POLLERR
84 Error condition (output only).
85
86 POLLHUP
87 Hang up (output only).
88
89 POLLNVAL
90 Invalid request: fd not open (output only).
91
92 When compiling with _XOPEN_SOURCE defined, one also has the following,
93 which convey no further information beyond the bits listed above:
94
95 POLLRDNORM
96 Equivalent to POLLIN.
97
98 POLLRDBAND
99 Priority band data can be read (generally unused on
100 Linux).
101
102 POLLWRNORM
103 Equivalent to POLLOUT.
104
105 POLLWRBAND
106 Priority data may be written.
107
108 Linux also knows about, but does not use POLLMSG.
109
110 ppoll()
111 The relationship between poll() and ppoll() is analogous to the rela‐
112 tionship between select(2) and pselect(2): like pselect(2), ppoll()
113 allows an application to safely wait until either a file descriptor
114 becomes ready or until a signal is caught.
115
116 Other than the difference in the precision of the timeout argument, the
117 following ppoll() call:
118
119 ready = ppoll(&fds, nfds, timeout_ts, &sigmask);
120
121 is equivalent to atomically executing the following calls:
122
123 sigset_t origmask;
124 int timeout;
125
126 timeout = (timeout_ts == NULL) ? -1 :
127 (timeout_ts.tv_sec * 1000 + timeout_ts.tv_nsec / 1000000);
128 sigprocmask(SIG_SETMASK, &sigmask, &origmask);
129 ready = poll(&fds, nfds, timeout);
130 sigprocmask(SIG_SETMASK, &origmask, NULL);
131
132 See the description of pselect(2) for an explanation of why ppoll() is
133 necessary.
134
135 If the sigmask argument is specified as NULL, then no signal mask
136 manipulation is performed (and thus ppoll() differs from poll() only in
137 the precision of the timeout argument).
138
139 The timeout_ts argument specifies an upper limit on the amount of time
140 that ppoll() will block. This argument is a pointer to a structure of
141 the following form:
142
143 struct timespec {
144 long tv_sec; /* seconds */
145 long tv_nsec; /* nanoseconds */
146 };
147
148 If timeout_ts is specified as NULL, then ppoll() can block indefi‐
149 nitely.
150
152 On success, a positive number is returned; this is the number of struc‐
153 tures which have nonzero revents fields (in other words, those descrip‐
154 tors with events or errors reported). A value of 0 indicates that the
155 call timed out and no file descriptors were ready. On error, -1 is
156 returned, and errno is set appropriately.
157
159 EFAULT The array given as argument was not contained in the calling
160 program's address space.
161
162 EINTR A signal occurred before any requested event; see signal(7).
163
164 EINVAL The nfds value exceeds the RLIMIT_NOFILE value.
165
166 ENOMEM There was no space to allocate file descriptor tables.
167
169 The poll() system call was introduced in Linux 2.1.23. On older ker‐
170 nels that lack this system call, the glibc (and the old Linux libc)
171 poll() wrapper function provides emulation using select(2).
172
173 The ppoll() system call was added to Linux in kernel 2.6.16. The
174 ppoll() library call was added in glibc 2.4.
175
177 poll() conforms to POSIX.1-2001. ppoll() is Linux-specific.
178
180 Some implementations define the nonstandard constant INFTIM with the
181 value -1 for use as a timeout for poll(). This constant is not pro‐
182 vided in glibc.
183
184 For a discussion of what may happen if a file descriptor being moni‐
185 tored by poll() is closed in another thread, see select(2).
186
187 Linux notes
188 The Linux ppoll() system call modifies its timeout_ts argument. How‐
189 ever, the glibc wrapper function hides this behavior by using a local
190 variable for the timeout argument that is passed to the system call.
191 Thus, the glibc ppoll() function does not modify its timeout_ts argu‐
192 ment.
193
195 See the discussion of spurious readiness notifications under the BUGS
196 section of select(2).
197
199 restart_syscall(2), select(2), select_tut(2), time(7)
200
202 This page is part of release 3.53 of the Linux man-pages project. A
203 description of the project, and information about reporting bugs, can
204 be found at http://www.kernel.org/doc/man-pages/.
205
206
207
208Linux 2013-07-30 POLL(2)