xref: /freebsd/lib/libsys/accept.2 (revision ae07a5805b1906f29e786f415d67bef334557bd3)
1.\" Copyright (c) 1983, 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd May 17, 2025
29.Dt ACCEPT 2
30.Os
31.Sh NAME
32.Nm accept ,
33.Nm accept4
34.Nd accept a connection on a socket
35.Sh LIBRARY
36.Lb libc
37.Sh SYNOPSIS
38.In sys/types.h
39.In sys/socket.h
40.Ft int
41.Fn accept "int s" "struct sockaddr * restrict addr" "socklen_t * restrict addrlen"
42.Ft int
43.Fn accept4 "int s" "struct sockaddr * restrict addr" "socklen_t * restrict addrlen" "int flags"
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
55system call extracts the first connection request on the
56queue of pending connections, creates a new socket,
57and allocates a new file descriptor for the socket which
58inherits the state of the
59.Dv O_NONBLOCK
60and
61.Dv O_ASYNC
62properties and the destination of
63.Dv SIGIO
64and
65.Dv SIGURG
66signals from the original socket
67.Fa s .
68.Pp
69The
70.Fn accept4
71system call is similar,
72but the
73.Dv O_NONBLOCK
74property of the new socket is instead determined by the
75.Dv SOCK_NONBLOCK
76flag in the
77.Fa flags
78argument,
79the
80.Dv O_ASYNC
81property is cleared,
82the signal destination is cleared
83and the close-on-exec flag on the new file descriptor can be set via the
84.Dv SOCK_CLOEXEC
85flag in the
86.Fa flags
87argument.
88Similarly, the
89.Dv O_CLOFORK
90property can be set via the
91.Dv SOCK_CLOFORK
92flag in the
93.Fa flags
94argument.
95.Pp
96If no pending connections are
97present on the queue, and the original socket
98is not marked as non-blocking,
99.Fn accept
100blocks the caller until a connection is present.
101If the original socket
102is marked non-blocking and no pending
103connections are present on the queue,
104.Fn accept
105returns an error as described below.
106The accepted socket
107may not be used
108to accept more connections.
109The original socket
110.Fa s
111remains open.
112.Pp
113The argument
114.Fa addr
115is a result argument that is filled-in with
116the address of the connecting entity,
117as known to the communications layer.
118The exact format of the
119.Fa addr
120argument is determined by the domain in which the communication
121is occurring.
122A null pointer may be specified for
123.Fa addr
124if the address information is not desired;
125in this case,
126.Fa addrlen
127is not used and should also be null.
128Otherwise, the
129.Fa addrlen
130argument
131is a value-result argument; it should initially contain the
132amount of space pointed to by
133.Fa addr ;
134on return it will contain the actual length (in bytes) of the
135address returned.
136This call
137is used with connection-based socket types, currently with
138.Dv SOCK_STREAM .
139.Pp
140It is possible to
141.Xr select 2
142a socket for the purposes of doing an
143.Fn accept
144by selecting it for read.
145.Pp
146For certain protocols which require an explicit confirmation,
147such as
148.Tn ISO
149or
150.Tn DATAKIT ,
151.Fn accept
152can be thought of
153as merely dequeueing the next connection
154request and not implying confirmation.
155Confirmation can be implied by a normal read or write on the new
156file descriptor, and rejection can be implied by closing the
157new socket.
158.Pp
159For some applications, performance may be enhanced by using an
160.Xr accept_filter 9
161to pre-process incoming connections.
162.Pp
163When using
164.Fn accept ,
165portable programs should not rely on the
166.Dv O_NONBLOCK
167and
168.Dv O_ASYNC
169properties and the signal destination being inherited,
170but should set them explicitly using
171.Xr fcntl 2 ;
172.Fn accept4
173sets these properties consistently,
174but may not be fully portable across
175.Ux
176platforms.
177.Sh RETURN VALUES
178These calls return \-1 on error.
179If they succeed, they return a non-negative
180integer that is a descriptor for the accepted socket.
181.Sh ERRORS
182The
183.Fn accept
184and
185.Fn accept4
186system calls will fail if:
187.Bl -tag -width Er
188.It Bq Er EBADF
189The descriptor is invalid.
190.It Bq Er EINTR
191The
192.Fn accept
193operation was interrupted.
194.It Bq Er EMFILE
195The per-process descriptor table is full.
196.It Bq Er ENFILE
197The system file table is full.
198.It Bq Er ENOTSOCK
199The descriptor references a file, not a socket.
200.It Bq Er EINVAL
201.Xr listen 2
202has not been called on the socket descriptor.
203.It Bq Er EFAULT
204The
205.Fa addr
206argument is not in a writable part of the
207user address space.
208.It Bo Er EWOULDBLOCK Bc or Bq Er EAGAIN
209The socket is marked non-blocking and no connections
210are present to be accepted.
211.It Bq Er ECONNABORTED
212A connection arrived, but it was closed while waiting
213on the listen queue.
214.El
215.Pp
216The
217.Fn accept4
218system call will also fail if:
219.Bl -tag -width Er
220.It Bq Er EINVAL
221The
222.Fa flags
223argument is invalid.
224.El
225.Sh SEE ALSO
226.Xr bind 2 ,
227.Xr connect 2 ,
228.Xr getpeername 2 ,
229.Xr getsockname 2 ,
230.Xr listen 2 ,
231.Xr select 2 ,
232.Xr socket 2 ,
233.Xr accept_filter 9
234.Sh HISTORY
235The
236.Fn accept
237system call appeared in
238.Bx 4.2 .
239.Pp
240The
241.Fn accept4
242system call appeared in
243.Fx 10.0 .
244.Pp
245The
246.Dv SOCK_CLOFORK
247flag appeared in
248.Fx 15.0 .
249