]> rtime.felk.cvut.cz Git - sojka/lightdm.git/commitdiff
Move config loading into one function; move that function into libcommon; call it...
authorMichael Terry <michael.terry@canonical.com>
Tue, 18 Mar 2014 02:05:57 +0000 (22:05 -0400)
committerMichael Terry <michael.terry@canonical.com>
Tue, 18 Mar 2014 02:05:57 +0000 (22:05 -0400)
common/Makefile.am
common/configuration.c
common/configuration.h
liblightdm-gobject/session.c
src/Makefile.am
src/lightdm.c

index f289e6aca9d8a711652d94678fa3609066e5d424..b501f221c5a2e2ab7cf9604c2f6fb5c61d692558 100644 (file)
@@ -14,7 +14,9 @@ libcommon_la_SOURCES = \
 
 libcommon_la_CFLAGS = \
        $(WARN_CFLAGS) \
-       $(GLIB_CFLAGS)
+       $(GLIB_CFLAGS) \
+       -DCONFIG_DIR=\"$(sysconfdir)/lightdm\" \
+       -DSYSTEM_CONFIG_DIR=\"$(pkgdatadir)/lightdm.conf.d\"
 
 libcommon_la_LIBADD = \
        $(GLIB_LDFLAGS)
index 8ced3642489b2651c33914e0216240205c2d349a..015d2fc44d770132365f6fedfb9e26404a312fc6 100644 (file)
@@ -9,6 +9,8 @@
  * license.
  */
 
+#include <string.h>
+
 #include "configuration.h"
 
 struct ConfigurationPrivate
@@ -70,6 +72,122 @@ config_load_from_file (Configuration *config, const gchar *path, GError **error)
     return TRUE;
 }
 
+static gchar *
+path_make_absolute (gchar *path)
+{
+    gchar *cwd, *abs_path;
+
+    if (!path)
+        return NULL;
+
+    if (g_path_is_absolute (path))
+        return path;
+
+    cwd = g_get_current_dir ();
+    abs_path = g_build_filename (cwd, path, NULL);
+    g_free (path);
+
+    return abs_path;
+}
+
+static int
+compare_strings (gconstpointer a, gconstpointer b)
+{
+    return strcmp (a, b);
+}
+
+static void
+load_config_directory (const gchar *path, GList **messages)
+{
+    GDir *dir;
+    GList *files = NULL, *link;
+    GError *error = NULL;
+
+    /* Find configuration files */
+    dir = g_dir_open (path, 0, &error);
+    if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
+        g_printerr ("Failed to open configuration directory %s: %s\n", path, error->message);
+    g_clear_error (&error);
+    if (dir)
+    {
+        const gchar *name;
+        while ((name = g_dir_read_name (dir)))
+            files = g_list_append (files, g_strdup (name));
+        g_dir_close (dir);
+    }
+
+    /* Sort alphabetically and load onto existing configuration */
+    files = g_list_sort (files, compare_strings);
+    for (link = files; link; link = link->next)
+    {
+        gchar *filename = link->data;
+        gchar *conf_path;
+
+        conf_path = g_build_filename (path, filename, NULL);
+        if (g_str_has_suffix (filename, ".conf"))
+        {
+            if (messages)
+                *messages = g_list_append (*messages, g_strdup_printf ("Loading configuration from %s", conf_path));
+            config_load_from_file (config_get_instance (), conf_path, &error);
+            if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
+                g_printerr ("Failed to load configuration from %s: %s\n", filename, error->message);
+            g_clear_error (&error);
+        }
+        else
+            g_debug ("Ignoring configuration file %s, it does not have .conf suffix", conf_path);
+        g_free (conf_path);
+    }
+    g_list_free_full (files, g_free);
+}
+
+gboolean
+config_load_from_standard_locations (Configuration *config, const gchar *config_path, GList **messages)
+{
+    gchar *config_dir, *config_d_dir = NULL;
+    gboolean explicit_config = FALSE;
+    gboolean success = TRUE;
+    GError *error = NULL;
+
+    if (config_path)
+    {
+        config_dir = g_path_get_basename (config_path);
+        config_dir = path_make_absolute (config_dir);
+        explicit_config = TRUE;
+    }
+    else
+    {
+        config_dir = g_strdup (CONFIG_DIR);
+        config_d_dir = g_build_filename (config_dir, "lightdm.conf.d", NULL);
+        config_path = g_build_filename (config_dir, "lightdm.conf", NULL);
+    }
+    config_set_string (config, "LightDM", "config-directory", config_dir);
+    g_free (config_dir);
+
+    load_config_directory (SYSTEM_CONFIG_DIR, messages);
+    if (config_d_dir)
+        load_config_directory (config_d_dir, messages);
+    g_free (config_d_dir);
+
+    if (messages)
+        *messages = g_list_append (*messages, g_strdup_printf ("Loading configuration from %s", config_path));
+    if (!config_load_from_file (config, config_path, &error))
+    {
+        gboolean is_empty;
+
+        is_empty = error && g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
+
+        if (explicit_config || !is_empty)
+        {
+            if (error)
+                g_printerr ("Failed to load configuration from %s: %s\n", config_path, error->message);
+            success = FALSE;
+        }
+    }
+    g_clear_error (&error);
+
+    return success;
+}
+
 gchar **
 config_get_groups (Configuration *config)
 {
index cae56912d1eec9fa37813de0a996adca057634f1..b689398e269a965db9e636272d56ddf88d478cc9 100644 (file)
@@ -38,6 +38,8 @@ Configuration *config_get_instance (void);
 
 gboolean config_load_from_file (Configuration *config, const gchar *path, GError **error);
 
+gboolean config_load_from_standard_locations (Configuration *config, const gchar *config_path, GList **messages);
+
 gchar **config_get_groups (Configuration *config);
 
 gchar **config_get_keys (Configuration *config, const gchar *group_name);
index 0b89eab74313fff1896ab020434f411f0a85793c..e3ef2953b23f356057ae4299bf4c013088694ba4 100644 (file)
@@ -192,11 +192,10 @@ load_sessions (const gchar *sessions_dir)
 static void
 update_sessions (void)
 {
-    gchar *config_path = NULL;
     gchar *sessions_dir;
     gchar *remote_sessions_dir;
     gboolean result;
-    GError *error = NULL;
+    gchar *value;
 
     if (have_sessions)
         return;
@@ -205,32 +204,21 @@ update_sessions (void)
     remote_sessions_dir = g_strdup (REMOTE_SESSIONS_DIR);
 
     /* Use session directory from configuration */
-    /* FIXME: This should be sent in the greeter connection */
-    config_path = g_build_filename (CONFIG_DIR, "lightdm.conf", NULL);
-    /* FIXME: This should load from lightdm.conf.d as well */
-    result = config_load_from_file (config_get_instance (), config_path, &error);
-    if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
-        g_warning ("Failed to open configuration file: %s", error->message);
-    g_clear_error (&error);
-    if (result)
-    {
-        gchar *value;
+    config_load_from_standard_locations (config_get_instance (), NULL, NULL);
       
-        value = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
-        if (value)
-        {
-            g_free (sessions_dir);
-            sessions_dir = value;
-        }
+    value = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
+    if (value)
+    {
+        g_free (sessions_dir);
+        sessions_dir = value;
+    }
 
-        value = config_get_string (config_get_instance (), "LightDM", "remote-sessions-directory");
-        if (value)
-        {
-            g_free (remote_sessions_dir);
-            remote_sessions_dir = value;
-        }
+    value = config_get_string (config_get_instance (), "LightDM", "remote-sessions-directory");
+    if (value)
+    {
+        g_free (remote_sessions_dir);
+        remote_sessions_dir = value;
     }
-    g_free (config_path);
 
     local_sessions = load_sessions (sessions_dir);
     remote_sessions = load_sessions (remote_sessions_dir);
index ba35eeaf638ce9b18d554a39fce0a421c452a833..da16d630d824db0a379b12c986331f32bb9eac68 100644 (file)
@@ -78,12 +78,10 @@ lightdm_CFLAGS = \
        $(LIGHTDM_CFLAGS) \
        -I"$(top_srcdir)/common" \
        -DSBIN_DIR=\"$(sbindir)\" \
-       -DCONFIG_DIR=\"$(sysconfdir)/lightdm\" \
        -DUSERS_DIR=\"$(localstatedir)/lib/lightdm-data\" \
        -DLOG_DIR=\"$(localstatedir)/log/lightdm\" \
        -DRUN_DIR=\"$(localstatedir)/run/lightdm\" \
        -DCACHE_DIR=\"$(localstatedir)/cache/lightdm\" \
-       -DSYSTEM_CONFIG_DIR=\"$(pkgdatadir)/lightdm.conf.d\" \
        -DSESSIONS_DIR=\"$(pkgdatadir)/sessions:$(datadir)/xsessions\" \
        -DREMOTE_SESSIONS_DIR=\"$(pkgdatadir)/remote-sessions\" \
        -DGREETERS_DIR=\"$(pkgdatadir)/greeters:$(datadir)/xgreeters\"
@@ -100,7 +98,6 @@ dm_tool_SOURCES = \
 dm_tool_CFLAGS = \
        $(WARN_CFLAGS) \
        $(LIGHTDM_CFLAGS) \
-       -DCONFIG_DIR=\"$(sysconfdir)/lightdm\" \
        -DLOCALE_DIR=\"$(datadir)/locale\"
 
 dm_tool_LDADD = \
index faa4a66b307e1946499bdbcfaee472eef33e81af..15e21bf09dbbbb47b46146eb689e070981c88f1d 100644 (file)
@@ -903,73 +903,6 @@ name_lost_cb (GDBusConnection *connection,
     exit (EXIT_FAILURE);
 }
 
-static gchar *
-path_make_absolute (gchar *path)
-{
-    gchar *cwd, *abs_path;
-
-    if (!path)
-        return NULL;
-
-    if (g_path_is_absolute (path))
-        return path;
-
-    cwd = g_get_current_dir ();
-    abs_path = g_build_filename (cwd, path, NULL);
-    g_free (path);
-
-    return abs_path;
-}
-
-static int
-compare_strings (gconstpointer a, gconstpointer b)
-{
-    return strcmp (a, b);
-}
-
-static void
-load_config_directory (const gchar *path, GList **messages)
-{
-    GDir *dir;
-    GList *files = NULL, *link;
-    GError *error = NULL;
-
-    /* Find configuration files */
-    dir = g_dir_open (path, 0, &error);
-    if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
-        g_printerr ("Failed to open configuration directory %s: %s\n", path, error->message);
-    g_clear_error (&error);
-    if (dir)
-    {
-        const gchar *name;
-        while ((name = g_dir_read_name (dir)))
-            files = g_list_append (files, g_strdup (name));
-        g_dir_close (dir);
-    }
-
-    /* Sort alphabetically and load onto existing configuration */
-    files = g_list_sort (files, compare_strings);
-    for (link = files; link; link = link->next)
-    {
-        gchar *filename = link->data;
-        gchar *conf_path;
-
-        conf_path = g_build_filename (path, filename, NULL);
-        if (g_str_has_suffix (filename, ".conf"))
-        {
-            *messages = g_list_append (*messages, g_strdup_printf ("Loading configuration from %s", conf_path));
-            config_load_from_file (config_get_instance (), conf_path, &error);
-            if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
-                g_printerr ("Failed to load configuration from %s: %s\n", filename, error->message);
-            g_clear_error (&error);
-        }
-        else
-            g_debug ("Ignoring configuration file %s, it does not have .conf suffix", conf_path);
-        g_free (conf_path);
-    }
-    g_list_free_full (files, g_free);
-}
-
 int
 main (int argc, char **argv)
 {
@@ -978,10 +911,8 @@ main (int argc, char **argv)
     gboolean result;
     gchar **groups, **i, *dir;
     gint n_seats = 0;
-    gboolean explicit_config = FALSE;
     gboolean test_mode = FALSE;
     gchar *pid_path = "/var/run/lightdm.pid";
-    gchar *config_dir, *config_d_dir = NULL;
     gchar *log_dir = NULL;
     gchar *run_dir = NULL;
     gchar *cache_dir = NULL;
@@ -1056,21 +987,6 @@ main (int argc, char **argv)
         return EXIT_SUCCESS;
     }
 
-    if (config_path)
-    {
-        config_dir = g_path_get_basename (config_path);
-        config_dir = path_make_absolute (config_dir);
-        explicit_config = TRUE;
-    }
-    else
-    {
-        config_dir = g_strdup (CONFIG_DIR);
-        config_d_dir = g_build_filename (config_dir, "lightdm.conf.d", NULL);
-        config_path = g_build_filename (config_dir, "lightdm.conf", NULL);
-    }
-    config_set_string (config_get_instance (), "LightDM", "config-directory", config_dir);
-    g_free (config_dir);
-
     if (!test_mode && getuid () != 0)
     {
         g_printerr ("Only root can run Light Display Manager.  To run as a regular user for testing run with the --test-mode flag.\n");
@@ -1125,25 +1041,8 @@ main (int argc, char **argv)
     }
 
     /* Load config file(s) */
-    load_config_directory (SYSTEM_CONFIG_DIR, &messages);
-    if (config_d_dir)
-        load_config_directory (config_d_dir, &messages);
-    g_free (config_d_dir);
-    messages = g_list_append (messages, g_strdup_printf ("Loading configuration from %s", config_path));
-    if (!config_load_from_file (config_get_instance (), config_path, &error))
-    {
-        gboolean is_empty;
-
-        is_empty = error && g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
-
-        if (explicit_config || !is_empty)
-        {
-            if (error)
-                g_printerr ("Failed to load configuration from %s: %s\n", config_path, error->message);
-            exit (EXIT_FAILURE);
-        }
-    }
-    g_clear_error (&error);
+    if (!config_load_from_standard_locations (config_get_instance (), config_path, &messages))
+        exit (EXIT_FAILURE);
     g_free (config_path);
 
     /* Set default values */