Merge branch 'vendor/LIBEDIT'
[dragonfly.git] / lib / libc / sys / accept.2
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 .\" 4. 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 .\"     @(#)accept.2    8.2 (Berkeley) 12/11/93
29 .\" $FreeBSD: src/lib/libc/sys/accept.2,v 1.10.2.11 2002/05/09 02:24:40 silby Exp $
30 .\" $DragonFly: src/lib/libc/sys/accept.2,v 1.4 2006/06/25 10:55:51 corecode Exp $
31 .\"
32 .Dd December 11, 1993
33 .Dt ACCEPT 2
34 .Os
35 .Sh NAME
36 .Nm accept
37 .Nd accept a connection on a socket
38 .Sh LIBRARY
39 .Lb libc
40 .Sh SYNOPSIS
41 .In sys/types.h
42 .In sys/socket.h
43 .Ft int
44 .Fn accept "int s" "struct sockaddr *addr" "socklen_t *addrlen"
45 .Sh DESCRIPTION
46 The argument
47 .Fa s
48 is a socket that has been created with
49 .Xr socket 2 ,
50 bound to an address with
51 .Xr bind 2 ,
52 and is listening for connections after a
53 .Xr listen 2 .
54 The
55 .Fn accept
56 call
57 extracts the first connection request
58 on the queue of pending connections, creates
59 a new socket with the same properties as
60 .Fa s ,
61 and allocates a new file descriptor
62 for the socket.  If no pending connections are
63 present on the queue, and the socket is not marked
64 as non-blocking,
65 .Fn accept
66 blocks the caller until a connection is present.
67 If the socket is marked non-blocking and no pending
68 connections are present on the queue,
69 .Fn accept
70 returns an error as described below.
71 The accepted socket
72 may not be used
73 to accept more connections.  The original socket
74 .Fa s
75 remains open.
76 .Pp
77 The argument
78 .Fa addr
79 is a result parameter that is filled-in with
80 the address of the connecting entity,
81 as known to the communications layer.
82 The exact format of the
83 .Fa addr
84 parameter is determined by the domain in which the communication
85 is occurring.
86 To ensure that the returned address fits,
87 .Fa *addr
88 should have a size of at least
89 .Ft sizeof(struct sockaddr_storage) .
90 The
91 .Fa addrlen
92 is a value-result parameter; it should initially contain the
93 amount of space pointed to by
94 .Fa addr ;
95 on return it will contain the actual length (in bytes) of the
96 address returned.
97 This call
98 is used with connection-based socket types, currently with
99 .Dv SOCK_STREAM .
100 .Pp
101 It is possible to
102 .Xr select 2
103 a socket for the purposes of doing an
104 .Fn accept
105 by selecting it for read.
106 .Pp
107 For certain protocols which require an explicit confirmation,
108 such as
109 .Tn ISO
110 or
111 .Tn DATAKIT ,
112 .Fn accept
113 can be thought of
114 as merely dequeueing the next connection
115 request and not implying confirmation.
116 Confirmation can be implied by a normal read or write on the new
117 file descriptor, and rejection can be implied by closing the
118 new socket.
119 .Pp
120 For some applications, performance may be enhanced by using an
121 .Xr accept_filter 9
122 to pre-process incoming connections.
123 .Sh RETURN VALUES
124 The call returns \-1 on error.  If it succeeds, it returns a non-negative
125 integer that is a descriptor for the accepted socket.
126 .Sh ERRORS
127 The
128 .Fn accept
129 will fail if:
130 .Bl -tag -width Er
131 .It Bq Er EBADF
132 The descriptor is invalid.
133 .It Bq Er EINTR
134 The
135 .Fn accept
136 operation was interrupted.
137 .It Bq Er EMFILE
138 The per-process descriptor table is full.
139 .It Bq Er ENFILE
140 The system file table is full.
141 .It Bq Er ENOTSOCK
142 The descriptor references a file, not a socket.
143 .It Bq Er EINVAL
144 .Xr listen 2
145 has not been called on the socket descriptor.
146 .It Bq Er EFAULT
147 The
148 .Fa addr
149 parameter is not in a writable part of the
150 user address space.
151 .It Bq Er EWOULDBLOCK
152 The socket is marked non-blocking and no connections
153 are present to be accepted.
154 .It Bq Er ECONNABORTED
155 A connection arrived, but it was closed while waiting
156 on the listen queue.
157 .El
158 .Sh SEE ALSO
159 .Xr bind 2 ,
160 .Xr connect 2 ,
161 .Xr getpeername 2 ,
162 .Xr listen 2 ,
163 .Xr select 2 ,
164 .Xr socket 2 ,
165 .Xr accept_filter 9
166 .Sh HISTORY
167 The
168 .Fn accept
169 function appeared in
170 .Bx 4.2 .