1 '\" te
   2 .\" Copyright 1989 AT&T
   3 .\" Copyright (C) 2002, Sun Microsystems, Inc. All Rights Reserved
   4 .\" Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved.
   5 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
   6 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
   7 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
   8 .TH ACCEPT 3SOCKET "Apr 19, 2013"
   9 .SH NAME
  10 accept \- accept a connection on a socket
  11 .SH SYNOPSIS
  12 .LP
  13 .nf
  14 \fBcc\fR [ \fIflag\fR ... ] \fIfile\fR ... \fB-lsocket\fR \fB -lnsl \fR [ \fIlibrary\fR ... ]
  15 #include <sys/types.h>
  16 #include <sys/socket.h>
  17 
  18 \fBint\fR \fBaccept\fR(\fBint\fR \fIs\fR, \fBstruct sockaddr *\fR\fIaddr\fR, \fBsocklen_t *\fR\fIaddrlen\fR);
  19 
  20 \fBint\fR \fBaccept4\fR(\fBint\fR \fIs\fR, \fBstruct sockaddr *\fR\fIaddr\fR, \fBsocklen_t *\fR\fIaddrlen\fR,
  21     \fBint\fR \fIflags\fR);
  22 .fi
  23 
  24 .SH DESCRIPTION
  25 .LP
  26 The argument \fIs\fR is a socket that has been created with
  27 \fBsocket\fR(3SOCKET) and bound to an address with \fBbind\fR(3SOCKET), and
  28 that is listening for connections after a call to \fBlisten\fR(3SOCKET). The
  29 \fBaccept()\fR function extracts the first connection on the queue of pending
  30 connections, creates a new socket with the properties of \fIs\fR, and allocates
  31 a new file descriptor, \fIns\fR, for the socket. If no pending connections are
  32 present on the queue and the socket is not marked as non-blocking,
  33 \fBaccept()\fR blocks the caller until a connection is present. If the socket
  34 is marked as non-blocking and no pending connections are present on the queue,
  35 \fBaccept()\fR returns an error as described below.  The \fBaccept()\fR
  36 function uses the \fBnetconfig\fR(4) file to determine the \fBSTREAMS\fR device
  37 file name associated with \fIs\fR.  This is the device on which the connect
  38 indication will be accepted. The accepted socket, \fIns\fR, is used to read and
  39 write data to and from the socket that connected to \fIns\fR. It is not used to
  40 accept more connections. The original socket (\fIs\fR) remains open for
  41 accepting further connections.
  42 .sp
  43 .LP
  44 The argument \fIaddr\fR is a result parameter that is filled in with the
  45 address of the connecting entity as it is known to the communications layer.
  46 The exact format of the \fIaddr\fR parameter is determined by the domain in
  47 which the communication occurs.
  48 .sp
  49 .LP
  50 The argument \fIaddrlen\fR is a value-result parameter. Initially, it contains
  51 the amount of space pointed to by \fIaddr\fR; on return it contains the length
  52 in bytes of the address returned.
  53 .sp
  54 .LP
  55 The \fBaccept()\fR function is used with connection-based socket types,
  56 currently with \fBSOCK_STREAM\fR.
  57 .sp
  58 .LP
  59 The \fBaccept4()\fR function allows flags that control the behavior of a
  60 successfully accepted socket.  If \fIflags\fR is 0, \fBaccept4()\fR acts
  61 identically to \fBaccept()\fR. Values for \fIflags\fR are constructed by
  62 a bitwise-inclusive-OR of flags from the following list, defined in
  63 <sys/socketvar.h>.
  64 .sp
  65 .ne 2
  66 .na
  67 \fB\fBSOCK_CLOEXEC\fR\fR
  68 .ad
  69 .RS 12n
  70 The accepted socket will have the FD_CLOEXEC flag set as if \fBfcntl()\fR
  71 was called on it.  This flag is set before the socket is passed to the
  72 caller thus avoiding the race condition between \fBaccept()\fR and
  73 \fBfcntl()\fR.  See, \fBO_CLOEXEC\fR in \fBopen(2)\fR for more details.
  74 .RE
  75 
  76 .sp
  77 .ne 2
  78 .na
  79 \fB\fBSOCK_NDELAY\fR\fR
  80 .ad
  81 .RS 12n
  82 The accepted socket will have the \fBO_NDELAY\fR flag set as if \fBfcntl()\fR
  83 was called on it.  This sets the socket into non-blocking mode.  See
  84 \fBO_NDELAY\fR in \fBfcntl.h(3HEAD)\fR for more details.
  85 .RE
  86 
  87 .sp
  88 .ne 2
  89 .na
  90 \fB\fBSOCK_NONBLOCK\fR\fR
  91 .ad
  92 .RS 12n
  93 The accepted socket will have the \fBO_NONBLOCK\fR flag set as if
  94 \fBfcntl()\fR was called on it.  This sets the socket into non-blocking mode
  95 (POSIX; see \fBstandards(5)\fR).  See \fBO_NONBLOCK\fR in \fBfcntl.h(3HEAD)\fR
  96 for more details.
  97 .RE
  98 .sp
  99 .LP
 100 It is possible to \fBselect\fR(3C) or \fBpoll\fR(2) a socket for the purpose of
 101 an \fBaccept()\fR by selecting or polling it for a read. However, this will
 102 only indicate when a connect indication is pending; it is still necessary to
 103 call \fBaccept()\fR.
 104 .SH RETURN VALUES
 105 .LP
 106 The \fBaccept()\fR function returns \fB\(mi1\fR on error. If it succeeds, it
 107 returns a non-negative integer that is a descriptor for the accepted socket.
 108 .SH ERRORS
 109 .LP
 110 \fBaccept()\fR and \fBaccept4()\fR will fail if:
 111 .sp
 112 .ne 2
 113 .na
 114 \fB\fBEBADF\fR\fR
 115 .ad
 116 .RS 16n
 117 The descriptor is invalid.
 118 .RE
 119 
 120 .sp
 121 .ne 2
 122 .na
 123 \fB\fBECONNABORTED\fR\fR
 124 .ad
 125 .RS 16n
 126 The remote side aborted the connection before the \fBaccept()\fR operation
 127 completed.
 128 .RE
 129 
 130 .sp
 131 .ne 2
 132 .na
 133 \fB\fBEFAULT\fR\fR
 134 .ad
 135 .RS 16n
 136 The \fIaddr\fR parameter or the \fIaddrlen\fR parameter is invalid.
 137 .RE
 138 
 139 .sp
 140 .ne 2
 141 .na
 142 \fB\fBEINTR\fR\fR
 143 .ad
 144 .RS 16n
 145 The \fBaccept()\fR attempt was interrupted by the delivery of a signal.
 146 .RE
 147 
 148 .sp
 149 .ne 2
 150 .na
 151 \fB\fBEMFILE\fR\fR
 152 .ad
 153 .RS 16n
 154 The per-process descriptor table is full.
 155 .RE
 156 
 157 .sp
 158 .ne 2
 159 .na
 160 \fB\fBENODEV\fR\fR
 161 .ad
 162 .RS 16n
 163 The protocol family and type corresponding to  \fIs\fR could not be found in
 164 the \fBnetconfig\fR file.
 165 .RE
 166 
 167 .sp
 168 .ne 2
 169 .na
 170 \fB\fBENOMEM\fR\fR
 171 .ad
 172 .RS 16n
 173 There was insufficient user memory available to complete the operation.
 174 .RE
 175 
 176 .sp
 177 .ne 2
 178 .na
 179 \fB\fBENOSR\fR\fR
 180 .ad
 181 .RS 16n
 182 There were insufficient \fBSTREAMS\fR resources available to complete the
 183 operation.
 184 .RE
 185 
 186 .sp
 187 .ne 2
 188 .na
 189 \fB\fBENOTSOCK\fR\fR
 190 .ad
 191 .RS 16n
 192 The descriptor does not reference a socket.
 193 .RE
 194 
 195 .sp
 196 .ne 2
 197 .na
 198 \fB\fBEOPNOTSUPP\fR\fR
 199 .ad
 200 .RS 16n
 201 The referenced socket is not of type \fBSOCK_STREAM\fR.
 202 .RE
 203 
 204 .sp
 205 .ne 2
 206 .na
 207 \fB\fBEPROTO\fR\fR
 208 .ad
 209 .RS 16n
 210 A protocol error has occurred; for example, the \fBSTREAMS\fR protocol stack
 211 has not been initialized or the connection has already been released.
 212 .RE
 213 
 214 .sp
 215 .ne 2
 216 .na
 217 \fB\fBEWOULDBLOCK\fR\fR
 218 .ad
 219 .RS 16n
 220 The socket is marked as non-blocking and no connections are present to be
 221 accepted.
 222 .RE
 223 
 224 .sp
 225 .LP
 226 Additionally, \fBaccept4()\fR will fail if:
 227 .sp
 228 .ne 2
 229 .na
 230 \fB\fBEINVAL\fR\fR
 231 .ad
 232 .RS 16n
 233 The \fIflags\fR value is invalid. The \fIflags\fR argument can only be the
 234 bitwise inclusive-OR of \fBSOCK_CLOEXEC\fR, \fBSOCK_NONBLOCK\fR, and
 235 \fBSOCK_NDELAY\fR.
 236 .RE
 237 
 238 .SH ATTRIBUTES
 239 .LP
 240 See \fBattributes\fR(5) for descriptions of the following attributes:
 241 .sp
 242 
 243 .sp
 244 .TS
 245 box;
 246 c | c
 247 l | l .
 248 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 249 _
 250 MT-Level        Safe
 251 .TE
 252 
 253 .SH SEE ALSO
 254 .LP
 255 \fBpoll\fR(2), \fBbind\fR(3SOCKET), \fBconnect\fR(3SOCKET),
 256 \fBlisten\fR(3SOCKET), \fBsockaddr\fR(3SOCKET), \fBselect\fR(3C),
 257 \fBsocket.h\fR(3HEAD), \fBsocket\fR(3SOCKET), \fBnetconfig\fR(4),
 258 \fBattributes\fR(5), \fBfcntl.h(3HEAD)\fR, \fBfcntl(2)\fR, \fBstandards(5)\fR