xref: /freebsd-12.1/lib/libc/sys/accept.2 (revision 42992a3a)
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.\" 4. 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.\" $FreeBSD$
30.\"
31.Dd December 11, 1993
32.Dt ACCEPT 2
33.Os
34.Sh NAME
35.Nm accept
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.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
60property from the original socket
61.Fa s .
62.Pp
63If no pending connections are
64present on the queue, and the original socket
65is not marked as non-blocking,
66.Fn accept
67blocks the caller until a connection is present.
68If the original socket
69is marked non-blocking and no pending
70connections are present on the queue,
71.Fn accept
72returns an error as described below.
73The accepted socket
74may not be used
75to accept more connections.
76The original socket
77.Fa s
78remains open.
79.Pp
80The argument
81.Fa addr
82is a result argument 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
87argument is determined by the domain in which the communication
88is occurring.
89A null pointer may be specified for
90.Fa addr
91if the address information is not desired;
92in this case,
93.Fa addrlen
94is not used and should also be null.
95Otherwise, the
96.Fa addrlen
97argument
98is a value-result argument; it should initially contain the
99amount of space pointed to by
100.Fa addr ;
101on return it will contain the actual length (in bytes) of the
102address returned.
103This call
104is used with connection-based socket types, currently with
105.Dv SOCK_STREAM .
106.Pp
107It is possible to
108.Xr select 2
109a socket for the purposes of doing an
110.Fn accept
111by selecting it for read.
112.Pp
113For certain protocols which require an explicit confirmation,
114such as
115.Tn ISO
116or
117.Tn DATAKIT ,
118.Fn accept
119can be thought of
120as merely dequeueing the next connection
121request and not implying confirmation.
122Confirmation can be implied by a normal read or write on the new
123file descriptor, and rejection can be implied by closing the
124new socket.
125.Pp
126For some applications, performance may be enhanced by using an
127.Xr accept_filter 9
128to pre-process incoming connections.
129.Pp
130Portable programs should not rely on the
131.Dv O_NONBLOCK
132property being inherited.
133.Sh RETURN VALUES
134The call returns \-1 on error.
135If it succeeds, it returns a non-negative
136integer that is a descriptor for the accepted socket.
137.Sh ERRORS
138The
139.Fn accept
140system call will fail if:
141.Bl -tag -width Er
142.It Bq Er EBADF
143The descriptor is invalid.
144.It Bq Er EINTR
145The
146.Fn accept
147operation was interrupted.
148.It Bq Er EMFILE
149The per-process descriptor table is full.
150.It Bq Er ENFILE
151The system file table is full.
152.It Bq Er ENOTSOCK
153The descriptor references a file, not a socket.
154.It Bq Er EINVAL
155.Xr listen 2
156has not been called on the socket descriptor.
157.It Bq Er EINVAL
158The
159.Fa addrlen
160argument is negative.
161.It Bq Er EFAULT
162The
163.Fa addr
164argument is not in a writable part of the
165user address space.
166.It Bq Er EWOULDBLOCK
167The socket is marked non-blocking and no connections
168are present to be accepted.
169.It Bq Er ECONNABORTED
170A connection arrived, but it was closed while waiting
171on the listen queue.
172.El
173.Sh SEE ALSO
174.Xr bind 2 ,
175.Xr connect 2 ,
176.Xr getpeername 2 ,
177.Xr getsockname 2 ,
178.Xr listen 2 ,
179.Xr select 2 ,
180.Xr socket 2 ,
181.Xr accept_filter 9
182.Sh HISTORY
183The
184.Fn accept
185system call appeared in
186.Bx 4.2 .
187