qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Michael Roth <mdroth@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: peter.maydell@linaro.org
Subject: [Qemu-devel] [PULL 18/24] qga: group agent init/cleanup init separate routines
Date: Tue, 30 Oct 2018 09:43:52 -0500	[thread overview]
Message-ID: <20181030144358.23144-19-mdroth@linux.vnet.ibm.com> (raw)
In-Reply-To: <20181030144358.23144-1-mdroth@linux.vnet.ibm.com>

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 <mdroth@linux.vnet.ibm.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
 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;
 }
 
-static int run_agent(GAState *s, GAConfig *config, int socket_activation)
+static GAState *initialize_agent(GAConfig *config)
 {
-    ga_state = s;
+    GAState *s = g_new0(GAState, 1);
+
+    g_assert(ga_state == NULL);
+
+    s->log_level = config->log_level;
+    s->log_file = stderr;
+#ifdef CONFIG_FSFREEZE
+    s->fsfreeze_hook = config->fsfreeze_hook;
+#endif
+    s->pstate_filepath = g_strdup_printf("%s/qga.state", config->state_dir);
+    s->state_filepath_isfrozen = g_strdup_printf("%s/qga.state.isfrozen",
+                                                 config->state_dir);
+    s->frozen = check_is_frozen(s);
 
     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, int socket_activation)
     if (g_mkdir_with_parents(config->state_dir, S_IRWXU) == -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
 
@@ -1254,7 +1266,7 @@ static int run_agent(GAState *s, GAConfig *config, int socket_activation)
             if (!log_file) {
                 g_critical("unable to open specified log file: %s",
                            strerror(errno));
-                return EXIT_FAILURE;
+                return NULL;
             }
             s->log_file = log_file;
         }
@@ -1265,7 +1277,7 @@ static int run_agent(GAState *s, GAConfig *config, int socket_activation)
                                s->pstate_filepath,
                                ga_is_frozen(s))) {
         g_critical("failed to load persistent state");
-        return EXIT_FAILURE;
+        return NULL;
     }
 
     config->blacklist = 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
 
     s->main_loop = g_main_loop_new(NULL, false);
 
+    ga_state = 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 = 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, int socket_activation)
 int main(int argc, char **argv)
 {
     int ret = EXIT_SUCCESS;
-    GAState *s = g_new0(GAState, 1);
+    GAState *s;
     GAConfig *config = g_new0(GAConfig, 1);
     int socket_activation;
 
@@ -1383,44 +1420,25 @@ int main(int argc, char **argv)
         }
     }
 
-    s->log_level = config->log_level;
-    s->log_file = stderr;
-#ifdef CONFIG_FSFREEZE
-    s->fsfreeze_hook = config->fsfreeze_hook;
-#endif
-    s->pstate_filepath = g_strdup_printf("%s/qga.state", config->state_dir);
-    s->state_filepath_isfrozen = g_strdup_printf("%s/qga.state.isfrozen",
-                                                 config->state_dir);
-    s->frozen = check_is_frozen(s);
-
     if (config->dumpconf) {
         config_dump(config);
         goto end;
     }
 
+    s = initialize_agent(config);
+    if (!s) {
+        g_critical("error initializing guest agent");
+        goto end;
+    }
     ret = run_agent(s, config, socket_activation);
+    cleanup_agent(s);
 
 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);
     }
 
     config_free(config);
-    if (s->main_loop) {
-        g_main_loop_unref(s->main_loop);
-    }
-    g_free(s);
 
     return ret;
 }
-- 
2.17.1

  parent reply	other threads:[~2018-10-30 14:44 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-30 14:43 [Qemu-devel] [PULL 00/24] qemu-ga patch queue for soft-freeze Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 01/24] qga: Support Unicode paths in guest-file-open on win32 Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 02/24] qga-win: add support for qmp_guest_fsfreeze_freeze_list Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 03/24] qga: ignore non present cpus when handling qmp_guest_get_vcpus() Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 04/24] configure: add test for libudev Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 05/24] qga: linux: report disk serial number Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 06/24] qga: linux: return disk device in guest-get-fsinfo Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 07/24] qga-win: prevent crash when executing fsinfo command Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 08/24] qga-win: fsinfo: pci-info: allow partial info Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 09/24] build: rename CONFIG_QGA_NTDDDISK to CONFIG_QGA_NTDDSCSI Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 10/24] qga-win: add debugging information Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 11/24] qga-win: refactor disk properties (bus) Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 12/24] qga-win: report disk serial number Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 13/24] qga-win: refactor disk info Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 14/24] qga-win: handle multi-disk volumes Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 15/24] qga-win: return disk device in guest-get-fsinfo Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 16/24] qga-win: demystify namespace stripping Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 17/24] qga: fix an off-by-one issue Michael Roth
2018-10-30 14:43 ` Michael Roth [this message]
2018-10-30 14:43 ` [Qemu-devel] [PULL 19/24] qga: hang GAConfig/socket_activation off of GAState global Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 20/24] qga: move w32 service handling out of run_agent() Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 21/24] qga: add --retry-path option for re-initializing channel on failure Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 22/24] qga-win: install service with --retry-path set by default Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 23/24] qga-win: report specific error when failing to open channel Michael Roth
2018-10-30 14:43 ` [Qemu-devel] [PULL 24/24] qga-win: changing --retry-path option behavior Michael Roth
2018-10-30 18:49 ` [Qemu-devel] [PULL 00/24] qemu-ga patch queue for soft-freeze Peter Maydell
2018-10-30 20:57   ` Michael Roth
2018-10-31 13:23     ` Peter Maydell
2018-10-31 13:59       ` Michael Roth
2018-10-31 15:41 ` no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181030144358.23144-19-mdroth@linux.vnet.ibm.com \
    --to=mdroth@linux.vnet.ibm.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).