- verboser log, faster start
[oweals/gnunet.git] / src / arm / gnunet-arm.c
index a37a78a26a2a61d12c0b8c6ddd8e155d37f2a8f4..8a98ba06b3096433a3a42eb01b89f7252a5dfba3 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009, 2012 Christian Grothoff (and other contributing authors)
+     (C) 2009, 2012, 2013 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -113,7 +113,7 @@ static struct GNUNET_ARM_Handle *h;
 /**
  * Our configuration.
  */
-static const struct GNUNET_CONFIGURATION_Handle *cfg;
+static struct GNUNET_CONFIGURATION_Handle *cfg;
 
 /**
  * Processing stage that we are in.  Simple counter.
@@ -128,12 +128,12 @@ static struct GNUNET_TIME_Relative timeout;
 /**
  * Do we want to give our stdout to gnunet-service-arm?
  */
-static unsigned int no_stdout = 0;
+static unsigned int no_stdout;
 
 /**
  * Do we want to give our stderr to gnunet-service-arm?
  */
-static unsigned int no_stderr = 0;
+static unsigned int no_stderr;
 
 
 /**
@@ -206,8 +206,7 @@ confirm_cb (void *cls,
     FPRINTF (stderr, "%s",  _("Unknown response code from ARM.\n"));
     break;
   }
-  GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
-                                    GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+  GNUNET_SCHEDULER_add_now (&cps_loop, NULL);
 }
 
 
@@ -236,6 +235,52 @@ list_cb (void *cls, int result, unsigned int count, const char *const*list)
 }
 
 
+/**
+ * Attempts to delete configuration file and SERVICEHOME
+ * on arm shutdown provided the end and delete options
+ * were specified when gnunet-arm was run.
+ */
+static void
+delete_files ()
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Will attempt to remove configuration file %s and service directory %s\n",
+             config_file, dir);
+
+  if (UNLINK (config_file) != 0)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                 _("Failed to remove configuration file %s\n"), config_file);
+    }
+
+  if (GNUNET_DISK_directory_remove (dir) != GNUNET_OK)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                 _("Failed to remove servicehome directory %s\n"), dir);
+
+    }
+}
+
+
+/**
+ * Main continuation-passing-style loop.  Runs the various
+ * jobs that we've been asked to do in order.
+ *
+ * @param cls closure, unused
+ * @param tc context, unused
+ */
+static void
+shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  GNUNET_ARM_disconnect (h);
+  h = NULL;
+  if ((end == GNUNET_YES) && (delete == GNUNET_YES))
+    delete_files ();   
+  GNUNET_CONFIGURATION_destroy (cfg);
+  cfg = NULL;
+}
+
+
 /**
  * Main function that will be run by the scheduler.
  *
@@ -248,7 +293,9 @@ static void
 run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
-  cfg = c;
+  char *armconfig;
+
+  cfg = GNUNET_CONFIGURATION_dup (c);
   config_file = cfgfile;
   if (GNUNET_CONFIGURATION_get_value_string
       (cfg, "PATHS", "SERVICEHOME", &dir) != GNUNET_OK)
@@ -257,42 +304,30 @@ run (void *cls, char *const *args, const char *cfgfile,
                               "PATHS", "SERVICEHOME");
     return;
     }
+  if (NULL != cfgfile)
+  {
+    if (GNUNET_OK !=
+        GNUNET_CONFIGURATION_get_value_filename (cfg, "arm", "CONFIG",
+                                                &armconfig))
+    {
+      GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG",
+                                             cfgfile);
+    }
+    else
+      GNUNET_free (armconfig);
+  }
   if (NULL == (h = GNUNET_ARM_connect (cfg, NULL)))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("Fatal error initializing ARM API.\n"));
     ret = 1;
+    GNUNET_CONFIGURATION_destroy (cfg);
+    cfg = NULL;
     return;
   }
-  GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
-                                    GNUNET_SCHEDULER_REASON_PREREQ_DONE);
-}
-
-
-/**
- * Attempts to delete configuration file and SERVICEHOME
- * on arm shutdown provided the end and delete options
- * were specified when gnunet-arm was run.
- */
-static void
-delete_files ()
-{
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Will attempt to remove configuration file %s and service directory %s\n",
-             config_file, dir);
-
-  if (UNLINK (config_file) != 0)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Failed to remove configuration file %s\n"), config_file);
-    }
-
-  if (GNUNET_DISK_directory_remove (dir) != GNUNET_OK)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Failed to remove servicehome directory %s\n"), dir);
-
-    }
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
+                               &shutdown_task, NULL);
+  GNUNET_SCHEDULER_add_now (&cps_loop, NULL);
 }
 
 
@@ -306,6 +341,10 @@ delete_files ()
 static void
 cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  if (NULL == h)
+    return;
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+    return;
   while (1)
   {
     switch (phase++)
@@ -393,9 +432,7 @@ cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       }
       /* Fall through */
     default:           /* last phase */
-      GNUNET_ARM_disconnect (h);
-      if ((end == GNUNET_YES) && (delete == GNUNET_YES))
-       delete_files ();
+      GNUNET_SCHEDULER_shutdown ();
       return;
     }
   }