REST/NAMESTORE: rework API
[oweals/gnunet.git] / src / arm / arm_api.c
index ac473e87e000001d498a899c72a8f7ca87dfe580..c8103c877e7d4eb31e1b8386aaf8e2b2c8df3372 100644 (file)
 /*
      This file is part of GNUnet.
-     (C) 2009, 2010 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009, 2010, 2012, 2013, 2016 GNUnet e.V.
 
-     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 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet 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.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 
 /**
  * @file arm/arm_api.c
  * @brief API for accessing the ARM service
  * @author Christian Grothoff
+ * @author LRN
  */
 #include "platform.h"
+#include "gnunet_util_lib.h"
 #include "gnunet_arm_service.h"
-#include "gnunet_client_lib.h"
-#include "gnunet_getopt_lib.h"
-#include "gnunet_os_lib.h"
 #include "gnunet_protocols.h"
-#include "gnunet_server_lib.h"
 #include "arm.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "arm-api",__VA_ARGS__)
+
 
 /**
- * Handle for interacting with ARM.
+ * Entry in a doubly-linked list of operations awaiting for replies
+ * (in-order) from the ARM service.
  */
-struct GNUNET_ARM_Handle
+struct GNUNET_ARM_Operation
 {
+  /**
+   * This is a doubly-linked list.
+   */
+  struct GNUNET_ARM_Operation *next;
 
   /**
-   * Our connection to the ARM service.
+   * This is a doubly-linked list.
    */
-  struct GNUNET_CLIENT_Connection *client;
+  struct GNUNET_ARM_Operation *prev;
 
   /**
-   * The configuration that we are using.
+   * ARM handle.
    */
-  struct GNUNET_CONFIGURATION_Handle *cfg;
+  struct GNUNET_ARM_Handle *h;
+
+  /**
+   * Callback for service state change requests.
+   */
+  GNUNET_ARM_ResultCallback result_cont;
 
   /**
-   * Scheduler to use.
+   * Callback for service list requests.
    */
-  struct GNUNET_SCHEDULER_Handle *sched;
+  GNUNET_ARM_ServiceListCallback list_cont;
 
+  /**
+   * Closure for @e result_cont or @e list_cont.
+   */
+  void *cont_cls;
+
+  /**
+   * Task for async completion.
+   */
+  struct GNUNET_SCHEDULER_Task *async;
+
+  /**
+   * Unique ID for the request.
+   */
+  uint64_t id;
+
+  /**
+   * Result of this operation for #notify_starting().
+   */
+  enum GNUNET_ARM_Result starting_ret;
+
+  /**
+   * Is this an operation to stop the ARM service?
+   */
+  int is_arm_stop;
 };
 
 
 /**
- * Context for handling the shutdown of a service.
+ * Handle for interacting with ARM.
  */
-struct ShutdownContext
+struct GNUNET_ARM_Handle
 {
   /**
-   * Scheduler to be used to call continuation
+   * Our connection to the ARM service.
    */
-  struct GNUNET_SCHEDULER_Handle *sched;
+  struct GNUNET_MQ_Handle *mq;
+
   /**
-   * Connection to the service that is being shutdown.
+   * The configuration that we are using.
    */
-  struct GNUNET_CLIENT_Connection *sock;
+  const struct GNUNET_CONFIGURATION_Handle *cfg;
 
   /**
-   * Time allowed for shutdown to happen.
+   * Head of doubly-linked list of pending operations.
    */
-  struct GNUNET_TIME_Absolute timeout;
+  struct GNUNET_ARM_Operation *operation_pending_head;
 
   /**
-   * Task set up to cancel the shutdown request on timeout.
+   * Tail of doubly-linked list of pending operations.
    */
-  GNUNET_SCHEDULER_TaskIdentifier cancel_task;
+  struct GNUNET_ARM_Operation *operation_pending_tail;
 
   /**
-   * Task to call once shutdown complete
+   * Callback to invoke on connection/disconnection.
    */
-  GNUNET_CLIENT_ShutdownTask cont;
+  GNUNET_ARM_ConnectionStatusCallback conn_status;
 
   /**
-   * Closure for shutdown continuation
+   * Closure for @e conn_status.
    */
-  void *cont_cls;
+  void *conn_status_cls;
+
+  /**
+   * ARM operation where the goal is to wait for ARM shutdown to
+   * complete.  This operation is special in that it waits for an
+   * error on the @e mq.  So we complete it by calling the
+   * continuation in the #mq_error_handler().  Note that the operation
+   * is no longer in the @e operation_pending_head DLL once it is
+   * referenced from this field.
+   */
+  struct GNUNET_ARM_Operation *thm;
+
+  /**
+   * ID of the reconnect task (if any).
+   */
+  struct GNUNET_SCHEDULER_Task *reconnect_task;
 
   /**
-   * We received a confirmation that the service will shut down.
+   * Current delay we use for re-trying to connect to core.
    */
-  int confirmed;
+  struct GNUNET_TIME_Relative retry_backoff;
+
+  /**
+   * Counter for request identifiers.  They are used to match replies
+   * from ARM to operations in the @e operation_pending_head DLL.
+   */
+  uint64_t request_id_counter;
+
+  /**
+   * Have we detected that ARM is up?
+   */
+  int currently_up;
 
 };
 
+
+/**
+ * Connect to arm.
+ *
+ * @param h arm handle
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
+ */
+static int
+reconnect_arm (struct GNUNET_ARM_Handle *h);
+
+
 /**
- * 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.
+ * Task scheduled to try to re-connect to arm.
  *
- * @param cls closure
- * @param msg NULL, indicating socket closure.
+ * @param cls the `struct GNUNET_ARM_Handle`
  */
 static void
-service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
+reconnect_arm_task (void *cls)
 {
-  struct ShutdownContext *shutdown_ctx = cls;
+  struct GNUNET_ARM_Handle *h = 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;
-       }
-    }
+  h->reconnect_task = NULL;
+  reconnect_arm (h);
 }
 
+
 /**
- * Shutting down took too long, cancel receive and return error.
+ * Close down any existing connection to the ARM service and
+ * try re-establishing it later.
  *
- * @param cls closure
- * @param tc context information (why was this task triggered now)
+ * @param h our handle
  */
-void service_shutdown_cancel (void *cls,
-                              const struct GNUNET_SCHEDULER_TaskContext * tc)
+static void
+reconnect_arm_later (struct GNUNET_ARM_Handle *h)
 {
-  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);
+  struct GNUNET_ARM_Operation *op;
+
+  if (NULL != h->mq)
+  {
+    GNUNET_MQ_destroy (h->mq);
+    h->mq = NULL;
+  }
+  h->currently_up = GNUNET_NO;
+  GNUNET_assert (NULL == h->reconnect_task);
+  h->reconnect_task =
+      GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
+                                   &reconnect_arm_task,
+                                   h);
+  while (NULL != (op = h->operation_pending_head))
+  {
+    if (NULL != op->result_cont)
+      op->result_cont (op->cont_cls,
+                       GNUNET_ARM_REQUEST_DISCONNECTED,
+                       0);
+    if (NULL != op->list_cont)
+      op->list_cont (op->cont_cls,
+                     GNUNET_ARM_REQUEST_DISCONNECTED,
+                     0,
+                     NULL);
+    GNUNET_ARM_operation_cancel (op);
+  }
+  GNUNET_assert (NULL == h->operation_pending_head);
+  h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
+  if (NULL != h->conn_status)
+    h->conn_status (h->conn_status_cls,
+                    GNUNET_NO);
 }
 
 
 /**
- * If possible, write a shutdown message to the target
- * buffer and destroy the client connection.
+ * Find a control message by its unique ID.
  *
- * @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
+ * @param h ARM handle
+ * @param id unique message ID to use for the lookup
+ * @return NULL if not found
  */
-static size_t
-write_shutdown (void *cls, size_t size, void *buf)
+static struct GNUNET_ARM_Operation *
+find_op_by_id (struct GNUNET_ARM_Handle *h,
+               uint64_t id)
 {
-  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 */
-    }
+  struct GNUNET_ARM_Operation *result;
 
-  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);
+  for (result = h->operation_pending_head; NULL != result; result = result->next)
+    if (id == result->id)
+      return result;
+  return NULL;
 }
 
 
 /**
- * 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
+ * Handler for ARM replies.
  *
+ * @param cls our `struct GNUNET_ARM_Handle`
+ * @param res the message received from the arm service
  */
 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)
+handle_arm_result (void *cls,
+                   const struct GNUNET_ARM_ResultMessage *res)
 {
-  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);
+  struct GNUNET_ARM_Handle *h = cls;
+  struct GNUNET_ARM_Operation *op;
+  uint64_t id;
+  enum GNUNET_ARM_Result result;
+  GNUNET_ARM_ResultCallback result_cont;
+  void *result_cont_cls;
+
+  id = GNUNET_ntohll (res->arm_msg.request_id);
+  op = find_op_by_id (h,
+                      id);
+  if (NULL == op)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Message with unknown id %llu\n",
+         (unsigned long long) id);
+    return;
+  }
+
+  result = (enum GNUNET_ARM_Result) ntohl (res->result);
+  if ( (GNUNET_YES == op->is_arm_stop) &&
+       (GNUNET_ARM_RESULT_STOPPING == result) )
+  {
+    /* special case: if we are stopping 'gnunet-service-arm', we do not just
+       wait for the result message, but also wait for the service to close
+       the connection (and then we have to close our client handle as well);
+       this is done by installing a different receive handler, waiting for
+       the connection to go down */
+    if (NULL != h->thm)
+    {
+      GNUNET_break (0);
+      op->result_cont (h->thm->cont_cls,
+                      GNUNET_ARM_REQUEST_SENT_OK,
+                      GNUNET_ARM_RESULT_IS_NOT_KNOWN);
+      GNUNET_free (h->thm);
+    }
+    GNUNET_CONTAINER_DLL_remove (h->operation_pending_head,
+                                 h->operation_pending_tail,
+                                 op);
+    h->thm = op;
+    return;
+  }
+  result_cont = op->result_cont;
+  result_cont_cls = op->cont_cls;
+  GNUNET_ARM_operation_cancel (op);
+  if (NULL != result_cont)
+    result_cont (result_cont_cls,
+                 GNUNET_ARM_REQUEST_SENT_OK,
+                 result);
 }
 
 
 /**
- * Setup a context for communicating with ARM.  Note that this
- * can be done even if the ARM service is not yet running.
+ * Checked that list result message is well-formed.
  *
- * @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 service service that *this* process is implementing/providing, can be NULL
- * @return context to use for further ARM operations, NULL on error
+ * @param cls our `struct GNUNET_ARM_Handle`
+ * @param lres the message received from the arm service
+ * @return #GNUNET_OK if message is well-formed
  */
-struct GNUNET_ARM_Handle *
-GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
-                   struct GNUNET_SCHEDULER_Handle *sched,
-                   const char *service)
+static int
+check_arm_list_result (void *cls,
+                       const struct GNUNET_ARM_ListResultMessage *lres)
 {
-  struct GNUNET_ARM_Handle *ret;
-
-  ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
-  ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
-  ret->sched = sched;
-  return ret;
+  const char *pos = (const char *) &lres[1];
+  uint16_t rcount = ntohs (lres->count);
+  uint16_t msize = ntohs (lres->arm_msg.header.size) - sizeof (*lres);
+  uint16_t size_check;
+
+  size_check = 0;
+  for (unsigned int i = 0; i < rcount; i++)
+  {
+    const char *end = memchr (pos, 0, msize - size_check);
+    if (NULL == end)
+    {
+      GNUNET_break (0);
+      return GNUNET_SYSERR;
+    }
+    size_check += (end - pos) + 1;
+    pos = end + 1;
+  }
+  return GNUNET_OK;
 }
 
 
 /**
- * Disconnect from the ARM service.
+ * Handler for ARM list replies.
  *
- * @param h the handle that was being used
+ * @param cls our `struct GNUNET_ARM_Handle`
+ * @param lres the message received from the arm service
  */
-void
-GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
+static void
+handle_arm_list_result (void *cls,
+                        const struct GNUNET_ARM_ListResultMessage *lres)
 {
-  if (h->client != NULL)
-    GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
-  GNUNET_CONFIGURATION_destroy (h->cfg);
-  GNUNET_free (h);
+  struct GNUNET_ARM_Handle *h = cls;
+  uint16_t rcount = ntohs (lres->count);
+  const char *list[rcount];
+  const char *pos = (const char *) &lres[1];
+  uint16_t msize = ntohs (lres->arm_msg.header.size) - sizeof (*lres);
+  struct GNUNET_ARM_Operation *op;
+  uint16_t size_check;
+  uint64_t id;
+
+  id = GNUNET_ntohll (lres->arm_msg.request_id);
+  op = find_op_by_id (h,
+                      id);
+  if (NULL == op)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Message with unknown id %llu\n",
+         (unsigned long long) id);
+    return;
+  }
+  size_check = 0;
+  for (unsigned int i = 0; i < rcount; i++)
+  {
+    const char *end = memchr (pos,
+                              0,
+                              msize - size_check);
+
+    /* Assert, as this was already checked in #check_arm_list_result() */
+    GNUNET_assert (NULL != end);
+    list[i] = pos;
+    size_check += (end - pos) + 1;
+    pos = end + 1;
+  }
+  if (NULL != op->list_cont)
+    op->list_cont (op->cont_cls,
+                   GNUNET_ARM_REQUEST_SENT_OK,
+                   rcount,
+                   list);
+  GNUNET_ARM_operation_cancel (op);
 }
 
-struct ARM_ShutdownContext
-{
-  /**
-   * Callback to call once shutdown complete.
-   */
-  GNUNET_ARM_Callback cb;
-
-  /**
-   * Closure for callback.
-   */
-  void *cb_cls;
-};
 
+/**
+ * Receive confirmation from test, ARM service is up.
+ *
+ * @param cls closure with the `struct GNUNET_ARM_Handle`
+ * @param msg message received
+ */
+static void
+handle_confirm (void *cls,
+                const struct GNUNET_MessageHeader *msg)
+{
+  struct GNUNET_ARM_Handle *h = cls;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Got confirmation from ARM that we are up!\n");
+  if (GNUNET_NO == h->currently_up)
+  {
+    h->currently_up = GNUNET_YES;
+    if (NULL != h->conn_status)
+      h->conn_status (h->conn_status_cls,
+                      GNUNET_YES);
+  }
+}
 
 
 /**
- * Internal state for a request with ARM.
+ * Generic error handler, called with the appropriate error code and
+ * the same closure specified at the creation of the message queue.
+ * Not every message queue implementation supports an error handler.
+ *
+ * @param cls closure with the `struct GNUNET_ARM_Handle *`
+ * @param error error code
  */
-struct RequestContext
+static void
+mq_error_handler (void *cls,
+                  enum GNUNET_MQ_Error error)
 {
+  struct GNUNET_ARM_Handle *h = cls;
+  struct GNUNET_ARM_Operation *op;
+
+  h->currently_up = GNUNET_NO;
+  if (NULL != (op = h->thm))
+  {
+    h->thm = NULL;
+    op->result_cont (op->cont_cls,
+                    GNUNET_ARM_REQUEST_SENT_OK,
+                    GNUNET_ARM_RESULT_STOPPED);
+    GNUNET_free (op);
+  }
+  reconnect_arm_later (h);
+}
 
-  /**
-   * Pointer to our handle with ARM.
-   */
-  struct GNUNET_ARM_Handle *h;
 
-  /**
-   * Function to call with a status code for the requested operation.
-   */
-  GNUNET_ARM_Callback callback;
+/**
+ * Connect to arm.
+ *
+ * @param h arm handle
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
+ */
+static int
+reconnect_arm (struct GNUNET_ARM_Handle *h)
+{
+  struct GNUNET_MQ_MessageHandler handlers[] = {
+    GNUNET_MQ_hd_fixed_size (arm_result,
+                             GNUNET_MESSAGE_TYPE_ARM_RESULT,
+                             struct GNUNET_ARM_ResultMessage,
+                             h),
+    GNUNET_MQ_hd_var_size (arm_list_result,
+                           GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT,
+                           struct GNUNET_ARM_ListResultMessage,
+                           h),
+    GNUNET_MQ_hd_fixed_size (confirm,
+                             GNUNET_MESSAGE_TYPE_ARM_TEST,
+                             struct GNUNET_MessageHeader,
+                             h),
+    GNUNET_MQ_handler_end ()
+  };
+  struct GNUNET_MessageHeader *test;
+  struct GNUNET_MQ_Envelope *env;
+
+  if (NULL != h->mq)
+    return GNUNET_OK;
+  GNUNET_assert (GNUNET_NO == h->currently_up);
+  h->mq = GNUNET_CLIENT_connect (h->cfg,
+                                 "arm",
+                                 handlers,
+                                 &mq_error_handler,
+                                 h);
+  if (NULL == h->mq)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "GNUNET_CLIENT_connect returned NULL\n");
+    if (NULL != h->conn_status)
+      h->conn_status (h->conn_status_cls,
+                      GNUNET_SYSERR);
+    return GNUNET_SYSERR;
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Sending TEST message to ARM\n");
+  env = GNUNET_MQ_msg (test,
+                       GNUNET_MESSAGE_TYPE_ARM_TEST);
+  GNUNET_MQ_send (h->mq,
+                  env);
+  return GNUNET_OK;
+}
 
-  /**
-   * Closure for "callback".
-   */
-  void *cls;
 
-  /**
-   * Timeout for the operation.
-   */
-  struct GNUNET_TIME_Absolute timeout;
+/**
+ * Set up a context for communicating with ARM, then
+ * start connecting to the ARM service using that context.
+ *
+ * @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 conn_status will be called when connecting/disconnecting
+ * @param conn_status_cls closure for @a conn_status
+ * @return context to use for further ARM operations, NULL on error.
+ */
+struct GNUNET_ARM_Handle *
+GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                    GNUNET_ARM_ConnectionStatusCallback conn_status,
+                   void *conn_status_cls)
+{
+  struct GNUNET_ARM_Handle *h;
 
-  /**
-   * Type of the request expressed as a message type (start or stop).
-   */
-  uint16_t type;
+  h = GNUNET_new (struct GNUNET_ARM_Handle);
+  h->cfg = cfg;
+  h->conn_status = conn_status;
+  h->conn_status_cls = conn_status_cls;
+  if (GNUNET_OK != reconnect_arm (h))
+  {
+    GNUNET_free (h);
+    return NULL;
+  }
+  return h;
+}
 
-};
 
-#include "do_start_process.c"
+/**
+ * Disconnect from the ARM service (if connected) and destroy the context.
+ *
+ * @param h the handle that was being used
+ */
+void
+GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
+{
+  struct GNUNET_ARM_Operation *op;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Disconnecting from ARM service\n");
+  while (NULL != (op = h->operation_pending_head))
+  {
+    GNUNET_CONTAINER_DLL_remove (h->operation_pending_head,
+                                 h->operation_pending_tail,
+                                 op);
+    if (NULL != op->result_cont)
+      op->result_cont (op->cont_cls,
+                       GNUNET_ARM_REQUEST_DISCONNECTED,
+                       0);
+    if (NULL != op->list_cont)
+      op->list_cont (op->cont_cls,
+                     GNUNET_ARM_REQUEST_DISCONNECTED,
+                     0,
+                     NULL);
+    if (NULL != op->async)
+    {
+      GNUNET_SCHEDULER_cancel (op->async);
+      op->async = NULL;
+    }
+    GNUNET_free (op);
+  }
+  if (NULL != h->mq)
+  {
+    GNUNET_MQ_destroy (h->mq);
+    h->mq = NULL;
+  }
+  if (NULL != h->reconnect_task)
+  {
+    GNUNET_SCHEDULER_cancel (h->reconnect_task);
+    h->reconnect_task = NULL;
+  }
+  GNUNET_free (h);
+}
 
 
 /**
  * A client specifically requested starting of ARM itself.
- * This function is called with information about whether
- * or not ARM is running; if it is, report success.  If
- * it is not, start the ARM process.
+ * Starts the ARM service.
  *
- * @param cls the context for the request that we will report on (struct RequestContext*)
- * @param tc why were we called (reason says if ARM is running)
+ * @param h the handle with configuration details
+ * @param std_inheritance inheritance of std streams
+ * @return operation status code
  */
-static void
-arm_service_report (void *cls,
-                   const struct GNUNET_SCHEDULER_TaskContext *tc)
+static enum GNUNET_ARM_Result
+start_arm_service (struct GNUNET_ARM_Handle *h,
+                   enum GNUNET_OS_InheritStdioFlags std_inheritance)
 {
-  struct RequestContext *pos = cls;
-  pid_t pid;
+  struct GNUNET_OS_Process *proc;
+  char *cbinary;
   char *binary;
+  char *quotedbinary;
   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? */
-
- if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
-                                            "arm", "PREFIX", &loprefix))
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_string (h->cfg,
+                                             "arm",
+                                             "PREFIX",
+                                             &loprefix))
     loprefix = GNUNET_strdup ("");
+  else
+    loprefix = GNUNET_CONFIGURATION_expand_dollar (h->cfg,
+                                                   loprefix);
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
-                                            "arm", "OPTIONS", &lopostfix))
+      GNUNET_CONFIGURATION_get_value_string (h->cfg,
+                                             "arm",
+                                             "OPTIONS",
+                                             &lopostfix))
     lopostfix = GNUNET_strdup ("");
+  else
+    lopostfix = GNUNET_CONFIGURATION_expand_dollar (h->cfg,
+                                                    lopostfix);
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
-                                            "arm",
-                                            "BINARY",
-                                            &binary))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                  _("Configuration failes to specify option `%s' in section `%s'!\n"),
-                 "BINARY",
-                 "arm");
-      if (pos->callback != NULL)
-        pos->callback (pos->cls, GNUNET_SYSERR);
-      GNUNET_free (pos);
-      GNUNET_free (loprefix);
-      GNUNET_free (lopostfix);
-      return;
-    }
+      GNUNET_CONFIGURATION_get_value_string (h->cfg,
+                                             "arm",
+                                             "BINARY",
+                                             &cbinary))
+  {
+    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
+                               "arm",
+                               "BINARY");
+    GNUNET_free (loprefix);
+    GNUNET_free (lopostfix);
+    return GNUNET_ARM_RESULT_IS_NOT_KNOWN;
+  }
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg,
-                                              "arm", "CONFIG", &config))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                  _("Configuration fails to specify option `%s' in section `%s'!\n"),
-                 "CONFIG",
-                 "arm");
-      if (pos->callback != NULL)
-        pos->callback (pos->cls, GNUNET_SYSERR);
-      GNUNET_free (binary);
-      GNUNET_free (pos);
-      GNUNET_free (loprefix);
-      GNUNET_free (lopostfix);
-      return;
-    }
-  if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (pos->h->cfg,
-                                                      "TESTING",
-                                                      "WEAKRANDOM")) &&
-      (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg,
-                                                           "TESTING",
-                                                           "WEAKRANDOM")))
-    {
-      /* 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);
-    }
+      GNUNET_CONFIGURATION_get_value_filename (h->cfg,
+                                               "arm",
+                                               "CONFIG",
+                                               &config))
+    config = NULL;
+  binary = GNUNET_OS_get_libexec_binary_path (cbinary);
+  GNUNET_asprintf (&quotedbinary,
+                  "\"%s\"",
+                  binary);
+  GNUNET_free (cbinary);
+  if ( (GNUNET_YES ==
+        GNUNET_CONFIGURATION_have_value (h->cfg,
+                                         "TESTING",
+                                         "WEAKRANDOM")) &&
+       (GNUNET_YES ==
+        GNUNET_CONFIGURATION_get_value_yesno (h->cfg,
+                                              "TESTING",
+                                              "WEAKRANDOM")) &&
+       (GNUNET_NO ==
+        GNUNET_CONFIGURATION_have_value (h->cfg,
+                                         "TESTING",
+                                         "HOSTFILE")))
+  {
+    /* Means we are ONLY running locally */
+    /* we're clearly running a test, don't daemonize */
+    if (NULL == config)
+      proc = GNUNET_OS_start_process_s (GNUNET_NO,
+                                        std_inheritance,
+                                        NULL,
+                                        loprefix,
+                                        quotedbinary,
+                                        /* no daemonization! */
+                                        lopostfix,
+                                        NULL);
+    else
+      proc = GNUNET_OS_start_process_s (GNUNET_NO,
+                                        std_inheritance,
+                                        NULL,
+                                        loprefix,
+                                        quotedbinary,
+                                        "-c", config,
+                                        /* no daemonization! */
+                                        lopostfix,
+                                        NULL);
+  }
   else
-    {
-      pid = do_start_process (NULL,
-                             loprefix,
-                             binary,
-                             "-c", config,
-#if DEBUG_ARM
-                             "-L", "DEBUG",
-#endif
-                             "-d",
-                             lopostfix,
-                             NULL);
-    }
+  {
+    if (NULL == config)
+      proc = GNUNET_OS_start_process_s (GNUNET_NO,
+                                        std_inheritance,
+                                        NULL,
+                                        loprefix,
+                                        quotedbinary,
+                                        "-d", /* do daemonize */
+                                        lopostfix,
+                                       NULL);
+    else
+      proc = GNUNET_OS_start_process_s (GNUNET_NO,
+                                        std_inheritance,
+                                        NULL,
+                                        loprefix,
+                                        quotedbinary,
+                                        "-c", config,
+                                        "-d", /* do daemonize */
+                                        lopostfix,
+                                        NULL);
+  }
   GNUNET_free (binary);
-  GNUNET_free (config);
+  GNUNET_free (quotedbinary);
+  GNUNET_free_non_null (config);
   GNUNET_free (loprefix);
   GNUNET_free (lopostfix);
-  if (pid == -1)
-    {
-      if (pos->callback != NULL)
-        pos->callback (pos->cls, GNUNET_SYSERR);
-      GNUNET_free (pos);
-      return;
-    }
-  if (pos->callback != NULL)
-    pos->callback (pos->cls, GNUNET_YES);
-  GNUNET_free (pos);
+  if (NULL == proc)
+    return GNUNET_ARM_RESULT_START_FAILED;
+  GNUNET_OS_process_destroy (proc);
+  return GNUNET_ARM_RESULT_STARTING;
 }
 
 
 /**
- * Process a response from ARM to a request for a change in service
- * status.
+ * Abort an operation.  Only prevents the callback from being
+ * called, the operation may still complete.
  *
- * @param cls the request context
- * @param msg the response
+ * @param op operation to cancel
  */
-static void
-handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
+void
+GNUNET_ARM_operation_cancel (struct GNUNET_ARM_Operation *op)
 {
-  struct RequestContext *sc = cls;
-  int ret;
-
-  if (msg == NULL)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("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);
-      return;
-    }
-#if DEBUG_ARM
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received response from ARM for service `%s': %u\n",
-             (const char*) &sc[1],
-             ntohs(msg->type));
-#endif
-  switch (ntohs (msg->type))
-    {
-    case GNUNET_MESSAGE_TYPE_ARM_IS_UP:
-      ret = GNUNET_YES;
-      break;
-    case GNUNET_MESSAGE_TYPE_ARM_IS_DOWN:
-      ret = GNUNET_NO;
-      break;
-    case GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN:
-      ret = GNUNET_SYSERR;
-      break;
-    default:
-      GNUNET_break (0);
-      ret = GNUNET_SYSERR;
-    }
-  if (sc->callback != NULL)
-    sc->callback (sc->cls, ret);
-  GNUNET_free (sc);
+  struct GNUNET_ARM_Handle *h = op->h;
+
+  if (h->thm == op)
+  {
+    op->result_cont = NULL;
+    return;
+  }
+  GNUNET_CONTAINER_DLL_remove (h->operation_pending_head,
+                               h->operation_pending_tail,
+                               op);
+  GNUNET_free (op);
 }
 
 
@@ -547,183 +735,261 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
  *
  * @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 cb_cls closure for @a cb
  * @param type type of the request
+ * @return handle to queue, NULL on error
  */
-static void
+static struct GNUNET_ARM_Operation *
 change_service (struct GNUNET_ARM_Handle *h,
-               const char *service_name,
-                struct GNUNET_TIME_Relative timeout,
-                GNUNET_ARM_Callback cb, void *cb_cls, uint16_t type)
+                const char *service_name,
+                GNUNET_ARM_ResultCallback cb,
+               void *cb_cls,
+                uint16_t type)
 {
-  struct RequestContext *sctx;
+  struct GNUNET_ARM_Operation *op;
   size_t slen;
-  struct GNUNET_MessageHeader *msg;
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_ARM_Message *msg;
 
   slen = strlen (service_name) + 1;
-  if (slen + sizeof (struct GNUNET_MessageHeader) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
-    {
-      GNUNET_break (0);
-      if (cb != NULL)
-        cb (cb_cls, GNUNET_NO);
-      return;
-    }
-#if DEBUG_ARM
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              (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->timeout = GNUNET_TIME_relative_to_absolute (timeout);
-  sctx->type = type;
-  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);
+  if (slen + sizeof (struct GNUNET_ARM_Message) >=
+      GNUNET_MAX_MESSAGE_SIZE)
+  {
+    GNUNET_break (0);
+    return NULL;
+  }
+  if (0 == h->request_id_counter)
+    h->request_id_counter++;
+  op = GNUNET_new (struct GNUNET_ARM_Operation);
+  op->h = h;
+  op->result_cont = cb;
+  op->cont_cls = cb_cls;
+  op->id = h->request_id_counter++;
+  GNUNET_CONTAINER_DLL_insert_tail (h->operation_pending_head,
+                                    h->operation_pending_tail,
+                                    op);
+  env = GNUNET_MQ_msg_extra (msg,
+                             slen,
+                             type);
+  msg->reserved = htonl (0);
+  msg->request_id = GNUNET_htonll (op->id);
+  GNUNET_memcpy (&msg[1],
+          service_name,
+          slen);
+  GNUNET_MQ_send (h->mq,
+                  env);
+  return op;
 }
 
 
 /**
- * Start a service.
+ * Task run to notify application that ARM is already up.
  *
- * @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 the operation that asked ARM to be started
  */
-void
-GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
-                         const char *service_name,
-                          struct GNUNET_TIME_Relative timeout,
-                          GNUNET_ARM_Callback cb, void *cb_cls)
+static void
+notify_running (void *cls)
 {
-  struct RequestContext *sctx;
-  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))
-    {
-      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);
+  struct GNUNET_ARM_Operation *op = cls;
+  struct GNUNET_ARM_Handle *h = op->h;
+
+  op->async = NULL;
+  GNUNET_CONTAINER_DLL_remove (h->operation_pending_head,
+                               h->operation_pending_tail,
+                               op);
+  if (NULL != op->result_cont)
+    op->result_cont (op->cont_cls,
+                     GNUNET_ARM_REQUEST_SENT_OK,
+                     GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
+  if ( (GNUNET_YES == h->currently_up) &&
+       (NULL != h->conn_status) )
+    h->conn_status (h->conn_status_cls,
+                    GNUNET_YES);
+  GNUNET_free (op);
 }
 
+
 /**
- * Callback from the arm stop service call, indicates that the arm service
- * is well and truly dead, won't die, or an error occurred.
+ * Task run to notify application that ARM is being started.
  *
- * @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
+ * @param cls the operation that asked ARM to be started
  */
-void arm_shutdown_callback (void *cls,
-                            int reason)
+static void
+notify_starting (void *cls)
 {
-  struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
+  struct GNUNET_ARM_Operation *op = cls;
+  struct GNUNET_ARM_Handle *h = op->h;
+
+  op->async = NULL;
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Notifying client that we started the ARM service\n");
+  GNUNET_CONTAINER_DLL_remove (h->operation_pending_head,
+                               h->operation_pending_tail,
+                               op);
+  if (NULL != op->result_cont)
+    op->result_cont (op->cont_cls,
+                     GNUNET_ARM_REQUEST_SENT_OK,
+                     op->starting_ret);
+  GNUNET_free (op);
+}
 
-  if (arm_shutdown_ctx->cb != NULL)
-    arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
 
-  GNUNET_free(arm_shutdown_ctx);
+/**
+ * Request for a service to be started.
+ *
+ * @param h handle to ARM
+ * @param service_name name of the service
+ * @param std_inheritance inheritance of std streams
+ * @param cont callback to invoke after request is sent or not sent
+ * @param cont_cls closure for @a cont
+ * @return handle for the operation, NULL on error
+ */
+struct GNUNET_ARM_Operation *
+GNUNET_ARM_request_service_start (struct GNUNET_ARM_Handle *h,
+                                 const char *service_name,
+                                 enum GNUNET_OS_InheritStdioFlags std_inheritance,
+                                 GNUNET_ARM_ResultCallback cont,
+                                 void *cont_cls)
+{
+  struct GNUNET_ARM_Operation *op;
+  enum GNUNET_ARM_Result ret;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Starting service `%s'\n",
+       service_name);
+  if (0 != strcasecmp ("arm",
+                       service_name))
+    return change_service (h,
+                           service_name,
+                           cont,
+                           cont_cls,
+                           GNUNET_MESSAGE_TYPE_ARM_START);
+
+  /* Possible cases:
+   * 1) We're connected to ARM already. Invoke the callback immediately.
+   * 2) We're not connected to ARM.
+   *    Cancel any reconnection attempts temporarily, then perform
+   *    a service test.
+   */
+  if (GNUNET_YES == h->currently_up)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "ARM is already running\n");
+    op = GNUNET_new (struct GNUNET_ARM_Operation);
+    op->h = h;
+    op->result_cont = cont;
+    op->cont_cls = cont_cls;
+    GNUNET_CONTAINER_DLL_insert_tail (h->operation_pending_head,
+                                      h->operation_pending_tail,
+                                      op);
+    op->async = GNUNET_SCHEDULER_add_now (&notify_running,
+                                          op);
+    return op;
+  }
+  /* This is an inherently uncertain choice, as it is of course
+     theoretically possible that ARM is up and we just did not
+     yet complete the MQ handshake.  However, given that users
+     are unlikely to hammer 'gnunet-arm -s' on a busy system,
+     the above check should catch 99.99% of the cases where ARM
+     is already running. */
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Starting ARM service\n");
+  ret = start_arm_service (h,
+                           std_inheritance);
+  if (GNUNET_ARM_RESULT_STARTING == ret)
+    reconnect_arm (h);
+  op = GNUNET_new (struct GNUNET_ARM_Operation);
+  op->h = h;
+  op->result_cont = cont;
+  op->cont_cls = cont_cls;
+  GNUNET_CONTAINER_DLL_insert_tail (h->operation_pending_head,
+                                    h->operation_pending_tail,
+                                    op);
+  op->starting_ret = ret;
+  op->async = GNUNET_SCHEDULER_add_now (&notify_starting,
+                                        op);
+  return op;
 }
 
 
 /**
- * Stop a service.
+ * Request a service to be stopped.  Stopping arm itself will not
+ * invalidate its handle, and ARM API will try to restore connection
+ * to the ARM service, even if ARM connection was lost because you
+ * asked for ARM to be stopped.  Call
+ * #GNUNET_ARM_disconnect() to free the handle and prevent
+ * further connection attempts.
  *
  * @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 cont callback to invoke after request is sent or is not sent
+ * @param cont_cls closure for @a cont
+ * @return handle for the operation, NULL 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)
+struct GNUNET_ARM_Operation *
+GNUNET_ARM_request_service_stop (struct GNUNET_ARM_Handle *h,
+                                const char *service_name,
+                                GNUNET_ARM_ResultCallback cont,
+                                void *cont_cls)
 {
-  struct ARM_ShutdownContext *arm_shutdown_ctx;
-  struct GNUNET_CLIENT_Connection *client;
+  struct GNUNET_ARM_Operation *op;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Stopping service `%s'\n",
+       service_name);
+  op = change_service (h,
+                       service_name,
+                       cont,
+                       cont_cls,
+                       GNUNET_MESSAGE_TYPE_ARM_STOP);
+  if (NULL == op)
+    return NULL;
+  /* If the service is ARM, set a flag as we will use MQ errors
+     to detect that the process is really gone. */
+  if (0 == strcasecmp (service_name,
+                       "arm"))
+    op->is_arm_stop = GNUNET_YES;
+  return op;
+}
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              _("Stopping service `%s' within %llu ms\n"), service_name,
-             (unsigned long long) timeout.value);
-  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;
-    }
-  if (0 == strcasecmp ("arm", service_name))
-    {
-      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;
-    }
-  change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP);
+
+/**
+ * Request a list of running services.
+ *
+ * @param h handle to ARM
+ * @param cont callback to invoke after request is sent or is not sent
+ * @param cont_cls closure for @a cont
+ * @return handle for the operation, NULL on error
+ */
+struct GNUNET_ARM_Operation *
+GNUNET_ARM_request_service_list (struct GNUNET_ARM_Handle *h,
+                                 GNUNET_ARM_ServiceListCallback cont,
+                                 void *cont_cls)
+{
+  struct GNUNET_ARM_Operation *op;
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_ARM_Message *msg;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Requesting LIST from ARM service\n");
+  if (0 == h->request_id_counter)
+    h->request_id_counter++;
+  op = GNUNET_new (struct GNUNET_ARM_Operation);
+  op->h = h;
+  op->list_cont = cont;
+  op->cont_cls = cont_cls;
+  op->id = h->request_id_counter++;
+  GNUNET_CONTAINER_DLL_insert_tail (h->operation_pending_head,
+                                    h->operation_pending_tail,
+                                    op);
+  env = GNUNET_MQ_msg (msg,
+                       GNUNET_MESSAGE_TYPE_ARM_LIST);
+  msg->reserved = htonl (0);
+  msg->request_id = GNUNET_htonll (op->id);
+  GNUNET_MQ_send (h->mq,
+                  env);
+  return op;
 }