accept.2 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. .\" Copyright (c) 1983, 1990, 1991, 1993
  2. .\" The Regents of the University of California. All rights reserved.
  3. .\"
  4. .\" Redistribution and use in source and binary forms, with or without
  5. .\" modification, are permitted provided that the following conditions
  6. .\" are met:
  7. .\" 1. Redistributions of source code must retain the above copyright
  8. .\" notice, this list of conditions and the following disclaimer.
  9. .\" 2. Redistributions in binary form must reproduce the above copyright
  10. .\" notice, this list of conditions and the following disclaimer in the
  11. .\" documentation and/or other materials provided with the distribution.
  12. .\" 3. Neither the name of the University nor the names of its contributors
  13. .\" may be used to endorse or promote products derived from this software
  14. .\" without specific prior written permission.
  15. .\"
  16. .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  17. .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  20. .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. .\" SUCH DAMAGE.
  27. .\"
  28. .Dd October 9, 2014
  29. .Dt ACCEPT 2
  30. .Os
  31. .Sh NAME
  32. .Nm accept ,
  33. .Nm accept4
  34. .Nd accept a connection on a socket
  35. .Sh LIBRARY
  36. .Lb libc
  37. .Sh SYNOPSIS
  38. .In sys/types.h
  39. .In sys/socket.h
  40. .Ft int
  41. .Fn accept "int s" "struct sockaddr * restrict addr" "socklen_t * restrict addrlen"
  42. .Ft int
  43. .Fn accept4 "int s" "struct sockaddr * restrict addr" "socklen_t * restrict addrlen" "int flags"
  44. .Sh DESCRIPTION
  45. The argument
  46. .Fa s
  47. is a socket that has been created with
  48. .Xr socket 2 ,
  49. bound to an address with
  50. .Xr bind 2 ,
  51. and is listening for connections after a
  52. .Xr listen 2 .
  53. The
  54. .Fn accept
  55. system call extracts the first connection request on the
  56. queue of pending connections, creates a new socket,
  57. and allocates a new file descriptor for the socket which
  58. inherits the state of the
  59. .Dv O_NONBLOCK
  60. and
  61. .Dv O_ASYNC
  62. properties and the destination of
  63. .Dv SIGIO
  64. and
  65. .Dv SIGURG
  66. signals from the original socket
  67. .Fa s .
  68. .Pp
  69. The
  70. .Fn accept4
  71. system call is similar,
  72. but the
  73. .Dv O_NONBLOCK
  74. property of the new socket is instead determined by the
  75. .Dv SOCK_NONBLOCK
  76. flag in the
  77. .Fa flags
  78. argument,
  79. the
  80. .Dv O_ASYNC
  81. property is cleared,
  82. the signal destination is cleared
  83. and the close-on-exec flag on the new file descriptor can be set via the
  84. .Dv SOCK_CLOEXEC
  85. flag in the
  86. .Fa flags
  87. argument.
  88. .Pp
  89. If no pending connections are
  90. present on the queue, and the original socket
  91. is not marked as non-blocking,
  92. .Fn accept
  93. blocks the caller until a connection is present.
  94. If the original socket
  95. is marked non-blocking and no pending
  96. connections are present on the queue,
  97. .Fn accept
  98. returns an error as described below.
  99. The accepted socket
  100. may not be used
  101. to accept more connections.
  102. The original socket
  103. .Fa s
  104. remains open.
  105. .Pp
  106. The argument
  107. .Fa addr
  108. is a result argument that is filled-in with
  109. the address of the connecting entity,
  110. as known to the communications layer.
  111. The exact format of the
  112. .Fa addr
  113. argument is determined by the domain in which the communication
  114. is occurring.
  115. A null pointer may be specified for
  116. .Fa addr
  117. if the address information is not desired;
  118. in this case,
  119. .Fa addrlen
  120. is not used and should also be null.
  121. Otherwise, the
  122. .Fa addrlen
  123. argument
  124. is a value-result argument; it should initially contain the
  125. amount of space pointed to by
  126. .Fa addr ;
  127. on return it will contain the actual length (in bytes) of the
  128. address returned.
  129. This call
  130. is used with connection-based socket types, currently with
  131. .Dv SOCK_STREAM .
  132. .Pp
  133. It is possible to
  134. .Xr select 2
  135. a socket for the purposes of doing an
  136. .Fn accept
  137. by selecting it for read.
  138. .Pp
  139. For certain protocols which require an explicit confirmation,
  140. such as
  141. .Tn ISO
  142. or
  143. .Tn DATAKIT ,
  144. .Fn accept
  145. can be thought of
  146. as merely dequeueing the next connection
  147. request and not implying confirmation.
  148. Confirmation can be implied by a normal read or write on the new
  149. file descriptor, and rejection can be implied by closing the
  150. new socket.
  151. .Pp
  152. For some applications, performance may be enhanced by using an
  153. .Xr accept_filter 9
  154. to pre-process incoming connections.
  155. .Pp
  156. When using
  157. .Fn accept ,
  158. portable programs should not rely on the
  159. .Dv O_NONBLOCK
  160. and
  161. .Dv O_ASYNC
  162. properties and the signal destination being inherited,
  163. but should set them explicitly using
  164. .Xr fcntl 2 ;
  165. .Fn accept4
  166. sets these properties consistently,
  167. but may not be fully portable across
  168. .Ux
  169. platforms.
  170. .Sh RETURN VALUES
  171. These calls return \-1 on error.
  172. If they succeed, they return a non-negative
  173. integer that is a descriptor for the accepted socket.
  174. .Sh ERRORS
  175. The
  176. .Fn accept
  177. and
  178. .Fn accept4
  179. system calls will fail if:
  180. .Bl -tag -width Er
  181. .It Bq Er EBADF
  182. The descriptor is invalid.
  183. .It Bq Er EINTR
  184. The
  185. .Fn accept
  186. operation was interrupted.
  187. .It Bq Er EMFILE
  188. The per-process descriptor table is full.
  189. .It Bq Er ENFILE
  190. The system file table is full.
  191. .It Bq Er ENOTSOCK
  192. The descriptor references a file, not a socket.
  193. .It Bq Er EINVAL
  194. .Xr listen 2
  195. has not been called on the socket descriptor.
  196. .It Bq Er EFAULT
  197. The
  198. .Fa addr
  199. argument is not in a writable part of the
  200. user address space.
  201. .It Bo Er EWOULDBLOCK Bc or Bq Er EAGAIN
  202. The socket is marked non-blocking and no connections
  203. are present to be accepted.
  204. .It Bq Er ECONNABORTED
  205. A connection arrived, but it was closed while waiting
  206. on the listen queue.
  207. .El
  208. .Pp
  209. The
  210. .Fn accept4
  211. system call will also fail if:
  212. .Bl -tag -width Er
  213. .It Bq Er EINVAL
  214. The
  215. .Fa flags
  216. argument is invalid.
  217. .El
  218. .Sh SEE ALSO
  219. .Xr bind 2 ,
  220. .Xr connect 2 ,
  221. .Xr getpeername 2 ,
  222. .Xr getsockname 2 ,
  223. .Xr listen 2 ,
  224. .Xr select 2 ,
  225. .Xr socket 2 ,
  226. .Xr accept_filter 9
  227. .Sh HISTORY
  228. The
  229. .Fn accept
  230. system call appeared in
  231. .Bx 4.2 .
  232. .Pp
  233. The
  234. .Fn accept4
  235. system call appeared in
  236. .Fx 10.0 .