]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/lib/libc/sys/listen.2
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / lib / libc / sys / listen.2
1 .\" Copyright (c) 1983, 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. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     From: @(#)listen.2      8.2 (Berkeley) 12/11/93
33 .\" $FreeBSD$
34 .\"
35 .Dd August 29, 2005
36 .Dt LISTEN 2
37 .Os
38 .Sh NAME
39 .Nm listen
40 .Nd listen for connections on a socket
41 .Sh LIBRARY
42 .Lb libc
43 .Sh SYNOPSIS
44 .In sys/types.h
45 .In sys/socket.h
46 .Ft int
47 .Fn listen "int s" "int backlog"
48 .Sh DESCRIPTION
49 To accept connections, a socket
50 is first created with
51 .Xr socket 2 ,
52 a willingness to accept incoming connections and
53 a queue limit for incoming connections are specified with
54 .Fn listen ,
55 and then the connections are
56 accepted with
57 .Xr accept 2 .
58 The
59 .Fn listen
60 system call applies only to sockets of type
61 .Dv SOCK_STREAM
62 or
63 .Dv SOCK_SEQPACKET .
64 .Pp
65 The
66 .Fa backlog
67 argument defines the maximum length the queue of
68 pending connections may grow to.
69 The real maximum queue length will be 1.5 times more than the value
70 specified in the
71 .Fa backlog
72 argument.
73 A subsequent
74 .Fn listen
75 system call on the listening socket allows the caller to change the maximum
76 queue length using a new
77 .Fa backlog
78 argument.
79 If a connection
80 request arrives with the queue full the client may
81 receive an error with an indication of
82 .Er ECONNREFUSED ,
83 or, in the case of TCP, the connection will be
84 silently dropped.
85 .Pp
86 Current queue lengths of listening sockets can be queried using
87 .Xr netstat 1
88 command.
89 .Pp
90 Note that before
91 .Fx 4.5
92 and the introduction of the syncache,
93 the
94 .Fa backlog
95 argument also determined the length of the incomplete
96 connection queue, which held TCP sockets in the process
97 of completing TCP's 3-way handshake.
98 These incomplete connections
99 are now held entirely in the syncache, which is unaffected by
100 queue lengths.
101 Inflated
102 .Fa backlog
103 values to help handle denial
104 of service attacks are no longer necessary.
105 .Pp
106 The
107 .Xr sysctl 3
108 MIB variable
109 .Dq Va kern.ipc.somaxconn
110 specifies a hard limit on
111 .Fa backlog ;
112 if a value greater than
113 .Va kern.ipc.somaxconn
114 or less than zero is specified,
115 .Fa backlog
116 is silently forced to
117 .Va kern.ipc.somaxconn .
118 .Sh INTERACTION WITH ACCEPT FILTERS
119 When accept filtering is used on a socket, a second queue will
120 be used to hold sockets that have connected, but have not yet
121 met their accept filtering criteria.
122 Once the criteria has been
123 met, these sockets will be moved over into the completed connection
124 queue to be
125 .Xr accept 2 Ns ed .
126 If this secondary queue is full and a
127 new connection comes in, the oldest socket which has not yet met
128 its accept filter criteria will be terminated.
129 .Pp
130 This secondary queue, like the primary listen queue, is sized
131 according to the
132 .Fa backlog
133 argument.
134 .Sh RETURN VALUES
135 .Rv -std listen
136 .Sh ERRORS
137 The
138 .Fn listen
139 system call
140 will fail if:
141 .Bl -tag -width Er
142 .It Bq Er EBADF
143 The argument
144 .Fa s
145 is not a valid descriptor.
146 .It Bq Er EINVAL
147 The socket is already connected, or in the process of being connected.
148 .It Bq Er ENOTSOCK
149 The argument
150 .Fa s
151 is not a socket.
152 .It Bq Er EOPNOTSUPP
153 The socket is not of a type that supports the operation
154 .Fn listen .
155 .El
156 .Sh SEE ALSO
157 .Xr netstat 1 ,
158 .Xr accept 2 ,
159 .Xr connect 2 ,
160 .Xr socket 2 ,
161 .Xr sysctl 3 ,
162 .Xr sysctl 8 ,
163 .Xr accept_filter 9
164 .Sh HISTORY
165 The
166 .Fn listen
167 system call appeared in
168 .Bx 4.2 .
169 The ability to configure the maximum
170 .Fa backlog
171 at run-time, and to use a negative
172 .Fa backlog
173 to request the maximum allowable value, was introduced in
174 .Fx 2.2 .