1.\" $OpenBSD: accept.2,v 1.20 2007/05/31 19:19:32 jmc Exp $ 2.\" $NetBSD: accept.2,v 1.7 1996/01/31 20:14:42 mycroft Exp $ 3.\" 4.\" Copyright (c) 1983, 1990, 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)accept.2 8.2 (Berkeley) 12/11/93 32.\" 33.Dd $Mdocdate: May 31 2007 $ 34.Dt ACCEPT 2 35.Os 36.Sh NAME 37.Nm accept 38.Nd accept a connection on a socket 39.Sh SYNOPSIS 40.Fd #include <sys/types.h> 41.Fd #include <sys/socket.h> 42.Ft int 43.Fn accept "int s" "struct sockaddr *addr" "socklen_t *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 55call extracts the first connection request on the queue of pending 56connections, creates a new socket with the same properties of 57.Fa s , 58and allocates a new file descriptor for the socket. 59If no pending connections are present on the queue, 60and the socket is not marked as non-blocking, 61.Fn accept 62blocks the caller until a connection is present. 63If the socket is marked non-blocking and no pending 64connections are present on the queue, 65.Fn accept 66returns an error as described below. 67The accepted socket may not be used to accept more connections. 68The original socket 69.Fa s 70remains open. 71.Pp 72The argument 73.Fa addr 74is a result parameter that is filled in with the address of the connecting 75entity as known to the communications layer. 76The exact format of the 77.Fa addr 78parameter is determined by the domain in which the communication 79is occurring. 80The structure 81.Li sockaddr_storage 82exists for greater portability. 83It is large enough to hold any of the types that may be returned in the 84.Fa addr 85parameter. 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 is used with connection-based socket types, currently with 94.Dv SOCK_STREAM . 95.Pp 96It is possible to 97.Xr select 2 98or 99.Xr poll 2 100a socket for the purposes of doing an 101.Fn accept 102by selecting it for read. 103.Pp 104For certain protocols which require an explicit confirmation, 105.Fn accept 106can be thought of as merely dequeuing the next connection 107request and not implying confirmation. 108Confirmation can be implied by a normal read or write on the new file 109descriptor, and rejection can be implied by closing the new socket. 110.Pp 111One can obtain user connection request data without confirming 112the connection by issuing a 113.Xr recvmsg 2 114call with an 115.Fa msg_iovlen 116of 0 and a non-zero 117.Fa msg_controllen , 118or by issuing a 119.Xr getsockopt 2 120request. 121Similarly, one can provide user connection rejection information 122by issuing a 123.Xr sendmsg 2 124call providing only the control information, or by calling 125.Xr setsockopt 2 . 126.Sh RETURN VALUES 127The call returns \-1 on error. 128If it succeeds, it returns a non-negative integer that is a descriptor 129for the accepted socket. 130.Sh EXAMPLES 131The following code uses struct 132.Li sockaddr_storage 133to allocate enough space for the returned address: 134.Bd -literal -offset indent 135#include <sys/types.h> 136#include <sys/socket.h> 137 138struct sockaddr_storage addr; 139socklen_t len = sizeof(addr); 140int retcode; 141 142retcode = accept(s, (struct sockaddr *)&addr, &len); 143if (retcode == -1) 144 err(1, "accept"); 145.Ed 146.Sh ERRORS 147The 148.Fn accept 149will fail if: 150.Bl -tag -width Er 151.It Bq Er EBADF 152The descriptor is invalid. 153.It Bq Er ENOTSOCK 154The descriptor references a file, not a socket. 155.It Bq Er EOPNOTSUPP 156The referenced socket is not of type 157.Dv SOCK_STREAM . 158.It Bq Er EINTR 159A signal was caught before a connection arrived. 160.It Bq Er EINVAL 161The referenced socket is not listening for connections (that is, 162.Xr listen 2 163has not yet been called). 164.It Bq Er EFAULT 165The 166.Fa addr 167or 168.Fa addrlen 169parameter is not in a valid part of the process address space. 170.It Bq Er EWOULDBLOCK 171The socket is marked non-blocking and no connections 172are present to be accepted. 173.It Bq Er EMFILE 174The per-process descriptor table is full. 175.It Bq Er ENFILE 176The system file table is full. 177.It Bq Er ECONNABORTED 178A connection has been aborted. 179.El 180.Sh SEE ALSO 181.Xr bind 2 , 182.Xr connect 2 , 183.Xr listen 2 , 184.Xr poll 2 , 185.Xr select 2 , 186.Xr socket 2 187.Sh HISTORY 188The 189.Fn accept 190function appeared in 191.Bx 4.2 . 192