From nobody Tue Feb 10 06:07:52 2026 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 1538033925004938.6840858918796; Thu, 27 Sep 2018 00:38:45 -0700 (PDT) Received: from localhost ([::1]:34172 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5Qsd-0007nA-2b for importer@patchew.org; Thu, 27 Sep 2018 03:38:35 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59267) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5Qqe-00066M-U0 for qemu-devel@nongnu.org; Thu, 27 Sep 2018 03:36:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g5Qqc-0003e7-3m for qemu-devel@nongnu.org; Thu, 27 Sep 2018 03:36:32 -0400 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]:50231) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1g5Qqb-0003dc-Ro for qemu-devel@nongnu.org; Thu, 27 Sep 2018 03:36:30 -0400 Received: by mail-wm1-x32a.google.com with SMTP id s12-v6so4786542wmc.0 for ; Thu, 27 Sep 2018 00:36:29 -0700 (PDT) Received: from bark.daynix ([141.226.163.173]) by smtp.gmail.com with ESMTPSA id y206-v6sm1708129wmg.14.2018.09.27.00.36.27 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 27 Sep 2018 00:36:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=RoXOieiwGLa29yBUbHNmTBhheE+ZLb9eAHolXD0J7Vo=; b=e6f+xF+xnOeKY0mj5aVqHybh0UiDKjBmSZ2cP+KYMrGMiwD4ZphD/0SgKg0bfJdiR/ gzvghv5Opw4e/ivIHq1VD9sxXQ+c9/4zLfrg/eB66/kDuFADkI1jT2em7OB9UDZNTpjb mw7li492P+XRJ1l++z30SNy9bpFVmWaKtsSwe26IM/Jw6/RWcExphNNRphtIdw5bnuY3 mp5SBNvo6APVtlAtVF9+FDO/PRnut1iOB+jAleXK85EuXWPhwymArLOweIB1R96UMWuB Y/gJqiX9E5U655OIiQDuD1cRzSR9Yq+4KlcFvuCtMZd0Bz9P1QbQzGIFQPd+RbQTEJ2z KifQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=RoXOieiwGLa29yBUbHNmTBhheE+ZLb9eAHolXD0J7Vo=; b=VzYswiRRxncYT7q3jvFlkfTqn0MpGrp07gBDHKgovR1sC0xL3LWHCQb93IOy/E6LcA lb1yGUkEQ8Le157C5FUhl7tATHBlh8gQiPjotuKcUQ/DylU6j9wp3te3I7ZFQa774g+M 5ksfFveewPH+ePrwWQuo9ACuiNt1V4xz60KCZ9d7y/bu7iwIkftpKc9QL/BBo93IZtRM i8ifEL49a5UvibsWwjbV0CrCKgVnd2SYjQFIbhbP2a1RxKU1rWPsbdkJwAvhjkkKb89h Fo0d//yCERPc24twjMrzU3U2a6FR3p3nP/oFNaeELd1zYK4t3UZWvcOYwR5YnuFqlXgD 9xOA== X-Gm-Message-State: ABuFfojstln83EtOoHhVFszONbjsisVNjlXk+7IJjBDpGj0aj5o8qTrK B74rcJAkoemK/IMMGcT8gj+5KGsL6ek= X-Google-Smtp-Source: ACcGV62vRqscH1eaFl97+GPSuj3uCVn2tAyk4z7VDmiSd//fipDS3K9b0MRdigIu0pAyMnsKS++dQQ== X-Received: by 2002:a1c:9355:: with SMTP id v82-v6mr6828861wmd.128.1538033788554; Thu, 27 Sep 2018 00:36:28 -0700 (PDT) From: Bishara AbuHattoum To: qemu-devel@nongnu.org, Michael Roth Date: Thu, 27 Sep 2018 10:36:18 +0300 Message-Id: <20180927073624.17163-2-bishara@daynix.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180927073624.17163-1-bishara@daynix.com> References: <20180927073624.17163-1-bishara@daynix.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::32a Subject: [Qemu-devel] [PATCH 1/7] 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: Yan Vugenfirer Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Michael Roth This patch better separates the init/cleanup routines out into separate functions to make 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 Message-Id: <20171026233054.21133-2-mdroth@linux.vnet.ibm.com> Reviewed-by: Marc-Andr=C3=A9 Lureau --- qga/main.c | 82 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/qga/main.c b/qga/main.c index 6d70242d05..7b7b837a14 100644 --- a/qga/main.c +++ b/qga/main.c @@ -1245,9 +1245,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); @@ -1263,7 +1275,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 @@ -1288,7 +1300,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; } @@ -1299,7 +1311,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); @@ -1320,12 +1332,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"); @@ -1349,7 +1386,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 @@ -1417,44 +1454,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.0