This system call is used with connection-based socket types (SOCK_STREAM, SOCK_SEQPACKET). It extracts the first connection request on the queue of pending connections, creates a new connected socket, and returns a new file descriptor referring to that socket. The newly created socket is not in the listening state.
If no pending connections are present on the queue, and the socket is not marked as non-blocking, SYS_ACCEPT blocks the caller until a connection is present. If the socket is marked non-blocking and no pending connections are present on the queue, SYS_ACCEPT fails with -EAGAIN.
In order to be notified of incoming connections on a socket, you can use sys_select or sys_poll. A readable event will be delivered when a new connection is attempted and you may then call SYS_ACCEPT to get a socket for that connection. Alternatively, you can set the socket to deliver SIGIO when activity occurs on a socket.
For certain protocols which require an explicit confirmation, such as DECNet, SYS_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.
Arguments:
1st |
Socket descriptor of a socket that has been created with SYS_SOCKET, bound to a local address with SYS_BIND, and is listening for connections after a SYS_LISTEN. This socket descriptor is unaffected by SYS_ACCEPT. |
2nd |
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 is determined by the socket's address family (see SYS_SOCKET and the respective protocol man pages). When this argument is NULL nothing is filled in.
sockaddr defined as follows:
struc sockaddr
{
sa_family rw 1
sa_data rb 14
} |
|
3rd |
Specifies the size of the structure pointed to by 2nd argument; on return it will contain the actual length (in bytes) of the address returned. |
Return values
If the system call succeeds the return value is a non-negative integer that is a descriptor for the accepted socket.
If the system call fails the return value is one of the following errno values:
-EAGAIN,
-EWOULDBLOCK |
The socket is marked non-blocking and no connections are present to be accepted. |
-EBADF |
The descriptor is invalid. |
-ECONNABORTED |
A connection has been aborted. |
-EINTR |
The system call was interrupted by a signal that was caught before a valid connection arrived. |
-EINVAL |
Socket is not listening for connections, or 3rd argument is invalid (e.g., is negative). |
-EMFILE |
The per-process limit of open file descriptors has been reached. |
-ENFILE |
The system limit on the total number of open files has been reached. |
-ENOTSOCK |
The descriptor references a file, not a socket. |
-EOPNOTSUPP |
The referenced socket is not of type SOCK_STREAM. |
-EFAULT |
2nd argument doesn't point to a writable part of the user address space. |
-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. |
-EPROTO |
Protocol error. |
-EPERM |
Firewall rules forbid connection. |
|
Also it's important to note that SYS_ACCEPT passes already-pending network errors on the new socket as an error code from SYS_ACCEPT. This behaviour differs from other BSD socket implementations. For reliable operation the application should detect the network errors defined for the protocol after SYS_ACCEPT and treat them like -EAGAIN by retrying. In case of TCP/IP these are ENETDOWN, EPROTO, ENOPROTOOPT, EHOSTDOWN, ENONET, EHOSTUNREACH, EOPNOTSUPP, and ENETUNREACH.
Remarks
The new socket returned by SYS_ACCEPT does not inherit file status flags such as O_NONBLOCK and O_ASYNC from the listening socket.
There may not always be a connection waiting after a SIGIO is delivered or sys_select or sys_poll return a readability event because the connection might have been removed by an asynchronous network error or another thread before SYS_ACCEPT is called. If this happens then the call will block waiting for the next connection to arrive. To ensure that SYS_ACCEPT never blocks, the passed socket descriptor (1st argument) needs to have the O_NONBLOCK flag set.
Compatibility
n/a |