1ACCEPT(2) Linux Programmer's Manual ACCEPT(2)
2
3
4
6 accept, accept4 - accept a connection on a socket
7
9 #include <sys/socket.h>
10
11 int accept(int sockfd, struct sockaddr *restrict addr,
12 socklen_t *restrict addrlen);
13
14 #define _GNU_SOURCE /* See feature_test_macros(7) */
15 #include <sys/socket.h>
16
17 int accept4(int sockfd, struct sockaddr *restrict addr,
18 socklen_t *restrict addrlen, int flags);
19
21 The accept() system call is used with connection-based socket types
22 (SOCK_STREAM, SOCK_SEQPACKET). It extracts the first connection re‐
23 quest on the queue of pending connections for the listening socket,
24 sockfd, creates a new connected socket, and returns a new file descrip‐
25 tor referring to that socket. The newly created socket is not in the
26 listening state. The original socket sockfd is unaffected by this
27 call.
28
29 The argument sockfd is a socket that has been created with socket(2),
30 bound to a local address with bind(2), and is listening for connections
31 after a listen(2).
32
33 The argument addr is a pointer to a sockaddr structure. This structure
34 is filled in with the address of the peer socket, as known to the com‐
35 munications layer. The exact format of the address returned addr is
36 determined by the socket's address family (see socket(2) and the re‐
37 spective protocol man pages). When addr is NULL, nothing is filled in;
38 in this case, addrlen is not used, and should also be NULL.
39
40 The addrlen argument is a value-result argument: the caller must ini‐
41 tialize it to contain the size (in bytes) of the structure pointed to
42 by addr; on return it will contain the actual size of the peer address.
43
44 The returned address is truncated if the buffer provided is too small;
45 in this case, addrlen will return a value greater than was supplied to
46 the call.
47
48 If no pending connections are present on the queue, and the socket is
49 not marked as nonblocking, accept() blocks the caller until a connec‐
50 tion is present. If the socket is marked nonblocking and no pending
51 connections are present on the queue, accept() fails with the error EA‐
52 GAIN or EWOULDBLOCK.
53
54 In order to be notified of incoming connections on a socket, you can
55 use select(2), poll(2), or epoll(7). A readable event will be deliv‐
56 ered when a new connection is attempted and you may then call accept()
57 to get a socket for that connection. Alternatively, you can set the
58 socket to deliver SIGIO when activity occurs on a socket; see socket(7)
59 for details.
60
61 If flags is 0, then accept4() is the same as accept(). The following
62 values can be bitwise ORed in flags to obtain different behavior:
63
64 SOCK_NONBLOCK Set the O_NONBLOCK file status flag on the open file
65 description (see open(2)) referred to by the new file
66 descriptor. Using this flag saves extra calls to fc‐
67 ntl(2) to achieve the same result.
68
69 SOCK_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the new file
70 descriptor. See the description of the O_CLOEXEC flag
71 in open(2) for reasons why this may be useful.
72
74 On success, these system calls return a file descriptor for the ac‐
75 cepted socket (a nonnegative integer). On error, -1 is returned, errno
76 is set to indicate the error, and addrlen is left unchanged.
77
78 Error handling
79 Linux accept() (and accept4()) passes already-pending network errors on
80 the new socket as an error code from accept(). This behavior differs
81 from other BSD socket implementations. For reliable operation the ap‐
82 plication should detect the network errors defined for the protocol af‐
83 ter accept() and treat them like EAGAIN by retrying. In the case of
84 TCP/IP, these are ENETDOWN, EPROTO, ENOPROTOOPT, EHOSTDOWN, ENONET,
85 EHOSTUNREACH, EOPNOTSUPP, and ENETUNREACH.
86
88 EAGAIN or EWOULDBLOCK
89 The socket is marked nonblocking and no connections are present
90 to be accepted. POSIX.1-2001 and POSIX.1-2008 allow either er‐
91 ror to be returned for this case, and do not require these con‐
92 stants to have the same value, so a portable application should
93 check for both possibilities.
94
95 EBADF sockfd is not an open file descriptor.
96
97 ECONNABORTED
98 A connection has been aborted.
99
100 EFAULT The addr argument is not in a writable part of the user address
101 space.
102
103 EINTR The system call was interrupted by a signal that was caught be‐
104 fore a valid connection arrived; see signal(7).
105
106 EINVAL Socket is not listening for connections, or addrlen is invalid
107 (e.g., is negative).
108
109 EINVAL (accept4()) invalid value in flags.
110
111 EMFILE The per-process limit on the number of open file descriptors has
112 been reached.
113
114 ENFILE The system-wide limit on the total number of open files has been
115 reached.
116
117 ENOBUFS, ENOMEM
118 Not enough free memory. This often means that the memory allo‐
119 cation is limited by the socket buffer limits, not by the system
120 memory.
121
122 ENOTSOCK
123 The file descriptor sockfd does not refer to a socket.
124
125 EOPNOTSUPP
126 The referenced socket is not of type SOCK_STREAM.
127
128 EPROTO Protocol error.
129
130 In addition, Linux accept() may fail if:
131
132 EPERM Firewall rules forbid connection.
133
134 In addition, network errors for the new socket and as defined for the
135 protocol may be returned. Various Linux kernels can return other er‐
136 rors such as ENOSR, ESOCKTNOSUPPORT, EPROTONOSUPPORT, ETIMEDOUT. The
137 value ERESTARTSYS may be seen during a trace.
138
140 The accept4() system call is available starting with Linux 2.6.28; sup‐
141 port in glibc is available starting with version 2.10.
142
144 accept(): POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD (accept() first ap‐
145 peared in 4.2BSD).
146
147 accept4() is a nonstandard Linux extension.
148
149 On Linux, the new socket returned by accept() does not inherit file
150 status flags such as O_NONBLOCK and O_ASYNC from the listening socket.
151 This behavior differs from the canonical BSD sockets implementation.
152 Portable programs should not rely on inheritance or noninheritance of
153 file status flags and always explicitly set all required flags on the
154 socket returned from accept().
155
157 There may not always be a connection waiting after a SIGIO is delivered
158 or select(2), poll(2), or epoll(7) return a readability event because
159 the connection might have been removed by an asynchronous network error
160 or another thread before accept() is called. If this happens, then the
161 call will block waiting for the next connection to arrive. To ensure
162 that accept() never blocks, the passed socket sockfd needs to have the
163 O_NONBLOCK flag set (see socket(7)).
164
165 For certain protocols which require an explicit confirmation, such as
166 DECnet, accept() can be thought of as merely dequeuing the next connec‐
167 tion request and not implying confirmation. Confirmation can be im‐
168 plied by a normal read or write on the new file descriptor, and rejec‐
169 tion can be implied by closing the new socket. Currently, only DECnet
170 has these semantics on Linux.
171
172 The socklen_t type
173 In the original BSD sockets implementation (and on other older systems)
174 the third argument of accept() was declared as an int *. A POSIX.1g
175 draft standard wanted to change it into a size_t *C; later POSIX stan‐
176 dards and glibc 2.x have socklen_t * .
177
179 See bind(2).
180
182 bind(2), connect(2), listen(2), select(2), socket(2), socket(7)
183
185 This page is part of release 5.12 of the Linux man-pages project. A
186 description of the project, information about reporting bugs, and the
187 latest version of this page, can be found at
188 https://www.kernel.org/doc/man-pages/.
189
190
191
192Linux 2021-03-22 ACCEPT(2)