xref: /freebsd-14.2/lib/libc/sys/accept.2 (revision fffcbbcd)
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. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)accept.2	8.2 (Berkeley) 12/11/93
33.\" $FreeBSD$
34.\"
35.Dd December 11, 1993
36.Dt ACCEPT 2
37.Os
38.Sh NAME
39.Nm accept
40.Nd accept a connection on a socket
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.Fd #include <sys/types.h>
45.Fd #include <sys/socket.h>
46.Ft int
47.Fn accept "int s" "struct sockaddr *addr" "socklen_t *addrlen"
48.Sh DESCRIPTION
49The argument
50.Fa s
51is a socket that has been created with
52.Xr socket 2 ,
53bound to an address with
54.Xr bind 2 ,
55and is listening for connections after a
56.Xr listen 2 .
57The
58.Fn accept
59call
60extracts the first connection request
61on the queue of pending connections, creates
62a new socket with the same properties as
63.Fa s ,
64and allocates a new file descriptor
65for the socket.  If no pending connections are
66present on the queue, and the socket is not marked
67as non-blocking,
68.Fn accept
69blocks the caller until a connection is present.
70If the socket is marked non-blocking and no pending
71connections are present on the queue,
72.Fn accept
73returns an error as described below.
74The accepted socket
75may not be used
76to accept more connections.  The original socket
77.Fa s
78remains open.
79.Pp
80The argument
81.Fa addr
82is a result parameter that is filled-in with
83the address of the connecting entity,
84as known to the communications layer.
85The exact format of the
86.Fa addr
87parameter is determined by the domain in which the communication
88is occurring.
89The
90.Fa addrlen
91is a value-result parameter; it should initially contain the
92amount of space pointed to by
93.Fa addr ;
94on return it will contain the actual length (in bytes) of the
95address returned.
96This call
97is used with connection-based socket types, currently with
98.Dv SOCK_STREAM .
99.Pp
100It is possible to
101.Xr select 2
102a socket for the purposes of doing an
103.Fn accept
104by selecting it for read.
105.Pp
106For certain protocols which require an explicit confirmation,
107such as
108.Tn ISO
109or
110.Tn DATAKIT ,
111.Fn accept
112can be thought of
113as merely dequeueing the next connection
114request and not implying confirmation.
115Confirmation can be implied by a normal read or write on the new
116file descriptor, and rejection can be implied by closing the
117new socket.
118.Pp
119One can obtain user connection request data without confirming
120the connection by issuing a
121.Xr recvmsg 2
122call with an
123.Fa msg_iovlen
124of 0 and a non-zero
125.Fa msg_controllen ,
126or by issuing a
127.Xr getsockopt 2
128request.
129Similarly, one can provide user connection rejection information
130by issuing a
131.Xr sendmsg 2
132call providing only the control information,
133or by calling
134.Xr setsockopt 2 .
135.Sh IMPLEMENTATION NOTES
136In the non-threaded library
137.Fn accept
138is implemented as the
139.Va accept
140syscall.
141.Pp
142In the threaded library, the
143.Va accept
144syscall is assembled to
145.Fn _thread_sys_accept
146and
147.Fn accept
148is implemented as a function which locks
149.Fa s
150for read and write, then calls
151.Fn _thread_sys_accept .
152If the call to
153.Fn _thread_sys_accept
154would block, a context switch is performed.
155Before returning,
156.Fn accept
157unlocks
158.Fa s .
159.Sh RETURN VALUES
160The call returns \-1 on error.  If it succeeds, it returns a non-negative
161integer that is a descriptor for the accepted socket.
162.Sh ERRORS
163The
164.Fn accept
165will fail if:
166.Bl -tag -width Er
167.It Bq Er EBADF
168The descriptor is invalid.
169.It Bq Er EINTR
170The
171.Fn accept
172operation was interrupted.
173.It Bq Er EMFILE
174The per-process descriptor table is full.
175.It Bq Er ENFILE
176The system file table is full.
177.It Bq Er ENOTSOCK
178The descriptor references a file, not a socket.
179.It Bq Er EINVAL
180.Xr listen 2
181has not been called on the socket descriptor.
182.It Bq Er EFAULT
183The
184.Fa addr
185parameter is not in a writable part of the
186user address space.
187.It Bq Er EWOULDBLOCK
188The socket is marked non-blocking and no connections
189are present to be accepted.
190.It Bq Er ECONNABORTED
191A connection arrived, but it was closed while waiting
192on the listen queue.
193.El
194.Sh SEE ALSO
195.Xr bind 2 ,
196.Xr connect 2 ,
197.Xr getpeername 2 ,
198.Xr listen 2 ,
199.Xr select 2 ,
200.Xr socket 2
201.Sh HISTORY
202The
203.Fn accept
204function appeared in
205.Bx 4.2 .
206