xref: /freebsd/lib/libsys/listen.2 (revision 8269e7673cf033aba67dab8264fe719920c70f87)
1*8269e767SBrooks Davis.\" Copyright (c) 1983, 1991, 1993
2*8269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
3*8269e767SBrooks Davis.\"
4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
6*8269e767SBrooks Davis.\" are met:
7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
8*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
10*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
11*8269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
13*8269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
14*8269e767SBrooks Davis.\"    without specific prior written permission.
15*8269e767SBrooks Davis.\"
16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8269e767SBrooks Davis.\" SUCH DAMAGE.
27*8269e767SBrooks Davis.\"
28*8269e767SBrooks Davis.Dd April 30, 2023
29*8269e767SBrooks Davis.Dt LISTEN 2
30*8269e767SBrooks Davis.Os
31*8269e767SBrooks Davis.Sh NAME
32*8269e767SBrooks Davis.Nm listen
33*8269e767SBrooks Davis.Nd listen for connections on a socket
34*8269e767SBrooks Davis.Sh LIBRARY
35*8269e767SBrooks Davis.Lb libc
36*8269e767SBrooks Davis.Sh SYNOPSIS
37*8269e767SBrooks Davis.In sys/socket.h
38*8269e767SBrooks Davis.Ft int
39*8269e767SBrooks Davis.Fn listen "int s" "int backlog"
40*8269e767SBrooks Davis.Sh DESCRIPTION
41*8269e767SBrooks DavisTo accept connections, a socket
42*8269e767SBrooks Davisis first created with
43*8269e767SBrooks Davis.Xr socket 2 ,
44*8269e767SBrooks Davisa willingness to accept incoming connections and
45*8269e767SBrooks Davisa queue limit for incoming connections are specified with
46*8269e767SBrooks Davis.Fn listen ,
47*8269e767SBrooks Davisand then the connections are
48*8269e767SBrooks Davisaccepted with
49*8269e767SBrooks Davis.Xr accept 2 .
50*8269e767SBrooks DavisThe
51*8269e767SBrooks Davis.Fn listen
52*8269e767SBrooks Davissystem call applies only to sockets of type
53*8269e767SBrooks Davis.Dv SOCK_STREAM
54*8269e767SBrooks Davisor
55*8269e767SBrooks Davis.Dv SOCK_SEQPACKET .
56*8269e767SBrooks Davis.Pp
57*8269e767SBrooks DavisThe
58*8269e767SBrooks Davis.Fa backlog
59*8269e767SBrooks Davisargument defines the maximum length the queue of
60*8269e767SBrooks Davispending connections may grow to.
61*8269e767SBrooks DavisThe real maximum queue length will be 1.5 times more than the value
62*8269e767SBrooks Davisspecified in the
63*8269e767SBrooks Davis.Fa backlog
64*8269e767SBrooks Davisargument.
65*8269e767SBrooks DavisA subsequent
66*8269e767SBrooks Davis.Fn listen
67*8269e767SBrooks Davissystem call on the listening socket allows the caller to change the maximum
68*8269e767SBrooks Davisqueue length using a new
69*8269e767SBrooks Davis.Fa backlog
70*8269e767SBrooks Davisargument.
71*8269e767SBrooks DavisIf a connection
72*8269e767SBrooks Davisrequest arrives with the queue full the client may
73*8269e767SBrooks Davisreceive an error with an indication of
74*8269e767SBrooks Davis.Er ECONNREFUSED ,
75*8269e767SBrooks Davisor, in the case of TCP, the connection will be
76*8269e767SBrooks Davissilently dropped.
77*8269e767SBrooks Davis.Pp
78*8269e767SBrooks DavisCurrent queue lengths of listening sockets can be queried using
79*8269e767SBrooks Davis.Xr netstat 1
80*8269e767SBrooks Daviscommand.
81*8269e767SBrooks Davis.Pp
82*8269e767SBrooks DavisNote that before
83*8269e767SBrooks Davis.Fx 4.5
84*8269e767SBrooks Davisand the introduction of the syncache,
85*8269e767SBrooks Davisthe
86*8269e767SBrooks Davis.Fa backlog
87*8269e767SBrooks Davisargument also determined the length of the incomplete
88*8269e767SBrooks Davisconnection queue, which held TCP sockets in the process
89*8269e767SBrooks Davisof completing TCP's 3-way handshake.
90*8269e767SBrooks DavisThese incomplete connections
91*8269e767SBrooks Davisare now held entirely in the syncache, which is unaffected by
92*8269e767SBrooks Davisqueue lengths.
93*8269e767SBrooks DavisInflated
94*8269e767SBrooks Davis.Fa backlog
95*8269e767SBrooks Davisvalues to help handle denial
96*8269e767SBrooks Davisof service attacks are no longer necessary.
97*8269e767SBrooks Davis.Pp
98*8269e767SBrooks DavisThe
99*8269e767SBrooks Davis.Xr sysctl 3
100*8269e767SBrooks DavisMIB variable
101*8269e767SBrooks Davis.Va kern.ipc.soacceptqueue
102*8269e767SBrooks Davisspecifies a hard limit on
103*8269e767SBrooks Davis.Fa backlog ;
104*8269e767SBrooks Davisif a value greater than
105*8269e767SBrooks Davis.Va kern.ipc.soacceptqueue
106*8269e767SBrooks Davisor less than zero is specified,
107*8269e767SBrooks Davis.Fa backlog
108*8269e767SBrooks Davisis silently forced to
109*8269e767SBrooks Davis.Va kern.ipc.soacceptqueue .
110*8269e767SBrooks Davis.Pp
111*8269e767SBrooks DavisIf the listen queue overflows, the kernel will emit a syslog message
112*8269e767SBrooks Davisusing default priority LOG_DEBUG (7).
113*8269e767SBrooks DavisThe
114*8269e767SBrooks Davis.Xr sysctl 3
115*8269e767SBrooks DavisMIB variable
116*8269e767SBrooks Davis.Va kern.ipc.sooverprio
117*8269e767SBrooks Davismay be used to change this priority to any value in a range of 0..7
118*8269e767SBrooks Davis(LOG_EMERG..LOG_DEBUG).
119*8269e767SBrooks DavisSee
120*8269e767SBrooks Davis.Xr syslog 3
121*8269e767SBrooks Davisfor details.
122*8269e767SBrooks DavisIt may be set to -1 to disable these messages.
123*8269e767SBrooks Davis.Pp
124*8269e767SBrooks DavisThe variable
125*8269e767SBrooks Davis.Va kern.ipc.sooverinterval
126*8269e767SBrooks Davisspecifies a per-socket limit on how often the kernel will emit these messages.
127*8269e767SBrooks Davis.Sh INTERACTION WITH ACCEPT FILTERS
128*8269e767SBrooks DavisWhen accept filtering is used on a socket, a second queue will
129*8269e767SBrooks Davisbe used to hold sockets that have connected, but have not yet
130*8269e767SBrooks Davismet their accept filtering criteria.
131*8269e767SBrooks DavisOnce the criteria has been
132*8269e767SBrooks Davismet, these sockets will be moved over into the completed connection
133*8269e767SBrooks Davisqueue to be
134*8269e767SBrooks Davis.Xr accept 2 Ns ed .
135*8269e767SBrooks DavisIf this secondary queue is full and a
136*8269e767SBrooks Davisnew connection comes in, the oldest socket which has not yet met
137*8269e767SBrooks Davisits accept filter criteria will be terminated.
138*8269e767SBrooks Davis.Pp
139*8269e767SBrooks DavisThis secondary queue, like the primary listen queue, is sized
140*8269e767SBrooks Davisaccording to the
141*8269e767SBrooks Davis.Fa backlog
142*8269e767SBrooks Davisargument.
143*8269e767SBrooks Davis.Sh RETURN VALUES
144*8269e767SBrooks Davis.Rv -std listen
145*8269e767SBrooks Davis.Sh ERRORS
146*8269e767SBrooks DavisThe
147*8269e767SBrooks Davis.Fn listen
148*8269e767SBrooks Davissystem call
149*8269e767SBrooks Daviswill fail if:
150*8269e767SBrooks Davis.Bl -tag -width Er
151*8269e767SBrooks Davis.It Bq Er EBADF
152*8269e767SBrooks DavisThe argument
153*8269e767SBrooks Davis.Fa s
154*8269e767SBrooks Davisis not a valid descriptor.
155*8269e767SBrooks Davis.It Bq Er EDESTADDRREQ
156*8269e767SBrooks DavisThe socket is not bound to a local address, and the protocol does not
157*8269e767SBrooks Davissupport listening on an unbound socket.
158*8269e767SBrooks Davis.It Bq Er EINVAL
159*8269e767SBrooks DavisThe socket is already connected, or in the process of being connected.
160*8269e767SBrooks Davis.It Bq Er ENOTSOCK
161*8269e767SBrooks DavisThe argument
162*8269e767SBrooks Davis.Fa s
163*8269e767SBrooks Davisis not a socket.
164*8269e767SBrooks Davis.It Bq Er EOPNOTSUPP
165*8269e767SBrooks DavisThe socket is not of a type that supports the operation
166*8269e767SBrooks Davis.Fn listen .
167*8269e767SBrooks Davis.El
168*8269e767SBrooks Davis.Sh SEE ALSO
169*8269e767SBrooks Davis.Xr netstat 1 ,
170*8269e767SBrooks Davis.Xr accept 2 ,
171*8269e767SBrooks Davis.Xr connect 2 ,
172*8269e767SBrooks Davis.Xr socket 2 ,
173*8269e767SBrooks Davis.Xr sysctl 3 ,
174*8269e767SBrooks Davis.Xr syslog 3 ,
175*8269e767SBrooks Davis.Xr sysctl 8 ,
176*8269e767SBrooks Davis.Xr accept_filter 9
177*8269e767SBrooks Davis.Sh HISTORY
178*8269e767SBrooks DavisThe
179*8269e767SBrooks Davis.Fn listen
180*8269e767SBrooks Davissystem call appeared in
181*8269e767SBrooks Davis.Bx 4.2 .
182*8269e767SBrooks DavisThe ability to configure the maximum
183*8269e767SBrooks Davis.Fa backlog
184*8269e767SBrooks Davisat run-time, and to use a negative
185*8269e767SBrooks Davis.Fa backlog
186*8269e767SBrooks Davisto request the maximum allowable value, was introduced in
187*8269e767SBrooks Davis.Fx 2.2 .
188*8269e767SBrooks DavisThe
189*8269e767SBrooks Davis.Va kern.ipc.somaxconn
190*8269e767SBrooks Davis.Xr sysctl 3
191*8269e767SBrooks Davishas been replaced with
192*8269e767SBrooks Davis.Va kern.ipc.soacceptqueue
193*8269e767SBrooks Davisin
194*8269e767SBrooks Davis.Fx 10.0
195*8269e767SBrooks Davisto prevent confusion about its actual functionality.
196*8269e767SBrooks DavisThe original
197*8269e767SBrooks Davis.Xr sysctl 3
198*8269e767SBrooks Davis.Va kern.ipc.somaxconn
199*8269e767SBrooks Davisis still available but hidden from a
200*8269e767SBrooks Davis.Xr sysctl 3
201*8269e767SBrooks Davis-a output so that existing applications and scripts continue to work.
202