From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54927) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZAGVO-0007K9-8f for qemu-devel@nongnu.org; Wed, 01 Jul 2015 07:48:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZAGVN-0005Q7-5T for qemu-devel@nongnu.org; Wed, 01 Jul 2015 07:48:42 -0400 Received: from mail-qk0-x231.google.com ([2607:f8b0:400d:c09::231]:33211) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZAGVN-0005Q1-1V for qemu-devel@nongnu.org; Wed, 01 Jul 2015 07:48:41 -0400 Received: by qkhu186 with SMTP id u186so27014601qkh.0 for ; Wed, 01 Jul 2015 04:48:40 -0700 (PDT) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Wed, 1 Jul 2015 13:47:46 +0200 Message-Id: <1435751267-26378-12-git-send-email-marcandre.lureau@gmail.com> In-Reply-To: <1435751267-26378-1-git-send-email-marcandre.lureau@gmail.com> References: <1435751267-26378-1-git-send-email-marcandre.lureau@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [Qemu-devel] [PATCH 11/12] qga: add an optionnal qemu-ga.conf system configuration List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , mdroth@linux.vnet.ibm.com 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 --- qga/main.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) 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; -- 2.4.3