1*8269e767SBrooks Davis.\" Copyright (c) 1983, 1990, 1991, 1993 2*8269e767SBrooks Davis.\" The Regents of the University of California. All rights reserved. 3*8269e767SBrooks Davis.\" 4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 6*8269e767SBrooks Davis.\" are met: 7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 8*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 10*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 11*8269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors 13*8269e767SBrooks Davis.\" may be used to endorse or promote products derived from this software 14*8269e767SBrooks Davis.\" without specific prior written permission. 15*8269e767SBrooks Davis.\" 16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*8269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*8269e767SBrooks Davis.\" SUCH DAMAGE. 27*8269e767SBrooks Davis.\" 28*8269e767SBrooks Davis.Dd October 9, 2014 29*8269e767SBrooks Davis.Dt ACCEPT 2 30*8269e767SBrooks Davis.Os 31*8269e767SBrooks Davis.Sh NAME 32*8269e767SBrooks Davis.Nm accept , 33*8269e767SBrooks Davis.Nm accept4 34*8269e767SBrooks Davis.Nd accept a connection on a socket 35*8269e767SBrooks Davis.Sh LIBRARY 36*8269e767SBrooks Davis.Lb libc 37*8269e767SBrooks Davis.Sh SYNOPSIS 38*8269e767SBrooks Davis.In sys/types.h 39*8269e767SBrooks Davis.In sys/socket.h 40*8269e767SBrooks Davis.Ft int 41*8269e767SBrooks Davis.Fn accept "int s" "struct sockaddr * restrict addr" "socklen_t * restrict addrlen" 42*8269e767SBrooks Davis.Ft int 43*8269e767SBrooks Davis.Fn accept4 "int s" "struct sockaddr * restrict addr" "socklen_t * restrict addrlen" "int flags" 44*8269e767SBrooks Davis.Sh DESCRIPTION 45*8269e767SBrooks DavisThe argument 46*8269e767SBrooks Davis.Fa s 47*8269e767SBrooks Davisis a socket that has been created with 48*8269e767SBrooks Davis.Xr socket 2 , 49*8269e767SBrooks Davisbound to an address with 50*8269e767SBrooks Davis.Xr bind 2 , 51*8269e767SBrooks Davisand is listening for connections after a 52*8269e767SBrooks Davis.Xr listen 2 . 53*8269e767SBrooks DavisThe 54*8269e767SBrooks Davis.Fn accept 55*8269e767SBrooks Davissystem call extracts the first connection request on the 56*8269e767SBrooks Davisqueue of pending connections, creates a new socket, 57*8269e767SBrooks Davisand allocates a new file descriptor for the socket which 58*8269e767SBrooks Davisinherits the state of the 59*8269e767SBrooks Davis.Dv O_NONBLOCK 60*8269e767SBrooks Davisand 61*8269e767SBrooks Davis.Dv O_ASYNC 62*8269e767SBrooks Davisproperties and the destination of 63*8269e767SBrooks Davis.Dv SIGIO 64*8269e767SBrooks Davisand 65*8269e767SBrooks Davis.Dv SIGURG 66*8269e767SBrooks Davissignals from the original socket 67*8269e767SBrooks Davis.Fa s . 68*8269e767SBrooks Davis.Pp 69*8269e767SBrooks DavisThe 70*8269e767SBrooks Davis.Fn accept4 71*8269e767SBrooks Davissystem call is similar, 72*8269e767SBrooks Davisbut the 73*8269e767SBrooks Davis.Dv O_NONBLOCK 74*8269e767SBrooks Davisproperty of the new socket is instead determined by the 75*8269e767SBrooks Davis.Dv SOCK_NONBLOCK 76*8269e767SBrooks Davisflag in the 77*8269e767SBrooks Davis.Fa flags 78*8269e767SBrooks Davisargument, 79*8269e767SBrooks Davisthe 80*8269e767SBrooks Davis.Dv O_ASYNC 81*8269e767SBrooks Davisproperty is cleared, 82*8269e767SBrooks Davisthe signal destination is cleared 83*8269e767SBrooks Davisand the close-on-exec flag on the new file descriptor can be set via the 84*8269e767SBrooks Davis.Dv SOCK_CLOEXEC 85*8269e767SBrooks Davisflag in the 86*8269e767SBrooks Davis.Fa flags 87*8269e767SBrooks Davisargument. 88*8269e767SBrooks Davis.Pp 89*8269e767SBrooks DavisIf no pending connections are 90*8269e767SBrooks Davispresent on the queue, and the original socket 91*8269e767SBrooks Davisis not marked as non-blocking, 92*8269e767SBrooks Davis.Fn accept 93*8269e767SBrooks Davisblocks the caller until a connection is present. 94*8269e767SBrooks DavisIf the original socket 95*8269e767SBrooks Davisis marked non-blocking and no pending 96*8269e767SBrooks Davisconnections are present on the queue, 97*8269e767SBrooks Davis.Fn accept 98*8269e767SBrooks Davisreturns an error as described below. 99*8269e767SBrooks DavisThe accepted socket 100*8269e767SBrooks Davismay not be used 101*8269e767SBrooks Davisto accept more connections. 102*8269e767SBrooks DavisThe original socket 103*8269e767SBrooks Davis.Fa s 104*8269e767SBrooks Davisremains open. 105*8269e767SBrooks Davis.Pp 106*8269e767SBrooks DavisThe argument 107*8269e767SBrooks Davis.Fa addr 108*8269e767SBrooks Davisis a result argument that is filled-in with 109*8269e767SBrooks Davisthe address of the connecting entity, 110*8269e767SBrooks Davisas known to the communications layer. 111*8269e767SBrooks DavisThe exact format of the 112*8269e767SBrooks Davis.Fa addr 113*8269e767SBrooks Davisargument is determined by the domain in which the communication 114*8269e767SBrooks Davisis occurring. 115*8269e767SBrooks DavisA null pointer may be specified for 116*8269e767SBrooks Davis.Fa addr 117*8269e767SBrooks Davisif the address information is not desired; 118*8269e767SBrooks Davisin this case, 119*8269e767SBrooks Davis.Fa addrlen 120*8269e767SBrooks Davisis not used and should also be null. 121*8269e767SBrooks DavisOtherwise, the 122*8269e767SBrooks Davis.Fa addrlen 123*8269e767SBrooks Davisargument 124*8269e767SBrooks Davisis a value-result argument; it should initially contain the 125*8269e767SBrooks Davisamount of space pointed to by 126*8269e767SBrooks Davis.Fa addr ; 127*8269e767SBrooks Davison return it will contain the actual length (in bytes) of the 128*8269e767SBrooks Davisaddress returned. 129*8269e767SBrooks DavisThis call 130*8269e767SBrooks Davisis used with connection-based socket types, currently with 131*8269e767SBrooks Davis.Dv SOCK_STREAM . 132*8269e767SBrooks Davis.Pp 133*8269e767SBrooks DavisIt is possible to 134*8269e767SBrooks Davis.Xr select 2 135*8269e767SBrooks Davisa socket for the purposes of doing an 136*8269e767SBrooks Davis.Fn accept 137*8269e767SBrooks Davisby selecting it for read. 138*8269e767SBrooks Davis.Pp 139*8269e767SBrooks DavisFor certain protocols which require an explicit confirmation, 140*8269e767SBrooks Davissuch as 141*8269e767SBrooks Davis.Tn ISO 142*8269e767SBrooks Davisor 143*8269e767SBrooks Davis.Tn DATAKIT , 144*8269e767SBrooks Davis.Fn accept 145*8269e767SBrooks Daviscan be thought of 146*8269e767SBrooks Davisas merely dequeueing the next connection 147*8269e767SBrooks Davisrequest and not implying confirmation. 148*8269e767SBrooks DavisConfirmation can be implied by a normal read or write on the new 149*8269e767SBrooks Davisfile descriptor, and rejection can be implied by closing the 150*8269e767SBrooks Davisnew socket. 151*8269e767SBrooks Davis.Pp 152*8269e767SBrooks DavisFor some applications, performance may be enhanced by using an 153*8269e767SBrooks Davis.Xr accept_filter 9 154*8269e767SBrooks Davisto pre-process incoming connections. 155*8269e767SBrooks Davis.Pp 156*8269e767SBrooks DavisWhen using 157*8269e767SBrooks Davis.Fn accept , 158*8269e767SBrooks Davisportable programs should not rely on the 159*8269e767SBrooks Davis.Dv O_NONBLOCK 160*8269e767SBrooks Davisand 161*8269e767SBrooks Davis.Dv O_ASYNC 162*8269e767SBrooks Davisproperties and the signal destination being inherited, 163*8269e767SBrooks Davisbut should set them explicitly using 164*8269e767SBrooks Davis.Xr fcntl 2 ; 165*8269e767SBrooks Davis.Fn accept4 166*8269e767SBrooks Davissets these properties consistently, 167*8269e767SBrooks Davisbut may not be fully portable across 168*8269e767SBrooks Davis.Ux 169*8269e767SBrooks Davisplatforms. 170*8269e767SBrooks Davis.Sh RETURN VALUES 171*8269e767SBrooks DavisThese calls return \-1 on error. 172*8269e767SBrooks DavisIf they succeed, they return a non-negative 173*8269e767SBrooks Davisinteger that is a descriptor for the accepted socket. 174*8269e767SBrooks Davis.Sh ERRORS 175*8269e767SBrooks DavisThe 176*8269e767SBrooks Davis.Fn accept 177*8269e767SBrooks Davisand 178*8269e767SBrooks Davis.Fn accept4 179*8269e767SBrooks Davissystem calls will fail if: 180*8269e767SBrooks Davis.Bl -tag -width Er 181*8269e767SBrooks Davis.It Bq Er EBADF 182*8269e767SBrooks DavisThe descriptor is invalid. 183*8269e767SBrooks Davis.It Bq Er EINTR 184*8269e767SBrooks DavisThe 185*8269e767SBrooks Davis.Fn accept 186*8269e767SBrooks Davisoperation was interrupted. 187*8269e767SBrooks Davis.It Bq Er EMFILE 188*8269e767SBrooks DavisThe per-process descriptor table is full. 189*8269e767SBrooks Davis.It Bq Er ENFILE 190*8269e767SBrooks DavisThe system file table is full. 191*8269e767SBrooks Davis.It Bq Er ENOTSOCK 192*8269e767SBrooks DavisThe descriptor references a file, not a socket. 193*8269e767SBrooks Davis.It Bq Er EINVAL 194*8269e767SBrooks Davis.Xr listen 2 195*8269e767SBrooks Davishas not been called on the socket descriptor. 196*8269e767SBrooks Davis.It Bq Er EFAULT 197*8269e767SBrooks DavisThe 198*8269e767SBrooks Davis.Fa addr 199*8269e767SBrooks Davisargument is not in a writable part of the 200*8269e767SBrooks Davisuser address space. 201*8269e767SBrooks Davis.It Bo Er EWOULDBLOCK Bc or Bq Er EAGAIN 202*8269e767SBrooks DavisThe socket is marked non-blocking and no connections 203*8269e767SBrooks Davisare present to be accepted. 204*8269e767SBrooks Davis.It Bq Er ECONNABORTED 205*8269e767SBrooks DavisA connection arrived, but it was closed while waiting 206*8269e767SBrooks Davison the listen queue. 207*8269e767SBrooks Davis.El 208*8269e767SBrooks Davis.Pp 209*8269e767SBrooks DavisThe 210*8269e767SBrooks Davis.Fn accept4 211*8269e767SBrooks Davissystem call will also fail if: 212*8269e767SBrooks Davis.Bl -tag -width Er 213*8269e767SBrooks Davis.It Bq Er EINVAL 214*8269e767SBrooks DavisThe 215*8269e767SBrooks Davis.Fa flags 216*8269e767SBrooks Davisargument is invalid. 217*8269e767SBrooks Davis.El 218*8269e767SBrooks Davis.Sh SEE ALSO 219*8269e767SBrooks Davis.Xr bind 2 , 220*8269e767SBrooks Davis.Xr connect 2 , 221*8269e767SBrooks Davis.Xr getpeername 2 , 222*8269e767SBrooks Davis.Xr getsockname 2 , 223*8269e767SBrooks Davis.Xr listen 2 , 224*8269e767SBrooks Davis.Xr select 2 , 225*8269e767SBrooks Davis.Xr socket 2 , 226*8269e767SBrooks Davis.Xr accept_filter 9 227*8269e767SBrooks Davis.Sh HISTORY 228*8269e767SBrooks DavisThe 229*8269e767SBrooks Davis.Fn accept 230*8269e767SBrooks Davissystem call appeared in 231*8269e767SBrooks Davis.Bx 4.2 . 232*8269e767SBrooks Davis.Pp 233*8269e767SBrooks DavisThe 234*8269e767SBrooks Davis.Fn accept4 235*8269e767SBrooks Davissystem call appeared in 236*8269e767SBrooks Davis.Fx 10.0 . 237