2 .\" Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH POLL 7D "April 9, 2016"
8 poll \- driver for fast poll on many file descriptors
12 \fB#include <sys/devpoll.h>
13 int fd = open("/dev/poll", O_RDWR);
14 ssize_t n = write(int fd, struct pollfd buf[], int bufsize);
15 int n = ioctl(int fd, DP_POLL, struct dvpoll* arg);
16 int n = ioctl(int fd, DP_ISPOLLED, struct pollfd* pfd);\fR
25 Open file descriptor that refers to the \fB/dev/poll\fR driver.
43 Array of \fBpollfd\fR structures.
52 Size of \fIbuf\fR in bytes.
61 Pointer to \fBpollcall\fR structure.
70 Pointer to \fBpollfd\fR structure.
75 The \fB/dev/poll\fR driver is a special driver that enables you to monitor
76 multiple sets of polled file descriptors. By using the \fB/dev/poll\fR
77 driver, you can efficiently poll large numbers of file descriptors. Access to
78 the \fB/dev/poll\fR driver is provided through \fBopen\fR(2), \fBwrite\fR(2),
79 and \fBioctl(2)\fR system calls.
82 Writing an array of \fBpollfd\fR struct to the \fB/dev/poll\fR driver has the
83 effect of adding these file descriptors to the monitored \fBpoll\fR file
84 descriptor set represented by the \fIfd\fR. To monitor multiple file
85 descriptor sets, open the \fB/dev/poll\fR driver multiple times. Each \fBfd\fR
86 corresponds to one set. For each \fBpollfd\fR struct entry (defined in
101 The \fBfd\fR field specifies the file descriptor being polled. The
102 \fBevents\fR field indicates the interested \fBpoll\fR \fBevents\fR on the file
103 descriptor. If a \fBpollfd\fR array contains multiple \fBpollfd\fR entries with
104 the same \fBfd\fR field, the "events" field in each \fBpollfd\fR entry is
105 OR'ed. A special \fBPOLLREMOVE\fR event in the \fBevents\fR field of the
106 \fBpollfd\fR structure removes the \fBfd\fR from the monitored set. The
107 \fBrevents\fR field is not used. Write returns the number of bytes written
108 successfully or \fB-1\fR when write fails.
111 The \fBDP_POLL\fR ioctl is used to retrieve returned \fBpoll\fR \fBevents\fR
112 occurred on the polled file descriptors in the monitored set represented by
113 \fIfd\fR. \fIarg\fR \fIis\fR \fIa\fR pointer to the devpoll structures which
114 are defined as follows:
119 struct pollfd* dp_fds;
128 The \fBdp_fds\fR points to a buffer that holds an array of returned
129 \fBpollfd\fR structures. The \fBdp_nfds\fR field specifies the size of the
130 buffer in terms of the number of \fBpollfd\fR entries it contains. The
131 \fBdp_nfds\fR field also indicates the maximum number of file descriptors from
132 which poll information can be obtained. If there is no interested \fBevents\fR
133 on any of the polled file descriptors, the \fBDP_POLL\fR ioctl call will wait
134 \fBdp_timeout\fR milliseconds before returning. If \fBdp_timeout\fR is
135 \fB0\fR, the ioctl call returns immediately. If \fBdp_timeout\fR is \fB-1\fR,
136 the call blocks until an interested \fBpoll\fR \fBevents\fR is available or the
137 call is interrupted. Upon return, if the ioctl call has failed, \fB-1\fR is
138 returned. The memory content pointed by \fBdp_fds\fR is not modified. A return
139 value \fB0\fR means the ioctl is timed out. In this case, the memory content
140 pointed by \fBdp_fds\fR is not modified. If the call is successful, it returns
141 the number of valid \fBpollfd\fR entries in the array pointed by \fBdp_fds\fR;
142 the contents of the rest of the buffer is undefined. For each valid
143 \fBpollfd\fR entry, the \fBfd\fR field indicates the file desciptor on which
144 the polled \fBevents\fR happened. The \fBevents\fR field is the user specified
145 \fBpoll\fR \fBevents\fR. The \fBrevents\fR field contains the \fBevents\fR
146 occurred. \fB-1\fR is returned if the call fails.
149 \fBDP_ISPOLLED\fR ioctl allows you to query if a file descriptor is already in
150 the monitored set represented by \fBfd\fR. The \fBfd\fR field of the
151 \fBpollfd\fR structure indicates the file descriptor of interest. The
152 \fBDP_ISPOLLED\fR ioctl returns \fB1\fR if the file descriptor is in the set.
153 The \fBevents\fR field contains \fB0\fR. The \fBrevents\fR field contains the
154 currently polled \fBevents\fR. The ioctl returns \fB0\fR if the file
155 descriptor is not in the set. The \fBpollfd\fR structure pointed by \fIpfd\fR
156 is not modified. The ioctl returns a \fB-1\fR if the call fails.
159 The following example shows how \fB/dev/poll\fR may be used.
168 if ((wfd = open("/dev/poll", O_RDWR)) < 0) {
171 pollfd = (struct pollfd* )malloc(sizeof(struct pollfd) * MAXBUF);
172 if (pollfd == NULL) {
179 for (i = 0; i < MAXBUF; i++) {
180 pollfd[i].fd = fds[i];
181 pollfd[i].events = POLLIN;
182 pollfd[i].revents = 0;
184 if (write(wfd, &pollfd[0], sizeof(struct pollfd) * MAXBUF) !=
185 sizeof(struct pollfd) * MAXBUF) {
186 perror("failed to write all pollfds");
192 * read from the devpoll driver
194 dopoll.dp_timeout = -1;
195 dopoll.dp_nfds = MAXBUF;
196 dopoll.dp_fds = pollfd;
197 result = ioctl(wfd, DP_POLL, &dopoll);
199 perror("/dev/poll ioctl DP_POLL failed");
204 for (i = 0; i < result; i++) {
205 read(dopoll.dp_fds[i].fd, rbuf, STRLEN);
214 The following example is part of a test program which shows how
215 \fBDP_ISPOLLED()\fR ioctl may be used.
223 while (loopcnt < ITERATION) {
226 if (write(fds[rn], TESTSTRING, strlen(TESTSTRING)) !=
227 strlen(TESTSTRING)) {
228 perror("write to fifo failed.");
237 result = ioctl(wfd, DP_ISPOLLED, &dpfd);
239 perror("/dev/poll ioctl DP_ISPOLLED failed");
240 printf("errno = %d\en", errno);
247 printf("DP_ISPOLLED returned incorrect result: %d.\en",
254 if (dpfd.fd != fds[rn]) {
255 printf("DP_ISPOLLED returned wrong fd %d, expect %d\en",
262 if (dpfd.revents != POLLIN) {
263 printf("DP_ISPOLLED returned unexpected revents %d\en",
270 if (read(dpfd.fd, rbuf, strlen(TESTSTRING)) !=
271 strlen(TESTSTRING)) {
272 perror("read from fifo failed");
290 A process does not have permission to access the content cached in
300 A signal was caught during the execution of the \fBioctl\fR(2) function.
309 The request argument requires a data transfer to or from a buffer pointed to by
310 \fIarg\fR, but \fIarg\fR points to an illegal address.
319 The request or \fIarg\fR parameter is not valid for this device, or field of
320 the dvpoll struct pointed by \fIarg\fR is not valid (for example, when using
321 write/pwrite dp_nfds is greater than {OPEN_MAX}, or when using the DPPOLL ioctl
322 dp_nfds is greater than or equal to {OPEN_MAX}}.
331 The \fBO_NONBLOCK\fR flag is set, the named file is a FIFO, the \fBO_WRONLY\fR
332 flag is set, and no process has the file open for reading; or the named file is
333 a character special or block special file and the device associated with this
334 special file does not exist.
339 See \fBattributes\fR(5) for a description of the following attributes:
347 ATTRIBUTE TYPE ATTRIBUTE VALUE
348 Architecture SPARC, x86
349 Interface Stability Obsolete
355 \fBopen\fR(2), \fBpoll\fR(2), \fBwrite\fR(2), \fBattributes\fR(5)
358 The \fB/dev/poll\fR API is particularly beneficial to applications that poll a
359 large number of file descriptors repeatedly. Applications will exhibit the
360 best performance gain if the polled file descriptor list rarely change.
363 When using the \fB/dev/poll\fR driver, you should remove a closed file
364 descriptor from a monitored poll set. Failure to do so may result in a
365 \fBPOLLNVAL\fR \fBrevents\fR being returned for the closed file descriptor.
366 When a file descriptor is closed but not removed from the monitored set, and is
367 reused in subsequent open of a different device, you will be polling the device
368 associated with the reused file descriptor. In a multithreaded application,
369 careful coordination among threads doing close and \fBDP_POLL\fR ioctl is
370 recommended for consistent results.
373 The \fB/dev/poll\fR driver caches a list of polled file descriptors, which are
374 specific to a process. Therefore, the \fB/dev/poll\fR file descriptor of a
375 process will be inherited by its child process, just like any other file
376 descriptors. But the child process will have very limited access through this
377 inherited \fB/dev/poll\fR file descriptor. Any attempt to write or do ioctl by
378 the child process will result in an \fBEACCES\fR error. The child process
379 should close the inherited \fB/dev/poll\fR file descriptor and open its own if
383 The \fB/dev/poll\fR driver does not yet support polling. Polling on a
384 \fB/dev/poll\fR file descriptor will result in \fBPOLLERR\fR being returned in
385 the \fBrevents\fR field of \fBpollfd\fR structure.