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