git: 76e6e4d72f8d - main - listen(): improve POSIX compliance

From: Michael Tuexen <tuexen_at_FreeBSD.org>
Date: Tue, 11 Oct 2022 21:55:50 UTC
The branch main has been updated by tuexen:

URL: https://cgit.FreeBSD.org/src/commit/?id=76e6e4d72f8d3da7d19242f303bc95461fde7fb9

commit 76e6e4d72f8d3da7d19242f303bc95461fde7fb9
Author:     Michael Tuexen <tuexen@FreeBSD.org>
AuthorDate: 2022-10-11 20:46:51 +0000
Commit:     Michael Tuexen <tuexen@FreeBSD.org>
CommitDate: 2022-10-11 20:46:51 +0000

    listen(): improve POSIX compliance
    
    Ensure that a negative backlog argument is handled as it if was 0.
    
    Reviewed by:            markj@, glebius@
    Sponsored by:           Netflix, Inc.
    Differential Revision:  https://reviews.freebsd.org/D31821
---
 lib/libc/sys/listen.2  | 12 ++++++++++--
 sys/kern/uipc_socket.c |  4 +++-
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/lib/libc/sys/listen.2 b/lib/libc/sys/listen.2
index 4d0962fd412c..ad4d6edf228f 100644
--- a/lib/libc/sys/listen.2
+++ b/lib/libc/sys/listen.2
@@ -28,7 +28,7 @@
 .\"	From: @(#)listen.2	8.2 (Berkeley) 12/11/93
 .\" $FreeBSD$
 .\"
-.Dd April 14, 2020
+.Dd October 11, 2022
 .Dt LISTEN 2
 .Os
 .Sh NAME
@@ -106,10 +106,13 @@ specifies a hard limit on
 .Fa backlog ;
 if a value greater than
 .Va kern.ipc.soacceptqueue
-or less than zero is specified,
+is specified,
 .Fa backlog
 is silently forced to
 .Va kern.ipc.soacceptqueue .
+If a negative value is used,
+.Fa backlog
+is silently forced to 0.
 .Pp
 If the listen queue overflows, the kernel will emit a LOG_DEBUG syslog message.
 The
@@ -191,3 +194,8 @@ The original
 is still available but hidden from a
 .Xr sysctl 3
 -a output so that existing applications and scripts continue to work.
+To improve POSIX compliance, a negative
+.Fa backlog
+argument is handled the same as 0.
+This was introduced in
+.Fx 14.0 .
diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c
index 7e1d2c910dbd..ff2ecadfb22a 100644
--- a/sys/kern/uipc_socket.c
+++ b/sys/kern/uipc_socket.c
@@ -1075,7 +1075,9 @@ solisten_proto(struct socket *so, int backlog)
 	so->so_options |= SO_ACCEPTCONN;
 
 listening:
-	if (backlog < 0 || backlog > somaxconn)
+	if (backlog < 0)
+		backlog = 0;
+	if (backlog > somaxconn)
 		backlog = somaxconn;
 	so->sol_qlimit = backlog;