pci_host: Turn into SysBus-derived QOM type
[qemu-kvm.git] / qemu-ga.c
blob26671fee3f61577505c63ddeec85ae502209d9c9
1 /*
2 * QEMU Guest Agent
4 * Copyright IBM Corp. 2011
6 * Authors:
7 * Adam Litke <aglitke@linux.vnet.ibm.com>
8 * Michael Roth <mdroth@linux.vnet.ibm.com>
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.
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <stdbool.h>
16 #include <glib.h>
17 #include <getopt.h>
18 #ifndef _WIN32
19 #include <syslog.h>
20 #include <sys/wait.h>
21 #include <sys/stat.h>
22 #endif
23 #include "json-streamer.h"
24 #include "json-parser.h"
25 #include "qint.h"
26 #include "qjson.h"
27 #include "qga/guest-agent-core.h"
28 #include "module.h"
29 #include "signal.h"
30 #include "qerror.h"
31 #include "qapi/qmp-core.h"
32 #include "qga/channel.h"
33 #ifdef _WIN32
34 #include "qga/service-win32.h"
35 #include <windows.h>
36 #endif
38 #ifndef _WIN32
39 #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0"
40 #else
41 #define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0"
42 #endif
43 #define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid"
44 #define QGA_STATEDIR_DEFAULT "/tmp"
45 #define QGA_SENTINEL_BYTE 0xFF
47 struct GAState {
48 JSONMessageParser parser;
49 GMainLoop *main_loop;
50 GAChannel *channel;
51 bool virtio; /* fastpath to check for virtio to deal with poll() quirks */
52 GACommandState *command_state;
53 GLogLevelFlags log_level;
54 FILE *log_file;
55 bool logging_enabled;
56 #ifdef _WIN32
57 GAService service;
58 #endif
59 bool delimit_response;
60 bool frozen;
61 GList *blacklist;
62 const char *state_filepath_isfrozen;
63 struct {
64 const char *log_filepath;
65 const char *pid_filepath;
66 } deferred_options;
69 struct GAState *ga_state;
71 /* commands that are safe to issue while filesystems are frozen */
72 static const char *ga_freeze_whitelist[] = {
73 "guest-ping",
74 "guest-info",
75 "guest-sync",
76 "guest-fsfreeze-status",
77 "guest-fsfreeze-thaw",
78 NULL
81 #ifdef _WIN32
82 DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data,
83 LPVOID ctx);
84 VOID WINAPI service_main(DWORD argc, TCHAR *argv[]);
85 #endif
87 static void quit_handler(int sig)
89 /* if we're frozen, don't exit unless we're absolutely forced to,
90 * because it's basically impossible for graceful exit to complete
91 * unless all log/pid files are on unfreezable filesystems. there's
92 * also a very likely chance killing the agent before unfreezing
93 * the filesystems is a mistake (or will be viewed as one later).
95 if (ga_is_frozen(ga_state)) {
96 return;
98 g_debug("received signal num %d, quitting", sig);
100 if (g_main_loop_is_running(ga_state->main_loop)) {
101 g_main_loop_quit(ga_state->main_loop);
105 #ifndef _WIN32
106 static gboolean register_signal_handlers(void)
108 struct sigaction sigact;
109 int ret;
111 memset(&sigact, 0, sizeof(struct sigaction));
112 sigact.sa_handler = quit_handler;
114 ret = sigaction(SIGINT, &sigact, NULL);
115 if (ret == -1) {
116 g_error("error configuring signal handler: %s", strerror(errno));
117 return false;
119 ret = sigaction(SIGTERM, &sigact, NULL);
120 if (ret == -1) {
121 g_error("error configuring signal handler: %s", strerror(errno));
122 return false;
125 return true;
128 /* TODO: use this in place of all post-fork() fclose(std*) callers */
129 void reopen_fd_to_null(int fd)
131 int nullfd;
133 nullfd = open("/dev/null", O_RDWR);
134 if (nullfd < 0) {
135 return;
138 dup2(nullfd, fd);
140 if (nullfd != fd) {
141 close(nullfd);
144 #endif
146 static void usage(const char *cmd)
148 printf(
149 "Usage: %s [-m <method> -p <path>] [<options>]\n"
150 "QEMU Guest Agent %s\n"
151 "\n"
152 " -m, --method transport method: one of unix-listen, virtio-serial, or\n"
153 " isa-serial (virtio-serial is the default)\n"
154 " -p, --path device/socket path (the default for virtio-serial is:\n"
155 " %s)\n"
156 " -l, --logfile set logfile path, logs to stderr by default\n"
157 " -f, --pidfile specify pidfile (default is %s)\n"
158 " -t, --statedir specify dir to store state information (absolute paths\n"
159 " only, default is %s)\n"
160 " -v, --verbose log extra debugging information\n"
161 " -V, --version print version information and exit\n"
162 " -d, --daemonize become a daemon\n"
163 #ifdef _WIN32
164 " -s, --service service commands: install, uninstall\n"
165 #endif
166 " -b, --blacklist comma-separated list of RPCs to disable (no spaces, \"?\"\n"
167 " to list available RPCs)\n"
168 " -h, --help display this help and exit\n"
169 "\n"
170 "Report bugs to <mdroth@linux.vnet.ibm.com>\n"
171 , cmd, QEMU_VERSION, QGA_VIRTIO_PATH_DEFAULT, QGA_PIDFILE_DEFAULT,
172 QGA_STATEDIR_DEFAULT);
175 static const char *ga_log_level_str(GLogLevelFlags level)
177 switch (level & G_LOG_LEVEL_MASK) {
178 case G_LOG_LEVEL_ERROR:
179 return "error";
180 case G_LOG_LEVEL_CRITICAL:
181 return "critical";
182 case G_LOG_LEVEL_WARNING:
183 return "warning";
184 case G_LOG_LEVEL_MESSAGE:
185 return "message";
186 case G_LOG_LEVEL_INFO:
187 return "info";
188 case G_LOG_LEVEL_DEBUG:
189 return "debug";
190 default:
191 return "user";
195 bool ga_logging_enabled(GAState *s)
197 return s->logging_enabled;
200 void ga_disable_logging(GAState *s)
202 s->logging_enabled = false;
205 void ga_enable_logging(GAState *s)
207 s->logging_enabled = true;
210 static void ga_log(const gchar *domain, GLogLevelFlags level,
211 const gchar *msg, gpointer opaque)
213 GAState *s = opaque;
214 GTimeVal time;
215 const char *level_str = ga_log_level_str(level);
217 if (!ga_logging_enabled(s)) {
218 return;
221 level &= G_LOG_LEVEL_MASK;
222 #ifndef _WIN32
223 if (domain && strcmp(domain, "syslog") == 0) {
224 syslog(LOG_INFO, "%s: %s", level_str, msg);
225 } else if (level & s->log_level) {
226 #else
227 if (level & s->log_level) {
228 #endif
229 g_get_current_time(&time);
230 fprintf(s->log_file,
231 "%lu.%lu: %s: %s\n", time.tv_sec, time.tv_usec, level_str, msg);
232 fflush(s->log_file);
236 void ga_set_response_delimited(GAState *s)
238 s->delimit_response = true;
241 #ifndef _WIN32
242 static bool ga_open_pidfile(const char *pidfile)
244 int pidfd;
245 char pidstr[32];
247 pidfd = open(pidfile, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
248 if (pidfd == -1 || lockf(pidfd, F_TLOCK, 0)) {
249 g_critical("Cannot lock pid file, %s", strerror(errno));
250 if (pidfd != -1) {
251 close(pidfd);
253 return false;
256 if (ftruncate(pidfd, 0) || lseek(pidfd, 0, SEEK_SET)) {
257 g_critical("Failed to truncate pid file");
258 goto fail;
260 sprintf(pidstr, "%d", getpid());
261 if (write(pidfd, pidstr, strlen(pidstr)) != strlen(pidstr)) {
262 g_critical("Failed to write pid file");
263 goto fail;
266 return true;
268 fail:
269 unlink(pidfile);
270 return false;
272 #else /* _WIN32 */
273 static bool ga_open_pidfile(const char *pidfile)
275 return true;
277 #endif
279 static gint ga_strcmp(gconstpointer str1, gconstpointer str2)
281 return strcmp(str1, str2);
284 /* disable commands that aren't safe for fsfreeze */
285 static void ga_disable_non_whitelisted(void)
287 char **list_head, **list;
288 bool whitelisted;
289 int i;
291 list_head = list = qmp_get_command_list();
292 while (*list != NULL) {
293 whitelisted = false;
294 i = 0;
295 while (ga_freeze_whitelist[i] != NULL) {
296 if (strcmp(*list, ga_freeze_whitelist[i]) == 0) {
297 whitelisted = true;
299 i++;
301 if (!whitelisted) {
302 g_debug("disabling command: %s", *list);
303 qmp_disable_command(*list);
305 g_free(*list);
306 list++;
308 g_free(list_head);
311 /* [re-]enable all commands, except those explicitly blacklisted by user */
312 static void ga_enable_non_blacklisted(GList *blacklist)
314 char **list_head, **list;
316 list_head = list = qmp_get_command_list();
317 while (*list != NULL) {
318 if (g_list_find_custom(blacklist, *list, ga_strcmp) == NULL &&
319 !qmp_command_is_enabled(*list)) {
320 g_debug("enabling command: %s", *list);
321 qmp_enable_command(*list);
323 g_free(*list);
324 list++;
326 g_free(list_head);
329 static bool ga_create_file(const char *path)
331 int fd = open(path, O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR);
332 if (fd == -1) {
333 g_warning("unable to open/create file %s: %s", path, strerror(errno));
334 return false;
336 close(fd);
337 return true;
340 static bool ga_delete_file(const char *path)
342 int ret = unlink(path);
343 if (ret == -1) {
344 g_warning("unable to delete file: %s: %s", path, strerror(errno));
345 return false;
348 return true;
351 bool ga_is_frozen(GAState *s)
353 return s->frozen;
356 void ga_set_frozen(GAState *s)
358 if (ga_is_frozen(s)) {
359 return;
361 /* disable all non-whitelisted (for frozen state) commands */
362 ga_disable_non_whitelisted();
363 g_warning("disabling logging due to filesystem freeze");
364 ga_disable_logging(s);
365 s->frozen = true;
366 if (!ga_create_file(s->state_filepath_isfrozen)) {
367 g_warning("unable to create %s, fsfreeze may not function properly",
368 s->state_filepath_isfrozen);
372 void ga_unset_frozen(GAState *s)
374 if (!ga_is_frozen(s)) {
375 return;
378 /* if we delayed creation/opening of pid/log files due to being
379 * in a frozen state at start up, do it now
381 if (s->deferred_options.log_filepath) {
382 s->log_file = fopen(s->deferred_options.log_filepath, "a");
383 if (!s->log_file) {
384 s->log_file = stderr;
386 s->deferred_options.log_filepath = NULL;
388 ga_enable_logging(s);
389 g_warning("logging re-enabled due to filesystem unfreeze");
390 if (s->deferred_options.pid_filepath) {
391 if (!ga_open_pidfile(s->deferred_options.pid_filepath)) {
392 g_warning("failed to create/open pid file");
394 s->deferred_options.pid_filepath = NULL;
397 /* enable all disabled, non-blacklisted commands */
398 ga_enable_non_blacklisted(s->blacklist);
399 s->frozen = false;
400 if (!ga_delete_file(s->state_filepath_isfrozen)) {
401 g_warning("unable to delete %s, fsfreeze may not function properly",
402 s->state_filepath_isfrozen);
406 static void become_daemon(const char *pidfile)
408 #ifndef _WIN32
409 pid_t pid, sid;
411 pid = fork();
412 if (pid < 0) {
413 exit(EXIT_FAILURE);
415 if (pid > 0) {
416 exit(EXIT_SUCCESS);
419 if (pidfile) {
420 if (!ga_open_pidfile(pidfile)) {
421 g_critical("failed to create pidfile");
422 exit(EXIT_FAILURE);
426 umask(0);
427 sid = setsid();
428 if (sid < 0) {
429 goto fail;
431 if ((chdir("/")) < 0) {
432 goto fail;
435 reopen_fd_to_null(STDIN_FILENO);
436 reopen_fd_to_null(STDOUT_FILENO);
437 reopen_fd_to_null(STDERR_FILENO);
438 return;
440 fail:
441 unlink(pidfile);
442 g_critical("failed to daemonize");
443 exit(EXIT_FAILURE);
444 #endif
447 static int send_response(GAState *s, QObject *payload)
449 const char *buf;
450 QString *payload_qstr, *response_qstr;
451 GIOStatus status;
453 g_assert(payload && s->channel);
455 payload_qstr = qobject_to_json(payload);
456 if (!payload_qstr) {
457 return -EINVAL;
460 if (s->delimit_response) {
461 s->delimit_response = false;
462 response_qstr = qstring_new();
463 qstring_append_chr(response_qstr, QGA_SENTINEL_BYTE);
464 qstring_append(response_qstr, qstring_get_str(payload_qstr));
465 QDECREF(payload_qstr);
466 } else {
467 response_qstr = payload_qstr;
470 qstring_append_chr(response_qstr, '\n');
471 buf = qstring_get_str(response_qstr);
472 status = ga_channel_write_all(s->channel, buf, strlen(buf));
473 QDECREF(response_qstr);
474 if (status != G_IO_STATUS_NORMAL) {
475 return -EIO;
478 return 0;
481 static void process_command(GAState *s, QDict *req)
483 QObject *rsp = NULL;
484 int ret;
486 g_assert(req);
487 g_debug("processing command");
488 rsp = qmp_dispatch(QOBJECT(req));
489 if (rsp) {
490 ret = send_response(s, rsp);
491 if (ret) {
492 g_warning("error sending response: %s", strerror(ret));
494 qobject_decref(rsp);
498 /* handle requests/control events coming in over the channel */
499 static void process_event(JSONMessageParser *parser, QList *tokens)
501 GAState *s = container_of(parser, GAState, parser);
502 QObject *obj;
503 QDict *qdict;
504 Error *err = NULL;
505 int ret;
507 g_assert(s && parser);
509 g_debug("process_event: called");
510 obj = json_parser_parse_err(tokens, NULL, &err);
511 if (err || !obj || qobject_type(obj) != QTYPE_QDICT) {
512 qobject_decref(obj);
513 qdict = qdict_new();
514 if (!err) {
515 g_warning("failed to parse event: unknown error");
516 error_set(&err, QERR_JSON_PARSING);
517 } else {
518 g_warning("failed to parse event: %s", error_get_pretty(err));
520 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
521 error_free(err);
522 } else {
523 qdict = qobject_to_qdict(obj);
526 g_assert(qdict);
528 /* handle host->guest commands */
529 if (qdict_haskey(qdict, "execute")) {
530 process_command(s, qdict);
531 } else {
532 if (!qdict_haskey(qdict, "error")) {
533 QDECREF(qdict);
534 qdict = qdict_new();
535 g_warning("unrecognized payload format");
536 error_set(&err, QERR_UNSUPPORTED);
537 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
538 error_free(err);
540 ret = send_response(s, QOBJECT(qdict));
541 if (ret) {
542 g_warning("error sending error response: %s", strerror(ret));
546 QDECREF(qdict);
549 /* false return signals GAChannel to close the current client connection */
550 static gboolean channel_event_cb(GIOCondition condition, gpointer data)
552 GAState *s = data;
553 gchar buf[QGA_READ_COUNT_DEFAULT+1];
554 gsize count;
555 GError *err = NULL;
556 GIOStatus status = ga_channel_read(s->channel, buf, QGA_READ_COUNT_DEFAULT, &count);
557 if (err != NULL) {
558 g_warning("error reading channel: %s", err->message);
559 g_error_free(err);
560 return false;
562 switch (status) {
563 case G_IO_STATUS_ERROR:
564 g_warning("error reading channel");
565 return false;
566 case G_IO_STATUS_NORMAL:
567 buf[count] = 0;
568 g_debug("read data, count: %d, data: %s", (int)count, buf);
569 json_message_parser_feed(&s->parser, (char *)buf, (int)count);
570 break;
571 case G_IO_STATUS_EOF:
572 g_debug("received EOF");
573 if (!s->virtio) {
574 return false;
576 case G_IO_STATUS_AGAIN:
577 /* virtio causes us to spin here when no process is attached to
578 * host-side chardev. sleep a bit to mitigate this
580 if (s->virtio) {
581 usleep(100*1000);
583 return true;
584 default:
585 g_warning("unknown channel read status, closing");
586 return false;
588 return true;
591 static gboolean channel_init(GAState *s, const gchar *method, const gchar *path)
593 GAChannelMethod channel_method;
595 if (method == NULL) {
596 method = "virtio-serial";
599 if (path == NULL) {
600 if (strcmp(method, "virtio-serial") != 0) {
601 g_critical("must specify a path for this channel");
602 return false;
604 /* try the default path for the virtio-serial port */
605 path = QGA_VIRTIO_PATH_DEFAULT;
608 if (strcmp(method, "virtio-serial") == 0) {
609 s->virtio = true; /* virtio requires special handling in some cases */
610 channel_method = GA_CHANNEL_VIRTIO_SERIAL;
611 } else if (strcmp(method, "isa-serial") == 0) {
612 channel_method = GA_CHANNEL_ISA_SERIAL;
613 } else if (strcmp(method, "unix-listen") == 0) {
614 channel_method = GA_CHANNEL_UNIX_LISTEN;
615 } else {
616 g_critical("unsupported channel method/type: %s", method);
617 return false;
620 s->channel = ga_channel_new(channel_method, path, channel_event_cb, s);
621 if (!s->channel) {
622 g_critical("failed to create guest agent channel");
623 return false;
626 return true;
629 #ifdef _WIN32
630 DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data,
631 LPVOID ctx)
633 DWORD ret = NO_ERROR;
634 GAService *service = &ga_state->service;
636 switch (ctrl)
638 case SERVICE_CONTROL_STOP:
639 case SERVICE_CONTROL_SHUTDOWN:
640 quit_handler(SIGTERM);
641 service->status.dwCurrentState = SERVICE_STOP_PENDING;
642 SetServiceStatus(service->status_handle, &service->status);
643 break;
645 default:
646 ret = ERROR_CALL_NOT_IMPLEMENTED;
648 return ret;
651 VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
653 GAService *service = &ga_state->service;
655 service->status_handle = RegisterServiceCtrlHandlerEx(QGA_SERVICE_NAME,
656 service_ctrl_handler, NULL);
658 if (service->status_handle == 0) {
659 g_critical("Failed to register extended requests function!\n");
660 return;
663 service->status.dwServiceType = SERVICE_WIN32;
664 service->status.dwCurrentState = SERVICE_RUNNING;
665 service->status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
666 service->status.dwWin32ExitCode = NO_ERROR;
667 service->status.dwServiceSpecificExitCode = NO_ERROR;
668 service->status.dwCheckPoint = 0;
669 service->status.dwWaitHint = 0;
670 SetServiceStatus(service->status_handle, &service->status);
672 g_main_loop_run(ga_state->main_loop);
674 service->status.dwCurrentState = SERVICE_STOPPED;
675 SetServiceStatus(service->status_handle, &service->status);
677 #endif
679 int main(int argc, char **argv)
681 const char *sopt = "hVvdm:p:l:f:b:s:t:";
682 const char *method = NULL, *path = NULL;
683 const char *log_filepath = NULL;
684 const char *pid_filepath = QGA_PIDFILE_DEFAULT;
685 const char *state_dir = QGA_STATEDIR_DEFAULT;
686 #ifdef _WIN32
687 const char *service = NULL;
688 #endif
689 const struct option lopt[] = {
690 { "help", 0, NULL, 'h' },
691 { "version", 0, NULL, 'V' },
692 { "logfile", 1, NULL, 'l' },
693 { "pidfile", 1, NULL, 'f' },
694 { "verbose", 0, NULL, 'v' },
695 { "method", 1, NULL, 'm' },
696 { "path", 1, NULL, 'p' },
697 { "daemonize", 0, NULL, 'd' },
698 { "blacklist", 1, NULL, 'b' },
699 #ifdef _WIN32
700 { "service", 1, NULL, 's' },
701 #endif
702 { "statedir", 1, NULL, 't' },
703 { NULL, 0, NULL, 0 }
705 int opt_ind = 0, ch, daemonize = 0, i, j, len;
706 GLogLevelFlags log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL;
707 GList *blacklist = NULL;
708 GAState *s;
710 module_call_init(MODULE_INIT_QAPI);
712 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
713 switch (ch) {
714 case 'm':
715 method = optarg;
716 break;
717 case 'p':
718 path = optarg;
719 break;
720 case 'l':
721 log_filepath = optarg;
722 break;
723 case 'f':
724 pid_filepath = optarg;
725 break;
726 case 't':
727 state_dir = optarg;
728 break;
729 case 'v':
730 /* enable all log levels */
731 log_level = G_LOG_LEVEL_MASK;
732 break;
733 case 'V':
734 printf("QEMU Guest Agent %s\n", QEMU_VERSION);
735 return 0;
736 case 'd':
737 daemonize = 1;
738 break;
739 case 'b': {
740 char **list_head, **list;
741 if (is_help_option(optarg)) {
742 list_head = list = qmp_get_command_list();
743 while (*list != NULL) {
744 printf("%s\n", *list);
745 g_free(*list);
746 list++;
748 g_free(list_head);
749 return 0;
751 for (j = 0, i = 0, len = strlen(optarg); i < len; i++) {
752 if (optarg[i] == ',') {
753 optarg[i] = 0;
754 blacklist = g_list_append(blacklist, &optarg[j]);
755 j = i + 1;
758 if (j < i) {
759 blacklist = g_list_append(blacklist, &optarg[j]);
761 break;
763 #ifdef _WIN32
764 case 's':
765 service = optarg;
766 if (strcmp(service, "install") == 0) {
767 return ga_install_service(path, log_filepath);
768 } else if (strcmp(service, "uninstall") == 0) {
769 return ga_uninstall_service();
770 } else {
771 printf("Unknown service command.\n");
772 return EXIT_FAILURE;
774 break;
775 #endif
776 case 'h':
777 usage(argv[0]);
778 return 0;
779 case '?':
780 g_print("Unknown option, try '%s --help' for more information.\n",
781 argv[0]);
782 return EXIT_FAILURE;
786 s = g_malloc0(sizeof(GAState));
787 s->log_level = log_level;
788 s->log_file = stderr;
789 g_log_set_default_handler(ga_log, s);
790 g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR);
791 ga_enable_logging(s);
792 s->state_filepath_isfrozen = g_strdup_printf("%s/qga.state.isfrozen",
793 state_dir);
794 s->frozen = false;
795 #ifndef _WIN32
796 /* check if a previous instance of qemu-ga exited with filesystems' state
797 * marked as frozen. this could be a stale value (a non-qemu-ga process
798 * or reboot may have since unfrozen them), but better to require an
799 * uneeded unfreeze than to risk hanging on start-up
801 struct stat st;
802 if (stat(s->state_filepath_isfrozen, &st) == -1) {
803 /* it's okay if the file doesn't exist, but if we can't access for
804 * some other reason, such as permissions, there's a configuration
805 * that needs to be addressed. so just bail now before we get into
806 * more trouble later
808 if (errno != ENOENT) {
809 g_critical("unable to access state file at path %s: %s",
810 s->state_filepath_isfrozen, strerror(errno));
811 return EXIT_FAILURE;
813 } else {
814 g_warning("previous instance appears to have exited with frozen"
815 " filesystems. deferring logging/pidfile creation and"
816 " disabling non-fsfreeze-safe commands until"
817 " guest-fsfreeze-thaw is issued, or filesystems are"
818 " manually unfrozen and the file %s is removed",
819 s->state_filepath_isfrozen);
820 s->frozen = true;
822 #endif
824 if (ga_is_frozen(s)) {
825 if (daemonize) {
826 /* delay opening/locking of pidfile till filesystem are unfrozen */
827 s->deferred_options.pid_filepath = pid_filepath;
828 become_daemon(NULL);
830 if (log_filepath) {
831 /* delay opening the log file till filesystems are unfrozen */
832 s->deferred_options.log_filepath = log_filepath;
834 ga_disable_logging(s);
835 ga_disable_non_whitelisted();
836 } else {
837 if (daemonize) {
838 become_daemon(pid_filepath);
840 if (log_filepath) {
841 FILE *log_file = fopen(log_filepath, "a");
842 if (!log_file) {
843 g_critical("unable to open specified log file: %s",
844 strerror(errno));
845 goto out_bad;
847 s->log_file = log_file;
851 if (blacklist) {
852 s->blacklist = blacklist;
853 do {
854 g_debug("disabling command: %s", (char *)blacklist->data);
855 qmp_disable_command(blacklist->data);
856 blacklist = g_list_next(blacklist);
857 } while (blacklist);
859 s->command_state = ga_command_state_new();
860 ga_command_state_init(s, s->command_state);
861 ga_command_state_init_all(s->command_state);
862 json_message_parser_init(&s->parser, process_event);
863 ga_state = s;
864 #ifndef _WIN32
865 if (!register_signal_handlers()) {
866 g_critical("failed to register signal handlers");
867 goto out_bad;
869 #endif
871 s->main_loop = g_main_loop_new(NULL, false);
872 if (!channel_init(ga_state, method, path)) {
873 g_critical("failed to initialize guest agent channel");
874 goto out_bad;
876 #ifndef _WIN32
877 g_main_loop_run(ga_state->main_loop);
878 #else
879 if (daemonize) {
880 SERVICE_TABLE_ENTRY service_table[] = {
881 { (char *)QGA_SERVICE_NAME, service_main }, { NULL, NULL } };
882 StartServiceCtrlDispatcher(service_table);
883 } else {
884 g_main_loop_run(ga_state->main_loop);
886 #endif
888 ga_command_state_cleanup_all(ga_state->command_state);
889 ga_channel_free(ga_state->channel);
891 if (daemonize) {
892 unlink(pid_filepath);
894 return 0;
896 out_bad:
897 if (daemonize) {
898 unlink(pid_filepath);
900 return EXIT_FAILURE;