2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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 2, or (at your
8 option) any later version.
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.
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.
23 * @brief API for accessing the ARM service
24 * @author Christian Grothoff
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"
37 * How long are we willing to wait for a service operation during the multi-operation
40 #define MULTI_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
44 * Handle for interacting with ARM.
46 struct GNUNET_ARM_Handle
50 * Our connection to the ARM service.
52 struct GNUNET_CLIENT_Connection *client;
55 * The configuration that we are using.
57 struct GNUNET_CONFIGURATION_Handle *cfg;
62 struct GNUNET_SCHEDULER_Handle *sched;
68 * Setup a context for communicating with ARM. Note that this
69 * can be done even if the ARM service is not yet running.
71 * @param cfg configuration to use (needed to contact ARM;
72 * the ARM service may internally use a different
73 * configuration to determine how to start the service).
74 * @param sched scheduler to use
75 * @param service service that *this* process is implementing/providing, can be NULL
76 * @return context to use for further ARM operations, NULL on error
78 struct GNUNET_ARM_Handle *
79 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
80 struct GNUNET_SCHEDULER_Handle *sched,
83 struct GNUNET_ARM_Handle *ret;
84 struct GNUNET_CLIENT_Connection *client;
86 client = GNUNET_CLIENT_connect (sched, "arm", cfg);
89 GNUNET_CLIENT_ignore_shutdown (client, GNUNET_YES);
90 ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
91 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
99 * Disconnect from the ARM service.
101 * @param h the handle that was being used
104 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
106 if (h->client != NULL)
107 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
108 GNUNET_CONFIGURATION_destroy (h->cfg);
112 struct ARM_ShutdownContext
115 * Callback to call once shutdown complete.
117 GNUNET_ARM_Callback cb;
120 * Closure for callback.
128 * Internal state for a request with ARM.
130 struct RequestContext
134 * Pointer to our handle with ARM.
136 struct GNUNET_ARM_Handle *h;
139 * Function to call with a status code for the requested operation.
141 GNUNET_ARM_Callback callback;
144 * Closure for "callback".
149 * Timeout for the operation.
151 struct GNUNET_TIME_Absolute timeout;
154 * Type of the request expressed as a message type (start or stop).
160 #include "do_start_process.c"
164 * A client specifically requested starting of ARM itself.
165 * This function is called with information about whether
166 * or not ARM is running; if it is, report success. If
167 * it is not, start the ARM process.
169 * @param cls the context for the request that we will report on (struct RequestContext*)
170 * @param tc why were we called (reason says if ARM is running)
173 arm_service_report (void *cls,
174 const struct GNUNET_SCHEDULER_TaskContext *tc)
176 struct RequestContext *pos = cls;
183 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 "Looks like `%s' is already running.\n",
188 "gnunet-service-arm");
190 /* arm is running! */
191 if (pos->callback != NULL)
192 pos->callback (pos->cls, GNUNET_YES);
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Looks like `%s' is not running, will start it.\n",
199 "gnunet-service-arm");
201 /* FIXME: should we check that HOSTNAME for 'arm' is localhost? */
204 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
205 "arm", "PREFIX", &loprefix))
206 loprefix = GNUNET_strdup ("");
208 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
209 "arm", "OPTIONS", &lopostfix))
210 lopostfix = GNUNET_strdup ("");
212 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
218 _("Configuration failes to specify option `%s' in section `%s'!\n"),
221 if (pos->callback != NULL)
222 pos->callback (pos->cls, GNUNET_SYSERR);
224 GNUNET_free (loprefix);
225 GNUNET_free (lopostfix);
229 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg,
230 "arm", "CONFIG", &config))
232 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
233 _("Configuration fails to specify option `%s' in section `%s'!\n"),
236 if (pos->callback != NULL)
237 pos->callback (pos->cls, GNUNET_SYSERR);
238 GNUNET_free (binary);
240 GNUNET_free (loprefix);
241 GNUNET_free (lopostfix);
244 pid = do_start_process (loprefix,
253 GNUNET_free (binary);
254 GNUNET_free (config);
255 GNUNET_free (loprefix);
256 GNUNET_free (lopostfix);
259 if (pos->callback != NULL)
260 pos->callback (pos->cls, GNUNET_SYSERR);
264 if (pos->callback != NULL)
265 pos->callback (pos->cls, GNUNET_YES);
271 * Process a response from ARM to a request for a change in service
274 * @param cls the request context
275 * @param msg the response
278 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
280 struct RequestContext *sc = cls;
285 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
286 _("Error receiving response to `%s' request from ARM for service `%s'\n"),
287 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START)
290 (const char*) &sc[1]);
291 GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
292 sc->h->client = GNUNET_CLIENT_connect (sc->h->sched,
295 GNUNET_assert (NULL != sc->h->client);
296 GNUNET_CLIENT_ignore_shutdown (sc->h->client, GNUNET_YES);
297 if (sc->callback != NULL)
298 sc->callback (sc->cls, GNUNET_SYSERR);
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 "Received response from ARM for service `%s': %u\n",
305 (const char*) &sc[1],
308 switch (ntohs (msg->type))
310 case GNUNET_MESSAGE_TYPE_ARM_IS_UP:
313 case GNUNET_MESSAGE_TYPE_ARM_IS_DOWN:
316 case GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN:
323 if (sc->callback != NULL)
324 sc->callback (sc->cls, ret);
330 * Start or stop a service.
332 * @param h handle to ARM
333 * @param service_name name of the service
334 * @param timeout how long to wait before failing for good
335 * @param cb callback to invoke when service is ready
336 * @param cb_cls closure for callback
337 * @param type type of the request
340 change_service (struct GNUNET_ARM_Handle *h,
341 const char *service_name,
342 struct GNUNET_TIME_Relative timeout,
343 GNUNET_ARM_Callback cb, void *cb_cls, uint16_t type)
345 struct RequestContext *sctx;
347 struct GNUNET_MessageHeader *msg;
349 slen = strlen (service_name) + 1;
350 if (slen + sizeof (struct GNUNET_MessageHeader) >
351 GNUNET_SERVER_MAX_MESSAGE_SIZE)
355 cb (cb_cls, GNUNET_NO);
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360 (type == GNUNET_MESSAGE_TYPE_ARM_START)
361 ? _("Requesting start of service `%s'.\n")
362 : _("Requesting termination of service `%s'.\n"),
365 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
369 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
371 memcpy (&sctx[1], service_name, slen);
372 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
373 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
374 msg->type = htons (sctx->type);
375 memcpy (&msg[1], service_name, slen);
377 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
379 GNUNET_TIME_absolute_get_remaining (sctx->timeout),
384 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
385 (type == GNUNET_MESSAGE_TYPE_ARM_START)
386 ? _("Error while trying to transmit request to start `%s' to ARM\n")
387 : _("Error while trying to transmit request to stop `%s' to ARM\n"),
388 (const char*) &service_name);
390 cb (cb_cls, GNUNET_SYSERR);
402 * @param h handle to ARM
403 * @param service_name name of the service
404 * @param timeout how long to wait before failing for good
405 * @param cb callback to invoke when service is ready
406 * @param cb_cls closure for callback
409 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
410 const char *service_name,
411 struct GNUNET_TIME_Relative timeout,
412 GNUNET_ARM_Callback cb, void *cb_cls)
414 struct RequestContext *sctx;
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 _("Asked to start service `%s' within %llu ms\n"), service_name,
419 (unsigned long long) timeout.value);
421 if (0 == strcasecmp ("arm", service_name))
423 slen = strlen ("arm") + 1;
424 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
428 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
429 memcpy (&sctx[1], service_name, slen);
430 GNUNET_CLIENT_service_test (h->sched,
432 h->cfg, timeout, &arm_service_report, sctx);
435 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_START);
439 * Callback from the arm stop service call, indicates that the arm service
440 * is well and truly dead.
442 * @param cls closure for the callback
443 * @param tc scheduler context
445 void arm_shutdown_callback (void *cls,
448 struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
450 if (arm_shutdown_ctx->cb != NULL)
451 arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
458 * @param h handle to ARM
459 * @param service_name name of the service
460 * @param timeout how long to wait before failing for good
461 * @param cb callback to invoke when service is ready
462 * @param cb_cls closure for callback
465 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
466 const char *service_name,
467 struct GNUNET_TIME_Relative timeout,
468 GNUNET_ARM_Callback cb, void *cb_cls)
470 struct ARM_ShutdownContext *arm_shutdown_ctx;
472 arm_shutdown_ctx = GNUNET_malloc(sizeof(struct ARM_ShutdownContext));
473 arm_shutdown_ctx->cb = cb;
474 arm_shutdown_ctx->cb_cls = cb_cls;
476 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
477 _("Stopping service `%s' within %llu ms\n"), service_name,
478 (unsigned long long) timeout.value);
479 if (0 == strcasecmp ("arm", service_name))
481 GNUNET_CLIENT_service_shutdown (h->sched, h->client, timeout, &arm_shutdown_callback, arm_shutdown_ctx);
485 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP);
490 * Function to call for each service.
492 * @param h handle to ARM
493 * @param service_name name of the service
494 * @param timeout how long to wait before failing for good
495 * @param cb callback to invoke when service is ready
496 * @param cb_cls closure for callback
498 typedef void (*ServiceOperation) (struct GNUNET_ARM_Handle *h,
499 const char *service_name,
500 struct GNUNET_TIME_Relative timeout,
501 GNUNET_ARM_Callback cb, void *cb_cls);
505 * Context for starting or stopping multiple services.
510 * NULL-terminated array of services to start or stop.
517 struct GNUNET_ARM_Handle *h;
520 * Identifies the operation (start or stop).
525 * Current position in "services".
532 * Run the operation for the next service in the multi-service
535 * @param cls the "struct MultiContext" that is being processed
536 * @param success status of the previous operation (ignored)
539 next_operation (void *cls,
542 struct MultiContext *mc = cls;
545 if (NULL == (pos = mc->services[mc->pos]))
547 GNUNET_free (mc->services);
548 GNUNET_ARM_disconnect (mc->h);
553 mc->op (mc->h, pos, MULTI_TIMEOUT, &next_operation, mc);
559 * Run a multi-service request.
561 * @param cfg configuration to use (needed to contact ARM;
562 * the ARM service may internally use a different
563 * configuration to determine how to start the service).
564 * @param sched scheduler to use
565 * @param op the operation to perform for each service
566 * @param va NULL-terminated list of services
569 run_multi_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
570 struct GNUNET_SCHEDULER_Handle *sched,
576 struct MultiContext *mc;
577 struct GNUNET_ARM_Handle *h;
580 h = GNUNET_ARM_connect (cfg, sched, NULL);
583 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
584 _("Error while trying to transmit to ARM service\n"));
589 while (NULL != (va_arg (cp, const char*))) total++;
591 mc = GNUNET_malloc (sizeof(struct MultiContext));
592 mc->services = GNUNET_malloc (total * sizeof (char*));
597 while (NULL != (c = va_arg (cp, const char*)))
598 mc->services[total++] = GNUNET_strdup (c);
600 next_operation (mc, GNUNET_YES);
605 * Start multiple services in the specified order. Convenience
606 * function. Works asynchronously, failures are not reported.
608 * @param cfg configuration to use (needed to contact ARM;
609 * the ARM service may internally use a different
610 * configuration to determine how to start the service).
611 * @param sched scheduler to use
612 * @param ... NULL-terminated list of service names (const char*)
615 GNUNET_ARM_start_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
616 struct GNUNET_SCHEDULER_Handle *sched,
621 va_start (ap, sched);
622 run_multi_request (cfg, sched, &GNUNET_ARM_start_service, ap);
628 * Stop multiple services in the specified order. Convenience
629 * function. Works asynchronously, failures are not reported.
630 * Should normally only be called from gnunet-arm or testcases,
631 * stopping a service is generally otherwise a bad idea.
633 * @param cfg configuration to use (needed to contact ARM;
634 * the ARM service may internally use a different
635 * configuration to determine how to start the service).
636 * @param sched scheduler to use
637 * @param ... NULL-terminated list of service names (const char*)
640 GNUNET_ARM_stop_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
641 struct GNUNET_SCHEDULER_Handle *sched,
646 va_start (ap, sched);
647 run_multi_request (cfg, sched, &GNUNET_ARM_stop_service, ap);
652 /* end of arm_api.c */