1*2fe8fb19SBen Gras.\" $NetBSD: poll.2,v 1.28 2010/03/22 19:30:55 joerg Exp $ 2*2fe8fb19SBen Gras.\" 3*2fe8fb19SBen Gras.\" Copyright (c) 1998, 2005 The NetBSD Foundation, Inc. 4*2fe8fb19SBen Gras.\" All rights reserved. 5*2fe8fb19SBen Gras.\" 6*2fe8fb19SBen Gras.\" This code is derived from software contributed to The NetBSD Foundation 7*2fe8fb19SBen Gras.\" by Charles M. Hannum. 8*2fe8fb19SBen Gras.\" 9*2fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 10*2fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 11*2fe8fb19SBen Gras.\" are met: 12*2fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 13*2fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 14*2fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 15*2fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 16*2fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 17*2fe8fb19SBen Gras.\" 18*2fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19*2fe8fb19SBen Gras.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20*2fe8fb19SBen Gras.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21*2fe8fb19SBen Gras.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22*2fe8fb19SBen Gras.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*2fe8fb19SBen Gras.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*2fe8fb19SBen Gras.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*2fe8fb19SBen Gras.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*2fe8fb19SBen Gras.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*2fe8fb19SBen Gras.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*2fe8fb19SBen Gras.\" POSSIBILITY OF SUCH DAMAGE. 29*2fe8fb19SBen Gras.\" 30*2fe8fb19SBen Gras.Dd September 8, 2006 31*2fe8fb19SBen Gras.Dt POLL 2 32*2fe8fb19SBen Gras.Os 33*2fe8fb19SBen Gras.Sh NAME 34*2fe8fb19SBen Gras.Nm poll, pollts 35*2fe8fb19SBen Gras.Nd synchronous I/O multiplexing 36*2fe8fb19SBen Gras.Sh LIBRARY 37*2fe8fb19SBen Gras.Lb libc 38*2fe8fb19SBen Gras.Sh SYNOPSIS 39*2fe8fb19SBen Gras.In poll.h 40*2fe8fb19SBen Gras.Ft int 41*2fe8fb19SBen Gras.Fn poll "struct pollfd *fds" "nfds_t nfds" "int timeout" 42*2fe8fb19SBen Gras.In poll.h 43*2fe8fb19SBen Gras.In signal.h 44*2fe8fb19SBen Gras.In time.h 45*2fe8fb19SBen Gras.Ft int 46*2fe8fb19SBen Gras.Fn pollts "struct pollfd * restrict fds" "nfds_t nfds" "const struct timespec * restrict ts" "const sigset_t * restrict sigmask" 47*2fe8fb19SBen Gras.Sh DESCRIPTION 48*2fe8fb19SBen Gras.Fn poll 49*2fe8fb19SBen Grasand 50*2fe8fb19SBen Gras.Fn pollts 51*2fe8fb19SBen Grasexamine a set of file descriptors to see if some of them are ready for 52*2fe8fb19SBen GrasI/O. 53*2fe8fb19SBen GrasThe 54*2fe8fb19SBen Gras.Fa fds 55*2fe8fb19SBen Grasargument is a pointer to an array of pollfd structures as defined in 56*2fe8fb19SBen Gras.In poll.h 57*2fe8fb19SBen Gras(shown below). 58*2fe8fb19SBen GrasThe 59*2fe8fb19SBen Gras.Fa nfds 60*2fe8fb19SBen Grasargument determines the size of the 61*2fe8fb19SBen Gras.Fa fds 62*2fe8fb19SBen Grasarray. 63*2fe8fb19SBen Gras.Bd -literal 64*2fe8fb19SBen Grasstruct pollfd { 65*2fe8fb19SBen Gras int fd; /* file descriptor */ 66*2fe8fb19SBen Gras short events; /* events to look for */ 67*2fe8fb19SBen Gras short revents; /* events returned */ 68*2fe8fb19SBen Gras}; 69*2fe8fb19SBen Gras.Ed 70*2fe8fb19SBen Gras.Pp 71*2fe8fb19SBen GrasThe fields of 72*2fe8fb19SBen Gras.Fa struct pollfd 73*2fe8fb19SBen Grasare as follows: 74*2fe8fb19SBen Gras.Bl -tag -width XXXrevents 75*2fe8fb19SBen Gras.It fd 76*2fe8fb19SBen GrasFile descriptor to poll. 77*2fe8fb19SBen GrasIf the value in 78*2fe8fb19SBen Gras.Em fd 79*2fe8fb19SBen Grasis negative, the file descriptor is ignored and 80*2fe8fb19SBen Gras.Em revents 81*2fe8fb19SBen Grasis set to 0. 82*2fe8fb19SBen Gras.It events 83*2fe8fb19SBen GrasEvents to poll for. 84*2fe8fb19SBen Gras(See below.) 85*2fe8fb19SBen Gras.It revents 86*2fe8fb19SBen GrasEvents which may occur. 87*2fe8fb19SBen Gras(See below.) 88*2fe8fb19SBen Gras.El 89*2fe8fb19SBen Gras.Pp 90*2fe8fb19SBen GrasThe event bitmasks in 91*2fe8fb19SBen Gras.Fa events 92*2fe8fb19SBen Grasand 93*2fe8fb19SBen Gras.Fa revents 94*2fe8fb19SBen Grashave the following bits: 95*2fe8fb19SBen Gras.Bl -tag -width XXXPOLLWRNORM 96*2fe8fb19SBen Gras.It POLLIN 97*2fe8fb19SBen GrasData other than high priority data may be read without blocking. 98*2fe8fb19SBen Gras.It POLLRDNORM 99*2fe8fb19SBen GrasNormal data may be read without blocking. 100*2fe8fb19SBen Gras.It POLLRDBAND 101*2fe8fb19SBen GrasData with a non-zero priority may be read without blocking. 102*2fe8fb19SBen Gras.It POLLPRI 103*2fe8fb19SBen GrasHigh priority data may be read without blocking. 104*2fe8fb19SBen Gras.It POLLOUT 105*2fe8fb19SBen GrasNormal data may be written without blocking. 106*2fe8fb19SBen Gras.It POLLWRNORM 107*2fe8fb19SBen GrasEquivalent to 108*2fe8fb19SBen GrasPOLLOUT. 109*2fe8fb19SBen Gras.It POLLWRBAND 110*2fe8fb19SBen GrasData with a non-zero priority may be written without blocking. 111*2fe8fb19SBen Gras.It POLLERR 112*2fe8fb19SBen GrasAn exceptional condition has occurred on the device or socket. 113*2fe8fb19SBen GrasThis flag is always checked, even if not present in the 114*2fe8fb19SBen Gras.Fa events 115*2fe8fb19SBen Grasbitmask. 116*2fe8fb19SBen Gras.It POLLHUP 117*2fe8fb19SBen GrasThe device or socket has been disconnected. 118*2fe8fb19SBen GrasThis flag is always 119*2fe8fb19SBen Graschecked, even if not present in the 120*2fe8fb19SBen Gras.Fa events 121*2fe8fb19SBen Grasbitmask. 122*2fe8fb19SBen GrasNote that 123*2fe8fb19SBen GrasPOLLHUP 124*2fe8fb19SBen Grasand 125*2fe8fb19SBen GrasPOLLOUT 126*2fe8fb19SBen Grasshould never be present in the 127*2fe8fb19SBen Gras.Fa revents 128*2fe8fb19SBen Grasbitmask at the same time. 129*2fe8fb19SBen GrasIf the remote end of a socket is closed, 130*2fe8fb19SBen Gras.Fn poll 131*2fe8fb19SBen Grasreturns a 132*2fe8fb19SBen GrasPOLLIN 133*2fe8fb19SBen Grasevent, rather than a 134*2fe8fb19SBen GrasPOLLHUP. 135*2fe8fb19SBen Gras.It POLLNVAL 136*2fe8fb19SBen GrasThe file descriptor is not open. 137*2fe8fb19SBen GrasThis flag is always checked, even 138*2fe8fb19SBen Grasif not present in the 139*2fe8fb19SBen Gras.Fa events 140*2fe8fb19SBen Grasbitmask. 141*2fe8fb19SBen Gras.El 142*2fe8fb19SBen Gras.Pp 143*2fe8fb19SBen GrasIf 144*2fe8fb19SBen Gras.Fa timeout 145*2fe8fb19SBen Grasis neither zero nor INFTIM (\-1), it specifies a maximum interval to 146*2fe8fb19SBen Graswait for any file descriptor to become ready, in milliseconds. 147*2fe8fb19SBen GrasIf 148*2fe8fb19SBen Gras.Fa timeout 149*2fe8fb19SBen Grasis INFTIM (\-1), the poll blocks indefinitely. 150*2fe8fb19SBen GrasIf 151*2fe8fb19SBen Gras.Fa timeout 152*2fe8fb19SBen Grasis zero, then 153*2fe8fb19SBen Gras.Fn poll 154*2fe8fb19SBen Graswill return without blocking. 155*2fe8fb19SBen Gras.Pp 156*2fe8fb19SBen GrasIf 157*2fe8fb19SBen Gras.Fa ts 158*2fe8fb19SBen Grasis a non-null pointer, it references a timespec structure which specifies a 159*2fe8fb19SBen Grasmaximum interval to wait for any file descriptor to become ready. 160*2fe8fb19SBen GrasIf 161*2fe8fb19SBen Gras.Fa ts 162*2fe8fb19SBen Grasis a null pointer, 163*2fe8fb19SBen Gras.Fn pollts 164*2fe8fb19SBen Grasblocks indefinitely. 165*2fe8fb19SBen GrasIf 166*2fe8fb19SBen Gras.Fa ts 167*2fe8fb19SBen Grasis a non-null pointer, referencing a zero-valued timespec structure, then 168*2fe8fb19SBen Gras.Fn pollts 169*2fe8fb19SBen Graswill return without blocking. 170*2fe8fb19SBen Gras.Pp 171*2fe8fb19SBen GrasIf 172*2fe8fb19SBen Gras.Fa sigmask 173*2fe8fb19SBen Grasis a non-null pointer, then the 174*2fe8fb19SBen Gras.Fn pollts 175*2fe8fb19SBen Grasfunction shall replace the signal mask of the caller by the set of 176*2fe8fb19SBen Grassignals pointed to by 177*2fe8fb19SBen Gras.Fa sigmask 178*2fe8fb19SBen Grasbefore examining the descriptors, and shall restore the signal mask 179*2fe8fb19SBen Grasof the caller before returning. 180*2fe8fb19SBen Gras.Sh RETURN VALUES 181*2fe8fb19SBen Gras.Fn poll 182*2fe8fb19SBen Grasreturns the number of descriptors that are ready for I/O, or \-1 if an 183*2fe8fb19SBen Graserror occurred. 184*2fe8fb19SBen GrasIf the time limit expires, 185*2fe8fb19SBen Gras.Fn poll 186*2fe8fb19SBen Grasreturns 0. 187*2fe8fb19SBen GrasIf 188*2fe8fb19SBen Gras.Fn poll 189*2fe8fb19SBen Grasreturns with an error, 190*2fe8fb19SBen Grasincluding one due to an interrupted call, 191*2fe8fb19SBen Grasthe 192*2fe8fb19SBen Gras.Fa fds 193*2fe8fb19SBen Grasarray will be unmodified. 194*2fe8fb19SBen Gras.Sh COMPATIBILITY 195*2fe8fb19SBen GrasThis implementation differs from the historical one in that a given 196*2fe8fb19SBen Grasfile descriptor may not cause 197*2fe8fb19SBen Gras.Fn poll 198*2fe8fb19SBen Grasto return with an error. 199*2fe8fb19SBen GrasIn cases where this would have happened in the historical implementation 200*2fe8fb19SBen Gras(e.g. trying to poll a 201*2fe8fb19SBen Gras.Xr revoke 2 Ns d 202*2fe8fb19SBen Grasdescriptor), this implementation instead copies the 203*2fe8fb19SBen Gras.Fa events 204*2fe8fb19SBen Grasbitmask to the 205*2fe8fb19SBen Gras.Fa revents 206*2fe8fb19SBen Grasbitmask. 207*2fe8fb19SBen GrasAttempting to perform I/O on this descriptor will then return an error. 208*2fe8fb19SBen GrasThis behaviour is believed to be more useful. 209*2fe8fb19SBen Gras.Sh ERRORS 210*2fe8fb19SBen GrasAn error return from 211*2fe8fb19SBen Gras.Fn poll 212*2fe8fb19SBen Grasindicates: 213*2fe8fb19SBen Gras.Bl -tag -width Er 214*2fe8fb19SBen Gras.It Bq Er EFAULT 215*2fe8fb19SBen Gras.Fa fds 216*2fe8fb19SBen Graspoints outside the process's allocated address space. 217*2fe8fb19SBen Gras.It Bq Er EINTR 218*2fe8fb19SBen GrasA signal was delivered before the time limit expired and 219*2fe8fb19SBen Grasbefore any of the selected events occurred. 220*2fe8fb19SBen Gras.It Bq Er EINVAL 221*2fe8fb19SBen GrasThe specified time limit is negative. 222*2fe8fb19SBen Gras.El 223*2fe8fb19SBen Gras.Sh SEE ALSO 224*2fe8fb19SBen Gras.Xr accept 2 , 225*2fe8fb19SBen Gras.Xr connect 2 , 226*2fe8fb19SBen Gras.Xr read 2 , 227*2fe8fb19SBen Gras.Xr recv 2 , 228*2fe8fb19SBen Gras.Xr select 2 , 229*2fe8fb19SBen Gras.Xr send 2 , 230*2fe8fb19SBen Gras.Xr write 2 231*2fe8fb19SBen Gras.Sh HISTORY 232*2fe8fb19SBen GrasThe 233*2fe8fb19SBen Gras.Fn poll 234*2fe8fb19SBen Grasfunction appeared in 235*2fe8fb19SBen Gras.At V.3 . 236*2fe8fb19SBen GrasThe 237*2fe8fb19SBen Gras.Fn pollts 238*2fe8fb19SBen Grasfunction first appeared in 239*2fe8fb19SBen Gras.Nx 3.0 . 240*2fe8fb19SBen Gras.Sh BUGS 241*2fe8fb19SBen GrasThe distinction between some of the fields in the 242*2fe8fb19SBen Gras.Fa events 243*2fe8fb19SBen Grasand 244*2fe8fb19SBen Gras.Fa revents 245*2fe8fb19SBen Grasbitmasks is really not useful without STREAMS. 246*2fe8fb19SBen GrasThe fields are defined for compatibility with existing software. 247