958e1b
From cf0bb22a1d897cc225f14489eaad0dd02ec4a6e3 Mon Sep 17 00:00:00 2001
958e1b
From: Fam Zheng <famz@redhat.com>
958e1b
Date: Wed, 16 Jul 2014 02:20:25 -0500
958e1b
Subject: [CHANGE 25/29] util: Split out exec_dir from os_find_datadir
958e1b
To: rhvirt-patches@redhat.com,
958e1b
    jen@redhat.com
958e1b
958e1b
RH-Author: Fam Zheng <famz@redhat.com>
958e1b
Message-id: <1405477228-11490-2-git-send-email-famz@redhat.com>
958e1b
Patchwork-id: 59920
958e1b
O-Subject: [RHEL-7 qemu-kvm PATCH 1/4] util: Split out exec_dir from os_find_datadir
958e1b
Bugzilla: 1017685
958e1b
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
958e1b
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
958e1b
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
958e1b
958e1b
From: Miroslav Rezanina <mrezanin@redhat.com>
958e1b
958e1b
Upstream: 10f5bff622cad71645e22c027b77ac31e51008ef
958e1b
958e1b
With this change, main() calls qemu_init_exec_dir and uses argv[0] to
958e1b
init exec_dir. The saved value can be retrieved with
958e1b
qemu_get_exec_dir later. It will be reused by module loading.
958e1b
958e1b
Signed-off-by: Fam Zheng <famz@redhat.com>
958e1b
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
958e1b
958e1b
This is manual backport. Following change is handling -Werror=strict-prototypes
958e1b
error (function declaration isn't a prototype):
958e1b
958e1b
Upstream: char *os_find_datadir();
958e1b
RHEL:     char *os_find_datadir(void);
958e1b
958e1b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
958e1b
Signed-off-by: Fam Zheng <famz@redhat.com>
958e1b
---
958e1b
 include/qemu-common.h |  2 +-
958e1b
 include/qemu/osdep.h  |  9 +++++++++
958e1b
 os-posix.c            | 42 +++++++--------------------------------
958e1b
 os-win32.c            | 21 ++------------------
958e1b
 qemu-img.c            |  1 +
958e1b
 qemu-io.c             |  1 +
958e1b
 qemu-nbd.c            |  1 +
958e1b
 util/oslib-posix.c    | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++
958e1b
 util/oslib-win32.c    | 30 ++++++++++++++++++++++++++++
958e1b
 vl.c                  |  3 ++-
958e1b
 10 files changed, 108 insertions(+), 56 deletions(-)
958e1b
958e1b
Signed-off-by: jen <jen@redhat.com>
958e1b
---
958e1b
 include/qemu-common.h |  2 +-
958e1b
 include/qemu/osdep.h  |  9 +++++++++
958e1b
 os-posix.c            | 42 +++++++--------------------------------
958e1b
 os-win32.c            | 21 ++------------------
958e1b
 qemu-img.c            |  1 +
958e1b
 qemu-io.c             |  1 +
958e1b
 qemu-nbd.c            |  1 +
958e1b
 util/oslib-posix.c    | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++
958e1b
 util/oslib-win32.c    | 30 ++++++++++++++++++++++++++++
958e1b
 vl.c                  |  3 ++-
958e1b
 10 files changed, 108 insertions(+), 56 deletions(-)
958e1b
958e1b
diff --git a/include/qemu-common.h b/include/qemu-common.h
958e1b
index 73c6419..aee85e3 100644
958e1b
--- a/include/qemu-common.h
958e1b
+++ b/include/qemu-common.h
958e1b
@@ -360,7 +360,7 @@ char *qemu_find_file(int type, const char *name);
958e1b
 
958e1b
 /* OS specific functions */
958e1b
 void os_setup_early_signal_handling(void);
958e1b
-char *os_find_datadir(const char *argv0);
958e1b
+char *os_find_datadir(void);
958e1b
 void os_parse_cmd_args(int index, const char *optarg);
958e1b
 void os_pidfile_error(void);
958e1b
 
958e1b
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
958e1b
index 26136f1..7a5ae28 100644
958e1b
--- a/include/qemu/osdep.h
958e1b
+++ b/include/qemu/osdep.h
958e1b
@@ -215,4 +215,13 @@ bool fips_get_state(void);
958e1b
  */
958e1b
 char *qemu_get_local_state_pathname(const char *relative_pathname);
958e1b
 
958e1b
+/* Find program directory, and save it for later usage with
958e1b
+ * qemu_get_exec_dir().
958e1b
+ * Try OS specific API first, if not working, parse from argv0. */
958e1b
+void qemu_init_exec_dir(const char *argv0);
958e1b
+
958e1b
+/* Get the saved exec dir.
958e1b
+ * Caller needs to release the returned string by g_free() */
958e1b
+char *qemu_get_exec_dir(void);
958e1b
+
958e1b
 #endif
958e1b
diff --git a/os-posix.c b/os-posix.c
958e1b
index 3a4678a..7cfca4b 100644
958e1b
--- a/os-posix.c
958e1b
+++ b/os-posix.c
958e1b
@@ -84,46 +84,17 @@ void os_setup_signal_handling(void)
958e1b
    running from the build tree this will be "$bindir/../pc-bios".  */
958e1b
 #define SHARE_SUFFIX "/share/qemu-kvm"
958e1b
 #define BUILD_SUFFIX "/pc-bios"
958e1b
-char *os_find_datadir(const char *argv0)
958e1b
+char *os_find_datadir(void)
958e1b
 {
958e1b
-    char *dir;
958e1b
-    char *p = NULL;
958e1b
+    char *dir, *exec_dir;
958e1b
     char *res;
958e1b
-    char buf[PATH_MAX];
958e1b
     size_t max_len;
958e1b
 
958e1b
-#if defined(__linux__)
958e1b
-    {
958e1b
-        int len;
958e1b
-        len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
958e1b
-        if (len > 0) {
958e1b
-            buf[len] = 0;
958e1b
-            p = buf;
958e1b
-        }
958e1b
+    exec_dir = qemu_get_exec_dir();
958e1b
+    if (exec_dir == NULL) {
958e1b
+        return NULL;
958e1b
     }
958e1b
-#elif defined(__FreeBSD__)
958e1b
-    {
958e1b
-        static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
958e1b
-        size_t len = sizeof(buf) - 1;
958e1b
-
958e1b
-        *buf = '\0';
958e1b
-        if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) &&
958e1b
-            *buf) {
958e1b
-            buf[sizeof(buf) - 1] = '\0';
958e1b
-            p = buf;
958e1b
-        }
958e1b
-    }
958e1b
-#endif
958e1b
-    /* If we don't have any way of figuring out the actual executable
958e1b
-       location then try argv[0].  */
958e1b
-    if (!p) {
958e1b
-        p = realpath(argv0, buf);
958e1b
-        if (!p) {
958e1b
-            return NULL;
958e1b
-        }
958e1b
-    }
958e1b
-    dir = dirname(p);
958e1b
-    dir = dirname(dir);
958e1b
+    dir = dirname(exec_dir);
958e1b
 
958e1b
     max_len = strlen(dir) +
958e1b
         MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
958e1b
@@ -137,6 +108,7 @@ char *os_find_datadir(const char *argv0)
958e1b
         }
958e1b
     }
958e1b
 
958e1b
+    g_free(exec_dir);
958e1b
     return res;
958e1b
 }
958e1b
 #undef SHARE_SUFFIX
958e1b
diff --git a/os-win32.c b/os-win32.c
958e1b
index 50b7f6f..5f95caa 100644
958e1b
--- a/os-win32.c
958e1b
+++ b/os-win32.c
958e1b
@@ -84,26 +84,9 @@ void os_setup_early_signal_handling(void)
958e1b
 }
958e1b
 
958e1b
 /* Look for support files in the same directory as the executable.  */
958e1b
-char *os_find_datadir(const char *argv0)
958e1b
+char *os_find_datadir(void)
958e1b
 {
958e1b
-    char *p;
958e1b
-    char buf[MAX_PATH];
958e1b
-    DWORD len;
958e1b
-
958e1b
-    len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
958e1b
-    if (len == 0) {
958e1b
-        return NULL;
958e1b
-    }
958e1b
-
958e1b
-    buf[len] = 0;
958e1b
-    p = buf + len - 1;
958e1b
-    while (p != buf && *p != '\\')
958e1b
-        p--;
958e1b
-    *p = 0;
958e1b
-    if (access(buf, R_OK) == 0) {
958e1b
-        return g_strdup(buf);
958e1b
-    }
958e1b
-    return NULL;
958e1b
+    return qemu_get_exec_dir();
958e1b
 }
958e1b
 
958e1b
 void os_set_line_buffering(void)
958e1b
diff --git a/qemu-img.c b/qemu-img.c
958e1b
index dcce380..ed1799c 100644
958e1b
--- a/qemu-img.c
958e1b
+++ b/qemu-img.c
958e1b
@@ -2726,6 +2726,7 @@ int main(int argc, char **argv)
958e1b
 #endif
958e1b
 
958e1b
     error_set_progname(argv[0]);
958e1b
+    qemu_init_exec_dir(argv[0]);
958e1b
 
958e1b
     qemu_init_main_loop();
958e1b
     bdrv_init();
958e1b
diff --git a/qemu-io.c b/qemu-io.c
958e1b
index 0959178..bbe2518 100644
958e1b
--- a/qemu-io.c
958e1b
+++ b/qemu-io.c
958e1b
@@ -2008,6 +2008,7 @@ int main(int argc, char **argv)
958e1b
 #endif
958e1b
 
958e1b
     progname = basename(argv[0]);
958e1b
+    qemu_init_exec_dir(argv[0]);
958e1b
 
958e1b
     while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
958e1b
         switch (c) {
958e1b
diff --git a/qemu-nbd.c b/qemu-nbd.c
958e1b
index c1b395d..207a610 100644
958e1b
--- a/qemu-nbd.c
958e1b
+++ b/qemu-nbd.c
958e1b
@@ -363,6 +363,7 @@ int main(int argc, char **argv)
958e1b
     memset(&sa_sigterm, 0, sizeof(sa_sigterm));
958e1b
     sa_sigterm.sa_handler = termsig_handler;
958e1b
     sigaction(SIGTERM, &sa_sigterm, NULL);
958e1b
+    qemu_init_exec_dir(argv[0]);
958e1b
 
958e1b
     while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
958e1b
         switch (ch) {
958e1b
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
958e1b
index 2c756c7..fef840a 100644
958e1b
--- a/util/oslib-posix.c
958e1b
+++ b/util/oslib-posix.c
958e1b
@@ -54,6 +54,7 @@ extern int daemon(int, int);
958e1b
 #include "trace.h"
958e1b
 #include "qemu/sockets.h"
958e1b
 #include <sys/mman.h>
958e1b
+#include <libgen.h>
958e1b
 
958e1b
 #ifdef CONFIG_LINUX
958e1b
 #include <sys/syscall.h>
958e1b
@@ -244,3 +245,56 @@ qemu_get_local_state_pathname(const char *relative_pathname)
958e1b
     return g_strdup_printf("%s/%s", CONFIG_QEMU_LOCALSTATEDIR,
958e1b
                            relative_pathname);
958e1b
 }
958e1b
+
958e1b
+static char exec_dir[PATH_MAX];
958e1b
+
958e1b
+void qemu_init_exec_dir(const char *argv0)
958e1b
+{
958e1b
+    char *dir;
958e1b
+    char *p = NULL;
958e1b
+    char buf[PATH_MAX];
958e1b
+
958e1b
+    assert(!exec_dir[0]);
958e1b
+
958e1b
+#if defined(__linux__)
958e1b
+    {
958e1b
+        int len;
958e1b
+        len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
958e1b
+        if (len > 0) {
958e1b
+            buf[len] = 0;
958e1b
+            p = buf;
958e1b
+        }
958e1b
+    }
958e1b
+#elif defined(__FreeBSD__)
958e1b
+    {
958e1b
+        static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
958e1b
+        size_t len = sizeof(buf) - 1;
958e1b
+
958e1b
+        *buf = '\0';
958e1b
+        if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) &&
958e1b
+            *buf) {
958e1b
+            buf[sizeof(buf) - 1] = '\0';
958e1b
+            p = buf;
958e1b
+        }
958e1b
+    }
958e1b
+#endif
958e1b
+    /* If we don't have any way of figuring out the actual executable
958e1b
+       location then try argv[0].  */
958e1b
+    if (!p) {
958e1b
+        if (!argv0) {
958e1b
+            return;
958e1b
+        }
958e1b
+        p = realpath(argv0, buf);
958e1b
+        if (!p) {
958e1b
+            return;
958e1b
+        }
958e1b
+    }
958e1b
+    dir = dirname(p);
958e1b
+
958e1b
+    pstrcpy(exec_dir, sizeof(exec_dir), dir);
958e1b
+}
958e1b
+
958e1b
+char *qemu_get_exec_dir(void)
958e1b
+{
958e1b
+    return g_strdup(exec_dir);
958e1b
+}
958e1b
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
958e1b
index 983b7a2..332e743 100644
958e1b
--- a/util/oslib-win32.c
958e1b
+++ b/util/oslib-win32.c
958e1b
@@ -179,3 +179,33 @@ qemu_get_local_state_pathname(const char *relative_pathname)
958e1b
     return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", base_path,
958e1b
                            relative_pathname);
958e1b
 }
958e1b
+
958e1b
+static char exec_dir[PATH_MAX];
958e1b
+
958e1b
+void qemu_init_exec_dir(const char *argv0)
958e1b
+{
958e1b
+
958e1b
+    char *p;
958e1b
+    char buf[MAX_PATH];
958e1b
+    DWORD len;
958e1b
+
958e1b
+    len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
958e1b
+    if (len == 0) {
958e1b
+        return;
958e1b
+    }
958e1b
+
958e1b
+    buf[len] = 0;
958e1b
+    p = buf + len - 1;
958e1b
+    while (p != buf && *p != '\\') {
958e1b
+        p--;
958e1b
+    }
958e1b
+    *p = 0;
958e1b
+    if (access(buf, R_OK) == 0) {
958e1b
+        pstrcpy(exec_dir, sizeof(exec_dir), buf);
958e1b
+    }
958e1b
+}
958e1b
+
958e1b
+char *qemu_get_exec_dir(void)
958e1b
+{
958e1b
+    return g_strdup(exec_dir);
958e1b
+}
958e1b
diff --git a/vl.c b/vl.c
958e1b
index ca3d498..ead90ba 100644
958e1b
--- a/vl.c
958e1b
+++ b/vl.c
958e1b
@@ -2842,6 +2842,7 @@ int main(int argc, char **argv, char **envp)
958e1b
 
958e1b
     atexit(qemu_run_exit_notifiers);
958e1b
     error_set_progname(argv[0]);
958e1b
+    qemu_init_exec_dir(argv[0]);
958e1b
 
958e1b
     g_mem_set_vtable(&mem_trace);
958e1b
     if (!g_thread_supported()) {
958e1b
@@ -3880,7 +3881,7 @@ int main(int argc, char **argv, char **envp)
958e1b
     /* If no data_dir is specified then try to find it relative to the
958e1b
        executable path.  */
958e1b
     if (data_dir_idx < ARRAY_SIZE(data_dir)) {
958e1b
-        data_dir[data_dir_idx] = os_find_datadir(argv[0]);
958e1b
+        data_dir[data_dir_idx] = os_find_datadir();
958e1b
         if (data_dir[data_dir_idx] != NULL) {
958e1b
             data_dir_idx++;
958e1b
         }
958e1b
-- 
958e1b
1.9.3
958e1b