fanotify_mark.2: ERRORS: add missing EBADF error for invalid 'dirfd'
[man-pages.git] / man2 / listen.2
blobc62378149ecac73fd3979fa304b30b064495432c
1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" and Copyright (C) 2007, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" All rights reserved.
4 .\"
5 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\"    notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice, this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\"    must display the following acknowledgement:
16 .\"     This product includes software developed by the University of
17 .\"     California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\"    may be used to endorse or promote products derived from this software
20 .\"    without specific prior written permission.
21 .\"
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" SUCH DAMAGE.
33 .\" %%%LICENSE_END
34 .\"
35 .\"     $Id: listen.2,v 1.6 1999/05/18 14:10:32 freitag Exp $
36 .\"
37 .\" Modified Fri Jul 23 22:07:54 1993 by Rik Faith <faith@cs.unc.edu>
38 .\" Modified 950727 by aeb, following a suggestion by Urs Thuermann
39 .\" <urs@isnogud.escape.de>
40 .\" Modified Tue Oct 22 08:11:14 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
41 .\" Modified 1998 by Andi Kleen
42 .\" Modified 11 May 2001 by Sam Varshavchik <mrsam@courier-mta.com>
43 .\"
44 .\"
45 .TH LISTEN 2 2021-03-22 "Linux" "Linux Programmer's Manual"
46 .SH NAME
47 listen \- listen for connections on a socket
48 .SH SYNOPSIS
49 .nf
50 .B #include <sys/socket.h>
51 .PP
52 .BI "int listen(int " sockfd ", int " backlog );
53 .fi
54 .SH DESCRIPTION
55 .BR listen ()
56 marks the socket referred to by
57 .I sockfd
58 as a passive socket, that is, as a socket that will
59 be used to accept incoming connection requests using
60 .BR accept (2).
61 .PP
62 The
63 .I sockfd
64 argument is a file descriptor that refers to a socket of type
65 .B SOCK_STREAM
67 .BR SOCK_SEQPACKET .
68 .PP
69 The
70 .I backlog
71 argument defines the maximum length
72 to which the queue of pending connections for
73 .I sockfd
74 may grow.
75 If a connection request arrives when the queue is full, the client
76 may receive an error with an indication of
77 .B ECONNREFUSED
78 or, if the underlying protocol supports retransmission, the request may be
79 ignored so that a later reattempt at connection succeeds.
80 .SH RETURN VALUE
81 On success, zero is returned.
82 On error, \-1 is returned, and
83 .I errno
84 is set to indicate the error.
85 .SH ERRORS
86 .TP
87 .B EADDRINUSE
88 Another socket is already listening on the same port.
89 .TP
90 .B EADDRINUSE
91 (Internet domain sockets)
92 The socket referred to by
93 .I sockfd
94 had not previously been bound to an address and,
95 upon attempting to bind it to an ephemeral port,
96 it was determined that all port numbers in the ephemeral port range
97 are currently in use.
98 See the discussion of
99 .I /proc/sys/net/ipv4/ip_local_port_range
101 .BR ip (7).
103 .B EBADF
104 The argument
105 .I sockfd
106 is not a valid file descriptor.
108 .B ENOTSOCK
109 The file descriptor
110 .I sockfd
111 does not refer to a socket.
113 .B EOPNOTSUPP
114 The socket is not of a type that supports the
115 .BR listen ()
116 operation.
117 .SH CONFORMING TO
118 POSIX.1-2001, POSIX.1-2008, 4.4BSD
119 .RB ( listen ()
120 first appeared in 4.2BSD).
121 .SH NOTES
122 To accept connections, the following steps are performed:
123 .RS 4
124 .IP 1. 4
125 A socket is created with
126 .BR socket (2).
127 .IP 2.
128 The socket is bound to a local address using
129 .BR bind (2),
130 so that other sockets may be
131 .BR connect (2)ed
132 to it.
133 .IP 3.
134 A willingness to accept incoming connections and a queue limit for incoming
135 connections are specified with
136 .BR listen ().
137 .IP 4.
138 Connections are accepted with
139 .BR accept (2).
142 The behavior of the
143 .I backlog
144 argument on TCP sockets changed with Linux 2.2.
145 Now it specifies the queue length for
146 .I completely
147 established sockets waiting to be accepted,
148 instead of the number of incomplete connection requests.
149 The maximum length of the queue for incomplete sockets
150 can be set using
151 .IR /proc/sys/net/ipv4/tcp_max_syn_backlog .
152 When syncookies are enabled there is no logical maximum
153 length and this setting is ignored.
155 .BR tcp (7)
156 for more information.
158 If the
159 .I backlog
160 argument is greater than the value in
161 .IR /proc/sys/net/core/somaxconn ,
162 then it is silently capped to that value.
163 Since Linux 5.4, the default in this file is 4096;
164 in earlier kernels, the default value is 128.
165 In kernels before 2.4.25, this limit was a hard coded value,
166 .BR SOMAXCONN ,
167 with the value 128.
168 .\" The following is now rather historic information (MTK, Jun 05)
169 .\" Don't rely on this value in portable applications since BSD
170 .\" (and some BSD-derived systems) limit the backlog to 5.
171 .SH EXAMPLES
173 .BR bind (2).
174 .SH SEE ALSO
175 .BR accept (2),
176 .BR bind (2),
177 .BR connect (2),
178 .BR socket (2),
179 .BR socket (7)