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

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