cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-sockets-update-SOCKET_ADDRESS_TYPE_FD-listen-2-backl.patch

a83cc2
From 415a6a1ced90cc8b8691eb7ab027bba4611fc236 Mon Sep 17 00:00:00 2001
a83cc2
From: Stefan Hajnoczi <stefanha@redhat.com>
a83cc2
Date: Wed, 2 Jun 2021 15:51:02 -0400
a83cc2
Subject: [PATCH 04/21] sockets: update SOCKET_ADDRESS_TYPE_FD listen(2)
a83cc2
 backlog
a83cc2
MIME-Version: 1.0
a83cc2
Content-Type: text/plain; charset=UTF-8
a83cc2
Content-Transfer-Encoding: 8bit
a83cc2
a83cc2
RH-Author: Miroslav Rezanina <mrezanin@redhat.com>
a83cc2
RH-MergeRequest: 8: Synchronize with RHEL-AV 8.5 release 19 to RHEL 9
a83cc2
RH-Commit: [3/8] a8fd97eb477ad51fca75c1cc344185e1de59caf1 (mrezanin/centos-src-qemu-kvm)
a83cc2
RH-Bugzilla: 1957194
a83cc2
RH-Acked-by: Daniel P. Berrangé <berrange@redhat.com>
a83cc2
RH-Acked-by: Greg Kurz <gkurz@redhat.com>
a83cc2
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
a83cc2
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
a83cc2
a83cc2
socket_get_fd() fails with the error "socket_get_fd: too many
a83cc2
connections" if the given listen backlog value is not 1.
a83cc2
a83cc2
Not all callers set the backlog to 1. For example, commit
a83cc2
582d4210eb2f2ab5baac328fe4b479cd86da1647 ("qemu-nbd: Use SOMAXCONN for
a83cc2
socket listen() backlog") uses SOMAXCONN. This will always fail with in
a83cc2
socket_get_fd().
a83cc2
a83cc2
This patch calls listen(2) on the fd to update the backlog value. The
a83cc2
socket may already be in the listen state. I have tested that this works
a83cc2
on Linux 5.10 and macOS Catalina.
a83cc2
a83cc2
As a bonus this allows us to detect when the fd cannot listen. Now we'll
a83cc2
be able to catch unbound or connected fds in socket_listen().
a83cc2
a83cc2
Drop the num argument from socket_get_fd() since this function is also
a83cc2
called by socket_connect() where a listen backlog value does not make
a83cc2
sense.
a83cc2
a83cc2
Fixes: e5b6353cf25c99c3f08bf51e29933352f7140e8f ("socket: Add backlog parameter to socket_listen")
a83cc2
Reported-by: Richard W.M. Jones <rjones@redhat.com>
a83cc2
Cc: Juan Quintela <quintela@redhat.com>
a83cc2
Cc: Eric Blake <eblake@redhat.com>
a83cc2
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
a83cc2
Message-Id: <20210310173004.420190-1-stefanha@redhat.com>
a83cc2
Tested-by: Richard W.M. Jones <rjones@redhat.com>
a83cc2
Reviewed-by: Eric Blake <eblake@redhat.com>
a83cc2
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
a83cc2
Signed-off-by: Eric Blake <eblake@redhat.com>
a83cc2
(cherry picked from commit 37179e9ea45d6428b29ae789209c119ac18c1d39)
a83cc2
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
a83cc2
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
a83cc2
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
a83cc2
---
a83cc2
 util/qemu-sockets.c | 29 ++++++++++++++++++++++-------
a83cc2
 1 file changed, 22 insertions(+), 7 deletions(-)
a83cc2
a83cc2
diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
a83cc2
index 8af0278f15..2463c49773 100644
a83cc2
--- a/util/qemu-sockets.c
a83cc2
+++ b/util/qemu-sockets.c
a83cc2
@@ -1116,14 +1116,10 @@ fail:
a83cc2
     return NULL;
a83cc2
 }
a83cc2
 
a83cc2
-static int socket_get_fd(const char *fdstr, int num, Error **errp)
a83cc2
+static int socket_get_fd(const char *fdstr, Error **errp)
a83cc2
 {
a83cc2
     Monitor *cur_mon = monitor_cur();
a83cc2
     int fd;
a83cc2
-    if (num != 1) {
a83cc2
-        error_setg_errno(errp, EINVAL, "socket_get_fd: too many connections");
a83cc2
-        return -1;
a83cc2
-    }
a83cc2
     if (cur_mon) {
a83cc2
         fd = monitor_get_fd(cur_mon, fdstr, errp);
a83cc2
         if (fd < 0) {
a83cc2
@@ -1159,7 +1155,7 @@ int socket_connect(SocketAddress *addr, Error **errp)
a83cc2
         break;
a83cc2
 
a83cc2
     case SOCKET_ADDRESS_TYPE_FD:
a83cc2
-        fd = socket_get_fd(addr->u.fd.str, 1, errp);
a83cc2
+        fd = socket_get_fd(addr->u.fd.str, errp);
a83cc2
         break;
a83cc2
 
a83cc2
     case SOCKET_ADDRESS_TYPE_VSOCK:
a83cc2
@@ -1187,7 +1183,26 @@ int socket_listen(SocketAddress *addr, int num, Error **errp)
a83cc2
         break;
a83cc2
 
a83cc2
     case SOCKET_ADDRESS_TYPE_FD:
a83cc2
-        fd = socket_get_fd(addr->u.fd.str, num, errp);
a83cc2
+        fd = socket_get_fd(addr->u.fd.str, errp);
a83cc2
+        if (fd < 0) {
a83cc2
+            return -1;
a83cc2
+        }
a83cc2
+
a83cc2
+        /*
a83cc2
+         * If the socket is not yet in the listen state, then transition it to
a83cc2
+         * the listen state now.
a83cc2
+         *
a83cc2
+         * If it's already listening then this updates the backlog value as
a83cc2
+         * requested.
a83cc2
+         *
a83cc2
+         * If this socket cannot listen because it's already in another state
a83cc2
+         * (e.g. unbound or connected) then we'll catch the error here.
a83cc2
+         */
a83cc2
+        if (listen(fd, num) != 0) {
a83cc2
+            error_setg_errno(errp, errno, "Failed to listen on fd socket");
a83cc2
+            closesocket(fd);
a83cc2
+            return -1;
a83cc2
+        }
a83cc2
         break;
a83cc2
 
a83cc2
     case SOCKET_ADDRESS_TYPE_VSOCK:
a83cc2
-- 
a83cc2
2.27.0
a83cc2