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