]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/accept.2
getent.1, iconv.1, ldd.1, locale.1, localedef.1, memusage.1, memusagestat.1, pldd...
[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.\"
a9cd9cb7 4.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
fea681da
MK
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\" notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\" notice, this list of conditions and the following disclaimer in the
12.\" documentation and/or other materials provided with the distribution.
13.\" 3. All advertising materials mentioning features or use of this software
14.\" must display the following acknowledgement:
15.\" This product includes software developed by the University of
16.\" California, Berkeley and its contributors.
17.\" 4. Neither the name of the University nor the names of its contributors
18.\" may be used to endorse or promote products derived from this software
19.\" without specific prior written permission.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
8c9302dc 32.\" %%%LICENSE_END
fea681da
MK
33.\"
34.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
35.\" Modified 1996-10-21 by Eric S. Raymond <esr@thyrsus.com>
36.\" Modified 1998-2000 by Andi Kleen to match Linux 2.2 reality
37.\" Modified 2002-04-23 by Roger Luethi <rl@hellgate.ch>
c11b1abf 38.\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
6f74cfbb 39.\" 2008-12-04, mtk, Add documentation of accept4()
fea681da 40.\"
9ba01802 41.TH ACCEPT 2 2019-03-06 "Linux" "Linux Programmer's Manual"
fea681da 42.SH NAME
191a77d4 43accept, accept4 \- accept a connection on a socket
fea681da 44.SH SYNOPSIS
6f74cfbb 45.nf
53a6b01d 46.BR "#include <sys/types.h>" " /* See NOTES */"
fea681da 47.B #include <sys/socket.h>
c63f06b5 48.PP
ae1a9ff4 49.BI "int accept(int " sockfd ", struct sockaddr *" addr ", socklen_t *" addrlen );
f90f031e 50
86b91fdf 51.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
6f74cfbb 52.B #include <sys/socket.h>
c63f06b5 53.PP
6f74cfbb
MK
54.BI "int accept4(int " sockfd ", struct sockaddr *" addr ,
55.BI " socklen_t *" addrlen ", int " flags );
56.fi
fea681da 57.SH DESCRIPTION
fea681da 58The
ae1a9ff4
MK
59.BR accept ()
60system call is used with connection-based socket types
fea681da 61.RB ( SOCK_STREAM ,
c51fc3c1 62.BR SOCK_SEQPACKET ).
fea681da 63It extracts the first connection request on the queue of pending
00b7d497
MK
64connections for the listening socket,
65.IR sockfd ,
66creates a new connected socket, and returns a new file
ae1a9ff4
MK
67descriptor referring to that socket.
68The newly created socket is not in the listening state.
fea681da 69The original socket
ae1a9ff4
MK
70.I sockfd
71is unaffected by this call.
fea681da
MK
72.PP
73The argument
ae1a9ff4 74.I sockfd
fea681da
MK
75is a socket that has been created with
76.BR socket (2),
77bound to a local address with
78.BR bind (2),
79and is listening for connections after a
80.BR listen (2).
c63f06b5 81.PP
fea681da
MK
82The argument
83.I addr
c13182ef 84is a pointer to a
ae1a9ff4
MK
85.I sockaddr
86structure.
87This structure is filled in with the address of the peer socket,
88as known to the communications layer.
89The exact format of the address returned
fea681da 90.I addr
ae1a9ff4 91is determined by the socket's address family (see
c13182ef 92.BR socket (2)
fea681da 93and the respective protocol man pages).
b98353f0
MK
94When
95.I addr
96is NULL, nothing is filled in; in this case,
97.I addrlen
98is not used, and should also be NULL.
c63f06b5 99.PP
fea681da
MK
100The
101.I addrlen
b98353f0
MK
102argument is a value-result argument:
103the caller must initialize it to contain the
104size (in bytes) of the structure pointed to by
fea681da 105.IR addr ;
b98353f0 106on return it will contain the actual size of the peer address.
c63f06b5 107.PP
b98353f0
MK
108The returned address is truncated if the buffer provided is too small;
109in this case,
110.I addrlen
111will return a value greater than was supplied to the call.
fea681da
MK
112.PP
113If no pending
114connections are present on the queue, and the socket is not marked as
ff40dbb3 115nonblocking,
ae1a9ff4 116.BR accept ()
c13182ef
MK
117blocks the caller until a connection is present.
118If the socket is marked
ff40dbb3 119nonblocking and no pending connections are present on the queue,
ae1a9ff4 120.BR accept ()
682edefb 121fails with the error
86426e0b
MK
122.BR EAGAIN
123or
124.BR EWOULDBLOCK .
fea681da
MK
125.PP
126In order to be notified of incoming connections on a socket, you can use
cb1bcdf2
MK
127.BR select (2),
128.BR poll (2),
fea681da 129or
cb1bcdf2 130.BR epoll (7).
fea681da
MK
131A readable event will be delivered when a new connection is attempted and you
132may then call
ae1a9ff4 133.BR accept ()
c13182ef
MK
134to get a socket for that connection.
135Alternatively, you can set the socket to deliver
fea681da
MK
136.B SIGIO
137when activity occurs on a socket; see
138.BR socket (7)
139for details.
c63f06b5 140.PP
6f74cfbb
MK
141If
142.IR flags
143is 0, then
144.BR accept4 ()
145is the same as
146.BR accept ().
147The following values can be bitwise ORed in
148.IR flags
149to obtain different behavior:
150.TP 16
151.B SOCK_NONBLOCK
152Set the
153.BR O_NONBLOCK
7f11e32c
MK
154file status flag on the open file description (see
155.BR open (2))
156referred to by the new file descriptor.
6f74cfbb
MK
157Using this flag saves extra calls to
158.BR fcntl (2)
159to achieve the same result.
160.TP
161.B SOCK_CLOEXEC
162Set the close-on-exec
163.RB ( FD_CLOEXEC )
164flag on the new file descriptor.
165See the description of the
166.B O_CLOEXEC
167flag in
168.BR open (2)
169for reasons why this may be useful.
47297adb 170.SH RETURN VALUE
cca657e2 171On success,
d9cb0d7d 172these system calls return a nonnegative integer that is a file descriptor
cca657e2
MK
173for the accepted socket.
174On error, \-1 is returned, and
175.I errno
176is set appropriately.
73d8cece 177.SS Error handling
c13182ef 178Linux
ae1a9ff4 179.BR accept ()
6f74cfbb
MK
180(and
181.BR accept4 ())
c13182ef
MK
182passes already-pending network errors on the new socket
183as an error code from
184.BR accept ().
d9bfdb9c 185This behavior differs from other BSD socket
c13182ef
MK
186implementations.
187For reliable operation the application should detect
188the network errors defined for the protocol after
ae1a9ff4 189.BR accept ()
fea681da 190and treat
c13182ef 191them like
0daa9e92 192.B EAGAIN
c13182ef 193by retrying.
2dd7f4cb 194In the case of TCP/IP, these are
fea681da
MK
195.BR ENETDOWN ,
196.BR EPROTO ,
197.BR ENOPROTOOPT ,
198.BR EHOSTDOWN ,
199.BR ENONET ,
200.BR EHOSTUNREACH ,
201.BR EOPNOTSUPP ,
202and
203.BR ENETUNREACH .
204.SH ERRORS
fea681da
MK
205.TP
206.BR EAGAIN " or " EWOULDBLOCK
86426e0b 207.\" Actually EAGAIN on Linux
ff40dbb3 208The socket is marked nonblocking and no connections are
fea681da 209present to be accepted.
fe472573
MK
210POSIX.1-2001 and POSIX.1-2008
211allow either error to be returned for this case,
212and do not require these constants to have the same value,
86426e0b 213so a portable application should check for both possibilities.
fea681da
MK
214.TP
215.B EBADF
3ad77694
MK
216.I sockfd
217is not an open file descriptor.
fea681da
MK
218.TP
219.B ECONNABORTED
220A connection has been aborted.
221.TP
fecaa19a
MK
222.B EFAULT
223The
224.I addr
225argument is not in a writable part of the user address space.
226.TP
fea681da
MK
227.B EINTR
228The system call was interrupted by a signal that was caught
01538d0d
MK
229before a valid connection arrived; see
230.BR signal (7).
fea681da
MK
231.TP
232.B EINVAL
16c892d3
MK
233Socket is not listening for connections, or
234.I addrlen
235is invalid (e.g., is negative).
fea681da 236.TP
6f74cfbb
MK
237.B EINVAL
238.RB ( accept4 ())
239invalid value in
240.IR flags .
241.TP
fea681da 242.B EMFILE
26c32fab 243The per-process limit on the number of open file descriptors has been reached.
fea681da
MK
244.TP
245.B ENFILE
e258766b 246The system-wide limit on the total number of open files has been reached.
fea681da 247.TP
fecaa19a
MK
248.BR ENOBUFS ", " ENOMEM
249Not enough free memory.
250This often means that the memory allocation is limited by the socket buffer
251limits, not by the system memory.
252.TP
fea681da 253.B ENOTSOCK
deedfd97
MK
254The file descriptor
255.I sockfd
256does not refer to a socket.
fea681da
MK
257.TP
258.B EOPNOTSUPP
259The referenced socket is not of type
c13182ef 260.BR SOCK_STREAM .
fea681da
MK
261.TP
262.B EPROTO
263Protocol error.
264.PP
fecaa19a 265In addition, Linux
ae1a9ff4 266.BR accept ()
fea681da
MK
267may fail if:
268.TP
269.B EPERM
270Firewall rules forbid connection.
271.PP
272In addition, network errors for the new socket and as defined
c13182ef
MK
273for the protocol may be returned.
274Various Linux kernels can
fea681da
MK
275return other errors such as
276.BR ENOSR ,
277.BR ESOCKTNOSUPPORT ,
278.BR EPROTONOSUPPORT ,
279.BR ETIMEDOUT .
280The value
281.B ERESTARTSYS
282may be seen during a trace.
6f74cfbb 283.SH VERSIONS
6f74cfbb
MK
284The
285.BR accept4 ()
286system call is available starting with Linux 2.6.28;
287support in glibc is available starting with version 2.10.
47297adb 288.SH CONFORMING TO
6f74cfbb 289.BR accept ():
9db8624b 290POSIX.1-2001, POSIX.1-2008,
2d7dbb05 291SVr4, 4.4BSD
ae1a9ff4 292.RB ( accept ()
6f74cfbb 293first appeared in 4.2BSD).
9d9dc1e8
MK
294.\" The BSD man page documents five possible error returns
295.\" (EBADF, ENOTSOCK, EOPNOTSUPP, EWOULDBLOCK, EFAULT).
c13182ef 296.\" POSIX.1-2001 documents errors
9d9dc1e8 297.\" EAGAIN, EBADF, ECONNABORTED, EINTR, EINVAL, EMFILE,
c13182ef 298.\" ENFILE, ENOBUFS, ENOMEM, ENOTSOCK, EOPNOTSUPP, EPROTO, EWOULDBLOCK.
9d9dc1e8 299.\" In addition, SUSv2 documents EFAULT and ENOSR.
c63f06b5 300.PP
6f74cfbb 301.BR accept4 ()
c8f2dd47 302is a nonstandard Linux extension.
c63f06b5 303.PP
ae1a9ff4 304On Linux, the new socket returned by
c13182ef 305.BR accept ()
ae1a9ff4 306does \fInot\fP inherit file status flags such as
0daa9e92 307.B O_NONBLOCK
ae1a9ff4 308and
0daa9e92 309.B O_ASYNC
ae1a9ff4 310from the listening socket.
d9bfdb9c 311This behavior differs from the canonical BSD sockets implementation.
ae1a9ff4
MK
312.\" Some testing seems to show that Tru64 5.1 and HP-UX 11 also
313.\" do not inherit file status flags -- MTK Jun 05
24b74457 314Portable programs should not rely on inheritance or noninheritance
c13182ef
MK
315of file status flags and always explicitly set all required flags on
316the socket returned from
ae1a9ff4 317.BR accept ().
19c98696 318.SH NOTES
53a6b01d
MK
319POSIX.1-2001 does not require the inclusion of
320.IR <sys/types.h> ,
321and this header file is not required on Linux.
322However, some historical (BSD) implementations required this header
323file, and portable applications are probably wise to include it.
c63f06b5 324.PP
a1d5f77c
MK
325There may not always be a connection waiting after a
326.B SIGIO
327is delivered or
cb1bcdf2
MK
328.BR select (2),
329.BR poll (2),
a1d5f77c 330or
cb1bcdf2 331.BR epoll (7)
a1d5f77c
MK
332return a readability event because the connection might have been
333removed by an asynchronous network error or another thread before
334.BR accept ()
335is called.
f14ae16e 336If this happens, then the call will block waiting for the next
a1d5f77c
MK
337connection to arrive.
338To ensure that
339.BR accept ()
340never blocks, the passed socket
341.I sockfd
342needs to have the
343.B O_NONBLOCK
344flag set (see
345.BR socket (7)).
c63f06b5 346.PP
ccdf8bc0 347For certain protocols which require an explicit confirmation,
4529738e 348such as DECnet,
ccdf8bc0
MK
349.BR accept ()
350can be thought of as merely dequeuing the next connection request and not
351implying confirmation.
352Confirmation can be implied by
353a normal read or write on the new file descriptor, and rejection can be
354implied by closing the new socket.
4529738e 355Currently, only DECnet has these semantics on Linux.
ccdf8bc0 356.\"
a1d5f77c 357.SS The socklen_t type
a05774c4
MK
358In the original BSD sockets implementation (and on other older systems)
359.\" such as Linux libc4 and libc5, SunOS 4, SGI
360the third argument of
ae1a9ff4 361.BR accept ()
a05774c4
MK
362was declared as an \fIint\ *\fP.
363A POSIX.1g draft
364standard wanted to change it into a \fIsize_t\ *\fPC;
365.\" SunOS 5 has 'size_t *'
366later POSIX standards and glibc 2.x have
9d66d927 367.IR "socklen_t\ * ".
f11e5e44
MK
368.SH EXAMPLE
369See
370.BR bind (2).
47297adb 371.SH SEE ALSO
fea681da
MK
372.BR bind (2),
373.BR connect (2),
374.BR listen (2),
375.BR select (2),
5dfedcae
MK
376.BR socket (2),
377.BR socket (7)