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: src/lib/libc/sys/listen.2,v 1.12.2.9 2002/05/09 02:24:40 silby Exp $ 30.\" $DragonFly: src/lib/libc/sys/listen.2,v 1.5 2007/05/12 21:22:10 swildner Exp $ 31.\" 32.Dd November 3, 1995 33.Dt LISTEN 2 34.Os 35.Sh NAME 36.Nm listen 37.Nd listen for connections 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 listen "int s" "int backlog" 45.Sh DESCRIPTION 46To accept connections, a socket 47is first created with 48.Xr socket 2 , 49a willingness to accept incoming connections and 50a queue limit for incoming connections are specified with 51.Fn listen , 52and then the connections are 53accepted with 54.Xr accept 2 . 55The 56.Fn listen 57call applies only to sockets of type 58.Dv SOCK_STREAM 59or 60.Dv SOCK_SEQPACKET . 61.Pp 62The 63.Fa backlog 64parameter defines the maximum length the queue of 65pending connections may grow to. 66If a connection 67request arrives with the queue full the client may 68receive an error with an indication of 69.Er ECONNREFUSED , 70or, in the case of TCP, the connection will be 71silently dropped. 72.Pp 73Note that before 74.Fx 4.5 75and the introduction of the syncache, the 76.Fa backlog 77parameter also determined the length of the incomplete 78connection queue, which held TCP sockets in the process 79of completing TCP's 3-way handshake. These incomplete connections 80are now held entirely in the syncache, which is unaffected by 81queue lengths. Inflated 82.Fa backlog 83values to help handle denial 84of service attacks are no longer necessary. 85.Pp 86The 87.Xr sysctl 3 88MIB variable 89.Dq Va kern.ipc.somaxconn 90specifies a hard limit on 91.Fa backlog ; 92if a value greater than 93.Va kern.ipc.somaxconn 94or less than zero is specified, 95.Fa backlog 96is silently forced to 97.Va kern.ipc.somaxconn . 98.Sh INTERACTION WITH ACCEPT FILTERS 99When accept filtering is used on a socket, a second queue will 100be used to hold sockets that have connected, but have not yet 101met their accept filtering criteria. Once the criteria has been 102met, these sockets will be moved over into the completed connection 103queue to be accept()ed. If this secondary queue is full and a 104new connection comes in, the oldest socket which has not yet met 105its accept filter criteria will be terminated. 106.Pp 107This secondary queue, like the primary listen queue, is sized 108according to the 109.Fa backlog 110parameter. 111.Sh RETURN VALUES 112.Rv -std listen 113.Sh ERRORS 114.Fn Listen 115will fail if: 116.Bl -tag -width Er 117.It Bq Er EBADF 118The argument 119.Fa s 120is not a valid descriptor. 121.It Bq Er ENOTSOCK 122The argument 123.Fa s 124is not a socket. 125.It Bq Er EOPNOTSUPP 126The socket is not of a type that supports the operation 127.Fn listen . 128.El 129.Sh SEE ALSO 130.Xr accept 2 , 131.Xr connect 2 , 132.Xr socket 2 , 133.Xr sysctl 3 , 134.Xr sysctl 8 , 135.Xr accept_filter 9 136.Sh HISTORY 137The 138.Fn listen 139function call appeared in 140.Bx 4.2 . 141The ability to configure the maximum 142.Fa backlog 143at run-time, and to use a negative 144.Fa backlog 145to request the maximum allowable value, was introduced in 146.Fx 2.2 . 147