1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
.\" Copyright (c) 1983, 1990, 1991 The Regents of the University of California.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $Id: accept.2,v 1.7 1999/05/24 11:53:23 freitag Exp $
.\"
.\" Modified Sat Jul 24 16:42:42 1993 by Rik Faith <faith@cs.unc.edu>
.\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
.\" Modified 1998,1999 by Andi Kleen to match Linux 2.2 reality
.TH ACCEPT 2 "7 May 1999" "Linux 2.2 Page" "Linux Programmer's Manual"
.SH NAME
accept \- accept a connection on a socket
.SH SYNOPSIS
.B #include <sys/types.h>
.br
.B #include <sys/socket.h>
.sp
.BI "int accept(int " s ", struct sockaddr *" addr ", socklen_t *" addrlen );
.SH DESCRIPTION
The
.B accept
function is used with connection-based socket types
.RB ( SOCK_STREAM ,
.B SOCK_SEQPACKET
and
.BR SOCK_RDM ).
It extracts the first connection request on the queue of pending
connections, creates a new connected socket with mostly the same properties as
.IR s ,
and allocates a new file descriptor for the socket, which is returned.
The newly created socket is no longer in the listening state.
The original socket
.I s
is unaffected by this call. Note that any per file descriptor flags
(everything that can be set with the
.B F_SETFL
fcntl, like non blocking or async state) are not inherited across
a
.I accept.
.PP
The argument
.I s
is a socket that has been created with
.BR socket (2),
bound to a local address with
.BR bind (2),
and is listening for connections after a
.BR listen (2).
The argument
.I addr
is a pointer to a sockaddr structure. This structure is filled in
with the address of the connecting entity,
as known to the communications layer. The exact format of the
address passed in the
.I addr
parameter is determined by the socket's family (see
.BR socket (2)
and the respective protocol man pages).
The
.I addrlen
argument is a value-result parameter: it should initially contain the
size of the structure pointed to by
.IR addr ;
on return it will contain the actual length (in bytes) of the address
returned. When
.I addr
is NULL nothing is filled in.
.PP
If no pending
connections are present on the queue, and the socket is not marked as
non-blocking,
.B accept
blocks the caller until a connection is present. If the socket is marked
non-blocking and no pending connections are present on the queue,
.B accept
returns EAGAIN.
.PP
In order to be notified of incoming connections on a socket, you can use
.BR select (2)
or
.BR poll (2).
A readable event will be delivered when a new connection is attempted and you
may then call
.B accept
to get a socket for that connection. Alternatively, you can set the socket
to deliver
.B SIGIO
when activity occurs on a socket; see
.BR socket (7)
for details.
.PP
For certain protocols which require an explicit confirmation,
such as
DECNet,
.B accept
can be thought of as merely dequeuing the next connection request and not
implying confirmation. Confirmation can be implied by
a normal read or write on the new file descriptor, and rejection can be
implied by closing the new socket. Currently only
DECNet
has these semantics on Linux.
.SH NOTES
There may not always be a connection waiting after a
.B SIGIO
is delivered or
.BR select (2)
or
.BR poll (2)
return a readability event because the connection might have been
removed by an asynchronous network error or another thread before
.B accept
is called.
If this happens then the call will block waiting for the next
connection to arrive.
To ensure that
.B accept
never blocks, the passed socket
.I s
needs to have the
.B O_NONBLOCK
flag set (see
.BR socket (7)).
.SH "RETURN VALUE"
The call returns \-1 on error. If it succeeds, it returns a non-negative
integer that is a descriptor for the accepted socket.
.SH ERROR HANDLING
Linux
.B accept
passes already-pending network errors on the new socket
as an error code from
.BR accept .
This behaviour differs from other BSD socket
implementations. For reliable operation the application should detect
the network errors defined for the protocol after
.B accept
and treat
them like
.BR EAGAIN
by retrying. In case of TCP/IP these are
.BR ENETDOWN ,
.BR EPROTO ,
.BR ENOPROTOOPT ,
.BR EHOSTDOWN ,
.BR ENONET ,
.BR EHOSTUNREACH ,
.BR EOPNOTSUPP ,
and
.BR ENETUNREACH .
.SH ERRORS
.TP
.BR EAGAIN " or " EWOULDBLOCK
The socket is marked non-blocking and no connections are
present to be accepted.
.TP
.B EBADF
The descriptor is invalid.
.TP
.B ENOTSOCK
The descriptor references a file, not a socket.
.TP
.B EOPNOTSUPP
The referenced socket is not of type
.BR SOCK_STREAM .
.TP
.B EFAULT
The
.I addr
parameter is not in a writable part of the user address space.
.TP
.B EPERM
Firewall rules forbid connection.
.TP
.B ENOBUFS, ENOMEM
Not enough free memory.
This often means that the memory allocation is limited by the socket buffer
limits, not by the system memory, but this is not 100% consistent.
.PP
In addition, network errors for the new socket and as defined
for the protocol may be returned. Various Linux kernels can
return other errors such as
.BR EMFILE ,
.BR EINVAL ,
.BR ENOSR ,
.BR ENOBUFS ,
.BR EPERM ,
.BR ECONNABORTED ,
.BR ESOCKTNOSUPPORT ,
.BR EPROTONOSUPPORT ,
.BR ETIMEDOUT ,
.BR ERESTARTSYS .
.SH CONFORMING TO
SVr4, 4.4BSD (the
.B accept
function first appeared in BSD 4.2).
The BSD man page documents five possible error returns
(EBADF, ENOTSOCK, EOPNOTSUPP, EWOULDBLOCK, EFAULT).
SUSv2 documents errors EAGAIN, EBADF, ECONNABORTED, EFAULT, EINTR,
EINVAL, EMFILE, ENFILE, ENOBUFS, ENOMEM, ENOSR, ENOTSOCK, EOPNOTSUPP,
EPROTO, EWOULDBLOCK.
.SH NOTE
The third argument of
.B accept
was originally declared as an `int *' (and is that under libc4 and libc5
and on many other systems like BSD 4.*, SunOS 4, SGI); a POSIX 1003.1g draft
standard wanted to change it into a `size_t *', and that is what it is
for SunOS 5.
Later POSIX drafts have `socklen_t *', and so do the Single Unix Specification
and glibc2.
Quoting Linus Torvalds:
.I
_Any_ sane library _must_ have "socklen_t" be the same size
as int. Anything else breaks any BSD socket layer stuff.
POSIX initially _did_ make it a size_t, and I (and hopefully others, but
obviously not too many) complained to them very loudly indeed. Making
it a size_t is completely broken, exactly because size_t very seldom is
the same size as "int" on 64-bit architectures, for example. And it
_has_ to be the same size as "int" because that's what the BSD socket
interface is.
Anyway, the POSIX people eventually got a clue, and created "socklen_t".
They shouldn't have touched it in the first place, but once they did
they felt it had to have a named type for some unfathomable reason
(probably somebody didn't like losing face over having done the original
stupid thing, so they silently just renamed their blunder).
.SH "SEE ALSO"
.BR bind (2),
.BR connect (2),
.BR listen (2),
.BR select (2),
.BR socket (2)
|