[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 082/109] virtiofsd: Prevent multiply running with same vhost_u
From: |
Dr. David Alan Gilbert (git) |
Subject: |
[PATCH v2 082/109] virtiofsd: Prevent multiply running with same vhost_user_socket |
Date: |
Tue, 21 Jan 2020 12:24:06 +0000 |
From: Masayoshi Mizuma <address@hidden>
virtiofsd can run multiply even if the vhost_user_socket is same path.
]# ./virtiofsd -o vhost_user_socket=/tmp/vhostqemu -o source=/tmp/share &
[1] 244965
virtio_session_mount: Waiting for vhost-user socket connection...
]# ./virtiofsd -o vhost_user_socket=/tmp/vhostqemu -o source=/tmp/share &
[2] 244966
virtio_session_mount: Waiting for vhost-user socket connection...
]#
The user will get confused about the situation and maybe the cause of the
unexpected problem. So it's better to prevent the multiple running.
Create a regular file under localstatedir directory to exclude the
vhost_user_socket. To create and lock the file, use qemu_write_pidfile()
because the API has some sanity checks and file lock.
Signed-off-by: Masayoshi Mizuma <address@hidden>
Signed-off-by: Dr. David Alan Gilbert <address@hidden>
Applied fixes from Stefan's review and moved osdep include
Signed-off-by: Stefan Hajnoczi <address@hidden>
Reviewed-by: Daniel P. Berrangé <address@hidden>
---
tools/virtiofsd/fuse_lowlevel.c | 1 +
tools/virtiofsd/fuse_virtio.c | 49 ++++++++++++++++++++++++++++++++-
2 files changed, 49 insertions(+), 1 deletion(-)
diff --git a/tools/virtiofsd/fuse_lowlevel.c b/tools/virtiofsd/fuse_lowlevel.c
index 2df120f35c..22f2cc9409 100644
--- a/tools/virtiofsd/fuse_lowlevel.c
+++ b/tools/virtiofsd/fuse_lowlevel.c
@@ -17,6 +17,7 @@
#include <assert.h>
#include <errno.h>
+#include <glib.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
index e7bd772805..b7948def27 100644
--- a/tools/virtiofsd/fuse_virtio.c
+++ b/tools/virtiofsd/fuse_virtio.c
@@ -13,11 +13,12 @@
#include "qemu/osdep.h"
#include "qemu/iov.h"
-#include "fuse_virtio.h"
+#include "qapi/error.h"
#include "fuse_i.h"
#include "standard-headers/linux/fuse.h"
#include "fuse_misc.h"
#include "fuse_opt.h"
+#include "fuse_virtio.h"
#include <assert.h>
#include <errno.h>
@@ -743,6 +744,42 @@ int virtio_loop(struct fuse_session *se)
return 0;
}
+static void strreplace(char *s, char old, char new)
+{
+ for (; *s; ++s) {
+ if (*s == old) {
+ *s = new;
+ }
+ }
+}
+
+static bool fv_socket_lock(struct fuse_session *se)
+{
+ g_autofree gchar *sk_name = NULL;
+ g_autofree gchar *pidfile = NULL;
+ g_autofree gchar *dir = NULL;
+ Error *local_err = NULL;
+
+ dir = qemu_get_local_state_pathname("run/virtiofsd");
+
+ if (g_mkdir_with_parents(dir, S_IRWXU) < 0) {
+ fuse_log(FUSE_LOG_ERR, "%s: Failed to create directory %s: %s",
+ __func__, dir, strerror(errno));
+ return false;
+ }
+
+ sk_name = g_strdup(se->vu_socket_path);
+ strreplace(sk_name, '/', '.');
+ pidfile = g_strdup_printf("%s/%s.pid", dir, sk_name);
+
+ if (!qemu_write_pidfile(pidfile, &local_err)) {
+ error_report_err(local_err);
+ return false;
+ }
+
+ return true;
+}
+
static int fv_create_listen_socket(struct fuse_session *se)
{
struct sockaddr_un un;
@@ -758,6 +795,16 @@ static int fv_create_listen_socket(struct fuse_session *se)
return -1;
}
+ if (!strlen(se->vu_socket_path)) {
+ fuse_log(FUSE_LOG_ERR, "Socket path is empty\n");
+ return -1;
+ }
+
+ /* Check the vu_socket_path is already used */
+ if (!fv_socket_lock(se)) {
+ return -1;
+ }
+
/*
* Create the Unix socket to communicate with qemu
* based on QEMU's vhost-user-bridge
--
2.24.1
- [PATCH v2 070/109] virtiofsd: passthrough_ll: control readdirplus, (continued)
- [PATCH v2 070/109] virtiofsd: passthrough_ll: control readdirplus, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 073/109] virtiofsd: extract root inode init into setup_root(), Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 074/109] virtiofsd: passthrough_ll: clean up cache related options, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 075/109] virtiofsd: passthrough_ll: use hashtable, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 076/109] virtiofsd: Clean up inodes on destroy, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 077/109] virtiofsd: support nanosecond resolution for file timestamp, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 078/109] virtiofsd: fix error handling in main(), Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 079/109] virtiofsd: cleanup allocated resource in se, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 080/109] virtiofsd: fix memory leak on lo.source, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 081/109] virtiofsd: add helper for lo_data cleanup, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 082/109] virtiofsd: Prevent multiply running with same vhost_user_socket,
Dr. David Alan Gilbert (git) <=
- [PATCH v2 083/109] virtiofsd: enable PARALLEL_DIROPS during INIT, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 084/109] virtiofsd: fix incorrect error handling in lo_do_lookup, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 085/109] Virtiofsd: fix memory leak on fuse queueinfo, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 086/109] virtiofsd: Support remote posix locks, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 088/109] virtiofsd: prevent fv_queue_thread() vs virtio_loop() races, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 087/109] virtiofsd: use fuse_lowlevel_is_virtio() in fuse_session_destroy(), Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 089/109] virtiofsd: make lo_release() atomic, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 090/109] virtiofsd: prevent races with lo_dirp_put(), Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 091/109] virtiofsd: rename inode->refcount to inode->nlookup, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 092/109] libvhost-user: Fix some memtable remap cases, Dr. David Alan Gilbert (git), 2020/01/21