1.\" $OpenBSD: accept.2,v 1.28 2014/09/09 03:50:43 guenther 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: September 9 2014 $ 34.Dt ACCEPT 2 35.Os 36.Sh NAME 37.Nm accept , 38.Nm accept4 39.Nd accept a connection on a socket 40.Sh SYNOPSIS 41.In sys/socket.h 42.Ft int 43.Fn accept "int s" "struct sockaddr *addr" "socklen_t *addrlen" 44.Ft int 45.Fn accept4 "int s" "struct sockaddr *addr" "socklen_t *addrlen" "int flags" 46.Sh DESCRIPTION 47The argument 48.Fa s 49is a socket that has been created with 50.Xr socket 2 , 51bound to an address with 52.Xr bind 2 , 53and is listening for connections after a 54.Xr listen 2 . 55The 56.Fn accept 57call extracts the first connection request on the queue of pending 58connections, creates a new socket with the same non-blocking I/O mode as 59.Fa s , 60and allocates a new file descriptor for the socket with the 61close-on-exec flag clear. 62.Pp 63The 64.Fn accept4 65system call is similar, however the non-blocking I/O mode of the 66new socket is determined by the 67.Dv SOCK_NONBLOCK 68flag in the 69.Fa flags 70argument and the close-on-exec flag on the new file descriptor is 71determined by the 72.Dv SOCK_CLOEXEC 73flag in the 74.Fa flags 75argument. 76.Pp 77If no pending connections are present on the queue, 78and the socket is not marked as non-blocking, 79.Fn accept 80blocks the caller until a connection is present. 81If the socket is marked non-blocking and no pending 82connections are present on the queue, 83.Fn accept 84returns an error as described below. 85The accepted socket may not be used to accept more connections. 86The original socket 87.Fa s 88remains open. 89.Pp 90The argument 91.Fa addr 92is a result parameter that is filled in with the address of the connecting 93entity as known to the communications layer. 94The exact format of the 95.Fa addr 96parameter is determined by the domain in which the communication 97is occurring. 98The structure 99.Li sockaddr_storage 100exists for greater portability. 101It is large enough to hold any of the types that may be returned in the 102.Fa addr 103parameter. 104.Pp 105The 106.Fa addrlen 107is a value-result parameter; it should initially contain the 108amount of space pointed to by 109.Fa addr ; 110on return it will contain the actual length (in bytes) of the 111address returned. 112If 113.Fa addrlen 114does not point to enough space to hold the entire socket address, the 115result will be truncated to the initial value of 116.Fa addrlen 117(in bytes). 118This call is used with connection-based socket types, currently with 119.Dv SOCK_STREAM . 120.Pp 121It is possible to 122.Xr select 2 123or 124.Xr poll 2 125a socket for the purposes of doing an 126.Fn accept 127by selecting it for read. 128.Sh RETURN VALUES 129The call returns \-1 on error. 130If it succeeds, it returns a non-negative integer that is a descriptor 131for the accepted socket. 132.Sh EXAMPLES 133The following code uses struct 134.Li sockaddr_storage 135to allocate enough space for the returned address: 136.Bd -literal -offset indent 137#include <sys/types.h> 138#include <sys/socket.h> 139 140struct sockaddr_storage addr; 141socklen_t len = sizeof(addr); 142int retcode; 143 144retcode = accept(s, (struct sockaddr *)&addr, &len); 145if (retcode == -1) 146 err(1, "accept"); 147.Ed 148.Sh ERRORS 149.Fn accept 150and 151.Fn accept4 152will fail if: 153.Bl -tag -width Er 154.It Bq Er EBADF 155The descriptor is invalid. 156.It Bq Er ENOTSOCK 157The descriptor doesn't reference a socket. 158.It Bq Er EOPNOTSUPP 159The referenced socket is not of type 160.Dv SOCK_STREAM . 161.It Bq Er EINTR 162A signal was caught before a connection arrived. 163.It Bq Er EINVAL 164The referenced socket is not listening for connections (that is, 165.Xr listen 2 166has not yet been called). 167.It Bq Er EFAULT 168The 169.Fa addr 170or 171.Fa addrlen 172parameter is not in a valid part of the process address space. 173.It Bq Er EWOULDBLOCK 174The socket is marked non-blocking and no connections 175are present to be accepted. 176.It Bq Er EMFILE 177The per-process descriptor table is full. 178.It Bq Er ENFILE 179The system file table is full. 180.It Bq Er ECONNABORTED 181A connection has been aborted. 182.El 183.Pp 184In addition, 185.Fn accept4 186will fail if 187.Bl -tag -width Er 188.It Bq Er EINVAL 189.Fa flags 190is invalid. 191.El 192.Sh SEE ALSO 193.Xr bind 2 , 194.Xr connect 2 , 195.Xr listen 2 , 196.Xr poll 2 , 197.Xr select 2 , 198.Xr socket 2 199.Sh STANDARDS 200The 201.Fn accept 202function conforms to 203.St -p1003.1-2008 . 204The 205.Fn accept4 206function is expected to conform to a future revision of that standard. 207.Sh HISTORY 208The 209.Fn accept 210system call first appeared in 211.Bx 4.1c 212and 213.Fn accept4 214in 215.Ox 5.7 . 216.Sh CAVEATS 217When 218.Er EMFILE 219or 220.Er ENFILE 221is returned, 222new connections are neither dequeued nor discarded. 223Thus considerable care is required in 224.Xr select 2 225and 226.Xr poll 2 227loops. 228