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