Skip to contentSkip to conversation

virtiofsd: Prevent multiply running with same vhost_user_socket

PATCH In review Masayoshi Mizuma 3 participants 4 messages Aug 13, 2019 – Aug 21, 2019 Arm/actions semi architecture

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 virtiofsd: Prevent multiply running with same vhost_user_socket

From: Masayoshi Mizuma <[email protected]>

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.

---
 contrib/virtiofsd/fuse_i.h        |  1 +
 contrib/virtiofsd/fuse_lowlevel.c |  6 ++++
 contrib/virtiofsd/fuse_virtio.c   | 51 +++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+)
contrib/virtiofsd/fuse_i.h +1 -0
index ff6e1b75ba..89cc20a787 100644
@@ -72,6 +72,7 @@ struct fuse_session {
         char *vu_socket_path;
         int   vu_listen_fd;
         int   vu_socketfd;
+        char *vu_socket_lock;
         struct fv_VuDev *virtio_dev;
 };
 
contrib/virtiofsd/fuse_lowlevel.c +6 -0
index 8adc4b1ab8..9d41b48d73 100644
@@ -16,6 +16,7 @@
 #include "fuse_misc.h"
 #include "fuse_virtio.h"
 
+#include <glib.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
@@ -2587,6 +2588,11 @@ void fuse_session_destroy(struct fuse_session *se)
 	free(se->vu_socket_path);
 	se->vu_socket_path = NULL;
 
+	if (se->vu_socket_lock) {
+		g_free(se->vu_socket_lock);
+		se->vu_socket_lock = NULL;
+	}
+
 	free(se);
 }
 
contrib/virtiofsd/fuse_virtio.c +51 -0

69 lines not on this page · raw message

Signature · 1 line
2.18.1
Reviewed-by on v3 1/1
#1 lore raw

On Tue, Aug 13, 2019 at 04:06:45PM -0400, Masayoshi Mizuma wrote:

Two minor comments below. They can be squashed in when merging this patch.

If users find it problematic that pid files are never deleted we could make this feature optional with --pidfile=PATH. But I think we can merge this patch for now and see if anyone encounters issues before the next virtio-fs release.

contrib/virtiofsd/fuse_virtio.c int virtio_loop(struct fuse_session *se) · line 885

Quoted 11 lines
contrib/virtiofsd/fuse_virtio.c +11 -0
+static int fv_socket_lock(struct fuse_session *se)
+{
+        char *dir, *sk_name;
+        Error *local_err = NULL;
+        int ret = -1;
+
+        dir = qemu_get_local_state_pathname("run/virtiofsd");
+
+        if (g_mkdir_with_parents(dir, S_IRWXU) < -1) {
+                fuse_err("%s: Failed to create directory %s: %s",
+                        __func__, dir, strerror(errno));

Missing g_free(dir).

contrib/virtiofsd/fuse_virtio.c static int fv_create_listen_socket(struct fuse_session *se) · line 920

Quoted 6 lines
contrib/virtiofsd/fuse_virtio.c +2 -0
@@ -876,6 +916,17 @@ static int fv_create_listen_socket(struct fuse_session *se)
                 return -1;
         }
 
+        if (!strlen(se->vu_socket_path)) {
+                fuse_err("Socket path is NULL\n");

s/NULL/empty/

#2 lore raw
Quoted 26 lines · * Stefan Hajnoczi ([email protected]) wrote:

On Tue, Aug 13, 2019 at 04:06:45PM -0400, Masayoshi Mizuma wrote:

Two minor comments below. They can be squashed in when merging this patch.

If users find it problematic that pid files are never deleted we could make this feature optional with --pidfile=PATH. But I think we can merge this patch for now and see if anyone encounters issues before the next virtio-fs release.

contrib/virtiofsd/fuse_virtio.c int virtio_loop(struct fuse_session *se) · line 885

Quoted 11 lines
contrib/virtiofsd/fuse_virtio.c +11 -0
+static int fv_socket_lock(struct fuse_session *se)
+{
+        char *dir, *sk_name;
+        Error *local_err = NULL;
+        int ret = -1;
+
+        dir = qemu_get_local_state_pathname("run/virtiofsd");
+
+        if (g_mkdir_with_parents(dir, S_IRWXU) < -1) {
+                fuse_err("%s: Failed to create directory %s: %s",
+                        __func__, dir, strerror(errno));

Missing g_free(dir).

Quoted 8 lines

contrib/virtiofsd/fuse_virtio.c static int fv_create_listen_socket(struct fuse_session *se) · line 920

Quoted 6 lines
contrib/virtiofsd/fuse_virtio.c +2 -0
@@ -876,6 +916,17 @@ static int fv_create_listen_socket(struct fuse_session *se)
                 return -1;
         }
 
+        if (!strlen(se->vu_socket_path)) {
+                fuse_err("Socket path is NULL\n");

s/NULL/empty/

Thanks; applied with those changes, plus also I found I had to move the #include of osdep.h and error.h to the top of the include list to avoid glib compilation errors.

Dave

Quoted 4 lines

_______________________________________________
Virtio-fs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virtio-fs

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#3 lore raw
Quoted 37 lines · Dr. David Alan Gilbert
Quoted 26 lines · * Stefan Hajnoczi ([email protected]) wrote:

On Tue, Aug 13, 2019 at 04:06:45PM -0400, Masayoshi Mizuma wrote:

Two minor comments below. They can be squashed in when merging this patch.

If users find it problematic that pid files are never deleted we could make this feature optional with --pidfile=PATH. But I think we can merge this patch for now and see if anyone encounters issues before the next virtio-fs release.

contrib/virtiofsd/fuse_virtio.c int virtio_loop(struct fuse_session *se) · line 885

Quoted 11 lines
contrib/virtiofsd/fuse_virtio.c +11 -0
+static int fv_socket_lock(struct fuse_session *se)
+{
+        char *dir, *sk_name;
+        Error *local_err = NULL;
+        int ret = -1;
+
+        dir = qemu_get_local_state_pathname("run/virtiofsd");
+
+        if (g_mkdir_with_parents(dir, S_IRWXU) < -1) {
+                fuse_err("%s: Failed to create directory %s: %s",
+                        __func__, dir, strerror(errno));

Missing g_free(dir).

Quoted 8 lines

contrib/virtiofsd/fuse_virtio.c static int fv_create_listen_socket(struct fuse_session *se) · line 920

Quoted 6 lines
contrib/virtiofsd/fuse_virtio.c +2 -0
@@ -876,6 +916,17 @@ static int fv_create_listen_socket(struct fuse_session *se)
                 return -1;
         }
 
+        if (!strlen(se->vu_socket_path)) {
+                fuse_err("Socket path is NULL\n");

s/NULL/empty/

Quoted 3 lines

Thanks; applied with those changes, plus also I found I had to move the #include of osdep.h and error.h to the top of the include list to avoid glib compilation errors.

Thank you for fixing it and merging the patch!

- Masa