|ACCEPT(2)||Linux Programmer's Manual||ACCEPT(2)|
int accept(int sockfd, struct sockaddr *restrict addr, socklen_t *restrict addrlen);
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <sys/socket.h>
int accept4(int sockfd, struct sockaddr *restrict addr, socklen_t *restrict addrlen, int flags);
The argument addr is a pointer to a sockaddr structure. This structure is filled in with the address of the peer socket, as known to the communications layer. The exact format of the address returned addr is determined by the socket's address family (see socket(2) and the respective protocol man pages). When addr is NULL, nothing is filled in; in this case, addrlen is not used, and should also be NULL.
The addrlen argument is a value-result argument: the caller must initialize it to contain the size (in bytes) of the structure pointed to by addr; on return it will contain the actual size of the peer address.
The returned address is truncated if the buffer provided is too small; in this case, addrlen will return a value greater than was supplied to the call.
If no pending connections are present on the queue, and the socket is not marked as nonblocking, accept() blocks the caller until a connection is present. If the socket is marked nonblocking and no pending connections are present on the queue, accept() fails with the error EAGAIN or EWOULDBLOCK.
In order to be notified of incoming connections on a socket, you can use select(2), poll(2), or epoll(7). A readable event will be delivered when a new connection is attempted and you may then call accept() to get a socket for that connection. Alternatively, you can set the socket to deliver SIGIO when activity occurs on a socket; see socket(7) for details.
If flags is 0, then accept4() is the same as accept(). The following values can be bitwise ORed in flags to obtain different behavior:
- Set the O_NONBLOCK file status flag on the open file description (see open(2)) referred to by the new file descriptor. Using this flag saves extra calls to fcntl(2) to achieve the same result.
- Set the close-on-exec (FD_CLOEXEC) flag on the new file descriptor. See the description of the O_CLOEXEC flag in open(2) for reasons why this may be useful.
- EAGAIN or EWOULDBLOCK
- The socket is marked nonblocking and no connections are present to be accepted. POSIX.1-2001 and POSIX.1-2008 allow either error to be returned for this case, and do not require these constants to have the same value, so a portable application should check for both possibilities.
- sockfd is not an open file descriptor.
- A connection has been aborted.
- The addr argument is not in a writable part of the user address space.
- The system call was interrupted by a signal that was caught before a valid connection arrived; see signal(7).
- Socket is not listening for connections, or addrlen is invalid (e.g., is negative).
- (accept4()) invalid value in flags.
- The per-process limit on the number of open file descriptors has been reached.
- The system-wide limit on the total number of open files has been reached.
- ENOBUFS, ENOMEM
- Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory.
- The file descriptor sockfd does not refer to a socket.
- The referenced socket is not of type SOCK_STREAM.
- Firewall rules forbid connection.
- Protocol error.
In addition, network errors for the new socket and as defined for the protocol may be returned. Various Linux kernels can return other errors such as ENOSR, ESOCKTNOSUPPORT, EPROTONOSUPPORT, ETIMEDOUT. The value ERESTARTSYS may be seen during a trace.
accept4() is a nonstandard Linux extension.
On Linux, the new socket returned by accept() does not inherit file status flags such as O_NONBLOCK and O_ASYNC from the listening socket. This behavior differs from the canonical BSD sockets implementation. Portable programs should not rely on inheritance or noninheritance of file status flags and always explicitly set all required flags on the socket returned from accept().
For certain protocols which require an explicit confirmation, such as DECnet, accept() can be thought of as merely dequeuing the next connection request and not implying confirmation. Confirmation can be implied by a normal read or write on the new file descriptor, and rejection can be implied by closing the new socket. Currently, only DECnet has these semantics on Linux.