1.\" $NetBSD: accept.2,v 1.24 2003/08/07 16:43:54 agc Exp $ 2.\" 3.\" Copyright (c) 1983, 1990, 1991, 1993 4.\" The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)accept.2 8.2 (Berkeley) 12/11/93 31.\" 32.Dd October 22, 2001 33.Dt ACCEPT 2 34.Os 35.Sh NAME 36.Nm accept 37.Nd accept a connection on a socket 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In sys/socket.h 42.Ft int 43.Fn accept "int s" "struct sockaddr * restrict addr" "socklen_t * restrict addrlen" 44.Sh DESCRIPTION 45The argument 46.Fa s 47is a socket that has been created with 48.Xr socket 2 , 49bound to an address with 50.Xr bind 2 , 51and is listening for connections after a 52.Xr listen 2 . 53The 54.Fn accept 55argument 56extracts the first connection request on the queue of pending 57connections, creates a new socket with the same properties of 58.Fa s 59and allocates a new file descriptor 60for the socket. 61If no pending connections are 62present on the queue, and the socket is not marked 63as non-blocking, 64.Fn accept 65blocks the caller until a connection is present. 66If the socket is marked non-blocking and no pending 67connections are present on the queue, 68.Fn accept 69returns an error as described below. 70The accepted socket 71may not be used 72to accept more connections. 73The original socket 74.Fa s 75remains open. 76.Pp 77The argument 78.Fa addr 79is a result parameter that is filled in with 80the address of the connecting entity, 81as known to the communications layer. 82The exact format of the 83.Fa addr 84parameter is determined by the domain in which the communication 85is occurring. 86The 87.Fa addrlen 88is a value-result parameter; it should initially contain the 89amount of space pointed to by 90.Fa addr ; 91on return it will contain the actual length (in bytes) of the 92address returned. 93This call 94is used with connection-based socket types, currently with 95.Dv SOCK_STREAM . 96.Pp 97It is possible to 98.Xr select 2 99or 100.Xr poll 2 101a socket for the purposes of doing an 102.Fn accept 103by selecting or polling it for read. 104.Pp 105For certain protocols which require an explicit confirmation, 106such as 107.Tn ISO 108or 109.Tn DATAKIT , 110.Fn accept 111can be thought of 112as merely dequeuing the next connection 113request and not implying confirmation. 114Confirmation can be implied by a normal read or write on the new 115file descriptor, and rejection can be implied by closing the 116new socket. 117.Pp 118One can obtain user connection request data without confirming 119the connection by issuing a 120.Xr recvmsg 2 121call with an 122.Fa msg_iovlen 123of 0 and a non-zero 124.Fa msg_controllen , 125or by issuing a 126.Xr getsockopt 2 127request. 128Similarly, one can provide user connection rejection information 129by issuing a 130.Xr sendmsg 2 131call with providing only the control information, 132or by calling 133.Xr setsockopt 2 . 134.Sh RETURN VALUES 135The call returns \-1 on error. 136If it succeeds, it returns a non-negative 137integer that is a descriptor for the accepted socket. 138.Sh ERRORS 139The 140.Fn accept 141will fail if: 142.Bl -tag -width Er 143.It Bq Er EBADF 144The descriptor is invalid. 145.It Bq Er EINVAL 146The socket has not been set up to accept connections (using 147.Xr bind 2 148and 149.Xr listen 2 ) . 150.It Bq Er ENOTSOCK 151The descriptor references a file, not a socket. 152.It Bq Er EOPNOTSUPP 153The referenced socket is not of type 154.Dv SOCK_STREAM . 155.It Bq Er EFAULT 156The 157.Fa addr 158parameter is not in a writable part of the 159user address space. 160.It Bq Er EAGAIN 161The socket is marked non-blocking and no connections 162are present to be accepted. 163.It Bq Er EMFILE 164The per-process descriptor table is full. 165.It Bq Er ENFILE 166The system file table is full. 167.It Bq Er ECONNABORTED 168A connection has been aborted. 169.El 170.Sh SEE ALSO 171.Xr bind 2 , 172.Xr connect 2 , 173.Xr listen 2 , 174.Xr poll 2 , 175.Xr select 2 , 176.Xr socket 2 177.Sh HISTORY 178The 179.Fn accept 180function appeared in 181.Bx 4.2 . 182