render / rpms / qemu

Forked from rpms/qemu 10 months ago
Clone

Blame 0090-virtiofsd-Prevent-multiply-running-with-same-vhost_u.patch

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