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