xref: /freebsd-14.2/lib/libc/sys/accept.2 (revision b2c76c41)
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.\"     @(#)accept.2	8.2 (Berkeley) 12/11/93
29.\"
30.Dd October 9, 2014
31.Dt ACCEPT 2
32.Os
33.Sh NAME
34.Nm accept ,
35.Nm accept4
36.Nd accept a connection on a socket
37.Sh LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In sys/types.h
41.In sys/socket.h
42.Ft int
43.Fn accept "int s" "struct sockaddr * restrict addr" "socklen_t * restrict addrlen"
44.Ft int
45.Fn accept4 "int s" "struct sockaddr * restrict addr" "socklen_t * restrict 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
57system call extracts the first connection request on the
58queue of pending connections, creates a new socket,
59and allocates a new file descriptor for the socket which
60inherits the state of the
61.Dv O_NONBLOCK
62and
63.Dv O_ASYNC
64properties and the destination of
65.Dv SIGIO
66and
67.Dv SIGURG
68signals from the original socket
69.Fa s .
70.Pp
71The
72.Fn accept4
73system call is similar,
74but the
75.Dv O_NONBLOCK
76property of the new socket is instead determined by the
77.Dv SOCK_NONBLOCK
78flag in the
79.Fa flags
80argument,
81the
82.Dv O_ASYNC
83property is cleared,
84the signal destination is cleared
85and the close-on-exec flag on the new file descriptor can be set via the
86.Dv SOCK_CLOEXEC
87flag in the
88.Fa flags
89argument.
90.Pp
91If no pending connections are
92present on the queue, and the original socket
93is not marked as non-blocking,
94.Fn accept
95blocks the caller until a connection is present.
96If the original socket
97is marked non-blocking and no pending
98connections are present on the queue,
99.Fn accept
100returns an error as described below.
101The accepted socket
102may not be used
103to accept more connections.
104The original socket
105.Fa s
106remains open.
107.Pp
108The argument
109.Fa addr
110is a result argument that is filled-in with
111the address of the connecting entity,
112as known to the communications layer.
113The exact format of the
114.Fa addr
115argument is determined by the domain in which the communication
116is occurring.
117A null pointer may be specified for
118.Fa addr
119if the address information is not desired;
120in this case,
121.Fa addrlen
122is not used and should also be null.
123Otherwise, the
124.Fa addrlen
125argument
126is a value-result argument; it should initially contain the
127amount of space pointed to by
128.Fa addr ;
129on return it will contain the actual length (in bytes) of the
130address returned.
131This call
132is used with connection-based socket types, currently with
133.Dv SOCK_STREAM .
134.Pp
135It is possible to
136.Xr select 2
137a socket for the purposes of doing an
138.Fn accept
139by selecting it for read.
140.Pp
141For certain protocols which require an explicit confirmation,
142such as
143.Tn ISO
144or
145.Tn DATAKIT ,
146.Fn accept
147can be thought of
148as merely dequeueing the next connection
149request and not implying confirmation.
150Confirmation can be implied by a normal read or write on the new
151file descriptor, and rejection can be implied by closing the
152new socket.
153.Pp
154For some applications, performance may be enhanced by using an
155.Xr accept_filter 9
156to pre-process incoming connections.
157.Pp
158When using
159.Fn accept ,
160portable programs should not rely on the
161.Dv O_NONBLOCK
162and
163.Dv O_ASYNC
164properties and the signal destination being inherited,
165but should set them explicitly using
166.Xr fcntl 2 ;
167.Fn accept4
168sets these properties consistently,
169but may not be fully portable across
170.Ux
171platforms.
172.Sh RETURN VALUES
173These calls return \-1 on error.
174If they succeed, they return a non-negative
175integer that is a descriptor for the accepted socket.
176.Sh ERRORS
177The
178.Fn accept
179and
180.Fn accept4
181system calls will fail if:
182.Bl -tag -width Er
183.It Bq Er EBADF
184The descriptor is invalid.
185.It Bq Er EINTR
186The
187.Fn accept
188operation was interrupted.
189.It Bq Er EMFILE
190The per-process descriptor table is full.
191.It Bq Er ENFILE
192The system file table is full.
193.It Bq Er ENOTSOCK
194The descriptor references a file, not a socket.
195.It Bq Er EINVAL
196.Xr listen 2
197has not been called on the socket descriptor.
198.It Bq Er EFAULT
199The
200.Fa addr
201argument is not in a writable part of the
202user address space.
203.It Bo Er EWOULDBLOCK Bc or Bq Er EAGAIN
204The socket is marked non-blocking and no connections
205are present to be accepted.
206.It Bq Er ECONNABORTED
207A connection arrived, but it was closed while waiting
208on the listen queue.
209.El
210.Pp
211The
212.Fn accept4
213system call will also fail if:
214.Bl -tag -width Er
215.It Bq Er EINVAL
216The
217.Fa flags
218argument is invalid.
219.El
220.Sh SEE ALSO
221.Xr bind 2 ,
222.Xr connect 2 ,
223.Xr getpeername 2 ,
224.Xr getsockname 2 ,
225.Xr listen 2 ,
226.Xr select 2 ,
227.Xr socket 2 ,
228.Xr accept_filter 9
229.Sh HISTORY
230The
231.Fn accept
232system call appeared in
233.Bx 4.2 .
234.Pp
235The
236.Fn accept4
237system call appeared in
238.Fx 10.0 .
239