[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 15/64] sockets: update SOCKET_ADDRESS_TYPE_FD listen(2) backlog
From: |
Michael Roth |
Subject: |
[PATCH 15/64] sockets: update SOCKET_ADDRESS_TYPE_FD listen(2) backlog |
Date: |
Tue, 19 Oct 2021 09:08:55 -0500 |
From: Stefan Hajnoczi <stefanha@redhat.com>
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.
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 <rjones@redhat.com>
Cc: Juan Quintela <quintela@redhat.com>
Cc: Eric Blake <eblake@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-Id: <20210310173004.420190-1-stefanha@redhat.com>
Tested-by: Richard W.M. Jones <rjones@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
(cherry picked from commit 37179e9ea45d6428b29ae789209c119ac18c1d39)
Signed-off-by: Michael Roth <michael.roth@amd.com>
---
util/qemu-sockets.c | 29 ++++++++++++++++++++++-------
1 file changed, 22 insertions(+), 7 deletions(-)
diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
index 8af0278f15..2463c49773 100644
--- a/util/qemu-sockets.c
+++ b/util/qemu-sockets.c
@@ -1116,14 +1116,10 @@ fail:
return NULL;
}
-static int socket_get_fd(const char *fdstr, int num, Error **errp)
+static int socket_get_fd(const char *fdstr, Error **errp)
{
Monitor *cur_mon = monitor_cur();
int fd;
- if (num != 1) {
- error_setg_errno(errp, EINVAL, "socket_get_fd: too many connections");
- return -1;
- }
if (cur_mon) {
fd = monitor_get_fd(cur_mon, fdstr, errp);
if (fd < 0) {
@@ -1159,7 +1155,7 @@ int socket_connect(SocketAddress *addr, Error **errp)
break;
case SOCKET_ADDRESS_TYPE_FD:
- fd = socket_get_fd(addr->u.fd.str, 1, errp);
+ fd = socket_get_fd(addr->u.fd.str, errp);
break;
case SOCKET_ADDRESS_TYPE_VSOCK:
@@ -1187,7 +1183,26 @@ int socket_listen(SocketAddress *addr, int num, Error
**errp)
break;
case SOCKET_ADDRESS_TYPE_FD:
- fd = socket_get_fd(addr->u.fd.str, num, errp);
+ fd = socket_get_fd(addr->u.fd.str, errp);
+ if (fd < 0) {
+ return -1;
+ }
+
+ /*
+ * If the socket is not yet in the listen state, then transition it to
+ * the listen state now.
+ *
+ * If it's already listening then this updates the backlog value as
+ * requested.
+ *
+ * If this socket cannot listen because it's already in another state
+ * (e.g. unbound or connected) then we'll catch the error here.
+ */
+ if (listen(fd, num) != 0) {
+ error_setg_errno(errp, errno, "Failed to listen on fd socket");
+ closesocket(fd);
+ return -1;
+ }
break;
case SOCKET_ADDRESS_TYPE_VSOCK:
--
2.25.1
- [PATCH 00/64] Patch Round-up for stable 6.0.1, freeze on 2021-10-26, Michael Roth, 2021/10/19
- [PATCH 09/64] target/xtensa: fix access ring in l32ex, Michael Roth, 2021/10/19
- [PATCH 10/64] qemu-option: support accept-any QemuOptsList in qemu_opts_absorb_qdict, Michael Roth, 2021/10/19
- [PATCH 11/64] qemu-config: load modules when instantiating option groups, Michael Roth, 2021/10/19
- [PATCH 12/64] qemu-config: parse configuration files to a QDict, Michael Roth, 2021/10/19
- [PATCH 13/64] vl: plumb keyval-based options into -readconfig, Michael Roth, 2021/10/19
- [PATCH 14/64] vl: plug -object back into -readconfig, Michael Roth, 2021/10/19
- [PATCH 15/64] sockets: update SOCKET_ADDRESS_TYPE_FD listen(2) backlog,
Michael Roth <=
- [PATCH 16/64] hmp: Fix loadvm to resume the VM on success instead of failure, Michael Roth, 2021/10/19
- [PATCH 17/64] configure: fix detection of gdbus-codegen, Michael Roth, 2021/10/19
- [PATCH 18/64] vhost-vdpa: don't initialize backend_features, Michael Roth, 2021/10/19
- [PATCH 19/64] esp: only assert INTR_DC interrupt flag if selection fails, Michael Roth, 2021/10/19
- [PATCH 20/64] esp: only set ESP_RSEQ at the start of the select sequence, Michael Roth, 2021/10/19
- [PATCH 01/64] multi-process: Initialize variables declared with g_auto*, Michael Roth, 2021/10/19
- [PATCH 21/64] runstate: Initialize Error * to NULL, Michael Roth, 2021/10/19
- [PATCH 22/64] vfio: Fix unregister SaveVMHandler in vfio_migration_finalize, Michael Roth, 2021/10/19
- [PATCH 23/64] vl: Fix an assert failure in error path, Michael Roth, 2021/10/19
- [PATCH 24/64] tcg/sparc: Fix temp_allocate_frame vs sparc stack bias, Michael Roth, 2021/10/19