18269e767SBrooks Davis.\" $NetBSD: poll.2,v 1.3 1996/09/07 21:53:08 mycroft Exp $ 28269e767SBrooks Davis.\" 38269e767SBrooks Davis.\" Copyright (c) 1996 Charles M. Hannum. All rights reserved. 48269e767SBrooks Davis.\" 58269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 68269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 78269e767SBrooks Davis.\" are met: 88269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 98269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 108269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 118269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 128269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 138269e767SBrooks Davis.\" 3. All advertising materials mentioning features or use of this software 148269e767SBrooks Davis.\" must display the following acknowledgement: 158269e767SBrooks Davis.\" This product includes software developed by Charles M. Hannum. 168269e767SBrooks Davis.\" 4. The name of the author may not be used to endorse or promote products 178269e767SBrooks Davis.\" derived from this software without specific prior written permission. 188269e767SBrooks Davis.\" 198269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 208269e767SBrooks Davis.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 218269e767SBrooks Davis.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 228269e767SBrooks Davis.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 238269e767SBrooks Davis.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 248269e767SBrooks Davis.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 258269e767SBrooks Davis.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 268269e767SBrooks Davis.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 278269e767SBrooks Davis.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 288269e767SBrooks Davis.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 298269e767SBrooks Davis.\" 30*74ecdf86SKyle Evans.Dd December 11, 2024 318269e767SBrooks Davis.Dt POLL 2 328269e767SBrooks Davis.Os 338269e767SBrooks Davis.Sh NAME 348269e767SBrooks Davis.Nm poll 358269e767SBrooks Davis.Nd synchronous I/O multiplexing 368269e767SBrooks Davis.Sh LIBRARY 378269e767SBrooks Davis.Lb libc 388269e767SBrooks Davis.Sh SYNOPSIS 398269e767SBrooks Davis.In poll.h 408269e767SBrooks Davis.Ft int 418269e767SBrooks Davis.Fn poll "struct pollfd fds[]" "nfds_t nfds" "int timeout" 428269e767SBrooks Davis.Ft int 438269e767SBrooks Davis.Fo ppoll 448269e767SBrooks Davis.Fa "struct pollfd fds[]" 458269e767SBrooks Davis.Fa "nfds_t nfds" 468269e767SBrooks Davis.Fa "const struct timespec * restrict timeout" 478269e767SBrooks Davis.Fa "const sigset_t * restrict newsigmask" 488269e767SBrooks Davis.Fc 498269e767SBrooks Davis.Sh DESCRIPTION 508269e767SBrooks DavisThe 518269e767SBrooks Davis.Fn poll 528269e767SBrooks Davissystem call 538269e767SBrooks Davisexamines a set of file descriptors to see if some of them are ready for 548269e767SBrooks DavisI/O. 558269e767SBrooks DavisThe 568269e767SBrooks Davis.Fa fds 578269e767SBrooks Davisargument is a pointer to an array of pollfd structures as defined in 588269e767SBrooks Davis.In poll.h 598269e767SBrooks Davis(shown below). 608269e767SBrooks DavisThe 618269e767SBrooks Davis.Fa nfds 628269e767SBrooks Davisargument determines the size of the 638269e767SBrooks Davis.Fa fds 648269e767SBrooks Davisarray. 658269e767SBrooks Davis.Bd -literal 668269e767SBrooks Davisstruct pollfd { 678269e767SBrooks Davis int fd; /* file descriptor */ 688269e767SBrooks Davis short events; /* events to look for */ 698269e767SBrooks Davis short revents; /* events returned */ 708269e767SBrooks Davis}; 718269e767SBrooks Davis.Ed 728269e767SBrooks Davis.Pp 738269e767SBrooks DavisThe fields of 748269e767SBrooks Davis.Fa struct pollfd 758269e767SBrooks Davisare as follows: 768269e767SBrooks Davis.Bl -tag -width XXXrevents 778269e767SBrooks Davis.It fd 788269e767SBrooks DavisFile descriptor to poll. 798269e767SBrooks DavisIf fd is equal to -1 then 808269e767SBrooks Davis.Fa revents 818269e767SBrooks Davisis cleared (set to zero), and that pollfd is not checked. 828269e767SBrooks Davis.It events 838269e767SBrooks DavisEvents to poll for. 848269e767SBrooks Davis(See below.) 858269e767SBrooks Davis.It revents 868269e767SBrooks DavisEvents which may occur. 878269e767SBrooks Davis(See below.) 888269e767SBrooks Davis.El 898269e767SBrooks Davis.Pp 908269e767SBrooks DavisThe event bitmasks in 918269e767SBrooks Davis.Fa events 928269e767SBrooks Davisand 938269e767SBrooks Davis.Fa revents 948269e767SBrooks Davishave the following bits: 958269e767SBrooks Davis.Bl -tag -width XXXPOLLWRNORM 968269e767SBrooks Davis.It POLLIN 978269e767SBrooks DavisData other than high priority data may be read without blocking. 988269e767SBrooks Davis.It POLLRDNORM 998269e767SBrooks DavisNormal data may be read without blocking. 1008269e767SBrooks Davis.It POLLRDBAND 1018269e767SBrooks DavisData with a non-zero priority may be read without blocking. 1028269e767SBrooks Davis.It POLLPRI 1038269e767SBrooks DavisHigh priority data may be read without blocking. 1048269e767SBrooks Davis.It POLLOUT 1058269e767SBrooks Davis.It POLLWRNORM 1068269e767SBrooks DavisNormal data may be written without blocking. 1078269e767SBrooks Davis.It POLLWRBAND 1088269e767SBrooks DavisData with a non-zero priority may be written without blocking. 1098269e767SBrooks Davis.It POLLERR 1108269e767SBrooks DavisAn exceptional condition has occurred on the device or socket. 1118269e767SBrooks DavisThis 1128269e767SBrooks Davisflag is always checked, even if not present in the 1138269e767SBrooks Davis.Fa events 1148269e767SBrooks Davisbitmask. 1158269e767SBrooks Davis.It POLLHUP 1168269e767SBrooks DavisThe device or socket has been disconnected. 1178269e767SBrooks DavisThis flag is always 1188269e767SBrooks Davischecked, even if not present in the 1198269e767SBrooks Davis.Fa events 1208269e767SBrooks Davisbitmask. 1218269e767SBrooks DavisNote that 1228269e767SBrooks DavisPOLLHUP 1238269e767SBrooks Davisand 1248269e767SBrooks DavisPOLLOUT 1258269e767SBrooks Davisshould never be present in the 1268269e767SBrooks Davis.Fa revents 1278269e767SBrooks Davisbitmask at the same time. 1288269e767SBrooks Davis.It POLLRDHUP 1298269e767SBrooks DavisRemote peer closed connection, or shut down writing. 1308269e767SBrooks DavisUnlike 1318269e767SBrooks DavisPOLLHUP, 1328269e767SBrooks DavisPOLLRDHUP 1338269e767SBrooks Davismust be present in the 1348269e767SBrooks Davis.Fa events 1358269e767SBrooks Davisbitmask to be reported. 1368269e767SBrooks DavisApplies only to stream sockets. 1378269e767SBrooks Davis.It POLLNVAL 1388269e767SBrooks DavisThe file descriptor is not open, 1398269e767SBrooks Davisor in capability mode the file descriptor has insufficient rights. 1408269e767SBrooks DavisThis flag is always checked, even 1418269e767SBrooks Davisif not present in the 1428269e767SBrooks Davis.Fa events 1438269e767SBrooks Davisbitmask. 1448269e767SBrooks Davis.El 1458269e767SBrooks Davis.Pp 1468269e767SBrooks DavisIf 1478269e767SBrooks Davis.Fa timeout 1488269e767SBrooks Davisis neither zero nor INFTIM (-1), it specifies a maximum interval to 1498269e767SBrooks Daviswait for any file descriptor to become ready, in milliseconds. 1508269e767SBrooks DavisIf 1518269e767SBrooks Davis.Fa timeout 1528269e767SBrooks Davisis INFTIM (-1), the poll blocks indefinitely. 1538269e767SBrooks DavisIf 1548269e767SBrooks Davis.Fa timeout 1558269e767SBrooks Davisis zero, then 1568269e767SBrooks Davis.Fn poll 1578269e767SBrooks Daviswill return without blocking. 1588269e767SBrooks Davis.Pp 1598269e767SBrooks DavisThe 1608269e767SBrooks Davis.Fn ppoll 1618269e767SBrooks Davissystem call, unlike 1628269e767SBrooks Davis.Fn poll , 1638269e767SBrooks Davisis used to safely wait until either a set of file descriptors becomes 1648269e767SBrooks Davisready or until a signal is caught. 1658269e767SBrooks DavisThe 1668269e767SBrooks Davis.Fa fds 1678269e767SBrooks Davisand 1688269e767SBrooks Davis.Fa nfds 1698269e767SBrooks Davisarguments are identical to the analogous arguments of 1708269e767SBrooks Davis.Fn poll . 1718269e767SBrooks DavisThe 1728269e767SBrooks Davis.Fa timeout 1738269e767SBrooks Davisargument in 1748269e767SBrooks Davis.Fn ppoll 1758269e767SBrooks Davispoints to a 1768269e767SBrooks Davis.Vt "const struct timespec" 1778269e767SBrooks Daviswhich is defined in 1788269e767SBrooks Davis.In sys/timespec.h 1798269e767SBrooks Davis(shown below) rather than the 1808269e767SBrooks Davis.Vt "int timeout" 1818269e767SBrooks Davisused by 1828269e767SBrooks Davis.Fn poll . 1838269e767SBrooks DavisA null pointer may be passed to indicate that 1848269e767SBrooks Davis.Fn ppoll 1858269e767SBrooks Davisshould wait indefinitely. 1868269e767SBrooks DavisFinally, 1878269e767SBrooks Davis.Fa newsigmask 1888269e767SBrooks Davisspecifies a signal mask which is set while waiting for input. 1898269e767SBrooks DavisWhen 1908269e767SBrooks Davis.Fn ppoll 1918269e767SBrooks Davisreturns, the original signal mask is restored. 1928269e767SBrooks Davis.Bd -literal 1938269e767SBrooks Davisstruct timespec { 1948269e767SBrooks Davis time_t tv_sec; /* seconds */ 1958269e767SBrooks Davis long tv_nsec; /* and nanoseconds */ 1968269e767SBrooks Davis}; 1978269e767SBrooks Davis.Ed 1988269e767SBrooks Davis.Sh RETURN VALUES 1998269e767SBrooks DavisThe 2008269e767SBrooks Davis.Fn poll 2018269e767SBrooks Davissystem call 2028269e767SBrooks Davisreturns the number of descriptors that are ready for I/O, or -1 if an 2038269e767SBrooks Daviserror occurred. 2048269e767SBrooks DavisIf the time limit expires, 2058269e767SBrooks Davis.Fn poll 2068269e767SBrooks Davisreturns 0. 2078269e767SBrooks DavisIf 2088269e767SBrooks Davis.Fn poll 2098269e767SBrooks Davisreturns with an error, 2108269e767SBrooks Davisincluding one due to an interrupted system call, 2118269e767SBrooks Davisthe 2128269e767SBrooks Davis.Fa fds 2138269e767SBrooks Davisarray will be unmodified. 2148269e767SBrooks Davis.Sh COMPATIBILITY 2158269e767SBrooks DavisThis implementation differs from the historical one in that a given 2168269e767SBrooks Davisfile descriptor may not cause 2178269e767SBrooks Davis.Fn poll 2188269e767SBrooks Davisto return with an error. 2198269e767SBrooks DavisIn cases where this would have happened in 2208269e767SBrooks Davisthe historical implementation (e.g.\& trying to poll a 2218269e767SBrooks Davis.Xr revoke 2 Ns ed 2228269e767SBrooks Davisdescriptor), this implementation instead copies the 2238269e767SBrooks Davis.Fa events 2248269e767SBrooks Davisbitmask to the 2258269e767SBrooks Davis.Fa revents 2268269e767SBrooks Davisbitmask. 2278269e767SBrooks DavisAttempting to perform I/O on this descriptor will then 2288269e767SBrooks Davisreturn an error. 2298269e767SBrooks DavisThis behaviour is believed to be more useful. 2308269e767SBrooks Davis.Sh ERRORS 2318269e767SBrooks DavisAn error return from 2328269e767SBrooks Davis.Fn poll 2338269e767SBrooks Davisindicates: 2348269e767SBrooks Davis.Bl -tag -width Er 2358269e767SBrooks Davis.It Bq Er EFAULT 2368269e767SBrooks DavisThe 2378269e767SBrooks Davis.Fa fds 2388269e767SBrooks Davisargument 2398269e767SBrooks Davispoints outside the process's allocated address space. 2408269e767SBrooks Davis.It Bq Er EINTR 2418269e767SBrooks DavisA signal was delivered before the time limit expired and 2428269e767SBrooks Davisbefore any of the selected events occurred. 2438269e767SBrooks Davis.It Bq Er EINVAL 2448269e767SBrooks DavisThe specified time limit is invalid. 2458269e767SBrooks DavisOne of its components is negative or too large. 2468269e767SBrooks Davis.It Bq Er EINVAL 2478269e767SBrooks DavisThe number of pollfd structures specified by 2488269e767SBrooks Davis.Fa nfds 2498269e767SBrooks Davisexceeds the system tunable 2508269e767SBrooks Davis.Va kern.maxfilesperproc 2518269e767SBrooks Davisand 2528269e767SBrooks Davis.Dv FD_SETSIZE . 2538269e767SBrooks Davis.El 2548269e767SBrooks Davis.Sh SEE ALSO 2558269e767SBrooks Davis.Xr accept 2 , 2568269e767SBrooks Davis.Xr connect 2 , 2578269e767SBrooks Davis.Xr kqueue 2 , 2588269e767SBrooks Davis.Xr pselect 2 , 2598269e767SBrooks Davis.Xr read 2 , 2608269e767SBrooks Davis.Xr recv 2 , 2618269e767SBrooks Davis.Xr select 2 , 2628269e767SBrooks Davis.Xr send 2 , 2638269e767SBrooks Davis.Xr write 2 2648269e767SBrooks Davis.Sh STANDARDS 2658269e767SBrooks DavisThe 2668269e767SBrooks Davis.Fn poll 2678269e767SBrooks Davisfunction conforms to 2688269e767SBrooks Davis.St -p1003.1-2001 . 2698269e767SBrooks DavisThe 2708269e767SBrooks Davis.Fn ppoll 271*74ecdf86SKyle Evansfunction conforms to 272*74ecdf86SKyle Evans.St -p1003.1-2024 . 2738269e767SBrooks DavisThe 2748269e767SBrooks DavisPOLLRDHUP 2758269e767SBrooks Davisflag is not specified by POSIX, but is compatible with Linux and illumos. 2768269e767SBrooks Davis.Sh HISTORY 2778269e767SBrooks DavisThe 2788269e767SBrooks Davis.Fn poll 2798269e767SBrooks Davisfunction appeared in 2808269e767SBrooks Davis.At V . 2818269e767SBrooks DavisThis manual page and the core of the implementation was taken from 2828269e767SBrooks Davis.Nx . 2838269e767SBrooks DavisThe 2848269e767SBrooks Davis.Fn ppoll 2858269e767SBrooks Davisfunction first appeared in 2865bcc33d1SDavid E. O'Brien.Fx 10.2 2878269e767SBrooks Davis.Sh BUGS 2888269e767SBrooks DavisThe distinction between some of the fields in the 2898269e767SBrooks Davis.Fa events 2908269e767SBrooks Davisand 2918269e767SBrooks Davis.Fa revents 2928269e767SBrooks Davisbitmasks is really not useful without STREAMS. 2938269e767SBrooks DavisThe fields are 2948269e767SBrooks Davisdefined for compatibility with existing software. 295