public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] proj/openrc-settingsd:master commit in: src/, /
@ 2012-02-05  6:46 Alexandre Restovtsev
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Restovtsev @ 2012-02-05  6:46 UTC (permalink / raw
  To: gentoo-commits

commit:     66ab55afd15550e937972bf980336d1c51246165
Author:     Alexandre Rostovtsev <tetromino <AT> gentoo <DOT> org>
AuthorDate: Sun Feb  5 06:43:59 2012 +0000
Commit:     Alexandre Restovtsev <tetromino <AT> gmail <DOT> com>
CommitDate: Sun Feb  5 06:43:59 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/openrc-settingsd.git;a=commit;h=66ab55af

Port to GFile, and add shell_utils_trivial_set_and_save() to simplify on_handle_set_* callbacks

---
 TODO              |    4 -
 src/hostnamed.c   |   57 ++++-----------
 src/shell-utils.c |  201 +++++++++++++++++++++++++++++++----------------------
 src/shell-utils.h |   13 +++-
 4 files changed, 145 insertions(+), 130 deletions(-)

diff --git a/TODO b/TODO
index 530ec81..62ec523 100644
--- a/TODO
+++ b/TODO
@@ -1,7 +1,3 @@
-Use GFile for file I/O to take advantage of built-in temporary handling.
-
-Refactor on_handle_set_* callbacks (lots of code duplication currently).
-
 Source /etc/rc.conf after /etc/conf.d/$service; do something intelligent
 if the relevant variable is set in /etc/rc.conf (go to read-only mode?)
 

diff --git a/src/hostnamed.c b/src/hostnamed.c
index d910fb0..c21d875 100644
--- a/src/hostnamed.c
+++ b/src/hostnamed.c
@@ -20,7 +20,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
 
 #include <dbus/dbus-protocol.h>
 #include <glib.h>
@@ -45,9 +44,11 @@ static OpenrcSettingsdHostnamedHostname1 *hostname1 = NULL;
 static gchar hostname[HOST_NAME_MAX + 1];
 G_LOCK_DEFINE_STATIC (hostname);
 static gchar *static_hostname = NULL;
+static GFile *static_hostname_file = NULL;
 G_LOCK_DEFINE_STATIC (static_hostname);
 static gchar *pretty_hostname = NULL;
 static gchar *icon_name = NULL;
+static GFile *machine_info_file = NULL;
 G_LOCK_DEFINE_STATIC (machine_info);
 
 static gboolean
@@ -174,25 +175,7 @@ on_handle_set_static_hostname (OpenrcSettingsdHostnamedHostname1 *hostname1,
     if (!hostname_is_valid (name))
         name = "localhost";
 
-    confd_file = shell_utils_trivial_new (SYSCONFDIR "/conf.d/hostname", &err);
-    if (confd_file == NULL) {
-        g_dbus_method_invocation_return_gerror (invocation, err);
-        G_UNLOCK (static_hostname);
-        goto end;
-    }
-
-    if (!shell_utils_trivial_set_variable (confd_file, "hostname", name, FALSE) &&
-        !shell_utils_trivial_set_variable (confd_file, "HOSTNAME", name, FALSE) &&
-        !shell_utils_trivial_set_variable (confd_file, "hostname", name, TRUE))
-    {
-        g_dbus_method_invocation_return_dbus_error (invocation,
-                                                    DBUS_ERROR_FAILED,
-                                                    "Failed to set static hostname in " SYSCONFDIR "/conf.d/hostname");
-        G_UNLOCK (static_hostname);
-        goto end;
-    }
-
-    if (!shell_utils_trivial_save (confd_file, &err)) {
+    if (!shell_utils_trivial_set_and_save (static_hostname_file, &err, "hostname", "HOSTNAME", name, NULL)) {
         g_dbus_method_invocation_return_gerror (invocation, err);
         G_UNLOCK (static_hostname);
         goto end;
@@ -240,24 +223,10 @@ on_handle_set_machine_info (OpenrcSettingsdHostnamedHostname1 *hostname1,
     if (name == NULL)
         name = "";
 
-    confd_file = shell_utils_trivial_new (SYSCONFDIR "/machine-info", &err);
-    if (confd_file == NULL) {
-        g_dbus_method_invocation_return_gerror (invocation, err);
-        G_UNLOCK (machine_info);
-        goto end;
-    }
-
-    if ((is_pretty_hostname && !shell_utils_trivial_set_variable (confd_file, "PRETTY_HOSTNAME", name, TRUE)) ||
-        (!is_pretty_hostname && !shell_utils_trivial_set_variable (confd_file, "ICON_NAME", name, TRUE)))
-    {
-        g_dbus_method_invocation_return_dbus_error (invocation,
-                                                    DBUS_ERROR_FAILED,
-                                                    "Failed to value in " SYSCONFDIR "/machine-info");
-        G_UNLOCK (machine_info);
-        goto end;
-    }
-
-    if (!shell_utils_trivial_save (confd_file, &err)) {
+    if ((is_pretty_hostname &&
+            !shell_utils_trivial_set_and_save (machine_info_file, &err, "PRETTY_HOSTNAME", NULL, name, NULL)) ||
+        (!is_pretty_hostname &&
+            !shell_utils_trivial_set_and_save (machine_info_file, &err, "ICON_NAME", NULL, name, NULL))) {
         g_dbus_method_invocation_return_gerror (invocation, err);
         G_UNLOCK (machine_info);
         goto end;
@@ -354,13 +323,16 @@ hostnamed_init (gboolean _read_only)
         hostname[0] = 0;
     }
 
-    static_hostname = shell_utils_source_var (SYSCONFDIR "/conf.d/hostname", "${hostname-${HOSTNAME-localhost}}", &err);
+    static_hostname_file = g_file_new_for_path (SYSCONFDIR "/conf.d/hostname");
+    machine_info_file = g_file_new_for_path (SYSCONFDIR "/machine-info");
+
+    static_hostname = shell_utils_source_var (static_hostname_file, "${hostname-${HOSTNAME-localhost}}", &err);
     if (err != NULL) {
         g_debug ("%s", err->message);
         g_error_free (err);
         err = NULL;
     }
-    pretty_hostname = shell_utils_source_var (SYSCONFDIR "/machine-info", "${PRETTY_HOSTNAME}", &err);
+    pretty_hostname = shell_utils_source_var (machine_info_file, "${PRETTY_HOSTNAME}", &err);
     if (pretty_hostname == NULL)
         pretty_hostname = g_strdup ("");
     if (err != NULL) {
@@ -368,7 +340,7 @@ hostnamed_init (gboolean _read_only)
         g_error_free (err);
         err = NULL;
     }
-    icon_name = shell_utils_source_var (SYSCONFDIR "/machine-info", "${ICON_NAME}", &err);
+    icon_name = shell_utils_source_var (machine_info_file, "${ICON_NAME}", &err);
     if (icon_name == NULL)
         icon_name = g_strdup ("");
     if (err != NULL) {
@@ -402,4 +374,7 @@ hostnamed_destroy (void)
     g_free (static_hostname);
     g_free (pretty_hostname);
     g_free (icon_name);
+
+    g_object_unref (static_hostname_file);
+    g_object_unref (machine_info_file);
 }
\ No newline at end of file

diff --git a/src/shell-utils.c b/src/shell-utils.c
index 6371ef1..4f76fee 100644
--- a/src/shell-utils.c
+++ b/src/shell-utils.c
@@ -16,15 +16,10 @@
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
+#include <stdarg.h>
 #include <string.h>
-#include <unistd.h>
-#include <sys/stat.h>
 
 #include <glib.h>
-#include <glib/gstdio.h>
 #include <gio/gio.h>
 
 #include "shell-utils.h"
@@ -53,23 +48,53 @@ struct ShellEntry {
 };
 
 gchar *
-shell_utils_source_var (const gchar *filename,
+shell_utils_source_var (GFile *file,
                         const gchar *variable,
                         GError **error)
 {
     gchar *argv[4] = { "sh", "-c", NULL, NULL };
-    gchar *quoted_filename = NULL;
+    gchar *filename, *quoted_filename;
     gchar *output = NULL;
+    GFileInfo *info;
+    const GFileAttributeInfo *attribute_info;
+
+    filename = g_file_get_path (file);
+    if ((info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_ACCESS_CAN_READ, G_FILE_QUERY_INFO_NONE, NULL, error)) == NULL) {
+        g_prefix_error (error, "Unable to source '%s': ", filename);
+        goto out;
+    }
+
+    if (g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR &&
+        g_file_info_get_file_type (info) != G_FILE_TYPE_SYMBOLIC_LINK) {
+        g_propagate_error (error,
+                           g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
+                                        "Unable to source '%s': not a regular file", filename));
+        goto out;
+    }
+
+    if (!g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ)) {
+        g_propagate_error (error,
+                           g_error_new (G_FILE_ERROR, G_FILE_ERROR_ACCES,
+                                        "Unable to source '%s': permission denied", filename));
+        goto out;
+    }
 
     quoted_filename = g_shell_quote (filename);
     argv[2] = g_strdup_printf (". %s; echo -n %s", quoted_filename, variable);
-    g_free (quoted_filename);
 
-    if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
-        if (!g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &output, NULL, NULL, error)) {
-            g_prefix_error (error, "Unable to source '%s': ", filename);
-        }
-    g_free (argv[2]);
+    if (!g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &output, NULL, NULL, error)) {
+        g_prefix_error (error, "Unable to source '%s': ", filename);
+    }
+
+  out:
+    if (filename != NULL)
+        g_free (filename);
+    if (quoted_filename != NULL)
+        g_free (quoted_filename);
+    if (info != NULL)
+        g_object_unref (info);
+    if (argv[2] != NULL)
+        g_free (argv[2]);
     return output;
 }
 
@@ -92,6 +117,8 @@ shell_utils_trivial_free (ShellUtilsTrivial *trivial)
     if (trivial == NULL)
         return;
 
+    if (trivial->file != NULL)
+        g_object_unref (trivial->file);
     if (trivial->filename != NULL)
         g_free (trivial->filename);
     if (trivial->entry_list != NULL)
@@ -100,7 +127,7 @@ shell_utils_trivial_free (ShellUtilsTrivial *trivial)
 }
 
 ShellUtilsTrivial *
-shell_utils_trivial_new (const gchar *filename,
+shell_utils_trivial_new (GFile *file,
                          GError **error)
 {
     gchar *filebuf = NULL;
@@ -108,20 +135,23 @@ shell_utils_trivial_new (const gchar *filename,
     GError *local_err;
     gchar *s;
 
-    g_assert (filename != NULL);
+    if (file == NULL)
+        return NULL;
 
     ret = g_new0 (ShellUtilsTrivial, 1);
-    ret->filename = g_strdup (filename);
+    g_object_ref (file);
+    ret->file = file;
+    ret->filename = g_file_get_path (file);
 
-    if (!g_file_get_contents (filename, &filebuf, NULL, &local_err)) {
+    if (!g_file_load_contents (file, NULL, &filebuf, NULL, NULL, &local_err)) {
         if (local_err != NULL) {
             /* Inability to parse or open is a failure; file not existing at all is *not* a failure */
-            if (local_err->code == G_FILE_ERROR_NOENT) {
+            if (local_err->code == G_IO_ERROR_NOT_FOUND) {
                 g_error_free (local_err);
                 return ret;
             }
 
-            g_propagate_prefixed_error (error, local_err, "Unable to read '%s':", filename);
+            g_propagate_prefixed_error (error, local_err, "Unable to read '%s':", ret->filename);
         }
         shell_utils_trivial_free (ret);
         return NULL;
@@ -261,7 +291,7 @@ no_match:
         match_info = NULL;
         g_propagate_error (error,
                            g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
-                                        "Unable to parse '%s'", filename));
+                                        "Unable to parse '%s'", ret->filename));
         shell_utils_trivial_free (ret);
         return NULL;
     }
@@ -317,86 +347,91 @@ gboolean
 shell_utils_trivial_save (ShellUtilsTrivial *trivial,
                           GError **error)
 {
+    gboolean ret = FALSE;
     GList *curr = NULL;
-    gchar *temp_filename = NULL;
-    gint fd;
-    gboolean retval = FALSE;
-    GStatBuf stat_buf;
-    gint mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; /* 644 by default */
+    GFileOutputStream *os;
 
-    g_assert (trivial != NULL);
-    g_assert (trivial->filename != NULL);
-
-    /* If the file exists, preserve its mode */
-    if (!g_lstat (trivial->filename, &stat_buf))
-        mode = stat_buf.st_mode;
-
-    temp_filename = g_strdup_printf ("%s.XXXXXX", trivial->filename);
-
-    if ((fd = g_mkstemp_full (temp_filename, O_WRONLY, mode)) < 0) {
-        int errsv = errno;
-        GFileError file_err;
-        file_err = g_file_error_from_errno (errsv);
-        g_propagate_prefixed_error (error,
-                                    g_error_copy ((GError*)&file_err),
-                                    "Unable to write '%s': Unable to create temp file: ",
-                                    trivial->filename);
+    g_assert (trivial != NULL && trivial->file != NULL && trivial->filename != NULL);
+    if ((os = g_file_replace (trivial->file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, error)) == NULL) {
+        g_prefix_error (error, "Unable to save '%s': ", trivial->filename);
         goto out;
     }
 
     for (curr = trivial->entry_list; curr != NULL; curr = curr->next) {
         struct ShellEntry *entry;
-        ssize_t written;
+        gsize written;
 
         entry = (struct ShellEntry *)(curr->data);
-        written = write (fd, entry->string, strlen (entry->string));
-        int errsv = errno;
-        if (written < strlen (entry->string)) {
-            if (written < 0) {
-                int errsv = errno;
-                GFileError file_err;
-                file_err = g_file_error_from_errno (errsv);
-                g_propagate_prefixed_error (error,
-                                            g_error_copy ((GError*)&file_err),
-                                            "Unable to write temp file '%s': ",
-                                            temp_filename);
-            } else {
-                g_propagate_error (error,
-                                   g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
-                                                "Unable to write temp file '%s'", temp_filename));
-            }
-            close (fd);
+        if (!g_output_stream_write_all (G_OUTPUT_STREAM (os), entry->string, strlen (entry->string), &written, NULL, error)) {
+            g_prefix_error (error, "Unable to save '%s': ", trivial->filename);
             goto out;
         }
     }
-    if (fsync (fd) || close (fd)) {
-        int errsv = errno;
-        GFileError file_err;
-        file_err = g_file_error_from_errno (errsv);
-        g_propagate_prefixed_error (error,
-                                    g_error_copy ((GError*)&file_err),
-                                    "Unable to sync or close temp file '%s': ",
-                                    temp_filename);
-        close (fd);
+    
+    if (!g_output_stream_close (G_OUTPUT_STREAM (os), NULL, error)) {
+        g_prefix_error (error, "Unable to save '%s': ", trivial->filename);
+        g_output_stream_close (G_OUTPUT_STREAM (os), NULL, NULL);
         goto out;
     }
+    ret = TRUE;
+
+  out:
+    if (os)
+        g_object_unref (os);
+    return ret;
+}
 
-    if (g_rename (temp_filename, trivial->filename) < 0) {
-        int errsv = errno;
-        GFileError file_err;
-        file_err = g_file_error_from_errno (errsv);
-        g_propagate_prefixed_error (error,
-                                    g_error_copy ((GError*)&file_err),
-                                    "Unable to copy '%s' to '%s': ",
-                                    temp_filename, trivial->filename);
+gboolean
+shell_utils_trivial_set_and_save (GFile *file,
+                                  GError **error,
+                                  const gchar *first_var_name,
+                                  const gchar *first_alt_var_name,
+                                  const gchar *first_value,
+                                  ...)
+{
+    va_list ap;
+    ShellUtilsTrivial *trivial;
+    gboolean ret = FALSE;
+    const gchar *var_name, *alt_var_name, *value;
+
+    va_start (ap, first_value);
+    if ((trivial = shell_utils_trivial_new (file, error)) == NULL)
         goto out;
-    }
-    retval = TRUE;
+
+    var_name = first_var_name;
+    alt_var_name = first_alt_var_name;
+    value = first_value;
+    do {
+        if (alt_var_name == NULL) {
+            if (!shell_utils_trivial_set_variable (trivial, var_name, value, TRUE)) {
+                g_propagate_error (error,
+                        g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
+                                    "Unable to set %s in '%s'", var_name, trivial->filename));
+                goto out;
+            }
+        } else {
+            if (!shell_utils_trivial_set_variable (trivial, var_name, value, FALSE) &&
+                !shell_utils_trivial_set_variable (trivial, alt_var_name, value, FALSE) &&
+                !shell_utils_trivial_set_variable (trivial, var_name, value, TRUE)) {
+                    g_propagate_error (error,
+                            g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
+                                        "Unable to set %s or %s in '%s'", var_name, alt_var_name, trivial->filename));
+                    goto out;
+            }
+        }
+    } while ((var_name = va_arg (ap, const gchar*)) != NULL ?
+                 alt_var_name = va_arg (ap, const gchar*), value = va_arg (ap, const gchar*), 1 : 0);
+
+    if (!shell_utils_trivial_save (trivial, error))
+        goto out;
+
+    ret = TRUE;
 
   out:
-    g_free (temp_filename);
-    g_unlink (temp_filename);
-    return retval;
+    va_end (ap);
+    if (trivial != NULL)
+        shell_utils_trivial_free (trivial);
+    return ret;
 }
 
 void

diff --git a/src/shell-utils.h b/src/shell-utils.h
index 861cd87..5e1863d 100644
--- a/src/shell-utils.h
+++ b/src/shell-utils.h
@@ -20,17 +20,19 @@
 #define _SHELL_UTILS_H_
 
 #include <glib.h>
+#include <gio/gio.h>
 
 typedef struct _ShellUtilsTrivial        ShellUtilsTrivial;
 
 struct _ShellUtilsTrivial
 {
+  GFile *file;
   gchar *filename;
   GList *entry_list;
 };
 
 gchar *
-shell_utils_source_var (const gchar *filename,
+shell_utils_source_var (GFile *file,
                         const gchar *variable,
                         GError **error);
 
@@ -41,7 +43,7 @@ void
 shell_utils_destroy (void);
 
 ShellUtilsTrivial *
-shell_utils_trivial_new (const gchar *filename,
+shell_utils_trivial_new (GFile *file,
                          GError **error);
 
 void
@@ -57,4 +59,11 @@ gboolean
 shell_utils_trivial_save (ShellUtilsTrivial *trivial,
                           GError **error);
 
+gboolean
+shell_utils_trivial_set_and_save (GFile *file,
+                                  GError **error,
+                                  const gchar *first_var_name,
+                                  const gchar *first_alt_var_name,
+                                  const gchar *first_value,
+                                  ...);
 #endif



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [gentoo-commits] proj/openrc-settingsd:master commit in: src/, /
@ 2012-02-05  7:49 Alexandre Restovtsev
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Restovtsev @ 2012-02-05  7:49 UTC (permalink / raw
  To: gentoo-commits

commit:     6a792d0ef603f04b560b6423881dd54d48c86615
Author:     Alexandre Rostovtsev <tetromino <AT> gentoo <DOT> org>
AuthorDate: Sun Feb  5 07:48:56 2012 +0000
Commit:     Alexandre Restovtsev <tetromino <AT> gmail <DOT> com>
CommitDate: Sun Feb  5 07:48:56 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/openrc-settingsd.git;a=commit;h=6a792d0e

Enable/disable debug at runtime via --debug flag

---
 configure.ac      |    9 ---------
 src/hostnamed.c   |    4 ----
 src/main.c        |   34 +++++++++++++++++++++++++++++++++-
 src/shell-utils.c |   14 --------------
 4 files changed, 33 insertions(+), 28 deletions(-)

diff --git a/configure.ac b/configure.ac
index 5abb61c..9e64fea 100644
--- a/configure.ac
+++ b/configure.ac
@@ -19,15 +19,6 @@ if test "x$GDBUS_CODEGEN" = x; then
     AC_MSG_ERROR([Failed to find gdbus-codegen])
 fi
 
-AC_ARG_ENABLE([debug],
-    [AS_HELP_STRING([--enable-debug],
-                    [enable extra debugging messages])],
-    [], [enable_debug=no])
-if test "x$enable_debug" = "xyes" ; then
-        AC_MSG_RESULT([enabling extra debugging messages])
-        AC_DEFINE(OPENRC_SETTINGSD_DEBUG,1,[Enable extra debugging messages.])
-fi
-
 AC_CONFIG_HEADERS([config.h])
 AC_CONFIG_FILES([
     Makefile

diff --git a/src/hostnamed.c b/src/hostnamed.c
index c21d875..4f95d04 100644
--- a/src/hostnamed.c
+++ b/src/hostnamed.c
@@ -261,9 +261,7 @@ on_bus_acquired (GDBusConnection *connection,
     gchar *name;
     GError *err = NULL;
 
-#ifdef OPENRC_SETTINGSD_DEBUG
     g_debug ("Acquired a message bus connection");
-#endif
 
     hostname1 = openrc_settingsd_hostnamed_hostname1_skeleton_new ();
 
@@ -293,9 +291,7 @@ on_name_acquired (GDBusConnection *connection,
                   const gchar     *bus_name,
                   gpointer         user_data)
 {
-#ifdef OPENRC_SETTINGSD_DEBUG
     g_debug ("Acquired the name %s", bus_name);
-#endif
 }
 
 static void

diff --git a/src/main.c b/src/main.c
index 1756b90..1cff0f5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -16,6 +16,9 @@
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
+#include <stdlib.h>
+#include <string.h>
+
 #include <glib.h>
 #include <gio/gio.h>
 
@@ -24,16 +27,45 @@
 
 #include "config.h"
 
+#define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE)
+
+static gboolean debug = FALSE;
+
+static GOptionEntry entries[] =
+{
+    { "debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Enable debugging messages", NULL },
+    { NULL }
+};
+
+static void
+log_handler (const gchar *log_domain,
+             GLogLevelFlags log_level,
+             const gchar *message,
+             gpointer user_data)
+{
+    if (debug || log_level & DEFAULT_LEVELS)
+        g_log_default_handler (log_domain, log_level, message, user_data);
+}
+
 gint
 main (gint argc, gchar *argv[])
 {
+    GError *error = NULL;
+    GOptionContext *context;
     GMainLoop *loop = NULL;
 
     g_type_init ();
+    g_log_set_default_handler (log_handler, NULL);
+
+    context = g_option_context_new ("- system settings D-Bus service for OpenRC");
+    g_option_context_add_main_entries (context, entries, NULL);
+    if (!g_option_context_parse (context, &argc, &argv, &error)) {
+        g_printerr ("Failed to parse options: %s\n", error->message);
+        exit (1);
+    }
 
     shell_utils_init ();
     hostnamed_init (FALSE);
-
     loop = g_main_loop_new (NULL, FALSE);
     g_main_loop_run (loop);
 

diff --git a/src/shell-utils.c b/src/shell-utils.c
index 4f76fee..41a77db 100644
--- a/src/shell-utils.c
+++ b/src/shell-utils.c
@@ -160,9 +160,7 @@ shell_utils_trivial_new (GFile *file,
     gboolean want_separator = FALSE; /* Do we expect the next entry to be a separator or comment? */
     s = filebuf;
     while (*s != 0) {
-#ifdef OPENRC_SETTINGSD_DEBUG
         g_debug ("Scanning string: ``%s''", s);
-#endif
         gboolean matched = FALSE;
         GMatchInfo *match_info = NULL;
         struct ShellEntry *entry = NULL;
@@ -174,9 +172,7 @@ shell_utils_trivial_new (GFile *file,
             entry->string = g_match_info_fetch (match_info, 0);
             ret->entry_list = g_list_prepend (ret->entry_list, entry);
             s += strlen (entry->string);
-#ifdef OPENRC_SETTINGSD_DEBUG
             g_debug ("Scanned comment: ``%s''", entry->string);
-#endif
             g_match_info_free (match_info);
             match_info = NULL;
             want_separator = FALSE;
@@ -192,9 +188,7 @@ shell_utils_trivial_new (GFile *file,
             entry->string = g_match_info_fetch (match_info, 0);
             ret->entry_list = g_list_prepend (ret->entry_list, entry);
             s += strlen (entry->string);
-#ifdef OPENRC_SETTINGSD_DEBUG
             g_debug ("Scanned separator: ``%s''", entry->string);
-#endif
             g_match_info_free (match_info);
             match_info = NULL;
             want_separator = FALSE;
@@ -210,9 +204,7 @@ shell_utils_trivial_new (GFile *file,
             entry->string = g_match_info_fetch (match_info, 0);
             ret->entry_list = g_list_prepend (ret->entry_list, entry);
             s += strlen (entry->string);
-#ifdef OPENRC_SETTINGSD_DEBUG
             g_debug ("Scanned indent: ``%s''", entry->string);
-#endif
             g_match_info_free (match_info);
             match_info = NULL;
             continue;
@@ -231,17 +223,13 @@ shell_utils_trivial_new (GFile *file,
             entry->string = g_match_info_fetch (match_info, 0);
             entry->variable = g_match_info_fetch (match_info, 1);
             s += strlen (entry->string);
-#ifdef OPENRC_SETTINGSD_DEBUG
             g_debug ("Scanned variable: ``%s''", entry->string);
-#endif
             g_match_info_free (match_info);
             match_info = NULL;
             want_separator = TRUE;
 
             while (*s != 0) {
-#ifdef OPENRC_SETTINGSD_DEBUG
                 g_debug ("Scanning string for values: ``%s''", s);
-#endif
                 gboolean matched2 = FALSE;
                 gchar *temp1 = NULL, *temp2 = NULL;
 
@@ -272,9 +260,7 @@ append_value:
                 temp2 = g_match_info_fetch (match_info, 0);
                 entry->string = g_strconcat (temp1, temp2, NULL);
                 s += strlen (temp2);
-#ifdef OPENRC_SETTINGSD_DEBUG
                 g_debug ("Scanned value: ``%s''", temp2);
-#endif
                 g_free (temp1);
                 g_free (temp2);
                 g_match_info_free (match_info);



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [gentoo-commits] proj/openrc-settingsd:master commit in: src/, /
@ 2012-02-05  8:36 Alexandre Restovtsev
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Restovtsev @ 2012-02-05  8:36 UTC (permalink / raw
  To: gentoo-commits

commit:     ffefb55a4a1bc00bceaa1ba477cf1db3e8635589
Author:     Alexandre Rostovtsev <tetromino <AT> gentoo <DOT> org>
AuthorDate: Sun Feb  5 08:35:50 2012 +0000
Commit:     Alexandre Restovtsev <tetromino <AT> gmail <DOT> com>
CommitDate: Sun Feb  5 08:36:23 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/openrc-settingsd.git;a=commit;h=ffefb55a

Add --read-only flag

---
 TODO       |    2 --
 src/main.c |    4 +++-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/TODO b/TODO
index 62ec523..d6bebd9 100644
--- a/TODO
+++ b/TODO
@@ -7,8 +7,6 @@ document that this case is not supported?
 Write an init.d file to read RC_SYS so that we can piggyback on openrc's
 built-in virtualization detection.
 
-Add command-line arguments (e.g. --no-hostnamed --readonly-localed).
-
 Implement localed and timedated.
 
 Do something about runtime dependency on systemd's org.freedesktop.hostname1.policy,

diff --git a/src/main.c b/src/main.c
index 7463fd3..9212eb6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -30,10 +30,12 @@
 #define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE)
 
 static gboolean debug = FALSE;
+static gboolean read_only = FALSE;
 
 static GOptionEntry option_entries[] =
 {
     { "debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Enable debugging messages", NULL },
+    { "read-only", 0, 0, G_OPTION_ARG_NONE, &read_only, "Run in read-only mode", NULL },
     { NULL }
 };
 
@@ -65,7 +67,7 @@ main (gint argc, gchar *argv[])
     }
 
     shell_utils_init ();
-    hostnamed_init (FALSE);
+    hostnamed_init (read_only);
     loop = g_main_loop_new (NULL, FALSE);
     g_main_loop_run (loop);
 



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [gentoo-commits] proj/openrc-settingsd:master commit in: src/, /
@ 2012-02-06 19:27 Alexandre Restovtsev
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Restovtsev @ 2012-02-06 19:27 UTC (permalink / raw
  To: gentoo-commits

commit:     f5f711762c6dd199a5789a317eaa2e788fbe6bb5
Author:     Alexandre Rostovtsev <tetromino <AT> gentoo <DOT> org>
AuthorDate: Mon Feb  6 19:25:25 2012 +0000
Commit:     Alexandre Restovtsev <tetromino <AT> gmail <DOT> com>
CommitDate: Mon Feb  6 19:26:31 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/openrc-settingsd.git;a=commit;h=f5f71176

newline at end of file

---
 README            |    2 +-
 src/bus-utils.c   |    2 +-
 src/bus-utils.h   |    2 +-
 src/hostnamed.c   |    2 +-
 src/main.c        |    2 +-
 src/shell-utils.c |    2 +-
 6 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/README b/README
index b1c01bc..62bd904 100644
--- a/README
+++ b/README
@@ -25,4 +25,4 @@ foo="bar"
 baz='Let'\''s go!'
 
 If openrc-settingsd fails to parse a settings file, it will refuse to modify
-it.
\ No newline at end of file
+it.

diff --git a/src/bus-utils.c b/src/bus-utils.c
index aef8037..f78f8f2 100644
--- a/src/bus-utils.c
+++ b/src/bus-utils.c
@@ -131,4 +131,4 @@ check_polkit_async (const gchar *unique_name,
     data->user_data = user_data;
 
     polkit_authority_get_async (NULL, check_polkit_authority_cb, data);
-}
\ No newline at end of file
+}

diff --git a/src/bus-utils.h b/src/bus-utils.h
index f2e80ea..4a57bb5 100644
--- a/src/bus-utils.h
+++ b/src/bus-utils.h
@@ -32,4 +32,4 @@ gboolean
 check_polkit_finish (GAsyncResult *res,
                      GError **error);
 
-#endif
\ No newline at end of file
+#endif

diff --git a/src/hostnamed.c b/src/hostnamed.c
index c5914bd..c5f6f9c 100644
--- a/src/hostnamed.c
+++ b/src/hostnamed.c
@@ -476,4 +476,4 @@ hostnamed_destroy (void)
 
     g_object_unref (static_hostname_file);
     g_object_unref (machine_info_file);
-}
\ No newline at end of file
+}

diff --git a/src/main.c b/src/main.c
index 9212eb6..c7165f0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -75,4 +75,4 @@ main (gint argc, gchar *argv[])
     hostnamed_destroy ();
     shell_utils_destroy ();
     return 0;
-}
\ No newline at end of file
+}

diff --git a/src/shell-utils.c b/src/shell-utils.c
index 41a77db..30026a9 100644
--- a/src/shell-utils.c
+++ b/src/shell-utils.c
@@ -486,4 +486,4 @@ shell_utils_init (void)
         unquoted_regex = g_regex_new ("^(?:[^\\s\"'`\\$\\|&<>;]|\\\\[\\s\"'`\\$\\|&<>;]|\\$\\{)+", G_REGEX_ANCHORED|G_REGEX_MULTILINE, 0, NULL);
         g_assert (unquoted_regex != NULL);
     }
-}
\ No newline at end of file
+}



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [gentoo-commits] proj/openrc-settingsd:master commit in: src/, /
@ 2012-02-08  5:38 Alexandre Restovtsev
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Restovtsev @ 2012-02-08  5:38 UTC (permalink / raw
  To: gentoo-commits

commit:     e18e537cd0a26073f216d6dbde66bed8845d00c4
Author:     Alexandre Rostovtsev <tetromino <AT> gentoo <DOT> org>
AuthorDate: Wed Feb  8 05:35:21 2012 +0000
Commit:     Alexandre Restovtsev <tetromino <AT> gmail <DOT> com>
CommitDate: Wed Feb  8 05:35:21 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/openrc-settingsd.git;a=commit;h=e18e537c

Rename generated files to match dbus interface

---
 Makefile.am     |    8 ++++----
 src/hostnamed.c |    2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 6b6cb78..f2a770a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -18,8 +18,8 @@ LDADD = $(OPENRC_SETTINGSD_LIBS)
 libexec_PROGRAMS = openrc-settingsd
 
 hostnamed_built_sources = \
-	src/hostnamed-generated.c \
-	src/hostnamed-generated.h \
+	src/hostname1-generated.c \
+	src/hostname1-generated.h \
 	$(NULL)
 
 openrc_settingsd_SOURCES = \
@@ -37,9 +37,9 @@ $(hostnamed_built_sources) : data/hostname1.xml
 	( $(GDBUS_CODEGEN) \
 	--interface-prefix org.freedesktop. \
 	--c-namespace OpenrcSettingsdHostnamed \
-	--generate-c-code hostnamed-generated \
+	--generate-c-code hostname1-generated \
 	$(srcdir)/data/hostname1.xml; \
-	mv hostnamed-generated.{c,h} $(top_srcdir)/src/ )
+	mv hostname1-generated.{c,h} $(top_srcdir)/src/ )
 
 BUILT_SOURCES = $(hostnamed_built_sources)
 CLEANFILES = $(hostnamed_built_sources)

diff --git a/src/hostnamed.c b/src/hostnamed.c
index 88c166e..df522c5 100644
--- a/src/hostnamed.c
+++ b/src/hostnamed.c
@@ -27,7 +27,7 @@
 #include <polkit/polkit.h>
 
 #include "hostnamed.h"
-#include "hostnamed-generated.h"
+#include "hostname1-generated.h"
 #include "bus-utils.h"
 #include "shell-utils.h"
 



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [gentoo-commits] proj/openrc-settingsd:master commit in: src/, /
@ 2012-09-05 23:46 Alexandre Restovtsev
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Restovtsev @ 2012-09-05 23:46 UTC (permalink / raw
  To: gentoo-commits

commit:     6345767b932f2ac1927be91d0213be251391f30b
Author:     Alexandre Rostovtsev <tetromino <AT> gentoo <DOT> org>
AuthorDate: Wed Sep  5 21:44:22 2012 +0000
Commit:     Alexandre Restovtsev <tetromino <AT> gmail <DOT> com>
CommitDate: Wed Sep  5 23:09:32 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/openrc-settingsd.git;a=commit;h=6345767b

Merge *-utils, rename ShellUtilsTrivial to ShellParser, fix memory leaks

---
 Makefile.am                    |    6 +-
 src/bus-utils.c                |  109 --------------
 src/bus-utils.h                |   11 +--
 src/hostnamed.c                |   47 ++++---
 src/localed.c                  |   59 ++++----
 src/main.c                     |    6 +-
 src/shell-utils.h              |   86 -----------
 src/timedated.c                |    9 +-
 src/{shell-utils.c => utils.c} |  308 +++++++++++++++++++++++++++-------------
 src/utils.h                    |  102 +++++++++++++
 10 files changed, 378 insertions(+), 365 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 32890fb..e12c3d7 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -59,10 +59,8 @@ openrc_settingsd_SOURCES = \
 	src/localed.h \
 	src/timedated.c \
 	src/timedated.h \
-	src/bus-utils.c \
-	src/bus-utils.h \
-	src/shell-utils.c \
-	src/shell-utils.h \
+	src/utils.c \
+	src/utils.h \
 	src/main.c \
 	$(NULL)
 

diff --git a/src/bus-utils.c b/src/bus-utils.c
index f78f8f2..8db9bb4 100644
--- a/src/bus-utils.c
+++ b/src/bus-utils.c
@@ -22,113 +22,4 @@
 
 #include "bus-utils.h"
 
-struct check_polkit_data {
-    const gchar *unique_name;
-    const gchar *action_id;
-    gboolean user_interaction;
-    GAsyncReadyCallback callback;
-    gpointer user_data;
 
-    PolkitAuthority *authority;
-    PolkitSubject *subject;
-};
-
-void
-check_polkit_data_free (struct check_polkit_data *data)
-{
-    if (data == NULL)
-        return;
-
-    if (data->subject != NULL)
-        g_object_unref (data->subject);
-    if (data->authority != NULL)
-        g_object_unref (data->authority);
-    
-    g_free (data);
-}
-
-gboolean
-check_polkit_finish (GAsyncResult *res,
-                     GError **error)
-{
-    GSimpleAsyncResult *simple;
-
-    simple = G_SIMPLE_ASYNC_RESULT (res);
-    if (g_simple_async_result_propagate_error (simple, error))
-        return FALSE;
-
-    return g_simple_async_result_get_op_res_gboolean (simple);
-}
-
-static void
-check_polkit_authorization_cb (GObject *source_object,
-                               GAsyncResult *res,
-                               gpointer _data)
-{
-    struct check_polkit_data *data;
-    PolkitAuthorizationResult *result;
-    GSimpleAsyncResult *simple;
-    GError *err = NULL;
-
-    data = (struct check_polkit_data *) _data;
-    if ((result = polkit_authority_check_authorization_finish (data->authority, res, &err)) == NULL) {
-        g_simple_async_report_take_gerror_in_idle (NULL, data->callback, data->user_data, err);
-        goto out;
-    }
- 
-    if (!polkit_authorization_result_get_is_authorized (result)) {
-        g_simple_async_report_error_in_idle (NULL, data->callback, data->user_data, POLKIT_ERROR, POLKIT_ERROR_NOT_AUTHORIZED, "Authorizing for '%s': not authorized", data->action_id);
-        goto out;
-    }
-    simple = g_simple_async_result_new (NULL, data->callback, data->user_data, check_polkit_async);
-    g_simple_async_result_set_op_res_gboolean (simple, TRUE);
-    g_simple_async_result_complete_in_idle (simple);
-    g_object_unref (simple);
-
-  out:
-    check_polkit_data_free (data);
-    if (result != NULL)
-        g_object_unref (result);
-}
-
-static void
-check_polkit_authority_cb (GObject *source_object,
-                           GAsyncResult *res,
-                           gpointer _data)
-{
-    struct check_polkit_data *data;
-    GError *err = NULL;
-
-    data = (struct check_polkit_data *) _data;
-    if ((data->authority = polkit_authority_get_finish (res, &err)) == NULL) {
-        g_simple_async_report_take_gerror_in_idle (NULL, data->callback, data->user_data, err);
-        check_polkit_data_free (data);
-        return;
-    }
-    if (data->unique_name == NULL || data->action_id == NULL || 
-        (data->subject = polkit_system_bus_name_new (data->unique_name)) == NULL) {
-        g_simple_async_report_error_in_idle (NULL, data->callback, data->user_data, POLKIT_ERROR, POLKIT_ERROR_FAILED, "Authorizing for '%s': failed sanity check", data->action_id);
-        check_polkit_data_free (data);
-        return;
-    }
-    polkit_authority_check_authorization (data->authority, data->subject, data->action_id, NULL, (PolkitCheckAuthorizationFlags) data->user_interaction, NULL, check_polkit_authorization_cb, data);
-}
-
-void
-check_polkit_async (const gchar *unique_name,
-                    const gchar *action_id,
-                    const gboolean user_interaction,
-                    GAsyncReadyCallback callback,
-                    gpointer user_data)
-{
-    struct check_polkit_data *data;
-
-    data = g_new0 (struct check_polkit_data, 1);
-    data->unique_name = unique_name;
-    data->action_id = action_id;
-    data->user_interaction = user_interaction;
-    data->callback = callback;
-    data->user_data = user_data;
-
-    polkit_authority_get_async (NULL, check_polkit_authority_cb, data);
-}

diff --git a/src/bus-utils.h b/src/bus-utils.h
index 4a57bb5..282a63e 100644
--- a/src/bus-utils.h
+++ b/src/bus-utils.h
@@ -21,15 +21,6 @@
 
 #include <glib.h>
 
-void
-check_polkit_async (const gchar *unique_name,
-                    const gchar *action_id,
-                    const gboolean user_interaction,
-                    GAsyncReadyCallback callback,
-                    gpointer user_data);
-
-gboolean
-check_polkit_finish (GAsyncResult *res,
-                     GError **error);
+
 
 #endif

diff --git a/src/hostnamed.c b/src/hostnamed.c
index f0992bd..1fb14c0 100644
--- a/src/hostnamed.c
+++ b/src/hostnamed.c
@@ -28,8 +28,7 @@
 
 #include "hostnamed.h"
 #include "hostname1-generated.h"
-#include "bus-utils.h"
-#include "shell-utils.h"
+#include "utils.h"
 
 #include "config.h"
 
@@ -66,10 +65,12 @@ hostname_is_valid (const gchar *name)
                                  name, G_REGEX_MULTILINE, 0);
 }
 
-static void
+static gchar *
 guess_icon_name ()
 {
     /* TODO: detect virtualization by reading rc_sys */
+    gchar *filebuf = NULL;
+    gchar *ret = NULL;
 
 #if defined(__i386__) || defined(__x86_64__)
     /* 
@@ -80,32 +81,34 @@ guess_icon_name ()
 
        http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
     */
-    gchar *contents;
 
-    if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &contents, NULL, NULL)) {
-        switch (g_ascii_strtoull (contents, NULL, 10)) {
+    if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
+        switch (g_ascii_strtoull (filebuf, NULL, 10)) {
         case 0x3:
         case 0x4:
         case 0x5:
         case 0x6:
         case 0x7:
-            icon_name = g_strdup ("computer-desktop");
-            return;
+            ret = g_strdup ("computer-desktop");
+            goto out;
         case 0x9:
         case 0xA:
         case 0xE:
-            icon_name = g_strdup ("computer-laptop");
-            return;
+            ret = g_strdup ("computer-laptop");
+            goto out;
         case 0x11:
         case 0x17:
         case 0x1C:
         case 0x1D:
-            icon_name = g_strdup ("computer-server");
-            return;
+            ret = g_strdup ("computer-server");
+            goto out;
         }
     }
 #endif
-    icon_name = g_strdup ("computer");
+    ret = g_strdup ("computer");
+  out:
+    g_free (filebuf);
+    return ret;
 }
 
 static void
@@ -198,7 +201,7 @@ on_handle_set_static_hostname_authorized_cb (GObject *source_object,
         data->name = g_strdup ("localhost");
     }
 
-    if (!shell_utils_trivial_set_and_save (static_hostname_file, &err, "hostname", "HOSTNAME", data->name, NULL)) {
+    if (!shell_parser_set_and_save (static_hostname_file, &err, "hostname", "HOSTNAME", data->name, NULL)) {
         g_dbus_method_invocation_return_gerror (data->invocation, err);
         G_UNLOCK (static_hostname);
         goto out;
@@ -257,7 +260,7 @@ on_handle_set_pretty_hostname_authorized_cb (GObject *source_object,
     if (data->name == NULL)
         data->name = g_strdup ("");
 
-    if (!shell_utils_trivial_set_and_save (machine_info_file, &err, "PRETTY_HOSTNAME", NULL, data->name, NULL)) {
+    if (!shell_parser_set_and_save (machine_info_file, &err, "PRETTY_HOSTNAME", NULL, data->name, NULL)) {
         g_dbus_method_invocation_return_gerror (data->invocation, err);
         G_UNLOCK (machine_info);
         goto out;
@@ -316,7 +319,7 @@ on_handle_set_icon_name_authorized_cb (GObject *source_object,
     if (data->name == NULL)
         data->name = g_strdup ("");
 
-    if (!shell_utils_trivial_set_and_save (machine_info_file, &err, "ICON_NAME", NULL, data->name, NULL)) {
+    if (!shell_parser_set_and_save (machine_info_file, &err, "ICON_NAME", NULL, data->name, NULL)) {
         g_dbus_method_invocation_return_gerror (data->invocation, err);
         G_UNLOCK (machine_info);
         goto out;
@@ -425,13 +428,13 @@ hostnamed_init (gboolean _read_only)
     static_hostname_file = g_file_new_for_path (SYSCONFDIR "/conf.d/hostname");
     machine_info_file = g_file_new_for_path (SYSCONFDIR "/machine-info");
 
-    static_hostname = shell_utils_source_var (static_hostname_file, "${hostname-${HOSTNAME-localhost}}", &err);
+    static_hostname = shell_source_var (static_hostname_file, "${hostname-${HOSTNAME-localhost}}", &err);
     if (err != NULL) {
         g_debug ("%s", err->message);
         g_error_free (err);
         err = NULL;
     }
-    pretty_hostname = shell_utils_source_var (machine_info_file, "${PRETTY_HOSTNAME}", &err);
+    pretty_hostname = shell_source_var (machine_info_file, "${PRETTY_HOSTNAME}", &err);
     if (pretty_hostname == NULL)
         pretty_hostname = g_strdup ("");
     if (err != NULL) {
@@ -439,7 +442,7 @@ hostnamed_init (gboolean _read_only)
         g_error_free (err);
         err = NULL;
     }
-    icon_name = shell_utils_source_var (machine_info_file, "${ICON_NAME}", &err);
+    icon_name = shell_source_var (machine_info_file, "${ICON_NAME}", &err);
     if (icon_name == NULL)
         icon_name = g_strdup ("");
     if (err != NULL) {
@@ -448,8 +451,10 @@ hostnamed_init (gboolean _read_only)
         err = NULL;
     }
 
-    if (icon_name == NULL || strlen (icon_name) == 0)
-        guess_icon_name ();
+    if (icon_name == NULL || *icon_name == 0) {
+        g_free (icon_name);
+        icon_name = guess_icon_name ();
+    }
 
     read_only = _read_only;
 

diff --git a/src/localed.c b/src/localed.c
index 2e8e44e..0cb4404 100644
--- a/src/localed.c
+++ b/src/localed.c
@@ -26,8 +26,7 @@
 
 #include "localed.h"
 #include "locale1-generated.h"
-#include "bus-utils.h"
-#include "shell-utils.h"
+#include "utils.h"
 
 #include "config.h"
 
@@ -215,16 +214,17 @@ kbd_model_map_load (GError **error)
     for (line = filebuf; *line != 0; line = newline + 1) {
         struct kbd_model_map_entry *entry = NULL;
         GMatchInfo *match_info = NULL;
+        gboolean m = FALSE;
 
         if ((newline = strstr (line, "\n")) != NULL)
             *newline = 0;
         else
             newline = line + strlen (line) - 1;
 
-        if (g_regex_match (kbd_model_map_line_comment_re, line, 0, &match_info)) {
-            g_match_info_free (match_info);
+        m = g_regex_match (kbd_model_map_line_comment_re, line, 0, &match_info);
+        _g_match_info_clear (&match_info);
+        if (m)
             continue;
-        }
 
         if (!g_regex_match (kbd_model_map_line_re, line,  0, &match_info)) {
             g_propagate_error (error,
@@ -253,7 +253,7 @@ kbd_model_map_load (GError **error)
             entry->x11_options[0] = 0;
 
         ret = g_list_prepend (ret, entry);
-        g_match_info_free (match_info);
+        _g_match_info_clear (&match_info);
     }
   out:
     if (ret != NULL)
@@ -452,6 +452,7 @@ xorg_confd_parser_new (GFile *xorg_confd_file,
         struct xorg_confd_line_entry *entry = NULL;
         GMatchInfo *match_info = NULL;
         gboolean matched = FALSE;
+        gboolean m = FALSE;
 
         if ((newline = strstr (line, "\n")) != NULL)
             *newline = 0;
@@ -463,48 +464,48 @@ xorg_confd_parser_new (GFile *xorg_confd_file,
         if (g_regex_match (xorg_confd_line_comment_re, line, 0, &match_info)) {
             g_debug ("Parsed line '%s' as comment", line);
             entry->type = XORG_CONFD_LINE_TYPE_COMMENT;
-        } else if (g_regex_match (xorg_confd_line_section_input_class_re, line, 0, &match_info)) {
+        } else if (_g_match_info_clear (&match_info) && g_regex_match (xorg_confd_line_section_input_class_re, line, 0, &match_info)) {
             g_debug ("Parsed line '%s' as InputClass section", line);
             if (in_section)
                 goto no_match;
             in_section = TRUE;
             entry->type = XORG_CONFD_LINE_TYPE_SECTION_INPUT_CLASS;
-        } else if (g_regex_match (xorg_confd_line_section_re, line, 0, &match_info)) {
+        } else if (_g_match_info_clear (&match_info) && g_regex_match (xorg_confd_line_section_re, line, 0, &match_info)) {
             g_debug ("Parsed line '%s' as non-InputClass section", line);
             if (in_section)
                 goto no_match;
             in_section = TRUE;
             entry->type = XORG_CONFD_LINE_TYPE_SECTION_OTHER;
-        } else if (g_regex_match (xorg_confd_line_end_section_re, line, 0, &match_info)) {
+        } else if (_g_match_info_clear (&match_info) && g_regex_match (xorg_confd_line_end_section_re, line, 0, &match_info)) {
             g_debug ("Parsed line '%s' as end of section", line);
             if (!in_section)
                 goto no_match;
             entry->type = XORG_CONFD_LINE_TYPE_END_SECTION;
-        } else if (g_regex_match (xorg_confd_line_match_is_keyboard_re, line, 0, &match_info)) {
+        } else if (_g_match_info_clear (&match_info) && g_regex_match (xorg_confd_line_match_is_keyboard_re, line, 0, &match_info)) {
             g_debug ("Parsed line '%s' as MatchIsKeyboard declaration", line);
             if (!in_section)
                 goto no_match;
             entry->type = XORG_CONFD_LINE_TYPE_MATCH_IS_KEYBOARD;
             in_xkb_section = TRUE;
-        } else if (g_regex_match (xorg_confd_line_xkb_layout_re, line, 0, &match_info)) {
+        } else if (_g_match_info_clear (&match_info) && g_regex_match (xorg_confd_line_xkb_layout_re, line, 0, &match_info)) {
             g_debug ("Parsed line '%s' as XkbLayout option", line);
             if (!in_section)
                 goto no_match;
             entry->type = XORG_CONFD_LINE_TYPE_XKB_LAYOUT;
             entry->value = g_match_info_fetch (match_info, 2);
-        } else if (g_regex_match (xorg_confd_line_xkb_model_re, line, 0, &match_info)) {
+        } else if (_g_match_info_clear (&match_info) && g_regex_match (xorg_confd_line_xkb_model_re, line, 0, &match_info)) {
             g_debug ("Parsed line '%s' as XkbModel option", line);
             if (!in_section)
                 goto no_match;
             entry->type = XORG_CONFD_LINE_TYPE_XKB_MODEL;
             entry->value = g_match_info_fetch (match_info, 2);
-        } else if (g_regex_match (xorg_confd_line_xkb_variant_re, line, 0, &match_info)) {
+        } else if (_g_match_info_clear (&match_info) && g_regex_match (xorg_confd_line_xkb_variant_re, line, 0, &match_info)) {
             g_debug ("Parsed line '%s' as XkbVariant option", line);
             if (!in_section)
                 goto no_match;
             entry->type = XORG_CONFD_LINE_TYPE_XKB_VARIANT;
             entry->value = g_match_info_fetch (match_info, 2);
-        } else if (g_regex_match (xorg_confd_line_xkb_options_re, line, 0, &match_info)) {
+        } else if (_g_match_info_clear (&match_info) && g_regex_match (xorg_confd_line_xkb_options_re, line, 0, &match_info)) {
             g_debug ("Parsed line '%s' as XkbOptions option", line);
             if (!in_section)
                 goto no_match;
@@ -515,7 +516,7 @@ xorg_confd_parser_new (GFile *xorg_confd_file,
         if (entry->type == XORG_CONFD_LINE_TYPE_UNKNOWN)
             g_debug ("Parsing line '%s' as unknown", line);
 
-        g_match_info_free (match_info);
+        _g_match_info_clear (&match_info);
         parser->line_list = g_list_prepend (parser->line_list, entry);
         if (in_section) {
             if (entry->type == XORG_CONFD_LINE_TYPE_SECTION_INPUT_CLASS)
@@ -534,7 +535,7 @@ xorg_confd_parser_new (GFile *xorg_confd_file,
   no_match:
         /* Nothing matched... */
         g_free (entry);
-        g_match_info_free (match_info);
+        _g_match_info_clear (&match_info);
         goto parse_fail;
     }
 
@@ -787,7 +788,7 @@ on_handle_set_locale_authorized_cb (GObject *source_object,
     GError *err = NULL;
     struct invoked_locale *data;
     gchar **loc, **var, **val, **locale_values = NULL;
-    ShellUtilsTrivial *locale_file_parsed = NULL;
+    ShellParser *locale_file_parsed = NULL;
 
     data = (struct invoked_locale *) user_data;
     if (!check_polkit_finish (res, &err)) {
@@ -825,16 +826,16 @@ on_handle_set_locale_authorized_cb (GObject *source_object,
         }
     }
 
-    if ((locale_file_parsed = shell_utils_trivial_new (locale_file, &err)) == NULL) {
+    if ((locale_file_parsed = shell_parser_new (locale_file, &err)) == NULL) {
         g_dbus_method_invocation_return_gerror (data->invocation, err);
         G_UNLOCK (locale);
         goto out;
     }
 
-    if (shell_utils_trivial_is_empty (locale_file_parsed)) {
+    if (shell_parser_is_empty (locale_file_parsed)) {
         /* Simply write the new env file */
-        shell_utils_trivial_free (locale_file_parsed);
-        if ((locale_file_parsed = shell_utils_trivial_new_from_string (locale_file, "# Configuration file for eselect\n# This file has been automatically generated\n", &err)) == NULL) {
+        shell_parser_free (locale_file_parsed);
+        if ((locale_file_parsed = shell_parser_new_from_string (locale_file, "# Configuration file for eselect\n# This file has been automatically generated\n", &err)) == NULL) {
             g_dbus_method_invocation_return_gerror (data->invocation, err);
             G_UNLOCK (locale);
             goto out;
@@ -843,12 +844,12 @@ on_handle_set_locale_authorized_cb (GObject *source_object,
 
     for (val = locale_values, var = locale_variables; *var != NULL; val++, var++) {
         if (*val == NULL)
-            shell_utils_trivial_clear_variable (locale_file_parsed, *var);
+            shell_parser_clear_variable (locale_file_parsed, *var);
         else
-            shell_utils_trivial_set_variable (locale_file_parsed, *var, *val, TRUE);
+            shell_parser_set_variable (locale_file_parsed, *var, *val, TRUE);
     }
 
-    if (!shell_utils_trivial_save (locale_file_parsed, &err)) {
+    if (!shell_parser_save (locale_file_parsed, &err)) {
         g_dbus_method_invocation_return_gerror (data->invocation, err);
         G_UNLOCK (locale);
         goto out;
@@ -869,7 +870,7 @@ on_handle_set_locale_authorized_cb (GObject *source_object,
     G_UNLOCK (locale);
 
   out:
-    shell_utils_trivial_free (locale_file_parsed);
+    shell_parser_free (locale_file_parsed);
     g_strfreev (locale_values);
     invoked_locale_free (data);
     if (err != NULL)
@@ -954,7 +955,7 @@ on_handle_set_vconsole_keyboard_authorized_cb (GObject *source_object,
     }
 
     /* We do not set vconsole_keymap_toggle because there is no good equivalent for it in OpenRC */
-    if (!shell_utils_trivial_set_and_save (keymaps_file, &err, "keymap", NULL, data->vconsole_keymap, NULL)) {
+    if (!shell_parser_set_and_save (keymaps_file, &err, "keymap", NULL, data->vconsole_keymap, NULL)) {
         g_dbus_method_invocation_return_gerror (data->invocation, err);
         goto unlock;
     }
@@ -1145,7 +1146,7 @@ on_handle_set_x11_keyboard_authorized_cb (GObject *source_object,
             g_printerr ("Failed to find conversion entry for x11 layout '%s' in '%s'\n", data->x11_layout, filename);
             g_free (filename);
         } else {
-            if (!shell_utils_trivial_set_and_save (keymaps_file, &err, "keymap", NULL, best_entry->vconsole_keymap, NULL)) {
+            if (!shell_parser_set_and_save (keymaps_file, &err, "keymap", NULL, best_entry->vconsole_keymap, NULL)) {
                 g_dbus_method_invocation_return_gerror (data->invocation, err);
                 goto unlock;
             }
@@ -1273,7 +1274,7 @@ localed_init (gboolean _read_only)
     x11_systemd_file = g_file_new_for_path (SYSCONFDIR "/X11/xorg.conf.d/00-keyboard.conf");
 
     locale = g_new0 (gchar *, g_strv_length (locale_variables) + 1);
-    locale_values = shell_utils_trivial_source_var_list (locale_file, (const gchar * const *)locale_variables, &err);
+    locale_values = shell_parser_source_var_list (locale_file, (const gchar * const *)locale_variables, &err);
     if (locale_values != NULL) {
         gchar **variable, **value, **loc;
         loc = locale;
@@ -1291,7 +1292,7 @@ localed_init (gboolean _read_only)
         g_clear_error (&err);
     }
 
-    vconsole_keymap = shell_utils_source_var (keymaps_file, "${keymap}", &err);
+    vconsole_keymap = shell_source_var (keymaps_file, "${keymap}", &err);
     if (vconsole_keymap == NULL)
         vconsole_keymap = g_strdup ("");
     if (err != NULL) {

diff --git a/src/main.c b/src/main.c
index b7d6324..54eb407 100644
--- a/src/main.c
+++ b/src/main.c
@@ -25,7 +25,7 @@
 #include "hostnamed.h"
 #include "localed.h"
 #include "timedated.h"
-#include "shell-utils.h"
+#include "utils.h"
 
 #include "config.h"
 
@@ -76,7 +76,7 @@ main (gint argc, gchar *argv[])
     } else
         g_log_set_default_handler (log_handler, NULL);
 
-    shell_utils_init ();
+    utils_init ();
     hostnamed_init (read_only);
     localed_init (read_only);
     timedated_init (read_only, ntp_preferred_service);
@@ -87,7 +87,7 @@ main (gint argc, gchar *argv[])
     timedated_destroy ();
     localed_destroy ();
     hostnamed_destroy ();
-    shell_utils_destroy ();
+    utils_destroy ();
     g_free (ntp_preferred_service);
     return 0;
 }

diff --git a/src/shell-utils.h b/src/shell-utils.h
deleted file mode 100644
index 629d017..0000000
--- a/src/shell-utils.h
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
-  Copyright 2012 Alexandre Rostovtsev
-
-  This program is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 2 of the License, or
-  (at your option) any later version.
-
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program; if not, write to the Free Software
-  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-*/
-
-#ifndef _SHELL_UTILS_H_
-#define _SHELL_UTILS_H_
-
-#include <glib.h>
-#include <gio/gio.h>
-
-typedef struct _ShellUtilsTrivial ShellUtilsTrivial;
-
-struct _ShellUtilsTrivial
-{
-  GFile *file;
-  gchar *filename;
-  GList *entry_list;
-};
-
-gchar *
-shell_utils_source_var (GFile *file,
-                        const gchar *variable,
-                        GError **error);
-
-void
-shell_utils_init (void);
-
-void
-shell_utils_destroy (void);
-
-ShellUtilsTrivial *
-shell_utils_trivial_new (GFile *file,
-                         GError **error);
-
-ShellUtilsTrivial *
-shell_utils_trivial_new_from_string (GFile *file,
-                                     gchar *filebuf,
-                                     GError **error);
-
-void
-shell_utils_trivial_free (ShellUtilsTrivial *trivial);
-
-gboolean
-shell_utils_trivial_is_empty (ShellUtilsTrivial *trivial);
-
-gboolean
-shell_utils_trivial_set_variable (ShellUtilsTrivial *trivial,
-                                  const gchar *variable,
-                                  const gchar *value,
-                                  gboolean add_if_unset);
-
-void
-shell_utils_trivial_clear_variable (ShellUtilsTrivial *trivial,
-                                    const gchar *variable);
-
-gboolean
-shell_utils_trivial_save (ShellUtilsTrivial *trivial,
-                          GError **error);
-
-gboolean
-shell_utils_trivial_set_and_save (GFile *file,
-                                  GError **error,
-                                  const gchar *first_var_name,
-                                  const gchar *first_alt_var_name,
-                                  const gchar *first_value,
-                                  ...);
-
-gchar **
-shell_utils_trivial_source_var_list (GFile *file,
-                                     const gchar * const *var_names,
-                                     GError **error);
-#endif

diff --git a/src/timedated.c b/src/timedated.c
index b8a41fc..d6588f4 100644
--- a/src/timedated.c
+++ b/src/timedated.c
@@ -30,8 +30,7 @@
 #include "copypaste/hwclock.h"
 #include "timedated.h"
 #include "timedate1-generated.h"
-#include "bus-utils.h"
-#include "shell-utils.h"
+#include "utils.h"
 
 #include "config.h"
 
@@ -61,7 +60,7 @@ get_local_rtc (GError **error)
     gchar *clock = NULL;
     gboolean ret = FALSE;
 
-    clock = shell_utils_source_var (hwclock_file, "${clock}", error);
+    clock = shell_source_var (hwclock_file, "${clock}", error);
     if (!g_strcmp0 (clock, "local"))
         ret = TRUE;
     return ret;
@@ -468,9 +467,9 @@ on_handle_set_local_rtc_authorized_cb (GObject *source_object,
     }
 
     G_LOCK (clock);
-    clock = shell_utils_source_var (hwclock_file, "${clock}", NULL);
+    clock = shell_source_var (hwclock_file, "${clock}", NULL);
     if (clock != NULL || data->local_rtc)
-        if (!shell_utils_trivial_set_and_save (hwclock_file, &err, "clock", NULL, clock_types[data->local_rtc], NULL)) {
+        if (!shell_parser_set_and_save (hwclock_file, &err, "clock", NULL, clock_types[data->local_rtc], NULL)) {
             g_dbus_method_invocation_return_gerror (data->invocation, err);
             goto unlock;
         }

diff --git a/src/shell-utils.c b/src/utils.c
similarity index 66%
rename from src/shell-utils.c
rename to src/utils.c
index f5c9b20..d21f9f4 100644
--- a/src/shell-utils.c
+++ b/src/utils.c
@@ -21,8 +21,9 @@
 
 #include <glib.h>
 #include <gio/gio.h>
+#include <polkit/polkit.h>
 
-#include "shell-utils.h"
+#include "utils.h"
 
 #include "config.h"
 
@@ -34,6 +35,128 @@ GRegex *single_quoted_regex = NULL;
 GRegex *double_quoted_regex = NULL;
 GRegex *unquoted_regex = NULL;
 
+/* Always returns TRUE */
+gboolean
+_g_match_info_clear (GMatchInfo **match_info)
+{
+    if (match_info == NULL || *match_info == NULL)
+        return TRUE;
+    g_match_info_free (*match_info);
+    *match_info = NULL;
+    return TRUE;
+}
+
+struct check_polkit_data {
+    const gchar *unique_name;
+    const gchar *action_id;
+    gboolean user_interaction;
+    GAsyncReadyCallback callback;
+    gpointer user_data;
+
+    PolkitAuthority *authority;
+    PolkitSubject *subject;
+};
+
+void
+check_polkit_data_free (struct check_polkit_data *data)
+{
+    if (data == NULL)
+        return;
+
+    if (data->subject != NULL)
+        g_object_unref (data->subject);
+    if (data->authority != NULL)
+        g_object_unref (data->authority);
+    
+    g_free (data);
+}
+
+gboolean
+check_polkit_finish (GAsyncResult *res,
+                     GError **error)
+{
+    GSimpleAsyncResult *simple;
+
+    simple = G_SIMPLE_ASYNC_RESULT (res);
+    if (g_simple_async_result_propagate_error (simple, error))
+        return FALSE;
+
+    return g_simple_async_result_get_op_res_gboolean (simple);
+}
+
+static void
+check_polkit_authorization_cb (GObject *source_object,
+                               GAsyncResult *res,
+                               gpointer _data)
+{
+    struct check_polkit_data *data;
+    PolkitAuthorizationResult *result;
+    GSimpleAsyncResult *simple;
+    GError *err = NULL;
+
+    data = (struct check_polkit_data *) _data;
+    if ((result = polkit_authority_check_authorization_finish (data->authority, res, &err)) == NULL) {
+        g_simple_async_report_take_gerror_in_idle (NULL, data->callback, data->user_data, err);
+        goto out;
+    }
+ 
+    if (!polkit_authorization_result_get_is_authorized (result)) {
+        g_simple_async_report_error_in_idle (NULL, data->callback, data->user_data, POLKIT_ERROR, POLKIT_ERROR_NOT_AUTHORIZED, "Authorizing for '%s': not authorized", data->action_id);
+        goto out;
+    }
+    simple = g_simple_async_result_new (NULL, data->callback, data->user_data, check_polkit_async);
+    g_simple_async_result_set_op_res_gboolean (simple, TRUE);
+    g_simple_async_result_complete_in_idle (simple);
+    g_object_unref (simple);
+
+  out:
+    check_polkit_data_free (data);
+    if (result != NULL)
+        g_object_unref (result);
+}
+
+static void
+check_polkit_authority_cb (GObject *source_object,
+                           GAsyncResult *res,
+                           gpointer _data)
+{
+    struct check_polkit_data *data;
+    GError *err = NULL;
+
+    data = (struct check_polkit_data *) _data;
+    if ((data->authority = polkit_authority_get_finish (res, &err)) == NULL) {
+        g_simple_async_report_take_gerror_in_idle (NULL, data->callback, data->user_data, err);
+        check_polkit_data_free (data);
+        return;
+    }
+    if (data->unique_name == NULL || data->action_id == NULL || 
+        (data->subject = polkit_system_bus_name_new (data->unique_name)) == NULL) {
+        g_simple_async_report_error_in_idle (NULL, data->callback, data->user_data, POLKIT_ERROR, POLKIT_ERROR_FAILED, "Authorizing for '%s': failed sanity check", data->action_id);
+        check_polkit_data_free (data);
+        return;
+    }
+    polkit_authority_check_authorization (data->authority, data->subject, data->action_id, NULL, (PolkitCheckAuthorizationFlags) data->user_interaction, NULL, check_polkit_authorization_cb, data);
+}
+
+void
+check_polkit_async (const gchar *unique_name,
+                    const gchar *action_id,
+                    const gboolean user_interaction,
+                    GAsyncReadyCallback callback,
+                    gpointer user_data)
+{
+    struct check_polkit_data *data;
+
+    data = g_new0 (struct check_polkit_data, 1);
+    data->unique_name = unique_name;
+    data->action_id = action_id;
+    data->user_interaction = user_interaction;
+    data->callback = callback;
+    data->user_data = user_data;
+
+    polkit_authority_get_async (NULL, check_polkit_authority_cb, data);
+}
+
 enum ShellEntryType {
     SHELL_ENTRY_TYPE_INDENT,
     SHELL_ENTRY_TYPE_COMMENT,
@@ -49,9 +172,9 @@ struct ShellEntry {
 };
 
 gchar *
-shell_utils_source_var (GFile *file,
-                        const gchar *variable,
-                        GError **error)
+shell_source_var (GFile *file,
+                  const gchar *variable,
+                  GError **error)
 {
     gchar *argv[4] = { "sh", "-c", NULL, NULL };
     gchar *filename = NULL, *quoted_filename = NULL;
@@ -110,26 +233,27 @@ shell_entry_free (struct ShellEntry *entry)
 }
 
 void
-shell_utils_trivial_free (ShellUtilsTrivial *trivial)
+shell_parser_free (ShellParser *parser)
 {
-    if (trivial == NULL)
+    if (parser == NULL)
         return;
 
-    if (trivial->file != NULL)
-        g_object_unref (trivial->file);
-    if (trivial->filename != NULL)
-        g_free (trivial->filename);
-    if (trivial->entry_list != NULL)
-        g_list_free_full (trivial->entry_list, (GDestroyNotify)shell_entry_free);
-    g_free (trivial);
+    if (parser->file != NULL)
+        g_object_unref (parser->file);
+    if (parser->filename != NULL)
+        g_free (parser->filename);
+    if (parser->entry_list != NULL)
+        g_list_free_full (parser->entry_list, (GDestroyNotify)shell_entry_free);
+    g_free (parser);
 }
 
-ShellUtilsTrivial *
-shell_utils_trivial_new (GFile *file,
-                         GError **error)
+ShellParser *
+shell_parser_new (GFile *file,
+                  GError **error)
 {
     gchar *filebuf = NULL;
     GError *local_err = NULL;
+    ShellParser *ret = NULL;
 
     if (file == NULL)
         return NULL;
@@ -138,10 +262,8 @@ shell_utils_trivial_new (GFile *file,
         if (local_err != NULL) {
             /* Inability to parse or open is a failure; file not existing at all is *not* a failure */
             if (local_err->code == G_IO_ERROR_NOT_FOUND) {
-                ShellUtilsTrivial *ret = NULL;
-
                 g_error_free (local_err);
-                ret = g_new0 (ShellUtilsTrivial, 1);
+                ret = g_new0 (ShellParser, 1);
                 g_object_ref (file);
                 ret->file = file;
                 ret->filename = g_file_get_path (file);
@@ -155,22 +277,24 @@ shell_utils_trivial_new (GFile *file,
         }
         return NULL;
     }
-    return shell_utils_trivial_new_from_string (file, filebuf, error);
+    ret = shell_parser_new_from_string (file, filebuf, error);
+    g_free (filebuf);
+    return ret;
 }
 
-ShellUtilsTrivial *
-shell_utils_trivial_new_from_string (GFile *file,
+ShellParser *
+shell_parser_new_from_string (GFile *file,
                                      gchar *filebuf,
                                      GError **error)
 {
-    ShellUtilsTrivial *ret = NULL;
+    ShellParser *ret = NULL;
     GError *local_err = NULL;
     gchar *s;
 
     if (file == NULL || filebuf == NULL)
         return NULL;
 
-    ret = g_new0 (ShellUtilsTrivial, 1);
+    ret = g_new0 (ShellParser, 1);
     g_object_ref (file);
     ret->file = file;
     ret->filename = g_file_get_path (file);
@@ -191,13 +315,11 @@ shell_utils_trivial_new_from_string (GFile *file,
             ret->entry_list = g_list_prepend (ret->entry_list, entry);
             s += strlen (entry->string);
             g_debug ("Scanned comment: ``%s''", entry->string);
-            g_match_info_free (match_info);
-            match_info = NULL;
+            _g_match_info_clear (&match_info);
             want_separator = FALSE;
             continue;
         }
-        g_match_info_free (match_info);
-        match_info = NULL;
+        _g_match_info_clear (&match_info);
 
         matched = g_regex_match (separator_regex, s, 0, &match_info);
         if (matched) {
@@ -207,13 +329,11 @@ shell_utils_trivial_new_from_string (GFile *file,
             ret->entry_list = g_list_prepend (ret->entry_list, entry);
             s += strlen (entry->string);
             g_debug ("Scanned separator: ``%s''", entry->string);
-            g_match_info_free (match_info);
-            match_info = NULL;
+            _g_match_info_clear (&match_info);
             want_separator = FALSE;
             continue;
         }
-        g_match_info_free (match_info);
-        match_info = NULL;
+        _g_match_info_clear (&match_info);
 
         matched = g_regex_match (indent_regex, s, 0, &match_info);
         if (matched) {
@@ -223,12 +343,10 @@ shell_utils_trivial_new_from_string (GFile *file,
             ret->entry_list = g_list_prepend (ret->entry_list, entry);
             s += strlen (entry->string);
             g_debug ("Scanned indent: ``%s''", entry->string);
-            g_match_info_free (match_info);
-            match_info = NULL;
+            _g_match_info_clear (&match_info);
             continue;
         }
-        g_match_info_free (match_info);
-        match_info = NULL;
+        _g_match_info_clear (&match_info);
 
         matched = g_regex_match (var_equals_regex, s, 0, &match_info);
         if (matched) {
@@ -243,8 +361,7 @@ shell_utils_trivial_new_from_string (GFile *file,
             entry->variable = g_match_info_fetch (match_info, 1);
             s += strlen (entry->string);
             g_debug ("Scanned variable: ``%s''", entry->string);
-            g_match_info_free (match_info);
-            match_info = NULL;
+            _g_match_info_clear (&match_info);
             want_separator = TRUE;
 
             while (*s != 0) {
@@ -255,26 +372,23 @@ shell_utils_trivial_new_from_string (GFile *file,
                 if (matched2)
                     goto append_value;
 
-                g_match_info_free (match_info);
-                match_info = NULL;
+                _g_match_info_clear (&match_info);
 
                 matched2 = g_regex_match (double_quoted_regex, s, 0, &match_info);
                 if (matched2)
                     goto append_value;
 
-                g_match_info_free (match_info);
-                match_info = NULL;
+                _g_match_info_clear (&match_info);
 
                 matched2 = g_regex_match (unquoted_regex, s, 0, &match_info);
                 if (matched2)
                     goto append_value;
 
-                g_match_info_free (match_info);
-                match_info = NULL;
+                _g_match_info_clear (&match_info);
 
                 break;
-append_value:
 
+  append_value:
                 if (raw_value == NULL) {
                     raw_value = g_match_info_fetch (match_info, 0);
                     s += strlen (raw_value);
@@ -288,8 +402,7 @@ append_value:
                     g_free (temp1);
                     g_free (temp2);
                 }
-                g_match_info_free (match_info);
-                match_info = NULL;
+                _g_match_info_clear (&match_info);
             }
 
             if (raw_value != NULL) {
@@ -307,17 +420,16 @@ append_value:
             continue;
         }
 
-no_match:
+  no_match:
         /* Nothing matches, parsing has failed! */
-        g_match_info_free (match_info);
-        match_info = NULL;
+        _g_match_info_clear (&match_info);
         if (local_err != NULL)
             g_propagate_prefixed_error (error, local_err, "Unable to parse '%s':", ret->filename);
         else
             g_propagate_error (error,
                                g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
                                             "Unable to parse '%s'", ret->filename));
-        shell_utils_trivial_free (ret);
+        shell_parser_free (ret);
         return NULL;
     }
 
@@ -326,28 +438,28 @@ no_match:
 }
 
 gboolean
-shell_utils_trivial_is_empty (ShellUtilsTrivial *trivial)
+shell_parser_is_empty (ShellParser *parser)
 {
-    if (trivial == NULL || trivial->entry_list == NULL)
+    if (parser == NULL || parser->entry_list == NULL)
         return TRUE;
     return FALSE;
 }
 
 gboolean
-shell_utils_trivial_set_variable (ShellUtilsTrivial *trivial,
-                                  const gchar *variable,
-                                  const gchar *value,
-                                  gboolean add_if_unset)
+shell_parser_set_variable (ShellParser *parser,
+                           const gchar *variable,
+                           const gchar *value,
+                           gboolean add_if_unset)
 {
     GList *curr = NULL;
     struct ShellEntry *found_entry = NULL;
     gchar *quoted_value = NULL;
     gboolean ret = FALSE;
 
-    g_assert (trivial != NULL);
+    g_assert (parser != NULL);
     g_assert (variable != NULL);
 
-    for (curr = trivial->entry_list; curr != NULL; curr = curr->next) {
+    for (curr = parser->entry_list; curr != NULL; curr = curr->next) {
         struct ShellEntry *entry;
 
         entry = (struct ShellEntry *)(curr->data);
@@ -367,7 +479,7 @@ shell_utils_trivial_set_variable (ShellUtilsTrivial *trivial,
             found_entry->type = SHELL_ENTRY_TYPE_ASSIGNMENT;
             found_entry->variable = g_strdup (variable);
             found_entry->string = g_strdup_printf ("%s=%s", variable, quoted_value);
-            trivial->entry_list = g_list_append (trivial->entry_list, found_entry);
+            parser->entry_list = g_list_append (parser->entry_list, found_entry);
             ret = TRUE;
         }
     }
@@ -377,16 +489,16 @@ shell_utils_trivial_set_variable (ShellUtilsTrivial *trivial,
 }
 
 void
-shell_utils_trivial_clear_variable (ShellUtilsTrivial *trivial,
-                                    const gchar *variable)
+shell_parser_clear_variable (ShellParser *parser,
+                             const gchar *variable)
 {
     GList *curr = NULL;
     gboolean ret = FALSE;
 
-    g_assert (trivial != NULL);
+    g_assert (parser != NULL);
     g_assert (variable != NULL);
 
-    for (curr = trivial->entry_list; curr != NULL; ) {
+    for (curr = parser->entry_list; curr != NULL; ) {
         struct ShellEntry *entry;
 
         entry = (struct ShellEntry *)(curr->data);
@@ -409,32 +521,32 @@ shell_utils_trivial_clear_variable (ShellUtilsTrivial *trivial,
 }
 
 gboolean
-shell_utils_trivial_save (ShellUtilsTrivial *trivial,
-                          GError **error)
+shell_parser_save (ShellParser *parser,
+                   GError **error)
 {
     gboolean ret = FALSE;
     GList *curr = NULL;
     GFileOutputStream *os;
 
-    g_assert (trivial != NULL && trivial->file != NULL && trivial->filename != NULL);
-    if ((os = g_file_replace (trivial->file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, error)) == NULL) {
-        g_prefix_error (error, "Unable to save '%s': ", trivial->filename);
+    g_assert (parser != NULL && parser->file != NULL && parser->filename != NULL);
+    if ((os = g_file_replace (parser->file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, error)) == NULL) {
+        g_prefix_error (error, "Unable to save '%s': ", parser->filename);
         goto out;
     }
 
-    for (curr = trivial->entry_list; curr != NULL; curr = curr->next) {
+    for (curr = parser->entry_list; curr != NULL; curr = curr->next) {
         struct ShellEntry *entry;
         gsize written;
 
         entry = (struct ShellEntry *)(curr->data);
         if (!g_output_stream_write_all (G_OUTPUT_STREAM (os), entry->string, strlen (entry->string), &written, NULL, error)) {
-            g_prefix_error (error, "Unable to save '%s': ", trivial->filename);
+            g_prefix_error (error, "Unable to save '%s': ", parser->filename);
             goto out;
         }
     }
     
     if (!g_output_stream_close (G_OUTPUT_STREAM (os), NULL, error)) {
-        g_prefix_error (error, "Unable to save '%s': ", trivial->filename);
+        g_prefix_error (error, "Unable to save '%s': ", parser->filename);
         g_output_stream_close (G_OUTPUT_STREAM (os), NULL, NULL);
         goto out;
     }
@@ -447,20 +559,20 @@ shell_utils_trivial_save (ShellUtilsTrivial *trivial,
 }
 
 gboolean
-shell_utils_trivial_set_and_save (GFile *file,
-                                  GError **error,
-                                  const gchar *first_var_name,
-                                  const gchar *first_alt_var_name,
-                                  const gchar *first_value,
-                                  ...)
+shell_parser_set_and_save (GFile *file,
+                           GError **error,
+                           const gchar *first_var_name,
+                           const gchar *first_alt_var_name,
+                           const gchar *first_value,
+                           ...)
 {
     va_list ap;
-    ShellUtilsTrivial *trivial;
+    ShellParser *parser;
     gboolean ret = FALSE;
     const gchar *var_name, *alt_var_name, *value;
 
     va_start (ap, first_value);
-    if ((trivial = shell_utils_trivial_new (file, error)) == NULL)
+    if ((parser = shell_parser_new (file, error)) == NULL)
         goto out;
 
     var_name = first_var_name;
@@ -468,56 +580,56 @@ shell_utils_trivial_set_and_save (GFile *file,
     value = first_value;
     do {
         if (alt_var_name == NULL) {
-            if (!shell_utils_trivial_set_variable (trivial, var_name, value, TRUE)) {
+            if (!shell_parser_set_variable (parser, var_name, value, TRUE)) {
                 g_propagate_error (error,
                         g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
-                                    "Unable to set %s in '%s'", var_name, trivial->filename));
+                                    "Unable to set %s in '%s'", var_name, parser->filename));
                 goto out;
             }
         } else {
-            if (!shell_utils_trivial_set_variable (trivial, var_name, value, FALSE) &&
-                !shell_utils_trivial_set_variable (trivial, alt_var_name, value, FALSE) &&
-                !shell_utils_trivial_set_variable (trivial, var_name, value, TRUE)) {
+            if (!shell_parser_set_variable (parser, var_name, value, FALSE) &&
+                !shell_parser_set_variable (parser, alt_var_name, value, FALSE) &&
+                !shell_parser_set_variable (parser, var_name, value, TRUE)) {
                     g_propagate_error (error,
                             g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
-                                        "Unable to set %s or %s in '%s'", var_name, alt_var_name, trivial->filename));
+                                        "Unable to set %s or %s in '%s'", var_name, alt_var_name, parser->filename));
                     goto out;
             }
         }
     } while ((var_name = va_arg (ap, const gchar*)) != NULL ?
                  alt_var_name = va_arg (ap, const gchar*), value = va_arg (ap, const gchar*), 1 : 0);
 
-    if (!shell_utils_trivial_save (trivial, error))
+    if (!shell_parser_save (parser, error))
         goto out;
 
     ret = TRUE;
 
   out:
     va_end (ap);
-    if (trivial != NULL)
-        shell_utils_trivial_free (trivial);
+    if (parser != NULL)
+        shell_parser_free (parser);
     return ret;
 }
 
 gchar **
-shell_utils_trivial_source_var_list (GFile *file,
-                                     const gchar * const *var_names,
-                                     GError **error)
+shell_parser_source_var_list (GFile *file,
+                              const gchar * const *var_names,
+                              GError **error)
 {
-    ShellUtilsTrivial *trivial;
+    ShellParser *parser;
     gchar **ret = NULL, **value;
     const gchar* const* var_name;
 
     if (var_names == NULL)
         return NULL;
 
-    if ((trivial = shell_utils_trivial_new (file, error)) == NULL)
+    if ((parser = shell_parser_new (file, error)) == NULL)
         return NULL;
 
     ret = g_new0 (gchar *, g_strv_length ((gchar **)var_names) + 1);
     for (var_name = var_names, value = ret; *var_name != NULL; var_name++, value++) {
         GList *curr;
-        for (curr = trivial->entry_list; curr != NULL; curr = curr->next) {
+        for (curr = parser->entry_list; curr != NULL; curr = curr->next) {
             struct ShellEntry *entry;
 
             entry = (struct ShellEntry *)(curr->data);
@@ -525,12 +637,12 @@ shell_utils_trivial_source_var_list (GFile *file,
                 *value = g_strdup (entry->unquoted_value);
         }
     }
-    shell_utils_trivial_free (trivial);
+    shell_parser_free (parser);
     return ret;
 }
 
 void
-shell_utils_destroy (void)
+utils_destroy (void)
 {
     if (indent_regex != NULL) {
         g_regex_unref (indent_regex);
@@ -563,7 +675,7 @@ shell_utils_destroy (void)
 }
 
 void
-shell_utils_init (void)
+utils_init (void)
 {
     if (indent_regex == NULL) {
         indent_regex = g_regex_new ("^[ \\t]+", G_REGEX_ANCHORED, 0, NULL);

diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..bf03d3e
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,102 @@
+/*
+  Copyright 2012 Alexandre Rostovtsev
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#ifndef _SHELL_UTILS_H_
+#define _SHELL_UTILS_H_
+
+#include <glib.h>
+#include <gio/gio.h>
+
+typedef struct _ShellParser ShellParser;
+
+struct _ShellParser
+{
+  GFile *file;
+  gchar *filename;
+  GList *entry_list;
+};
+
+/* Always return TRUE */
+gboolean
+_g_match_info_clear (GMatchInfo **match_info);
+
+void
+check_polkit_async (const gchar *unique_name,
+                    const gchar *action_id,
+                    const gboolean user_interaction,
+                    GAsyncReadyCallback callback,
+                    gpointer user_data);
+
+gboolean
+check_polkit_finish (GAsyncResult *res,
+                     GError **error);
+
+gchar *
+shell_source_var (GFile *file,
+                  const gchar *variable,
+                  GError **error);
+
+ShellParser *
+shell_parser_new (GFile *file,
+                  GError **error);
+
+ShellParser *
+shell_parser_new_from_string (GFile *file,
+                              gchar *filebuf,
+                              GError **error);
+
+void
+shell_parser_free (ShellParser *parser);
+
+gboolean
+shell_parser_is_empty (ShellParser *parser);
+
+gboolean
+shell_parser_set_variable (ShellParser *parser,
+                                  const gchar *variable,
+                                  const gchar *value,
+                                  gboolean add_if_unset);
+
+void
+shell_parser_clear_variable (ShellParser *parser,
+                             const gchar *variable);
+
+gboolean
+shell_parser_save (ShellParser *parser,
+                   GError **error);
+
+gboolean
+shell_parser_set_and_save (GFile *file,
+                           GError **error,
+                           const gchar *first_var_name,
+                           const gchar *first_alt_var_name,
+                           const gchar *first_value,
+                           ...);
+
+gchar **
+shell_parser_source_var_list (GFile *file,
+                              const gchar * const *var_names,
+                              GError **error);
+
+void
+utils_init (void);
+
+void
+utils_destroy (void);
+
+#endif


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [gentoo-commits] proj/openrc-settingsd:master commit in: src/, /
@ 2012-09-06  6:29 Alexandre Restovtsev
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Restovtsev @ 2012-09-06  6:29 UTC (permalink / raw
  To: gentoo-commits

commit:     30d1b0efed5f89eb3969937aa9d8695081b1aabb
Author:     Alexandre Rostovtsev <tetromino <AT> gentoo <DOT> org>
AuthorDate: Thu Sep  6 05:03:34 2012 +0000
Commit:     Alexandre Restovtsev <tetromino <AT> gmail <DOT> com>
CommitDate: Thu Sep  6 05:09:08 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/openrc-settingsd.git;a=commit;h=30d1b0ef

Use rc_sys for vm detection

---
 TODO            |    3 ---
 src/hostnamed.c |    9 ++++++++-
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/TODO b/TODO
index ff32ee0..9b68c84 100644
--- a/TODO
+++ b/TODO
@@ -6,9 +6,6 @@ if the relevant variable is set in /etc/rc.conf (go to read-only mode?)
 What happens if e.g. /etc/conf.d/hostname.{runlevel} files exist? Just
 document that this case is not supported?
 
-Write an init.d file to read RC_SYS so that we can piggyback on openrc's
-built-in virtualization detection.
-
 De-systemd-ize hwclock.c
 
 Do something about runtime dependency on systemd's org.freedesktop.hostname1.policy,

diff --git a/src/hostnamed.c b/src/hostnamed.c
index 1fb14c0..b476cdb 100644
--- a/src/hostnamed.c
+++ b/src/hostnamed.c
@@ -26,6 +26,8 @@
 #include <gio/gio.h>
 #include <polkit/polkit.h>
 
+#include <rc.h>
+
 #include "hostnamed.h"
 #include "hostname1-generated.h"
 #include "utils.h"
@@ -68,10 +70,15 @@ hostname_is_valid (const gchar *name)
 static gchar *
 guess_icon_name ()
 {
-    /* TODO: detect virtualization by reading rc_sys */
     gchar *filebuf = NULL;
     gchar *ret = NULL;
 
+    /* Note that rc_sys() leaks memory :( */
+    if (rc_sys() != NULL) {
+        ret = g_strdup ("computer-vm");
+        goto out;
+    }
+
 #if defined(__i386__) || defined(__x86_64__)
     /* 
        Taken with a few minor changes from systemd's hostnamed


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [gentoo-commits] proj/openrc-settingsd:master commit in: src/, /
@ 2012-09-08  0:20 Alexandre Restovtsev
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Restovtsev @ 2012-09-08  0:20 UTC (permalink / raw
  To: gentoo-commits

commit:     48f9a65ff49996468b8e56258ca945483597ee9e
Author:     Alexandre Rostovtsev <tetromino <AT> gentoo <DOT> org>
AuthorDate: Fri Sep  7 23:03:32 2012 +0000
Commit:     Alexandre Restovtsev <tetromino <AT> gmail <DOT> com>
CommitDate: Fri Sep  7 23:16:07 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/openrc-settingsd.git;a=commit;h=48f9a65f

Run env-update after updating an env.d file

---
 Makefile.am   |    1 +
 src/localed.c |   25 +++++++++++++++++--------
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index bdfb3dd..70a9ec4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -69,6 +69,7 @@ AM_CPPFLAGS = \
 	-DLIBEXECDIR=\""$(libexecdir)"\" \
 	-DPKGDATADIR=\""$(pkgdatadir)"\" \
 	-DPIDFILE=\""$(pidfile)"\" \
+	-DENV_UPDATE=\""$(prefix)/sbin/env-update"\" \
 	$(OPENRC_SETTINGSD_CFLAGS) \
 	-I$(top_srcdir)/src \
 	-I$(top_builddir)/src \

diff --git a/src/localed.c b/src/localed.c
index efc4a8b..008ed16 100644
--- a/src/localed.c
+++ b/src/localed.c
@@ -791,6 +791,7 @@ on_handle_set_locale_authorized_cb (GObject *source_object,
     struct invoked_locale *data;
     gchar **loc, **var, **val, **locale_values = NULL;
     ShellParser *locale_file_parsed = NULL;
+    gint status = 0;
 
     data = (struct invoked_locale *) user_data;
     if (!check_polkit_finish (res, &err)) {
@@ -822,16 +823,14 @@ on_handle_set_locale_authorized_cb (GObject *source_object,
             if (!found) {
                 g_dbus_method_invocation_return_dbus_error (data->invocation, DBUS_ERROR_INVALID_ARGS,
                                                             "Invalid locale variable name or value");
-                G_UNLOCK (locale);
-                goto out;
+                goto unlock;
             }
         }
     }
 
     if ((locale_file_parsed = shell_parser_new (locale_file, &err)) == NULL) {
         g_dbus_method_invocation_return_gerror (data->invocation, err);
-        G_UNLOCK (locale);
-        goto out;
+        goto unlock;
     }
 
     if (shell_parser_is_empty (locale_file_parsed)) {
@@ -839,8 +838,7 @@ on_handle_set_locale_authorized_cb (GObject *source_object,
         shell_parser_free (locale_file_parsed);
         if ((locale_file_parsed = shell_parser_new_from_string (locale_file, "# Configuration file for eselect\n# This file has been automatically generated\n", &err)) == NULL) {
             g_dbus_method_invocation_return_gerror (data->invocation, err);
-            G_UNLOCK (locale);
-            goto out;
+            goto unlock;
         }
     }
 
@@ -853,8 +851,7 @@ on_handle_set_locale_authorized_cb (GObject *source_object,
 
     if (!shell_parser_save (locale_file_parsed, &err)) {
         g_dbus_method_invocation_return_gerror (data->invocation, err);
-        G_UNLOCK (locale);
-        goto out;
+        goto unlock;
     }
 
     g_strfreev (locale);
@@ -867,8 +864,20 @@ on_handle_set_locale_authorized_cb (GObject *source_object,
         }
     }
 
+    if (!g_spawn_command_line_sync (ENV_UPDATE " --no-ldconfig", NULL, NULL, &status, &err)) {
+        g_dbus_method_invocation_return_gerror (data->invocation, err);
+        goto unlock;
+    }
+    if (status) {
+        g_dbus_method_invocation_return_dbus_error (data->invocation, DBUS_ERROR_FAILED,
+                                                    "env-update failed");
+        goto unlock;
+    }
+
     openrc_settingsd_localed_locale1_complete_set_locale (locale1, data->invocation);
     openrc_settingsd_localed_locale1_set_locale (locale1, (const gchar * const *) locale);
+
+  unlock:
     G_UNLOCK (locale);
 
   out:


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-09-08  0:21 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-08  0:20 [gentoo-commits] proj/openrc-settingsd:master commit in: src/, / Alexandre Restovtsev
  -- strict thread matches above, loose matches on Subject: below --
2012-09-06  6:29 Alexandre Restovtsev
2012-09-05 23:46 Alexandre Restovtsev
2012-02-08  5:38 Alexandre Restovtsev
2012-02-06 19:27 Alexandre Restovtsev
2012-02-05  8:36 Alexandre Restovtsev
2012-02-05  7:49 Alexandre Restovtsev
2012-02-05  6:46 Alexandre Restovtsev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox