1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
'\" te
.\" Copyright 1989 AT&T
.\" Copyright (C) 2002, Sun Microsystems, Inc. All Rights Reserved
.\" Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH ACCEPT 3SOCKET "Apr 19, 2013"
.SH NAME
accept \- accept a connection on a socket
.SH SYNOPSIS
.LP
.nf
\fBcc\fR [ \fIflag\fR ... ] \fIfile\fR ... \fB-lsocket\fR \fB -lnsl \fR [ \fIlibrary\fR ... ]
#include <sys/types.h>
#include <sys/socket.h>
\fBint\fR \fBaccept\fR(\fBint\fR \fIs\fR, \fBstruct sockaddr *\fR\fIaddr\fR, \fBsocklen_t *\fR\fIaddrlen\fR);
\fBint\fR \fBaccept4\fR(\fBint\fR \fIs\fR, \fBstruct sockaddr *\fR\fIaddr\fR, \fBsocklen_t *\fR\fIaddrlen\fR,
\fBint\fR \fIflags\fR);
.fi
.SH DESCRIPTION
.sp
.LP
The argument \fIs\fR is a socket that has been created with
\fBsocket\fR(3SOCKET) and bound to an address with \fBbind\fR(3SOCKET), and
that is listening for connections after a call to \fBlisten\fR(3SOCKET). The
\fBaccept()\fR function extracts the first connection on the queue of pending
connections, creates a new socket with the properties of \fIs\fR, and allocates
a new file descriptor, \fIns\fR, for the socket. If no pending connections are
present on the queue and the socket is not marked as non-blocking,
\fBaccept()\fR blocks the caller until a connection is present. If the socket
is marked as non-blocking and no pending connections are present on the queue,
\fBaccept()\fR returns an error as described below. The \fBaccept()\fR
function uses the \fBnetconfig\fR(4) file to determine the \fBSTREAMS\fR device
file name associated with \fIs\fR. This is the device on which the connect
indication will be accepted. The accepted socket, \fIns\fR, is used to read and
write data to and from the socket that connected to \fIns\fR. It is not used to
accept more connections. The original socket (\fIs\fR) remains open for
accepting further connections.
.sp
.LP
The argument \fIaddr\fR is a result parameter that is filled in with the
address of the connecting entity as it is known to the communications layer.
The exact format of the \fIaddr\fR parameter is determined by the domain in
which the communication occurs.
.sp
.LP
The argument \fIaddrlen\fR is a value-result parameter. Initially, it contains
the amount of space pointed to by \fIaddr\fR; on return it contains the length
in bytes of the address returned.
.sp
.LP
The \fBaccept()\fR function is used with connection-based socket types,
currently with \fBSOCK_STREAM\fR.
.sp
.LP
The \fBaccept4()\fR function allows flags that control the behavior of a
successfully accepted socket. If \fIflags\fR is 0, \fBaccept4()\fR acts
identically to \fBaccept()\fR. Values for \fIflags\fR are constructed by
a bitwise-inclusive-OR of flags from the following list, defined in
<sys/socketvar.h>.
.sp
.ne 2
.na
\fB\fBSOCK_CLOEXEC\fR\fR
.ad
.RS 12n
The accepted socket will have the FD_CLOEXEC flag set as if \fBfcntl()\fR
was called on it. This flag is set before the socket is passed to the
caller thus avoiding the race condition between \fBaccept()\fR and
\fBfcntl()\fR. See, \fBO_CLOEXEC\fR in \fBopen(2)\fR for more details.
.RE
.sp
.ne 2
.na
\fB\fBSOCK_NDELAY\fR\fR
.ad
.RS 12n
The accepted socket will have the \fBO_NDELAY\fR flag set as if \fBfcntl()\fR
was called on it. This sets the socket into non-blocking mode. See
\fBO_NDELAY\fR in \fBfcntl.h(3HEAD)\fR for more details.
.RE
.sp
.ne 2
.na
\fB\fBSOCK_NONBLOCK\fR\fR
.ad
.RS 12n
The accepted socket will have the \fBO_NONBLOCK\fR flag set as if
\fBfcntl()\fR was called on it. This sets the socket into non-blocking mode
(POSIX; see \fBstandards(5)\fR). See \fBO_NONBLOCK\fR in \fBfcntl.h(3HEAD)\fR
for more details.
.RE
.sp
.LP
It is possible to \fBselect\fR(3C) or \fBpoll\fR(2) a socket for the purpose of
an \fBaccept()\fR by selecting or polling it for a read. However, this will
only indicate when a connect indication is pending; it is still necessary to
call \fBaccept()\fR.
.SH RETURN VALUES
.sp
.LP
The \fBaccept()\fR function returns \fB\(mi1\fR on error. If it succeeds, it
returns a non-negative integer that is a descriptor for the accepted socket.
.SH ERRORS
.sp
.LP
\fBaccept()\fR and \fBaccept4()\fR will fail if:
.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 16n
The descriptor is invalid.
.RE
.sp
.ne 2
.na
\fB\fBECONNABORTED\fR\fR
.ad
.RS 16n
The remote side aborted the connection before the \fBaccept()\fR operation
completed.
.RE
.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 16n
The \fIaddr\fR parameter or the \fIaddrlen\fR parameter is invalid.
.RE
.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 16n
The \fBaccept()\fR attempt was interrupted by the delivery of a signal.
.RE
.sp
.ne 2
.na
\fB\fBEMFILE\fR\fR
.ad
.RS 16n
The per-process descriptor table is full.
.RE
.sp
.ne 2
.na
\fB\fBENODEV\fR\fR
.ad
.RS 16n
The protocol family and type corresponding to \fIs\fR could not be found in
the \fBnetconfig\fR file.
.RE
.sp
.ne 2
.na
\fB\fBENOMEM\fR\fR
.ad
.RS 16n
There was insufficient user memory available to complete the operation.
.RE
.sp
.ne 2
.na
\fB\fBENOSR\fR\fR
.ad
.RS 16n
There were insufficient \fBSTREAMS\fR resources available to complete the
operation.
.RE
.sp
.ne 2
.na
\fB\fBENOTSOCK\fR\fR
.ad
.RS 16n
The descriptor does not reference a socket.
.RE
.sp
.ne 2
.na
\fB\fBEOPNOTSUPP\fR\fR
.ad
.RS 16n
The referenced socket is not of type \fBSOCK_STREAM\fR.
.RE
.sp
.ne 2
.na
\fB\fBEPROTO\fR\fR
.ad
.RS 16n
A protocol error has occurred; for example, the \fBSTREAMS\fR protocol stack
has not been initialized or the connection has already been released.
.RE
.sp
.ne 2
.na
\fB\fBEWOULDBLOCK\fR\fR
.ad
.RS 16n
The socket is marked as non-blocking and no connections are present to be
accepted.
.RE
.sp
.LP
\fBAdditionally, \fBaccept4()\fR will fail if:
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 16n
The \fIflags\fR value is invalid. The \fIflags\fR argument can only be the
bitwise inclusive-OR of \fBSOCK_CLOEXEC\fR, \fBSOCK_NONBLOCK\fR, and
\fBSOCK_NDELAY\fR.
.RE
.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp
.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE ATTRIBUTE VALUE
_
MT-Level Safe
.TE
.SH SEE ALSO
.sp
.LP
\fBpoll\fR(2), \fBbind\fR(3SOCKET), \fBconnect\fR(3SOCKET),
\fBlisten\fR(3SOCKET), \fBselect\fR(3C), \fBsocket.h\fR(3HEAD),
\fBsocket\fR(3SOCKET), \fBnetconfig\fR(4), \fBattributes\fR(5),
\fBfcntl.h(3HEAD)\fR, \fBfcntl(2)\fR, \fBstandards(5)\fR
|