[ostree] daemon: Allow running as non-root in test mode



commit 6cdea44c4b72938531469bde47ef650d7aba0d83
Author: Colin Walters <walters verbum org>
Date:   Sun Nov 27 11:34:03 2011 -0500

    daemon: Allow running as non-root in test mode

 src/daemon/ostreed.c   |   31 ++++++++++++++++++----
 src/daemon/ot-daemon.c |   65 ++++++++++++++++++++++++++++++++++++++---------
 src/daemon/ot-daemon.h |   12 +++++++++
 3 files changed, 89 insertions(+), 19 deletions(-)
---
diff --git a/src/daemon/ostreed.c b/src/daemon/ostreed.c
index eda32e8..d68cfbc 100644
--- a/src/daemon/ostreed.c
+++ b/src/daemon/ostreed.c
@@ -29,25 +29,44 @@
 #include <unistd.h>
 #include <stdlib.h>
 
+static OstreeDaemonConfig config;
+
+static GOptionEntry entries[] = {
+  {
+    "dummy-test-path", 0, 0, G_OPTION_ARG_FILENAME, &config.dummy_test_path, "Run against the given tree on the session bus", "path"},
+  { NULL }
+};
+
 int
 main (int    argc,
       char **argv)
 {
   OstreeDaemon *daemon = NULL;
+  GError *error = NULL;
+  GOptionContext *context = NULL;
 
   g_type_init ();
 
-  g_set_prgname (argv[0]);
+  context = g_option_context_new ("- OSTree system daemon");
+  g_option_context_add_main_entries (context, entries, NULL);
 
-  if (getuid () != 0)
-    {
-      g_printerr ("This program must be run as root\n");
-      exit (1);
-    }
+  if (!g_option_context_parse (context, &argc, &argv, &error))
+    goto out;
 
   daemon = ostree_daemon_new ();
 
+  if (!ostree_daemon_config (daemon, &config, &error))
+    goto out;
+
   g_main_loop_run (daemon->loop);
 
+ out:
+  ostree_daemon_free (daemon);
+  if (error)
+    {
+      g_printerr ("%s\n", error->message);
+      g_clear_error (&error);
+      exit (1);
+    }
   return 0;
 }
diff --git a/src/daemon/ot-daemon.c b/src/daemon/ot-daemon.c
index 7044880..f0ac553 100644
--- a/src/daemon/ot-daemon.c
+++ b/src/daemon/ot-daemon.c
@@ -233,6 +233,9 @@ on_bus_acquired (GDBusConnection *connection,
 
   self->bus = g_object_ref (connection);
 
+  if (introspection_data == NULL)
+    introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
+
   id = g_dbus_connection_register_object (connection,
                                           OSTREE_DAEMON_PATH,
                                           introspection_data->interfaces[0],
@@ -250,8 +253,11 @@ on_name_acquired (GDBusConnection *connection,
 {
   OstreeDaemon *self = user_data;
   GError *error = NULL;
+  char *repo_path;
 
-  self->repo = ostree_repo_new ("/sysroot/ostree/repo");
+  repo_path = g_build_filename (ot_gfile_get_path_cached (self->prefix), "repo", NULL);
+  self->repo = ostree_repo_new (repo_path);
+  g_free (repo_path);
   if (!ostree_repo_check (self->repo, &error))
     {
       g_printerr ("%s\n", error->message);
@@ -271,21 +277,54 @@ on_name_lost (GDBusConnection *connection,
 OstreeDaemon *
 ostree_daemon_new (void)
 {
-  OstreeDaemon *ret = g_new0 (OstreeDaemon, 1);
+  OstreeDaemon *self = g_new0 (OstreeDaemon, 1);
 
-  ret->loop = g_main_loop_new (NULL, TRUE);
+  self->loop = g_main_loop_new (NULL, TRUE);
+  self->ops = g_hash_table_new_full (g_int_hash, g_int_equal, NULL, NULL);
 
-  ret->name_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
-                                 OSTREE_DAEMON_NAME,
-                                 G_BUS_NAME_OWNER_FLAGS_NONE,
-                                 on_bus_acquired,
-                                 on_name_acquired,
-                                 on_name_lost,
-                                 NULL,
-                                 NULL);
+  return self;
+}
+
+void
+ostree_daemon_free (OstreeDaemon  *self)
+{
+  g_main_loop_unref (self->loop);
+  g_hash_table_unref (self->ops);
+  g_free (self);
+}
+
+gboolean
+ostree_daemon_config (OstreeDaemon *self,
+                      OstreeDaemonConfig *config,
+                      GError        **error)
+{
+  gboolean ret = FALSE;
+  gboolean is_dummy = config->dummy_test_path != NULL;
 
-  ret->ops = g_hash_table_new_full (g_int_hash, g_int_equal, NULL, NULL);
+  if (!is_dummy)
+    {
+      if (getuid () != 0)
+        {
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                       "This program must be run as root");
+          goto out;
+        }
+    }
 
+  if (is_dummy)
+    self->prefix = ot_gfile_new_for_path (config->dummy_test_path);
+  else
+    self->prefix = ot_gfile_new_for_path ("/sysroot/ostree");
+      
+  self->name_id = g_bus_own_name (is_dummy ? G_BUS_TYPE_SESSION : G_BUS_TYPE_SYSTEM,
+                                  OSTREE_DAEMON_NAME,
+                                  G_BUS_NAME_OWNER_FLAGS_NONE,
+                                  on_bus_acquired,
+                                  on_name_acquired,
+                                  on_name_lost,
+                                  self,
+                                  NULL);
+  ret = TRUE;
+ out:
   return ret;
 }
-
diff --git a/src/daemon/ot-daemon.h b/src/daemon/ot-daemon.h
index b0d8778..5319670 100644
--- a/src/daemon/ot-daemon.h
+++ b/src/daemon/ot-daemon.h
@@ -32,7 +32,13 @@
 G_BEGIN_DECLS
 
 typedef struct {
+  char *dummy_test_path;
+} OstreeDaemonConfig;
+
+typedef struct {
   GMainLoop *loop;
+  GFile *prefix;
+
   OstreeRepo  *repo;
 
   GDBusConnection *bus;
@@ -53,6 +59,12 @@ typedef struct {
 
 OstreeDaemon *ostree_daemon_new (void);
 
+void ostree_daemon_free (OstreeDaemon  *self);
+
+gboolean ostree_daemon_config (OstreeDaemon *self,
+                               OstreeDaemonConfig *config,
+                               GError        **error);
+
 G_END_DECLS
 
 #endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]