Skip to content

Commit 06680b1

Browse files
committed
include: move qemu_*_exec_dir() to cutils
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 <[email protected]> Reviewed-by: Markus Armbruster <[email protected]> Message-Id: <[email protected]>
1 parent 2417cbd commit 06680b1

File tree

8 files changed

+129
-128
lines changed

8 files changed

+129
-128
lines changed

include/qemu/cutils.h

+7
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ int uleb128_decode_small(const uint8_t *in, uint32_t *n);
193193
*/
194194
int qemu_pstrcmp0(const char **str1, const char **str2);
195195

196+
/* Find program directory, and save it for later usage with
197+
* qemu_get_exec_dir().
198+
* Try OS specific API first, if not working, parse from argv0. */
199+
void qemu_init_exec_dir(const char *argv0);
200+
201+
/* Get the saved exec dir. */
202+
const char *qemu_get_exec_dir(void);
196203

197204
/**
198205
* get_relocated_path:

include/qemu/osdep.h

-8
Original file line numberDiff line numberDiff line change
@@ -557,14 +557,6 @@ void qemu_set_cloexec(int fd);
557557
*/
558558
char *qemu_get_local_state_dir(void);
559559

560-
/* Find program directory, and save it for later usage with
561-
* qemu_get_exec_dir().
562-
* Try OS specific API first, if not working, parse from argv0. */
563-
void qemu_init_exec_dir(const char *argv0);
564-
565-
/* Get the saved exec dir. */
566-
const char *qemu_get_exec_dir(void);
567-
568560
/**
569561
* qemu_getauxval:
570562
* @type: the auxiliary vector key to lookup

qemu-io.c

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#endif
1717

1818
#include "qemu/help-texts.h"
19+
#include "qemu/cutils.h"
1920
#include "qapi/error.h"
2021
#include "qemu-io.h"
2122
#include "qemu/error-report.h"

storage-daemon/qemu-storage-daemon.c

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
#include "qemu/help-texts.h"
4646
#include "qemu-version.h"
47+
#include "qemu/cutils.h"
4748
#include "qemu/config-file.h"
4849
#include "qemu/error-report.h"
4950
#include "qemu/help_option.h"

tests/qtest/fuzz/fuzz.c

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <wordexp.h>
1717

18+
#include "qemu/cutils.h"
1819
#include "qemu/datadir.h"
1920
#include "sysemu/sysemu.h"
2021
#include "sysemu/qtest.h"

util/cutils.c

+117
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
#include "qemu/host-utils.h"
2727
#include <math.h>
2828

29+
#ifdef __FreeBSD__
30+
#include <sys/sysctl.h>
31+
#include <sys/user.h>
32+
#endif
33+
34+
#ifdef __NetBSD__
35+
#include <sys/sysctl.h>
36+
#endif
37+
2938
#include "qemu/ctype.h"
3039
#include "qemu/cutils.h"
3140
#include "qemu/error-report.h"
@@ -931,6 +940,114 @@ static inline const char *next_component(const char *dir, int *p_len)
931940
return dir;
932941
}
933942

943+
static const char *exec_dir;
944+
945+
void qemu_init_exec_dir(const char *argv0)
946+
{
947+
#ifdef G_OS_WIN32
948+
char *p;
949+
char buf[MAX_PATH];
950+
DWORD len;
951+
952+
if (exec_dir) {
953+
return;
954+
}
955+
956+
len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
957+
if (len == 0) {
958+
return;
959+
}
960+
961+
buf[len] = 0;
962+
p = buf + len - 1;
963+
while (p != buf && *p != '\\') {
964+
p--;
965+
}
966+
*p = 0;
967+
if (access(buf, R_OK) == 0) {
968+
exec_dir = g_strdup(buf);
969+
} else {
970+
exec_dir = CONFIG_BINDIR;
971+
}
972+
#else
973+
char *p = NULL;
974+
char buf[PATH_MAX];
975+
976+
if (exec_dir) {
977+
return;
978+
}
979+
980+
#if defined(__linux__)
981+
{
982+
int len;
983+
len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
984+
if (len > 0) {
985+
buf[len] = 0;
986+
p = buf;
987+
}
988+
}
989+
#elif defined(__FreeBSD__) \
990+
|| (defined(__NetBSD__) && defined(KERN_PROC_PATHNAME))
991+
{
992+
#if defined(__FreeBSD__)
993+
static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
994+
#else
995+
static int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
996+
#endif
997+
size_t len = sizeof(buf) - 1;
998+
999+
*buf = '\0';
1000+
if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) &&
1001+
*buf) {
1002+
buf[sizeof(buf) - 1] = '\0';
1003+
p = buf;
1004+
}
1005+
}
1006+
#elif defined(__APPLE__)
1007+
{
1008+
char fpath[PATH_MAX];
1009+
uint32_t len = sizeof(fpath);
1010+
if (_NSGetExecutablePath(fpath, &len) == 0) {
1011+
p = realpath(fpath, buf);
1012+
if (!p) {
1013+
return;
1014+
}
1015+
}
1016+
}
1017+
#elif defined(__HAIKU__)
1018+
{
1019+
image_info ii;
1020+
int32_t c = 0;
1021+
1022+
*buf = '\0';
1023+
while (get_next_image_info(0, &c, &ii) == B_OK) {
1024+
if (ii.type == B_APP_IMAGE) {
1025+
strncpy(buf, ii.name, sizeof(buf));
1026+
buf[sizeof(buf) - 1] = 0;
1027+
p = buf;
1028+
break;
1029+
}
1030+
}
1031+
}
1032+
#endif
1033+
/* If we don't have any way of figuring out the actual executable
1034+
location then try argv[0]. */
1035+
if (!p && argv0) {
1036+
p = realpath(argv0, buf);
1037+
}
1038+
if (p) {
1039+
exec_dir = g_path_get_dirname(p);
1040+
} else {
1041+
exec_dir = CONFIG_BINDIR;
1042+
}
1043+
#endif
1044+
}
1045+
1046+
const char *qemu_get_exec_dir(void)
1047+
{
1048+
return exec_dir;
1049+
}
1050+
9341051
char *get_relocated_path(const char *dir)
9351052
{
9361053
size_t prefix_len = strlen(CONFIG_PREFIX);

util/oslib-posix.c

+2-84
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,13 @@
4848
#endif
4949

5050
#ifdef __FreeBSD__
51-
#include <sys/sysctl.h>
52-
#include <sys/user.h>
5351
#include <sys/thr.h>
52+
#include <sys/types.h>
53+
#include <sys/user.h>
5454
#include <libutil.h>
5555
#endif
5656

5757
#ifdef __NetBSD__
58-
#include <sys/sysctl.h>
5958
#include <lwp.h>
6059
#endif
6160

@@ -283,87 +282,6 @@ void qemu_set_tty_echo(int fd, bool echo)
283282
tcsetattr(fd, TCSANOW, &tty);
284283
}
285284

286-
static const char *exec_dir;
287-
288-
void qemu_init_exec_dir(const char *argv0)
289-
{
290-
char *p = NULL;
291-
char buf[PATH_MAX];
292-
293-
if (exec_dir) {
294-
return;
295-
}
296-
297-
#if defined(__linux__)
298-
{
299-
int len;
300-
len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
301-
if (len > 0) {
302-
buf[len] = 0;
303-
p = buf;
304-
}
305-
}
306-
#elif defined(__FreeBSD__) \
307-
|| (defined(__NetBSD__) && defined(KERN_PROC_PATHNAME))
308-
{
309-
#if defined(__FreeBSD__)
310-
static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
311-
#else
312-
static int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
313-
#endif
314-
size_t len = sizeof(buf) - 1;
315-
316-
*buf = '\0';
317-
if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) &&
318-
*buf) {
319-
buf[sizeof(buf) - 1] = '\0';
320-
p = buf;
321-
}
322-
}
323-
#elif defined(__APPLE__)
324-
{
325-
char fpath[PATH_MAX];
326-
uint32_t len = sizeof(fpath);
327-
if (_NSGetExecutablePath(fpath, &len) == 0) {
328-
p = realpath(fpath, buf);
329-
if (!p) {
330-
return;
331-
}
332-
}
333-
}
334-
#elif defined(__HAIKU__)
335-
{
336-
image_info ii;
337-
int32_t c = 0;
338-
339-
*buf = '\0';
340-
while (get_next_image_info(0, &c, &ii) == B_OK) {
341-
if (ii.type == B_APP_IMAGE) {
342-
strncpy(buf, ii.name, sizeof(buf));
343-
buf[sizeof(buf) - 1] = 0;
344-
p = buf;
345-
break;
346-
}
347-
}
348-
}
349-
#endif
350-
/* If we don't have any way of figuring out the actual executable
351-
location then try argv[0]. */
352-
if (!p && argv0) {
353-
p = realpath(argv0, buf);
354-
}
355-
if (p) {
356-
exec_dir = g_path_get_dirname(p);
357-
} else {
358-
exec_dir = CONFIG_BINDIR;
359-
}
360-
}
361-
362-
const char *qemu_get_exec_dir(void)
363-
{
364-
return exec_dir;
365-
}
366-
367285
#ifdef CONFIG_LINUX
368286
static void sigbus_handler(int signal, siginfo_t *siginfo, void *ctx)
369287
#else /* CONFIG_LINUX */

util/oslib-win32.c

-36
Original file line numberDiff line numberDiff line change
@@ -269,42 +269,6 @@ void qemu_set_tty_echo(int fd, bool echo)
269269
}
270270
}
271271

272-
static const char *exec_dir;
273-
274-
void qemu_init_exec_dir(const char *argv0)
275-
{
276-
277-
char *p;
278-
char buf[MAX_PATH];
279-
DWORD len;
280-
281-
if (exec_dir) {
282-
return;
283-
}
284-
285-
len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
286-
if (len == 0) {
287-
return;
288-
}
289-
290-
buf[len] = 0;
291-
p = buf + len - 1;
292-
while (p != buf && *p != '\\') {
293-
p--;
294-
}
295-
*p = 0;
296-
if (access(buf, R_OK) == 0) {
297-
exec_dir = g_strdup(buf);
298-
} else {
299-
exec_dir = CONFIG_BINDIR;
300-
}
301-
}
302-
303-
const char *qemu_get_exec_dir(void)
304-
{
305-
return exec_dir;
306-
}
307-
308272
int getpagesize(void)
309273
{
310274
SYSTEM_INFO system_info;

0 commit comments

Comments
 (0)