xref: /netbsd-src/lib/libc/sys/accept.2 (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1.\"	$NetBSD: accept.2,v 1.15 2001/05/19 17:23:39 jdolecek Exp $
2.\"
3.\" Copyright (c) 1983, 1990, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)accept.2	8.2 (Berkeley) 12/11/93
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.Fd #include <sys/types.h>
46.Fd #include <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
60argument
61extracts the first connection request
62on the queue of pending connections, creates
63a new socket with the same properties of
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.
90The
91.Fa addrlen
92is a value-result parameter; it should initially contain the
93amount of space pointed to by
94.Fa addr ;
95on return it will contain the actual length (in bytes) of the
96address returned.
97This call
98is used with connection-based socket types, currently with
99.Dv SOCK_STREAM .
100.Pp
101It is possible to
102.Xr select 2
103or
104.Xr poll 2
105a socket for the purposes of doing an
106.Fn accept
107by selecting or polling it for read.
108.Pp
109For certain protocols which require an explicit confirmation,
110such as
111.Tn ISO
112or
113.Tn DATAKIT ,
114.Fn accept
115can be thought of
116as merely dequeuing the next connection
117request and not implying confirmation.
118Confirmation can be implied by a normal read or write on the new
119file descriptor, and rejection can be implied by closing the
120new socket.
121.Pp
122One can obtain user connection request data without confirming
123the connection by issuing a
124.Xr recvmsg 2
125call with an
126.Fa msg_iovlen
127of 0 and a non-zero
128.Fa msg_controllen ,
129or by issuing a
130.Xr getsockopt 2
131request.
132Similarly, one can provide user connection rejection information
133by issuing a
134.Xr sendmsg 2
135call with providing only the control information,
136or by calling
137.Xr setsockopt 2 .
138.Sh RETURN VALUES
139The call returns \-1 on error.  If it succeeds, it returns a non-negative
140integer that is a descriptor for the accepted socket.
141.Sh ERRORS
142The
143.Fn accept
144will fail if:
145.Bl -tag -width Er
146.It Bq Er EBADF
147The descriptor is invalid.
148.It Bq Er EINVAL
149The socket has not been set up to accept connections (using
150.Xr bind 2
151and
152.Xr listen 2 ).
153.It Bq Er ENOTSOCK
154The descriptor references a file, not a socket.
155.It Bq Er EOPNOTSUPP
156The referenced socket is not of type
157.Dv SOCK_STREAM .
158.It Bq Er EFAULT
159The
160.Fa addr
161parameter is not in a writable part of the
162user address space.
163.It Bq Er EAGAIN
164The socket is marked non-blocking and no connections
165are present to be accepted.
166.It Bq Er EMFILE
167The per-process descriptor table is full.
168.It Bq Er ENFILE
169The system file table is full.
170.It Bq Er ECONNABORTED
171A connection has been aborted.
172.El
173.Sh SEE ALSO
174.Xr bind 2 ,
175.Xr connect 2 ,
176.Xr listen 2 ,
177.Xr poll 2 ,
178.Xr select 2 ,
179.Xr socket 2
180.Sh HISTORY
181The
182.Fn accept
183function appeared in
184.Bx 4.2 .
185