d0babf0ae7a4734b78340a3c415b0a22ea187d14
[oweals/gnunet.git] / src / arm / arm_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file arm/arm_api.c
23  * @brief API for accessing the ARM service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_client_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_server_lib.h"
33 #include "arm.h"
34
35 #define LOG(kind,...) GNUNET_log_from (kind, "arm-api",__VA_ARGS__)
36
37 /**
38  * Handle for interacting with ARM.
39  */
40 struct GNUNET_ARM_Handle
41 {
42
43   /**
44    * Our connection to the ARM service.
45    */
46   struct GNUNET_CLIENT_Connection *client;
47
48   /**
49    * The configuration that we are using.
50    */
51   struct GNUNET_CONFIGURATION_Handle *cfg;
52
53 };
54
55 /**
56  * Context for handling the shutdown of a service.
57  */
58 struct ShutdownContext
59 {
60   /**
61    * Connection to the service that is being shutdown.
62    */
63   struct GNUNET_CLIENT_Connection *sock;
64
65   /**
66    * Time allowed for shutdown to happen.
67    */
68   struct GNUNET_TIME_Absolute timeout;
69
70   /**
71    * Task set up to cancel the shutdown request on timeout.
72    */
73   GNUNET_SCHEDULER_TaskIdentifier cancel_task;
74
75   /**
76    * Task to call once shutdown complete
77    */
78   GNUNET_CLIENT_ShutdownTask cont;
79
80   /**
81    * Closure for shutdown continuation
82    */
83   void *cont_cls;
84
85   /**
86    * Result of the operation
87    */
88   enum GNUNET_ARM_ProcessStatus confirmed;
89
90 };
91
92
93 /**
94  * Handler receiving response to service shutdown requests.
95  * First call with NULL: service misbehaving, or something.
96  * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
97  *   - service will shutdown
98  * Second call with NULL:
99  *   - service has now really shut down.
100  *
101  * @param cls closure
102  * @param msg NULL, indicating socket closure.
103  */
104 static void
105 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
106 {
107   struct ShutdownContext *shutdown_ctx = cls;
108   const struct GNUNET_ARM_ResultMessage *rmsg;
109
110   if (msg == NULL)
111   {
112     if (shutdown_ctx->cont != NULL)
113     {
114       if (shutdown_ctx->confirmed == GNUNET_ARM_PROCESS_SHUTDOWN)
115       {
116         /* shutdown is now complete, as we waited for the network disconnect... */
117         shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_DOWN);
118       }
119       else
120       {
121         /* communication error */
122         shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
123       }
124     }
125     GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
126     GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
127     GNUNET_free (shutdown_ctx);
128     return;
129   }
130   if (ntohs (msg->size) ==
131       sizeof (struct GNUNET_ARM_ResultMessage))
132   {
133     rmsg = (const struct GNUNET_ARM_ResultMessage*) msg;
134     shutdown_ctx->confirmed = (enum GNUNET_ARM_ProcessStatus) ntohl (rmsg->status);
135     if (shutdown_ctx->confirmed != GNUNET_ARM_PROCESS_SHUTDOWN)
136     {
137       /* ARM is not shutting down, well, report the error and be done with it... */
138       shutdown_ctx->cont (shutdown_ctx->cont_cls, shutdown_ctx->confirmed);
139       GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
140       GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
141       GNUNET_free (shutdown_ctx);
142       return;
143     }
144   }
145   GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
146                          shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
147 }
148
149
150 /**
151  * Shutting down took too long, cancel receive and return error.
152  *
153  * @param cls closure
154  * @param tc context information (why was this task triggered now)
155  */
156 static void
157 service_shutdown_cancel (void *cls,
158                          const struct GNUNET_SCHEDULER_TaskContext *tc)
159 {
160   struct ShutdownContext *shutdown_ctx = cls;
161
162   shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT);
163   GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
164   GNUNET_free (shutdown_ctx);
165 }
166
167
168 /**
169  * If possible, write a shutdown message to the target
170  * buffer and destroy the client connection.
171  *
172  * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
173  * @param size number of bytes available in buf
174  * @param buf NULL on error, otherwise target buffer
175  * @return number of bytes written to buf
176  */
177 static size_t
178 write_shutdown (void *cls, size_t size, void *buf)
179 {
180   struct GNUNET_MessageHeader *msg;
181   struct ShutdownContext *shutdown_ctx = cls;
182
183   if (size < sizeof (struct GNUNET_MessageHeader))
184     {
185       LOG (GNUNET_ERROR_TYPE_WARNING,
186            _("Failed to transmit shutdown request to client.\n"));
187       shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
188       GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
189       GNUNET_free (shutdown_ctx);
190       return 0;                 /* client disconnected */
191     }
192
193   GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
194                          shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
195   shutdown_ctx->cancel_task =
196     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
197                                   (shutdown_ctx->timeout),
198                                   &service_shutdown_cancel, shutdown_ctx);
199   msg = (struct GNUNET_MessageHeader *) buf;
200   msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
201   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
202   return sizeof (struct GNUNET_MessageHeader);
203 }
204
205
206 /**
207  * Request that the service should shutdown.
208  * Afterwards, the connection will automatically be
209  * disconnected.  Hence the "sock" should not
210  * be used by the caller after this call
211  * (calling this function frees "sock" after a while).
212  *
213  * @param sock the socket connected to the service
214  * @param timeout how long to wait before giving up on transmission
215  * @param cont continuation to call once the service is really down
216  * @param cont_cls closure for continuation
217  *
218  */
219 static void
220 arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
221                       struct GNUNET_TIME_Relative timeout,
222                       GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
223 {
224   struct ShutdownContext *shutdown_ctx;
225
226   shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
227   shutdown_ctx->cont = cont;
228   shutdown_ctx->cont_cls = cont_cls;
229   shutdown_ctx->sock = sock;
230   shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
231   shutdown_ctx->confirmed = GNUNET_ARM_PROCESS_COMMUNICATION_ERROR;    
232   /* FIXME: store return value? */
233   GNUNET_CLIENT_notify_transmit_ready (sock,
234                                        sizeof (struct GNUNET_MessageHeader),
235                                        timeout, GNUNET_YES, &write_shutdown,
236                                        shutdown_ctx);
237 }
238
239
240 /**
241  * Setup a context for communicating with ARM.  Note that this
242  * can be done even if the ARM service is not yet running.
243  *
244  * @param cfg configuration to use (needed to contact ARM;
245  *        the ARM service may internally use a different
246  *        configuration to determine how to start the service).
247  * @param service service that *this* process is implementing/providing, can be NULL
248  * @return context to use for further ARM operations, NULL on error
249  */
250 struct GNUNET_ARM_Handle *
251 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
252                     const char *service)
253 {
254   struct GNUNET_ARM_Handle *ret;
255
256   ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
257   ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
258   return ret;
259 }
260
261
262 /**
263  * Disconnect from the ARM service.
264  *
265  * @param h the handle that was being used
266  */
267 void
268 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
269 {
270   if (h->client != NULL)
271     GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
272   GNUNET_CONFIGURATION_destroy (h->cfg);
273   GNUNET_free (h);
274 }
275
276
277 struct ARM_ShutdownContext
278 {
279   /**
280    * Callback to call once shutdown complete.
281    */
282   GNUNET_ARM_Callback cb;
283
284   /**
285    * Closure for callback.
286    */
287   void *cb_cls;
288 };
289
290
291 /**
292  * Internal state for a request with ARM.
293  */
294 struct RequestContext
295 {
296
297   /**
298    * Pointer to our handle with ARM.
299    */
300   struct GNUNET_ARM_Handle *h;
301
302   /**
303    * Function to call with a status code for the requested operation.
304    */
305   GNUNET_ARM_Callback callback;
306
307   /**
308    * Closure for "callback".
309    */
310   void *cls;
311
312   /**
313    * Timeout for the operation.
314    */
315   struct GNUNET_TIME_Absolute timeout;
316
317   /**
318    * Type of the request expressed as a message type (start or stop).
319    */
320   uint16_t type;
321
322 };
323
324 #include "do_start_process.c"
325
326
327 /**
328  * A client specifically requested starting of ARM itself.
329  * This function is called with information about whether
330  * or not ARM is running; if it is, report success.  If
331  * it is not, start the ARM process.
332  *
333  * @param cls the context for the request that we will report on (struct RequestContext*)
334  * @param tc why were we called (reason says if ARM is running)
335  */
336 static void
337 arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
338 {
339   struct RequestContext *pos = cls;
340   struct GNUNET_OS_Process *proc;
341   char *binary;
342   char *config;
343   char *loprefix;
344   char *lopostfix;
345
346   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
347     {
348 #if DEBUG_ARM
349       LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
350            "gnunet-service-arm");
351 #endif
352       /* arm is running! */
353       if (pos->callback != NULL)
354         pos->callback (pos->cls, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
355       GNUNET_free (pos);
356       return;
357     }
358 #if DEBUG_ARM
359   LOG (GNUNET_ERROR_TYPE_DEBUG,
360        "Looks like `%s' is not running, will start it.\n",
361        "gnunet-service-arm");
362 #endif
363   if (GNUNET_OK !=
364       GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "PREFIX",
365                                              &loprefix))
366     loprefix = GNUNET_strdup ("");
367   if (GNUNET_OK !=
368       GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "OPTIONS",
369                                              &lopostfix))
370     lopostfix = GNUNET_strdup ("");
371   if (GNUNET_OK !=
372       GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "BINARY",
373                                              &binary))
374     {
375       LOG (GNUNET_ERROR_TYPE_WARNING,
376            _
377            ("Configuration failes to specify option `%s' in section `%s'!\n"),
378            "BINARY", "arm");
379       if (pos->callback != NULL)
380         pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
381       GNUNET_free (pos);
382       GNUNET_free (loprefix);
383       GNUNET_free (lopostfix);
384       return;
385     }
386   if (GNUNET_OK !=
387       GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg, "arm", "CONFIG",
388                                                &config))
389     {
390       LOG (GNUNET_ERROR_TYPE_WARNING,
391            _("Configuration fails to specify option `%s' in section `%s'!\n"),
392            "CONFIG", "arm");
393       if (pos->callback != NULL)
394         pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
395       GNUNET_free (binary);
396       GNUNET_free (pos);
397       GNUNET_free (loprefix);
398       GNUNET_free (lopostfix);
399       return;
400     }
401   if ((GNUNET_YES ==
402        GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING", "WEAKRANDOM"))
403       && (GNUNET_YES ==
404           GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg, "TESTING",
405                                                 "WEAKRANDOM"))
406       && (GNUNET_NO ==
407           GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING",
408                                            "HOSTFILE")))
409     {
410       /* Means we are ONLY running locally */
411       /* we're clearly running a test, don't daemonize */
412       proc = do_start_process (GNUNET_NO,
413                                NULL, loprefix, binary, "-c", config,
414 #if DEBUG_ARM
415                                "-L", "DEBUG",
416 #endif
417                                /* no daemonization! */
418                                lopostfix, NULL);
419     }
420   else
421     {
422       proc = do_start_process (GNUNET_NO,
423                                NULL, loprefix, binary, "-c", config,
424 #if DEBUG_ARM
425                                "-L", "DEBUG",
426 #endif
427                                "-d", lopostfix, NULL);
428     }
429   GNUNET_free (binary);
430   GNUNET_free (config);
431   GNUNET_free (loprefix);
432   GNUNET_free (lopostfix);
433   if (proc == NULL)
434     {
435       if (pos->callback != NULL)
436         pos->callback (pos->cls, GNUNET_ARM_PROCESS_FAILURE);
437       GNUNET_free (pos);
438       return;
439     }
440   if (pos->callback != NULL)
441     pos->callback (pos->cls, GNUNET_ARM_PROCESS_STARTING);
442   GNUNET_free (proc);
443   GNUNET_free (pos);
444 }
445
446
447 /**
448  * Process a response from ARM to a request for a change in service
449  * status.
450  *
451  * @param cls the request context
452  * @param msg the response
453  */
454 static void
455 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
456 {
457   struct RequestContext *sc = cls;
458   const struct GNUNET_ARM_ResultMessage *res;
459   enum GNUNET_ARM_ProcessStatus status;
460
461   if ((msg == NULL) ||
462       (ntohs (msg->size) != sizeof (struct GNUNET_ARM_ResultMessage)))
463     {
464       LOG (GNUNET_ERROR_TYPE_WARNING,
465            _
466            ("Error receiving response to `%s' request from ARM for service `%s'\n"),
467            (sc->type == GNUNET_MESSAGE_TYPE_ARM_START) ? "START" : "STOP",
468            (const char *) &sc[1]);
469       GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
470       sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
471       GNUNET_assert (NULL != sc->h->client);
472       if (sc->callback != NULL)
473         sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
474       GNUNET_free (sc);
475       return;
476     }
477   res = (const struct GNUNET_ARM_ResultMessage *) msg;
478 #if DEBUG_ARM
479   LOG (GNUNET_ERROR_TYPE_DEBUG,
480        "Received response from ARM for service `%s': %u\n",
481        (const char *) &sc[1], ntohs (msg->type));
482 #endif
483   status = (enum GNUNET_ARM_ProcessStatus) ntohl (res->status);
484   if (sc->callback != NULL)
485     sc->callback (sc->cls, status);
486   GNUNET_free (sc);
487 }
488
489 /**
490  * Internal state for a list request with ARM.
491  */
492 struct ListRequestContext
493 {
494
495   /**
496    * Pointer to our handle with ARM.
497    */
498   struct GNUNET_ARM_Handle *h;
499
500   /**
501    * Function to call with a status code for the requested operation.
502    */
503   GNUNET_ARM_List_Callback callback;
504
505   /**
506    * Closure for "callback".
507    */
508   void *cls;
509
510   /**
511    * Timeout for the operation.
512    */
513   struct GNUNET_TIME_Absolute timeout;
514 };
515
516 /**
517  * Process a response from ARM for the list request.
518  *
519  * @param cls the list request context
520  * @param msg the response
521  */
522 static void
523 handle_list_response (void *cls, const struct GNUNET_MessageHeader *msg)
524 {
525   struct ListRequestContext *sc = cls;
526   const struct GNUNET_ARM_ListResultMessage *res;
527   int success;
528   
529    if (msg == NULL)
530    {
531       LOG (GNUNET_ERROR_TYPE_WARNING,
532            "Error receiving response to LIST request from ARM\n");
533       GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
534       sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
535       GNUNET_assert (NULL != sc->h->client);
536       if (sc->callback != NULL)
537         sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
538       GNUNET_free (sc);
539       return;
540    }
541    
542    if (sc->callback != NULL) 
543    {
544      char **list;
545      const char *pos;
546      uint16_t size_check;
547      
548      size_check = 0;
549      res = (const struct GNUNET_ARM_ListResultMessage *) msg;
550      success = (ntohs (res->header.type) 
551                 == GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT ? 
552                 GNUNET_YES : GNUNET_NO);
553      list = GNUNET_malloc (ntohs (res->count) * sizeof (char *));
554      pos = (const char *)&res[1];
555      
556      int i;
557      for (i=0; i<ntohs (res->count); i++)
558      {
559        list[i] = GNUNET_malloc (strlen (pos) + 1);
560        memcpy (list[i], pos, strlen (pos) + 1);
561        pos += strlen (pos) + 1;
562        size_check += strlen (pos) +1;
563        
564        if (size_check > ntohs (res->header.size))
565        {
566           GNUNET_free (list);
567           GNUNET_free (sc);
568           sc->callback (sc->cls, GNUNET_NO, 0, NULL);
569           return;
570        }
571      }
572      
573      sc->callback (sc->cls, success, ntohs (res->count), (const char**)list);
574    }
575    GNUNET_free (sc);
576 }
577
578 /**
579  * Start or stop a service.
580  *
581  * @param h handle to ARM
582  * @param service_name name of the service
583  * @param timeout how long to wait before failing for good
584  * @param cb callback to invoke when service is ready
585  * @param cb_cls closure for callback
586  * @param type type of the request
587  */
588 static void
589 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
590                 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_Callback cb,
591                 void *cb_cls, uint16_t type)
592 {
593   struct RequestContext *sctx;
594   size_t slen;
595   struct GNUNET_MessageHeader *msg;
596
597   slen = strlen (service_name) + 1;
598   if (slen + sizeof (struct GNUNET_MessageHeader) >=
599       GNUNET_SERVER_MAX_MESSAGE_SIZE)
600     {
601       GNUNET_break (0);
602       if (cb != NULL)
603         cb (cb_cls, GNUNET_NO);
604       return;
605     }
606 #if DEBUG_ARM
607   LOG (GNUNET_ERROR_TYPE_DEBUG,
608        (type ==
609         GNUNET_MESSAGE_TYPE_ARM_START) ?
610        _("Requesting start of service `%s'.\n") :
611        _("Requesting termination of service `%s'.\n"), service_name);
612 #endif
613   sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
614   sctx->h = h;
615   sctx->callback = cb;
616   sctx->cls = cb_cls;
617   sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
618   sctx->type = type;
619   memcpy (&sctx[1], service_name, slen);
620   msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
621   msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
622   msg->type = htons (sctx->type);
623   memcpy (&msg[1], service_name, slen);
624   if (GNUNET_OK !=
625       GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, msg,
626                                                GNUNET_TIME_absolute_get_remaining
627                                                (sctx->timeout), GNUNET_YES,
628                                                &handle_response, sctx))
629     {
630       LOG (GNUNET_ERROR_TYPE_WARNING,
631            (type ==
632             GNUNET_MESSAGE_TYPE_ARM_START) ?
633            _("Error while trying to transmit request to start `%s' to ARM\n")
634            :
635            _("Error while trying to transmit request to stop `%s' to ARM\n"),
636            (const char *) &service_name);
637       if (cb != NULL)
638         cb (cb_cls, GNUNET_SYSERR);
639       GNUNET_free (sctx);
640       GNUNET_free (msg);
641       return;
642     }
643   GNUNET_free (msg);
644 }
645
646
647 /**
648  * Start a service.
649  *
650  * @param h handle to ARM
651  * @param service_name name of the service
652  * @param timeout how long to wait before failing for good
653  * @param cb callback to invoke when service is ready
654  * @param cb_cls closure for callback
655  */
656 void
657 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
658                           const char *service_name,
659                           struct GNUNET_TIME_Relative timeout,
660                           GNUNET_ARM_Callback cb, void *cb_cls)
661 {
662   struct RequestContext *sctx;
663   struct GNUNET_CLIENT_Connection *client;
664   size_t slen;
665
666 #if DEBUG_ARM
667   LOG (GNUNET_ERROR_TYPE_DEBUG,
668        _("Asked to start service `%s' within %llu ms\n"), service_name,
669        (unsigned long long) timeout.rel_value);
670 #endif
671   if (0 == strcasecmp ("arm", service_name))
672     {
673       slen = strlen ("arm") + 1;
674       sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
675       sctx->h = h;
676       sctx->callback = cb;
677       sctx->cls = cb_cls;
678       sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
679       memcpy (&sctx[1], service_name, slen);
680       GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
681                                   sctx);
682       return;
683     }
684   if (h->client == NULL)
685     {
686       client = GNUNET_CLIENT_connect ("arm", h->cfg);
687       if (client == NULL)
688         {
689           LOG (GNUNET_ERROR_TYPE_DEBUG,
690                "arm_api, GNUNET_CLIENT_connect returned NULL\n");
691           cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
692           return;
693         }
694       LOG (GNUNET_ERROR_TYPE_DEBUG,
695            "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
696       h->client = client;
697     }
698   LOG (GNUNET_ERROR_TYPE_DEBUG, "arm_api, h->client non-NULL\n");
699   change_service (h, service_name, timeout, cb, cb_cls,
700                   GNUNET_MESSAGE_TYPE_ARM_START);
701 }
702
703
704 /**
705  * Callback from the arm stop service call, indicates that the arm service
706  * is well and truly dead, won't die, or an error occurred.
707  *
708  * @param cls closure for the callback
709  * @param reason reason for callback
710  */
711 static void
712 arm_shutdown_callback (void *cls, enum GNUNET_ARM_ProcessStatus reason)
713 {
714   struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
715
716   if (arm_shutdown_ctx->cb != NULL)
717     arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
718
719   GNUNET_free (arm_shutdown_ctx);
720 }
721
722
723 /**
724  * Stop a service.
725  *
726  * @param h handle to ARM
727  * @param service_name name of the service
728  * @param timeout how long to wait before failing for good
729  * @param cb callback to invoke when service is ready
730  * @param cb_cls closure for callback
731  */
732 void
733 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
734                          const char *service_name,
735                          struct GNUNET_TIME_Relative timeout,
736                          GNUNET_ARM_Callback cb, void *cb_cls)
737 {
738   struct ARM_ShutdownContext *arm_shutdown_ctx;
739   struct GNUNET_CLIENT_Connection *client;
740
741   LOG (GNUNET_ERROR_TYPE_INFO, _("Stopping service `%s' within %llu ms\n"),
742        service_name, (unsigned long long) timeout.rel_value);
743   if (h->client == NULL)
744     {
745       client = GNUNET_CLIENT_connect ("arm", h->cfg);
746       if (client == NULL)
747         {
748           cb (cb_cls, GNUNET_SYSERR);
749           return;
750         }
751       h->client = client;
752     }
753   if (0 == strcasecmp ("arm", service_name))
754     {
755       arm_shutdown_ctx = GNUNET_malloc (sizeof (struct ARM_ShutdownContext));
756       arm_shutdown_ctx->cb = cb;
757       arm_shutdown_ctx->cb_cls = cb_cls;
758       arm_service_shutdown (h->client, timeout, &arm_shutdown_callback,
759                             arm_shutdown_ctx);
760       h->client = NULL;
761       return;
762     }
763   change_service (h, service_name, timeout, cb, cb_cls,
764                   GNUNET_MESSAGE_TYPE_ARM_STOP);
765 }
766
767 /**
768  * List all running services.
769  * 
770  * @param h handle to ARM
771  * @param timeout how long to wait before failing for good
772  * @param cb callback to invoke when service is ready
773  * @param cb_cls closure for callback
774  */
775 void
776 GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
777                                   struct GNUNET_TIME_Relative timeout,
778                                   GNUNET_ARM_List_Callback cb, void *cb_cls)
779 {
780   struct ListRequestContext *sctx;
781   struct GNUNET_MessageHeader *msg;
782   struct GNUNET_CLIENT_Connection *client;
783   
784     if (h->client == NULL)
785     {
786       client = GNUNET_CLIENT_connect ("arm", h->cfg);
787       if (client == NULL)
788         {
789           LOG (GNUNET_ERROR_TYPE_DEBUG,
790                "arm_api, GNUNET_CLIENT_connect returned NULL\n");
791           cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
792           return;
793         }
794       LOG (GNUNET_ERROR_TYPE_DEBUG,
795            "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
796       h->client = client;
797     }
798
799   sctx = GNUNET_malloc (sizeof (struct RequestContext));
800   sctx->h = h;
801   sctx->callback = cb;
802   sctx->cls = cb_cls;
803   sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
804   msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader));
805   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
806   msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
807   
808   LOG(GNUNET_ERROR_TYPE_DEBUG, 
809       "Requesting LIST from ARM service with timeout: %llu ms\n", 
810       (unsigned long long)timeout.rel_value);
811   
812     if (GNUNET_OK !=
813       GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, 
814                                                msg,
815                                                GNUNET_TIME_absolute_get_remaining
816                                                (sctx->timeout), 
817                                                GNUNET_YES,
818                                                &handle_list_response, 
819                                                sctx))
820     {
821       LOG (GNUNET_ERROR_TYPE_WARNING, 
822            "Error while trying to transmit request to list services to ARM\n");
823       if (cb != NULL)
824         cb (cb_cls, GNUNET_SYSERR, 0, NULL);
825       GNUNET_free (sctx);
826       GNUNET_free (msg);
827       return;
828     }
829   GNUNET_free (msg);
830 }
831
832 /* end of arm_api.c */