Skip to content

Commit ea1edcd

Browse files
committed
vl: relocate paths to data directories
As an additional advantage, the logic is now unified between POSIX and Win32 systems. Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 63c4db4 commit ea1edcd

File tree

6 files changed

+32
-47
lines changed

6 files changed

+32
-47
lines changed

include/qemu-common.h

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ char *qemu_find_file(int type, const char *name);
131131

132132
/* OS specific functions */
133133
void os_setup_early_signal_handling(void);
134-
char *os_find_datadir(void);
135134
int os_parse_cmd_args(int index, const char *optarg);
136135

137136
/*

include/sysemu/sysemu.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern const char *qemu_name;
1414
extern QemuUUID qemu_uuid;
1515
extern bool qemu_uuid_set;
1616

17-
void qemu_add_data_dir(const char *path);
17+
void qemu_add_data_dir(char *path);
1818

1919
void qemu_add_exit_notifier(Notifier *notify);
2020
void qemu_remove_exit_notifier(Notifier *notify);

os-posix.c

-20
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,6 @@ void os_setup_signal_handling(void)
8080
sigaction(SIGTERM, &act, NULL);
8181
}
8282

83-
/*
84-
* Find a likely location for support files using the location of the binary.
85-
* When running from the build tree this will be "$bindir/pc-bios".
86-
* Otherwise, this is CONFIG_QEMU_DATADIR.
87-
*
88-
* The caller must use g_free() to free the returned data when it is
89-
* no longer required.
90-
*/
91-
char *os_find_datadir(void)
92-
{
93-
g_autofree char *dir = NULL;
94-
95-
dir = g_build_filename(qemu_get_exec_dir(), "pc-bios", NULL);
96-
if (g_file_test(dir, G_FILE_TEST_IS_DIR)) {
97-
return g_steal_pointer(&dir);
98-
}
99-
100-
return g_strdup(CONFIG_QEMU_DATADIR);
101-
}
102-
10383
void os_set_proc_name(const char *s)
10484
{
10585
#if defined(PR_SET_NAME)

os-win32.c

-11
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,6 @@ void os_setup_early_signal_handling(void)
5757
atexit(os_undo_timer_resolution);
5858
}
5959

60-
/*
61-
* Look for support files in the same directory as the executable.
62-
*
63-
* The caller must use g_free() to free the returned data when it is
64-
* no longer required.
65-
*/
66-
char *os_find_datadir(void)
67-
{
68-
return g_strdup(qemu_get_exec_dir());
69-
}
70-
7160
void os_set_line_buffering(void)
7261
{
7362
setbuf(stdout, NULL);

softmmu/vl.c

+28-12
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,7 @@ char *qemu_find_file(int type, const char *name)
20052005
return NULL;
20062006
}
20072007

2008-
void qemu_add_data_dir(const char *path)
2008+
void qemu_add_data_dir(char *path)
20092009
{
20102010
int i;
20112011

@@ -2017,10 +2017,11 @@ void qemu_add_data_dir(const char *path)
20172017
}
20182018
for (i = 0; i < data_dir_idx; i++) {
20192019
if (strcmp(data_dir[i], path) == 0) {
2020-
return; /* duplicate */
2020+
g_free(path); /* duplicate */
2021+
return;
20212022
}
20222023
}
2023-
data_dir[data_dir_idx++] = g_strdup(path);
2024+
data_dir[data_dir_idx++] = path;
20242025
}
20252026

20262027
static inline bool nonempty_str(const char *str)
@@ -2829,6 +2830,26 @@ static void create_default_memdev(MachineState *ms, const char *path)
28292830
&error_fatal);
28302831
}
28312832

2833+
/*
2834+
* Find a likely location for support files using the location of the binary.
2835+
* When running from the build tree this will be "$bindir/pc-bios".
2836+
* Otherwise, this is CONFIG_QEMU_DATADIR (possibly relocated).
2837+
*
2838+
* The caller must use g_free() to free the returned data when it is
2839+
* no longer required.
2840+
*/
2841+
static char *find_datadir(void)
2842+
{
2843+
g_autofree char *dir = NULL;
2844+
2845+
dir = g_build_filename(qemu_get_exec_dir(), "pc-bios", NULL);
2846+
if (g_file_test(dir, G_FILE_TEST_IS_DIR)) {
2847+
return g_steal_pointer(&dir);
2848+
}
2849+
2850+
return get_relocated_path(CONFIG_QEMU_DATADIR);
2851+
}
2852+
28322853
void qemu_init(int argc, char **argv, char **envp)
28332854
{
28342855
int i;
@@ -2862,7 +2883,7 @@ void qemu_init(int argc, char **argv, char **envp)
28622883
Error *main_loop_err = NULL;
28632884
Error *err = NULL;
28642885
bool list_data_dirs = false;
2865-
char *dir, **dirs;
2886+
char **dirs;
28662887
const char *mem_path = NULL;
28672888
bool have_custom_ram_size;
28682889
BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
@@ -3195,7 +3216,7 @@ void qemu_init(int argc, char **argv, char **envp)
31953216
if (is_help_option(optarg)) {
31963217
list_data_dirs = true;
31973218
} else {
3198-
qemu_add_data_dir(optarg);
3219+
qemu_add_data_dir(g_strdup(optarg));
31993220
}
32003221
break;
32013222
case QEMU_OPTION_bios:
@@ -3927,17 +3948,12 @@ void qemu_init(int argc, char **argv, char **envp)
39273948
/* add configured firmware directories */
39283949
dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
39293950
for (i = 0; dirs[i] != NULL; i++) {
3930-
qemu_add_data_dir(dirs[i]);
3951+
qemu_add_data_dir(get_relocated_path(dirs[i]));
39313952
}
39323953
g_strfreev(dirs);
39333954

39343955
/* try to find datadir relative to the executable path */
3935-
dir = os_find_datadir();
3936-
qemu_add_data_dir(dir);
3937-
g_free(dir);
3938-
3939-
/* add the datadir specified when building */
3940-
qemu_add_data_dir(CONFIG_QEMU_DATADIR);
3956+
qemu_add_data_dir(find_datadir());
39413957

39423958
/* -L help lists the data directories and exits. */
39433959
if (list_data_dirs) {

tests/qtest/fuzz/fuzz.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,9 @@ int LLVMFuzzerInitialize(int *argc, char ***argv, char ***envp)
170170
datadir = g_build_filename(bindir, "pc-bios", NULL);
171171
if (g_file_test(datadir, G_FILE_TEST_IS_DIR)) {
172172
qemu_add_data_dir(datadir);
173-
}
174-
g_free(datadir);
173+
} else {
174+
g_free(datadir);
175+
}
175176
} else if (*argc > 1) { /* The target is specified as an argument */
176177
target_name = (*argv)[1];
177178
if (!strstr(target_name, "--fuzz-target=")) {

0 commit comments

Comments
 (0)