Commit 10f5bff6 authored by Fam Zheng's avatar Fam Zheng Committed by Paolo Bonzini
Browse files

util: Split out exec_dir from os_find_datadir



With this change, main() calls qemu_init_exec_dir and uses argv[0] to
init exec_dir. The saved value can be retrieved with
qemu_get_exec_dir later. It will be reused by module loading.

Signed-off-by: default avatarFam Zheng <famz@redhat.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 46eef33b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -360,7 +360,7 @@ char *qemu_find_file(int type, const char *name);

/* OS specific functions */
void os_setup_early_signal_handling(void);
char *os_find_datadir(const char *argv0);
char *os_find_datadir(void);
void os_parse_cmd_args(int index, const char *optarg);
void os_pidfile_error(void);

+9 −0
Original line number Diff line number Diff line
@@ -215,6 +215,15 @@ bool fips_get_state(void);
 */
char *qemu_get_local_state_pathname(const char *relative_pathname);

/* 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.
 * Caller needs to release the returned string by g_free() */
char *qemu_get_exec_dir(void);

/**
 * qemu_getauxval:
 * @type: the auxiliary vector key to lookup
+7 −35
Original line number Diff line number Diff line
@@ -84,46 +84,17 @@ void os_setup_signal_handling(void)
   running from the build tree this will be "$bindir/../pc-bios".  */
#define SHARE_SUFFIX "/share/qemu"
#define BUILD_SUFFIX "/pc-bios"
char *os_find_datadir(const char *argv0)
char *os_find_datadir(void)
{
    char *dir;
    char *p = NULL;
    char *dir, *exec_dir;
    char *res;
    char buf[PATH_MAX];
    size_t max_len;

#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__)
    {
        static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
        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;
        }
    }
#endif
    /* If we don't have any way of figuring out the actual executable
       location then try argv[0].  */
    if (!p) {
        p = realpath(argv0, buf);
        if (!p) {
    exec_dir = qemu_get_exec_dir();
    if (exec_dir == NULL) {
        return NULL;
    }
    }
    dir = dirname(p);
    dir = dirname(dir);
    dir = dirname(exec_dir);

    max_len = strlen(dir) +
        MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
@@ -137,6 +108,7 @@ char *os_find_datadir(const char *argv0)
        }
    }

    g_free(exec_dir);
    return res;
}
#undef SHARE_SUFFIX
+2 −19
Original line number Diff line number Diff line
@@ -84,26 +84,9 @@ void os_setup_early_signal_handling(void)
}

/* Look for support files in the same directory as the executable.  */
char *os_find_datadir(const char *argv0)
char *os_find_datadir(void)
{
    char *p;
    char buf[MAX_PATH];
    DWORD len;

    len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
    if (len == 0) {
        return NULL;
    }

    buf[len] = 0;
    p = buf + len - 1;
    while (p != buf && *p != '\\')
        p--;
    *p = 0;
    if (access(buf, R_OK) == 0) {
        return g_strdup(buf);
    }
    return NULL;
    return qemu_get_exec_dir();
}

void os_set_line_buffering(void)
+1 −0
Original line number Diff line number Diff line
@@ -2719,6 +2719,7 @@ int main(int argc, char **argv)
#endif

    error_set_progname(argv[0]);
    qemu_init_exec_dir(argv[0]);

    qemu_init_main_loop();
    bdrv_init();
Loading