summaryrefslogtreecommitdiff
path: root/man3p/poll.3p
blob: 73995e5d373a4c54c4990c50ec2b72795da86c83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
.\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved 
.TH "POLL" P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\" poll 
.SH NAME
poll \- input/output multiplexing
.SH SYNOPSIS
.LP
\fB#include <poll.h>
.br
.sp
int poll(struct pollfd\fP \fIfds\fP\fB[], nfds_t\fP \fInfds\fP\fB,
int\fP \fItimeout\fP\fB); \fP
\fB
.br
\fP
.SH DESCRIPTION
.LP
The \fIpoll\fP() function provides applications with a mechanism for
multiplexing input/output over a set of file descriptors.
For each member of the array pointed to by \fIfds\fP, \fIpoll\fP()
shall examine the given file descriptor for the event(s)
specified in \fIevents\fP. The number of \fBpollfd\fP structures in
the \fIfds\fP array is specified by \fInfds\fP. The
\fIpoll\fP() function shall identify those file descriptors on which
an application can read or write data, or on which certain
events have occurred.
.LP
The \fIfds\fP argument specifies the file descriptors to be examined
and the events of interest for each file descriptor. It is
a pointer to an array with one member for each open file descriptor
of interest. The array's members are \fBpollfd\fP structures
within which \fIfd\fP specifies an open file descriptor and \fIevents\fP
and \fIrevents\fP are bitmasks constructed by OR'ing a
combination of the following event flags:
.TP 7
POLLIN
Data other than high-priority data may be read without blocking. 
.LP
For STREAMS, this flag is set in \fIrevents\fP even if the message
is of zero length. This flag shall be equivalent to POLLRDNORM
| POLLRDBAND. 
.TP 7
POLLRDNORM
Normal data may be read without blocking. 
.LP
For STREAMS, data on priority band 0 may be read without blocking.
This flag is set in \fIrevents\fP even if the message is of
zero length. 
.TP 7
POLLRDBAND
Priority data may be read without blocking. 
.LP
For STREAMS, data on priority bands greater than 0 may be read without
blocking. This flag is set in \fIrevents\fP even if the
message is of zero length. 
.TP 7
POLLPRI
High-priority data may be read without blocking. 
.LP
For STREAMS, this flag is set in \fIrevents\fP even if the message
is of zero length. 
.TP 7
POLLOUT
Normal data may be written without blocking. 
.LP
For STREAMS, data on priority band 0 may be written without blocking.
.TP 7
POLLWRNORM
Equivalent to POLLOUT.
.TP 7
POLLWRBAND
Priority data may be written. 
.LP
For STREAMS, data on priority bands greater than 0 may be written
without blocking. If any priority band has been written to on
this STREAM, this event only examines bands that have been written
to at least once. 
.TP 7
POLLERR
An error has occurred on the device or stream. This flag is only valid
in the \fIrevents\fP bitmask; it shall be ignored in
the \fIevents\fP member.
.TP 7
POLLHUP
The device has been disconnected. This event and POLLOUT are mutually-exclusive;
a stream can never be writable if a hangup has
occurred. However, this event and POLLIN, POLLRDNORM, POLLRDBAND,
or POLLPRI are not mutually-exclusive. This flag is only valid in
the \fIrevents\fP bitmask; it shall be ignored in the \fIevents\fP
member.
.TP 7
POLLNVAL
The specified \fIfd\fP value is invalid. This flag is only valid in
the \fIrevents\fP member; it shall ignored in the
\fIevents\fP member.
.sp
.LP
The significance and semantics of normal, priority, and high-priority
data are file and device-specific.
.LP
If the value of \fIfd\fP is less than 0, \fIevents\fP shall be ignored,
and \fIrevents\fP shall be set to 0 in that entry on
return from \fIpoll\fP().
.LP
In each \fBpollfd\fP structure, \fIpoll\fP() shall clear the \fIrevents\fP
member, except that where the application
requested a report on a condition by setting one of the bits of \fIevents\fP
listed above, \fIpoll\fP() shall set the
corresponding bit in \fIrevents\fP if the requested condition is true.
In addition, \fIpoll\fP() shall set the POLLHUP, POLLERR,
and POLLNVAL flag in \fIrevents\fP if the condition is true, even
if the application did not set the corresponding bit in
\fIevents\fP.
.LP
If none of the defined events have occurred on any selected file descriptor,
\fIpoll\fP() shall wait at least \fItimeout\fP
milliseconds for an event to occur on any of the selected file descriptors.
If the value of \fItimeout\fP is 0, \fIpoll\fP()
shall return immediately. If the value of \fItimeout\fP is -1, \fIpoll\fP()
shall block until a requested event occurs or until
the call is interrupted.
.LP
Implementations may place limitations on the granularity of timeout
intervals. If the requested timeout interval requires a
finer granularity than the implementation supports, the actual timeout
interval shall be rounded up to the next supported
value.
.LP
The \fIpoll\fP() function shall not be affected by the O_NONBLOCK
flag.
.LP
The \fIpoll\fP() function shall support regular files, terminal and
pseudo-terminal devices, FIFOs, pipes, sockets and  
\ STREAMS-based files.  The behavior of \fIpoll\fP() on
elements of \fIfds\fP that refer to other types of file is unspecified.
.LP
Regular files shall always poll TRUE for reading and writing.
.LP
A file descriptor for a socket that is listening for connections shall
indicate that it is ready for reading, once connections
are available. A file descriptor for a socket that is connecting asynchronously
shall indicate that it is ready for writing, once a
connection has been established.
.SH RETURN VALUE
.LP
Upon successful completion, \fIpoll\fP() shall return a non-negative
value. A positive value indicates the total number of file
descriptors that have been selected (that is, file descriptors for
which the \fIrevents\fP member is non-zero). A value of 0
indicates that the call timed out and no file descriptors have been
selected. Upon failure, \fIpoll\fP() shall return -1 and set
\fIerrno\fP to indicate the error.
.SH ERRORS
.LP
The \fIpoll\fP() function shall fail if:
.TP 7
.B EAGAIN
The allocation of internal data structures failed but a subsequent
request may succeed.
.TP 7
.B EINTR
A signal was caught during \fIpoll\fP().
.TP 7
.B EINVAL
The \fInfds\fP argument is greater than {OPEN_MAX},   \ or one of
the \fIfd\fP members refers to a STREAM or multiplexer
that is linked (directly or indirectly) downstream from a multiplexer.
.sp
.LP
\fIThe following sections are informative.\fP
.SH EXAMPLES
.SS Checking for Events on a Stream
.LP
The following example opens a pair of STREAMS devices and then waits
for either one to become writable. This example proceeds as
follows:
.IP " 1." 4
Sets the \fItimeout\fP parameter to 500 milliseconds.
.LP
.IP " 2." 4
Opens the STREAMS devices \fB/dev/dev0\fP and \fB/dev/dev1\fP, and
then polls them, specifying POLLOUT and POLLWRBAND as the
events of interest.
.LP
The STREAMS device names \fB/dev/dev0\fP and \fB/dev/dev1\fP are only
examples of how STREAMS devices can be named; STREAMS
naming conventions may vary among systems conforming to the IEEE\ Std\ 1003.1-2001.
.LP
.IP " 3." 4
Uses the \fIret\fP variable to determine whether an event has occurred
on either of the two STREAMS. The \fIpoll\fP() function
is given 500 milliseconds to wait for an event to occur (if it has
not occurred prior to the \fIpoll\fP() call).
.LP
.IP " 4." 4
Checks the returned value of \fIret\fP. If a positive value is returned,
one of the following can be done:
.RS
.IP " a." 4
Priority data can be written to the open STREAM on priority bands
greater than 0, because the POLLWRBAND event occurred on the
open STREAM ( \fIfds\fP[0] or \fIfds\fP[1]).
.LP
.IP " b." 4
Data can be written to the open STREAM on priority-band 0, because
the POLLOUT event occurred on the open STREAM ( \fIfds\fP[0]
or \fIfds\fP[1]).
.LP
.RE
.LP
.IP " 5." 4
If the returned value is not a positive value, permission to write
data to the open STREAM (on any priority band) is denied.
.LP
.IP " 6." 4
If the POLLHUP event occurs on the open STREAM ( \fIfds\fP[0] or \fIfds\fP[1]),
the device on the open STREAM has
disconnected.
.LP
.sp
.RS
.nf

\fB#include <stropts.h>
#include <poll.h>
\&...
struct pollfd fds[2];
int timeout_msecs = 500;
int ret;
    int i;
.sp

/* Open STREAMS device. */
fds[0].fd = open("/dev/dev0", ...);
fds[1].fd = open("/dev/dev1", ...);
    fds[0].events = POLLOUT | POLLWRBAND;
    fds[1].events = POLLOUT | POLLWRBAND;
.sp

ret = poll(fds, 2, timeout_msecs);
.sp

if (ret > 0) {
    /* An event on one of the fds has occurred. */
    for (i=0; i<2; i++) {
        if (fds[i].revents & POLLWRBAND) {
        /* Priority data may be written on device number i. */
\&...
        }
        if (fds[i].revents & POLLOUT) {
        /* Data may be written on device number i. */
\&...
        }
        if (fds[i].revents & POLLHUP) {
        /* A hangup has occurred on device number i. */
\&...
        }
    }
}
\fP
.fi
.RE
.SH APPLICATION USAGE
.LP
None.
.SH RATIONALE
.LP
None.
.SH FUTURE DIRECTIONS
.LP
None.
.SH SEE ALSO
.LP
\fISTREAMS\fP , \fIgetmsg\fP() , \fIputmsg\fP() , \fIread\fP() , \fIselect\fP()
, \fIwrite\fP() , the Base Definitions volume of IEEE\ Std\ 1003.1-2001,
\fI<poll.h>\fP, \fI<stropts.h>\fP
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group. In the
event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .