Lines Matching +full:- +full:- +full:disable +full:- +full:guest +full:- +full:agent
2 * QEMU Guest Agent
11 * See the COPYING file in the top-level directory.
21 #include "qemu/help-texts.h"
22 #include "qobject/json-parser.h"
25 #include "guest-agent-core.h"
26 #include "qga-qapi-init-commands.h"
33 #include "qemu-version.h"
36 #include "qga/service-win32.h"
37 #include "qga/vss-win32.h"
39 #include "commands-common.h"
45 #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0"
51 #define QGA_STATE_RELATIVE_DIR "qemu-ga"
55 #define QGA_FSFREEZE_HOOK_DEFAULT CONFIG_QEMU_CONFDIR "/fsfreeze-hook"
58 #define QGA_CONF_DEFAULT CONFIG_QEMU_CONFDIR G_DIR_SEPARATOR_S "qemu-ga.conf"
133 "guest-ping",
134 "guest-info",
135 "guest-sync",
136 "guest-sync-delimited",
137 "guest-fsfreeze-status",
138 "guest-fsfreeze-thaw",
159 dfl_pathnames.pidfile = g_build_filename(state, QGA_STATE_RELATIVE_DIR, "qemu-ga.pid", NULL); in init_dfl_pathnames()
167 * also a very likely chance killing the agent before unfreezing in quit_handler()
211 if (ret == -1) { in register_signal_handlers()
215 if (ret == -1) { in register_signal_handlers()
228 /* TODO: use this in place of all post-fork() fclose(std*) callers */
254 "Usage: %s [-m <method> -p <path>] [<options>]\n" in usage()
255 "QEMU Guest Agent " QEMU_FULL_VERSION "\n" in usage()
258 " -c, --config=PATH configuration file path (default is\n" in usage()
259 " %s/qemu-ga.conf\n" in usage()
261 " -m, --method transport method: one of unix-listen, virtio-serial,\n" in usage()
262 " isa-serial, or vsock-listen (virtio-serial is the default)\n" in usage()
263 " -p, --path device/socket path (the default for virtio-serial is:\n" in usage()
265 " the default for isa-serial is:\n" in usage()
267 " Socket addresses for vsock-listen are written as\n" in usage()
269 " -l, --logfile set logfile path, logs to stderr by default\n" in usage()
270 " -f, --pidfile specify pidfile (default is %s)\n" in usage()
272 " -F, --fsfreeze-hook\n" in usage()
277 " If using -F with an argument, do not follow -F with a\n" in usage()
279 " (for example: -F/var/run/fsfreezehook.sh)\n" in usage()
281 " -t, --statedir specify dir to store state information (absolute paths\n" in usage()
283 " -v, --verbose log extra debugging information\n" in usage()
284 " -V, --version print version information and exit\n" in usage()
285 " -d, --daemonize become a daemon\n" in usage()
287 " -s, --service service commands: install, uninstall, vss-install, vss-uninstall\n" in usage()
289 " -b, --block-rpcs comma-separated list of RPCs to disable (no spaces,\n" in usage()
290 " use \"--block-rpcs=help\" to list available RPCs)\n" in usage()
291 " -a, --allow-rpcs comma-separated list of RPCs to enable (no spaces,\n" in usage()
292 " use \"--allow-rpcs=help\" to list available RPCs)\n" in usage()
293 " -D, --dump-conf dump a qemu-ga config file based on current config\n" in usage()
294 " options / command-line parameters to stdout\n" in usage()
295 " -r, --retry-path attempt re-opening path if it's unavailable or closed\n" in usage()
297 " (virtio-serial driver re-install, serial device hot\n" in usage()
299 " -h, --help display this help and exit\n" in usage()
332 return s->logging_enabled; in ga_logging_enabled()
337 s->logging_enabled = false; in ga_disable_logging()
342 s->logging_enabled = true; in ga_enable_logging()
392 ReportEvent(s->event_log, glib_log_level_to_system(level), in ga_log()
395 } else if (level & s->log_level) { in ga_log()
398 fprintf(s->log_file, "%s: %s: %s\n", nowstr, level_str, msg); in ga_log()
399 fflush(s->log_file); in ga_log()
405 s->delimit_response = true; in ga_set_response_delimited()
429 GAConfig *config = state->config; in ga_command_is_allowed()
434 if (config->allowedrpcs) { in ga_command_is_allowed()
436 * If an allow-list is given, this changes the fallback in ga_command_is_allowed()
441 if (g_list_find_custom(config->allowedrpcs, name, ga_strcmp) != NULL) { in ga_command_is_allowed()
450 if (config->blockedrpcs) { in ga_command_is_allowed()
451 if (g_list_find_custom(config->blockedrpcs, name, ga_strcmp) != NULL) { in ga_command_is_allowed()
460 if (state->frozen) { in ga_command_is_allowed()
502 if (fd == -1) { in ga_create_file()
513 if (ret == -1) { in ga_delete_file()
523 return s->frozen; in ga_is_frozen()
532 s->frozen = true; in ga_set_frozen()
533 if (!ga_create_file(s->state_filepath_isfrozen)) { in ga_set_frozen()
535 s->state_filepath_isfrozen); in ga_set_frozen()
550 if (s->deferred_options.log_filepath) { in ga_unset_frozen()
551 s->log_file = ga_open_logfile(s->deferred_options.log_filepath); in ga_unset_frozen()
552 if (!s->log_file) { in ga_unset_frozen()
553 s->log_file = stderr; in ga_unset_frozen()
555 s->deferred_options.log_filepath = NULL; in ga_unset_frozen()
558 g_warning("logging re-enabled due to filesystem unfreeze"); in ga_unset_frozen()
559 if (s->deferred_options.pid_filepath) { in ga_unset_frozen()
562 if (!qemu_write_pidfile(s->deferred_options.pid_filepath, &err)) { in ga_unset_frozen()
566 s->deferred_options.pid_filepath = NULL; in ga_unset_frozen()
569 /* enable all disabled, non-blocked and allowed commands */ in ga_unset_frozen()
570 s->frozen = false; in ga_unset_frozen()
571 if (!ga_delete_file(s->state_filepath_isfrozen)) { in ga_unset_frozen()
573 s->state_filepath_isfrozen); in ga_unset_frozen()
581 return s->fsfreeze_hook; in ga_fsfreeze_hook()
636 g_assert(s->channel); in send_response()
644 return -EINVAL; in send_response()
647 if (s->delimit_response) { in send_response()
648 s->delimit_response = false; in send_response()
653 status = ga_channel_write_all(s->channel, response->str, response->len); in send_response()
656 return -EIO; in send_response()
682 g_warning("error sending error response: %s", strerror(-ret)); in process_event()
694 GIOStatus status = ga_channel_read(s->channel, buf, QGA_READ_COUNT_DEFAULT, &count); in channel_event_cb()
703 json_message_parser_feed(&s->parser, (char *)buf, (int)count); in channel_event_cb()
707 if (!s->virtio) { in channel_event_cb()
713 * host-side chardev. sleep a bit to mitigate this in channel_event_cb()
715 if (s->virtio) { in channel_event_cb()
731 if (strcmp(method, "virtio-serial") == 0) { in channel_init()
732 s->virtio = true; /* virtio requires special handling in some cases */ in channel_init()
734 } else if (strcmp(method, "isa-serial") == 0) { in channel_init()
736 } else if (strcmp(method, "unix-listen") == 0) { in channel_init()
738 } else if (strcmp(method, "vsock-listen") == 0) { in channel_init()
745 s->channel = ga_channel_new(channel_method, path, listen_fd, in channel_init()
747 if (!s->channel) { in channel_init()
748 g_critical("failed to create guest agent channel"); in channel_init()
761 if (broadcast_header->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) { in handle_serial_device_events()
765 /* Start QEMU-ga's service */ in handle_serial_device_events()
766 if (!SetEvent(ga_state->wakeup_event)) { in handle_serial_device_events()
774 /* Stop QEMU-ga's service */ in handle_serial_device_events()
775 if (!ResetEvent(ga_state->wakeup_event)) { in handle_serial_device_events()
790 GAService *service = &ga_state->service; in service_ctrl_handler()
796 SetEvent(ga_state->wakeup_event); in service_ctrl_handler()
797 service->status.dwCurrentState = SERVICE_STOP_PENDING; in service_ctrl_handler()
798 SetServiceStatus(service->status_handle, &service->status); in service_ctrl_handler()
812 GAService *service = &ga_state->service; in service_main()
814 service->status_handle = RegisterServiceCtrlHandlerEx(QGA_SERVICE_NAME, in service_main()
817 if (service->status_handle == 0) { in service_main()
822 service->status.dwServiceType = SERVICE_WIN32; in service_main()
823 service->status.dwCurrentState = SERVICE_RUNNING; in service_main()
824 service->status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; in service_main()
825 service->status.dwWin32ExitCode = NO_ERROR; in service_main()
826 service->status.dwServiceSpecificExitCode = NO_ERROR; in service_main()
827 service->status.dwCheckPoint = 0; in service_main()
828 service->status.dwWaitHint = 0; in service_main()
835 service->device_notification_handle = in service_main()
836 RegisterDeviceNotification(service->status_handle, in service_main()
838 if (!service->device_notification_handle) { in service_main()
842 SetServiceStatus(service->status_handle, &service->status); in service_main()
846 UnregisterDeviceNotification(service->device_notification_handle); in service_main()
847 service->status.dwCurrentState = SERVICE_STOPPED; in service_main()
848 SetServiceStatus(service->status_handle, &service->status); in service_main()
855 pstate->fd_counter = QGA_PSTATE_DEFAULT_FD_COUNTER; in set_persistent_state_defaults()
866 * values. so load them now, and ignore any errors in accessing key-value in persistent_state_from_keyfile()
872 pstate->fd_counter = in persistent_state_from_keyfile()
883 g_key_file_set_integer(keyfile, "global", "fd_counter", pstate->fd_counter); in persistent_state_to_keyfile()
901 gerr->message); in write_persistent_state()
909 path, gerr->message); in write_persistent_state()
935 if (stat(path, &st) == -1) { in read_persistent_state()
953 * update the agent from a version that didn't use a key store in read_persistent_state()
954 * while qemu-ga thought the filesystem was frozen, or in read_persistent_state()
956 * to restarting the agent. in this case we go ahead and defer in read_persistent_state()
977 path, gerr->message); in read_persistent_state()
999 g_assert(s->pstate_filepath); in ga_get_fd_handle()
1007 handle = s->pstate.fd_counter++; in ga_get_fd_handle()
1009 /* This should never happen on a reasonable timeframe, as guest-file-open in ga_get_fd_handle()
1011 if (s->pstate.fd_counter == INT64_MAX) { in ga_get_fd_handle()
1015 if (!write_persistent_state(&s->pstate, s->pstate_filepath)) { in ga_get_fd_handle()
1017 return -1; in ga_get_fd_handle()
1034 strv = g_strsplit(str, delim, -1); in split_list()
1054 config->daemonize = in config_load()
1058 config->method = in config_load()
1062 config->channel_path = in config_load()
1066 config->log_filepath = in config_load()
1070 config->pid_filepath = in config_load()
1074 if (g_key_file_has_key(keyfile, "general", "fsfreeze-hook", NULL)) { in config_load()
1075 config->fsfreeze_hook = in config_load()
1077 "general", "fsfreeze-hook", &gerr); in config_load()
1081 config->state_dir = in config_load()
1087 config->log_level = G_LOG_LEVEL_MASK; in config_load()
1089 if (g_key_file_has_key(keyfile, "general", "retry-path", NULL)) { in config_load()
1090 config->retry_path = in config_load()
1091 g_key_file_get_boolean(keyfile, "general", "retry-path", &gerr); in config_load()
1094 if (g_key_file_has_key(keyfile, "general", "block-rpcs", NULL)) { in config_load()
1095 config->bliststr = in config_load()
1096 g_key_file_get_string(keyfile, "general", "block-rpcs", &gerr); in config_load()
1097 config->blockedrpcs = g_list_concat(config->blockedrpcs, in config_load()
1098 split_list(config->bliststr, ",")); in config_load()
1100 if (g_key_file_has_key(keyfile, "general", "allow-rpcs", NULL)) { in config_load()
1101 config->aliststr = in config_load()
1102 g_key_file_get_string(keyfile, "general", "allow-rpcs", &gerr); in config_load()
1103 config->allowedrpcs = g_list_concat(config->allowedrpcs, in config_load()
1104 split_list(config->aliststr, ",")); in config_load()
1110 !(gerr->domain == G_FILE_ERROR && gerr->code == G_FILE_ERROR_NOENT))) { in config_load()
1112 confpath, gerr->message); in config_load()
1123 str = g_string_append(str, (gchar *)list->data); in list_join()
1142 g_key_file_set_boolean(keyfile, "general", "daemon", config->daemonize); in config_dump()
1143 g_key_file_set_string(keyfile, "general", "method", config->method); in config_dump()
1144 if (config->channel_path) { in config_dump()
1145 g_key_file_set_string(keyfile, "general", "path", config->channel_path); in config_dump()
1147 if (config->log_filepath) { in config_dump()
1149 config->log_filepath); in config_dump()
1151 g_key_file_set_string(keyfile, "general", "pidfile", config->pid_filepath); in config_dump()
1153 if (config->fsfreeze_hook) { in config_dump()
1154 g_key_file_set_string(keyfile, "general", "fsfreeze-hook", in config_dump()
1155 config->fsfreeze_hook); in config_dump()
1158 g_key_file_set_string(keyfile, "general", "statedir", config->state_dir); in config_dump()
1160 config->log_level == G_LOG_LEVEL_MASK); in config_dump()
1161 g_key_file_set_boolean(keyfile, "general", "retry-path", in config_dump()
1162 config->retry_path); in config_dump()
1163 tmp = list_join(config->blockedrpcs, ','); in config_dump()
1164 g_key_file_set_string(keyfile, "general", "block-rpcs", tmp); in config_dump()
1166 tmp = list_join(config->allowedrpcs, ','); in config_dump()
1167 g_key_file_set_string(keyfile, "general", "allow-rpcs", tmp); in config_dump()
1172 g_critical("Failed to dump keyfile: %s", error->message); in config_dump()
1190 { "dump-conf", 0, NULL, 'D' }, in config_parse()
1194 { "fsfreeze-hook", 2, NULL, 'F' }, in config_parse()
1200 { "block-rpcs", 1, NULL, 'b' }, in config_parse()
1201 { "allow-rpcs", 1, NULL, 'a' }, in config_parse()
1206 { "retry-path", 0, NULL, 'r' }, in config_parse()
1213 while ((ch = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { in config_parse()
1230 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { in config_parse()
1233 g_free(config->method); in config_parse()
1234 config->method = g_strdup(optarg); in config_parse()
1237 g_free(config->channel_path); in config_parse()
1238 config->channel_path = g_strdup(optarg); in config_parse()
1241 g_free(config->log_filepath); in config_parse()
1242 config->log_filepath = g_strdup(optarg); in config_parse()
1245 g_free(config->pid_filepath); in config_parse()
1246 config->pid_filepath = g_strdup(optarg); in config_parse()
1250 g_free(config->fsfreeze_hook); in config_parse()
1251 … config->fsfreeze_hook = optarg ? g_strdup(optarg) : get_relocated_path(QGA_FSFREEZE_HOOK_DEFAULT); in config_parse()
1255 g_free(config->state_dir); in config_parse()
1256 config->state_dir = g_strdup(optarg); in config_parse()
1260 config->log_level = G_LOG_LEVEL_MASK; in config_parse()
1263 printf("QEMU Guest Agent %s\n", QEMU_VERSION); in config_parse()
1266 config->daemonize = 1; in config_parse()
1269 config->dumpconf = 1; in config_parse()
1272 config->retry_path = true; in config_parse()
1279 config->blockedrpcs = g_list_concat(config->blockedrpcs, in config_parse()
1288 config->allowedrpcs = g_list_concat(config->allowedrpcs, in config_parse()
1294 config->service = optarg; in config_parse()
1295 if (strcmp(config->service, "install") == 0) { in config_parse()
1299 if (ga_install_service(config->channel_path, in config_parse()
1300 config->log_filepath, config->state_dir)) { in config_parse()
1304 } else if (strcmp(config->service, "uninstall") == 0) { in config_parse()
1307 } else if (strcmp(config->service, "vss-install") == 0) { in config_parse()
1312 } else if (strcmp(config->service, "vss-uninstall") == 0) { in config_parse()
1325 g_print("Unknown option, try '%s --help' for more information.\n", in config_parse()
1334 g_free(config->method); in config_free()
1335 g_free(config->log_filepath); in config_free()
1336 g_free(config->pid_filepath); in config_free()
1337 g_free(config->state_dir); in config_free()
1338 g_free(config->channel_path); in config_free()
1339 g_free(config->bliststr); in config_free()
1340 g_free(config->aliststr); in config_free()
1342 g_free(config->fsfreeze_hook); in config_free()
1344 g_list_free_full(config->blockedrpcs, g_free); in config_free()
1345 g_list_free_full(config->allowedrpcs, g_free); in config_free()
1352 /* check if a previous instance of qemu-ga exited with filesystems' state in check_is_frozen()
1353 * marked as frozen. this could be a stale value (a non-qemu-ga process in check_is_frozen()
1355 * unneeded unfreeze than to risk hanging on start-up in check_is_frozen()
1358 if (stat(s->state_filepath_isfrozen, &st) == -1) { in check_is_frozen()
1366 s->state_filepath_isfrozen, strerror(errno)); in check_is_frozen()
1372 " disabling non-fsfreeze-safe commands until" in check_is_frozen()
1373 " guest-fsfreeze-thaw is issued, or filesystems are" in check_is_frozen()
1375 s->state_filepath_isfrozen); in check_is_frozen()
1388 s->log_level = config->log_level; in initialize_agent()
1389 s->log_file = stderr; in initialize_agent()
1391 s->fsfreeze_hook = config->fsfreeze_hook; in initialize_agent()
1393 s->pstate_filepath = g_strdup_printf("%s/qga.state", config->state_dir); in initialize_agent()
1394 s->state_filepath_isfrozen = g_strdup_printf("%s/qga.state.isfrozen", in initialize_agent()
1395 config->state_dir); in initialize_agent()
1396 s->frozen = check_is_frozen(s); in initialize_agent()
1402 g_debug("Guest agent version %s started", QEMU_FULL_VERSION); in initialize_agent()
1405 s->event_log = RegisterEventSource(NULL, "qemu-ga"); in initialize_agent()
1406 if (!s->event_log) { in initialize_agent()
1418 if (g_mkdir_with_parents(config->state_dir, S_IRWXU) == -1) { in initialize_agent()
1420 " '%s': %s", config->state_dir, strerror(errno)); in initialize_agent()
1430 if (config->daemonize) { in initialize_agent()
1432 s->deferred_options.pid_filepath = config->pid_filepath; in initialize_agent()
1434 if (config->log_filepath) { in initialize_agent()
1436 s->deferred_options.log_filepath = config->log_filepath; in initialize_agent()
1440 if (config->log_filepath) { in initialize_agent()
1441 FILE *log_file = ga_open_logfile(config->log_filepath); in initialize_agent()
1447 s->log_file = log_file; in initialize_agent()
1452 if (!read_persistent_state(&s->pstate, in initialize_agent()
1453 s->pstate_filepath, in initialize_agent()
1459 s->command_state = ga_command_state_new(); in initialize_agent()
1460 ga_command_state_init(s, s->command_state); in initialize_agent()
1461 ga_command_state_init_all(s->command_state); in initialize_agent()
1462 json_message_parser_init(&s->parser, process_event, s, NULL); in initialize_agent()
1471 s->main_loop = g_main_loop_new(NULL, false); in initialize_agent()
1473 s->config = config; in initialize_agent()
1474 s->socket_activation = socket_activation; in initialize_agent()
1477 s->wakeup_event = CreateEvent(NULL, TRUE, FALSE, TEXT("WakeUp")); in initialize_agent()
1478 if (s->wakeup_event == NULL) { in initialize_agent()
1486 if (!channel_init(s, s->config->method, s->config->channel_path, in initialize_agent()
1487 s->socket_activation ? FIRST_SOCKET_ACTIVATION_FD : -1)) { in initialize_agent()
1488 g_critical("failed to initialize guest agent channel"); in initialize_agent()
1492 if (config->daemonize) { in initialize_agent()
1496 become_daemon(config->pid_filepath); in initialize_agent()
1507 CloseHandle(s->wakeup_event); in cleanup_agent()
1508 CloseHandle(s->event_log); in cleanup_agent()
1510 if (s->command_state) { in cleanup_agent()
1511 ga_command_state_cleanup_all(s->command_state); in cleanup_agent()
1512 ga_command_state_free(s->command_state); in cleanup_agent()
1513 json_message_parser_destroy(&s->parser); in cleanup_agent()
1515 g_free(s->pstate_filepath); in cleanup_agent()
1516 g_free(s->state_filepath_isfrozen); in cleanup_agent()
1517 if (s->main_loop) { in cleanup_agent()
1518 g_main_loop_unref(s->main_loop); in cleanup_agent()
1526 if (!s->channel && in run_agent_once()
1527 channel_init(s, s->config->method, s->config->channel_path, in run_agent_once()
1528 s->socket_activation ? FIRST_SOCKET_ACTIVATION_FD : -1)) { in run_agent_once()
1529 g_critical("failed to initialize guest agent channel"); in run_agent_once()
1533 g_main_loop_run(s->main_loop); in run_agent_once()
1535 if (s->channel) { in run_agent_once()
1536 ga_channel_free(s->channel); in run_agent_once()
1537 s->channel = NULL; in run_agent_once()
1551 dwWaitResult = WaitForSingleObject(s->wakeup_event, INFINITE); in wait_for_channel_availability()
1568 s->force_exit = false; in run_agent()
1572 if (s->config->retry_path && !s->force_exit) { in run_agent()
1573 g_warning("agent stopped unexpectedly, restarting..."); in run_agent()
1576 } while (s->config->retry_path && !s->force_exit); in run_agent()
1583 if (!s->force_exit) { in stop_agent()
1584 s->force_exit = requested; in stop_agent()
1587 if (g_main_loop_is_running(s->main_loop)) { in stop_agent()
1588 g_main_loop_quit(s->main_loop); in stop_agent()
1599 config->log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL; in main()
1607 if (config->pid_filepath == NULL) { in main()
1608 config->pid_filepath = g_strdup(dfl_pathnames.pidfile); in main()
1611 if (config->state_dir == NULL) { in main()
1612 config->state_dir = g_strdup(dfl_pathnames.state_dir); in main()
1615 if (config->method == NULL) { in main()
1616 config->method = g_strdup("virtio-serial"); in main()
1621 g_critical("qemu-ga only supports listening on one socket"); in main()
1627 g_free(config->method); in main()
1628 g_free(config->channel_path); in main()
1629 config->method = NULL; in main()
1630 config->channel_path = NULL; in main()
1634 if (addr->type == SOCKET_ADDRESS_TYPE_UNIX) { in main()
1635 config->method = g_strdup("unix-listen"); in main()
1636 } else if (addr->type == SOCKET_ADDRESS_TYPE_VSOCK) { in main()
1637 config->method = g_strdup("vsock-listen"); in main()
1643 if (!config->method) { in main()
1647 } else if (config->channel_path == NULL) { in main()
1648 if (strcmp(config->method, "virtio-serial") == 0) { in main()
1649 /* try the default path for the virtio-serial port */ in main()
1650 config->channel_path = g_strdup(QGA_VIRTIO_PATH_DEFAULT); in main()
1651 } else if (strcmp(config->method, "isa-serial") == 0) { in main()
1652 /* try the default path for the serial port - COM1 */ in main()
1653 config->channel_path = g_strdup(QGA_SERIAL_PATH_DEFAULT); in main()
1660 if (config->dumpconf) { in main()
1668 g_critical("error initializing guest agent"); in main()
1673 if (config->daemonize) { in main()
1688 if (config->daemonize) { in main()
1689 unlink(config->pid_filepath); in main()