1accept(2)                     System Calls Manual                    accept(2)
2
3
4

NAME

6       accept, accept4 - accept a connection on a socket
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <sys/socket.h>
13
14       int accept(int sockfd, struct sockaddr *_Nullable restrict addr,
15                  socklen_t *_Nullable restrict addrlen);
16
17       #define _GNU_SOURCE             /* See feature_test_macros(7) */
18       #include <sys/socket.h>
19
20       int accept4(int sockfd, struct sockaddr *_Nullable restrict addr,
21                  socklen_t *_Nullable restrict addrlen, int flags);
22

DESCRIPTION

24       The  accept()  system  call  is used with connection-based socket types
25       (SOCK_STREAM, SOCK_SEQPACKET).  It extracts the  first  connection  re‐
26       quest  on  the  queue  of pending connections for the listening socket,
27       sockfd, creates a new connected socket, and returns a new file descrip‐
28       tor  referring  to that socket.  The newly created socket is not in the
29       listening state.  The original socket  sockfd  is  unaffected  by  this
30       call.
31
32       The  argument  sockfd is a socket that has been created with socket(2),
33       bound to a local address with bind(2), and is listening for connections
34       after a listen(2).
35
36       The argument addr is a pointer to a sockaddr structure.  This structure
37       is filled in with the address of the peer socket, as known to the  com‐
38       munications  layer.   The  exact format of the address returned addr is
39       determined by the socket's address family (see socket(2)  and  the  re‐
40       spective protocol man pages).  When addr is NULL, nothing is filled in;
41       in this case, addrlen is not used, and should also be NULL.
42
43       The addrlen argument is a value-result argument: the caller  must  ini‐
44       tialize  it  to contain the size (in bytes) of the structure pointed to
45       by addr; on return it will contain the actual size of the peer address.
46
47       The returned address is truncated if the buffer provided is too  small;
48       in  this case, addrlen will return a value greater than was supplied to
49       the call.
50
51       If no pending connections are present on the queue, and the  socket  is
52       not  marked  as nonblocking, accept() blocks the caller until a connec‐
53       tion is present.  If the socket is marked nonblocking  and  no  pending
54       connections are present on the queue, accept() fails with the error EA‐
55       GAIN or EWOULDBLOCK.
56
57       In order to be notified of incoming connections on a  socket,  you  can
58       use  select(2),  poll(2), or epoll(7).  A readable event will be deliv‐
59       ered when a new connection is attempted and you may then call  accept()
60       to  get  a  socket for that connection.  Alternatively, you can set the
61       socket to deliver SIGIO when activity occurs on a socket; see socket(7)
62       for details.
63
64       If  flags  is 0, then accept4() is the same as accept().  The following
65       values can be bitwise ORed in flags to obtain different behavior:
66
67       SOCK_NONBLOCK   Set the O_NONBLOCK file status flag on  the  open  file
68                       description  (see  open(2)) referred to by the new file
69                       descriptor.  Using this flag saves extra calls  to  fc‐
70                       ntl(2) to achieve the same result.
71
72       SOCK_CLOEXEC    Set the close-on-exec (FD_CLOEXEC) flag on the new file
73                       descriptor.  See the description of the O_CLOEXEC  flag
74                       in open(2) for reasons why this may be useful.
75

RETURN VALUE

77       On  success,  these  system  calls return a file descriptor for the ac‐
78       cepted socket (a nonnegative integer).  On error, -1 is returned, errno
79       is set to indicate the error, and addrlen is left unchanged.
80
81   Error handling
82       Linux accept() (and accept4()) passes already-pending network errors on
83       the new socket as an error code from accept().  This  behavior  differs
84       from  other BSD socket implementations.  For reliable operation the ap‐
85       plication should detect the network errors defined for the protocol af‐
86       ter  accept()  and  treat them like EAGAIN by retrying.  In the case of
87       TCP/IP, these are ENETDOWN,  EPROTO,  ENOPROTOOPT,  EHOSTDOWN,  ENONET,
88       EHOSTUNREACH, EOPNOTSUPP, and ENETUNREACH.
89

ERRORS

91       EAGAIN or EWOULDBLOCK
92              The  socket is marked nonblocking and no connections are present
93              to be accepted.  POSIX.1-2001 and POSIX.1-2008 allow either  er‐
94              ror  to be returned for this case, and do not require these con‐
95              stants to have the same value, so a portable application  should
96              check for both possibilities.
97
98       EBADF  sockfd is not an open file descriptor.
99
100       ECONNABORTED
101              A connection has been aborted.
102
103       EFAULT The  addr argument is not in a writable part of the user address
104              space.
105
106       EINTR  The system call was interrupted by a signal that was caught  be‐
107              fore a valid connection arrived; see signal(7).
108
109       EINVAL Socket  is  not listening for connections, or addrlen is invalid
110              (e.g., is negative).
111
112       EINVAL (accept4()) invalid value in flags.
113
114       EMFILE The per-process limit on the number of open file descriptors has
115              been reached.
116
117       ENFILE The system-wide limit on the total number of open files has been
118              reached.
119
120       ENOBUFS, ENOMEM
121              Not enough free memory.  This often means that the memory  allo‐
122              cation is limited by the socket buffer limits, not by the system
123              memory.
124
125       ENOTSOCK
126              The file descriptor sockfd does not refer to a socket.
127
128       EOPNOTSUPP
129              The referenced socket is not of type SOCK_STREAM.
130
131       EPERM  Firewall rules forbid connection.
132
133       EPROTO Protocol error.
134
135       In addition, network errors for the new socket and as defined  for  the
136       protocol  may  be returned.  Various Linux kernels can return other er‐
137       rors such as ENOSR, ESOCKTNOSUPPORT, EPROTONOSUPPORT,  ETIMEDOUT.   The
138       value ERESTARTSYS may be seen during a trace.
139

VERSIONS

141       On  Linux,  the  new  socket returned by accept() does not inherit file
142       status flags such as O_NONBLOCK and O_ASYNC from the listening  socket.
143       This  behavior  differs  from the canonical BSD sockets implementation.
144       Portable programs should not rely on inheritance or  noninheritance  of
145       file  status  flags and always explicitly set all required flags on the
146       socket returned from accept().
147

STANDARDS

149       accept()
150              POSIX.1-2008.
151
152       accept4()
153              Linux.
154

HISTORY

156       accept()
157              POSIX.1-2001, SVr4, 4.4BSD (accept() first appeared in 4.2BSD).
158
159       accept4()
160              Linux 2.6.28, glibc 2.10.
161

NOTES

163       There may not always be a connection waiting after a SIGIO is delivered
164       or  select(2),  poll(2), or epoll(7) return a readability event because
165       the connection might have been removed by an asynchronous network error
166       or another thread before accept() is called.  If this happens, then the
167       call will block waiting for the next connection to arrive.   To  ensure
168       that  accept() never blocks, the passed socket sockfd needs to have the
169       O_NONBLOCK flag set (see socket(7)).
170
171       For certain protocols which require an explicit confirmation,  such  as
172       DECnet, accept() can be thought of as merely dequeuing the next connec‐
173       tion request and not implying confirmation.  Confirmation  can  be  im‐
174       plied  by a normal read or write on the new file descriptor, and rejec‐
175       tion can be implied by closing the new socket.  Currently, only  DECnet
176       has these semantics on Linux.
177
178   The socklen_t type
179       In the original BSD sockets implementation (and on other older systems)
180       the third argument of accept() was declared as an  int *.   A  POSIX.1g
181       draft  standard wanted to change it into a size_t *C; later POSIX stan‐
182       dards and glibc 2.x have socklen_t * .
183

EXAMPLES

185       See bind(2).
186

SEE ALSO

188       bind(2), connect(2), listen(2), select(2), socket(2), socket(7)
189
190
191
192Linux man-pages 6.05              2023-04-05                         accept(2)
Impressum