diff mbox

[11/12] qga: add an optionnal qemu-ga.conf system configuration

Message ID 1435751267-26378-12-git-send-email-marcandre.lureau@gmail.com
State New
Headers show

Commit Message

Marc-André Lureau July 1, 2015, 11:47 a.m. UTC
Learn to configure the agent with a system configuration.

This may simplify command-line handling, especially when the blacklist
is long.

Among the other benefits, this may standardize the configuration of a
init service (instead distro-specific init keys/files)

Signed-off-by: Marc-André Lureau <marcandre.lureau@gmail.com>
---
 qga/main.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
diff mbox

Patch

diff --git a/qga/main.c b/qga/main.c
index f6dbb3e..9026c9a 100644
--- a/qga/main.c
+++ b/qga/main.c
@@ -56,6 +56,7 @@ 
 #define QGA_FSFREEZE_HOOK_DEFAULT CONFIG_QEMU_CONFDIR "/fsfreeze-hook"
 #endif
 #define QGA_SENTINEL_BYTE 0xFF
+#define QGA_CONF_DEFAULT CONFIG_QEMU_CONFDIR G_DIR_SEPARATOR_S "qemu-ga.conf"
 
 static struct {
     const char *state_dir;
@@ -951,6 +952,7 @@  static char *state_dir;
 #ifdef _WIN32
 static const char *service;
 #endif
+static char *bliststr;
 static GList *blacklist;
 static int daemonize, dumpconf;
 static GLogLevelFlags log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL;
@@ -959,6 +961,7 @@  static void dump_config(void)
 {
     gchar *bl = list_join(blacklist, ',');
 
+    printf("### Read from configuration file: %s ###\n", QGA_CONF_DEFAULT);
     printf("[general]\n");
     printf("daemonize = %d\n", daemonize);
     printf("pidfile = %s\n", pid_filepath);
@@ -974,6 +977,71 @@  static void dump_config(void)
     g_free(bl);
 }
 
+static void load_system_config(void)
+{
+    GError *gerr = NULL;
+    GKeyFile *keyfile;
+
+    /* read system config */
+    keyfile = g_key_file_new();
+    if (!g_key_file_load_from_file(keyfile, QGA_CONF_DEFAULT, 0, &gerr)) {
+        goto end;
+    }
+    if (g_key_file_has_key(keyfile, "general", "daemon", NULL)) {
+        daemonize =
+            g_key_file_get_boolean(keyfile, "general", "daemon", &gerr);
+    }
+    if (g_key_file_has_key(keyfile, "general", "method", NULL)) {
+        method =
+            g_key_file_get_string(keyfile, "general", "method", &gerr);
+    }
+    if (g_key_file_has_key(keyfile, "general", "path", NULL)) {
+        device_path =
+            g_key_file_get_string(keyfile, "general", "path", &gerr);
+    }
+    if (g_key_file_has_key(keyfile, "general", "logfile", NULL)) {
+        log_filepath =
+            g_key_file_get_string(keyfile, "general", "logfile", &gerr);
+    }
+    if (g_key_file_has_key(keyfile, "general", "pidfile", NULL)) {
+        pid_filepath =
+            g_key_file_get_string(keyfile, "general", "pidfile", &gerr);
+    }
+#ifdef CONFIG_FSFREEZE
+    if (g_key_file_has_key(keyfile, "general", "fsfreeze-hook", NULL)) {
+        fsfreeze_hook =
+            g_key_file_get_string(keyfile,
+                                  "general", "fsfreeze-hook", &gerr);
+    }
+#endif
+    if (g_key_file_has_key(keyfile, "general", "statedir", NULL)) {
+        state_dir =
+            g_key_file_get_string(keyfile, "general", "statedir", &gerr);
+    }
+    if (g_key_file_has_key(keyfile, "general", "verbose", NULL) &&
+        g_key_file_get_boolean(keyfile, "general", "verbose", &gerr)) {
+        /* enable all log levels */
+        log_level = G_LOG_LEVEL_MASK;
+    }
+    if (g_key_file_has_key(keyfile, "general", "blacklist", NULL)) {
+        bliststr =
+            g_key_file_get_string(keyfile, "general", "blacklist", &gerr);
+        blacklist = g_list_concat(blacklist, split_list(bliststr, ','));
+    }
+
+end:
+    if (keyfile) {
+        g_key_file_free(keyfile);
+    }
+    if (gerr &&
+        !(gerr->domain == G_FILE_ERROR && gerr->code == G_FILE_ERROR_NOENT)) {
+        g_critical("error loading configuration from path: %s, %s",
+                   QGA_CONF_DEFAULT, gerr->message);
+        exit(EXIT_FAILURE);
+    }
+    g_clear_error(&gerr);
+}
+
 static void option_parse(int argc, char **argv)
 {
     const char *sopt = "hVvdm:p:l:f:F::b:s:t:D";
@@ -1216,6 +1284,7 @@  int main(int argc, char **argv)
     module_call_init(MODULE_INIT_QAPI);
 
     init_dfl_pathnames();
+    load_system_config();
     option_parse(argc, argv);
 
     if (pid_filepath == NULL) {
@@ -1283,6 +1352,7 @@  end:
 #ifdef CONFIG_FSFREEZE
     g_free(fsfreeze_hook);
 #endif
+    g_free(bliststr);
     g_free(s);
 
     return ret;