xref: /xnu-12377.1.9/bsd/man/man2/accept.2 (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1*f6217f89SApple OSS Distributions.\"	$NetBSD: accept.2,v 1.7 1996/01/31 20:14:42 mycroft Exp $
2*f6217f89SApple OSS Distributions.\"
3*f6217f89SApple OSS Distributions.\" Copyright (c) 1983, 1990, 1991, 1993
4*f6217f89SApple OSS Distributions.\"	The Regents of the University of California.  All rights reserved.
5*f6217f89SApple OSS Distributions.\"
6*f6217f89SApple OSS Distributions.\" Redistribution and use in source and binary forms, with or without
7*f6217f89SApple OSS Distributions.\" modification, are permitted provided that the following conditions
8*f6217f89SApple OSS Distributions.\" are met:
9*f6217f89SApple OSS Distributions.\" 1. Redistributions of source code must retain the above copyright
10*f6217f89SApple OSS Distributions.\"    notice, this list of conditions and the following disclaimer.
11*f6217f89SApple OSS Distributions.\" 2. Redistributions in binary form must reproduce the above copyright
12*f6217f89SApple OSS Distributions.\"    notice, this list of conditions and the following disclaimer in the
13*f6217f89SApple OSS Distributions.\"    documentation and/or other materials provided with the distribution.
14*f6217f89SApple OSS Distributions.\" 3. All advertising materials mentioning features or use of this software
15*f6217f89SApple OSS Distributions.\"    must display the following acknowledgement:
16*f6217f89SApple OSS Distributions.\"	This product includes software developed by the University of
17*f6217f89SApple OSS Distributions.\"	California, Berkeley and its contributors.
18*f6217f89SApple OSS Distributions.\" 4. Neither the name of the University nor the names of its contributors
19*f6217f89SApple OSS Distributions.\"    may be used to endorse or promote products derived from this software
20*f6217f89SApple OSS Distributions.\"    without specific prior written permission.
21*f6217f89SApple OSS Distributions.\"
22*f6217f89SApple OSS Distributions.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*f6217f89SApple OSS Distributions.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*f6217f89SApple OSS Distributions.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*f6217f89SApple OSS Distributions.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*f6217f89SApple OSS Distributions.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*f6217f89SApple OSS Distributions.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*f6217f89SApple OSS Distributions.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*f6217f89SApple OSS Distributions.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*f6217f89SApple OSS Distributions.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*f6217f89SApple OSS Distributions.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*f6217f89SApple OSS Distributions.\" SUCH DAMAGE.
33*f6217f89SApple OSS Distributions.\"
34*f6217f89SApple OSS Distributions.\"     @(#)accept.2	8.2 (Berkeley) 12/11/93
35*f6217f89SApple OSS Distributions.\"
36*f6217f89SApple OSS Distributions.Dd March 18, 2015
37*f6217f89SApple OSS Distributions.Dt ACCEPT 2
38*f6217f89SApple OSS Distributions.Os BSD 4.2
39*f6217f89SApple OSS Distributions.Sh NAME
40*f6217f89SApple OSS Distributions.Nm accept
41*f6217f89SApple OSS Distributions.Nd accept a connection on a socket
42*f6217f89SApple OSS Distributions.Sh SYNOPSIS
43*f6217f89SApple OSS Distributions.Fd #include <sys/socket.h>
44*f6217f89SApple OSS Distributions.Ft int
45*f6217f89SApple OSS Distributions.Fo accept
46*f6217f89SApple OSS Distributions.Fa "int socket"
47*f6217f89SApple OSS Distributions.Fa "struct sockaddr *restrict address"
48*f6217f89SApple OSS Distributions.Fa "socklen_t *restrict address_len"
49*f6217f89SApple OSS Distributions.Fc
50*f6217f89SApple OSS Distributions.Sh DESCRIPTION
51*f6217f89SApple OSS DistributionsThe argument
52*f6217f89SApple OSS Distributions.Fa socket
53*f6217f89SApple OSS Distributionsis a socket that has been created with
54*f6217f89SApple OSS Distributions.Xr socket 2 ,
55*f6217f89SApple OSS Distributionsbound to an address with
56*f6217f89SApple OSS Distributions.Xr bind 2 ,
57*f6217f89SApple OSS Distributionsand is listening for connections after a
58*f6217f89SApple OSS Distributions.Xr listen 2 .
59*f6217f89SApple OSS Distributions.Fn accept
60*f6217f89SApple OSS Distributionsextracts the first connection request
61*f6217f89SApple OSS Distributionson the queue of pending connections, creates
62*f6217f89SApple OSS Distributionsa new socket with the same properties of
63*f6217f89SApple OSS Distributions.Fa socket ,
64*f6217f89SApple OSS Distributionsand allocates a new file descriptor
65*f6217f89SApple OSS Distributionsfor the socket.  If no pending connections are
66*f6217f89SApple OSS Distributionspresent on the queue, and the socket is not marked
67*f6217f89SApple OSS Distributionsas non-blocking,
68*f6217f89SApple OSS Distributions.Fn accept
69*f6217f89SApple OSS Distributionsblocks the caller until a connection is present.
70*f6217f89SApple OSS DistributionsIf the socket is marked non-blocking and no pending
71*f6217f89SApple OSS Distributionsconnections are present on the queue,
72*f6217f89SApple OSS Distributions.Fn accept
73*f6217f89SApple OSS Distributionsreturns an error as described below.
74*f6217f89SApple OSS DistributionsThe accepted socket
75*f6217f89SApple OSS Distributionsmay not be used
76*f6217f89SApple OSS Distributionsto accept more connections.  The original socket
77*f6217f89SApple OSS Distributions.Fa socket,
78*f6217f89SApple OSS Distributionsremains open.
79*f6217f89SApple OSS Distributions.Pp
80*f6217f89SApple OSS DistributionsThe argument
81*f6217f89SApple OSS Distributions.Fa address
82*f6217f89SApple OSS Distributionsis a result parameter that is filled in with
83*f6217f89SApple OSS Distributionsthe address of the connecting entity,
84*f6217f89SApple OSS Distributionsas known to the communications layer.
85*f6217f89SApple OSS DistributionsThe exact format of the
86*f6217f89SApple OSS Distributions.Fa address
87*f6217f89SApple OSS Distributionsparameter is determined by the domain in which the communication
88*f6217f89SApple OSS Distributionsis occurring.
89*f6217f89SApple OSS DistributionsThe
90*f6217f89SApple OSS Distributions.Fa address_len
91*f6217f89SApple OSS Distributionsis a value-result parameter; it should initially contain the
92*f6217f89SApple OSS Distributionsamount of space pointed to by
93*f6217f89SApple OSS Distributions.Fa address ;
94*f6217f89SApple OSS Distributionson return it will contain the actual length (in bytes) of the
95*f6217f89SApple OSS Distributionsaddress returned.
96*f6217f89SApple OSS DistributionsThis call
97*f6217f89SApple OSS Distributionsis used with connection-based socket types, currently with
98*f6217f89SApple OSS Distributions.Dv SOCK_STREAM .
99*f6217f89SApple OSS Distributions.Pp
100*f6217f89SApple OSS DistributionsIt is possible to
101*f6217f89SApple OSS Distributions.Xr select 2
102*f6217f89SApple OSS Distributionsa socket for the purposes of doing an
103*f6217f89SApple OSS Distributions.Fn accept
104*f6217f89SApple OSS Distributionsby selecting it for read.
105*f6217f89SApple OSS Distributions.Pp
106*f6217f89SApple OSS DistributionsFor certain protocols which require an explicit confirmation,
107*f6217f89SApple OSS Distributionssuch as
108*f6217f89SApple OSS Distributions.Tn ISO
109*f6217f89SApple OSS Distributionsor
110*f6217f89SApple OSS Distributions.Tn DATAKIT ,
111*f6217f89SApple OSS Distributions.Fn accept
112*f6217f89SApple OSS Distributionscan be thought of
113*f6217f89SApple OSS Distributionsas merely dequeuing the next connection
114*f6217f89SApple OSS Distributionsrequest and not implying confirmation.
115*f6217f89SApple OSS DistributionsConfirmation can be implied by a normal read or write on the new
116*f6217f89SApple OSS Distributionsfile descriptor, and rejection can be implied by closing the
117*f6217f89SApple OSS Distributionsnew socket.
118*f6217f89SApple OSS Distributions.Pp
119*f6217f89SApple OSS DistributionsOne can obtain user connection request data without confirming
120*f6217f89SApple OSS Distributionsthe connection by issuing a
121*f6217f89SApple OSS Distributions.Xr recvmsg 2
122*f6217f89SApple OSS Distributionscall with an
123*f6217f89SApple OSS Distributions.Fa msg_iovlen
124*f6217f89SApple OSS Distributionsof 0 and a non-zero
125*f6217f89SApple OSS Distributions.Fa msg_controllen ,
126*f6217f89SApple OSS Distributionsor by issuing a
127*f6217f89SApple OSS Distributions.Xr getsockopt 2
128*f6217f89SApple OSS Distributionsrequest.
129*f6217f89SApple OSS DistributionsSimilarly, one can provide user connection rejection information
130*f6217f89SApple OSS Distributionsby issuing a
131*f6217f89SApple OSS Distributions.Xr sendmsg 2
132*f6217f89SApple OSS Distributionscall with providing only the control information,
133*f6217f89SApple OSS Distributionsor by calling
134*f6217f89SApple OSS Distributions.Xr setsockopt 2 .
135*f6217f89SApple OSS Distributions.Sh RETURN VALUES
136*f6217f89SApple OSS DistributionsThe call returns \-1 on error and the global variable
137*f6217f89SApple OSS Distributions.Va errno
138*f6217f89SApple OSS Distributionsis set to indicate the error.
139*f6217f89SApple OSS DistributionsIf it succeeds, it returns a non-negative integer
140*f6217f89SApple OSS Distributionsthat is a descriptor for the accepted socket.
141*f6217f89SApple OSS Distributions.Sh ERRORS
142*f6217f89SApple OSS DistributionsThe
143*f6217f89SApple OSS Distributions.Fn accept
144*f6217f89SApple OSS Distributionssystem call will fail if:
145*f6217f89SApple OSS Distributions.Bl -tag -width Er
146*f6217f89SApple OSS Distributions.\" ==========
147*f6217f89SApple OSS Distributions.It Bq Er EBADF
148*f6217f89SApple OSS Distributions.Fa socket
149*f6217f89SApple OSS Distributionsis not a valid file descriptor.
150*f6217f89SApple OSS Distributions.\" ==========
151*f6217f89SApple OSS Distributions.It Bq Er ECONNABORTED
152*f6217f89SApple OSS DistributionsThe connection to
153*f6217f89SApple OSS Distributions.Fa socket
154*f6217f89SApple OSS Distributionshas been aborted.
155*f6217f89SApple OSS Distributions.\" ==========
156*f6217f89SApple OSS Distributions.It Bq Er EFAULT
157*f6217f89SApple OSS DistributionsThe
158*f6217f89SApple OSS Distributions.Fa address
159*f6217f89SApple OSS Distributionsparameter is not in a writable part of the
160*f6217f89SApple OSS Distributionsuser address space.
161*f6217f89SApple OSS Distributions.\" ==========
162*f6217f89SApple OSS Distributions.It Bq Er EINTR
163*f6217f89SApple OSS DistributionsThe
164*f6217f89SApple OSS Distributions.Fn accept
165*f6217f89SApple OSS Distributionssystem call was terminated by a signal.
166*f6217f89SApple OSS Distributions.\" ==========
167*f6217f89SApple OSS Distributions.It Bq Er EINVAL
168*f6217f89SApple OSS Distributions.Fa socket
169*f6217f89SApple OSS Distributionsis unwilling to accept connections.
170*f6217f89SApple OSS Distributions.\" ==========
171*f6217f89SApple OSS Distributions.It Bq Er EMFILE
172*f6217f89SApple OSS DistributionsThe per-process descriptor table is full.
173*f6217f89SApple OSS Distributions.\" ==========
174*f6217f89SApple OSS Distributions.It Bq Er ENFILE
175*f6217f89SApple OSS DistributionsThe system file table is full.
176*f6217f89SApple OSS Distributions.\" ==========
177*f6217f89SApple OSS Distributions.It Bq Er ENOMEM
178*f6217f89SApple OSS DistributionsInsufficient memory was available to complete the operation.
179*f6217f89SApple OSS Distributions.\" ==========
180*f6217f89SApple OSS Distributions.It Bq Er ENOTSOCK
181*f6217f89SApple OSS Distributions.Fa socket
182*f6217f89SApple OSS Distributionsreferences a file type other than a socket.
183*f6217f89SApple OSS Distributions.\" ==========
184*f6217f89SApple OSS Distributions.It Bq Er EOPNOTSUPP
185*f6217f89SApple OSS Distributions.Fa socket
186*f6217f89SApple OSS Distributionsis not of type
187*f6217f89SApple OSS Distributions.Dv SOCK_STREAM
188*f6217f89SApple OSS Distributionsand thus does not accept connections.
189*f6217f89SApple OSS Distributions.\" ==========
190*f6217f89SApple OSS Distributions.It Bq Er EWOULDBLOCK
191*f6217f89SApple OSS Distributions.Fa socket
192*f6217f89SApple OSS Distributionsis marked as non-blocking and no connections are present to be accepted.
193*f6217f89SApple OSS Distributions.El
194*f6217f89SApple OSS Distributions.Sh LEGACY SYNOPSIS
195*f6217f89SApple OSS Distributions.Fd #include <sys/types.h>
196*f6217f89SApple OSS Distributions.Fd #include <sys/socket.h>
197*f6217f89SApple OSS Distributions.Pp
198*f6217f89SApple OSS DistributionsThe include file
199*f6217f89SApple OSS Distributions.In sys/types.h
200*f6217f89SApple OSS Distributionsis necessary.
201*f6217f89SApple OSS Distributions.Sh SEE ALSO
202*f6217f89SApple OSS Distributions.Xr bind 2 ,
203*f6217f89SApple OSS Distributions.Xr connect 2 ,
204*f6217f89SApple OSS Distributions.Xr connectx 2 ,
205*f6217f89SApple OSS Distributions.Xr listen 2 ,
206*f6217f89SApple OSS Distributions.Xr select 2 ,
207*f6217f89SApple OSS Distributions.Xr socket 2 ,
208*f6217f89SApple OSS Distributions.Xr compat 5
209*f6217f89SApple OSS Distributions.Sh HISTORY
210*f6217f89SApple OSS DistributionsThe
211*f6217f89SApple OSS Distributions.Fn accept
212*f6217f89SApple OSS Distributionsfunction appeared in
213*f6217f89SApple OSS Distributions.Bx 4.2 .
214