On Wed, Mar 10, 2021 at 05:30:04PM +0000, Stefan Hajnoczi wrote:
socket_get_fd() fails with the error "socket_get_fd: too many
connections" if the given listen backlog value is not 1.

Not all callers set the backlog to 1. For example, commit
582d4210eb2f2ab5baac328fe4b479cd86da1647 ("qemu-nbd: Use SOMAXCONN for
socket listen() backlog") uses SOMAXCONN. This will always fail with in
socket_get_fd().

This patch calls listen(2) on the fd to update the backlog value. The
socket may already be in the listen state. I have tested that this works
on Linux 5.10 and macOS Catalina.

Confirmed also from the listen(2) man page of FreeBSD:
"A subsequent listen() system call on the listening socket allows the caller to change the maximum queue length using a new backlog argument."


As a bonus this allows us to detect when the fd cannot listen. Now we'll
be able to catch unbound or connected fds in socket_listen().

Drop the num argument from socket_get_fd() since this function is also
called by socket_connect() where a listen backlog value does not make
sense.

Fixes: e5b6353cf25c99c3f08bf51e29933352f7140e8f ("socket: Add backlog parameter to 
socket_listen")
Reported-by: Richard W.M. Jones <[email protected]>
Cc: Juan Quintela <[email protected]>
Cc: Eric Blake <[email protected]>
Signed-off-by: Stefan Hajnoczi <[email protected]>
---
util/qemu-sockets.c | 29 ++++++++++++++++++++++-------
1 file changed, 22 insertions(+), 7 deletions(-)

Reviewed-by: Stefano Garzarella <[email protected]>

Thanks,
Stefano


Reply via email to