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