xref: /openbsd/lib/libc/sys/accept.2 (revision 09467b48)
1.\"	$OpenBSD: accept.2,v 1.29 2019/05/26 09:47:28 krw 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 26 2019 $
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
129If successful,
130.Fn accept
131and
132.Fn accept4
133return a non-negative integer, the accepted socket file descriptor.
134Otherwise, a value of \-1 is returned and
135.Va errno
136is set to indicate the error.
137.Sh EXAMPLES
138The following code uses struct
139.Li sockaddr_storage
140to allocate enough space for the returned address:
141.Bd -literal -offset indent
142#include <sys/types.h>
143#include <sys/socket.h>
144
145struct sockaddr_storage addr;
146socklen_t len = sizeof(addr);
147int retcode;
148
149retcode = accept(s, (struct sockaddr *)&addr, &len);
150if (retcode == -1)
151	err(1, "accept");
152.Ed
153.Sh ERRORS
154.Fn accept
155and
156.Fn accept4
157will fail if:
158.Bl -tag -width Er
159.It Bq Er EBADF
160The descriptor is invalid.
161.It Bq Er ENOTSOCK
162The descriptor doesn't reference a socket.
163.It Bq Er EOPNOTSUPP
164The referenced socket is not of type
165.Dv SOCK_STREAM .
166.It Bq Er EINTR
167A signal was caught before a connection arrived.
168.It Bq Er EINVAL
169The referenced socket is not listening for connections (that is,
170.Xr listen 2
171has not yet been called).
172.It Bq Er EFAULT
173The
174.Fa addr
175or
176.Fa addrlen
177parameter is not in a valid part of the process address space.
178.It Bq Er EWOULDBLOCK
179The socket is marked non-blocking and no connections
180are present to be accepted.
181.It Bq Er EMFILE
182The per-process descriptor table is full.
183.It Bq Er ENFILE
184The system file table is full.
185.It Bq Er ECONNABORTED
186A connection has been aborted.
187.El
188.Pp
189In addition,
190.Fn accept4
191will fail if
192.Bl -tag -width Er
193.It Bq Er EINVAL
194.Fa flags
195is invalid.
196.El
197.Sh SEE ALSO
198.Xr bind 2 ,
199.Xr connect 2 ,
200.Xr listen 2 ,
201.Xr poll 2 ,
202.Xr select 2 ,
203.Xr socket 2
204.Sh STANDARDS
205The
206.Fn accept
207function conforms to
208.St -p1003.1-2008 .
209The
210.Fn accept4
211function is expected to conform to a future revision of that standard.
212.Sh HISTORY
213The
214.Fn accept
215system call first appeared in
216.Bx 4.1c
217and
218.Fn accept4
219in
220.Ox 5.7 .
221.Sh CAVEATS
222When
223.Er EMFILE
224or
225.Er ENFILE
226is returned,
227new connections are neither dequeued nor discarded.
228Thus considerable care is required in
229.Xr select 2
230and
231.Xr poll 2
232loops.
233