From nobody Thu Nov 6 12:13:06 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 (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1540911612194185.08346946071651; Tue, 30 Oct 2018 08:00:12 -0700 (PDT) Received: from localhost ([::1]:53720 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gHVV4-0006Jq-M4 for importer@patchew.org; Tue, 30 Oct 2018 11:00:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46363) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gHVGJ-0007dJ-Fj for qemu-devel@nongnu.org; Tue, 30 Oct 2018 10:44:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gHVGH-0000SL-Q5 for qemu-devel@nongnu.org; Tue, 30 Oct 2018 10:44:55 -0400 Received: from mail-qk1-x732.google.com ([2607:f8b0:4864:20::732]:34007) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gHVGH-0000Rb-Kf for qemu-devel@nongnu.org; Tue, 30 Oct 2018 10:44:53 -0400 Received: by mail-qk1-x732.google.com with SMTP id a132so7407382qkg.1 for ; Tue, 30 Oct 2018 07:44:53 -0700 (PDT) Received: from localhost (76-251-165-188.lightspeed.austtx.sbcglobal.net. [76.251.165.188]) by smtp.gmail.com with ESMTPSA id c8-v6sm14900599qkb.81.2018.10.30.07.44.50 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 30 Oct 2018 07:44:50 -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 :mime-version:content-transfer-encoding; bh=tXnxRiw5dgfWTyLrIDra9WPBMKy8b+BOWuJZw8waj94=; b=czzKtheDBstZq+IDzsmN/sT/sIRgq2lOhFml265/aRUn1WnqDzbDaL/f56WvzDuRzE eOlUyYjgIjjbnQjNTCTTJiwxUZ9iHOJKd+Cnu3ulGMwwM0s7pW4hvb4NH99dC11EKnRL RIkrcxpo3mOL4+IeByc2VaIuwaDlF9YQN3729A7lZWzfs+M8Qo4EFcrAjgtjfSyqBTeU QpqvGxZ2l+NTarCgkVCFzzEfAuf+EBU/YGJCczlJn/RwqLphi0s9D46KryDw0w18sqH7 FEtozTO9wfsvSqEo1CJSxJCvGo3WVAQwKXrfN8gx5/MzWuypQvhWCvtgd6+SBRvGU4sM C5sQ== 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:mime-version:content-transfer-encoding; bh=tXnxRiw5dgfWTyLrIDra9WPBMKy8b+BOWuJZw8waj94=; b=F8wd+0fbn6ORj7kUgoX4G7QVakkXBFskS3KMpybRubH/610fxakASpnr1ih/qfH9uN Gl5tbJ2y0YWz8lgxFvh/hX0/Roxu/8yTFwihAsiSlmrX8R/Upi/D/UjpbVbAPJvhEagP xQ3I3LxpckX4gbNV9ITExQ0zzhJkq4Hto07DFWekVACtiZ+Ozuj4VVivq+FvY/8CcC6n 9297Pd6lgv4IsYXB6ZGcCeXs0KY3OvN38dfe6ozr1wh8P8nq67DcHIYLokT+9eTQzsF3 k4tyWfBPwPccIu4etroJVB5nDH4W4+bDLL5TexRDmnHPIQo4emO6ItF+ftR26ekqvyfe YaBA== X-Gm-Message-State: AGRZ1gKjePI28iuL3hQvJEYAiuxyepQm3XQ0j2mJqudqQhqQk2Q/itj4 YySQV55rfVHCcWzoELdnPWjcWlbD X-Google-Smtp-Source: AJdET5dYS0GG2RX5LxoQdb+ACh+FaMAHFcHopUaA0w+MV6tH/gAPfJ16IvZk/ZvgMtNsa6jhSydOIA== X-Received: by 2002:a37:153:: with SMTP id 80mr5617852qkb.122.1540910691978; Tue, 30 Oct 2018 07:44:51 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Tue, 30 Oct 2018 09:43:52 -0500 Message-Id: <20181030144358.23144-19-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> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::732 Subject: [Qemu-devel] [PULL 18/24] qga: group agent init/cleanup init separate routines 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) This patch better separates the init/cleanup routines out into separate functions to make the start-up procedure a bit easier to follow. This will be useful when we eventually break out the actual start/stop of the agent's main loop into separates routines that can be called multiple times after the init phase. Signed-off-by: Michael Roth Reviewed-by: Marc-Andr=C3=A9 Lureau Signed-off-by: Michael Roth --- qga/main.c | 82 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/qga/main.c b/qga/main.c index afcd268ee3..ab3eaa5163 100644 --- a/qga/main.c +++ b/qga/main.c @@ -1211,9 +1211,21 @@ static bool check_is_frozen(GAState *s) return false; } =20 -static int run_agent(GAState *s, GAConfig *config, int socket_activation) +static GAState *initialize_agent(GAConfig *config) { - ga_state =3D s; + GAState *s =3D g_new0(GAState, 1); + + g_assert(ga_state =3D=3D NULL); + + s->log_level =3D config->log_level; + s->log_file =3D stderr; +#ifdef CONFIG_FSFREEZE + s->fsfreeze_hook =3D config->fsfreeze_hook; +#endif + s->pstate_filepath =3D g_strdup_printf("%s/qga.state", config->state_d= ir); + s->state_filepath_isfrozen =3D g_strdup_printf("%s/qga.state.isfrozen", + config->state_dir); + s->frozen =3D check_is_frozen(s); =20 g_log_set_default_handler(ga_log, s); g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR); @@ -1229,7 +1241,7 @@ static int run_agent(GAState *s, GAConfig *config, in= t socket_activation) if (g_mkdir_with_parents(config->state_dir, S_IRWXU) =3D=3D -1) { g_critical("unable to create (an ancestor of) the state directory" " '%s': %s", config->state_dir, strerror(errno)); - return EXIT_FAILURE; + return NULL; } #endif =20 @@ -1254,7 +1266,7 @@ static int run_agent(GAState *s, GAConfig *config, in= t socket_activation) if (!log_file) { g_critical("unable to open specified log file: %s", strerror(errno)); - return EXIT_FAILURE; + return NULL; } s->log_file =3D log_file; } @@ -1265,7 +1277,7 @@ static int run_agent(GAState *s, GAConfig *config, in= t socket_activation) s->pstate_filepath, ga_is_frozen(s))) { g_critical("failed to load persistent state"); - return EXIT_FAILURE; + return NULL; } =20 config->blacklist =3D ga_command_blacklist_init(config->blacklist); @@ -1286,12 +1298,37 @@ static int run_agent(GAState *s, GAConfig *config, = int socket_activation) #ifndef _WIN32 if (!register_signal_handlers()) { g_critical("failed to register signal handlers"); - return EXIT_FAILURE; + return NULL; } #endif =20 s->main_loop =3D g_main_loop_new(NULL, false); =20 + ga_state =3D s; + return s; +} + +static void cleanup_agent(GAState *s) +{ + if (s->command_state) { + ga_command_state_cleanup_all(s->command_state); + 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) { + g_main_loop_unref(s->main_loop); + } + g_free(s); + ga_state =3D NULL; +} + +static int run_agent(GAState *s, GAConfig *config, int socket_activation) +{ if (!channel_init(ga_state, config->method, config->channel_path, socket_activation ? FIRST_SOCKET_ACTIVATION_FD : -1)= ) { g_critical("failed to initialize guest agent channel"); @@ -1315,7 +1352,7 @@ static int run_agent(GAState *s, GAConfig *config, in= t socket_activation) int main(int argc, char **argv) { int ret =3D EXIT_SUCCESS; - GAState *s =3D g_new0(GAState, 1); + GAState *s; GAConfig *config =3D g_new0(GAConfig, 1); int socket_activation; =20 @@ -1383,44 +1420,25 @@ int main(int argc, char **argv) } } =20 - s->log_level =3D config->log_level; - s->log_file =3D stderr; -#ifdef CONFIG_FSFREEZE - s->fsfreeze_hook =3D config->fsfreeze_hook; -#endif - s->pstate_filepath =3D g_strdup_printf("%s/qga.state", config->state_d= ir); - s->state_filepath_isfrozen =3D g_strdup_printf("%s/qga.state.isfrozen", - config->state_dir); - s->frozen =3D check_is_frozen(s); - if (config->dumpconf) { config_dump(config); goto end; } =20 + s =3D initialize_agent(config); + if (!s) { + g_critical("error initializing guest agent"); + goto end; + } ret =3D run_agent(s, config, socket_activation); + cleanup_agent(s); =20 end: - if (s->command_state) { - ga_command_state_cleanup_all(s->command_state); - 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 (config->daemonize) { unlink(config->pid_filepath); } =20 config_free(config); - if (s->main_loop) { - g_main_loop_unref(s->main_loop); - } - g_free(s); =20 return ret; } --=20 2.17.1