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
14 #include <poll.h>
15
16 int ppoll(struct pollfd *fds, nfds_t nfds,
17 const struct timespec *timeout, 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 nfds 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 field fd contains a file descriptor for an open file.
33
34 The field events is an input parameter, a bit mask specifying the
35 events the application is interested in.
36
37 The field revents is an output parameter, filled by the kernel with the
38 events that actually occurred. The bits returned in revents can
39 include any of those specified in events, or one of the values POLLERR,
40 POLLHUP, or POLLNVAL. (These three bits are meaningless in the events
41 field, and will be set in the revents field whenever the corresponding
42 condition is true.)
43
44 If none of the events requested (and no error) has occurred for any of
45 the file descriptors, then poll() blocks until one of the events
46 occurs.
47
48 The timeout argument specifies an upper limit on the time for which
49 poll() will block, in milliseconds. Specifying a negative value in
50 timeout means an infinite timeout.
51
52 The bits that may be set/returned in events and revents are defined in
53 <poll.h>:
54
55 POLLIN There is data to read.
56
57 POLLPRI
58 There is urgent data to read (e.g., out-of-band data on
59 TCP socket; pseudo-terminal master in packet mode has
60 seen state change in slave).
61
62 POLLOUT
63 Writing now will not block.
64
65 POLLRDHUP (since Linux 2.6.17)
66 Stream socket peer closed connection, or shut down writ‐
67 ing half of connection. The _GNU_SOURCE feature test
68 macro must be defined in order to obtain this definition.
69
70 POLLERR
71 Error condition (output only).
72
73 POLLHUP
74 Hang up (output only).
75
76 POLLNVAL
77 Invalid request: fd not open (output only).
78
79 When compiling with _XOPEN_SOURCE defined, one also has the following,
80 which convey no further information beyond the bits listed above:
81
82 POLLRDNORM
83 Equivalent to POLLIN.
84
85 POLLRDBAND
86 Priority band data can be read (generally unused on
87 Linux).
88
89 POLLWRNORM
90 Equivalent to POLLOUT.
91
92 POLLWRBAND
93 Priority data may be written.
94
95 Linux also knows about, but does not use POLLMSG.
96
97 ppoll()
98 The relationship between poll() and ppoll() is analogous to the rela‐
99 tionship between select(2) and pselect(2): like pselect(2), ppoll()
100 allows an application to safely wait until either a file descriptor
101 becomes ready or until a signal is caught.
102
103 Other than the difference in the timeout argument, the following
104 ppoll() call:
105
106 ready = ppoll(&fds, nfds, timeout, &sigmask);
107
108 is equivalent to atomically executing the following calls:
109
110 sigset_t origmask;
111
112 sigprocmask(SIG_SETMASK, &sigmask, &origmask);
113 ready = poll(&fds, nfds, timeout);
114 sigprocmask(SIG_SETMASK, &origmask, NULL);
115
116 See the description of pselect(2) for an explanation of why ppoll() is
117 necessary.
118
119 If the sigmask argument is specified as NULL, then no signal mask
120 manipulation is performed (and thus ppoll() differs from poll() only in
121 the precision of the timeout argument).
122
123 The timeout argument specifies an upper limit on the amount of time
124 that ppoll() will block. This argument is a pointer to a structure of
125 the following form:
126
127 struct timespec {
128 long tv_sec; /* seconds */
129 long tv_nsec; /* nanoseconds */
130 };
131
132 If timeout is specified as NULL, then ppoll() can block indefinitely.
133
135 On success, a positive number is returned; this is the number of struc‐
136 tures which have non-zero revents fields (in other words, those
137 descriptors with events or errors reported). A value of 0 indicates
138 that the call timed out and no file descriptors were ready. On error,
139 -1 is returned, and errno is set appropriately.
140
142 EFAULT The array given as argument was not contained in the calling
143 program's address space.
144
145 EINTR A signal occurred before any requested event; see signal(7).
146
147 EINVAL The nfds value exceeds the RLIMIT_NOFILE value.
148
149 ENOMEM There was no space to allocate file descriptor tables.
150
152 The poll() system call was introduced in Linux 2.1.23. The poll()
153 library call was introduced in libc 5.4.28 (and provides emulation
154 using select(2) if your kernel does not have a poll() system call).
155
156 The ppoll() system call was added to Linux in kernel 2.6.16. The
157 ppoll() library call was added in glibc 2.4.
158
160 poll() conforms to POSIX.1-2001. ppoll() is Linux-specific.
161
163 Some implementations define the non-standard constant INFTIM with the
164 value -1 for use as a timeout. This constant is not provided in glibc.
165
166 Linux Notes
167 The Linux ppoll() system call modifies its timeout argument. However,
168 the glibc wrapper function hides this behavior by using a local vari‐
169 able for the timeout argument that is passed to the system call. Thus,
170 the glibc ppoll() function does not modify its timeout argument.
171
173 See the discussion of spurious readiness notifications under the BUGS
174 section of select(2).
175
177 select(2), select_tut(2), feature_test_macros(7), time(7)
178
180 This page is part of release 3.22 of the Linux man-pages project. A
181 description of the project, and information about reporting bugs, can
182 be found at http://www.kernel.org/doc/man-pages/.
183
184
185
186Linux 2009-06-02 POLL(2)