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

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