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