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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
.\" Copyright (c) 1983, 1990, 1991 The Regents of the University of California.
.\" All rights reserved.
.\"
.\" SPDX-License-Identifier: BSD-4-Clause-UC
.\"
.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
.\" Modified 1996-10-21 by Eric S. Raymond <esr@thyrsus.com>
.\" Modified 1998-2000 by Andi Kleen to match Linux 2.2 reality
.\" Modified 2002-04-23 by Roger Luethi <rl@hellgate.ch>
.\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
.\" 2008-12-04, mtk, Add documentation of accept4()
.\"
.TH accept 2 2024-05-02 "Linux man-pages 6.8"
.SH NAME
accept, accept4 \- accept a connection on a socket
.SH LIBRARY
Standard C library
.RI ( libc ", " \-lc )
.SH SYNOPSIS
.nf
.B #include <sys/socket.h>
.P
.BI "int accept(int " sockfd ", struct sockaddr *_Nullable restrict " addr ,
.BI " socklen_t *_Nullable restrict " addrlen );
.P
.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
.B #include <sys/socket.h>
.P
.BI "int accept4(int " sockfd ", struct sockaddr *_Nullable restrict " addr ,
.BI " socklen_t *_Nullable restrict " addrlen ", int " flags );
.fi
.SH DESCRIPTION
The
.BR accept ()
system call is used with connection-based socket types
.RB ( SOCK_STREAM ,
.BR SOCK_SEQPACKET ).
It extracts the first connection request on the queue of pending
connections for the listening socket,
.IR sockfd ,
creates a new connected socket, and returns a new file
descriptor referring to that socket.
The newly created socket is not in the listening state.
The original socket
.I sockfd
is unaffected by this call.
.P
The argument
.I sockfd
is a socket that has been created with
.BR socket (2),
bound to a local address with
.BR bind (2),
and is listening for connections after a
.BR listen (2).
.P
The argument
.I addr
is a pointer to a
.I sockaddr
structure.
This structure is filled in with the address of the peer socket,
as known to the communications layer.
The exact format of the address returned
.I addr
is determined by the socket's address family (see
.BR socket (2)
and the respective protocol man pages).
When
.I addr
is NULL, nothing is filled in; in this case,
.I addrlen
is not used, and should also be NULL.
.P
The
.I addrlen
argument is a value-result argument:
the caller must initialize it to contain the
size (in bytes) of the structure pointed to by
.IR addr ;
on return it will contain the actual size of the peer address.
.P
The returned address is truncated if the buffer provided is too small;
in this case,
.I addrlen
will return a value greater than was supplied to the call.
.P
If no pending
connections are present on the queue, and the socket is not marked as
nonblocking,
.BR accept ()
blocks the caller until a connection is present.
If the socket is marked
nonblocking and no pending connections are present on the queue,
.BR accept ()
fails with the error
.B EAGAIN
or
.BR EWOULDBLOCK .
.P
In order to be notified of incoming connections on a socket, you can use
.BR select (2),
.BR poll (2),
or
.BR epoll (7).
A readable event will be delivered when a new connection is attempted and you
may then call
.BR accept ()
to get a socket for that connection.
Alternatively, you can set the socket to deliver
.B SIGIO
when activity occurs on a socket; see
.BR socket (7)
for details.
.P
If
.I flags
is 0, then
.BR accept4 ()
is the same as
.BR accept ().
The following values can be bitwise ORed in
.I flags
to obtain different behavior:
.TP 16
.B SOCK_NONBLOCK
Set the
.B O_NONBLOCK
file status flag on the open file description (see
.BR open (2))
referred to by the new file descriptor.
Using this flag saves extra calls to
.BR fcntl (2)
to achieve the same result.
.TP
.B SOCK_CLOEXEC
Set the close-on-exec
.RB ( FD_CLOEXEC )
flag on the new file descriptor.
See the description of the
.B O_CLOEXEC
flag in
.BR open (2)
for reasons why this may be useful.
.SH RETURN VALUE
On success,
these system calls return a file descriptor
for the accepted socket (a nonnegative integer).
On error, \-1 is returned,
.I errno
is set to indicate the error, and
.I addrlen
is left unchanged.
.SS Error handling
Linux
.BR accept ()
(and
.BR accept4 ())
passes already-pending network errors on the new socket
as an error code from
.BR accept ().
This behavior differs from other BSD socket
implementations.
For reliable operation the application should detect
the network errors defined for the protocol after
.BR accept ()
and treat
them like
.B EAGAIN
by retrying.
In the case of TCP/IP, these are
.BR ENETDOWN ,
.BR EPROTO ,
.BR ENOPROTOOPT ,
.BR EHOSTDOWN ,
.BR ENONET ,
.BR EHOSTUNREACH ,
.BR EOPNOTSUPP ,
and
.BR ENETUNREACH .
.SH ERRORS
.TP
.BR EAGAIN " or " EWOULDBLOCK
.\" Actually EAGAIN on Linux
The socket is marked nonblocking and no connections are
present to be accepted.
POSIX.1-2001 and POSIX.1-2008
allow either error to be returned for this case,
and do not require these constants to have the same value,
so a portable application should check for both possibilities.
.TP
.B EBADF
.I sockfd
is not an open file descriptor.
.TP
.B ECONNABORTED
A connection has been aborted.
.TP
.B EFAULT
The
.I addr
argument is not in a writable part of the user address space.
.TP
.B EINTR
The system call was interrupted by a signal that was caught
before a valid connection arrived; see
.BR signal (7).
.TP
.B EINVAL
Socket is not listening for connections, or
.I addrlen
is invalid (e.g., is negative).
.TP
.B EINVAL
.RB ( accept4 ())
invalid value in
.IR flags .
.TP
.B EMFILE
The per-process limit on the number of open file descriptors has been reached.
.TP
.B ENFILE
The system-wide limit on the total number of open files has been reached.
.TP
.B ENOBUFS
.TQ
.B ENOMEM
Not enough free memory.
This often means that the memory allocation is limited by the socket buffer
limits, not by the system memory.
.TP
.B ENOTSOCK
The file descriptor
.I sockfd
does not refer to a socket.
.TP
.B EOPNOTSUPP
The referenced socket is not of type
.BR SOCK_STREAM .
.TP
.B EPERM
Firewall rules forbid connection.
.TP
.B EPROTO
Protocol error.
.P
In addition, network errors for the new socket and as defined
for the protocol may be returned.
Various Linux kernels can
return other errors such as
.BR ENOSR ,
.BR ESOCKTNOSUPPORT ,
.BR EPROTONOSUPPORT ,
.BR ETIMEDOUT .
The value
.B ERESTARTSYS
may be seen during a trace.
.SH VERSIONS
On Linux, the new socket returned by
.BR accept ()
does \fInot\fP inherit file status flags such as
.B O_NONBLOCK
and
.B O_ASYNC
from the listening socket.
This behavior differs from the canonical BSD sockets implementation.
.\" Some testing seems to show that Tru64 5.1 and HP-UX 11 also
.\" do not inherit file status flags -- MTK Jun 05
Portable programs should not rely on inheritance or noninheritance
of file status flags and always explicitly set all required flags on
the socket returned from
.BR accept ().
.SH STANDARDS
.TP
.BR accept ()
POSIX.1-2008.
.TP
.BR accept4 ()
Linux.
.SH HISTORY
.TP
.BR accept ()
POSIX.1-2001, SVr4, 4.4BSD
.RB ( accept ()
first appeared in 4.2BSD).
.\" The BSD man page documents five possible error returns
.\" (EBADF, ENOTSOCK, EOPNOTSUPP, EWOULDBLOCK, EFAULT).
.\" POSIX.1-2001 documents errors
.\" EAGAIN, EBADF, ECONNABORTED, EINTR, EINVAL, EMFILE,
.\" ENFILE, ENOBUFS, ENOMEM, ENOTSOCK, EOPNOTSUPP, EPROTO, EWOULDBLOCK.
.\" In addition, SUSv2 documents EFAULT and ENOSR.
.TP
.BR accept4 ()
Linux 2.6.28,
glibc 2.10.
.SH NOTES
There may not always be a connection waiting after a
.B SIGIO
is delivered or
.BR select (2),
.BR poll (2),
or
.BR epoll (7)
return a readability event because the connection might have been
removed by an asynchronous network error or another thread before
.BR accept ()
is called.
If this happens, then the call will block waiting for the next
connection to arrive.
To ensure that
.BR accept ()
never blocks, the passed socket
.I sockfd
needs to have the
.B O_NONBLOCK
flag set (see
.BR socket (7)).
.P
For certain protocols which require an explicit confirmation,
such as DECnet,
.BR accept ()
can be thought of as merely dequeuing the next connection request and not
implying confirmation.
Confirmation can be implied by
a normal read or write on the new file descriptor, and rejection can be
implied by closing the new socket.
Currently, only DECnet has these semantics on Linux.
.\"
.SS The socklen_t type
In the original BSD sockets implementation (and on other older systems)
.\" such as Linux libc4 and libc5, SunOS 4, SGI
the third argument of
.BR accept ()
was declared as an \fIint\ *\fP.
A POSIX.1g draft
standard wanted to change it into a \fIsize_t\ *\fPC;
.\" SunOS 5 has 'size_t *'
later POSIX standards and glibc 2.x have
.IR "socklen_t\ * ".
.SH EXAMPLES
See
.BR bind (2).
.SH SEE ALSO
.BR bind (2),
.BR connect (2),
.BR listen (2),
.BR select (2),
.BR socket (2),
.BR socket (7)
|