1 ACCEPT(3C) Standard C Library Functions ACCEPT(3C) 2 3 NAME 4 accept - accept a connection on a socket 5 6 LIBRARY 7 Standard C Library (libc, -lc) 8 9 SYNOPSIS 10 #include <sys/types.h> 11 #include <sys/socket.h> 12 13 int 14 accept(int s, struct sockaddr *restrict addr, socklen_t *addrlen); 15 16 int 17 accept4(int s, struct sockaddr *restrict addr, socklen_t *addrlen, 18 int flags); 19 20 DESCRIPTION 21 The argument s is a socket that has been created with socket(3C) and 22 bound to an address with bind(3C), and that is listening for connections 23 after a call to listen(3C). The accept() function extracts the first 24 connection on the queue of pending connections, creates a new socket with 25 the properties of s, and allocates a new file descriptor, ns, for the 26 socket. If no pending connections are present on the queue and the 27 socket is not marked as non-blocking, accept() blocks the caller until a 28 connection is present. If the socket is marked as non-blocking and no 29 pending connections are present on the queue, accept() returns an error 30 as described below. The accept() function uses the netconfig(4) file to 31 determine the STREAMS device file name associated with s. This is the 32 device on which the connect indication will be accepted. The accepted 33 socket, ns, is used to read and write data to and from the socket that 34 connected to ns. It is not used to accept more connections. The 35 original socket (s) remains open for accepting further connections. 36 37 The argument addr is a result parameter that is filled in with the 38 address of the connecting entity as it is known to the communications 39 layer. The exact format of the addr parameter is determined by the 40 domain in which the communication occurs. 41 42 The argument addrlen is a value-result parameter. Initially, it contains 43 the amount of space pointed to by addr; on return it contains the length 44 in bytes of the address returned. 45 46 The accept() function is used with connection-based socket types, 47 currently with SOCK_STREAM. 48 49 The accept4() function allows flags that control the behavior of a 50 successfully accepted socket. If flags is 0, accept4() acts identically 51 to accept(). Values for flags are constructed by a bitwise-inclusive-OR 52 of flags from the following list, defined in <sys/socketvar.h>: 53 54 SOCK_CLOEXEC 55 The accepted socket will have the FD_CLOEXEC flag set as if 56 fcntl(2) was called on it. This flag is set before the socket is 57 passed to the caller thus avoiding the race condition between 58 accept() and fcntl(2). See O_CLOEXEC in open(2) for more 59 details. 60 61 SOCK_NDELAY 62 The accepted socket will have the O_NDELAY flag set as if 63 fcntl(2) was called on it. This sets the socket into non- 64 blocking mode. See O_NDELAY in fcntl.h(3HEAD) for more details. 65 66 SOCK_NONBLOCK 67 The accepted socket will have the O_NONBLOCK flag set as if 68 fcntl(2) was called on it. This sets the socket into non- 69 blocking mode (POSIX; see standards(5)). See O_NONBLOCK in 70 fcntl.h(3HEAD) for more details. 71 72 It is possible to select(3C) or poll(2) a socket for the purpose of an 73 accept() by selecting or polling it for a read. However, this will only 74 indicate when a connect indication is pending; it is still necessary to 75 call accept(). 76 77 RETURN VALUES 78 The accept() function returns -1 on error. If it succeeds, it returns a 79 non-negative integer that is a descriptor for the accepted socket. 80 81 ERRORS 82 accept() and accept4() will fail if: 83 84 [EBADF] The descriptor is invalid. 85 86 [ECONNABORTED] The remote side aborted the connection before the 87 accept() operation completed. 88 89 [EFAULT] The addr parameter or the addrlen parameter is 90 invalid. 91 92 [EINTR] The accept() attempt was interrupted by the delivery 93 of a signal. 94 95 [EMFILE] The per-process descriptor table is full. 96 97 [ENODEV] The protocol family and type corresponding to s could 98 not be found in the netconfig file. 99 100 [ENOMEM] There was insufficient user memory available to 101 complete the operation. 102 103 [ENOSR] There were insufficient STREAMS resources available to 104 complete the operation. 105 106 [ENOTSOCK] The descriptor does not reference a socket. 107 108 [EOPNOTSUPP] The referenced socket is not of type SOCK_STREAM. 109 110 [EPROTO] A protocol error has occurred; for example, the 111 STREAMS protocol stack has not been initialized or the 112 connection has already been released. 113 114 [EWOULDBLOCK] The socket is marked as non-blocking and no 115 connections are present to be accepted. 116 117 Additionally, accept4() will fail if: 118 119 [EINVAL] The flags value is invalid. The flags argument can 120 only be the bitwise inclusive-OR of SOCK_CLOEXEC, 121 SOCK_NONBLOCK, and SOCK_NDELAY. 122 123 MT-LEVEL 124 Safe 125 126 SEE ALSO 127 fcntl(2), poll(2), bind(3C), connect(3C), listen(3C), select(3C), 128 sockaddr(3C), socket(3C), fcntl.h(3HEAD), socket.h(3HEAD), netconfig(4), 129 attributes(5), standards(5) 130 131 illumos August 2, 2018 illumos