]>
Commit | Line | Data |
---|---|---|
48ff7a62 MR |
1 | /* |
2 | * QEMU Guest Agent | |
3 | * | |
4 | * Copyright IBM Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Adam Litke <aglitke@linux.vnet.ibm.com> | |
8 | * Michael Roth <mdroth@linux.vnet.ibm.com> | |
9 | * | |
10 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
11 | * See the COPYING file in the top-level directory. | |
12 | */ | |
e688df6b | 13 | |
4459bf38 | 14 | #include "qemu/osdep.h" |
48ff7a62 | 15 | #include <getopt.h> |
39097daf | 16 | #include <glib/gstdio.h> |
d8ca685a | 17 | #ifndef _WIN32 |
48ff7a62 | 18 | #include <syslog.h> |
11d0f125 | 19 | #include <sys/wait.h> |
d8ca685a | 20 | #endif |
49f95221 | 21 | #include "qemu/help-texts.h" |
86cdf9ec | 22 | #include "qapi/qmp/json-parser.h" |
452fcdbc | 23 | #include "qapi/qmp/qdict.h" |
7b1b5d19 | 24 | #include "qapi/qmp/qjson.h" |
dc03272d | 25 | #include "guest-agent-core.h" |
00ca24ff | 26 | #include "qga-qapi-init-commands.h" |
7b1b5d19 | 27 | #include "qapi/qmp/qerror.h" |
e688df6b | 28 | #include "qapi/error.h" |
dc03272d | 29 | #include "channel.h" |
a9eacf8b | 30 | #include "qemu/cutils.h" |
f348b6d1 | 31 | #include "qemu/help_option.h" |
26de2296 | 32 | #include "qemu/sockets.h" |
53fabd4b | 33 | #include "qemu/systemd.h" |
8f1c29af | 34 | #include "qemu-version.h" |
bc62fa03 | 35 | #ifdef _WIN32 |
b70d6afe | 36 | #include <dbt.h> |
bc62fa03 | 37 | #include "qga/service-win32.h" |
f311f2c2 | 38 | #include "qga/vss-win32.h" |
bc62fa03 | 39 | #endif |
ec0f694c TS |
40 | #ifdef __linux__ |
41 | #include <linux/fs.h> | |
42 | #ifdef FIFREEZE | |
43 | #define CONFIG_FSFREEZE | |
44 | #endif | |
45 | #endif | |
48ff7a62 | 46 | |
7868e26e | 47 | #ifndef _WIN32 |
48ff7a62 | 48 | #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0" |
c394ecb7 | 49 | #define QGA_STATE_RELATIVE_DIR "run" |
a749f42d | 50 | #define QGA_SERIAL_PATH_DEFAULT "/dev/ttyS0" |
7868e26e MR |
51 | #else |
52 | #define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0" | |
c394ecb7 | 53 | #define QGA_STATE_RELATIVE_DIR "qemu-ga" |
a749f42d | 54 | #define QGA_SERIAL_PATH_DEFAULT "COM1" |
7868e26e | 55 | #endif |
ec0f694c TS |
56 | #ifdef CONFIG_FSFREEZE |
57 | #define QGA_FSFREEZE_HOOK_DEFAULT CONFIG_QEMU_CONFDIR "/fsfreeze-hook" | |
58 | #endif | |
3cf0bed8 | 59 | #define QGA_SENTINEL_BYTE 0xFF |
e236d060 | 60 | #define QGA_CONF_DEFAULT CONFIG_QEMU_CONFDIR G_DIR_SEPARATOR_S "qemu-ga.conf" |
d951fada | 61 | #define QGA_RETRY_INTERVAL 5 |
48ff7a62 | 62 | |
c394ecb7 LE |
63 | static struct { |
64 | const char *state_dir; | |
65 | const char *pidfile; | |
66 | } dfl_pathnames; | |
67 | ||
39097daf MR |
68 | typedef struct GAPersistentState { |
69 | #define QGA_PSTATE_DEFAULT_FD_COUNTER 1000 | |
70 | int64_t fd_counter; | |
71 | } GAPersistentState; | |
72 | ||
0f4d3a49 MR |
73 | typedef struct GAConfig GAConfig; |
74 | ||
48ff7a62 MR |
75 | struct GAState { |
76 | JSONMessageParser parser; | |
77 | GMainLoop *main_loop; | |
125b310e | 78 | GAChannel *channel; |
48ff7a62 MR |
79 | bool virtio; /* fastpath to check for virtio to deal with poll() quirks */ |
80 | GACommandState *command_state; | |
81 | GLogLevelFlags log_level; | |
82 | FILE *log_file; | |
83 | bool logging_enabled; | |
bc62fa03 MR |
84 | #ifdef _WIN32 |
85 | GAService service; | |
b70d6afe | 86 | HANDLE wakeup_event; |
bc62fa03 | 87 | #endif |
3cf0bed8 | 88 | bool delimit_response; |
f22d85e9 MR |
89 | bool frozen; |
90 | GList *blacklist; | |
d4c8a5d4 | 91 | char *state_filepath_isfrozen; |
f789aa7b MR |
92 | struct { |
93 | const char *log_filepath; | |
94 | const char *pid_filepath; | |
95 | } deferred_options; | |
ec0f694c TS |
96 | #ifdef CONFIG_FSFREEZE |
97 | const char *fsfreeze_hook; | |
98 | #endif | |
d4c8a5d4 | 99 | gchar *pstate_filepath; |
39097daf | 100 | GAPersistentState pstate; |
0f4d3a49 MR |
101 | GAConfig *config; |
102 | int socket_activation; | |
d951fada | 103 | bool force_exit; |
48ff7a62 MR |
104 | }; |
105 | ||
3cf0bed8 | 106 | struct GAState *ga_state; |
1527badb | 107 | QmpCommandList ga_commands; |
48ff7a62 | 108 | |
f22d85e9 MR |
109 | /* commands that are safe to issue while filesystems are frozen */ |
110 | static const char *ga_freeze_whitelist[] = { | |
111 | "guest-ping", | |
112 | "guest-info", | |
113 | "guest-sync", | |
c5dcb6ae | 114 | "guest-sync-delimited", |
f22d85e9 MR |
115 | "guest-fsfreeze-status", |
116 | "guest-fsfreeze-thaw", | |
117 | NULL | |
118 | }; | |
119 | ||
bc62fa03 MR |
120 | #ifdef _WIN32 |
121 | DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data, | |
122 | LPVOID ctx); | |
b70d6afe | 123 | DWORD WINAPI handle_serial_device_events(DWORD type, LPVOID data); |
bc62fa03 MR |
124 | VOID WINAPI service_main(DWORD argc, TCHAR *argv[]); |
125 | #endif | |
d88495a8 | 126 | static int run_agent(GAState *s); |
d951fada | 127 | static void stop_agent(GAState *s, bool requested); |
bc62fa03 | 128 | |
c394ecb7 LE |
129 | static void |
130 | init_dfl_pathnames(void) | |
131 | { | |
1fbf2665 MAL |
132 | g_autofree char *state = qemu_get_local_state_dir(); |
133 | ||
c394ecb7 LE |
134 | g_assert(dfl_pathnames.state_dir == NULL); |
135 | g_assert(dfl_pathnames.pidfile == NULL); | |
1fbf2665 MAL |
136 | dfl_pathnames.state_dir = g_build_filename(state, QGA_STATE_RELATIVE_DIR, NULL); |
137 | dfl_pathnames.pidfile = g_build_filename(state, QGA_STATE_RELATIVE_DIR, "qemu-ga.pid", NULL); | |
c394ecb7 LE |
138 | } |
139 | ||
48ff7a62 MR |
140 | static void quit_handler(int sig) |
141 | { | |
f22d85e9 MR |
142 | /* if we're frozen, don't exit unless we're absolutely forced to, |
143 | * because it's basically impossible for graceful exit to complete | |
144 | * unless all log/pid files are on unfreezable filesystems. there's | |
145 | * also a very likely chance killing the agent before unfreezing | |
146 | * the filesystems is a mistake (or will be viewed as one later). | |
94d81ae8 SJ |
147 | * On Windows the freeze interval is limited to 10 seconds, so |
148 | * we should quit, but first we should wait for the timeout, thaw | |
149 | * the filesystem and quit. | |
f22d85e9 MR |
150 | */ |
151 | if (ga_is_frozen(ga_state)) { | |
94d81ae8 SJ |
152 | #ifdef _WIN32 |
153 | int i = 0; | |
154 | Error *err = NULL; | |
155 | HANDLE hEventTimeout; | |
156 | ||
157 | g_debug("Thawing filesystems before exiting"); | |
158 | ||
159 | hEventTimeout = OpenEvent(EVENT_ALL_ACCESS, FALSE, EVENT_NAME_TIMEOUT); | |
160 | if (hEventTimeout) { | |
161 | WaitForSingleObject(hEventTimeout, 0); | |
162 | CloseHandle(hEventTimeout); | |
163 | } | |
0692b03e | 164 | qga_vss_fsfreeze(&i, false, NULL, &err); |
94d81ae8 SJ |
165 | if (err) { |
166 | g_debug("Error unfreezing filesystems prior to exiting: %s", | |
167 | error_get_pretty(err)); | |
168 | error_free(err); | |
169 | } | |
170 | #else | |
f22d85e9 | 171 | return; |
94d81ae8 | 172 | #endif |
f22d85e9 | 173 | } |
2542bfd5 | 174 | g_debug("received signal num %d, quitting", sig); |
48ff7a62 | 175 | |
d951fada | 176 | stop_agent(ga_state, true); |
48ff7a62 MR |
177 | } |
178 | ||
bc62fa03 | 179 | #ifndef _WIN32 |
125b310e | 180 | static gboolean register_signal_handlers(void) |
48ff7a62 | 181 | { |
dc8764f0 | 182 | struct sigaction sigact; |
48ff7a62 MR |
183 | int ret; |
184 | ||
185 | memset(&sigact, 0, sizeof(struct sigaction)); | |
186 | sigact.sa_handler = quit_handler; | |
187 | ||
188 | ret = sigaction(SIGINT, &sigact, NULL); | |
189 | if (ret == -1) { | |
190 | g_error("error configuring signal handler: %s", strerror(errno)); | |
48ff7a62 MR |
191 | } |
192 | ret = sigaction(SIGTERM, &sigact, NULL); | |
193 | if (ret == -1) { | |
194 | g_error("error configuring signal handler: %s", strerror(errno)); | |
195 | } | |
11d0f125 | 196 | |
4005b473 DL |
197 | sigact.sa_handler = SIG_IGN; |
198 | if (sigaction(SIGPIPE, &sigact, NULL) != 0) { | |
199 | g_error("error configuring SIGPIPE signal handler: %s", | |
200 | strerror(errno)); | |
201 | } | |
202 | ||
125b310e | 203 | return true; |
48ff7a62 | 204 | } |
04b4e75f LC |
205 | |
206 | /* TODO: use this in place of all post-fork() fclose(std*) callers */ | |
207 | void reopen_fd_to_null(int fd) | |
208 | { | |
209 | int nullfd; | |
210 | ||
211 | nullfd = open("/dev/null", O_RDWR); | |
212 | if (nullfd < 0) { | |
213 | return; | |
214 | } | |
215 | ||
216 | dup2(nullfd, fd); | |
217 | ||
218 | if (nullfd != fd) { | |
219 | close(nullfd); | |
220 | } | |
221 | } | |
d8ca685a | 222 | #endif |
48ff7a62 MR |
223 | |
224 | static void usage(const char *cmd) | |
225 | { | |
a2482794 AO |
226 | #ifdef CONFIG_FSFREEZE |
227 | g_autofree char *fsfreeze_hook = get_relocated_path(QGA_FSFREEZE_HOOK_DEFAULT); | |
228 | #endif | |
229 | ||
48ff7a62 | 230 | printf( |
4bdd0416 | 231 | "Usage: %s [-m <method> -p <path>] [<options>]\n" |
7e563bfb | 232 | "QEMU Guest Agent " QEMU_FULL_VERSION "\n" |
8f1c29af | 233 | QEMU_COPYRIGHT "\n" |
48ff7a62 | 234 | "\n" |
586ef5de SH |
235 | " -m, --method transport method: one of unix-listen, virtio-serial,\n" |
236 | " isa-serial, or vsock-listen (virtio-serial is the default)\n" | |
4bdd0416 | 237 | " -p, --path device/socket path (the default for virtio-serial is:\n" |
a749f42d MM |
238 | " %s,\n" |
239 | " the default for isa-serial is:\n" | |
7b46aadb SH |
240 | " %s).\n" |
241 | " Socket addresses for vsock-listen are written as\n" | |
242 | " <cid>:<port>.\n" | |
48ff7a62 MR |
243 | " -l, --logfile set logfile path, logs to stderr by default\n" |
244 | " -f, --pidfile specify pidfile (default is %s)\n" | |
ec0f694c TS |
245 | #ifdef CONFIG_FSFREEZE |
246 | " -F, --fsfreeze-hook\n" | |
247 | " enable fsfreeze hook. Accepts an optional argument that\n" | |
248 | " specifies script to run on freeze/thaw. Script will be\n" | |
249 | " called with 'freeze'/'thaw' arguments accordingly.\n" | |
250 | " (default is %s)\n" | |
251 | " If using -F with an argument, do not follow -F with a\n" | |
252 | " space.\n" | |
253 | " (for example: -F/var/run/fsfreezehook.sh)\n" | |
254 | #endif | |
f789aa7b MR |
255 | " -t, --statedir specify dir to store state information (absolute paths\n" |
256 | " only, default is %s)\n" | |
48ff7a62 MR |
257 | " -v, --verbose log extra debugging information\n" |
258 | " -V, --version print version information and exit\n" | |
259 | " -d, --daemonize become a daemon\n" | |
bc62fa03 | 260 | #ifdef _WIN32 |
5e031072 | 261 | " -s, --service service commands: install, uninstall, vss-install, vss-uninstall\n" |
d8ca685a | 262 | #endif |
582a098e TH |
263 | " -b, --block-rpcs comma-separated list of RPCs to disable (no spaces,\n" |
264 | " use \"help\" to list available RPCs)\n" | |
aeadcbb6 MAL |
265 | " -D, --dump-conf dump a qemu-ga config file based on current config\n" |
266 | " options / command-line parameters to stdout\n" | |
d951fada MR |
267 | " -r, --retry-path attempt re-opening path if it's unavailable or closed\n" |
268 | " due to an error which may be recoverable in the future\n" | |
269 | " (virtio-serial driver re-install, serial device hot\n" | |
270 | " plug/unplug, etc.)\n" | |
48ff7a62 MR |
271 | " -h, --help display this help and exit\n" |
272 | "\n" | |
f5048cb7 | 273 | QEMU_HELP_BOTTOM "\n" |
8f1c29af | 274 | , cmd, QGA_VIRTIO_PATH_DEFAULT, QGA_SERIAL_PATH_DEFAULT, |
a749f42d | 275 | dfl_pathnames.pidfile, |
ec0f694c | 276 | #ifdef CONFIG_FSFREEZE |
a2482794 | 277 | fsfreeze_hook, |
ec0f694c | 278 | #endif |
c394ecb7 | 279 | dfl_pathnames.state_dir); |
48ff7a62 MR |
280 | } |
281 | ||
48ff7a62 MR |
282 | static const char *ga_log_level_str(GLogLevelFlags level) |
283 | { | |
284 | switch (level & G_LOG_LEVEL_MASK) { | |
86dc17d4 AC |
285 | case G_LOG_LEVEL_ERROR: |
286 | return "error"; | |
287 | case G_LOG_LEVEL_CRITICAL: | |
288 | return "critical"; | |
289 | case G_LOG_LEVEL_WARNING: | |
290 | return "warning"; | |
291 | case G_LOG_LEVEL_MESSAGE: | |
292 | return "message"; | |
293 | case G_LOG_LEVEL_INFO: | |
294 | return "info"; | |
295 | case G_LOG_LEVEL_DEBUG: | |
296 | return "debug"; | |
297 | default: | |
298 | return "user"; | |
48ff7a62 MR |
299 | } |
300 | } | |
301 | ||
302 | bool ga_logging_enabled(GAState *s) | |
303 | { | |
304 | return s->logging_enabled; | |
305 | } | |
306 | ||
307 | void ga_disable_logging(GAState *s) | |
308 | { | |
309 | s->logging_enabled = false; | |
310 | } | |
311 | ||
312 | void ga_enable_logging(GAState *s) | |
313 | { | |
314 | s->logging_enabled = true; | |
315 | } | |
316 | ||
317 | static void ga_log(const gchar *domain, GLogLevelFlags level, | |
318 | const gchar *msg, gpointer opaque) | |
319 | { | |
320 | GAState *s = opaque; | |
48ff7a62 MR |
321 | const char *level_str = ga_log_level_str(level); |
322 | ||
323 | if (!ga_logging_enabled(s)) { | |
324 | return; | |
325 | } | |
326 | ||
327 | level &= G_LOG_LEVEL_MASK; | |
d8ca685a | 328 | #ifndef _WIN32 |
f300414c | 329 | if (g_strcmp0(domain, "syslog") == 0) { |
48ff7a62 MR |
330 | syslog(LOG_INFO, "%s: %s", level_str, msg); |
331 | } else if (level & s->log_level) { | |
d8ca685a MR |
332 | #else |
333 | if (level & s->log_level) { | |
334 | #endif | |
55fa0170 MAL |
335 | g_autoptr(GDateTime) now = g_date_time_new_now_utc(); |
336 | g_autofree char *nowstr = g_date_time_format(now, "%s.%f"); | |
337 | fprintf(s->log_file, "%s: %s: %s\n", nowstr, level_str, msg); | |
48ff7a62 MR |
338 | fflush(s->log_file); |
339 | } | |
340 | } | |
341 | ||
3cf0bed8 MR |
342 | void ga_set_response_delimited(GAState *s) |
343 | { | |
344 | s->delimit_response = true; | |
345 | } | |
346 | ||
9e92f6d4 LC |
347 | static FILE *ga_open_logfile(const char *logfile) |
348 | { | |
349 | FILE *f; | |
350 | ||
351 | f = fopen(logfile, "a"); | |
352 | if (!f) { | |
353 | return NULL; | |
354 | } | |
355 | ||
356 | qemu_set_cloexec(fileno(f)); | |
357 | return f; | |
358 | } | |
359 | ||
f22d85e9 MR |
360 | static gint ga_strcmp(gconstpointer str1, gconstpointer str2) |
361 | { | |
362 | return strcmp(str1, str2); | |
363 | } | |
364 | ||
365 | /* disable commands that aren't safe for fsfreeze */ | |
f0ccc00b | 366 | static void ga_disable_non_whitelisted(const QmpCommand *cmd, void *opaque) |
f22d85e9 | 367 | { |
8dc4d915 MW |
368 | bool whitelisted = false; |
369 | int i = 0; | |
370 | const char *name = qmp_command_name(cmd); | |
371 | ||
372 | while (ga_freeze_whitelist[i] != NULL) { | |
373 | if (strcmp(name, ga_freeze_whitelist[i]) == 0) { | |
374 | whitelisted = true; | |
f22d85e9 | 375 | } |
8dc4d915 MW |
376 | i++; |
377 | } | |
378 | if (!whitelisted) { | |
379 | g_debug("disabling command: %s", name); | |
c98939da | 380 | qmp_disable_command(&ga_commands, name, "the agent is in frozen state"); |
f22d85e9 | 381 | } |
f22d85e9 MR |
382 | } |
383 | ||
a31f0531 | 384 | /* [re-]enable all commands, except those explicitly blacklisted by user */ |
f0ccc00b | 385 | static void ga_enable_non_blacklisted(const QmpCommand *cmd, void *opaque) |
f22d85e9 | 386 | { |
8dc4d915 MW |
387 | GList *blacklist = opaque; |
388 | const char *name = qmp_command_name(cmd); | |
389 | ||
390 | if (g_list_find_custom(blacklist, name, ga_strcmp) == NULL && | |
391 | !qmp_command_is_enabled(cmd)) { | |
392 | g_debug("enabling command: %s", name); | |
1527badb | 393 | qmp_enable_command(&ga_commands, name); |
f22d85e9 | 394 | } |
f22d85e9 MR |
395 | } |
396 | ||
f789aa7b MR |
397 | static bool ga_create_file(const char *path) |
398 | { | |
399 | int fd = open(path, O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR); | |
400 | if (fd == -1) { | |
401 | g_warning("unable to open/create file %s: %s", path, strerror(errno)); | |
402 | return false; | |
403 | } | |
404 | close(fd); | |
405 | return true; | |
406 | } | |
407 | ||
408 | static bool ga_delete_file(const char *path) | |
409 | { | |
410 | int ret = unlink(path); | |
411 | if (ret == -1) { | |
412 | g_warning("unable to delete file: %s: %s", path, strerror(errno)); | |
413 | return false; | |
414 | } | |
415 | ||
416 | return true; | |
417 | } | |
418 | ||
f22d85e9 MR |
419 | bool ga_is_frozen(GAState *s) |
420 | { | |
421 | return s->frozen; | |
422 | } | |
423 | ||
424 | void ga_set_frozen(GAState *s) | |
425 | { | |
426 | if (ga_is_frozen(s)) { | |
427 | return; | |
428 | } | |
429 | /* disable all non-whitelisted (for frozen state) commands */ | |
1527badb | 430 | qmp_for_each_command(&ga_commands, ga_disable_non_whitelisted, NULL); |
f22d85e9 MR |
431 | g_warning("disabling logging due to filesystem freeze"); |
432 | ga_disable_logging(s); | |
433 | s->frozen = true; | |
f789aa7b MR |
434 | if (!ga_create_file(s->state_filepath_isfrozen)) { |
435 | g_warning("unable to create %s, fsfreeze may not function properly", | |
436 | s->state_filepath_isfrozen); | |
437 | } | |
f22d85e9 MR |
438 | } |
439 | ||
440 | void ga_unset_frozen(GAState *s) | |
441 | { | |
442 | if (!ga_is_frozen(s)) { | |
443 | return; | |
444 | } | |
445 | ||
f789aa7b MR |
446 | /* if we delayed creation/opening of pid/log files due to being |
447 | * in a frozen state at start up, do it now | |
448 | */ | |
449 | if (s->deferred_options.log_filepath) { | |
9e92f6d4 | 450 | s->log_file = ga_open_logfile(s->deferred_options.log_filepath); |
f789aa7b MR |
451 | if (!s->log_file) { |
452 | s->log_file = stderr; | |
453 | } | |
454 | s->deferred_options.log_filepath = NULL; | |
455 | } | |
f22d85e9 | 456 | ga_enable_logging(s); |
f789aa7b MR |
457 | g_warning("logging re-enabled due to filesystem unfreeze"); |
458 | if (s->deferred_options.pid_filepath) { | |
9e6bdef2 MAL |
459 | Error *err = NULL; |
460 | ||
461 | if (!qemu_write_pidfile(s->deferred_options.pid_filepath, &err)) { | |
462 | g_warning("%s", error_get_pretty(err)); | |
463 | error_free(err); | |
f789aa7b MR |
464 | } |
465 | s->deferred_options.pid_filepath = NULL; | |
466 | } | |
f22d85e9 MR |
467 | |
468 | /* enable all disabled, non-blacklisted commands */ | |
1527badb | 469 | qmp_for_each_command(&ga_commands, ga_enable_non_blacklisted, s->blacklist); |
f22d85e9 | 470 | s->frozen = false; |
f789aa7b MR |
471 | if (!ga_delete_file(s->state_filepath_isfrozen)) { |
472 | g_warning("unable to delete %s, fsfreeze may not function properly", | |
473 | s->state_filepath_isfrozen); | |
474 | } | |
f22d85e9 MR |
475 | } |
476 | ||
ec0f694c TS |
477 | #ifdef CONFIG_FSFREEZE |
478 | const char *ga_fsfreeze_hook(GAState *s) | |
479 | { | |
480 | return s->fsfreeze_hook; | |
481 | } | |
482 | #endif | |
483 | ||
48ff7a62 MR |
484 | static void become_daemon(const char *pidfile) |
485 | { | |
f789aa7b | 486 | #ifndef _WIN32 |
48ff7a62 | 487 | pid_t pid, sid; |
48ff7a62 MR |
488 | |
489 | pid = fork(); | |
490 | if (pid < 0) { | |
491 | exit(EXIT_FAILURE); | |
492 | } | |
493 | if (pid > 0) { | |
494 | exit(EXIT_SUCCESS); | |
495 | } | |
496 | ||
f789aa7b | 497 | if (pidfile) { |
9e6bdef2 MAL |
498 | Error *err = NULL; |
499 | ||
500 | if (!qemu_write_pidfile(pidfile, &err)) { | |
501 | g_critical("%s", error_get_pretty(err)); | |
502 | error_free(err); | |
f789aa7b MR |
503 | exit(EXIT_FAILURE); |
504 | } | |
48ff7a62 MR |
505 | } |
506 | ||
c689b4f1 | 507 | umask(S_IRWXG | S_IRWXO); |
48ff7a62 MR |
508 | sid = setsid(); |
509 | if (sid < 0) { | |
510 | goto fail; | |
511 | } | |
512 | if ((chdir("/")) < 0) { | |
513 | goto fail; | |
514 | } | |
515 | ||
226a4894 LC |
516 | reopen_fd_to_null(STDIN_FILENO); |
517 | reopen_fd_to_null(STDOUT_FILENO); | |
518 | reopen_fd_to_null(STDERR_FILENO); | |
48ff7a62 MR |
519 | return; |
520 | ||
521 | fail: | |
4bdb1a30 SW |
522 | if (pidfile) { |
523 | unlink(pidfile); | |
524 | } | |
48ff7a62 MR |
525 | g_critical("failed to daemonize"); |
526 | exit(EXIT_FAILURE); | |
d8ca685a | 527 | #endif |
f789aa7b | 528 | } |
48ff7a62 | 529 | |
781f2b3d | 530 | static int send_response(GAState *s, const QDict *rsp) |
48ff7a62 | 531 | { |
eab3a467 | 532 | GString *response; |
125b310e | 533 | GIOStatus status; |
48ff7a62 | 534 | |
844bd70b MAL |
535 | g_assert(s->channel); |
536 | ||
537 | if (!rsp) { | |
538 | return 0; | |
539 | } | |
48ff7a62 | 540 | |
eab3a467 MA |
541 | response = qobject_to_json(QOBJECT(rsp)); |
542 | if (!response) { | |
48ff7a62 MR |
543 | return -EINVAL; |
544 | } | |
545 | ||
3cf0bed8 MR |
546 | if (s->delimit_response) { |
547 | s->delimit_response = false; | |
eab3a467 | 548 | g_string_prepend_c(response, QGA_SENTINEL_BYTE); |
3cf0bed8 MR |
549 | } |
550 | ||
eab3a467 MA |
551 | g_string_append_c(response, '\n'); |
552 | status = ga_channel_write_all(s->channel, response->str, response->len); | |
553 | g_string_free(response, true); | |
125b310e MR |
554 | if (status != G_IO_STATUS_NORMAL) { |
555 | return -EIO; | |
48ff7a62 | 556 | } |
125b310e MR |
557 | |
558 | return 0; | |
48ff7a62 MR |
559 | } |
560 | ||
48ff7a62 | 561 | /* handle requests/control events coming in over the channel */ |
62815d85 | 562 | static void process_event(void *opaque, QObject *obj, Error *err) |
48ff7a62 | 563 | { |
62815d85 | 564 | GAState *s = opaque; |
781f2b3d | 565 | QDict *rsp; |
48ff7a62 MR |
566 | int ret; |
567 | ||
48ff7a62 | 568 | g_debug("process_event: called"); |
84a56f38 | 569 | assert(!obj != !err); |
ae7da1e5 | 570 | if (err) { |
781f2b3d MAL |
571 | rsp = qmp_error_response(err); |
572 | goto end; | |
48ff7a62 MR |
573 | } |
574 | ||
781f2b3d | 575 | g_debug("processing command"); |
41725fa7 | 576 | rsp = qmp_dispatch(&ga_commands, obj, false, NULL); |
ae7da1e5 | 577 | |
781f2b3d | 578 | end: |
ae7da1e5 MAL |
579 | ret = send_response(s, rsp); |
580 | if (ret < 0) { | |
581 | g_warning("error sending error response: %s", strerror(-ret)); | |
582 | } | |
583 | qobject_unref(rsp); | |
584 | qobject_unref(obj); | |
48ff7a62 MR |
585 | } |
586 | ||
125b310e MR |
587 | /* false return signals GAChannel to close the current client connection */ |
588 | static gboolean channel_event_cb(GIOCondition condition, gpointer data) | |
48ff7a62 MR |
589 | { |
590 | GAState *s = data; | |
0697e9ed | 591 | gchar buf[QGA_READ_COUNT_DEFAULT + 1]; |
48ff7a62 | 592 | gsize count; |
125b310e | 593 | GIOStatus status = ga_channel_read(s->channel, buf, QGA_READ_COUNT_DEFAULT, &count); |
48ff7a62 MR |
594 | switch (status) { |
595 | case G_IO_STATUS_ERROR: | |
125b310e | 596 | g_warning("error reading channel"); |
d951fada | 597 | stop_agent(s, false); |
48ff7a62 MR |
598 | return false; |
599 | case G_IO_STATUS_NORMAL: | |
125b310e | 600 | buf[count] = 0; |
48ff7a62 MR |
601 | g_debug("read data, count: %d, data: %s", (int)count, buf); |
602 | json_message_parser_feed(&s->parser, (char *)buf, (int)count); | |
125b310e MR |
603 | break; |
604 | case G_IO_STATUS_EOF: | |
605 | g_debug("received EOF"); | |
606 | if (!s->virtio) { | |
607 | return false; | |
608 | } | |
f5b79578 | 609 | /* fall through */ |
48ff7a62 MR |
610 | case G_IO_STATUS_AGAIN: |
611 | /* virtio causes us to spin here when no process is attached to | |
612 | * host-side chardev. sleep a bit to mitigate this | |
613 | */ | |
614 | if (s->virtio) { | |
4e8c4194 | 615 | g_usleep(G_USEC_PER_SEC / 10); |
48ff7a62 MR |
616 | } |
617 | return true; | |
48ff7a62 MR |
618 | default: |
619 | g_warning("unknown channel read status, closing"); | |
48ff7a62 MR |
620 | return false; |
621 | } | |
622 | return true; | |
623 | } | |
624 | ||
26de2296 SH |
625 | static gboolean channel_init(GAState *s, const gchar *method, const gchar *path, |
626 | int listen_fd) | |
48ff7a62 | 627 | { |
125b310e | 628 | GAChannelMethod channel_method; |
48ff7a62 | 629 | |
125b310e MR |
630 | if (strcmp(method, "virtio-serial") == 0) { |
631 | s->virtio = true; /* virtio requires special handling in some cases */ | |
632 | channel_method = GA_CHANNEL_VIRTIO_SERIAL; | |
633 | } else if (strcmp(method, "isa-serial") == 0) { | |
634 | channel_method = GA_CHANNEL_ISA_SERIAL; | |
635 | } else if (strcmp(method, "unix-listen") == 0) { | |
636 | channel_method = GA_CHANNEL_UNIX_LISTEN; | |
586ef5de SH |
637 | } else if (strcmp(method, "vsock-listen") == 0) { |
638 | channel_method = GA_CHANNEL_VSOCK_LISTEN; | |
48ff7a62 | 639 | } else { |
125b310e MR |
640 | g_critical("unsupported channel method/type: %s", method); |
641 | return false; | |
48ff7a62 MR |
642 | } |
643 | ||
26de2296 SH |
644 | s->channel = ga_channel_new(channel_method, path, listen_fd, |
645 | channel_event_cb, s); | |
125b310e MR |
646 | if (!s->channel) { |
647 | g_critical("failed to create guest agent channel"); | |
648 | return false; | |
649 | } | |
650 | ||
651 | return true; | |
48ff7a62 MR |
652 | } |
653 | ||
bc62fa03 | 654 | #ifdef _WIN32 |
b70d6afe BA |
655 | DWORD WINAPI handle_serial_device_events(DWORD type, LPVOID data) |
656 | { | |
657 | DWORD ret = NO_ERROR; | |
658 | PDEV_BROADCAST_HDR broadcast_header = (PDEV_BROADCAST_HDR)data; | |
659 | ||
660 | if (broadcast_header->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) { | |
661 | switch (type) { | |
662 | /* Device inserted */ | |
663 | case DBT_DEVICEARRIVAL: | |
664 | /* Start QEMU-ga's service */ | |
665 | if (!SetEvent(ga_state->wakeup_event)) { | |
666 | ret = GetLastError(); | |
667 | } | |
668 | break; | |
669 | /* Device removed */ | |
670 | case DBT_DEVICEQUERYREMOVE: | |
671 | case DBT_DEVICEREMOVEPENDING: | |
672 | case DBT_DEVICEREMOVECOMPLETE: | |
673 | /* Stop QEMU-ga's service */ | |
674 | if (!ResetEvent(ga_state->wakeup_event)) { | |
675 | ret = GetLastError(); | |
676 | } | |
677 | break; | |
678 | default: | |
679 | ret = ERROR_CALL_NOT_IMPLEMENTED; | |
680 | } | |
681 | } | |
682 | return ret; | |
683 | } | |
684 | ||
bc62fa03 MR |
685 | DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data, |
686 | LPVOID ctx) | |
687 | { | |
688 | DWORD ret = NO_ERROR; | |
689 | GAService *service = &ga_state->service; | |
690 | ||
aaaed199 | 691 | switch (ctrl) { |
86dc17d4 AC |
692 | case SERVICE_CONTROL_STOP: |
693 | case SERVICE_CONTROL_SHUTDOWN: | |
694 | quit_handler(SIGTERM); | |
695 | SetEvent(ga_state->wakeup_event); | |
696 | service->status.dwCurrentState = SERVICE_STOP_PENDING; | |
697 | SetServiceStatus(service->status_handle, &service->status); | |
698 | break; | |
699 | case SERVICE_CONTROL_DEVICEEVENT: | |
700 | handle_serial_device_events(type, data); | |
701 | break; | |
bc62fa03 | 702 | |
86dc17d4 AC |
703 | default: |
704 | ret = ERROR_CALL_NOT_IMPLEMENTED; | |
bc62fa03 MR |
705 | } |
706 | return ret; | |
707 | } | |
708 | ||
709 | VOID WINAPI service_main(DWORD argc, TCHAR *argv[]) | |
710 | { | |
711 | GAService *service = &ga_state->service; | |
712 | ||
713 | service->status_handle = RegisterServiceCtrlHandlerEx(QGA_SERVICE_NAME, | |
714 | service_ctrl_handler, NULL); | |
715 | ||
716 | if (service->status_handle == 0) { | |
717 | g_critical("Failed to register extended requests function!\n"); | |
718 | return; | |
719 | } | |
720 | ||
721 | service->status.dwServiceType = SERVICE_WIN32; | |
722 | service->status.dwCurrentState = SERVICE_RUNNING; | |
723 | service->status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; | |
724 | service->status.dwWin32ExitCode = NO_ERROR; | |
725 | service->status.dwServiceSpecificExitCode = NO_ERROR; | |
726 | service->status.dwCheckPoint = 0; | |
727 | service->status.dwWaitHint = 0; | |
b70d6afe BA |
728 | DEV_BROADCAST_DEVICEINTERFACE notification_filter; |
729 | ZeroMemory(¬ification_filter, sizeof(notification_filter)); | |
730 | notification_filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; | |
731 | notification_filter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE); | |
732 | notification_filter.dbcc_classguid = GUID_VIOSERIAL_PORT; | |
733 | ||
734 | service->device_notification_handle = | |
735 | RegisterDeviceNotification(service->status_handle, | |
736 | ¬ification_filter, DEVICE_NOTIFY_SERVICE_HANDLE); | |
737 | if (!service->device_notification_handle) { | |
738 | g_critical("Failed to register device notification handle!\n"); | |
739 | return; | |
740 | } | |
bc62fa03 MR |
741 | SetServiceStatus(service->status_handle, &service->status); |
742 | ||
d88495a8 | 743 | run_agent(ga_state); |
bc62fa03 | 744 | |
b70d6afe | 745 | UnregisterDeviceNotification(service->device_notification_handle); |
bc62fa03 MR |
746 | service->status.dwCurrentState = SERVICE_STOPPED; |
747 | SetServiceStatus(service->status_handle, &service->status); | |
748 | } | |
749 | #endif | |
750 | ||
39097daf MR |
751 | static void set_persistent_state_defaults(GAPersistentState *pstate) |
752 | { | |
753 | g_assert(pstate); | |
754 | pstate->fd_counter = QGA_PSTATE_DEFAULT_FD_COUNTER; | |
755 | } | |
756 | ||
757 | static void persistent_state_from_keyfile(GAPersistentState *pstate, | |
758 | GKeyFile *keyfile) | |
759 | { | |
760 | g_assert(pstate); | |
761 | g_assert(keyfile); | |
762 | /* if any fields are missing, either because the file was tampered with | |
763 | * by agents of chaos, or because the field wasn't present at the time the | |
764 | * file was created, the best we can ever do is start over with the default | |
765 | * values. so load them now, and ignore any errors in accessing key-value | |
766 | * pairs | |
767 | */ | |
768 | set_persistent_state_defaults(pstate); | |
769 | ||
770 | if (g_key_file_has_key(keyfile, "global", "fd_counter", NULL)) { | |
771 | pstate->fd_counter = | |
4f306496 | 772 | g_key_file_get_integer(keyfile, "global", "fd_counter", NULL); |
39097daf MR |
773 | } |
774 | } | |
775 | ||
776 | static void persistent_state_to_keyfile(const GAPersistentState *pstate, | |
777 | GKeyFile *keyfile) | |
778 | { | |
779 | g_assert(pstate); | |
780 | g_assert(keyfile); | |
781 | ||
4f306496 | 782 | g_key_file_set_integer(keyfile, "global", "fd_counter", pstate->fd_counter); |
39097daf MR |
783 | } |
784 | ||
785 | static gboolean write_persistent_state(const GAPersistentState *pstate, | |
786 | const gchar *path) | |
787 | { | |
788 | GKeyFile *keyfile = g_key_file_new(); | |
789 | GError *gerr = NULL; | |
790 | gboolean ret = true; | |
791 | gchar *data = NULL; | |
792 | gsize data_len; | |
793 | ||
794 | g_assert(pstate); | |
795 | ||
796 | persistent_state_to_keyfile(pstate, keyfile); | |
797 | data = g_key_file_to_data(keyfile, &data_len, &gerr); | |
798 | if (gerr) { | |
799 | g_critical("failed to convert persistent state to string: %s", | |
800 | gerr->message); | |
801 | ret = false; | |
802 | goto out; | |
803 | } | |
804 | ||
805 | g_file_set_contents(path, data, data_len, &gerr); | |
806 | if (gerr) { | |
807 | g_critical("failed to write persistent state to %s: %s", | |
808 | path, gerr->message); | |
809 | ret = false; | |
810 | goto out; | |
811 | } | |
812 | ||
813 | out: | |
814 | if (gerr) { | |
815 | g_error_free(gerr); | |
816 | } | |
817 | if (keyfile) { | |
818 | g_key_file_free(keyfile); | |
819 | } | |
820 | g_free(data); | |
821 | return ret; | |
822 | } | |
823 | ||
824 | static gboolean read_persistent_state(GAPersistentState *pstate, | |
825 | const gchar *path, gboolean frozen) | |
826 | { | |
827 | GKeyFile *keyfile = NULL; | |
828 | GError *gerr = NULL; | |
829 | struct stat st; | |
830 | gboolean ret = true; | |
831 | ||
832 | g_assert(pstate); | |
833 | ||
834 | if (stat(path, &st) == -1) { | |
835 | /* it's okay if state file doesn't exist, but any other error | |
836 | * indicates a permissions issue or some other misconfiguration | |
837 | * that we likely won't be able to recover from. | |
838 | */ | |
839 | if (errno != ENOENT) { | |
840 | g_critical("unable to access state file at path %s: %s", | |
841 | path, strerror(errno)); | |
842 | ret = false; | |
843 | goto out; | |
844 | } | |
845 | ||
846 | /* file doesn't exist. initialize state to default values and | |
847 | * attempt to save now. (we could wait till later when we have | |
848 | * modified state we need to commit, but if there's a problem, | |
849 | * such as a missing parent directory, we want to catch it now) | |
850 | * | |
851 | * there is a potential scenario where someone either managed to | |
852 | * update the agent from a version that didn't use a key store | |
853 | * while qemu-ga thought the filesystem was frozen, or | |
854 | * deleted the key store prior to issuing a fsfreeze, prior | |
855 | * to restarting the agent. in this case we go ahead and defer | |
856 | * initial creation till we actually have modified state to | |
857 | * write, otherwise fail to recover from freeze. | |
858 | */ | |
859 | set_persistent_state_defaults(pstate); | |
860 | if (!frozen) { | |
861 | ret = write_persistent_state(pstate, path); | |
862 | if (!ret) { | |
863 | g_critical("unable to create state file at path %s", path); | |
864 | ret = false; | |
865 | goto out; | |
866 | } | |
867 | } | |
868 | ret = true; | |
869 | goto out; | |
870 | } | |
871 | ||
872 | keyfile = g_key_file_new(); | |
873 | g_key_file_load_from_file(keyfile, path, 0, &gerr); | |
874 | if (gerr) { | |
875 | g_critical("error loading persistent state from path: %s, %s", | |
876 | path, gerr->message); | |
877 | ret = false; | |
878 | goto out; | |
879 | } | |
880 | ||
881 | persistent_state_from_keyfile(pstate, keyfile); | |
882 | ||
883 | out: | |
884 | if (keyfile) { | |
885 | g_key_file_free(keyfile); | |
886 | } | |
887 | if (gerr) { | |
888 | g_error_free(gerr); | |
889 | } | |
890 | ||
891 | return ret; | |
892 | } | |
893 | ||
894 | int64_t ga_get_fd_handle(GAState *s, Error **errp) | |
895 | { | |
896 | int64_t handle; | |
897 | ||
898 | g_assert(s->pstate_filepath); | |
899 | /* we blacklist commands and avoid operations that potentially require | |
900 | * writing to disk when we're in a frozen state. this includes opening | |
901 | * new files, so we should never get here in that situation | |
902 | */ | |
903 | g_assert(!ga_is_frozen(s)); | |
904 | ||
905 | handle = s->pstate.fd_counter++; | |
ce7f7cc2 LC |
906 | |
907 | /* This should never happen on a reasonable timeframe, as guest-file-open | |
908 | * would have to be issued 2^63 times */ | |
909 | if (s->pstate.fd_counter == INT64_MAX) { | |
910 | abort(); | |
39097daf | 911 | } |
ce7f7cc2 | 912 | |
39097daf MR |
913 | if (!write_persistent_state(&s->pstate, s->pstate_filepath)) { |
914 | error_setg(errp, "failed to commit persistent state to disk"); | |
a903f40c | 915 | return -1; |
39097daf MR |
916 | } |
917 | ||
918 | return handle; | |
919 | } | |
920 | ||
f0ccc00b | 921 | static void ga_print_cmd(const QmpCommand *cmd, void *opaque) |
8dc4d915 MW |
922 | { |
923 | printf("%s\n", qmp_command_name(cmd)); | |
924 | } | |
925 | ||
4bca81ce | 926 | static GList *split_list(const gchar *str, const gchar *delim) |
23b42894 MAL |
927 | { |
928 | GList *list = NULL; | |
4bca81ce MAL |
929 | int i; |
930 | gchar **strv; | |
23b42894 | 931 | |
4bca81ce MAL |
932 | strv = g_strsplit(str, delim, -1); |
933 | for (i = 0; strv[i]; i++) { | |
934 | list = g_list_prepend(list, strv[i]); | |
23b42894 | 935 | } |
4bca81ce | 936 | g_free(strv); |
23b42894 MAL |
937 | |
938 | return list; | |
939 | } | |
940 | ||
0f4d3a49 | 941 | struct GAConfig { |
7a406694 MAL |
942 | char *channel_path; |
943 | char *method; | |
944 | char *log_filepath; | |
945 | char *pid_filepath; | |
ec0f694c | 946 | #ifdef CONFIG_FSFREEZE |
7a406694 | 947 | char *fsfreeze_hook; |
ec0f694c | 948 | #endif |
7a406694 | 949 | char *state_dir; |
bc62fa03 | 950 | #ifdef _WIN32 |
7a406694 | 951 | const char *service; |
bc62fa03 | 952 | #endif |
e236d060 | 953 | gchar *bliststr; /* blacklist may point to this string */ |
7a406694 MAL |
954 | GList *blacklist; |
955 | int daemonize; | |
956 | GLogLevelFlags log_level; | |
aeadcbb6 | 957 | int dumpconf; |
d951fada | 958 | bool retry_path; |
0f4d3a49 | 959 | }; |
7a406694 | 960 | |
e236d060 MAL |
961 | static void config_load(GAConfig *config) |
962 | { | |
963 | GError *gerr = NULL; | |
964 | GKeyFile *keyfile; | |
a9eacf8b | 965 | g_autofree char *conf = g_strdup(g_getenv("QGA_CONF")) ?: get_relocated_path(QGA_CONF_DEFAULT); |
582a098e | 966 | const gchar *blockrpcs_key = "block-rpcs"; |
e236d060 MAL |
967 | |
968 | /* read system config */ | |
969 | keyfile = g_key_file_new(); | |
8e34bf36 | 970 | if (!g_key_file_load_from_file(keyfile, conf, 0, &gerr)) { |
e236d060 MAL |
971 | goto end; |
972 | } | |
973 | if (g_key_file_has_key(keyfile, "general", "daemon", NULL)) { | |
974 | config->daemonize = | |
975 | g_key_file_get_boolean(keyfile, "general", "daemon", &gerr); | |
976 | } | |
977 | if (g_key_file_has_key(keyfile, "general", "method", NULL)) { | |
978 | config->method = | |
979 | g_key_file_get_string(keyfile, "general", "method", &gerr); | |
980 | } | |
981 | if (g_key_file_has_key(keyfile, "general", "path", NULL)) { | |
982 | config->channel_path = | |
983 | g_key_file_get_string(keyfile, "general", "path", &gerr); | |
984 | } | |
985 | if (g_key_file_has_key(keyfile, "general", "logfile", NULL)) { | |
986 | config->log_filepath = | |
987 | g_key_file_get_string(keyfile, "general", "logfile", &gerr); | |
988 | } | |
989 | if (g_key_file_has_key(keyfile, "general", "pidfile", NULL)) { | |
990 | config->pid_filepath = | |
991 | g_key_file_get_string(keyfile, "general", "pidfile", &gerr); | |
992 | } | |
993 | #ifdef CONFIG_FSFREEZE | |
994 | if (g_key_file_has_key(keyfile, "general", "fsfreeze-hook", NULL)) { | |
995 | config->fsfreeze_hook = | |
996 | g_key_file_get_string(keyfile, | |
997 | "general", "fsfreeze-hook", &gerr); | |
998 | } | |
999 | #endif | |
1000 | if (g_key_file_has_key(keyfile, "general", "statedir", NULL)) { | |
1001 | config->state_dir = | |
1002 | g_key_file_get_string(keyfile, "general", "statedir", &gerr); | |
1003 | } | |
1004 | if (g_key_file_has_key(keyfile, "general", "verbose", NULL) && | |
1005 | g_key_file_get_boolean(keyfile, "general", "verbose", &gerr)) { | |
1006 | /* enable all log levels */ | |
1007 | config->log_level = G_LOG_LEVEL_MASK; | |
1008 | } | |
d951fada MR |
1009 | if (g_key_file_has_key(keyfile, "general", "retry-path", NULL)) { |
1010 | config->retry_path = | |
1011 | g_key_file_get_boolean(keyfile, "general", "retry-path", &gerr); | |
1012 | } | |
582a098e | 1013 | |
e236d060 | 1014 | if (g_key_file_has_key(keyfile, "general", "blacklist", NULL)) { |
582a098e TH |
1015 | g_warning("config using deprecated 'blacklist' key, should be replaced" |
1016 | " with the 'block-rpcs' key."); | |
1017 | blockrpcs_key = "blacklist"; | |
1018 | } | |
1019 | if (g_key_file_has_key(keyfile, "general", blockrpcs_key, NULL)) { | |
e236d060 | 1020 | config->bliststr = |
582a098e | 1021 | g_key_file_get_string(keyfile, "general", blockrpcs_key, &gerr); |
e236d060 MAL |
1022 | config->blacklist = g_list_concat(config->blacklist, |
1023 | split_list(config->bliststr, ",")); | |
1024 | } | |
1025 | ||
1026 | end: | |
1027 | g_key_file_free(keyfile); | |
1028 | if (gerr && | |
1029 | !(gerr->domain == G_FILE_ERROR && gerr->code == G_FILE_ERROR_NOENT)) { | |
1030 | g_critical("error loading configuration from path: %s, %s", | |
a9eacf8b | 1031 | conf, gerr->message); |
e236d060 MAL |
1032 | exit(EXIT_FAILURE); |
1033 | } | |
1034 | g_clear_error(&gerr); | |
1035 | } | |
1036 | ||
aeadcbb6 MAL |
1037 | static gchar *list_join(GList *list, const gchar separator) |
1038 | { | |
1039 | GString *str = g_string_new(""); | |
1040 | ||
1041 | while (list) { | |
1042 | str = g_string_append(str, (gchar *)list->data); | |
1043 | list = g_list_next(list); | |
1044 | if (list) { | |
1045 | str = g_string_append_c(str, separator); | |
1046 | } | |
1047 | } | |
1048 | ||
1049 | return g_string_free(str, FALSE); | |
1050 | } | |
1051 | ||
1052 | static void config_dump(GAConfig *config) | |
1053 | { | |
1054 | GError *error = NULL; | |
1055 | GKeyFile *keyfile; | |
1056 | gchar *tmp; | |
1057 | ||
1058 | keyfile = g_key_file_new(); | |
1059 | g_assert(keyfile); | |
1060 | ||
1061 | g_key_file_set_boolean(keyfile, "general", "daemon", config->daemonize); | |
1062 | g_key_file_set_string(keyfile, "general", "method", config->method); | |
26de2296 SH |
1063 | if (config->channel_path) { |
1064 | g_key_file_set_string(keyfile, "general", "path", config->channel_path); | |
1065 | } | |
aeadcbb6 MAL |
1066 | if (config->log_filepath) { |
1067 | g_key_file_set_string(keyfile, "general", "logfile", | |
1068 | config->log_filepath); | |
1069 | } | |
1070 | g_key_file_set_string(keyfile, "general", "pidfile", config->pid_filepath); | |
1071 | #ifdef CONFIG_FSFREEZE | |
1072 | if (config->fsfreeze_hook) { | |
1073 | g_key_file_set_string(keyfile, "general", "fsfreeze-hook", | |
1074 | config->fsfreeze_hook); | |
1075 | } | |
1076 | #endif | |
1077 | g_key_file_set_string(keyfile, "general", "statedir", config->state_dir); | |
1078 | g_key_file_set_boolean(keyfile, "general", "verbose", | |
1079 | config->log_level == G_LOG_LEVEL_MASK); | |
d951fada MR |
1080 | g_key_file_set_boolean(keyfile, "general", "retry-path", |
1081 | config->retry_path); | |
aeadcbb6 | 1082 | tmp = list_join(config->blacklist, ','); |
582a098e | 1083 | g_key_file_set_string(keyfile, "general", "block-rpcs", tmp); |
aeadcbb6 MAL |
1084 | g_free(tmp); |
1085 | ||
1086 | tmp = g_key_file_to_data(keyfile, NULL, &error); | |
cbcd9ba1 MAL |
1087 | if (error) { |
1088 | g_critical("Failed to dump keyfile: %s", error->message); | |
1089 | g_clear_error(&error); | |
1090 | } else { | |
1091 | printf("%s", tmp); | |
1092 | } | |
aeadcbb6 MAL |
1093 | |
1094 | g_free(tmp); | |
1095 | g_key_file_free(keyfile); | |
1096 | } | |
1097 | ||
e236d060 | 1098 | static void config_parse(GAConfig *config, int argc, char **argv) |
7a406694 | 1099 | { |
d951fada | 1100 | const char *sopt = "hVvdm:p:l:f:F::b:s:t:Dr"; |
7a406694 | 1101 | int opt_ind = 0, ch; |
48ff7a62 MR |
1102 | const struct option lopt[] = { |
1103 | { "help", 0, NULL, 'h' }, | |
1104 | { "version", 0, NULL, 'V' }, | |
aeadcbb6 | 1105 | { "dump-conf", 0, NULL, 'D' }, |
bc62fa03 MR |
1106 | { "logfile", 1, NULL, 'l' }, |
1107 | { "pidfile", 1, NULL, 'f' }, | |
ec0f694c TS |
1108 | #ifdef CONFIG_FSFREEZE |
1109 | { "fsfreeze-hook", 2, NULL, 'F' }, | |
1110 | #endif | |
48ff7a62 | 1111 | { "verbose", 0, NULL, 'v' }, |
bc62fa03 MR |
1112 | { "method", 1, NULL, 'm' }, |
1113 | { "path", 1, NULL, 'p' }, | |
48ff7a62 | 1114 | { "daemonize", 0, NULL, 'd' }, |
582a098e TH |
1115 | { "block-rpcs", 1, NULL, 'b' }, |
1116 | { "blacklist", 1, NULL, 'b' }, /* deprecated alias for 'block-rpcs' */ | |
bc62fa03 MR |
1117 | #ifdef _WIN32 |
1118 | { "service", 1, NULL, 's' }, | |
f22d85e9 | 1119 | #endif |
f789aa7b | 1120 | { "statedir", 1, NULL, 't' }, |
d951fada | 1121 | { "retry-path", 0, NULL, 'r' }, |
48ff7a62 MR |
1122 | { NULL, 0, NULL, 0 } |
1123 | }; | |
48ff7a62 MR |
1124 | |
1125 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { | |
1126 | switch (ch) { | |
1127 | case 'm': | |
e236d060 | 1128 | g_free(config->method); |
7a406694 | 1129 | config->method = g_strdup(optarg); |
48ff7a62 MR |
1130 | break; |
1131 | case 'p': | |
e236d060 | 1132 | g_free(config->channel_path); |
7a406694 | 1133 | config->channel_path = g_strdup(optarg); |
48ff7a62 MR |
1134 | break; |
1135 | case 'l': | |
e236d060 | 1136 | g_free(config->log_filepath); |
7a406694 | 1137 | config->log_filepath = g_strdup(optarg); |
48ff7a62 MR |
1138 | break; |
1139 | case 'f': | |
e236d060 | 1140 | g_free(config->pid_filepath); |
7a406694 | 1141 | config->pid_filepath = g_strdup(optarg); |
48ff7a62 | 1142 | break; |
ec0f694c TS |
1143 | #ifdef CONFIG_FSFREEZE |
1144 | case 'F': | |
e236d060 | 1145 | g_free(config->fsfreeze_hook); |
a9eacf8b | 1146 | config->fsfreeze_hook = optarg ? g_strdup(optarg) : get_relocated_path(QGA_FSFREEZE_HOOK_DEFAULT); |
ec0f694c TS |
1147 | break; |
1148 | #endif | |
f789aa7b | 1149 | case 't': |
e236d060 | 1150 | g_free(config->state_dir); |
7a406694 | 1151 | config->state_dir = g_strdup(optarg); |
2e38d990 | 1152 | break; |
48ff7a62 MR |
1153 | case 'v': |
1154 | /* enable all log levels */ | |
7a406694 | 1155 | config->log_level = G_LOG_LEVEL_MASK; |
48ff7a62 MR |
1156 | break; |
1157 | case 'V': | |
8efacc43 | 1158 | printf("QEMU Guest Agent %s\n", QEMU_VERSION); |
c6c84523 | 1159 | exit(EXIT_SUCCESS); |
48ff7a62 | 1160 | case 'd': |
7a406694 | 1161 | config->daemonize = 1; |
48ff7a62 | 1162 | break; |
aeadcbb6 MAL |
1163 | case 'D': |
1164 | config->dumpconf = 1; | |
1165 | break; | |
d951fada MR |
1166 | case 'r': |
1167 | config->retry_path = true; | |
1168 | break; | |
abd6cf6d | 1169 | case 'b': { |
c8057f95 | 1170 | if (is_help_option(optarg)) { |
1527badb | 1171 | qmp_for_each_command(&ga_commands, ga_print_cmd, NULL); |
c6c84523 | 1172 | exit(EXIT_SUCCESS); |
abd6cf6d | 1173 | } |
7a406694 MAL |
1174 | config->blacklist = g_list_concat(config->blacklist, |
1175 | split_list(optarg, ",")); | |
abd6cf6d MR |
1176 | break; |
1177 | } | |
bc62fa03 MR |
1178 | #ifdef _WIN32 |
1179 | case 's': | |
7a406694 MAL |
1180 | config->service = optarg; |
1181 | if (strcmp(config->service, "install") == 0) { | |
f311f2c2 | 1182 | if (ga_install_vss_provider()) { |
c6c84523 | 1183 | exit(EXIT_FAILURE); |
f311f2c2 | 1184 | } |
7a406694 MAL |
1185 | if (ga_install_service(config->channel_path, |
1186 | config->log_filepath, config->state_dir)) { | |
c6c84523 | 1187 | exit(EXIT_FAILURE); |
f311f2c2 | 1188 | } |
c6c84523 | 1189 | exit(EXIT_SUCCESS); |
7a406694 | 1190 | } else if (strcmp(config->service, "uninstall") == 0) { |
f311f2c2 | 1191 | ga_uninstall_vss_provider(); |
c6c84523 | 1192 | exit(ga_uninstall_service()); |
7a406694 | 1193 | } else if (strcmp(config->service, "vss-install") == 0) { |
5e031072 | 1194 | if (ga_install_vss_provider()) { |
c6c84523 | 1195 | exit(EXIT_FAILURE); |
5e031072 | 1196 | } |
c6c84523 | 1197 | exit(EXIT_SUCCESS); |
7a406694 | 1198 | } else if (strcmp(config->service, "vss-uninstall") == 0) { |
5e031072 | 1199 | ga_uninstall_vss_provider(); |
c6c84523 | 1200 | exit(EXIT_SUCCESS); |
bc62fa03 MR |
1201 | } else { |
1202 | printf("Unknown service command.\n"); | |
c6c84523 | 1203 | exit(EXIT_FAILURE); |
bc62fa03 MR |
1204 | } |
1205 | break; | |
1206 | #endif | |
48ff7a62 MR |
1207 | case 'h': |
1208 | usage(argv[0]); | |
c6c84523 | 1209 | exit(EXIT_SUCCESS); |
48ff7a62 MR |
1210 | case '?': |
1211 | g_print("Unknown option, try '%s --help' for more information.\n", | |
1212 | argv[0]); | |
c6c84523 | 1213 | exit(EXIT_FAILURE); |
48ff7a62 MR |
1214 | } |
1215 | } | |
7a406694 MAL |
1216 | } |
1217 | ||
1218 | static void config_free(GAConfig *config) | |
1219 | { | |
1220 | g_free(config->method); | |
1221 | g_free(config->log_filepath); | |
1222 | g_free(config->pid_filepath); | |
1223 | g_free(config->state_dir); | |
1224 | g_free(config->channel_path); | |
e236d060 | 1225 | g_free(config->bliststr); |
7a406694 MAL |
1226 | #ifdef CONFIG_FSFREEZE |
1227 | g_free(config->fsfreeze_hook); | |
1228 | #endif | |
2aa67a91 | 1229 | g_list_free_full(config->blacklist, g_free); |
7a406694 MAL |
1230 | g_free(config); |
1231 | } | |
1232 | ||
e3d31039 | 1233 | static bool check_is_frozen(GAState *s) |
7a406694 | 1234 | { |
f789aa7b MR |
1235 | #ifndef _WIN32 |
1236 | /* check if a previous instance of qemu-ga exited with filesystems' state | |
1237 | * marked as frozen. this could be a stale value (a non-qemu-ga process | |
1238 | * or reboot may have since unfrozen them), but better to require an | |
1239 | * uneeded unfreeze than to risk hanging on start-up | |
1240 | */ | |
1241 | struct stat st; | |
1242 | if (stat(s->state_filepath_isfrozen, &st) == -1) { | |
1243 | /* it's okay if the file doesn't exist, but if we can't access for | |
1244 | * some other reason, such as permissions, there's a configuration | |
1245 | * that needs to be addressed. so just bail now before we get into | |
1246 | * more trouble later | |
1247 | */ | |
1248 | if (errno != ENOENT) { | |
1249 | g_critical("unable to access state file at path %s: %s", | |
1250 | s->state_filepath_isfrozen, strerror(errno)); | |
1251 | return EXIT_FAILURE; | |
1252 | } | |
1253 | } else { | |
1254 | g_warning("previous instance appears to have exited with frozen" | |
1255 | " filesystems. deferring logging/pidfile creation and" | |
1256 | " disabling non-fsfreeze-safe commands until" | |
1257 | " guest-fsfreeze-thaw is issued, or filesystems are" | |
1258 | " manually unfrozen and the file %s is removed", | |
1259 | s->state_filepath_isfrozen); | |
e3d31039 MAL |
1260 | return true; |
1261 | } | |
1262 | #endif | |
1263 | return false; | |
1264 | } | |
1265 | ||
0f4d3a49 | 1266 | static GAState *initialize_agent(GAConfig *config, int socket_activation) |
e3d31039 | 1267 | { |
50d5b3c4 MR |
1268 | GAState *s = g_new0(GAState, 1); |
1269 | ||
1270 | g_assert(ga_state == NULL); | |
1271 | ||
1272 | s->log_level = config->log_level; | |
1273 | s->log_file = stderr; | |
1274 | #ifdef CONFIG_FSFREEZE | |
1275 | s->fsfreeze_hook = config->fsfreeze_hook; | |
1276 | #endif | |
1277 | s->pstate_filepath = g_strdup_printf("%s/qga.state", config->state_dir); | |
1278 | s->state_filepath_isfrozen = g_strdup_printf("%s/qga.state.isfrozen", | |
1279 | config->state_dir); | |
1280 | s->frozen = check_is_frozen(s); | |
e3d31039 MAL |
1281 | |
1282 | g_log_set_default_handler(ga_log, s); | |
1283 | g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR); | |
1284 | ga_enable_logging(s); | |
1285 | ||
323f3a8f KK |
1286 | g_debug("Guest agent version %s started", QEMU_FULL_VERSION); |
1287 | ||
e3d31039 MAL |
1288 | #ifdef _WIN32 |
1289 | /* On win32 the state directory is application specific (be it the default | |
1290 | * or a user override). We got past the command line parsing; let's create | |
1291 | * the directory (with any intermediate directories). If we run into an | |
1292 | * error later on, we won't try to clean up the directory, it is considered | |
1293 | * persistent. | |
1294 | */ | |
1295 | if (g_mkdir_with_parents(config->state_dir, S_IRWXU) == -1) { | |
1296 | g_critical("unable to create (an ancestor of) the state directory" | |
1297 | " '%s': %s", config->state_dir, strerror(errno)); | |
50d5b3c4 | 1298 | return NULL; |
f789aa7b MR |
1299 | } |
1300 | #endif | |
1301 | ||
1302 | if (ga_is_frozen(s)) { | |
7a406694 | 1303 | if (config->daemonize) { |
8e8be266 | 1304 | /* delay opening/locking of pidfile till filesystems are unfrozen */ |
7a406694 | 1305 | s->deferred_options.pid_filepath = config->pid_filepath; |
f789aa7b MR |
1306 | become_daemon(NULL); |
1307 | } | |
7a406694 | 1308 | if (config->log_filepath) { |
f789aa7b | 1309 | /* delay opening the log file till filesystems are unfrozen */ |
7a406694 | 1310 | s->deferred_options.log_filepath = config->log_filepath; |
f789aa7b MR |
1311 | } |
1312 | ga_disable_logging(s); | |
1527badb | 1313 | qmp_for_each_command(&ga_commands, ga_disable_non_whitelisted, NULL); |
f789aa7b | 1314 | } else { |
7a406694 MAL |
1315 | if (config->daemonize) { |
1316 | become_daemon(config->pid_filepath); | |
f789aa7b | 1317 | } |
7a406694 MAL |
1318 | if (config->log_filepath) { |
1319 | FILE *log_file = ga_open_logfile(config->log_filepath); | |
6c615ec5 | 1320 | if (!log_file) { |
f789aa7b MR |
1321 | g_critical("unable to open specified log file: %s", |
1322 | strerror(errno)); | |
50d5b3c4 | 1323 | return NULL; |
f789aa7b | 1324 | } |
6c615ec5 | 1325 | s->log_file = log_file; |
f789aa7b MR |
1326 | } |
1327 | } | |
1328 | ||
39097daf MR |
1329 | /* load persistent state from disk */ |
1330 | if (!read_persistent_state(&s->pstate, | |
1331 | s->pstate_filepath, | |
1332 | ga_is_frozen(s))) { | |
1333 | g_critical("failed to load persistent state"); | |
50d5b3c4 | 1334 | return NULL; |
39097daf MR |
1335 | } |
1336 | ||
7a406694 MAL |
1337 | config->blacklist = ga_command_blacklist_init(config->blacklist); |
1338 | if (config->blacklist) { | |
1339 | GList *l = config->blacklist; | |
1340 | s->blacklist = config->blacklist; | |
f22d85e9 | 1341 | do { |
7a406694 | 1342 | g_debug("disabling command: %s", (char *)l->data); |
c98939da | 1343 | qmp_disable_command(&ga_commands, l->data, NULL); |
7a406694 MAL |
1344 | l = g_list_next(l); |
1345 | } while (l); | |
f22d85e9 | 1346 | } |
e3d4d252 MR |
1347 | s->command_state = ga_command_state_new(); |
1348 | ga_command_state_init(s, s->command_state); | |
1349 | ga_command_state_init_all(s->command_state); | |
62815d85 | 1350 | json_message_parser_init(&s->parser, process_event, s, NULL); |
f8837b37 | 1351 | |
d8ca685a | 1352 | #ifndef _WIN32 |
125b310e MR |
1353 | if (!register_signal_handlers()) { |
1354 | g_critical("failed to register signal handlers"); | |
50d5b3c4 | 1355 | return NULL; |
125b310e | 1356 | } |
d8ca685a | 1357 | #endif |
48ff7a62 | 1358 | |
125b310e | 1359 | s->main_loop = g_main_loop_new(NULL, false); |
26de2296 | 1360 | |
0f4d3a49 MR |
1361 | s->config = config; |
1362 | s->socket_activation = socket_activation; | |
b70d6afe BA |
1363 | |
1364 | #ifdef _WIN32 | |
1365 | s->wakeup_event = CreateEvent(NULL, TRUE, FALSE, TEXT("WakeUp")); | |
1366 | if (s->wakeup_event == NULL) { | |
1367 | g_critical("CreateEvent failed"); | |
1368 | return NULL; | |
1369 | } | |
1370 | #endif | |
1371 | ||
50d5b3c4 MR |
1372 | ga_state = s; |
1373 | return s; | |
1374 | } | |
1375 | ||
1376 | static void cleanup_agent(GAState *s) | |
1377 | { | |
b70d6afe BA |
1378 | #ifdef _WIN32 |
1379 | CloseHandle(s->wakeup_event); | |
1380 | #endif | |
50d5b3c4 MR |
1381 | if (s->command_state) { |
1382 | ga_command_state_cleanup_all(s->command_state); | |
1383 | ga_command_state_free(s->command_state); | |
1384 | json_message_parser_destroy(&s->parser); | |
1385 | } | |
50d5b3c4 MR |
1386 | g_free(s->pstate_filepath); |
1387 | g_free(s->state_filepath_isfrozen); | |
1388 | if (s->main_loop) { | |
1389 | g_main_loop_unref(s->main_loop); | |
1390 | } | |
1391 | g_free(s); | |
1392 | ga_state = NULL; | |
1393 | } | |
1394 | ||
d951fada | 1395 | static int run_agent_once(GAState *s) |
50d5b3c4 | 1396 | { |
0f4d3a49 MR |
1397 | if (!channel_init(s, s->config->method, s->config->channel_path, |
1398 | s->socket_activation ? FIRST_SOCKET_ACTIVATION_FD : -1)) { | |
125b310e | 1399 | g_critical("failed to initialize guest agent channel"); |
e3d31039 | 1400 | return EXIT_FAILURE; |
125b310e | 1401 | } |
d88495a8 | 1402 | |
48ff7a62 MR |
1403 | g_main_loop_run(ga_state->main_loop); |
1404 | ||
d951fada MR |
1405 | if (s->channel) { |
1406 | ga_channel_free(s->channel); | |
1407 | } | |
1408 | ||
e3d31039 MAL |
1409 | return EXIT_SUCCESS; |
1410 | } | |
48ff7a62 | 1411 | |
b70d6afe BA |
1412 | static void wait_for_channel_availability(GAState *s) |
1413 | { | |
1414 | g_warning("waiting for channel path..."); | |
1415 | #ifndef _WIN32 | |
1416 | sleep(QGA_RETRY_INTERVAL); | |
1417 | #else | |
1418 | DWORD dwWaitResult; | |
1419 | ||
1420 | dwWaitResult = WaitForSingleObject(s->wakeup_event, INFINITE); | |
1421 | ||
1422 | switch (dwWaitResult) { | |
1423 | case WAIT_OBJECT_0: | |
1424 | break; | |
1425 | case WAIT_TIMEOUT: | |
1426 | break; | |
1427 | default: | |
1428 | g_critical("WaitForSingleObject failed"); | |
1429 | } | |
1430 | #endif | |
1431 | } | |
1432 | ||
d951fada MR |
1433 | static int run_agent(GAState *s) |
1434 | { | |
1435 | int ret = EXIT_SUCCESS; | |
1436 | ||
1437 | s->force_exit = false; | |
1438 | ||
1439 | do { | |
1440 | ret = run_agent_once(s); | |
1441 | if (s->config->retry_path && !s->force_exit) { | |
1442 | g_warning("agent stopped unexpectedly, restarting..."); | |
b70d6afe | 1443 | wait_for_channel_availability(s); |
d951fada MR |
1444 | } |
1445 | } while (s->config->retry_path && !s->force_exit); | |
1446 | ||
1447 | return ret; | |
1448 | } | |
1449 | ||
1450 | static void stop_agent(GAState *s, bool requested) | |
1451 | { | |
1452 | if (!s->force_exit) { | |
1453 | s->force_exit = requested; | |
1454 | } | |
1455 | ||
1456 | if (g_main_loop_is_running(s->main_loop)) { | |
1457 | g_main_loop_quit(s->main_loop); | |
1458 | } | |
1459 | } | |
1460 | ||
e3d31039 MAL |
1461 | int main(int argc, char **argv) |
1462 | { | |
1463 | int ret = EXIT_SUCCESS; | |
50d5b3c4 | 1464 | GAState *s; |
e236d060 | 1465 | GAConfig *config = g_new0(GAConfig, 1); |
53fabd4b | 1466 | int socket_activation; |
e3d31039 | 1467 | |
6eaeae37 MAL |
1468 | config->log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL; |
1469 | ||
a9eacf8b | 1470 | qemu_init_exec_dir(argv[0]); |
1527badb | 1471 | qga_qmp_init_marshal(&ga_commands); |
e3d31039 MAL |
1472 | |
1473 | init_dfl_pathnames(); | |
e236d060 MAL |
1474 | config_load(config); |
1475 | config_parse(config, argc, argv); | |
e3d31039 MAL |
1476 | |
1477 | if (config->pid_filepath == NULL) { | |
1478 | config->pid_filepath = g_strdup(dfl_pathnames.pidfile); | |
1479 | } | |
1480 | ||
1481 | if (config->state_dir == NULL) { | |
1482 | config->state_dir = g_strdup(dfl_pathnames.state_dir); | |
1483 | } | |
1484 | ||
1485 | if (config->method == NULL) { | |
1486 | config->method = g_strdup("virtio-serial"); | |
125b310e | 1487 | } |
125b310e | 1488 | |
53fabd4b PB |
1489 | socket_activation = check_socket_activation(); |
1490 | if (socket_activation > 1) { | |
1491 | g_critical("qemu-ga only supports listening on one socket"); | |
1492 | ret = EXIT_FAILURE; | |
1493 | goto end; | |
1494 | } | |
1495 | if (socket_activation) { | |
bd269ebc | 1496 | SocketAddress *addr; |
26de2296 SH |
1497 | |
1498 | g_free(config->method); | |
1499 | g_free(config->channel_path); | |
1500 | config->method = NULL; | |
1501 | config->channel_path = NULL; | |
1502 | ||
53fabd4b | 1503 | addr = socket_local_address(FIRST_SOCKET_ACTIVATION_FD, NULL); |
26de2296 | 1504 | if (addr) { |
bd269ebc | 1505 | if (addr->type == SOCKET_ADDRESS_TYPE_UNIX) { |
26de2296 | 1506 | config->method = g_strdup("unix-listen"); |
bd269ebc | 1507 | } else if (addr->type == SOCKET_ADDRESS_TYPE_VSOCK) { |
26de2296 SH |
1508 | config->method = g_strdup("vsock-listen"); |
1509 | } | |
1510 | ||
bd269ebc | 1511 | qapi_free_SocketAddress(addr); |
26de2296 SH |
1512 | } |
1513 | ||
1514 | if (!config->method) { | |
1515 | g_critical("unsupported listen fd type"); | |
1516 | ret = EXIT_FAILURE; | |
1517 | goto end; | |
1518 | } | |
1519 | } else if (config->channel_path == NULL) { | |
e3d31039 MAL |
1520 | if (strcmp(config->method, "virtio-serial") == 0) { |
1521 | /* try the default path for the virtio-serial port */ | |
1522 | config->channel_path = g_strdup(QGA_VIRTIO_PATH_DEFAULT); | |
1523 | } else if (strcmp(config->method, "isa-serial") == 0) { | |
1524 | /* try the default path for the serial port - COM1 */ | |
1525 | config->channel_path = g_strdup(QGA_SERIAL_PATH_DEFAULT); | |
1526 | } else { | |
1527 | g_critical("must specify a path for this channel"); | |
1528 | ret = EXIT_FAILURE; | |
1529 | goto end; | |
1530 | } | |
1531 | } | |
1532 | ||
aeadcbb6 MAL |
1533 | if (config->dumpconf) { |
1534 | config_dump(config); | |
1535 | goto end; | |
1536 | } | |
1537 | ||
0f4d3a49 | 1538 | s = initialize_agent(config, socket_activation); |
50d5b3c4 MR |
1539 | if (!s) { |
1540 | g_critical("error initializing guest agent"); | |
1541 | goto end; | |
1542 | } | |
d88495a8 MR |
1543 | |
1544 | #ifdef _WIN32 | |
1545 | if (config->daemonize) { | |
1546 | SERVICE_TABLE_ENTRY service_table[] = { | |
1547 | { (char *)QGA_SERVICE_NAME, service_main }, { NULL, NULL } }; | |
1548 | StartServiceCtrlDispatcher(service_table); | |
1549 | } else { | |
1550 | ret = run_agent(s); | |
1551 | } | |
1552 | #else | |
0f4d3a49 | 1553 | ret = run_agent(s); |
d88495a8 MR |
1554 | #endif |
1555 | ||
50d5b3c4 | 1556 | cleanup_agent(s); |
e3d31039 MAL |
1557 | |
1558 | end: | |
7a406694 MAL |
1559 | if (config->daemonize) { |
1560 | unlink(config->pid_filepath); | |
125b310e | 1561 | } |
2e38d990 | 1562 | |
7a406694 | 1563 | config_free(config); |
2e38d990 | 1564 | |
e3d31039 | 1565 | return ret; |
48ff7a62 | 1566 | } |