5331 want sockaddr(3SOCKET)
[illumos-gate.git] / usr / src / man / man3socket / accept.3socket
blobc253ba3aea289e23e011af03286d3868c3f5195e
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>
18 \fBint\fR \fBaccept\fR(\fBint\fR \fIs\fR, \fBstruct sockaddr *\fR\fIaddr\fR, \fBsocklen_t *\fR\fIaddrlen\fR);
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
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
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
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
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
110 \fBaccept()\fR and \fBaccept4()\fR will fail if:
112 .ne 2
114 \fB\fBEBADF\fR\fR
116 .RS 16n
117 The descriptor is invalid.
121 .ne 2
123 \fB\fBECONNABORTED\fR\fR
125 .RS 16n
126 The remote side aborted the connection before the \fBaccept()\fR operation
127 completed.
131 .ne 2
133 \fB\fBEFAULT\fR\fR
135 .RS 16n
136 The \fIaddr\fR parameter or the \fIaddrlen\fR parameter is invalid.
140 .ne 2
142 \fB\fBEINTR\fR\fR
144 .RS 16n
145 The \fBaccept()\fR attempt was interrupted by the delivery of a signal.
149 .ne 2
151 \fB\fBEMFILE\fR\fR
153 .RS 16n
154 The per-process descriptor table is full.
158 .ne 2
160 \fB\fBENODEV\fR\fR
162 .RS 16n
163 The protocol family and type corresponding to  \fIs\fR could not be found in
164 the \fBnetconfig\fR file.
168 .ne 2
170 \fB\fBENOMEM\fR\fR
172 .RS 16n
173 There was insufficient user memory available to complete the operation.
177 .ne 2
179 \fB\fBENOSR\fR\fR
181 .RS 16n
182 There were insufficient \fBSTREAMS\fR resources available to complete the
183 operation.
187 .ne 2
189 \fB\fBENOTSOCK\fR\fR
191 .RS 16n
192 The descriptor does not reference a socket.
196 .ne 2
198 \fB\fBEOPNOTSUPP\fR\fR
200 .RS 16n
201 The referenced socket is not of type \fBSOCK_STREAM\fR.
205 .ne 2
207 \fB\fBEPROTO\fR\fR
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.
215 .ne 2
217 \fB\fBEWOULDBLOCK\fR\fR
219 .RS 16n
220 The socket is marked as non-blocking and no connections are present to be
221 accepted.
226 \fBAdditionally, \fBaccept4()\fR will fail if:
228 .ne 2
230 \fB\fBEINVAL\fR\fR
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.
238 .SH ATTRIBUTES
240 See \fBattributes\fR(5) for descriptions of the following attributes:
245 box;
246 c | c
247 l | l .
248 ATTRIBUTE TYPE  ATTRIBUTE VALUE
250 MT-Level        Safe
253 .SH SEE ALSO
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