Smallers steps to keep plugin running
[oweals/gnunet.git] / src / arm / arm_api.c
index 49e25b38ddb57804b28b5afd59e6945293f413d8..66747a624acde3d7190926e1044ad00df255301d 100644 (file)
@@ -1,10 +1,10 @@
 /*
      This file is part of GNUnet.
-     (C) 2009 Christian Grothoff (and other contributing authors)
+     (C) 2009, 2010 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
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
 #include "gnunet_server_lib.h"
 #include "arm.h"
 
-/**
- * How often do we re-try tranmsitting requests to ARM before
- * giving up?  Note that if we succeeded transmitting a request
- * but failed to read a response, we do NOT re-try (since that
- * might result in ARM getting a request twice).
- */
-#define MAX_ATTEMPTS 4
-
-/**
- * Minimum delay between attempts to talk to ARM.
- */
-#define MIN_RETRY_DELAY  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
-
-
-/**
- * How long are we willing to wait for a service operation during the multi-operation
- * request processing?
- */
-#define MULTI_TIMEOUT  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
-
 
 /**
  * Handle for interacting with ARM.
- */ 
+ */
 struct GNUNET_ARM_Handle
 {
 
@@ -67,7 +47,7 @@ struct GNUNET_ARM_Handle
   /**
    * The configuration that we are using.
    */
-  const struct GNUNET_CONFIGURATION_Handle *cfg;
+  struct GNUNET_CONFIGURATION_Handle *cfg;
 
   /**
    * Scheduler to use.
@@ -77,6 +57,212 @@ struct GNUNET_ARM_Handle
 };
 
 
+/**
+ * Context for handling the shutdown of a service.
+ */
+struct ShutdownContext
+{
+  /**
+   * Scheduler to be used to call continuation
+   */
+  struct GNUNET_SCHEDULER_Handle *sched;
+  /**
+   * Connection to the service that is being shutdown.
+   */
+  struct GNUNET_CLIENT_Connection *sock;
+
+  /**
+   * Time allowed for shutdown to happen.
+   */
+  struct GNUNET_TIME_Absolute timeout;
+
+  /**
+   * Task set up to cancel the shutdown request on timeout.
+   */
+  GNUNET_SCHEDULER_TaskIdentifier cancel_task;
+
+  /**
+   * Task to call once shutdown complete
+   */
+  GNUNET_CLIENT_ShutdownTask cont;
+
+  /**
+   * Closure for shutdown continuation
+   */
+  void *cont_cls;
+
+  /**
+   * We received a confirmation that the service will shut down.
+   */
+  int confirmed;
+
+};
+
+/**
+ * Handler receiving response to service shutdown requests.
+ * First call with NULL: service misbehaving, or something.
+ * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
+ *   - service will shutdown
+ * Second call with NULL:
+ *   - service has now really shut down.
+ *
+ * @param cls closure
+ * @param msg NULL, indicating socket closure.
+ */
+static void
+service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
+{
+  struct ShutdownContext *shutdown_ctx = cls;
+
+  if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))
+    {
+      /* Means the other side closed the connection and never confirmed a shutdown */
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                 "Service handle shutdown before ACK!\n");
+      if (shutdown_ctx->cont != NULL)
+        shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
+      GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
+      GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
+      GNUNET_free(shutdown_ctx);
+    }
+  else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES))
+    {
+#if DEBUG_ARM
+      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+                "Service shutdown complete.\n");
+#endif
+      if (shutdown_ctx->cont != NULL)
+        shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_NO);
+
+      GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
+      GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
+      GNUNET_free(shutdown_ctx);
+    }
+  else
+    {
+      GNUNET_assert(ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
+      switch (ntohs(msg->type))
+       {
+       case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
+#if DEBUG_ARM
+         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+                    "Received confirmation for service shutdown.\n");
+#endif
+         shutdown_ctx->confirmed = GNUNET_YES;
+         GNUNET_CLIENT_receive (shutdown_ctx->sock,
+                                &service_shutdown_handler,
+                                shutdown_ctx,
+                                GNUNET_TIME_UNIT_FOREVER_REL);
+         break;
+       default: /* Fall through */
+#if DEBUG_ARM
+         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+                    "Service shutdown refused!\n");
+#endif
+         if (shutdown_ctx->cont != NULL)
+           shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_YES);
+
+         GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
+         GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
+         GNUNET_free(shutdown_ctx);
+         break;
+       }
+    }
+}
+
+/**
+ * Shutting down took too long, cancel receive and return error.
+ *
+ * @param cls closure
+ * @param tc context information (why was this task triggered now)
+ */
+void service_shutdown_cancel (void *cls,
+                              const struct GNUNET_SCHEDULER_TaskContext * tc)
+{
+  struct ShutdownContext *shutdown_ctx = cls;
+  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
+  shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
+  GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
+  GNUNET_free(shutdown_ctx);
+}
+
+
+/**
+ * If possible, write a shutdown message to the target
+ * buffer and destroy the client connection.
+ *
+ * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
+ * @param size number of bytes available in buf
+ * @param buf NULL on error, otherwise target buffer
+ * @return number of bytes written to buf
+ */
+static size_t
+write_shutdown (void *cls, size_t size, void *buf)
+{
+  struct GNUNET_MessageHeader *msg;
+  struct ShutdownContext *shutdown_ctx = cls;
+
+  if (size < sizeof (struct GNUNET_MessageHeader))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  _("Failed to transmit shutdown request to client.\n"));
+      shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
+      GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
+      GNUNET_free(shutdown_ctx);
+      return 0;                 /* client disconnected */
+    }
+
+  GNUNET_CLIENT_receive (shutdown_ctx->sock,
+                        &service_shutdown_handler, shutdown_ctx,
+                        GNUNET_TIME_UNIT_FOREVER_REL);
+  shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (shutdown_ctx->sched,
+                                                           GNUNET_TIME_absolute_get_remaining(shutdown_ctx->timeout),
+                                                           &service_shutdown_cancel,
+                                                           shutdown_ctx);
+  msg = (struct GNUNET_MessageHeader *) buf;
+  msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
+  msg->size = htons (sizeof (struct GNUNET_MessageHeader));
+  return sizeof (struct GNUNET_MessageHeader);
+}
+
+
+/**
+ * Request that the service should shutdown.
+ * Afterwards, the connection will automatically be
+ * disconnected.  Hence the "sock" should not
+ * be used by the caller after this call
+ * (calling this function frees "sock" after a while).
+ *
+ * @param sched the scheduler to use for calling shutdown continuation
+ * @param sock the socket connected to the service
+ * @param timeout how long to wait before giving up on transmission
+ * @param cont continuation to call once the service is really down
+ * @param cont_cls closure for continuation
+ *
+ */
+static void
+arm_service_shutdown (struct GNUNET_SCHEDULER_Handle *sched,
+                     struct GNUNET_CLIENT_Connection *sock,
+                     struct GNUNET_TIME_Relative timeout,
+                     GNUNET_CLIENT_ShutdownTask cont,
+                     void *cont_cls)
+{
+  struct ShutdownContext *shutdown_ctx;
+  shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
+  shutdown_ctx->sched = sched;
+  shutdown_ctx->cont = cont;
+  shutdown_ctx->cont_cls = cont_cls;
+  shutdown_ctx->sock = sock;
+  shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout);
+  GNUNET_CLIENT_notify_transmit_ready (sock,
+                                      sizeof (struct
+                                              GNUNET_MessageHeader),
+                                      timeout,
+                                      GNUNET_YES,
+                                      &write_shutdown, shutdown_ctx);
+}
+
+
 /**
  * Setup a context for communicating with ARM.  Note that this
  * can be done even if the ARM service is not yet running.
@@ -94,15 +280,10 @@ GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
                    const char *service)
 {
   struct GNUNET_ARM_Handle *ret;
-  struct GNUNET_CLIENT_Connection *client;
 
-  client = GNUNET_CLIENT_connect (sched, "arm", cfg);
-  if (client == NULL)
-    return NULL;
   ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
-  ret->cfg = cfg;
+  ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
   ret->sched = sched;
-  ret->client = client;
   return ret;
 }
 
@@ -116,10 +297,25 @@ void
 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
 {
   if (h->client != NULL)
-    GNUNET_CLIENT_disconnect (h->client);
+    GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
+  GNUNET_CONFIGURATION_destroy (h->cfg);
   GNUNET_free (h);
 }
 
+struct ARM_ShutdownContext
+{
+  /**
+   * Callback to call once shutdown complete.
+   */
+  GNUNET_ARM_Callback cb;
+
+  /**
+   * Closure for callback.
+   */
+  void *cb_cls;
+};
+
+
 
 /**
  * Internal state for a request with ARM.
@@ -142,28 +338,11 @@ struct RequestContext
    */
   void *cls;
 
-  /**
-   * The service that is being manipulated.  Do not free.
-   */
-  const char *service_name;
-
   /**
    * Timeout for the operation.
    */
   struct GNUNET_TIME_Absolute timeout;
 
-  /**
-   * Length of service_name plus one.
-   */
-  size_t slen;
-
-  /**
-   * Number of attempts left for transmitting the request to ARM.
-   * We may fail the first time (say because ARM is not yet up),
-   * in which case we wait a bit and re-try (timeout permitting).
-   */
-  unsigned int attempts_left;
-
   /**
    * Type of the request expressed as a message type (start or stop).
    */
@@ -171,6 +350,8 @@ struct RequestContext
 
 };
 
+#include "do_start_process.c"
+
 
 /**
  * A client specifically requested starting of ARM itself.
@@ -189,17 +370,37 @@ arm_service_report (void *cls,
   pid_t pid;
   char *binary;
   char *config;
+  char *loprefix;
+  char *lopostfix;
 
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
     {
+#if DEBUG_ARM
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Looks like `%s' is already running.\n",
+                 "gnunet-service-arm");
+#endif
       /* arm is running! */
       if (pos->callback != NULL)
         pos->callback (pos->cls, GNUNET_YES);
       GNUNET_free (pos);
       return;
     }
+#if DEBUG_ARM
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Looks like `%s' is not running, will start it.\n",
+             "gnunet-service-arm");
+#endif
   /* FIXME: should we check that HOSTNAME for 'arm' is localhost? */
-  /* start service */
+
+ if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
+                                            "arm", "PREFIX", &loprefix))
+    loprefix = GNUNET_strdup ("");
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
+                                            "arm", "OPTIONS", &lopostfix))
+    lopostfix = GNUNET_strdup ("");
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
                                             "arm",
@@ -213,6 +414,8 @@ arm_service_report (void *cls,
       if (pos->callback != NULL)
         pos->callback (pos->cls, GNUNET_SYSERR);
       GNUNET_free (pos);
+      GNUNET_free (loprefix);
+      GNUNET_free (lopostfix);
       return;
     }
   if (GNUNET_OK !=
@@ -227,15 +430,50 @@ arm_service_report (void *cls,
         pos->callback (pos->cls, GNUNET_SYSERR);
       GNUNET_free (binary);
       GNUNET_free (pos);
+      GNUNET_free (loprefix);
+      GNUNET_free (lopostfix);
       return;
     }
-  pid = GNUNET_OS_start_process (binary, binary, "-d", "-c", config,
+  if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (pos->h->cfg,
+                                                      "TESTING",
+                                                      "WEAKRANDOM")) &&
+      (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg,
+                                                           "TESTING",
+                                                           "WEAKRANDOM")) &&
+      (GNUNET_NO == GNUNET_CONFIGURATION_have_value (pos->h->cfg,
+                                                     "TESTING",
+                                                     "HOSTFILE")) /* Means we are ONLY running locally */
+                                                           )
+    {
+      /* we're clearly running a test, don't daemonize */
+      pid = do_start_process (NULL,
+                             loprefix,
+                             binary,
+                             "-c", config,
+#if DEBUG_ARM
+                             "-L", "DEBUG",
+#endif
+                             /* no daemonization! */
+                             lopostfix,
+                             NULL);
+    }
+  else
+    {
+      pid = do_start_process (NULL,
+                             loprefix,
+                             binary,
+                             "-c", config,
 #if DEBUG_ARM
-                                 "-L", "DEBUG",
+                             "-L", "DEBUG",
 #endif
-                                 NULL);
+                             "-d",
+                             lopostfix,
+                             NULL);
+    }
   GNUNET_free (binary);
   GNUNET_free (config);
+  GNUNET_free (loprefix);
+  GNUNET_free (lopostfix);
   if (pid == -1)
     {
       if (pos->callback != NULL)
@@ -253,7 +491,7 @@ arm_service_report (void *cls,
  * Process a response from ARM to a request for a change in service
  * status.
  *
- * @param cls the request context 
+ * @param cls the request context
  * @param msg the response
  */
 static void
@@ -265,12 +503,17 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
   if (msg == NULL)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                  _("Error receiving response from ARM service\n"));
-      GNUNET_CLIENT_disconnect (sc->h->client);
-      sc->h->client = GNUNET_CLIENT_connect (sc->h->sched, 
-                                            "arm", 
+                 _("Error receiving response to `%s' request from ARM for service `%s'\n"),
+                 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START)
+                 ? "START"
+                 : "STOP",
+                 (const char*) &sc[1]);
+      GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
+      sc->h->client = GNUNET_CLIENT_connect (sc->h->sched,
+                                            "arm",
                                             sc->h->cfg);
       GNUNET_assert (NULL != sc->h->client);
+      GNUNET_CLIENT_ignore_shutdown (sc->h->client, GNUNET_YES);
       if (sc->callback != NULL)
         sc->callback (sc->cls, GNUNET_SYSERR);
       GNUNET_free (sc);
@@ -278,7 +521,8 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
     }
 #if DEBUG_ARM
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received response from ARM service: %u\n",
+              "Received response from ARM for service `%s': %u\n",
+             (const char*) &sc[1],
              ntohs(msg->type));
 #endif
   switch (ntohs (msg->type))
@@ -302,121 +546,6 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
 }
 
 
-/**
- * We've failed to transmit the request to the ARM service.
- * Report our failure and clean up the state.
- *
- * @param sctx the state of the (now failed) request
- */
-static void
-report_transmit_failure (struct RequestContext *sctx)
-{
-  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-             _("Error while trying to transmit to ARM service\n"));
-  if (sctx->callback != NULL)
-    sctx->callback (sctx->cls, GNUNET_SYSERR);
-  GNUNET_free (sctx);
-}
-
-
-/**
- * Transmit a request for a service status change to the
- * ARM service.
- *
- * @param cls the "struct RequestContext" identifying the request
- * @param size how many bytes are available in buf
- * @param buf where to write the request, NULL on error
- * @return number of bytes written to buf
- */
-static size_t
-send_service_msg (void *cls, size_t size, void *buf);
-
-
-/**
- * We've failed to transmit the request to the ARM service but
- * are now going to try again.
- * 
- * @param cls state of the request
- * @param tc task context (unused)
- */
-static void
-retry_request (void *cls,
-              const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct RequestContext *sctx = cls;
-
-  if (NULL ==
-      GNUNET_CLIENT_notify_transmit_ready (sctx->h->client,
-                                          sctx->slen +
-                                          sizeof (struct
-                                                  GNUNET_MessageHeader),
-                                          GNUNET_TIME_absolute_get_remaining (sctx->timeout),
-                                          &send_service_msg, 
-                                          sctx))
-    {
-      report_transmit_failure (sctx);    
-      return;
-    }
-}
-
-
-/**
- * Transmit a request for a service status change to the
- * ARM service.
- *
- * @param cls the "struct RequestContext" identifying the request
- * @param size how many bytes are available in buf
- * @param buf where to write the request, NULL on error
- * @return number of bytes written to buf
- */
-static size_t
-send_service_msg (void *cls, size_t size, void *buf)
-{
-  struct RequestContext *sctx = cls;
-  struct GNUNET_MessageHeader *msg;
-  struct GNUNET_TIME_Relative rem;
-
-  if (buf == NULL)
-    {
-      GNUNET_CLIENT_disconnect (sctx->h->client);
-      sctx->h->client = GNUNET_CLIENT_connect (sctx->h->sched, 
-                                              "arm", 
-                                              sctx->h->cfg);
-      GNUNET_assert (sctx->h->client != NULL);
-      rem = GNUNET_TIME_absolute_get_remaining (sctx->timeout);
-      if ( (sctx->attempts_left-- > 0) &&
-          (rem.value > 0) )
-       {
-         GNUNET_SCHEDULER_add_delayed (sctx->h->sched,
-                                       GNUNET_NO,
-                                       GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                       GNUNET_SCHEDULER_NO_TASK,
-                                       GNUNET_TIME_relative_min (MIN_RETRY_DELAY,
-                                                                 rem),
-                                       &retry_request,
-                                       sctx);
-         return 0;
-       }
-      report_transmit_failure (sctx);
-      return 0;
-    }
-#if DEBUG_ARM
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              _("Transmitting service request to ARM.\n"));
-#endif
-  GNUNET_assert (size >= sctx->slen);
-  msg = buf;
-  msg->size = htons (sizeof (struct GNUNET_MessageHeader) + sctx->slen);
-  msg->type = htons (sctx->type);
-  memcpy (&msg[1], sctx->service_name, sctx->slen);
-  GNUNET_CLIENT_receive (sctx->h->client,
-                         &handle_response,
-                         sctx,
-                         GNUNET_TIME_absolute_get_remaining (sctx->timeout));
-  return sctx->slen + sizeof (struct GNUNET_MessageHeader);
-}
-
-
 /**
  * Start or stop a service.
  *
@@ -425,7 +554,7 @@ send_service_msg (void *cls, size_t size, void *buf)
  * @param timeout how long to wait before failing for good
  * @param cb callback to invoke when service is ready
  * @param cb_cls closure for callback
- * @param type type of the request 
+ * @param type type of the request
  */
 static void
 change_service (struct GNUNET_ARM_Handle *h,
@@ -435,10 +564,10 @@ change_service (struct GNUNET_ARM_Handle *h,
 {
   struct RequestContext *sctx;
   size_t slen;
+  struct GNUNET_MessageHeader *msg;
 
   slen = strlen (service_name) + 1;
-  if (slen + sizeof (struct GNUNET_MessageHeader) >
-      GNUNET_SERVER_MAX_MESSAGE_SIZE)
+  if (slen + sizeof (struct GNUNET_MessageHeader) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
     {
       GNUNET_break (0);
       if (cb != NULL)
@@ -447,21 +576,42 @@ change_service (struct GNUNET_ARM_Handle *h,
     }
 #if DEBUG_ARM
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              _("ARM requests starting of service `%s'.\n"), service_name);
+              (type == GNUNET_MESSAGE_TYPE_ARM_START)
+             ? _("Requesting start of service `%s'.\n")
+             : _("Requesting termination of service `%s'.\n"),
+             service_name);
 #endif
   sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
   sctx->h = h;
   sctx->callback = cb;
   sctx->cls = cb_cls;
-  sctx->service_name = (const char*) &sctx[1];
-  memcpy (&sctx[1],
-         service_name,
-         slen);
   sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
-  sctx->slen = slen;
-  sctx->attempts_left = MAX_ATTEMPTS;
   sctx->type = type;
-  retry_request (sctx, NULL);
+  memcpy (&sctx[1], service_name, slen);
+  msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
+  msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
+  msg->type = htons (sctx->type);
+  memcpy (&msg[1], service_name, slen);
+  if (GNUNET_OK !=
+      GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
+                                              msg,
+                                              GNUNET_TIME_absolute_get_remaining (sctx->timeout),
+                                              GNUNET_YES,
+                                              &handle_response,
+                                              sctx))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                 (type == GNUNET_MESSAGE_TYPE_ARM_START)
+                 ? _("Error while trying to transmit request to start `%s' to ARM\n")
+                 : _("Error while trying to transmit request to stop `%s' to ARM\n"),
+                 (const char*) &service_name);
+      if (cb != NULL)
+       cb (cb_cls, GNUNET_SYSERR);
+      GNUNET_free (sctx);
+      GNUNET_free (msg);
+      return;
+    }
+  GNUNET_free (msg);
 }
 
 
@@ -481,55 +631,63 @@ GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
                           GNUNET_ARM_Callback cb, void *cb_cls)
 {
   struct RequestContext *sctx;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              _("Starting service `%s'\n"), service_name);
-  if (0 == strcmp ("arm", service_name))
+  struct GNUNET_CLIENT_Connection *client;
+  size_t slen;
+#if DEBUG_ARM
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              _("Asked to start service `%s' within %llu ms\n"), service_name,
+             (unsigned long long) timeout.value);
+#endif
+  if (0 == strcasecmp ("arm", service_name))
     {
-      sctx = GNUNET_malloc (sizeof (struct RequestContext));
+      slen = strlen ("arm") + 1;
+      sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
       sctx->h = h;
       sctx->callback = cb;
       sctx->cls = cb_cls;
       sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
+      memcpy (&sctx[1], service_name, slen);
       GNUNET_CLIENT_service_test (h->sched,
                                   "arm",
                                   h->cfg, timeout, &arm_service_report, sctx);
       return;
     }
+  if (h->client == NULL)
+    {
+      client = GNUNET_CLIENT_connect (h->sched, "arm", h->cfg);
+      if (client == NULL)
+       {
+         cb (cb_cls, GNUNET_SYSERR);
+         return;
+       }
+      GNUNET_CLIENT_ignore_shutdown (client, GNUNET_YES);
+      h->client = client;
+    }
   change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_START);
 }
 
-
 /**
- * Stop a service.
+ * Callback from the arm stop service call, indicates that the arm service
+ * is well and truly dead, won't die, or an error occurred.
  *
- * @param h handle to ARM
- * @param service_name name of the service
- * @param timeout how long to wait before failing for good
- * @param cb callback to invoke when service is ready
- * @param cb_cls closure for callback
+ * @param cls closure for the callback
+ * @param reason reason for callback, GNUNET_NO if arm is shutdown
+ *        GNUNET_YES if arm remains running, and GNUNET_SYSERR on error
  */
-void
-GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
-                        const char *service_name,
-                         struct GNUNET_TIME_Relative timeout,
-                         GNUNET_ARM_Callback cb, void *cb_cls)
+void arm_shutdown_callback (void *cls,
+                            int reason)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              _("Stopping service `%s'\n"), service_name);
-  if (0 == strcmp ("arm", service_name))
-    {
-      GNUNET_CLIENT_service_shutdown (h->client);
-      if (cb != NULL)
-        cb (cb_cls, GNUNET_NO);
-      return;
-    }
-  change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP);
+  struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
+
+  if (arm_shutdown_ctx->cb != NULL)
+    arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
+
+  GNUNET_free(arm_shutdown_ctx);
 }
 
 
 /**
- * Function to call for each service.
+ * Stop a service.
  *
  * @param h handle to ARM
  * @param service_name name of the service
@@ -537,155 +695,39 @@ GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
  * @param cb callback to invoke when service is ready
  * @param cb_cls closure for callback
  */
-typedef void (*ServiceOperation) (struct GNUNET_ARM_Handle *h,
-                                 const char *service_name,
-                                 struct GNUNET_TIME_Relative timeout,
-                                 GNUNET_ARM_Callback cb, void *cb_cls);
-
-
-/**
- * Context for starting or stopping multiple services.
- */
-struct MultiContext
+void
+GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
+                         const char *service_name,
+                         struct GNUNET_TIME_Relative timeout,
+                         GNUNET_ARM_Callback cb, void *cb_cls)
 {
-  /**
-   * NULL-terminated array of services to start or stop.
-   */
-  char **services;
-
-  /**
-   * Our handle to ARM.
-   */
-  struct GNUNET_ARM_Handle *h;
-
-  /**
-   * Identifies the operation (start or stop).
-   */
-  ServiceOperation op;
-
-  /**
-   * Current position in "services".
-   */
-  unsigned int pos;
-};
-
+  struct ARM_ShutdownContext *arm_shutdown_ctx;
+  struct GNUNET_CLIENT_Connection *client;
 
-/**
- * Run the operation for the next service in the multi-service
- * request.
- *
- * @param cls the "struct MultiContext" that is being processed
- * @param success status of the previous operation (ignored)
- */
-static void
-next_operation (void *cls,
-               int success)
-{
-  struct MultiContext *mc = cls;
-  char *pos;
-  
-  if (NULL == (pos = mc->services[mc->pos]))
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              _("Stopping service `%s' within %llu ms\n"), service_name,
+             (unsigned long long) timeout.value);
+  if (h->client == NULL)
     {
-      GNUNET_free (mc->services);
-      GNUNET_ARM_disconnect (mc->h);
-      GNUNET_free (mc);
-      return;
+      client = GNUNET_CLIENT_connect (h->sched, "arm", h->cfg);
+      if (client == NULL)
+       {
+         cb (cb_cls, GNUNET_SYSERR);
+         return;
+       }
+      GNUNET_CLIENT_ignore_shutdown (client, GNUNET_YES);
+      h->client = client;
     }
-  mc->pos++;
-  mc->op (mc->h, pos, MULTI_TIMEOUT, &next_operation, mc);
-  GNUNET_free (pos);
-}
-
-
-/**
- * Run a multi-service request.
- *
- * @param cfg configuration to use (needed to contact ARM;
- *        the ARM service may internally use a different
- *        configuration to determine how to start the service).
- * @param sched scheduler to use
- * @param op the operation to perform for each service
- * @param va NULL-terminated list of services
- */
-static void
-run_multi_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
-                  struct GNUNET_SCHEDULER_Handle *sched,                   
-                  ServiceOperation op,
-                  va_list va)
-{
-  va_list cp;
-  unsigned int total;
-  struct MultiContext *mc;
-  struct GNUNET_ARM_Handle *h;
-  const char *c;
-  
-  h = GNUNET_ARM_connect (cfg, sched, NULL);
-  if (NULL == h)
+  if (0 == strcasecmp ("arm", service_name))
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Error while trying to transmit to ARM service\n"));
-      return; 
+      arm_shutdown_ctx = GNUNET_malloc(sizeof(struct ARM_ShutdownContext));
+      arm_shutdown_ctx->cb = cb;
+      arm_shutdown_ctx->cb_cls = cb_cls;
+      arm_service_shutdown (h->sched, h->client, timeout, &arm_shutdown_callback, arm_shutdown_ctx);
+      h->client = NULL;
+      return;
     }
-  total = 1;
-  va_copy (cp, va);
-  while (NULL != (va_arg (cp, const char*))) total++;
-  va_end (cp);
-  mc = GNUNET_malloc (sizeof(struct MultiContext));
-  mc->services = GNUNET_malloc (total * sizeof (char*));
-  mc->h = h;
-  mc->op = op;
-  total = 0;
-  va_copy (cp, va);
-  while (NULL != (c = va_arg (cp, const char*))) 
-    mc->services[total++] = GNUNET_strdup (c);
-  va_end (cp);
-  next_operation (mc, GNUNET_YES);
-}
-
-
-/**
- * Start multiple services in the specified order.  Convenience
- * function.  Works asynchronously, failures are not reported.
- *
- * @param cfg configuration to use (needed to contact ARM;
- *        the ARM service may internally use a different
- *        configuration to determine how to start the service).
- * @param sched scheduler to use
- * @param ... NULL-terminated list of service names (const char*)
- */
-void
-GNUNET_ARM_start_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
-                          struct GNUNET_SCHEDULER_Handle *sched,
-                          ...)
-{
-  va_list ap;
-
-  va_start (ap, sched);
-  run_multi_request (cfg, sched, &GNUNET_ARM_start_service, ap);
-  va_end (ap);
-}
-
-
-/**
- * Stop multiple services in the specified order.  Convenience
- * function.  Works asynchronously, failures are not reported.
- *
- * @param cfg configuration to use (needed to contact ARM;
- *        the ARM service may internally use a different
- *        configuration to determine how to start the service).
- * @param sched scheduler to use
- * @param ... NULL-terminated list of service names (const char*)
- */
-void
-GNUNET_ARM_stop_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
-                         struct GNUNET_SCHEDULER_Handle *sched,
-                         ...)
-{
-  va_list ap;
-
-  va_start (ap, sched);
-  run_multi_request (cfg, sched, &GNUNET_ARM_stop_service, ap);
-  va_end (ap);
+  change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP);
 }