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