Skip to content

Commit 16a53c8

Browse files
IdanHoawesomekling
authored andcommitted
Kernel: Treat a backlog argument of 0 to listen() as if it was 1
As per POSIX, the behavior of listen() with a backlog value of 0 is implementation defined: "A backlog argument of 0 may allow the socket to accept connections, in which case the length of the listen queue may be set to an implementation-defined minimum value." Since creating a socket that can't accept any connections seems relatively useless, and as other platforms (Linux, FreeBSD, etc) chose to support accepting connections with this backlog value, support it as well by normalizing it to 1.
1 parent afe2488 commit 16a53c8

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Kernel/Syscalls/socket.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,14 @@ ErrorOr<FlatPtr> Process::sys$bind(int sockfd, Userspace<sockaddr const*> addres
6565
ErrorOr<FlatPtr> Process::sys$listen(int sockfd, int backlog)
6666
{
6767
VERIFY_NO_PROCESS_BIG_LOCK(this);
68-
if (backlog < 0)
69-
backlog = 0;
68+
// As per POSIX, the behavior of listen() with a backlog value of 0 is implementation defined:
69+
// "A backlog argument of 0 may allow the socket to accept connections, in which case the length of the listen queue may be set to an implementation-defined minimum value."
70+
// Since creating a socket that can't accept any connections seems relatively useless, and as other platforms (Linux, FreeBSD, etc) chose to support accepting connections
71+
// with this backlog value, support it as well by normalizing it to 1.
72+
// Also, as per POSIX, the behaviour of a negative backlog value is equivalent to a backlog value of 0:
73+
// "If listen() is called with a backlog argument value that is less than 0, the function behaves as if it had been called with a backlog argument value of 0."
74+
if (backlog <= 0)
75+
backlog = 1;
7076
auto description = TRY(open_file_description(sockfd));
7177
if (!description->is_socket())
7278
return ENOTSOCK;

0 commit comments

Comments
 (0)