-fixing #2546
[oweals/gnunet.git] / src / arm / gnunet-arm.c
index 13b1fdc175f4570df5818bea94085e28a18fa75a..d84de1c7ab84bbdde405407a3494c25de5034a6a 100644 (file)
  */
 #include "platform.h"
 #include "gnunet_arm_service.h"
-#include "gnunet_client_lib.h"
 #include "gnunet_constants.h"
-#include "gnunet_getopt_lib.h"
-#include "gnunet_program_lib.h"
-#include "gnunet_time_lib.h"
+#include "gnunet_util_lib.h"
 
 /**
  * Timeout for stopping services.  Long to give some services a real chance.
  */
 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
 
+/**
+ * Timeout for listing all running services.
+ */
+#define LIST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
+
 /**
  * Set if we are to shutdown all services (including ARM).
  */
@@ -73,6 +75,11 @@ static int delete;
  */
 static int quiet;
 
+/**
+ * Set if we should print a list of currently running services.
+ */
+static int list;
+
 /**
  * Set to the name of a service to start.
  */
@@ -119,6 +126,16 @@ static unsigned int phase;
 static struct GNUNET_TIME_Relative timeout;
 
 
+/**
+ * Do we want to give our stdout to gnunet-service-arm?
+ */
+static unsigned int no_stdout = 0;
+
+/**
+ * Do we want to give our stderr to gnunet-service-arm?
+ */
+static unsigned int no_stderr = 0;
+
 /**
  * Main continuation-passing-style loop.  Runs the various
  * jobs that we've been asked to do in order.
@@ -146,53 +163,76 @@ confirm_cb (void *cls,
   switch (result)
   {
   case GNUNET_ARM_PROCESS_UNKNOWN:
-    fprintf (stderr, _("Service `%s' is unknown to ARM.\n"), service);
+    FPRINTF (stderr, _("Service `%s' is unknown to ARM.\n"), service);
     ret = 1;
     break;
   case GNUNET_ARM_PROCESS_DOWN:
     if (quiet != GNUNET_YES)
-      fprintf (stdout, _("Service `%s' has been stopped.\n"), service);
+      FPRINTF (stdout, _("Service `%s' has been stopped.\n"), service);
     break;
   case GNUNET_ARM_PROCESS_ALREADY_RUNNING:
-    fprintf (stderr, _("Service `%s' was already running.\n"), service);
+    FPRINTF (stderr, _("Service `%s' was already running.\n"), service);
     ret = 1;
     break;
   case GNUNET_ARM_PROCESS_STARTING:
     if (quiet != GNUNET_YES)
-      fprintf (stdout, _("Service `%s' has been started.\n"), service);
+      FPRINTF (stdout, _("Service `%s' has been started.\n"), service);
     break;
   case GNUNET_ARM_PROCESS_ALREADY_STOPPING:
-    fprintf (stderr, _("Service `%s' was already being stopped.\n"), service);
+    FPRINTF (stderr, _("Service `%s' was already being stopped.\n"), service);
     ret = 1;
     break;
   case GNUNET_ARM_PROCESS_ALREADY_DOWN:
-    fprintf (stderr, _("Service `%s' was already not running.\n"), service);
+    FPRINTF (stderr, _("Service `%s' was already not running.\n"), service);
     ret = 1;
     break;
   case GNUNET_ARM_PROCESS_SHUTDOWN:
-    fprintf (stderr, _("Request ignored as ARM is shutting down.\n"));
+    FPRINTF (stderr, "%s", _("Request ignored as ARM is shutting down.\n"));
     ret = 1;
     break;
   case GNUNET_ARM_PROCESS_COMMUNICATION_ERROR:
-    fprintf (stderr, _("Error communicating with ARM service.\n"));
+    FPRINTF (stderr, "%s", _("Error communicating with ARM service.\n"));
     ret = 1;
     break;
   case GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT:
-    fprintf (stderr, _("Timeout communicating with ARM service.\n"));
+    FPRINTF (stderr, "%s",  _("Timeout communicating with ARM service.\n"));
     ret = 1;
     break;
   case GNUNET_ARM_PROCESS_FAILURE:
-    fprintf (stderr, _("Operation failed.\n"));
+    FPRINTF (stderr, "%s",  _("Operation failed.\n"));
     ret = 1;
     break;
   default:
-    fprintf (stderr, _("Unknown response code from ARM.\n"));
+    FPRINTF (stderr, "%s",  _("Unknown response code from ARM.\n"));
     break;
   }
   GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
                                     GNUNET_SCHEDULER_REASON_PREREQ_DONE);
 }
 
+/**
+ * Callback invoked with the list of running services.  
+ * Reports to the user and then runs the next phase in the FSM.
+ *
+ * @param cls currently not used
+ * @param result result of the operation
+ * @param count number of running services
+ * @param list copy of the list of running services
+ */
+static void
+list_cb (void *cls, int result, unsigned int count, const char *const*list)
+{
+  unsigned int i;
+
+  if ( (result != GNUNET_YES) || (NULL == list) )
+  {
+    FPRINTF (stderr, "%s", _("Error communicating with ARM. ARM not running?\n"));
+    return;
+  }
+  FPRINTF (stdout, "%s", _("Running services:\n"));
+  for (i=0; i<count; i++)
+    FPRINTF (stdout, "%s\n", list[i]);
+}
 
 /**
  * Main function that will be run by the scheduler.
@@ -293,6 +333,8 @@ cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
          if (start)
            {
              GNUNET_ARM_start_service (h, "arm",
+                                        (no_stdout ? 0 : GNUNET_OS_INHERIT_STD_OUT) |
+                                        (no_stderr ? 0 : GNUNET_OS_INHERIT_STD_ERR),
                                        (0 ==
                                         timeout.rel_value) ? START_TIMEOUT :
                                        timeout, &confirm_cb, "arm");
@@ -303,6 +345,8 @@ cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
          if (init != NULL)
            {
              GNUNET_ARM_start_service (h, init,
+                                        (no_stdout ? 0 : GNUNET_OS_INHERIT_STD_OUT) |
+                                        (no_stderr ? 0 : GNUNET_OS_INHERIT_STD_ERR),
                                        (0 ==
                                         timeout.rel_value) ? START_TIMEOUT :
                                        timeout, &confirm_cb, init);
@@ -318,7 +362,7 @@ cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
              start = 1;
              restart = 0;
              h = GNUNET_ARM_connect (cfg, NULL);
-             if (h == NULL)
+             if (NULL == h)
                {
                  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                              _("Fatal error initializing ARM API.\n"));
@@ -328,6 +372,25 @@ cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
              GNUNET_SCHEDULER_add_now (&cps_loop, NULL);
              return;
            }
+         break;
+        case 5:
+          if (list) {
+              GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Going to list all running services controlled by ARM.\n");
+  
+              if (NULL == h) 
+              {
+               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                              _("Fatal error initializing ARM API.\n"));
+               return;
+              }
+              
+              GNUNET_ARM_list_running_services (h, 
+                                                (0 == 
+                                                 timeout.rel_value) ? LIST_TIMEOUT : 
+                                                 timeout, &list_cb, NULL);
+            return;
+          }
          /* Fall through */
        default:                /* last phase */
          GNUNET_ARM_disconnect (h);
@@ -349,8 +412,6 @@ cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 int
 main (int argc, char *const *argv)
 {
-  static unsigned long long temp_timeout_ms;
-
   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
     {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
      GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
@@ -370,12 +431,18 @@ main (int argc, char *const *argv)
      GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
     {'T', "timeout", NULL,
      gettext_noop ("timeout for completing current operation"),
-     GNUNET_YES, &GNUNET_GETOPT_set_ulong, &temp_timeout_ms},
+     GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &timeout},
+    {'I', "info", NULL, gettext_noop ("List currently running services"),
+     GNUNET_NO, &GNUNET_GETOPT_set_one, &list},
+    {'O', "no-stdout", NULL, gettext_noop ("Don't let gnunet-service-arm inherit standard output"),
+     GNUNET_NO, &GNUNET_GETOPT_set_one, &no_stdout},
+    {'E', "no-stderr", NULL, gettext_noop ("Don't let gnunet-service-arm inherit standard error"),
+     GNUNET_NO, &GNUNET_GETOPT_set_one, &no_stderr},
     GNUNET_GETOPT_OPTION_END
   };
 
-  if (temp_timeout_ms > 0)
-    timeout.rel_value = temp_timeout_ms;
+  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
+    return 2;
 
   if (GNUNET_OK ==
       GNUNET_PROGRAM_run (argc, argv, "gnunet-arm",