xref: /dragonfly/lib/libc/sys/accept.2 (revision f746689a)
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: src/lib/libc/sys/accept.2,v 1.10.2.11 2002/05/09 02:24:40 silby Exp $
34.\" $DragonFly: src/lib/libc/sys/accept.2,v 1.4 2006/06/25 10:55:51 corecode Exp $
35.\"
36.Dd December 11, 1993
37.Dt ACCEPT 2
38.Os
39.Sh NAME
40.Nm accept
41.Nd accept a connection on a socket
42.Sh LIBRARY
43.Lb libc
44.Sh SYNOPSIS
45.In sys/types.h
46.In sys/socket.h
47.Ft int
48.Fn accept "int s" "struct sockaddr *addr" "socklen_t *addrlen"
49.Sh DESCRIPTION
50The argument
51.Fa s
52is a socket that has been created with
53.Xr socket 2 ,
54bound to an address with
55.Xr bind 2 ,
56and is listening for connections after a
57.Xr listen 2 .
58The
59.Fn accept
60call
61extracts the first connection request
62on the queue of pending connections, creates
63a new socket with the same properties as
64.Fa s ,
65and allocates a new file descriptor
66for the socket.  If no pending connections are
67present on the queue, and the socket is not marked
68as non-blocking,
69.Fn accept
70blocks the caller until a connection is present.
71If the socket is marked non-blocking and no pending
72connections are present on the queue,
73.Fn accept
74returns an error as described below.
75The accepted socket
76may not be used
77to accept more connections.  The original socket
78.Fa s
79remains open.
80.Pp
81The argument
82.Fa addr
83is a result parameter that is filled-in with
84the address of the connecting entity,
85as known to the communications layer.
86The exact format of the
87.Fa addr
88parameter is determined by the domain in which the communication
89is occurring.
90To ensure that the returned address fits,
91.Fa *addr
92should have a size of at least
93.Ft sizeof(struct sockaddr_storage) .
94The
95.Fa addrlen
96is a value-result parameter; it should initially contain the
97amount of space pointed to by
98.Fa addr ;
99on return it will contain the actual length (in bytes) of the
100address returned.
101This call
102is used with connection-based socket types, currently with
103.Dv SOCK_STREAM .
104.Pp
105It is possible to
106.Xr select 2
107a socket for the purposes of doing an
108.Fn accept
109by selecting it for read.
110.Pp
111For certain protocols which require an explicit confirmation,
112such as
113.Tn ISO
114or
115.Tn DATAKIT ,
116.Fn accept
117can be thought of
118as merely dequeueing the next connection
119request and not implying confirmation.
120Confirmation can be implied by a normal read or write on the new
121file descriptor, and rejection can be implied by closing the
122new socket.
123.Pp
124For some applications, performance may be enhanced by using an
125.Xr accept_filter 9
126to pre-process incoming connections.
127.Sh RETURN VALUES
128The call returns \-1 on error.  If it succeeds, it returns a non-negative
129integer that is a descriptor for the accepted socket.
130.Sh ERRORS
131The
132.Fn accept
133will fail if:
134.Bl -tag -width Er
135.It Bq Er EBADF
136The descriptor is invalid.
137.It Bq Er EINTR
138The
139.Fn accept
140operation was interrupted.
141.It Bq Er EMFILE
142The per-process descriptor table is full.
143.It Bq Er ENFILE
144The system file table is full.
145.It Bq Er ENOTSOCK
146The descriptor references a file, not a socket.
147.It Bq Er EINVAL
148.Xr listen 2
149has not been called on the socket descriptor.
150.It Bq Er EFAULT
151The
152.Fa addr
153parameter is not in a writable part of the
154user address space.
155.It Bq Er EWOULDBLOCK
156The socket is marked non-blocking and no connections
157are present to be accepted.
158.It Bq Er ECONNABORTED
159A connection arrived, but it was closed while waiting
160on the listen queue.
161.El
162.Sh SEE ALSO
163.Xr bind 2 ,
164.Xr connect 2 ,
165.Xr getpeername 2 ,
166.Xr listen 2 ,
167.Xr select 2 ,
168.Xr socket 2 ,
169.Xr accept_filter 9
170.Sh HISTORY
171The
172.Fn accept
173function appeared in
174.Bx 4.2 .
175