From nobody Thu Nov 6 12:13:08 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1540911211961329.58297327200603; Tue, 30 Oct 2018 07:53:31 -0700 (PDT) Received: from localhost ([::1]:53684 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gHVOW-00061l-Tx for importer@patchew.org; Tue, 30 Oct 2018 10:53:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46426) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gHVGV-0007n8-OM for qemu-devel@nongnu.org; Tue, 30 Oct 2018 10:45:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gHVGT-0000eS-CJ for qemu-devel@nongnu.org; Tue, 30 Oct 2018 10:45:07 -0400 Received: from mail-qt1-x82f.google.com ([2607:f8b0:4864:20::82f]:33587) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gHVGT-0000e2-6z for qemu-devel@nongnu.org; Tue, 30 Oct 2018 10:45:05 -0400 Received: by mail-qt1-x82f.google.com with SMTP id i15-v6so13760459qtr.0 for ; Tue, 30 Oct 2018 07:45:05 -0700 (PDT) Received: from localhost (76-251-165-188.lightspeed.austtx.sbcglobal.net. [76.251.165.188]) by smtp.gmail.com with ESMTPSA id e39-v6sm18843895qte.43.2018.10.30.07.45.02 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 30 Oct 2018 07:45:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=bQg6nndu4WqXPXDR4YukMSLGAa2GpB46yZFWniLRgXE=; b=hsZef++/z9tUPPghRJF5wLFcidmogrzdUpwd5Pg4KszLALls9k50lPYMixgb6zXp/5 +/St2uyDn6TG+TBCZOOs1ksft3OAQhK9ZQynbXj9YMVFOnL+kaY0iQqGSpx/vkf/VCDw uANSUeE8ryvm48dL6i+ihyPTD4uRnUloMb2/7ybAXtqcphuQP1LV6Oy5dZlo45hNwR/a HkB4EK4Whhrx99dTdH5euL3n40DJE75/GQy3T4ZwVIHggAUYt9JRO2PQEH7rqfrfwODE r5rlqne9wvY2g9thuLuHvwAZRls5Oxt9rlBBiJp6yac4Upnztl8AJEbbQgA6TNV06O4u LLRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=bQg6nndu4WqXPXDR4YukMSLGAa2GpB46yZFWniLRgXE=; b=Ryi+xCh1Nh4BIQNJHAnb9wGp6l60b9ZYU0X26bb9oT5g4/Epnker6Iya+xqFm4OgH6 DzzCPr9Gxz99vVE1YxQ0qcPFTnCf+Z33A70lk8FcO4R7qNhWNX4c0DwTxH9+FnAMxteH 75Ovs+WB2iJBCY1kibKCE+71qnTIjH2jxVRMF1xYzfYr1bXY+VHbDVA7ZBxWebRgjJvz T+ECDtpNHAZtv1MjBuLRXzdx9Hj4IzPoWF7N0Ago/mrPP04XZhYA32V05+sOizn6J4HW vDJDXw04p9tsWLNF+CXxqHiRSU3jcaGzQnocaQX2NFHkt4Q6nZsxVaVnhoSLlgLYgrEG tJPQ== X-Gm-Message-State: AGRZ1gLNfj6oh+FGQYSO8VWiYsY1A0QcklEhEfyHa2Xiwo+ED7Lesn1m kwHD5NTBqm/XqX8BJaORa1Q+b7Hr X-Google-Smtp-Source: AJdET5cjHIFPBwIzc933B8P0Jq87s+VJEXVG2hituDD80T0Ju3t1om/AQcjyAFLkI/Yf5lRs8MO9JQ== X-Received: by 2002:a0c:99e5:: with SMTP id y37-v6mr17039358qve.133.1540910703889; Tue, 30 Oct 2018 07:45:03 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Tue, 30 Oct 2018 09:43:55 -0500 Message-Id: <20181030144358.23144-22-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181030144358.23144-1-mdroth@linux.vnet.ibm.com> References: <20181030144358.23144-1-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::82f Subject: [Qemu-devel] [PULL 21/24] qga: add --retry-path option for re-initializing channel on failure X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This adds an option to instruct the agent to periodically attempt re-opening the communication channel after a channel error has occurred. The main use-case for this is providing an OS-independent way of allowing the agent to survive situations like hotplug/unplug of the communication channel, or initial guest set up where the agent may be installed/started prior to the installation of the channel device's driver. There are nicer ways of implementing this functionality via things like systemd services, but this option is useful for platforms like *BSD/w32. Currently a channel error will result in the GSource for that channel being removed from the GMainLoop, but the main loop continuing to run. That behavior results in a dead loop when --retry-path isn't set, and prevents us from knowing when to attempt re-opening the channel when it is set, so we also force the loop to exit as part of this patch. Signed-off-by: Michael Roth --- qga/main.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 8 deletions(-) diff --git a/qga/main.c b/qga/main.c index 761007deb4..506a314140 100644 --- a/qga/main.c +++ b/qga/main.c @@ -58,6 +58,7 @@ #endif #define QGA_SENTINEL_BYTE 0xFF #define QGA_CONF_DEFAULT CONFIG_QEMU_CONFDIR G_DIR_SEPARATOR_S "qemu-ga.co= nf" +#define QGA_RETRY_INTERVAL 5 =20 static struct { const char *state_dir; @@ -98,6 +99,7 @@ struct GAState { GAPersistentState pstate; GAConfig *config; int socket_activation; + bool force_exit; }; =20 struct GAState *ga_state; @@ -120,6 +122,7 @@ DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD typ= e, LPVOID data, VOID WINAPI service_main(DWORD argc, TCHAR *argv[]); #endif static int run_agent(GAState *s); +static void stop_agent(GAState *s, bool requested); =20 static void init_dfl_pathnames(void) @@ -168,9 +171,7 @@ static void quit_handler(int sig) } g_debug("received signal num %d, quitting", sig); =20 - if (g_main_loop_is_running(ga_state->main_loop)) { - g_main_loop_quit(ga_state->main_loop); - } + stop_agent(ga_state, true); } =20 #ifndef _WIN32 @@ -255,6 +256,10 @@ QEMU_COPYRIGHT "\n" " to list available RPCs)\n" " -D, --dump-conf dump a qemu-ga config file based on current config\n" " options / command-line parameters to stdout\n" +" -r, --retry-path attempt re-opening path if it's unavailable or closed= \n" +" due to an error which may be recoverable in the futur= e\n" +" (virtio-serial driver re-install, serial device hot\n" +" plug/unplug, etc.)\n" " -h, --help display this help and exit\n" "\n" QEMU_HELP_BOTTOM "\n" @@ -614,6 +619,7 @@ static gboolean channel_event_cb(GIOCondition condition= , gpointer data) switch (status) { case G_IO_STATUS_ERROR: g_warning("error reading channel"); + stop_agent(s, false); return false; case G_IO_STATUS_NORMAL: buf[count] =3D 0; @@ -927,6 +933,7 @@ struct GAConfig { int daemonize; GLogLevelFlags log_level; int dumpconf; + bool retry_path; }; =20 static void config_load(GAConfig *config) @@ -976,6 +983,10 @@ static void config_load(GAConfig *config) /* enable all log levels */ config->log_level =3D G_LOG_LEVEL_MASK; } + if (g_key_file_has_key(keyfile, "general", "retry-path", NULL)) { + config->retry_path =3D + g_key_file_get_boolean(keyfile, "general", "retry-path", &gerr= ); + } if (g_key_file_has_key(keyfile, "general", "blacklist", NULL)) { config->bliststr =3D g_key_file_get_string(keyfile, "general", "blacklist", &gerr); @@ -1037,6 +1048,8 @@ static void config_dump(GAConfig *config) g_key_file_set_string(keyfile, "general", "statedir", config->state_di= r); g_key_file_set_boolean(keyfile, "general", "verbose", config->log_level =3D=3D G_LOG_LEVEL_MASK); + g_key_file_set_boolean(keyfile, "general", "retry-path", + config->retry_path); tmp =3D list_join(config->blacklist, ','); g_key_file_set_string(keyfile, "general", "blacklist", tmp); g_free(tmp); @@ -1055,7 +1068,7 @@ static void config_dump(GAConfig *config) =20 static void config_parse(GAConfig *config, int argc, char **argv) { - const char *sopt =3D "hVvdm:p:l:f:F::b:s:t:D"; + const char *sopt =3D "hVvdm:p:l:f:F::b:s:t:Dr"; int opt_ind =3D 0, ch; const struct option lopt[] =3D { { "help", 0, NULL, 'h' }, @@ -1075,6 +1088,7 @@ static void config_parse(GAConfig *config, int argc, = char **argv) { "service", 1, NULL, 's' }, #endif { "statedir", 1, NULL, 't' }, + { "retry-path", 0, NULL, 'r' }, { NULL, 0, NULL, 0 } }; =20 @@ -1119,6 +1133,9 @@ static void config_parse(GAConfig *config, int argc, = char **argv) case 'D': config->dumpconf =3D 1; break; + case 'r': + config->retry_path =3D true; + break; case 'b': { if (is_help_option(optarg)) { qmp_for_each_command(&ga_commands, ga_print_cmd, NULL); @@ -1322,9 +1339,6 @@ static void cleanup_agent(GAState *s) ga_command_state_free(s->command_state); json_message_parser_destroy(&s->parser); } - if (s->channel) { - ga_channel_free(s->channel); - } g_free(s->pstate_filepath); g_free(s->state_filepath_isfrozen); if (s->main_loop) { @@ -1334,7 +1348,7 @@ static void cleanup_agent(GAState *s) ga_state =3D NULL; } =20 -static int run_agent(GAState *s) +static int run_agent_once(GAState *s) { if (!channel_init(s, s->config->method, s->config->channel_path, s->socket_activation ? FIRST_SOCKET_ACTIVATION_FD : = -1)) { @@ -1344,9 +1358,41 @@ static int run_agent(GAState *s) =20 g_main_loop_run(ga_state->main_loop); =20 + if (s->channel) { + ga_channel_free(s->channel); + } + return EXIT_SUCCESS; } =20 +static int run_agent(GAState *s) +{ + int ret =3D EXIT_SUCCESS; + + s->force_exit =3D false; + + do { + ret =3D run_agent_once(s); + if (s->config->retry_path && !s->force_exit) { + g_warning("agent stopped unexpectedly, restarting..."); + sleep(QGA_RETRY_INTERVAL); + } + } while (s->config->retry_path && !s->force_exit); + + return ret; +} + +static void stop_agent(GAState *s, bool requested) +{ + if (!s->force_exit) { + s->force_exit =3D requested; + } + + if (g_main_loop_is_running(s->main_loop)) { + g_main_loop_quit(s->main_loop); + } +} + int main(int argc, char **argv) { int ret =3D EXIT_SUCCESS; --=20 2.17.1