]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/accept.2
All pages: Remove the 5th argument to .TH
[thirdparty/man-pages.git] / man2 / accept.2
CommitLineData
fea681da
MK
1.\" Copyright (c) 1983, 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
47009d5e 4.\" SPDX-License-Identifier: BSD-4-Clause-UC
fea681da
MK
5.\"
6.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
7.\" Modified 1996-10-21 by Eric S. Raymond <esr@thyrsus.com>
8.\" Modified 1998-2000 by Andi Kleen to match Linux 2.2 reality
9.\" Modified 2002-04-23 by Roger Luethi <rl@hellgate.ch>
c11b1abf 10.\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
6f74cfbb 11.\" 2008-12-04, mtk, Add documentation of accept4()
fea681da 12.\"
45186a5d 13.TH ACCEPT 2 2021-08-27 "Linux man-pages (unreleased)"
fea681da 14.SH NAME
191a77d4 15accept, accept4 \- accept a connection on a socket
974a5c3d
AC
16.SH LIBRARY
17Standard C library
8fc3b2cf 18.RI ( libc ", " \-lc )
fea681da 19.SH SYNOPSIS
6f74cfbb 20.nf
fea681da 21.B #include <sys/socket.h>
c63f06b5 22.PP
e234eef0
AC
23.BI "int accept(int " sockfd ", struct sockaddr *restrict " addr ,
24.BI " socklen_t *restrict " addrlen );
eaa18d3c 25.PP
86b91fdf 26.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
6f74cfbb 27.B #include <sys/socket.h>
c63f06b5 28.PP
e234eef0
AC
29.BI "int accept4(int " sockfd ", struct sockaddr *restrict " addr ,
30.BI " socklen_t *restrict " addrlen ", int " flags );
6f74cfbb 31.fi
fea681da 32.SH DESCRIPTION
fea681da 33The
ae1a9ff4
MK
34.BR accept ()
35system call is used with connection-based socket types
fea681da 36.RB ( SOCK_STREAM ,
c51fc3c1 37.BR SOCK_SEQPACKET ).
fea681da 38It extracts the first connection request on the queue of pending
00b7d497
MK
39connections for the listening socket,
40.IR sockfd ,
41creates a new connected socket, and returns a new file
ae1a9ff4
MK
42descriptor referring to that socket.
43The newly created socket is not in the listening state.
fea681da 44The original socket
ae1a9ff4
MK
45.I sockfd
46is unaffected by this call.
fea681da
MK
47.PP
48The argument
ae1a9ff4 49.I sockfd
fea681da
MK
50is a socket that has been created with
51.BR socket (2),
52bound to a local address with
53.BR bind (2),
54and is listening for connections after a
55.BR listen (2).
c63f06b5 56.PP
fea681da
MK
57The argument
58.I addr
c13182ef 59is a pointer to a
ae1a9ff4
MK
60.I sockaddr
61structure.
62This structure is filled in with the address of the peer socket,
63as known to the communications layer.
64The exact format of the address returned
fea681da 65.I addr
ae1a9ff4 66is determined by the socket's address family (see
c13182ef 67.BR socket (2)
fea681da 68and the respective protocol man pages).
b98353f0
MK
69When
70.I addr
71is NULL, nothing is filled in; in this case,
72.I addrlen
73is not used, and should also be NULL.
c63f06b5 74.PP
fea681da
MK
75The
76.I addrlen
b98353f0
MK
77argument is a value-result argument:
78the caller must initialize it to contain the
79size (in bytes) of the structure pointed to by
fea681da 80.IR addr ;
b98353f0 81on return it will contain the actual size of the peer address.
c63f06b5 82.PP
b98353f0
MK
83The returned address is truncated if the buffer provided is too small;
84in this case,
85.I addrlen
86will return a value greater than was supplied to the call.
fea681da
MK
87.PP
88If no pending
89connections are present on the queue, and the socket is not marked as
ff40dbb3 90nonblocking,
ae1a9ff4 91.BR accept ()
c13182ef
MK
92blocks the caller until a connection is present.
93If the socket is marked
ff40dbb3 94nonblocking and no pending connections are present on the queue,
ae1a9ff4 95.BR accept ()
682edefb 96fails with the error
1ae6b2c7 97.B EAGAIN
86426e0b
MK
98or
99.BR EWOULDBLOCK .
fea681da
MK
100.PP
101In order to be notified of incoming connections on a socket, you can use
cb1bcdf2
MK
102.BR select (2),
103.BR poll (2),
fea681da 104or
cb1bcdf2 105.BR epoll (7).
fea681da
MK
106A readable event will be delivered when a new connection is attempted and you
107may then call
ae1a9ff4 108.BR accept ()
c13182ef
MK
109to get a socket for that connection.
110Alternatively, you can set the socket to deliver
fea681da
MK
111.B SIGIO
112when activity occurs on a socket; see
113.BR socket (7)
114for details.
c63f06b5 115.PP
6f74cfbb 116If
1ae6b2c7 117.I flags
6f74cfbb
MK
118is 0, then
119.BR accept4 ()
120is the same as
121.BR accept ().
122The following values can be bitwise ORed in
1ae6b2c7 123.I flags
6f74cfbb
MK
124to obtain different behavior:
125.TP 16
126.B SOCK_NONBLOCK
127Set the
1ae6b2c7 128.B O_NONBLOCK
7f11e32c
MK
129file status flag on the open file description (see
130.BR open (2))
131referred to by the new file descriptor.
6f74cfbb
MK
132Using this flag saves extra calls to
133.BR fcntl (2)
134to achieve the same result.
135.TP
136.B SOCK_CLOEXEC
137Set the close-on-exec
138.RB ( FD_CLOEXEC )
139flag on the new file descriptor.
140See the description of the
141.B O_CLOEXEC
142flag in
143.BR open (2)
144for reasons why this may be useful.
47297adb 145.SH RETURN VALUE
cca657e2 146On success,
ed09120a
MK
147these system calls return a file descriptor
148for the accepted socket (a nonnegative integer).
03349e33 149On error, \-1 is returned,
cca657e2 150.I errno
c112329f 151is set to indicate the error, and
03349e33
MK
152.I addrlen
153is left unchanged.
73d8cece 154.SS Error handling
c13182ef 155Linux
ae1a9ff4 156.BR accept ()
6f74cfbb
MK
157(and
158.BR accept4 ())
c13182ef
MK
159passes already-pending network errors on the new socket
160as an error code from
161.BR accept ().
d9bfdb9c 162This behavior differs from other BSD socket
c13182ef
MK
163implementations.
164For reliable operation the application should detect
165the network errors defined for the protocol after
ae1a9ff4 166.BR accept ()
fea681da 167and treat
c13182ef 168them like
0daa9e92 169.B EAGAIN
c13182ef 170by retrying.
2dd7f4cb 171In the case of TCP/IP, these are
fea681da
MK
172.BR ENETDOWN ,
173.BR EPROTO ,
174.BR ENOPROTOOPT ,
175.BR EHOSTDOWN ,
176.BR ENONET ,
177.BR EHOSTUNREACH ,
178.BR EOPNOTSUPP ,
179and
180.BR ENETUNREACH .
181.SH ERRORS
fea681da
MK
182.TP
183.BR EAGAIN " or " EWOULDBLOCK
86426e0b 184.\" Actually EAGAIN on Linux
ff40dbb3 185The socket is marked nonblocking and no connections are
fea681da 186present to be accepted.
fe472573
MK
187POSIX.1-2001 and POSIX.1-2008
188allow either error to be returned for this case,
189and do not require these constants to have the same value,
86426e0b 190so a portable application should check for both possibilities.
fea681da
MK
191.TP
192.B EBADF
3ad77694
MK
193.I sockfd
194is not an open file descriptor.
fea681da
MK
195.TP
196.B ECONNABORTED
197A connection has been aborted.
198.TP
fecaa19a
MK
199.B EFAULT
200The
201.I addr
202argument is not in a writable part of the user address space.
203.TP
fea681da
MK
204.B EINTR
205The system call was interrupted by a signal that was caught
01538d0d
MK
206before a valid connection arrived; see
207.BR signal (7).
fea681da
MK
208.TP
209.B EINVAL
16c892d3
MK
210Socket is not listening for connections, or
211.I addrlen
212is invalid (e.g., is negative).
fea681da 213.TP
6f74cfbb
MK
214.B EINVAL
215.RB ( accept4 ())
216invalid value in
217.IR flags .
218.TP
fea681da 219.B EMFILE
26c32fab 220The per-process limit on the number of open file descriptors has been reached.
fea681da
MK
221.TP
222.B ENFILE
e258766b 223The system-wide limit on the total number of open files has been reached.
fea681da 224.TP
fecaa19a
MK
225.BR ENOBUFS ", " ENOMEM
226Not enough free memory.
227This often means that the memory allocation is limited by the socket buffer
228limits, not by the system memory.
229.TP
fea681da 230.B ENOTSOCK
deedfd97
MK
231The file descriptor
232.I sockfd
233does not refer to a socket.
fea681da
MK
234.TP
235.B EOPNOTSUPP
236The referenced socket is not of type
c13182ef 237.BR SOCK_STREAM .
fea681da 238.TP
fea681da
MK
239.B EPERM
240Firewall rules forbid connection.
18ce9c4a
MK
241.TP
242.B EPROTO
243Protocol error.
fea681da
MK
244.PP
245In addition, network errors for the new socket and as defined
c13182ef
MK
246for the protocol may be returned.
247Various Linux kernels can
fea681da
MK
248return other errors such as
249.BR ENOSR ,
250.BR ESOCKTNOSUPPORT ,
251.BR EPROTONOSUPPORT ,
252.BR ETIMEDOUT .
253The value
254.B ERESTARTSYS
255may be seen during a trace.
6f74cfbb 256.SH VERSIONS
6f74cfbb
MK
257The
258.BR accept4 ()
259system call is available starting with Linux 2.6.28;
260support in glibc is available starting with version 2.10.
3113c7f3 261.SH STANDARDS
6f74cfbb 262.BR accept ():
9db8624b 263POSIX.1-2001, POSIX.1-2008,
2d7dbb05 264SVr4, 4.4BSD
ae1a9ff4 265.RB ( accept ()
6f74cfbb 266first appeared in 4.2BSD).
9d9dc1e8
MK
267.\" The BSD man page documents five possible error returns
268.\" (EBADF, ENOTSOCK, EOPNOTSUPP, EWOULDBLOCK, EFAULT).
c13182ef 269.\" POSIX.1-2001 documents errors
9d9dc1e8 270.\" EAGAIN, EBADF, ECONNABORTED, EINTR, EINVAL, EMFILE,
c13182ef 271.\" ENFILE, ENOBUFS, ENOMEM, ENOTSOCK, EOPNOTSUPP, EPROTO, EWOULDBLOCK.
9d9dc1e8 272.\" In addition, SUSv2 documents EFAULT and ENOSR.
c63f06b5 273.PP
6f74cfbb 274.BR accept4 ()
c8f2dd47 275is a nonstandard Linux extension.
c63f06b5 276.PP
ae1a9ff4 277On Linux, the new socket returned by
c13182ef 278.BR accept ()
ae1a9ff4 279does \fInot\fP inherit file status flags such as
0daa9e92 280.B O_NONBLOCK
ae1a9ff4 281and
0daa9e92 282.B O_ASYNC
ae1a9ff4 283from the listening socket.
d9bfdb9c 284This behavior differs from the canonical BSD sockets implementation.
ae1a9ff4
MK
285.\" Some testing seems to show that Tru64 5.1 and HP-UX 11 also
286.\" do not inherit file status flags -- MTK Jun 05
24b74457 287Portable programs should not rely on inheritance or noninheritance
c13182ef
MK
288of file status flags and always explicitly set all required flags on
289the socket returned from
ae1a9ff4 290.BR accept ().
19c98696 291.SH NOTES
a1d5f77c
MK
292There may not always be a connection waiting after a
293.B SIGIO
294is delivered or
cb1bcdf2
MK
295.BR select (2),
296.BR poll (2),
a1d5f77c 297or
cb1bcdf2 298.BR epoll (7)
a1d5f77c
MK
299return a readability event because the connection might have been
300removed by an asynchronous network error or another thread before
301.BR accept ()
302is called.
f14ae16e 303If this happens, then the call will block waiting for the next
a1d5f77c
MK
304connection to arrive.
305To ensure that
306.BR accept ()
307never blocks, the passed socket
308.I sockfd
309needs to have the
310.B O_NONBLOCK
311flag set (see
312.BR socket (7)).
c63f06b5 313.PP
ccdf8bc0 314For certain protocols which require an explicit confirmation,
4529738e 315such as DECnet,
ccdf8bc0
MK
316.BR accept ()
317can be thought of as merely dequeuing the next connection request and not
318implying confirmation.
319Confirmation can be implied by
320a normal read or write on the new file descriptor, and rejection can be
321implied by closing the new socket.
4529738e 322Currently, only DECnet has these semantics on Linux.
ccdf8bc0 323.\"
a1d5f77c 324.SS The socklen_t type
a05774c4
MK
325In the original BSD sockets implementation (and on other older systems)
326.\" such as Linux libc4 and libc5, SunOS 4, SGI
327the third argument of
ae1a9ff4 328.BR accept ()
a05774c4
MK
329was declared as an \fIint\ *\fP.
330A POSIX.1g draft
331standard wanted to change it into a \fIsize_t\ *\fPC;
332.\" SunOS 5 has 'size_t *'
333later POSIX standards and glibc 2.x have
9d66d927 334.IR "socklen_t\ * ".
a14af333 335.SH EXAMPLES
f11e5e44
MK
336See
337.BR bind (2).
47297adb 338.SH SEE ALSO
fea681da
MK
339.BR bind (2),
340.BR connect (2),
341.BR listen (2),
342.BR select (2),
5dfedcae
MK
343.BR socket (2),
344.BR socket (7)