qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir()
@ 2023-09-21  7:54 Akihiko Odaki
  2023-09-21  7:54 ` [PATCH v3 1/8] " Akihiko Odaki
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: Akihiko Odaki @ 2023-09-21  7:54 UTC (permalink / raw)
  Cc: qemu-devel, qemu-block, virtio-fs, Yuval Shaia, Marcel Apfelbaum,
	Konstantin Kostiuk, Michael Roth, Paolo Bonzini, Fam Zheng,
	Dr . David Alan Gilbert, Stefan Hajnoczi, Gerd Hoffmann,
	Stefan Weil, Yan Vugenfirer, Akihiko Odaki

qemu_get_runtime_dir() returns a dynamically allocated directory path
that is appropriate for storing runtime files. It corresponds to "run"
directory in Unix.

With a tree-wide search, it was found that there are several cases
where such a functionality is implemented so let's have one as a common
utlity function.

A notable feature of qemu_get_runtime_dir() is that it uses
$XDG_RUNTIME_DIR if available. While the function is often called by
executables which requires root privileges, it is still possible that
they are called from a user without privilege to write the system
runtime directory. In fact, I decided to write this patch when I ran
virtiofsd in a Linux namespace created by a normal user and realized
it tries to write the system runtime directory, not writable in this
case. $XDG_RUNTIME_DIR should provide a writable directory in such
cases.

This function does not use qemu_get_local_state_dir() or its logic
for Windows. Actually the implementation of qemu_get_local_state_dir()
for Windows seems not right as it calls g_get_system_data_dirs(),
which refers to $XDG_DATA_DIRS. In Unix terminology, it is basically
"/usr/share", not "/var", which qemu_get_local_state_dir() is intended
to provide. Instead, this function try to use the following in order:
- $XDG_RUNTIME_DIR
- LocalAppData folder
- get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR "/run")

This function does not use g_get_user_runtime_dir() either as it
falls back to g_get_user_cache_dir() when $XDG_DATA_DIRS is not
available. In the case, we rather use:
get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR "/run")

V2 -> V3:
  Rebase to the current master.
  Dropped patch "qga: Remove platform GUID definitions" since it is
  irrelevant.

V1 -> V2:
  Rebased to the current master since Patchew complains.

Akihiko Odaki (8):
  util: Introduce qemu_get_runtime_dir()
  ivshmem-server: Use qemu_get_runtime_dir()
  contrib/rdmacm-mux: Use qemu_get_runtime_dir()
  qga: Use qemu_get_runtime_dir()
  scsi: Use qemu_get_runtime_dir()
  module: Use qemu_get_runtime_dir()
  util: Remove qemu_get_local_state_dir()
  spice-app: Use qemu_get_runtime_dir()

 include/qemu/osdep.h           | 10 +++++++---
 contrib/ivshmem-server/main.c  | 20 ++++++++++++++++----
 contrib/rdmacm-mux/main.c      | 22 ++++++++++++++--------
 qga/main.c                     |  9 ++++-----
 scsi/qemu-pr-helper.c          |  6 +++---
 ui/spice-app.c                 |  4 ++--
 util/module.c                  |  3 ++-
 util/oslib-posix.c             |  9 +++++++--
 util/oslib-win32.c             | 24 ++++++++++++++++++++----
 contrib/rdmacm-mux/meson.build |  2 +-
 10 files changed, 76 insertions(+), 33 deletions(-)

-- 
2.41.0



^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v3 1/8] util: Introduce qemu_get_runtime_dir()
  2023-09-21  7:54 [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir() Akihiko Odaki
@ 2023-09-21  7:54 ` Akihiko Odaki
  2023-09-21  7:54 ` [PATCH v3 2/8] ivshmem-server: Use qemu_get_runtime_dir() Akihiko Odaki
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Akihiko Odaki @ 2023-09-21  7:54 UTC (permalink / raw)
  Cc: qemu-devel, qemu-block, virtio-fs, Yuval Shaia, Marcel Apfelbaum,
	Konstantin Kostiuk, Michael Roth, Paolo Bonzini, Fam Zheng,
	Dr . David Alan Gilbert, Stefan Hajnoczi, Gerd Hoffmann,
	Stefan Weil, Yan Vugenfirer, Akihiko Odaki

qemu_get_runtime_dir() returns a dynamically allocated directory path
that is appropriate for storing runtime files. It corresponds to "run"
directory in Unix.

With a tree-wide search, it was found that there are several cases
where such a functionality is implemented so let's have one as a common
utlity function.

A notable feature of qemu_get_runtime_dir() is that it uses
$XDG_RUNTIME_DIR if available. While the function is often called by
executables which requires root privileges, it is still possible that
they are called from a user without privilege to write the system
runtime directory. In fact, I decided to write this patch when I ran
virtiofsd in a Linux namespace created by a normal user and realized
it tries to write the system runtime directory, not writable in this
case. $XDG_RUNTIME_DIR should provide a writable directory in such
cases.

This function does not use qemu_get_local_state_dir() or its logic
for Windows. Actually the implementation of qemu_get_local_state_dir()
for Windows seems not right as it calls g_get_system_data_dirs(),
which refers to $XDG_DATA_DIRS. In Unix terminology, it is basically
"/usr/share", not "/var", which qemu_get_local_state_dir() is intended
to provide. Instead, this function try to use the following in order:
- $XDG_RUNTIME_DIR
- LocalAppData folder
- get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR "/run")

This function does not use g_get_user_runtime_dir() either as it
falls back to g_get_user_cache_dir() when $XDG_DATA_DIRS is not
available. In the case, we rather use:
get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR "/run")

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
 include/qemu/osdep.h | 12 ++++++++++++
 util/oslib-posix.c   | 11 +++++++++++
 util/oslib-win32.c   | 26 ++++++++++++++++++++++++++
 3 files changed, 49 insertions(+)

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index 2897720fac..bb857c910f 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -636,6 +636,18 @@ void qemu_set_cloexec(int fd);
  */
 char *qemu_get_local_state_dir(void);
 
+/**
+ * qemu_get_runtime_dir:
+ *
+ * Return a dynamically allocated directory path that is appropriate for storing
+ * runtime files. It corresponds to "run" directory in Unix, and uses
+ * $XDG_RUNTIME_DIR if available.
+ *
+ * The caller is responsible for releasing the value returned with g_free()
+ * after use.
+ */
+char *qemu_get_runtime_dir(void);
+
 /**
  * qemu_getauxval:
  * @type: the auxiliary vector key to lookup
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index e86fd64e09..0c82717be5 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -273,6 +273,17 @@ qemu_get_local_state_dir(void)
     return get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR);
 }
 
+char *
+qemu_get_runtime_dir(void)
+{
+    char *env = getenv("XDG_RUNTIME_DIR");
+    if (env) {
+        return g_strdup(env);
+    }
+
+    return get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR "/run");
+}
+
 void qemu_set_tty_echo(int fd, bool echo)
 {
     struct termios tty;
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index 19a0ea7fbe..38df7b57b5 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -27,6 +27,8 @@
  */
 
 #include "qemu/osdep.h"
+#include <shlobj.h>
+#include <wchar.h>
 #include <windows.h>
 #include "qapi/error.h"
 #include "qemu/main-loop.h"
@@ -237,6 +239,30 @@ qemu_get_local_state_dir(void)
     return g_strdup(data_dirs[0]);
 }
 
+char *
+qemu_get_runtime_dir(void)
+{
+    size_t size = GetEnvironmentVariableA("XDG_RUNTIME_DIR", NULL, 0);
+    if (size) {
+        char *env = g_malloc(size);
+        GetEnvironmentVariableA("XDG_RUNTIME_DIR", env, size);
+        return env;
+    }
+
+    PWSTR wpath;
+    const wchar_t *cwpath;
+    if (!SHGetKnownFolderPath(&FOLDERID_LocalAppData, KF_FLAG_DEFAULT, NULL, &wpath)) {
+        cwpath = wpath;
+        size = wcsrtombs(NULL, &cwpath, 0, &(mbstate_t){0}) + 1;
+        char *path = g_malloc(size);
+        wcsrtombs(path, &cwpath, size, &(mbstate_t){0});
+        CoTaskMemFree(wpath);
+        return path;
+    }
+
+    return get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR "/run");
+}
+
 void qemu_set_tty_echo(int fd, bool echo)
 {
     HANDLE handle = (HANDLE)_get_osfhandle(fd);
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v3 2/8] ivshmem-server: Use qemu_get_runtime_dir()
  2023-09-21  7:54 [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir() Akihiko Odaki
  2023-09-21  7:54 ` [PATCH v3 1/8] " Akihiko Odaki
@ 2023-09-21  7:54 ` Akihiko Odaki
  2023-09-21  7:54 ` [PATCH v3 3/8] contrib/rdmacm-mux: " Akihiko Odaki
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Akihiko Odaki @ 2023-09-21  7:54 UTC (permalink / raw)
  Cc: qemu-devel, qemu-block, virtio-fs, Yuval Shaia, Marcel Apfelbaum,
	Konstantin Kostiuk, Michael Roth, Paolo Bonzini, Fam Zheng,
	Dr . David Alan Gilbert, Stefan Hajnoczi, Gerd Hoffmann,
	Stefan Weil, Yan Vugenfirer, Akihiko Odaki

qemu_get_runtime_dir() is used to construct the default PID file path.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
 contrib/ivshmem-server/main.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/contrib/ivshmem-server/main.c b/contrib/ivshmem-server/main.c
index 5901f17707..313124dd45 100644
--- a/contrib/ivshmem-server/main.c
+++ b/contrib/ivshmem-server/main.c
@@ -14,7 +14,6 @@
 
 #define IVSHMEM_SERVER_DEFAULT_VERBOSE        0
 #define IVSHMEM_SERVER_DEFAULT_FOREGROUND     0
-#define IVSHMEM_SERVER_DEFAULT_PID_FILE       "/var/run/ivshmem-server.pid"
 #define IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH "/tmp/ivshmem_socket"
 #define IVSHMEM_SERVER_DEFAULT_SHM_PATH       "ivshmem"
 #define IVSHMEM_SERVER_DEFAULT_SHM_SIZE       (4 * 1024 * 1024)
@@ -35,15 +34,23 @@ typedef struct IvshmemServerArgs {
     unsigned n_vectors;
 } IvshmemServerArgs;
 
+static char *ivshmem_server_get_default_pid_file(void)
+{
+    g_autofree char *run = qemu_get_runtime_dir();
+    return g_build_filename(run, "ivshmem-server.pid", NULL);
+}
+
 static void
 ivshmem_server_usage(const char *progname)
 {
+    g_autofree char *pid_file = ivshmem_server_get_default_pid_file();
+
     printf("Usage: %s [OPTION]...\n"
            "  -h: show this help\n"
            "  -v: verbose mode\n"
            "  -F: foreground mode (default is to daemonize)\n"
            "  -p <pid-file>: path to the PID file (used in daemon mode only)\n"
-           "     default " IVSHMEM_SERVER_DEFAULT_PID_FILE "\n"
+           "     default %s\n"
            "  -S <unix-socket-path>: path to the unix socket to listen to\n"
            "     default " IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH "\n"
            "  -M <shm-name>: POSIX shared memory object to use\n"
@@ -54,7 +61,7 @@ ivshmem_server_usage(const char *progname)
            "     default %u\n"
            "  -n <nvectors>: number of vectors\n"
            "     default %u\n",
-           progname, IVSHMEM_SERVER_DEFAULT_SHM_SIZE,
+           progname, pid_file, IVSHMEM_SERVER_DEFAULT_SHM_SIZE,
            IVSHMEM_SERVER_DEFAULT_N_VECTORS);
 }
 
@@ -189,10 +196,10 @@ main(int argc, char *argv[])
 {
     IvshmemServer server;
     struct sigaction sa, sa_quit;
+    g_autofree char *default_pid_file = NULL;
     IvshmemServerArgs args = {
         .verbose = IVSHMEM_SERVER_DEFAULT_VERBOSE,
         .foreground = IVSHMEM_SERVER_DEFAULT_FOREGROUND,
-        .pid_file = IVSHMEM_SERVER_DEFAULT_PID_FILE,
         .unix_socket_path = IVSHMEM_SERVER_DEFAULT_UNIX_SOCK_PATH,
         .shm_path = IVSHMEM_SERVER_DEFAULT_SHM_PATH,
         .use_shm_open = true,
@@ -207,6 +214,11 @@ main(int argc, char *argv[])
      */
     printf("*** Example code, do not use in production ***\n");
 
+    qemu_init_exec_dir(argv[0]);
+
+    default_pid_file = ivshmem_server_get_default_pid_file();
+    args.pid_file = default_pid_file;
+
     /* parse arguments, will exit on error */
     ivshmem_server_parse_args(&args, argc, argv);
 
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v3 3/8] contrib/rdmacm-mux: Use qemu_get_runtime_dir()
  2023-09-21  7:54 [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir() Akihiko Odaki
  2023-09-21  7:54 ` [PATCH v3 1/8] " Akihiko Odaki
  2023-09-21  7:54 ` [PATCH v3 2/8] ivshmem-server: Use qemu_get_runtime_dir() Akihiko Odaki
@ 2023-09-21  7:54 ` Akihiko Odaki
  2023-09-21  7:54 ` [PATCH v3 4/8] qga: " Akihiko Odaki
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Akihiko Odaki @ 2023-09-21  7:54 UTC (permalink / raw)
  Cc: qemu-devel, qemu-block, virtio-fs, Yuval Shaia, Marcel Apfelbaum,
	Konstantin Kostiuk, Michael Roth, Paolo Bonzini, Fam Zheng,
	Dr . David Alan Gilbert, Stefan Hajnoczi, Gerd Hoffmann,
	Stefan Weil, Yan Vugenfirer, Akihiko Odaki

qemu_get_runtime_dir() is used to construct the default Unix socket
path.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
 contrib/rdmacm-mux/main.c      | 22 ++++++++++++++--------
 contrib/rdmacm-mux/meson.build |  2 +-
 2 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/contrib/rdmacm-mux/main.c b/contrib/rdmacm-mux/main.c
index 771ca01e03..00c14031ca 100644
--- a/contrib/rdmacm-mux/main.c
+++ b/contrib/rdmacm-mux/main.c
@@ -14,6 +14,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/cutils.h"
 #include <sys/poll.h>
 #include <sys/ioctl.h>
 #include <pthread.h>
@@ -40,8 +41,6 @@
 #define CM_REQ_DGID_POS      80
 #define CM_SIDR_REQ_DGID_POS 44
 
-/* The below can be override by command line parameter */
-#define UNIX_SOCKET_PATH "/var/run/rdmacm-mux"
 /* Has format %s-%s-%d" <path>-<rdma-dev--name>-<port> */
 #define SOCKET_PATH_MAX (PATH_MAX - NAME_MAX - sizeof(int) - 2)
 #define RDMA_PORT_NUM 1
@@ -77,7 +76,13 @@ typedef struct RdmaCmServer {
 
 static RdmaCMServer server = {0};
 
-static void usage(const char *progname)
+static char *get_default_unix_socket_path(void)
+{
+    g_autofree char *run = qemu_get_runtime_dir();
+    return g_build_filename(run, "rdmacm-mux", NULL);
+}
+
+static void usage(const char *progname, const char *default_unix_socket_path)
 {
     printf("Usage: %s [OPTION]...\n"
            "Start a RDMA-CM multiplexer\n"
@@ -86,7 +91,7 @@ static void usage(const char *progname)
            "\t-d rdma-device-name   Name of RDMA device to register with\n"
            "\t-s unix-socket-path   Path to unix socket to listen on (default %s)\n"
            "\t-p rdma-device-port   Port number of RDMA device to register with (default %d)\n",
-           progname, UNIX_SOCKET_PATH, RDMA_PORT_NUM);
+           progname, default_unix_socket_path, RDMA_PORT_NUM);
 }
 
 static void help(const char *progname)
@@ -97,16 +102,16 @@ static void help(const char *progname)
 static void parse_args(int argc, char *argv[])
 {
     int c;
-    char unix_socket_path[SOCKET_PATH_MAX];
+    g_autofree char *default_unix_socket_path = get_default_unix_socket_path();
+    char *unix_socket_path = default_unix_socket_path;
 
     strcpy(server.args.rdma_dev_name, "");
-    strcpy(unix_socket_path, UNIX_SOCKET_PATH);
     server.args.rdma_port_num = RDMA_PORT_NUM;
 
     while ((c = getopt(argc, argv, "hs:d:p:")) != -1) {
         switch (c) {
         case 'h':
-            usage(argv[0]);
+            usage(argv[0], default_unix_socket_path);
             exit(0);
 
         case 'd':
@@ -115,7 +120,7 @@ static void parse_args(int argc, char *argv[])
 
         case 's':
             /* This is temporary, final name will build below */
-            strncpy(unix_socket_path, optarg, SOCKET_PATH_MAX - 1);
+            unix_socket_path = optarg;
             break;
 
         case 'p':
@@ -811,6 +816,7 @@ int main(int argc, char *argv[])
 {
     int rc;
 
+    qemu_init_exec_dir(argv[0]);
     memset(&server, 0, sizeof(server));
 
     parse_args(argc, argv);
diff --git a/contrib/rdmacm-mux/meson.build b/contrib/rdmacm-mux/meson.build
index 36c9c89630..59f60f9cac 100644
--- a/contrib/rdmacm-mux/meson.build
+++ b/contrib/rdmacm-mux/meson.build
@@ -1,7 +1,7 @@
 if have_pvrdma
   # FIXME: broken on big endian architectures
   executable('rdmacm-mux', files('main.c'), genh,
-             dependencies: [glib, libumad],
+             dependencies: [glib, libumad, qemuutil],
              build_by_default: false,
              install: false)
 endif
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v3 4/8] qga: Use qemu_get_runtime_dir()
  2023-09-21  7:54 [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir() Akihiko Odaki
                   ` (2 preceding siblings ...)
  2023-09-21  7:54 ` [PATCH v3 3/8] contrib/rdmacm-mux: " Akihiko Odaki
@ 2023-09-21  7:54 ` Akihiko Odaki
  2023-09-21  7:54 ` [PATCH v3 5/8] scsi: " Akihiko Odaki
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Akihiko Odaki @ 2023-09-21  7:54 UTC (permalink / raw)
  Cc: qemu-devel, qemu-block, virtio-fs, Yuval Shaia, Marcel Apfelbaum,
	Konstantin Kostiuk, Michael Roth, Paolo Bonzini, Fam Zheng,
	Dr . David Alan Gilbert, Stefan Hajnoczi, Gerd Hoffmann,
	Stefan Weil, Yan Vugenfirer, Akihiko Odaki

qemu_get_runtime_dir() is used to construct the default state directory.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
 qga/main.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/qga/main.c b/qga/main.c
index 8668b9f3d3..145ee02df3 100644
--- a/qga/main.c
+++ b/qga/main.c
@@ -45,12 +45,11 @@
 #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0"
 #endif /* CONFIG_BSD */
 #define QGA_SERIAL_PATH_DEFAULT "/dev/ttyS0"
-#define QGA_STATE_RELATIVE_DIR  "run"
 #else
 #define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0"
-#define QGA_STATE_RELATIVE_DIR  "qemu-ga"
 #define QGA_SERIAL_PATH_DEFAULT "COM1"
 #endif
+#define QGA_STATE_RELATIVE_DIR  "qemu-ga"
 #ifdef CONFIG_FSFREEZE
 #define QGA_FSFREEZE_HOOK_DEFAULT CONFIG_QEMU_CONFDIR "/fsfreeze-hook"
 #endif
@@ -129,12 +128,12 @@ static void stop_agent(GAState *s, bool requested);
 static void
 init_dfl_pathnames(void)
 {
-    g_autofree char *state = qemu_get_local_state_dir();
+    g_autofree char *run = qemu_get_runtime_dir();
 
     g_assert(dfl_pathnames.state_dir == NULL);
     g_assert(dfl_pathnames.pidfile == NULL);
-    dfl_pathnames.state_dir = g_build_filename(state, QGA_STATE_RELATIVE_DIR, NULL);
-    dfl_pathnames.pidfile = g_build_filename(state, QGA_STATE_RELATIVE_DIR, "qemu-ga.pid", NULL);
+    dfl_pathnames.state_dir = g_build_filename(run, QGA_STATE_RELATIVE_DIR, NULL);
+    dfl_pathnames.pidfile = g_build_filename(run, QGA_STATE_RELATIVE_DIR, "qemu-ga.pid", NULL);
 }
 
 static void quit_handler(int sig)
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v3 5/8] scsi: Use qemu_get_runtime_dir()
  2023-09-21  7:54 [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir() Akihiko Odaki
                   ` (3 preceding siblings ...)
  2023-09-21  7:54 ` [PATCH v3 4/8] qga: " Akihiko Odaki
@ 2023-09-21  7:54 ` Akihiko Odaki
  2023-09-21  7:54 ` [PATCH v3 6/8] module: " Akihiko Odaki
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Akihiko Odaki @ 2023-09-21  7:54 UTC (permalink / raw)
  Cc: qemu-devel, qemu-block, virtio-fs, Yuval Shaia, Marcel Apfelbaum,
	Konstantin Kostiuk, Michael Roth, Paolo Bonzini, Fam Zheng,
	Dr . David Alan Gilbert, Stefan Hajnoczi, Gerd Hoffmann,
	Stefan Weil, Yan Vugenfirer, Akihiko Odaki

qemu_get_runtime_dir() is used to construct the default paths.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
 scsi/qemu-pr-helper.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/scsi/qemu-pr-helper.c b/scsi/qemu-pr-helper.c
index c6c6347e9b..507f23357f 100644
--- a/scsi/qemu-pr-helper.c
+++ b/scsi/qemu-pr-helper.c
@@ -77,10 +77,10 @@ static int gid = -1;
 
 static void compute_default_paths(void)
 {
-    g_autofree char *state = qemu_get_local_state_dir();
+    g_autofree char *run = qemu_get_runtime_dir();
 
-    socket_path = g_build_filename(state, "run", "qemu-pr-helper.sock", NULL);
-    pidfile = g_build_filename(state, "run", "qemu-pr-helper.pid", NULL);
+    socket_path = g_build_filename(run, "qemu-pr-helper.sock", NULL);
+    pidfile = g_build_filename(run, "qemu-pr-helper.pid", NULL);
 }
 
 static void usage(const char *name)
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v3 6/8] module: Use qemu_get_runtime_dir()
  2023-09-21  7:54 [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir() Akihiko Odaki
                   ` (4 preceding siblings ...)
  2023-09-21  7:54 ` [PATCH v3 5/8] scsi: " Akihiko Odaki
@ 2023-09-21  7:54 ` Akihiko Odaki
  2023-09-21  7:54 ` [PATCH v3 7/8] util: Remove qemu_get_local_state_dir() Akihiko Odaki
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Akihiko Odaki @ 2023-09-21  7:54 UTC (permalink / raw)
  Cc: qemu-devel, qemu-block, virtio-fs, Yuval Shaia, Marcel Apfelbaum,
	Konstantin Kostiuk, Michael Roth, Paolo Bonzini, Fam Zheng,
	Dr . David Alan Gilbert, Stefan Hajnoczi, Gerd Hoffmann,
	Stefan Weil, Yan Vugenfirer, Akihiko Odaki

qemu_get_runtime_dir() is used to construct the path to module upgrades.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
 util/module.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/util/module.c b/util/module.c
index 32e263163c..580658edf4 100644
--- a/util/module.c
+++ b/util/module.c
@@ -242,7 +242,8 @@ int module_load(const char *prefix, const char *name, Error **errp)
     version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION),
                              G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.~",
                              '_');
-    dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir);
+    g_autofree char *run = qemu_get_runtime_dir();
+    dirs[n_dirs++] = g_build_filename(run, "qemu", version_dir, NULL);
 #endif
     assert(n_dirs <= ARRAY_SIZE(dirs));
 
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v3 7/8] util: Remove qemu_get_local_state_dir()
  2023-09-21  7:54 [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir() Akihiko Odaki
                   ` (5 preceding siblings ...)
  2023-09-21  7:54 ` [PATCH v3 6/8] module: " Akihiko Odaki
@ 2023-09-21  7:54 ` Akihiko Odaki
  2023-09-21  7:54 ` [PATCH v3 8/8] spice-app: Use qemu_get_runtime_dir() Akihiko Odaki
  2024-02-18  7:19 ` [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir() Akihiko Odaki
  8 siblings, 0 replies; 10+ messages in thread
From: Akihiko Odaki @ 2023-09-21  7:54 UTC (permalink / raw)
  Cc: qemu-devel, qemu-block, virtio-fs, Yuval Shaia, Marcel Apfelbaum,
	Konstantin Kostiuk, Michael Roth, Paolo Bonzini, Fam Zheng,
	Dr . David Alan Gilbert, Stefan Hajnoczi, Gerd Hoffmann,
	Stefan Weil, Yan Vugenfirer, Akihiko Odaki

There are no users of the function anymore.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
 include/qemu/osdep.h |  8 --------
 util/oslib-posix.c   |  6 ------
 util/oslib-win32.c   | 10 ----------
 3 files changed, 24 deletions(-)

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index bb857c910f..cc585447ef 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -628,14 +628,6 @@ ssize_t qemu_write_full(int fd, const void *buf, size_t count)
 
 void qemu_set_cloexec(int fd);
 
-/* Return a dynamically allocated directory path that is appropriate for storing
- * local state.
- *
- * The caller is responsible for releasing the value returned with g_free()
- * after use.
- */
-char *qemu_get_local_state_dir(void);
-
 /**
  * qemu_get_runtime_dir:
  *
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 0c82717be5..f3054ad2cd 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -267,12 +267,6 @@ int qemu_socketpair(int domain, int type, int protocol, int sv[2])
     return ret;
 }
 
-char *
-qemu_get_local_state_dir(void)
-{
-    return get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR);
-}
-
 char *
 qemu_get_runtime_dir(void)
 {
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index 38df7b57b5..f93c3bff8e 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -229,16 +229,6 @@ int qemu_get_thread_id(void)
     return GetCurrentThreadId();
 }
 
-char *
-qemu_get_local_state_dir(void)
-{
-    const char * const *data_dirs = g_get_system_data_dirs();
-
-    g_assert(data_dirs && data_dirs[0]);
-
-    return g_strdup(data_dirs[0]);
-}
-
 char *
 qemu_get_runtime_dir(void)
 {
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v3 8/8] spice-app: Use qemu_get_runtime_dir()
  2023-09-21  7:54 [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir() Akihiko Odaki
                   ` (6 preceding siblings ...)
  2023-09-21  7:54 ` [PATCH v3 7/8] util: Remove qemu_get_local_state_dir() Akihiko Odaki
@ 2023-09-21  7:54 ` Akihiko Odaki
  2024-02-18  7:19 ` [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir() Akihiko Odaki
  8 siblings, 0 replies; 10+ messages in thread
From: Akihiko Odaki @ 2023-09-21  7:54 UTC (permalink / raw)
  Cc: qemu-devel, qemu-block, virtio-fs, Yuval Shaia, Marcel Apfelbaum,
	Konstantin Kostiuk, Michael Roth, Paolo Bonzini, Fam Zheng,
	Dr . David Alan Gilbert, Stefan Hajnoczi, Gerd Hoffmann,
	Stefan Weil, Yan Vugenfirer, Akihiko Odaki

qemu_get_runtime_dir() provides QEMU-specific fallback of runtime
directory.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
 ui/spice-app.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/ui/spice-app.c b/ui/spice-app.c
index 405fb7f9f5..f6c2343213 100644
--- a/ui/spice-app.c
+++ b/ui/spice-app.c
@@ -151,8 +151,8 @@ static void spice_app_display_early_init(DisplayOptions *opts)
     atexit(spice_app_atexit);
 
     if (qemu_name) {
-        app_dir = g_build_filename(g_get_user_runtime_dir(),
-                                   "qemu", qemu_name, NULL);
+        g_autofree char *run = qemu_get_runtime_dir();
+        app_dir = g_build_filename(run, "qemu", qemu_name, NULL);
         if (g_mkdir_with_parents(app_dir, S_IRWXU) < -1) {
             error_report("Failed to create directory %s: %s",
                          app_dir, strerror(errno));
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir()
  2023-09-21  7:54 [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir() Akihiko Odaki
                   ` (7 preceding siblings ...)
  2023-09-21  7:54 ` [PATCH v3 8/8] spice-app: Use qemu_get_runtime_dir() Akihiko Odaki
@ 2024-02-18  7:19 ` Akihiko Odaki
  8 siblings, 0 replies; 10+ messages in thread
From: Akihiko Odaki @ 2024-02-18  7:19 UTC (permalink / raw)
  Cc: qemu-devel, qemu-block, virtio-fs, Yuval Shaia, Marcel Apfelbaum,
	Konstantin Kostiuk, Michael Roth, Paolo Bonzini, Fam Zheng,
	Dr . David Alan Gilbert, Stefan Hajnoczi, Gerd Hoffmann,
	Stefan Weil, Yan Vugenfirer

Hi,

This patch series has been forgotten for a while but can still be 
applied. Can anyone review it?

Regards,
Akihiko Odaki

On 2023/09/21 16:54, Akihiko Odaki wrote:
> qemu_get_runtime_dir() returns a dynamically allocated directory path
> that is appropriate for storing runtime files. It corresponds to "run"
> directory in Unix.
> 
> With a tree-wide search, it was found that there are several cases
> where such a functionality is implemented so let's have one as a common
> utlity function.
> 
> A notable feature of qemu_get_runtime_dir() is that it uses
> $XDG_RUNTIME_DIR if available. While the function is often called by
> executables which requires root privileges, it is still possible that
> they are called from a user without privilege to write the system
> runtime directory. In fact, I decided to write this patch when I ran
> virtiofsd in a Linux namespace created by a normal user and realized
> it tries to write the system runtime directory, not writable in this
> case. $XDG_RUNTIME_DIR should provide a writable directory in such
> cases.
> 
> This function does not use qemu_get_local_state_dir() or its logic
> for Windows. Actually the implementation of qemu_get_local_state_dir()
> for Windows seems not right as it calls g_get_system_data_dirs(),
> which refers to $XDG_DATA_DIRS. In Unix terminology, it is basically
> "/usr/share", not "/var", which qemu_get_local_state_dir() is intended
> to provide. Instead, this function try to use the following in order:
> - $XDG_RUNTIME_DIR
> - LocalAppData folder
> - get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR "/run")
> 
> This function does not use g_get_user_runtime_dir() either as it
> falls back to g_get_user_cache_dir() when $XDG_DATA_DIRS is not
> available. In the case, we rather use:
> get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR "/run")
> 
> V2 -> V3:
>    Rebase to the current master.
>    Dropped patch "qga: Remove platform GUID definitions" since it is
>    irrelevant.
> 
> V1 -> V2:
>    Rebased to the current master since Patchew complains.
> 
> Akihiko Odaki (8):
>    util: Introduce qemu_get_runtime_dir()
>    ivshmem-server: Use qemu_get_runtime_dir()
>    contrib/rdmacm-mux: Use qemu_get_runtime_dir()
>    qga: Use qemu_get_runtime_dir()
>    scsi: Use qemu_get_runtime_dir()
>    module: Use qemu_get_runtime_dir()
>    util: Remove qemu_get_local_state_dir()
>    spice-app: Use qemu_get_runtime_dir()
> 
>   include/qemu/osdep.h           | 10 +++++++---
>   contrib/ivshmem-server/main.c  | 20 ++++++++++++++++----
>   contrib/rdmacm-mux/main.c      | 22 ++++++++++++++--------
>   qga/main.c                     |  9 ++++-----
>   scsi/qemu-pr-helper.c          |  6 +++---
>   ui/spice-app.c                 |  4 ++--
>   util/module.c                  |  3 ++-
>   util/oslib-posix.c             |  9 +++++++--
>   util/oslib-win32.c             | 24 ++++++++++++++++++++----
>   contrib/rdmacm-mux/meson.build |  2 +-
>   10 files changed, 76 insertions(+), 33 deletions(-)
> 


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-02-18  7:20 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-21  7:54 [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir() Akihiko Odaki
2023-09-21  7:54 ` [PATCH v3 1/8] " Akihiko Odaki
2023-09-21  7:54 ` [PATCH v3 2/8] ivshmem-server: Use qemu_get_runtime_dir() Akihiko Odaki
2023-09-21  7:54 ` [PATCH v3 3/8] contrib/rdmacm-mux: " Akihiko Odaki
2023-09-21  7:54 ` [PATCH v3 4/8] qga: " Akihiko Odaki
2023-09-21  7:54 ` [PATCH v3 5/8] scsi: " Akihiko Odaki
2023-09-21  7:54 ` [PATCH v3 6/8] module: " Akihiko Odaki
2023-09-21  7:54 ` [PATCH v3 7/8] util: Remove qemu_get_local_state_dir() Akihiko Odaki
2023-09-21  7:54 ` [PATCH v3 8/8] spice-app: Use qemu_get_runtime_dir() Akihiko Odaki
2024-02-18  7:19 ` [PATCH v3 0/8] util: Introduce qemu_get_runtime_dir() Akihiko Odaki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).