yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-virtiofsd-Prevent-multiply-running-with-same-vhost_u.patch

ddf19c
From ab336e3aea97d76c1b2ac725d19b4518f47dd8f0 Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:01:59 +0100
ddf19c
Subject: [PATCH 088/116] virtiofsd: Prevent multiply running with same
ddf19c
 vhost_user_socket
ddf19c
MIME-Version: 1.0
ddf19c
Content-Type: text/plain; charset=UTF-8
ddf19c
Content-Transfer-Encoding: 8bit
ddf19c
ddf19c
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
Message-id: <20200127190227.40942-85-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93541
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 084/112] virtiofsd: Prevent multiply running with same vhost_user_socket
ddf19c
Bugzilla: 1694164
ddf19c
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
ddf19c
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
ddf19c
ddf19c
From: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
ddf19c
ddf19c
virtiofsd can run multiply even if the vhost_user_socket is same path.
ddf19c
ddf19c
  ]# ./virtiofsd -o vhost_user_socket=/tmp/vhostqemu -o source=/tmp/share &
ddf19c
  [1] 244965
ddf19c
  virtio_session_mount: Waiting for vhost-user socket connection...
ddf19c
  ]# ./virtiofsd -o vhost_user_socket=/tmp/vhostqemu -o source=/tmp/share &
ddf19c
  [2] 244966
ddf19c
  virtio_session_mount: Waiting for vhost-user socket connection...
ddf19c
  ]#
ddf19c
ddf19c
The user will get confused about the situation and maybe the cause of the
ddf19c
unexpected problem. So it's better to prevent the multiple running.
ddf19c
ddf19c
Create a regular file under localstatedir directory to exclude the
ddf19c
vhost_user_socket. To create and lock the file, use qemu_write_pidfile()
ddf19c
because the API has some sanity checks and file lock.
ddf19c
ddf19c
Signed-off-by: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
  Applied fixes from Stefan's review and moved osdep include
ddf19c
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit 96814800d2b49d18737c36e021c387697ec40c62)
ddf19c
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/fuse_lowlevel.c |  1 +
ddf19c
 tools/virtiofsd/fuse_virtio.c   | 49 ++++++++++++++++++++++++++++++++++++++++-
ddf19c
 2 files changed, 49 insertions(+), 1 deletion(-)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/fuse_lowlevel.c b/tools/virtiofsd/fuse_lowlevel.c
ddf19c
index 440508a..aac282f 100644
ddf19c
--- a/tools/virtiofsd/fuse_lowlevel.c
ddf19c
+++ b/tools/virtiofsd/fuse_lowlevel.c
ddf19c
@@ -18,6 +18,7 @@
ddf19c
 
ddf19c
 #include <assert.h>
ddf19c
 #include <errno.h>
ddf19c
+#include <glib.h>
ddf19c
 #include <limits.h>
ddf19c
 #include <stdbool.h>
ddf19c
 #include <stddef.h>
ddf19c
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
ddf19c
index e7bd772..b7948de 100644
ddf19c
--- a/tools/virtiofsd/fuse_virtio.c
ddf19c
+++ b/tools/virtiofsd/fuse_virtio.c
ddf19c
@@ -13,11 +13,12 @@
ddf19c
 
ddf19c
 #include "qemu/osdep.h"
ddf19c
 #include "qemu/iov.h"
ddf19c
-#include "fuse_virtio.h"
ddf19c
+#include "qapi/error.h"
ddf19c
 #include "fuse_i.h"
ddf19c
 #include "standard-headers/linux/fuse.h"
ddf19c
 #include "fuse_misc.h"
ddf19c
 #include "fuse_opt.h"
ddf19c
+#include "fuse_virtio.h"
ddf19c
 
ddf19c
 #include <assert.h>
ddf19c
 #include <errno.h>
ddf19c
@@ -743,6 +744,42 @@ int virtio_loop(struct fuse_session *se)
ddf19c
     return 0;
ddf19c
 }
ddf19c
 
ddf19c
+static void strreplace(char *s, char old, char new)
ddf19c
+{
ddf19c
+    for (; *s; ++s) {
ddf19c
+        if (*s == old) {
ddf19c
+            *s = new;
ddf19c
+        }
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+static bool fv_socket_lock(struct fuse_session *se)
ddf19c
+{
ddf19c
+    g_autofree gchar *sk_name = NULL;
ddf19c
+    g_autofree gchar *pidfile = NULL;
ddf19c
+    g_autofree gchar *dir = NULL;
ddf19c
+    Error *local_err = NULL;
ddf19c
+
ddf19c
+    dir = qemu_get_local_state_pathname("run/virtiofsd");
ddf19c
+
ddf19c
+    if (g_mkdir_with_parents(dir, S_IRWXU) < 0) {
ddf19c
+        fuse_log(FUSE_LOG_ERR, "%s: Failed to create directory %s: %s",
ddf19c
+                 __func__, dir, strerror(errno));
ddf19c
+        return false;
ddf19c
+    }
ddf19c
+
ddf19c
+    sk_name = g_strdup(se->vu_socket_path);
ddf19c
+    strreplace(sk_name, '/', '.');
ddf19c
+    pidfile = g_strdup_printf("%s/%s.pid", dir, sk_name);
ddf19c
+
ddf19c
+    if (!qemu_write_pidfile(pidfile, &local_err)) {
ddf19c
+        error_report_err(local_err);
ddf19c
+        return false;
ddf19c
+    }
ddf19c
+
ddf19c
+    return true;
ddf19c
+}
ddf19c
+
ddf19c
 static int fv_create_listen_socket(struct fuse_session *se)
ddf19c
 {
ddf19c
     struct sockaddr_un un;
ddf19c
@@ -758,6 +795,16 @@ static int fv_create_listen_socket(struct fuse_session *se)
ddf19c
         return -1;
ddf19c
     }
ddf19c
 
ddf19c
+    if (!strlen(se->vu_socket_path)) {
ddf19c
+        fuse_log(FUSE_LOG_ERR, "Socket path is empty\n");
ddf19c
+        return -1;
ddf19c
+    }
ddf19c
+
ddf19c
+    /* Check the vu_socket_path is already used */
ddf19c
+    if (!fv_socket_lock(se)) {
ddf19c
+        return -1;
ddf19c
+    }
ddf19c
+
ddf19c
     /*
ddf19c
      * Create the Unix socket to communicate with qemu
ddf19c
      * based on QEMU's vhost-user-bridge
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c