1.\" $OpenBSD: poll.2,v 1.23 2013/11/02 17:25:34 espie Exp $ 2.\" 3.\" Copyright (c) 1994 Jason R. Thorpe 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by Jason R. Thorpe. 17.\" 4. The name of the author may not be used to endorse or promote products 18.\" derived from this software without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 25.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 27.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" 31.Dd $Mdocdate: November 2 2013 $ 32.Dt POLL 2 33.Os 34.Sh NAME 35.Nm poll , 36.Nm ppoll 37.Nd synchronous I/O multiplexing 38.Sh SYNOPSIS 39.Fd #include <poll.h> 40.Ft int 41.Fn poll "struct pollfd *fds" "nfds_t nfds" "int timeout" 42.Ft int 43.Fn ppoll "struct pollfd *fds" "nfds_t nfds" "const struct timespec *timeout" "const sigset_t *mask" 44.Sh DESCRIPTION 45.Fn poll 46provides a mechanism for multiplexing I/O across a set of file 47descriptors. 48It is similar in function to 49.Xr select 2 . 50Unlike 51.Xr select 2 , 52however, it is possible to only pass in data corresponding to the 53file descriptors for which events are wanted. 54This makes 55.Fn poll 56more efficient than 57.Xr select 2 58in most cases. 59.Pp 60The arguments are as follows: 61.Bl -tag -width timeout 62.It Fa fds 63Points to an array of 64.Fa pollfd 65structures, which are defined as: 66.Bd -literal -offset indent 67struct pollfd { 68 int fd; 69 short events; 70 short revents; 71}; 72.Ed 73.Pp 74The 75.Fa fd 76member is an open file descriptor. 77If 78.Fa fd 79is -1, 80the 81.Fa pollfd 82structure is considered unused, and 83.Fa revents 84will be cleared. 85.Pp 86The 87.Fa events 88and 89.Fa revents 90members are bitmasks of conditions to monitor and conditions found, 91respectively. 92.It Fa nfds 93An unsigned integer specifying the number of 94.Fa pollfd 95structures in the array. 96.It Fa timeout 97Maximum interval to wait for the poll to complete, in milliseconds. 98If this value is 0, 99.Fn poll 100will return immediately. 101If this value is INFTIM (-1), 102.Fn poll 103will block indefinitely until a condition is found. 104.El 105.Pp 106The calling process sets the 107.Fa events 108bitmask and 109.Fn poll 110sets the 111.Fa revents 112bitmask. 113Each call to 114.Fn poll 115resets the 116.Fa revents 117bitmask for accuracy. 118The condition flags in the bitmasks are defined as: 119.Bl -tag -width POLLRDNORM 120.It Dv POLLIN 121Data other than high-priority data may be read without blocking. 122.It Dv POLLRDNORM 123Normal data may be read without blocking. 124.It Dv POLLRDBAND 125Priority data may be read without blocking. 126.It Dv POLLNORM 127Same as 128.Dv POLLRDNORM . 129This flag is provided for source code compatibility with older 130programs and should not be used in new code. 131.It Dv POLLPRI 132High-priority data may be read without blocking. 133.It Dv POLLOUT 134Normal data may be written without blocking. 135.It Dv POLLWRNORM 136Same as 137.Dv POLLOUT . 138.It Dv POLLWRBAND 139Priority data may be written. 140.It Dv POLLERR 141An error has occurred on the device or socket. 142This flag is only valid in the 143.Fa revents 144bitmask; it is ignored in the 145.Fa events 146member. 147.It Dv POLLHUP 148The device or socket has been disconnected. 149This event and 150.Dv POLLOUT 151are mutually-exclusive; a descriptor can never be writable if a hangup has 152occurred. 153However, this event and 154.Dv POLLIN , 155.Dv POLLRDNORM , 156.Dv POLLRDBAND , 157or 158.Dv POLLPRI 159are not mutually-exclusive. 160This flag is only valid in the 161.Fa revents 162bitmask; it is ignored in the 163.Fa events 164member. 165.It Dv POLLNVAL 166The corresponding file descriptor is invalid. 167This flag is only valid in the 168.Fa revents 169bitmask; it is ignored in the 170.Fa events 171member. 172.El 173.Pp 174The significance and semantics of normal, priority, and high-priority 175data are device-specific. 176.Pp 177In addition to I/O multiplexing, 178.Fn poll 179can be used to generate simple timeouts. 180This functionality may be achieved by passing a null pointer for 181.Fa fds . 182.Pp 183The 184.Fn ppoll 185function is similar to 186.Fn poll 187except that it specifies the timeout using a timespec structure, 188and a null pointer is used to specify an indefinite timeout 189instead of 190.Dv INFTIM . 191Also, if 192.Fa mask 193is a non-null pointer, 194.Fn ppoll 195atomically sets the calling thread's signal mask to the signal set 196pointed to by 197.Fa mask 198for the duration of the function call. 199In this case, the original signal mask will be restored before 200.Fn ppoll 201returns. 202.Sh RETURN VALUES 203Upon error, 204.Fn poll 205and 206.Fn ppoll 207return \-1 and set the global variable 208.Va errno 209to indicate the error. 210If the timeout interval was reached before any events occurred, 211they return 0. 212Otherwise, they return the number of 213.Fa pollfd 214structures for which 215.Fa revents 216is non-zero. 217.Sh EXAMPLES 218The following example implements a read from the standard input that times 219out after 60 seconds: 220.Bd -literal -offset indent 221#include <err.h> 222#include <poll.h> 223#include <stdio.h> 224#include <unistd.h> 225 226struct pollfd pfd[1]; 227char buf[BUFSIZ]; 228int nfds; 229 230pfd[0].fd = STDIN_FILENO; 231pfd[0].events = POLLIN; 232nfds = poll(pfd, 1, 60 * 1000); 233if (nfds == -1 || (pfd[0].revents & (POLLERR|POLLHUP|POLLNVAL))) 234 errx(1, "poll error"); 235if (nfds == 0) 236 errx(1, "time out"); 237if (read(STDIN_FILENO, buf, sizeof(buf)) == -1) 238 errx(1, "read"); 239.Ed 240.Sh ERRORS 241.Fn poll 242and 243.Fn ppoll 244will fail if: 245.Bl -tag -width Er 246.It Bq Er EFAULT 247.Fa fds 248points outside the process's allocated address space. 249.It Bq Er EINTR 250A signal was caught before any polled events occurred 251and before the timeout elapsed. 252.It Bq Er EINVAL 253.Fa nfds 254was greater than the number of available 255file descriptors. 256.It Bq Er EINVAL 257The timeout passed was invalid. 258.El 259.Sh SEE ALSO 260.Xr clock_gettime 2 , 261.Xr getrlimit 2 , 262.Xr read 2 , 263.Xr select 2 , 264.Xr write 2 265.Sh STANDARDS 266The 267.Fn poll 268function is compliant with the 269.St -xpg4.3 270specification. 271The 272.Fn ppoll 273function is a Linux extension. 274.Sh HISTORY 275A 276.Fn poll 277system call appeared in 278.At V.3 . 279The 280.Fn ppoll 281function appeared in 282.Ox 5.4 . 283.Sh BUGS 284The 285.Dv POLLERR 286and 287.Dv POLLWRBAND 288flags are accepted but ignored by the kernel. 289.Pp 290Because 291.Ox 292does not implement 293.Tn STREAMS , 294there is no distinction between some of the fields in the 295.Fa events 296and 297.Fa revents 298bitmasks. 299As a result, the 300.Dv POLLIN , 301.Dv POLLNORM , 302and 303.Dv POLLRDNORM 304flags are equivalent. 305.Pp 306Internally to the kernel, 307.Fn poll 308and 309.Fn ppoll 310work poorly if multiple processes wait on the same file descriptor. 311