]>
Commit | Line | Data |
---|---|---|
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 | 43 | accept, 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 | 58 | The |
ae1a9ff4 MK |
59 | .BR accept () |
60 | system call is used with connection-based socket types | |
fea681da | 61 | .RB ( SOCK_STREAM , |
c51fc3c1 | 62 | .BR SOCK_SEQPACKET ). |
fea681da | 63 | It extracts the first connection request on the queue of pending |
00b7d497 MK |
64 | connections for the listening socket, |
65 | .IR sockfd , | |
66 | creates a new connected socket, and returns a new file | |
ae1a9ff4 MK |
67 | descriptor referring to that socket. |
68 | The newly created socket is not in the listening state. | |
fea681da | 69 | The original socket |
ae1a9ff4 MK |
70 | .I sockfd |
71 | is unaffected by this call. | |
fea681da MK |
72 | .PP |
73 | The argument | |
ae1a9ff4 | 74 | .I sockfd |
fea681da MK |
75 | is a socket that has been created with |
76 | .BR socket (2), | |
77 | bound to a local address with | |
78 | .BR bind (2), | |
79 | and is listening for connections after a | |
80 | .BR listen (2). | |
c63f06b5 | 81 | .PP |
fea681da MK |
82 | The argument |
83 | .I addr | |
c13182ef | 84 | is a pointer to a |
ae1a9ff4 MK |
85 | .I sockaddr |
86 | structure. | |
87 | This structure is filled in with the address of the peer socket, | |
88 | as known to the communications layer. | |
89 | The exact format of the address returned | |
fea681da | 90 | .I addr |
ae1a9ff4 | 91 | is determined by the socket's address family (see |
c13182ef | 92 | .BR socket (2) |
fea681da | 93 | and the respective protocol man pages). |
b98353f0 MK |
94 | When |
95 | .I addr | |
96 | is NULL, nothing is filled in; in this case, | |
97 | .I addrlen | |
98 | is not used, and should also be NULL. | |
c63f06b5 | 99 | .PP |
fea681da MK |
100 | The |
101 | .I addrlen | |
b98353f0 MK |
102 | argument is a value-result argument: |
103 | the caller must initialize it to contain the | |
104 | size (in bytes) of the structure pointed to by | |
fea681da | 105 | .IR addr ; |
b98353f0 | 106 | on return it will contain the actual size of the peer address. |
c63f06b5 | 107 | .PP |
b98353f0 MK |
108 | The returned address is truncated if the buffer provided is too small; |
109 | in this case, | |
110 | .I addrlen | |
111 | will return a value greater than was supplied to the call. | |
fea681da MK |
112 | .PP |
113 | If no pending | |
114 | connections are present on the queue, and the socket is not marked as | |
ff40dbb3 | 115 | nonblocking, |
ae1a9ff4 | 116 | .BR accept () |
c13182ef MK |
117 | blocks the caller until a connection is present. |
118 | If the socket is marked | |
ff40dbb3 | 119 | nonblocking and no pending connections are present on the queue, |
ae1a9ff4 | 120 | .BR accept () |
682edefb | 121 | fails with the error |
86426e0b MK |
122 | .BR EAGAIN |
123 | or | |
124 | .BR EWOULDBLOCK . | |
fea681da MK |
125 | .PP |
126 | In order to be notified of incoming connections on a socket, you can use | |
cb1bcdf2 MK |
127 | .BR select (2), |
128 | .BR poll (2), | |
fea681da | 129 | or |
cb1bcdf2 | 130 | .BR epoll (7). |
fea681da MK |
131 | A readable event will be delivered when a new connection is attempted and you |
132 | may then call | |
ae1a9ff4 | 133 | .BR accept () |
c13182ef MK |
134 | to get a socket for that connection. |
135 | Alternatively, you can set the socket to deliver | |
fea681da MK |
136 | .B SIGIO |
137 | when activity occurs on a socket; see | |
138 | .BR socket (7) | |
139 | for details. | |
c63f06b5 | 140 | .PP |
6f74cfbb MK |
141 | If |
142 | .IR flags | |
143 | is 0, then | |
144 | .BR accept4 () | |
145 | is the same as | |
146 | .BR accept (). | |
147 | The following values can be bitwise ORed in | |
148 | .IR flags | |
149 | to obtain different behavior: | |
150 | .TP 16 | |
151 | .B SOCK_NONBLOCK | |
152 | Set the | |
153 | .BR O_NONBLOCK | |
7f11e32c MK |
154 | file status flag on the open file description (see |
155 | .BR open (2)) | |
156 | referred to by the new file descriptor. | |
6f74cfbb MK |
157 | Using this flag saves extra calls to |
158 | .BR fcntl (2) | |
159 | to achieve the same result. | |
160 | .TP | |
161 | .B SOCK_CLOEXEC | |
162 | Set the close-on-exec | |
163 | .RB ( FD_CLOEXEC ) | |
164 | flag on the new file descriptor. | |
165 | See the description of the | |
166 | .B O_CLOEXEC | |
167 | flag in | |
168 | .BR open (2) | |
169 | for reasons why this may be useful. | |
47297adb | 170 | .SH RETURN VALUE |
cca657e2 | 171 | On success, |
d9cb0d7d | 172 | these system calls return a nonnegative integer that is a file descriptor |
cca657e2 MK |
173 | for the accepted socket. |
174 | On error, \-1 is returned, and | |
175 | .I errno | |
176 | is set appropriately. | |
73d8cece | 177 | .SS Error handling |
c13182ef | 178 | Linux |
ae1a9ff4 | 179 | .BR accept () |
6f74cfbb MK |
180 | (and |
181 | .BR accept4 ()) | |
c13182ef MK |
182 | passes already-pending network errors on the new socket |
183 | as an error code from | |
184 | .BR accept (). | |
d9bfdb9c | 185 | This behavior differs from other BSD socket |
c13182ef MK |
186 | implementations. |
187 | For reliable operation the application should detect | |
188 | the network errors defined for the protocol after | |
ae1a9ff4 | 189 | .BR accept () |
fea681da | 190 | and treat |
c13182ef | 191 | them like |
0daa9e92 | 192 | .B EAGAIN |
c13182ef | 193 | by retrying. |
2dd7f4cb | 194 | In 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 , | |
202 | and | |
203 | .BR ENETUNREACH . | |
204 | .SH ERRORS | |
fea681da MK |
205 | .TP |
206 | .BR EAGAIN " or " EWOULDBLOCK | |
86426e0b | 207 | .\" Actually EAGAIN on Linux |
ff40dbb3 | 208 | The socket is marked nonblocking and no connections are |
fea681da | 209 | present to be accepted. |
fe472573 MK |
210 | POSIX.1-2001 and POSIX.1-2008 |
211 | allow either error to be returned for this case, | |
212 | and do not require these constants to have the same value, | |
86426e0b | 213 | so a portable application should check for both possibilities. |
fea681da MK |
214 | .TP |
215 | .B EBADF | |
3ad77694 MK |
216 | .I sockfd |
217 | is not an open file descriptor. | |
fea681da MK |
218 | .TP |
219 | .B ECONNABORTED | |
220 | A connection has been aborted. | |
221 | .TP | |
fecaa19a MK |
222 | .B EFAULT |
223 | The | |
224 | .I addr | |
225 | argument is not in a writable part of the user address space. | |
226 | .TP | |
fea681da MK |
227 | .B EINTR |
228 | The system call was interrupted by a signal that was caught | |
01538d0d MK |
229 | before a valid connection arrived; see |
230 | .BR signal (7). | |
fea681da MK |
231 | .TP |
232 | .B EINVAL | |
16c892d3 MK |
233 | Socket is not listening for connections, or |
234 | .I addrlen | |
235 | is invalid (e.g., is negative). | |
fea681da | 236 | .TP |
6f74cfbb MK |
237 | .B EINVAL |
238 | .RB ( accept4 ()) | |
239 | invalid value in | |
240 | .IR flags . | |
241 | .TP | |
fea681da | 242 | .B EMFILE |
26c32fab | 243 | The per-process limit on the number of open file descriptors has been reached. |
fea681da MK |
244 | .TP |
245 | .B ENFILE | |
e258766b | 246 | The system-wide limit on the total number of open files has been reached. |
fea681da | 247 | .TP |
fecaa19a MK |
248 | .BR ENOBUFS ", " ENOMEM |
249 | Not enough free memory. | |
250 | This often means that the memory allocation is limited by the socket buffer | |
251 | limits, not by the system memory. | |
252 | .TP | |
fea681da | 253 | .B ENOTSOCK |
deedfd97 MK |
254 | The file descriptor |
255 | .I sockfd | |
256 | does not refer to a socket. | |
fea681da MK |
257 | .TP |
258 | .B EOPNOTSUPP | |
259 | The referenced socket is not of type | |
c13182ef | 260 | .BR SOCK_STREAM . |
fea681da MK |
261 | .TP |
262 | .B EPROTO | |
263 | Protocol error. | |
264 | .PP | |
fecaa19a | 265 | In addition, Linux |
ae1a9ff4 | 266 | .BR accept () |
fea681da MK |
267 | may fail if: |
268 | .TP | |
269 | .B EPERM | |
270 | Firewall rules forbid connection. | |
271 | .PP | |
272 | In addition, network errors for the new socket and as defined | |
c13182ef MK |
273 | for the protocol may be returned. |
274 | Various Linux kernels can | |
fea681da MK |
275 | return other errors such as |
276 | .BR ENOSR , | |
277 | .BR ESOCKTNOSUPPORT , | |
278 | .BR EPROTONOSUPPORT , | |
279 | .BR ETIMEDOUT . | |
280 | The value | |
281 | .B ERESTARTSYS | |
282 | may be seen during a trace. | |
6f74cfbb | 283 | .SH VERSIONS |
6f74cfbb MK |
284 | The |
285 | .BR accept4 () | |
286 | system call is available starting with Linux 2.6.28; | |
287 | support in glibc is available starting with version 2.10. | |
47297adb | 288 | .SH CONFORMING TO |
6f74cfbb | 289 | .BR accept (): |
9db8624b | 290 | POSIX.1-2001, POSIX.1-2008, |
2d7dbb05 | 291 | SVr4, 4.4BSD |
ae1a9ff4 | 292 | .RB ( accept () |
6f74cfbb | 293 | first 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 | 302 | is a nonstandard Linux extension. |
c63f06b5 | 303 | .PP |
ae1a9ff4 | 304 | On Linux, the new socket returned by |
c13182ef | 305 | .BR accept () |
ae1a9ff4 | 306 | does \fInot\fP inherit file status flags such as |
0daa9e92 | 307 | .B O_NONBLOCK |
ae1a9ff4 | 308 | and |
0daa9e92 | 309 | .B O_ASYNC |
ae1a9ff4 | 310 | from the listening socket. |
d9bfdb9c | 311 | This 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 | 314 | Portable programs should not rely on inheritance or noninheritance |
c13182ef MK |
315 | of file status flags and always explicitly set all required flags on |
316 | the socket returned from | |
ae1a9ff4 | 317 | .BR accept (). |
19c98696 | 318 | .SH NOTES |
53a6b01d MK |
319 | POSIX.1-2001 does not require the inclusion of |
320 | .IR <sys/types.h> , | |
321 | and this header file is not required on Linux. | |
322 | However, some historical (BSD) implementations required this header | |
323 | file, and portable applications are probably wise to include it. | |
c63f06b5 | 324 | .PP |
a1d5f77c MK |
325 | There may not always be a connection waiting after a |
326 | .B SIGIO | |
327 | is delivered or | |
cb1bcdf2 MK |
328 | .BR select (2), |
329 | .BR poll (2), | |
a1d5f77c | 330 | or |
cb1bcdf2 | 331 | .BR epoll (7) |
a1d5f77c MK |
332 | return a readability event because the connection might have been |
333 | removed by an asynchronous network error or another thread before | |
334 | .BR accept () | |
335 | is called. | |
f14ae16e | 336 | If this happens, then the call will block waiting for the next |
a1d5f77c MK |
337 | connection to arrive. |
338 | To ensure that | |
339 | .BR accept () | |
340 | never blocks, the passed socket | |
341 | .I sockfd | |
342 | needs to have the | |
343 | .B O_NONBLOCK | |
344 | flag set (see | |
345 | .BR socket (7)). | |
c63f06b5 | 346 | .PP |
ccdf8bc0 | 347 | For certain protocols which require an explicit confirmation, |
4529738e | 348 | such as DECnet, |
ccdf8bc0 MK |
349 | .BR accept () |
350 | can be thought of as merely dequeuing the next connection request and not | |
351 | implying confirmation. | |
352 | Confirmation can be implied by | |
353 | a normal read or write on the new file descriptor, and rejection can be | |
354 | implied by closing the new socket. | |
4529738e | 355 | Currently, only DECnet has these semantics on Linux. |
ccdf8bc0 | 356 | .\" |
a1d5f77c | 357 | .SS The socklen_t type |
a05774c4 MK |
358 | In the original BSD sockets implementation (and on other older systems) |
359 | .\" such as Linux libc4 and libc5, SunOS 4, SGI | |
360 | the third argument of | |
ae1a9ff4 | 361 | .BR accept () |
a05774c4 MK |
362 | was declared as an \fIint\ *\fP. |
363 | A POSIX.1g draft | |
364 | standard wanted to change it into a \fIsize_t\ *\fPC; | |
365 | .\" SunOS 5 has 'size_t *' | |
366 | later POSIX standards and glibc 2.x have | |
9d66d927 | 367 | .IR "socklen_t\ * ". |
f11e5e44 MK |
368 | .SH EXAMPLE |
369 | See | |
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) |