]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/listen.2
zfs: merge openzfs/zfs@8e8acabdc
[FreeBSD/FreeBSD.git] / 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. 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 .\"     From: @(#)listen.2      8.2 (Berkeley) 12/11/93
29 .\" $FreeBSD$
30 .\"
31 .Dd April 30, 2023
32 .Dt LISTEN 2
33 .Os
34 .Sh NAME
35 .Nm listen
36 .Nd listen for connections on a socket
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In sys/socket.h
41 .Ft int
42 .Fn listen "int s" "int backlog"
43 .Sh DESCRIPTION
44 To accept connections, a socket
45 is first created with
46 .Xr socket 2 ,
47 a willingness to accept incoming connections and
48 a queue limit for incoming connections are specified with
49 .Fn listen ,
50 and then the connections are
51 accepted with
52 .Xr accept 2 .
53 The
54 .Fn listen
55 system call applies only to sockets of type
56 .Dv SOCK_STREAM
57 or
58 .Dv SOCK_SEQPACKET .
59 .Pp
60 The
61 .Fa backlog
62 argument defines the maximum length the queue of
63 pending connections may grow to.
64 The real maximum queue length will be 1.5 times more than the value
65 specified in the
66 .Fa backlog
67 argument.
68 A subsequent
69 .Fn listen
70 system call on the listening socket allows the caller to change the maximum
71 queue length using a new
72 .Fa backlog
73 argument.
74 If a connection
75 request arrives with the queue full the client may
76 receive an error with an indication of
77 .Er ECONNREFUSED ,
78 or, in the case of TCP, the connection will be
79 silently dropped.
80 .Pp
81 Current queue lengths of listening sockets can be queried using
82 .Xr netstat 1
83 command.
84 .Pp
85 Note that before
86 .Fx 4.5
87 and the introduction of the syncache,
88 the
89 .Fa backlog
90 argument also determined the length of the incomplete
91 connection queue, which held TCP sockets in the process
92 of completing TCP's 3-way handshake.
93 These incomplete connections
94 are now held entirely in the syncache, which is unaffected by
95 queue lengths.
96 Inflated
97 .Fa backlog
98 values to help handle denial
99 of service attacks are no longer necessary.
100 .Pp
101 The
102 .Xr sysctl 3
103 MIB variable
104 .Va kern.ipc.soacceptqueue
105 specifies a hard limit on
106 .Fa backlog ;
107 if a value greater than
108 .Va kern.ipc.soacceptqueue
109 or less than zero is specified,
110 .Fa backlog
111 is silently forced to
112 .Va kern.ipc.soacceptqueue .
113 .Pp
114 If the listen queue overflows, the kernel will emit a syslog message
115 using default priority LOG_DEBUG (7).
116 The
117 .Xr sysctl 3
118 MIB variable
119 .Va kern.ipc.sooverprio
120 may be used to change this priority to any value in a range of 0..7
121 (LOG_EMERG..LOG_DEBUG).
122 See
123 .Xr syslog 3
124 for details.
125 It may be set to -1 to disable these messages.
126 .Pp
127 The variable
128 .Va kern.ipc.sooverinterval
129 specifies a per-socket limit on how often the kernel will emit these messages.
130 .Sh INTERACTION WITH ACCEPT FILTERS
131 When accept filtering is used on a socket, a second queue will
132 be used to hold sockets that have connected, but have not yet
133 met their accept filtering criteria.
134 Once the criteria has been
135 met, these sockets will be moved over into the completed connection
136 queue to be
137 .Xr accept 2 Ns ed .
138 If this secondary queue is full and a
139 new connection comes in, the oldest socket which has not yet met
140 its accept filter criteria will be terminated.
141 .Pp
142 This secondary queue, like the primary listen queue, is sized
143 according to the
144 .Fa backlog
145 argument.
146 .Sh RETURN VALUES
147 .Rv -std listen
148 .Sh ERRORS
149 The
150 .Fn listen
151 system call
152 will fail if:
153 .Bl -tag -width Er
154 .It Bq Er EBADF
155 The argument
156 .Fa s
157 is not a valid descriptor.
158 .It Bq Er EDESTADDRREQ
159 The socket is not bound to a local address, and the protocol does not
160 support listening on an unbound socket.
161 .It Bq Er EINVAL
162 The socket is already connected, or in the process of being connected.
163 .It Bq Er ENOTSOCK
164 The argument
165 .Fa s
166 is not a socket.
167 .It Bq Er EOPNOTSUPP
168 The socket is not of a type that supports the operation
169 .Fn listen .
170 .El
171 .Sh SEE ALSO
172 .Xr netstat 1 ,
173 .Xr accept 2 ,
174 .Xr connect 2 ,
175 .Xr socket 2 ,
176 .Xr sysctl 3 ,
177 .Xr syslog 3 ,
178 .Xr sysctl 8 ,
179 .Xr accept_filter 9
180 .Sh HISTORY
181 The
182 .Fn listen
183 system call appeared in
184 .Bx 4.2 .
185 The ability to configure the maximum
186 .Fa backlog
187 at run-time, and to use a negative
188 .Fa backlog
189 to request the maximum allowable value, was introduced in
190 .Fx 2.2 .
191 The
192 .Va kern.ipc.somaxconn
193 .Xr sysctl 3
194 has been replaced with
195 .Va kern.ipc.soacceptqueue
196 in
197 .Fx 10.0
198 to prevent confusion about its actual functionality.
199 The original
200 .Xr sysctl 3
201 .Va kern.ipc.somaxconn
202 is still available but hidden from a
203 .Xr sysctl 3
204 -a output so that existing applications and scripts continue to work.