1*84d9c625SLionel Sambuc.\" $NetBSD: accept.2,v 1.31 2013/08/02 20:13:09 wiz Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 1983, 1990, 1991, 1993 42fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 82fe8fb19SBen Gras.\" are met: 92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 102fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 122fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 132fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 152fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 162fe8fb19SBen Gras.\" without specific prior written permission. 172fe8fb19SBen Gras.\" 182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 212fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 282fe8fb19SBen Gras.\" SUCH DAMAGE. 292fe8fb19SBen Gras.\" 302fe8fb19SBen Gras.\" @(#)accept.2 8.2 (Berkeley) 12/11/93 312fe8fb19SBen Gras.\" 32f14fb602SLionel Sambuc.Dd March 19, 2012 332fe8fb19SBen Gras.Dt ACCEPT 2 342fe8fb19SBen Gras.Os 352fe8fb19SBen Gras.Sh NAME 36f14fb602SLionel Sambuc.Nm accept , 37f14fb602SLionel Sambuc.Nm paccept 382fe8fb19SBen Gras.Nd accept a connection on a socket 392fe8fb19SBen Gras.Sh LIBRARY 402fe8fb19SBen Gras.Lb libc 412fe8fb19SBen Gras.Sh SYNOPSIS 422fe8fb19SBen Gras.In sys/socket.h 432fe8fb19SBen Gras.Ft int 442fe8fb19SBen Gras.Fn accept "int s" "struct sockaddr * restrict addr" "socklen_t * restrict addrlen" 45f14fb602SLionel Sambuc.Ft int 46f14fb602SLionel Sambuc.Fn paccept "int s" "struct sockaddr * restrict addr" "socklen_t * restrict addrlen" "const sigset_t * restrict sigmask" "int flags" 472fe8fb19SBen Gras.Sh DESCRIPTION 482fe8fb19SBen GrasThe argument 492fe8fb19SBen Gras.Fa s 502fe8fb19SBen Grasis a socket that has been created with 512fe8fb19SBen Gras.Xr socket 2 , 522fe8fb19SBen Grasbound to an address with 532fe8fb19SBen Gras.Xr bind 2 , 542fe8fb19SBen Grasand is listening for connections after a 552fe8fb19SBen Gras.Xr listen 2 . 562fe8fb19SBen GrasThe 572fe8fb19SBen Gras.Fn accept 582fe8fb19SBen Grasargument 592fe8fb19SBen Grasextracts the first connection request on the queue of pending 602fe8fb19SBen Grasconnections, creates a new socket with the same properties of 612fe8fb19SBen Gras.Fa s 622fe8fb19SBen Grasand allocates a new file descriptor 632fe8fb19SBen Grasfor the socket. 642fe8fb19SBen GrasIf no pending connections are 652fe8fb19SBen Graspresent on the queue, and the socket is not marked 662fe8fb19SBen Grasas non-blocking, 672fe8fb19SBen Gras.Fn accept 682fe8fb19SBen Grasblocks the caller until a connection is present. 692fe8fb19SBen GrasIf the socket is marked non-blocking and no pending 702fe8fb19SBen Grasconnections are present on the queue, 712fe8fb19SBen Gras.Fn accept 722fe8fb19SBen Grasreturns an error as described below. 732fe8fb19SBen GrasThe accepted socket 742fe8fb19SBen Grasmay not be used 752fe8fb19SBen Grasto accept more connections. 762fe8fb19SBen GrasThe original socket 772fe8fb19SBen Gras.Fa s 782fe8fb19SBen Grasremains open. 792fe8fb19SBen Gras.Pp 802fe8fb19SBen GrasThe argument 812fe8fb19SBen Gras.Fa addr 822fe8fb19SBen Grasis a result parameter that is filled in with 832fe8fb19SBen Grasthe address of the connecting entity, 842fe8fb19SBen Grasas known to the communications layer. 852fe8fb19SBen GrasThe exact format of the 862fe8fb19SBen Gras.Fa addr 872fe8fb19SBen Grasparameter is determined by the domain in which the communication 882fe8fb19SBen Grasis occurring. 892fe8fb19SBen GrasThe 902fe8fb19SBen Gras.Fa addrlen 912fe8fb19SBen Grasis a value-result parameter; it should initially contain the 922fe8fb19SBen Grasamount of space pointed to by 932fe8fb19SBen Gras.Fa addr ; 942fe8fb19SBen Grason return it will contain the actual length (in bytes) of the 952fe8fb19SBen Grasaddress returned. 962fe8fb19SBen GrasThis call 972fe8fb19SBen Grasis used with connection-based socket types, currently with 982fe8fb19SBen Gras.Dv SOCK_STREAM . 992fe8fb19SBen Gras.Pp 1002fe8fb19SBen GrasIt is possible to 1012fe8fb19SBen Gras.Xr select 2 1022fe8fb19SBen Grasor 1032fe8fb19SBen Gras.Xr poll 2 1042fe8fb19SBen Grasa socket for the purposes of doing an 1052fe8fb19SBen Gras.Fn accept 1062fe8fb19SBen Grasby selecting or polling it for read. 1072fe8fb19SBen Gras.Pp 1082fe8fb19SBen GrasFor certain protocols which require an explicit confirmation, 1092fe8fb19SBen Grassuch as 1102fe8fb19SBen Gras.Tn ISO 1112fe8fb19SBen Grasor 1122fe8fb19SBen Gras.Tn DATAKIT , 1132fe8fb19SBen Gras.Fn accept 1142fe8fb19SBen Grascan be thought of 1152fe8fb19SBen Grasas merely dequeuing the next connection 1162fe8fb19SBen Grasrequest and not implying confirmation. 1172fe8fb19SBen GrasConfirmation can be implied by a normal read or write on the new 1182fe8fb19SBen Grasfile descriptor, and rejection can be implied by closing the 1192fe8fb19SBen Grasnew socket. 1202fe8fb19SBen Gras.Pp 1212fe8fb19SBen GrasOne can obtain user connection request data without confirming 1222fe8fb19SBen Grasthe connection by issuing a 1232fe8fb19SBen Gras.Xr recvmsg 2 1242fe8fb19SBen Grascall with an 1252fe8fb19SBen Gras.Fa msg_iovlen 1262fe8fb19SBen Grasof 0 and a non-zero 1272fe8fb19SBen Gras.Fa msg_controllen , 1282fe8fb19SBen Grasor by issuing a 1292fe8fb19SBen Gras.Xr getsockopt 2 1302fe8fb19SBen Grasrequest. 1312fe8fb19SBen GrasSimilarly, one can provide user connection rejection information 1322fe8fb19SBen Grasby issuing a 1332fe8fb19SBen Gras.Xr sendmsg 2 1342fe8fb19SBen Grascall with providing only the control information, 1352fe8fb19SBen Grasor by calling 1362fe8fb19SBen Gras.Xr setsockopt 2 . 137f14fb602SLionel Sambuc.Pp 138f14fb602SLionel SambucThe 139f14fb602SLionel Sambuc.Fn paccept 140f14fb602SLionel Sambucfunction behaves exactly like 141f14fb602SLionel Sambuc.Fn accept , 142f14fb602SLionel Sambucbut it also allows to set the following 143f14fb602SLionel Sambuc.Fa flags 144f14fb602SLionel Sambucon the returned file descriptor: 145*84d9c625SLionel Sambuc.Bl -tag -width SOCK_NOSIGPIPEXX -offset indent 146f14fb602SLionel Sambuc.It Dv SOCK_CLOEXEC 147f14fb602SLionel SambucSet the close on exec property. 148f14fb602SLionel Sambuc.It Dv SOCK_NONBLOCK 149f14fb602SLionel SambucSets non-blocking I/O. 150*84d9c625SLionel Sambuc.It Dv SOCK_NOSIGPIPE 151*84d9c625SLionel SambucReturn 152*84d9c625SLionel Sambuc.Er EPIPE 153*84d9c625SLionel Sambucinstead of raising 154*84d9c625SLionel Sambuc.Dv SIGPIPE . 155f14fb602SLionel Sambuc.El 156f14fb602SLionel Sambuc.Pp 157f14fb602SLionel SambucIt can also temporarily replace the signal mask of the calling thread if 158f14fb602SLionel Sambuc.Fa sigmask 159f14fb602SLionel Sambucis a 160f14fb602SLionel Sambuc.Pf non- Dv NULL 161f14fb602SLionel Sambucpointer, then the 162f14fb602SLionel Sambuc.Fn paccept 163f14fb602SLionel Sambucfunction shall replace the signal mask of the caller by the set of 164f14fb602SLionel Sambucsignals pointed to by 165f14fb602SLionel Sambuc.Fa sigmask 166f14fb602SLionel Sambucbefore waiting for a connection, and shall restore the signal mask 167f14fb602SLionel Sambucof the calling thread before returning. 1682fe8fb19SBen Gras.Sh RETURN VALUES 169f14fb602SLionel SambucThe 170f14fb602SLionel Sambuc.Fn accept 171f14fb602SLionel Sambucand 172f14fb602SLionel Sambuc.Fn paccept 173f14fb602SLionel Sambuccalls return \-1 on error. 174f14fb602SLionel SambucIf they succeed, they return a non-negative 1752fe8fb19SBen Grasinteger that is a descriptor for the accepted socket. 176f14fb602SLionel Sambuc.Sh COMPATIBILITY 177f14fb602SLionel SambucThe 178f14fb602SLionel Sambuc.Fn accept 179f14fb602SLionel Sambucimplementation makes the new file descriptor inherit file flags 180f14fb602SLionel Sambuc(like 181f14fb602SLionel Sambuc.Dv O_NONBLOCK ) 182f14fb602SLionel Sambucfrom the listening socket. 183f14fb602SLionel SambucIt's a traditional behaviour for BSD derivative systems. 184f14fb602SLionel SambucOn the other hand, there are implementations which don't do so. 185f14fb602SLionel SambucLinux is an example of such implementations. 186f14fb602SLionel SambucPortable programs should not rely on either of the behaviours. 1872fe8fb19SBen Gras.Sh ERRORS 1882fe8fb19SBen GrasThe 1892fe8fb19SBen Gras.Fn accept 1902fe8fb19SBen Graswill fail if: 1912fe8fb19SBen Gras.Bl -tag -width Er 1922fe8fb19SBen Gras.It Bq Er EAGAIN 1932fe8fb19SBen GrasThe socket is marked non-blocking and no connections 1942fe8fb19SBen Grasare present to be accepted. 1952fe8fb19SBen Gras.It Bq Er EBADF 1962fe8fb19SBen GrasThe descriptor is invalid. 1972fe8fb19SBen Gras.It Bq Er ECONNABORTED 1982fe8fb19SBen GrasA connection has been aborted. 1992fe8fb19SBen Gras.It Bq Er EFAULT 2002fe8fb19SBen GrasThe 2012fe8fb19SBen Gras.Fa addr 2022fe8fb19SBen Grasparameter is not in a writable part of the 2032fe8fb19SBen Grasuser address space. 2042fe8fb19SBen Gras.It Bq Er EINTR 2052fe8fb19SBen GrasThe 2062fe8fb19SBen Gras.Fn accept 2072fe8fb19SBen Grascall has been interrupted by a signal. 2082fe8fb19SBen Gras.It Bq Er EINVAL 2092fe8fb19SBen GrasThe socket has not been set up to accept connections (using 2102fe8fb19SBen Gras.Xr bind 2 2112fe8fb19SBen Grasand 2122fe8fb19SBen Gras.Xr listen 2 ) . 2132fe8fb19SBen Gras.It Bq Er EMFILE 2142fe8fb19SBen GrasThe per-process descriptor table is full. 2152fe8fb19SBen Gras.It Bq Er ENFILE 2162fe8fb19SBen GrasThe system file table is full. 2172fe8fb19SBen Gras.It Bq Er ENOTSOCK 2182fe8fb19SBen GrasThe descriptor references a file, not a socket. 2192fe8fb19SBen Gras.It Bq Er EOPNOTSUPP 2202fe8fb19SBen GrasThe referenced socket is not of type 2212fe8fb19SBen Gras.Dv SOCK_STREAM . 2222fe8fb19SBen Gras.El 2232fe8fb19SBen Gras.Sh SEE ALSO 2242fe8fb19SBen Gras.Xr bind 2 , 2252fe8fb19SBen Gras.Xr connect 2 , 2262fe8fb19SBen Gras.Xr listen 2 , 2272fe8fb19SBen Gras.Xr poll 2 , 2282fe8fb19SBen Gras.Xr select 2 , 2292fe8fb19SBen Gras.Xr socket 2 2302fe8fb19SBen Gras.Sh HISTORY 2312fe8fb19SBen GrasThe 2322fe8fb19SBen Gras.Fn accept 2332fe8fb19SBen Grasfunction appeared in 2342fe8fb19SBen Gras.Bx 4.2 . 235f14fb602SLionel SambucThe 236f14fb602SLionel Sambuc.Fn paccept 237f14fb602SLionel Sambucfunction is inspired from Linux and appeared in 238f14fb602SLionel Sambuc.Nx 6.0 . 239