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