All of lore.kernel.org
 help / color / mirror / Atom feed
From: marcandre.lureau@redhat.com
To: qemu-devel@nongnu.org
Cc: "Kevin Wolf" <kwolf@redhat.com>,
	"Laurent Vivier" <lvivier@redhat.com>,
	"Thomas Huth" <thuth@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"open list:Block layer core" <qemu-block@nongnu.org>,
	"Darren Kenny" <darren.kenny@oracle.com>,
	"Stefan Weil" <sw@weilnetz.de>, "Bandan Das" <bsd@redhat.com>,
	"Qiuhao Li" <Qiuhao.Li@outlook.com>,
	"Alexander Bulekov" <alxndr@bu.edu>,
	"Hanna Reitz" <hreitz@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: [PATCH v2 03/26] include: move qemu_*_exec_dir() to cutils
Date: Tue, 26 Apr 2022 13:26:52 +0400	[thread overview]
Message-ID: <20220426092715.3931705-4-marcandre.lureau@redhat.com> (raw)
In-Reply-To: <20220426092715.3931705-1-marcandre.lureau@redhat.com>

From: Marc-André Lureau <marcandre.lureau@redhat.com>

The function is required by get_relocated_path() (already in cutils),
and used by qemu-ga and may be generally useful.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 include/qemu/cutils.h                |   7 ++
 include/qemu/osdep.h                 |   8 --
 qemu-io.c                            |   1 +
 storage-daemon/qemu-storage-daemon.c |   1 +
 tests/qtest/fuzz/fuzz.c              |   1 +
 util/cutils.c                        | 108 +++++++++++++++++++++++++++
 util/oslib-posix.c                   |  81 --------------------
 util/oslib-win32.c                   |  36 ---------
 8 files changed, 118 insertions(+), 125 deletions(-)

diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
index 5c6572d44422..40e10e19a7ed 100644
--- a/include/qemu/cutils.h
+++ b/include/qemu/cutils.h
@@ -193,6 +193,13 @@ int uleb128_decode_small(const uint8_t *in, uint32_t *n);
  */
 int qemu_pstrcmp0(const char **str1, const char **str2);
 
+/* Find program directory, and save it for later usage with
+ * qemu_get_exec_dir().
+ * Try OS specific API first, if not working, parse from argv0. */
+void qemu_init_exec_dir(const char *argv0);
+
+/* Get the saved exec dir.  */
+const char *qemu_get_exec_dir(void);
 
 /**
  * get_relocated_path:
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index baaa23c1568d..220b44f710e5 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -564,14 +564,6 @@ bool fips_get_state(void);
  */
 char *qemu_get_local_state_dir(void);
 
-/* Find program directory, and save it for later usage with
- * qemu_get_exec_dir().
- * Try OS specific API first, if not working, parse from argv0. */
-void qemu_init_exec_dir(const char *argv0);
-
-/* Get the saved exec dir.  */
-const char *qemu_get_exec_dir(void);
-
 /**
  * qemu_getauxval:
  * @type: the auxiliary vector key to lookup
diff --git a/qemu-io.c b/qemu-io.c
index d70d3dd4fde5..2bd7bfb65073 100644
--- a/qemu-io.c
+++ b/qemu-io.c
@@ -16,6 +16,7 @@
 #endif
 
 #include "qemu/help-texts.h"
+#include "qemu/cutils.h"
 #include "qapi/error.h"
 #include "qemu-io.h"
 #include "qemu/error-report.h"
diff --git a/storage-daemon/qemu-storage-daemon.c b/storage-daemon/qemu-storage-daemon.c
index 9b8b17f52e48..c104817cdddc 100644
--- a/storage-daemon/qemu-storage-daemon.c
+++ b/storage-daemon/qemu-storage-daemon.c
@@ -44,6 +44,7 @@
 
 #include "qemu/help-texts.h"
 #include "qemu-version.h"
+#include "qemu/cutils.h"
 #include "qemu/config-file.h"
 #include "qemu/error-report.h"
 #include "qemu/help_option.h"
diff --git a/tests/qtest/fuzz/fuzz.c b/tests/qtest/fuzz/fuzz.c
index 5f77c849837f..d3afd294db24 100644
--- a/tests/qtest/fuzz/fuzz.c
+++ b/tests/qtest/fuzz/fuzz.c
@@ -15,6 +15,7 @@
 
 #include <wordexp.h>
 
+#include "qemu/cutils.h"
 #include "qemu/datadir.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/qtest.h"
diff --git a/util/cutils.c b/util/cutils.c
index b2777210e7da..6cc7cc8cde99 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -931,6 +931,114 @@ static inline const char *next_component(const char *dir, int *p_len)
     return dir;
 }
 
+static const char *exec_dir;
+
+void qemu_init_exec_dir(const char *argv0)
+{
+#ifdef G_OS_WIN32
+    char *p;
+    char buf[MAX_PATH];
+    DWORD len;
+
+    if (exec_dir) {
+        return;
+    }
+
+    len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
+    if (len == 0) {
+        return;
+    }
+
+    buf[len] = 0;
+    p = buf + len - 1;
+    while (p != buf && *p != '\\') {
+        p--;
+    }
+    *p = 0;
+    if (access(buf, R_OK) == 0) {
+        exec_dir = g_strdup(buf);
+    } else {
+        exec_dir = CONFIG_BINDIR;
+    }
+#else
+    char *p = NULL;
+    char buf[PATH_MAX];
+
+    if (exec_dir) {
+        return;
+    }
+
+#if defined(__linux__)
+    {
+        int len;
+        len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
+        if (len > 0) {
+            buf[len] = 0;
+            p = buf;
+        }
+    }
+#elif defined(__FreeBSD__) \
+      || (defined(__NetBSD__) && defined(KERN_PROC_PATHNAME))
+    {
+#if defined(__FreeBSD__)
+        static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
+#else
+        static int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
+#endif
+        size_t len = sizeof(buf) - 1;
+
+        *buf = '\0';
+        if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) &&
+            *buf) {
+            buf[sizeof(buf) - 1] = '\0';
+            p = buf;
+        }
+    }
+#elif defined(__APPLE__)
+    {
+        char fpath[PATH_MAX];
+        uint32_t len = sizeof(fpath);
+        if (_NSGetExecutablePath(fpath, &len) == 0) {
+            p = realpath(fpath, buf);
+            if (!p) {
+                return;
+            }
+        }
+    }
+#elif defined(__HAIKU__)
+    {
+        image_info ii;
+        int32_t c = 0;
+
+        *buf = '\0';
+        while (get_next_image_info(0, &c, &ii) == B_OK) {
+            if (ii.type == B_APP_IMAGE) {
+                strncpy(buf, ii.name, sizeof(buf));
+                buf[sizeof(buf) - 1] = 0;
+                p = buf;
+                break;
+            }
+        }
+    }
+#endif
+    /* If we don't have any way of figuring out the actual executable
+       location then try argv[0].  */
+    if (!p && argv0) {
+        p = realpath(argv0, buf);
+    }
+    if (p) {
+        exec_dir = g_path_get_dirname(p);
+    } else {
+        exec_dir = CONFIG_BINDIR;
+    }
+#endif
+}
+
+const char *qemu_get_exec_dir(void)
+{
+    return exec_dir;
+}
+
 char *get_relocated_path(const char *dir)
 {
     size_t prefix_len = strlen(CONFIG_PREFIX);
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index b8bf7d4070ce..03d97741562c 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -317,87 +317,6 @@ void qemu_set_tty_echo(int fd, bool echo)
     tcsetattr(fd, TCSANOW, &tty);
 }
 
-static const char *exec_dir;
-
-void qemu_init_exec_dir(const char *argv0)
-{
-    char *p = NULL;
-    char buf[PATH_MAX];
-
-    if (exec_dir) {
-        return;
-    }
-
-#if defined(__linux__)
-    {
-        int len;
-        len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
-        if (len > 0) {
-            buf[len] = 0;
-            p = buf;
-        }
-    }
-#elif defined(__FreeBSD__) \
-      || (defined(__NetBSD__) && defined(KERN_PROC_PATHNAME))
-    {
-#if defined(__FreeBSD__)
-        static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
-#else
-        static int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
-#endif
-        size_t len = sizeof(buf) - 1;
-
-        *buf = '\0';
-        if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) &&
-            *buf) {
-            buf[sizeof(buf) - 1] = '\0';
-            p = buf;
-        }
-    }
-#elif defined(__APPLE__)
-    {
-        char fpath[PATH_MAX];
-        uint32_t len = sizeof(fpath);
-        if (_NSGetExecutablePath(fpath, &len) == 0) {
-            p = realpath(fpath, buf);
-            if (!p) {
-                return;
-            }
-        }
-    }
-#elif defined(__HAIKU__)
-    {
-        image_info ii;
-        int32_t c = 0;
-
-        *buf = '\0';
-        while (get_next_image_info(0, &c, &ii) == B_OK) {
-            if (ii.type == B_APP_IMAGE) {
-                strncpy(buf, ii.name, sizeof(buf));
-                buf[sizeof(buf) - 1] = 0;
-                p = buf;
-                break;
-            }
-        }
-    }
-#endif
-    /* If we don't have any way of figuring out the actual executable
-       location then try argv[0].  */
-    if (!p && argv0) {
-        p = realpath(argv0, buf);
-    }
-    if (p) {
-        exec_dir = g_path_get_dirname(p);
-    } else {
-        exec_dir = CONFIG_BINDIR;
-    }
-}
-
-const char *qemu_get_exec_dir(void)
-{
-    return exec_dir;
-}
-
 #ifdef CONFIG_LINUX
 static void sigbus_handler(int signal, siginfo_t *siginfo, void *ctx)
 #else /* CONFIG_LINUX */
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index 9c1e8121fd6d..9483c4c1d5de 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -269,42 +269,6 @@ void qemu_set_tty_echo(int fd, bool echo)
     }
 }
 
-static const char *exec_dir;
-
-void qemu_init_exec_dir(const char *argv0)
-{
-
-    char *p;
-    char buf[MAX_PATH];
-    DWORD len;
-
-    if (exec_dir) {
-        return;
-    }
-
-    len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
-    if (len == 0) {
-        return;
-    }
-
-    buf[len] = 0;
-    p = buf + len - 1;
-    while (p != buf && *p != '\\') {
-        p--;
-    }
-    *p = 0;
-    if (access(buf, R_OK) == 0) {
-        exec_dir = g_strdup(buf);
-    } else {
-        exec_dir = CONFIG_BINDIR;
-    }
-}
-
-const char *qemu_get_exec_dir(void)
-{
-    return exec_dir;
-}
-
 int getpagesize(void)
 {
     SYSTEM_INFO system_info;
-- 
2.36.0



  parent reply	other threads:[~2022-04-26  9:36 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-26  9:26 [PATCH v2 00/26] Misc cleanups marcandre.lureau
2022-04-26  9:26 ` [PATCH v2 01/26] Use QEMU_SANITIZE_THREAD marcandre.lureau
2022-04-26 20:43   ` Richard Henderson
2022-04-27  7:59     ` Marc-André Lureau
2022-04-26  9:26 ` [PATCH v2 02/26] Use QEMU_SANITIZE_ADDRESS marcandre.lureau
2022-04-26 20:46   ` Richard Henderson
2022-04-27  9:36   ` Thomas Huth
2022-04-26  9:26 ` marcandre.lureau [this message]
2022-04-26  9:26 ` [PATCH v2 04/26] util/win32: simplify qemu_get_local_state_dir() marcandre.lureau
2022-04-26  9:26 ` [PATCH v2 05/26] tests: move libqtest.h back under qtest/ marcandre.lureau
2022-04-26 10:30   ` Stefan Berger
2022-05-30 14:41   ` Juan Quintela
2022-04-26  9:26 ` [PATCH v2 06/26] libqtest: split QMP part in libqmp marcandre.lureau
2022-04-26  9:26 ` [PATCH v2 07/26] tests: make libqmp buildable for win32 marcandre.lureau
2022-04-26  9:32   ` Thomas Huth
2022-04-26 10:25     ` Marc-André Lureau
2022-04-26 10:29       ` Thomas Huth
2022-04-26  9:26 ` [PATCH v2 08/26] Use g_unix_set_fd_nonblocking() marcandre.lureau
2022-04-27  1:00   ` Richard Henderson
2022-04-26  9:26 ` [PATCH v2 09/26] block: move fcntl_setfl() marcandre.lureau
2022-04-27  1:00   ` Richard Henderson
2022-04-26  9:26 ` [PATCH v2 10/26] Replace qemu_pipe() with g_unix_open_pipe() marcandre.lureau
2022-04-27  1:06   ` Richard Henderson
2022-04-26  9:27 ` [PATCH v2 11/26] util: replace pipe()+cloexec " marcandre.lureau
2022-04-27  1:06   ` Richard Henderson
2022-04-26  9:27 ` [PATCH v2 12/26] qga: replace pipe() with g_unix_open_pipe(CLOEXEC) marcandre.lureau
2022-04-27  1:08   ` Richard Henderson
2022-04-27  8:24     ` Marc-André Lureau
2022-05-03 11:22       ` Daniel P. Berrangé
2022-04-26  9:27 ` [PATCH v2 13/26] tests: " marcandre.lureau
2022-05-03 10:11   ` Daniel P. Berrangé
2022-04-26  9:27 ` [PATCH v2 14/26] os-posix: replace pipe()+cloexec " marcandre.lureau
2022-04-27  1:11   ` Richard Henderson
2022-04-26  9:27 ` [PATCH v2 15/26] virtiofsd: replace pipe() " marcandre.lureau
2022-04-26  9:27   ` [Virtio-fs] " marcandre.lureau
2022-05-03 10:16   ` Daniel P. Berrangé
2022-05-03 10:16     ` [Virtio-fs] " Daniel P. Berrangé
2022-04-26  9:27 ` [PATCH v2 16/26] io: " marcandre.lureau
2022-05-03 10:29   ` Daniel P. Berrangé
2022-04-26  9:27 ` [PATCH v2 17/26] Replace fcntl(0_NONBLOCK) with g_unix_set_fd_nonblocking() marcandre.lureau
2022-04-27  1:15   ` Richard Henderson
2022-04-27  9:08     ` Marc-André Lureau
2022-04-26  9:27 ` [PATCH v2 18/26] io: make qio_channel_command_new_pid() static marcandre.lureau
2022-04-27  1:15   ` Richard Henderson
2022-04-26  9:27 ` [PATCH v2 19/26] chardev: replace qemu_set_nonblock() marcandre.lureau
2022-05-03 10:32   ` Daniel P. Berrangé
2022-04-26  9:27 ` [PATCH v2 20/26] io: replace qemu_set{_non}block() marcandre.lureau
2022-05-03 10:33   ` Daniel P. Berrangé
2022-04-26  9:27 ` [PATCH v2 21/26] qga: replace qemu_set_nonblock() marcandre.lureau
2022-05-03 10:35   ` Daniel P. Berrangé
2022-04-26  9:27 ` [PATCH v2 22/26] hw: " marcandre.lureau
2022-05-03 10:38   ` Daniel P. Berrangé
2022-04-26  9:27 ` [PATCH v2 23/26] ui: " marcandre.lureau
2022-05-03 10:41   ` Daniel P. Berrangé
2022-04-26  9:27 ` [PATCH v2 24/26] net: " marcandre.lureau
2022-05-03 10:42   ` Daniel P. Berrangé
2022-04-26  9:27 ` [PATCH v2 25/26] tests: " marcandre.lureau
2022-04-27  9:41   ` Thomas Huth
2022-04-27 10:33     ` Marc-André Lureau
2022-04-27 11:15       ` Thomas Huth
2022-05-03 10:44   ` Daniel P. Berrangé
2022-04-26  9:27 ` [PATCH v2 26/26] util: rename qemu_*block() socket functions marcandre.lureau
2022-04-26 10:28   ` Marc-André Lureau
2022-04-26 14:33   ` Stefan Hajnoczi
2022-04-27 10:39     ` Marc-André Lureau
2022-05-02  7:45 ` [PATCH v2 00/26] Misc cleanups Marc-André Lureau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220426092715.3931705-4-marcandre.lureau@redhat.com \
    --to=marcandre.lureau@redhat.com \
    --cc=Qiuhao.Li@outlook.com \
    --cc=alxndr@bu.edu \
    --cc=berrange@redhat.com \
    --cc=bsd@redhat.com \
    --cc=darren.kenny@oracle.com \
    --cc=hreitz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=sw@weilnetz.de \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.