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