From 1b43c880ba43260fe551b0ac923826f16737484f Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 6 Nov 2015 16:45:27 +0100 Subject: config: let NMConfig handle "NetworkManager.state" file (bgo#764474) Move reading and writing of the state file to NMConfig ("/var/lib/NetworkManager/NetworkManager.state" file). Originally, I intended to persist more state, thus it made sense to cleanup handling of the state file and move it all at one place. Now, it's not clear that will happen anytime soon. Still, the change is a worthy cleanup, so do it anyway. https://bugzilla.gnome.org/show_bug.cgi?id=764474 --- src/main.c | 94 +++++--------------------------------------------------------- 1 file changed, 7 insertions(+), 87 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 4616155d9a..41f4eda275 100644 --- a/src/main.c +++ b/src/main.c @@ -71,81 +71,10 @@ static struct { char *opt_log_level; char *opt_log_domains; char *pidfile; - char *state_file; } global_opt = { .become_daemon = TRUE, }; -static gboolean -parse_state_file (const char *filename, - gboolean *net_enabled, - gboolean *wifi_enabled, - gboolean *wwan_enabled, - GError **error) -{ - GKeyFile *state_file; - GError *tmp_error = NULL; - gboolean wifi, net, wwan; - - g_return_val_if_fail (net_enabled != NULL, FALSE); - g_return_val_if_fail (wifi_enabled != NULL, FALSE); - g_return_val_if_fail (wwan_enabled != NULL, FALSE); - - state_file = g_key_file_new (); - g_key_file_set_list_separator (state_file, ','); - if (!g_key_file_load_from_file (state_file, filename, G_KEY_FILE_KEEP_COMMENTS, &tmp_error)) { - gboolean ret = FALSE; - - /* This is kinda ugly; create the file and directory if it doesn't - * exist yet. We can't rely on distros necessarily creating the - * /var/lib/NetworkManager for us since we have to ensure that - * users upgrading NM get this working too. - */ - if (g_error_matches (tmp_error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) { - char *data; - gsize len = 0; - - g_clear_error (&tmp_error); - - /* Write out the initial state to the state file */ - g_key_file_set_boolean (state_file, "main", "NetworkingEnabled", *net_enabled); - g_key_file_set_boolean (state_file, "main", "WirelessEnabled", *wifi_enabled); - g_key_file_set_boolean (state_file, "main", "WWANEnabled", *wwan_enabled); - - data = g_key_file_to_data (state_file, &len, NULL); - if (data) - ret = g_file_set_contents (filename, data, len, error); - g_free (data); - } else { - /* the error is not "No such file or directory" - propagate the error */ - g_propagate_error (error, tmp_error); - } - - return ret; - } - - /* Reading state bits of NetworkManager; an error leaves the passed-in state - * value unchanged. - */ - net = g_key_file_get_boolean (state_file, "main", "NetworkingEnabled", &tmp_error); - if (tmp_error == NULL) - *net_enabled = net; - g_clear_error (&tmp_error); - - wifi = g_key_file_get_boolean (state_file, "main", "WirelessEnabled", &tmp_error); - if (tmp_error == NULL) - *wifi_enabled = wifi; - g_clear_error (&tmp_error); - - wwan = g_key_file_get_boolean (state_file, "main", "WWANEnabled", &tmp_error); - if (tmp_error == NULL) - *wwan_enabled = wwan; - g_clear_error (&tmp_error); - - g_key_file_free (state_file); - return TRUE; -} - static void _set_g_fatal_warnings (void) { @@ -239,7 +168,6 @@ do_early_setup (int *argc, char **argv[], NMConfigCmdLineOptions *config_cli) "PLATFORM,RFKILL,WIFI" }, { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &global_opt.g_fatal_warnings, N_("Make all warnings fatal"), NULL }, { "pid-file", 'p', 0, G_OPTION_ARG_FILENAME, &global_opt.pidfile, N_("Specify the location of a PID file"), N_(NM_DEFAULT_PID_FILE) }, - { "state-file", 0, 0, G_OPTION_ARG_FILENAME, &global_opt.state_file, N_("State file location"), N_(NM_DEFAULT_SYSTEM_STATE_FILE) }, { "run-from-build-dir", 0, 0, G_OPTION_ARG_NONE, &global_opt.run_from_build_dir, "Run from build directory", NULL }, { "print-config", 0, 0, G_OPTION_ARG_NONE, &global_opt.print_config, N_("Print NetworkManager configuration and exit"), NULL }, {NULL} @@ -255,7 +183,6 @@ do_early_setup (int *argc, char **argv[], NMConfigCmdLineOptions *config_cli) exit (1); global_opt.pidfile = global_opt.pidfile ? global_opt.pidfile : g_strdup (NM_DEFAULT_PID_FILE); - global_opt.state_file = global_opt.state_file ? global_opt.state_file : g_strdup (NM_DEFAULT_SYSTEM_STATE_FILE); } /* @@ -265,7 +192,6 @@ do_early_setup (int *argc, char **argv[], NMConfigCmdLineOptions *config_cli) int main (int argc, char *argv[]) { - gboolean wifi_enabled = TRUE, net_enabled = TRUE, wwan_enabled = TRUE; gboolean success = FALSE; NMConfig *config; GError *error = NULL; @@ -406,17 +332,12 @@ main (int argc, char *argv[]) nm_log_info (LOGD_CORE, "NetworkManager (version " NM_DIST_VERSION ") is starting..."); - /* Parse the state file */ - if (!parse_state_file (global_opt.state_file, &net_enabled, &wifi_enabled, &wwan_enabled, &error)) { - nm_log_err (LOGD_CORE, "State file %s parsing failed: %s", - global_opt.state_file, - error->message); - /* Not a hard failure */ - } - g_clear_error (&error); - nm_log_info (LOGD_CORE, "Read config: %s", nm_config_data_get_config_description (nm_config_get_data (config))); nm_config_data_log (nm_config_get_data (config), "CONFIG: ", " ", NULL); + + /* the first access to RunState causes the file to be read (and possibly print a warning) */ + nm_config_run_state_get (config); + nm_log_dbg (LOGD_CORE, "WEXT support is %s", #if HAVE_WEXT "enabled" @@ -427,10 +348,7 @@ main (int argc, char *argv[]) nm_auth_manager_setup (nm_config_get_auth_polkit (config)); - nm_manager_setup (global_opt.state_file, - net_enabled, - wifi_enabled, - wwan_enabled); + nm_manager_setup (); if (!nm_bus_manager_get_connection (nm_bus_manager_get ())) { nm_log_warn (LOGD_CORE, "Failed to connect to D-Bus; only private bus is available"); @@ -482,6 +400,8 @@ done: nm_manager_stop (nm_manager_get ()); + nm_config_run_state_set (config, TRUE, TRUE); + if (global_opt.pidfile && wrote_pidfile) unlink (global_opt.pidfile); -- cgit v1.2.1 From d0836be0ebb3faa1aaf8f4ac0ec7b52fed011cc2 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 7 Apr 2016 18:42:24 +0200 Subject: core: rename nm_config_run_state* to nm_config_state* After all, this state is stored persistently to /var/lib/NetworkManager, and not to volatile storage in /var/run. Hence the name is better. It's also shorter, so rename it. The commit is mostly trivial, including update of code comments and logging messages. Fixes: 1b43c880ba43260fe551b0ac923826f16737484f --- src/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 41f4eda275..b60ec84eb3 100644 --- a/src/main.c +++ b/src/main.c @@ -335,8 +335,8 @@ main (int argc, char *argv[]) nm_log_info (LOGD_CORE, "Read config: %s", nm_config_data_get_config_description (nm_config_get_data (config))); nm_config_data_log (nm_config_get_data (config), "CONFIG: ", " ", NULL); - /* the first access to RunState causes the file to be read (and possibly print a warning) */ - nm_config_run_state_get (config); + /* the first access to State causes the file to be read (and possibly print a warning) */ + nm_config_state_get (config); nm_log_dbg (LOGD_CORE, "WEXT support is %s", #if HAVE_WEXT @@ -400,7 +400,7 @@ done: nm_manager_stop (nm_manager_get ()); - nm_config_run_state_set (config, TRUE, TRUE); + nm_config_state_set (config, TRUE, TRUE); if (global_opt.pidfile && wrote_pidfile) unlink (global_opt.pidfile); -- cgit v1.2.1 From c5ac69174464521e8d20d8e5e1e8ec1cefe41773 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 30 Apr 2016 03:26:06 +0200 Subject: build: cleanup includes of is implicitly included by which is available everywhere. For that reason, we would not have to include this header at all. However, it is recommended to explicitly include where needed. So, include it where needed -- if wouldn't be there -- and drop it from where it is not needed. --- src/main.c | 1 - 1 file changed, 1 deletion(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index b60ec84eb3..c4782b609e 100644 --- a/src/main.c +++ b/src/main.c @@ -31,7 +31,6 @@ #include #include #include -#include #include #include -- cgit v1.2.1