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 bitmask specifying the events
35 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() and pselect(): like pselect(), ppoll() allows
100 an application to safely wait until either a file descriptor becomes
101 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 The timeout argument specifies an upper limit on the amount of time
120 that ppoll() will block. This argument is a pointer to a structure of
121 the following form:
122
123 struct timespec {
124 long tv_sec; /* seconds */
125 long tv_nsec; /* nanoseconds */
126 };
127
128 If timeout is specified as NULL, then ppoll() can block indefinitely.
129
131 On success, a positive number is returned; this is the number of struc‐
132 tures which have non-zero revents fields (in other words, those
133 descriptors with events or errors reported). A value of 0 indicates
134 that the call timed out and no file descriptors were ready. On error,
135 -1 is returned, and errno is set appropriately.
136
138 EBADF An invalid file descriptor was given in one of the sets.
139
140 EFAULT The array given as argument was not contained in the calling
141 program's address space.
142
143 EINTR A signal occurred before any requested event.
144
145 EINVAL The nfds value exceeds the RLIMIT_NOFILE value.
146
147 ENOMEM There was no space to allocate file descriptor tables.
148
150 The Linux ppoll() system call modifies its timeout argument. However,
151 the glibc wrapper function hides this behaviour by using a local vari‐
152 able for the timeout argument that is passed to the system call. Thus,
153 the glibc ppoll() function does not modify its timeout argument.
154
156 See the discussion of spurious readiness notifications under the BUGS
157 section of select(2).
158
160 poll() conforms to POSIX.1-2001. ppoll() is Linux specific.
161
163 The poll() system call was introduced in Linux 2.1.23. The poll()
164 library call was introduced in libc 5.4.28 (and provides emulation
165 using select() if your kernel does not have a poll() system call).
166
167 The ppoll() system call was added to Linux in kernel 2.6.16. The
168 ppoll() library call was added in glibc 2.4.
169
171 Some implementations define the non-standard constant INFTIM with the
172 value -1 for use as a timeout. This constant is not provided in glibc.
173
175 select(2), select_tut(2), feature_test_macros(7)
176
177
178
179Linux 2.6.16 2006-03-13 POLL(2)