xref: /freebsd/lib/libsys/accept.2 (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 October 9, 2014
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.
88.Pp
89If no pending connections are
90present on the queue, and the original socket
91is not marked as non-blocking,
92.Fn accept
93blocks the caller until a connection is present.
94If the original socket
95is marked non-blocking and no pending
96connections are present on the queue,
97.Fn accept
98returns an error as described below.
99The accepted socket
100may not be used
101to accept more connections.
102The original socket
103.Fa s
104remains open.
105.Pp
106The argument
107.Fa addr
108is a result argument that is filled-in with
109the address of the connecting entity,
110as known to the communications layer.
111The exact format of the
112.Fa addr
113argument is determined by the domain in which the communication
114is occurring.
115A null pointer may be specified for
116.Fa addr
117if the address information is not desired;
118in this case,
119.Fa addrlen
120is not used and should also be null.
121Otherwise, the
122.Fa addrlen
123argument
124is a value-result argument; it should initially contain the
125amount of space pointed to by
126.Fa addr ;
127on return it will contain the actual length (in bytes) of the
128address returned.
129This call
130is used with connection-based socket types, currently with
131.Dv SOCK_STREAM .
132.Pp
133It is possible to
134.Xr select 2
135a socket for the purposes of doing an
136.Fn accept
137by selecting it for read.
138.Pp
139For certain protocols which require an explicit confirmation,
140such as
141.Tn ISO
142or
143.Tn DATAKIT ,
144.Fn accept
145can be thought of
146as merely dequeueing the next connection
147request and not implying confirmation.
148Confirmation can be implied by a normal read or write on the new
149file descriptor, and rejection can be implied by closing the
150new socket.
151.Pp
152For some applications, performance may be enhanced by using an
153.Xr accept_filter 9
154to pre-process incoming connections.
155.Pp
156When using
157.Fn accept ,
158portable programs should not rely on the
159.Dv O_NONBLOCK
160and
161.Dv O_ASYNC
162properties and the signal destination being inherited,
163but should set them explicitly using
164.Xr fcntl 2 ;
165.Fn accept4
166sets these properties consistently,
167but may not be fully portable across
168.Ux
169platforms.
170.Sh RETURN VALUES
171These calls return \-1 on error.
172If they succeed, they return a non-negative
173integer that is a descriptor for the accepted socket.
174.Sh ERRORS
175The
176.Fn accept
177and
178.Fn accept4
179system calls will fail if:
180.Bl -tag -width Er
181.It Bq Er EBADF
182The descriptor is invalid.
183.It Bq Er EINTR
184The
185.Fn accept
186operation was interrupted.
187.It Bq Er EMFILE
188The per-process descriptor table is full.
189.It Bq Er ENFILE
190The system file table is full.
191.It Bq Er ENOTSOCK
192The descriptor references a file, not a socket.
193.It Bq Er EINVAL
194.Xr listen 2
195has not been called on the socket descriptor.
196.It Bq Er EFAULT
197The
198.Fa addr
199argument is not in a writable part of the
200user address space.
201.It Bo Er EWOULDBLOCK Bc or Bq Er EAGAIN
202The socket is marked non-blocking and no connections
203are present to be accepted.
204.It Bq Er ECONNABORTED
205A connection arrived, but it was closed while waiting
206on the listen queue.
207.El
208.Pp
209The
210.Fn accept4
211system call will also fail if:
212.Bl -tag -width Er
213.It Bq Er EINVAL
214The
215.Fa flags
216argument is invalid.
217.El
218.Sh SEE ALSO
219.Xr bind 2 ,
220.Xr connect 2 ,
221.Xr getpeername 2 ,
222.Xr getsockname 2 ,
223.Xr listen 2 ,
224.Xr select 2 ,
225.Xr socket 2 ,
226.Xr accept_filter 9
227.Sh HISTORY
228The
229.Fn accept
230system call appeared in
231.Bx 4.2 .
232.Pp
233The
234.Fn accept4
235system call appeared in
236.Fx 10.0 .
237