main: add argument --print-config to NetworkManager
authorThomas Haller <thaller@redhat.com>
Fri, 20 Nov 2015 16:52:54 +0000 (17:52 +0100)
committerThomas Haller <thaller@redhat.com>
Sun, 22 Nov 2015 12:40:32 +0000 (13:40 +0100)
man/NetworkManager.xml
src/main.c
src/nm-config-data.c
src/nm-config-data.h
src/nm-config.c
src/tests/config/test-config.c

index f31e4b6..607f625 100644 (file)
          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>
 
index e2ed871..ee8e0f2 100644 (file)
@@ -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"
index 66b1da5..e2b6c3a 100644 (file)
@@ -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);
                }
        }
 
index 4c0be72..2decfdb 100644 (file)
@@ -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);
index f070ff9..7d65a48 100644 (file)
@@ -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)));
index ed6f875..e8de86c 100644 (file)
@@ -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);
 }