diff options
-rw-r--r-- | man/NetworkManager.xml | 6 | ||||
-rw-r--r-- | src/main.c | 34 | ||||
-rw-r--r-- | src/nm-config-data.c | 27 | ||||
-rw-r--r-- | src/nm-config-data.h | 5 | ||||
-rw-r--r-- | src/nm-config.c | 2 | ||||
-rw-r--r-- | src/tests/config/test-config.c | 2 |
6 files changed, 65 insertions, 11 deletions
diff --git a/man/NetworkManager.xml b/man/NetworkManager.xml index f31e4b6fa7..607f625a6c 100644 --- a/man/NetworkManager.xml +++ b/man/NetworkManager.xml @@ -360,6 +360,12 @@ for more information. </para></listitem> </varlistentry> + <varlistentry> + <term><option>--print-config</option></term> + <listitem><para> + Print the NetworkMangager configuration to stdout and exit. + </para></listitem> + </varlistentry> </variablelist> </refsect1> diff --git a/src/main.c b/src/main.c index e2ed871508..ee8e0f2d49 100644 --- a/src/main.c +++ b/src/main.c @@ -64,6 +64,7 @@ static gboolean configure_and_quit = FALSE; static struct { gboolean show_version; + gboolean print_config; gboolean become_daemon; gboolean g_fatal_warnings; gboolean run_from_build_dir; @@ -211,6 +212,28 @@ manager_configure_quit (NMManager *manager, gpointer user_data) configure_and_quit = TRUE; } +static int +print_config (NMConfigCmdLineOptions *config_cli) +{ + gs_unref_object NMConfig *config = NULL; + gs_free_error GError *error = NULL; + NMConfigData *config_data; + + nm_logging_setup ("OFF", "ALL", NULL, NULL); + + config = nm_config_new (config_cli, NULL, &error); + if (config == NULL) { + fprintf (stderr, _("Failed to read configuration: %s\n"), + (error && error->message) ? error->message : _("unknown")); + return 7; + } + + config_data = nm_config_get_data (config); + fprintf (stdout, "# NetworkManager configuration: %s\n", nm_config_data_get_config_description (config_data)); + nm_config_data_log (config_data, "", "", stdout); + return 0; +} + static void do_early_setup (int *argc, char **argv[], NMConfigCmdLineOptions *config_cli) { @@ -225,6 +248,7 @@ do_early_setup (int *argc, char **argv[], NMConfigCmdLineOptions *config_cli) { "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} }; @@ -279,6 +303,14 @@ main (int argc, char *argv[]) exit (0); } + if (global_opt.print_config) { + int result; + + result = print_config (config_cli); + nm_config_cmd_line_options_free (config_cli); + exit (result); + } + nm_main_utils_ensure_root (); nm_main_utils_ensure_not_running_pidfile (global_opt.pidfile); @@ -397,7 +429,7 @@ main (int argc, char *argv[]) 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: "); + nm_config_data_log (nm_config_get_data (config), "CONFIG: ", " ", NULL); nm_log_dbg (LOGD_CORE, "WEXT support is %s", #if HAVE_WEXT "enabled" diff --git a/src/nm-config-data.c b/src/nm-config-data.c index 66b1da5916..e2b6c3a6e6 100644 --- a/src/nm-config-data.c +++ b/src/nm-config-data.c @@ -495,22 +495,34 @@ _nm_config_data_log_sort (const char **pa, const char **pb, gpointer dummy) } void -nm_config_data_log (const NMConfigData *self, const char *prefix) +nm_config_data_log (const NMConfigData *self, + const char *prefix, + const char *key_prefix, + /* FILE* */ gpointer print_stream) { NMConfigDataPrivate *priv; gs_strfreev char **groups = NULL; gsize ngroups; guint g, k; + FILE *stream = print_stream; g_return_if_fail (NM_IS_CONFIG_DATA (self)); - if (!nm_logging_enabled (LOGL_DEBUG, LOGD_CORE)) + if (!stream && !nm_logging_enabled (LOGL_DEBUG, LOGD_CORE)) return; if (!prefix) prefix = ""; + if (!key_prefix) + key_prefix = ""; -#define _LOG(...) _nm_log (LOGL_DEBUG, LOGD_CORE, 0, "%s"_NM_UTILS_MACRO_FIRST(__VA_ARGS__), prefix _NM_UTILS_MACRO_REST (__VA_ARGS__)) +#define _LOG(stream, prefix, ...) \ + G_STMT_START { \ + if (!stream) \ + _nm_log (LOGL_DEBUG, LOGD_CORE, 0, "%s"_NM_UTILS_MACRO_FIRST(__VA_ARGS__)"%s", prefix _NM_UTILS_MACRO_REST (__VA_ARGS__), ""); \ + else \ + fprintf (stream, "%s"_NM_UTILS_MACRO_FIRST(__VA_ARGS__)"%s", prefix _NM_UTILS_MACRO_REST (__VA_ARGS__), "\n"); \ + } G_STMT_END priv = NM_CONFIG_DATA_GET_PRIVATE (self); @@ -525,7 +537,8 @@ nm_config_data_log (const NMConfigData *self, const char *prefix) NULL); } - _LOG ("config-data[%p]: %lu groups", self, (unsigned long) ngroups); + if (!stream) + _LOG (stream, prefix, "config-data[%p]: %lu groups", self, (unsigned long) ngroups); for (g = 0; g < ngroups; g++) { const char *group = groups[g]; @@ -534,8 +547,8 @@ nm_config_data_log (const NMConfigData *self, const char *prefix) is_atomic = nm_config_data_is_intern_atomic_group (self, group); - _LOG (""); - _LOG ("[%s]%s", group, is_atomic ? "*" : ""); + _LOG (stream, prefix, ""); + _LOG (stream, prefix, "[%s]%s", group, is_atomic && !stream ? " # atomic section" : ""); keys = g_key_file_get_keys (priv->keyfile, group, NULL, NULL); for (k = 0; keys && keys[k]; k++) { @@ -543,7 +556,7 @@ nm_config_data_log (const NMConfigData *self, const char *prefix) gs_free char *value = NULL; value = g_key_file_get_value (priv->keyfile, group, key, NULL); - _LOG (" %s=%s", key, value); + _LOG (stream, prefix, "%s%s=%s", key_prefix, key, value); } } diff --git a/src/nm-config-data.h b/src/nm-config-data.h index 4c0be72a8b..2decfdb49e 100644 --- a/src/nm-config-data.h +++ b/src/nm-config-data.h @@ -105,7 +105,10 @@ NMConfigData *nm_config_data_new_update_no_auto_default (const NMConfigData *bas NMConfigChangeFlags nm_config_data_diff (NMConfigData *old_data, NMConfigData *new_data); -void nm_config_data_log (const NMConfigData *config_data, const char *prefix); +void nm_config_data_log (const NMConfigData *self, + const char *prefix, + const char *key_prefix, + /* FILE* */ gpointer print_stream); const char *nm_config_data_get_config_main_file (const NMConfigData *config_data); const char *nm_config_data_get_config_description (const NMConfigData *config_data); diff --git a/src/nm-config.c b/src/nm-config.c index f070ff9756..7d65a48ad1 100644 --- a/src/nm-config.c +++ b/src/nm-config.c @@ -1796,7 +1796,7 @@ _set_config_data (NMConfig *self, NMConfigData *new_data, int signal) if (new_data) { nm_log_info (LOGD_CORE, "config: update %s (%s)", nm_config_data_get_config_description (new_data), (log_str = nm_config_change_flags_to_string (changes))); - nm_config_data_log (new_data, "CONFIG: "); + nm_config_data_log (new_data, "CONFIG: ", " ", NULL); priv->config_data = new_data; } else if (had_new_data) nm_log_info (LOGD_CORE, "config: signal %s (no changes from disk)", (log_str = nm_config_change_flags_to_string (changes))); diff --git a/src/tests/config/test-config.c b/src/tests/config/test-config.c index ed6f875740..e8de86c8a8 100644 --- a/src/tests/config/test-config.c +++ b/src/tests/config/test-config.c @@ -476,7 +476,7 @@ test_config_confdir (void) g_assert_cmpstr (value, ==, "VAL5"); g_free (value); - nm_config_data_log (nm_config_get_data_orig (config), ">>> TEST: "); + nm_config_data_log (nm_config_get_data_orig (config), ">>> TEST: ", " ", NULL); g_object_unref (config); } |