2 This file is part of GNUnet.
3 (C) 2009, 2010 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 3, 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 * Handle for interacting with ARM.
39 struct GNUNET_ARM_Handle
43 * Our connection to the ARM service.
45 struct GNUNET_CLIENT_Connection *client;
48 * The configuration that we are using.
50 struct GNUNET_CONFIGURATION_Handle *cfg;
56 * Context for handling the shutdown of a service.
58 struct ShutdownContext
61 * Connection to the service that is being shutdown.
63 struct GNUNET_CLIENT_Connection *sock;
66 * Time allowed for shutdown to happen.
68 struct GNUNET_TIME_Absolute timeout;
71 * Task set up to cancel the shutdown request on timeout.
73 GNUNET_SCHEDULER_TaskIdentifier cancel_task;
76 * Task to call once shutdown complete
78 GNUNET_CLIENT_ShutdownTask cont;
81 * Closure for shutdown continuation
86 * We received a confirmation that the service will shut down.
93 * Handler receiving response to service shutdown requests.
94 * First call with NULL: service misbehaving, or something.
95 * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
96 * - service will shutdown
97 * Second call with NULL:
98 * - service has now really shut down.
101 * @param msg NULL, indicating socket closure.
104 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
106 struct ShutdownContext *shutdown_ctx = cls;
108 if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))
110 /* Means the other side closed the connection and never confirmed a shutdown */
111 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
112 "Service handle shutdown before ACK!\n");
113 if (shutdown_ctx->cont != NULL)
114 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
115 GNUNET_SCHEDULER_cancel(shutdown_ctx->cancel_task);
116 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
117 GNUNET_free(shutdown_ctx);
119 else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES))
122 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
123 "Service shutdown complete.\n");
125 if (shutdown_ctx->cont != NULL)
126 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_NO);
128 GNUNET_SCHEDULER_cancel(shutdown_ctx->cancel_task);
129 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
130 GNUNET_free(shutdown_ctx);
134 GNUNET_assert(ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
135 switch (ntohs(msg->type))
137 case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
139 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
140 "Received confirmation for service shutdown.\n");
142 shutdown_ctx->confirmed = GNUNET_YES;
143 GNUNET_CLIENT_receive (shutdown_ctx->sock,
144 &service_shutdown_handler,
146 GNUNET_TIME_UNIT_FOREVER_REL);
148 default: /* Fall through */
150 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
151 "Service shutdown refused!\n");
153 if (shutdown_ctx->cont != NULL)
154 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_YES);
156 GNUNET_SCHEDULER_cancel(shutdown_ctx->cancel_task);
157 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
158 GNUNET_free(shutdown_ctx);
165 * Shutting down took too long, cancel receive and return error.
168 * @param tc context information (why was this task triggered now)
170 void service_shutdown_cancel (void *cls,
171 const struct GNUNET_SCHEDULER_TaskContext * tc)
173 struct ShutdownContext *shutdown_ctx = cls;
174 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
175 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
176 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
177 GNUNET_free(shutdown_ctx);
182 * If possible, write a shutdown message to the target
183 * buffer and destroy the client connection.
185 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
186 * @param size number of bytes available in buf
187 * @param buf NULL on error, otherwise target buffer
188 * @return number of bytes written to buf
191 write_shutdown (void *cls, size_t size, void *buf)
193 struct GNUNET_MessageHeader *msg;
194 struct ShutdownContext *shutdown_ctx = cls;
196 if (size < sizeof (struct GNUNET_MessageHeader))
198 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
199 _("Failed to transmit shutdown request to client.\n"));
200 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
201 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
202 GNUNET_free(shutdown_ctx);
203 return 0; /* client disconnected */
206 GNUNET_CLIENT_receive (shutdown_ctx->sock,
207 &service_shutdown_handler, shutdown_ctx,
208 GNUNET_TIME_UNIT_FOREVER_REL);
209 shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining(shutdown_ctx->timeout),
210 &service_shutdown_cancel,
212 msg = (struct GNUNET_MessageHeader *) buf;
213 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
214 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
215 return sizeof (struct GNUNET_MessageHeader);
220 * Request that the service should shutdown.
221 * Afterwards, the connection will automatically be
222 * disconnected. Hence the "sock" should not
223 * be used by the caller after this call
224 * (calling this function frees "sock" after a while).
226 * @param sock the socket connected to the service
227 * @param timeout how long to wait before giving up on transmission
228 * @param cont continuation to call once the service is really down
229 * @param cont_cls closure for continuation
233 arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
234 struct GNUNET_TIME_Relative timeout,
235 GNUNET_CLIENT_ShutdownTask cont,
238 struct ShutdownContext *shutdown_ctx;
239 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
240 shutdown_ctx->cont = cont;
241 shutdown_ctx->cont_cls = cont_cls;
242 shutdown_ctx->sock = sock;
243 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout);
244 GNUNET_CLIENT_notify_transmit_ready (sock,
246 GNUNET_MessageHeader),
249 &write_shutdown, shutdown_ctx);
254 * Setup a context for communicating with ARM. Note that this
255 * can be done even if the ARM service is not yet running.
257 * @param cfg configuration to use (needed to contact ARM;
258 * the ARM service may internally use a different
259 * configuration to determine how to start the service).
260 * @param service service that *this* process is implementing/providing, can be NULL
261 * @return context to use for further ARM operations, NULL on error
263 struct GNUNET_ARM_Handle *
264 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
267 struct GNUNET_ARM_Handle *ret;
269 ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
270 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
276 * Disconnect from the ARM service.
278 * @param h the handle that was being used
281 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
283 if (h->client != NULL)
284 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
285 GNUNET_CONFIGURATION_destroy (h->cfg);
289 struct ARM_ShutdownContext
292 * Callback to call once shutdown complete.
294 GNUNET_ARM_Callback cb;
297 * Closure for callback.
305 * Internal state for a request with ARM.
307 struct RequestContext
311 * Pointer to our handle with ARM.
313 struct GNUNET_ARM_Handle *h;
316 * Function to call with a status code for the requested operation.
318 GNUNET_ARM_Callback callback;
321 * Closure for "callback".
326 * Timeout for the operation.
328 struct GNUNET_TIME_Absolute timeout;
331 * Type of the request expressed as a message type (start or stop).
337 #include "do_start_process.c"
341 * A client specifically requested starting of ARM itself.
342 * This function is called with information about whether
343 * or not ARM is running; if it is, report success. If
344 * it is not, start the ARM process.
346 * @param cls the context for the request that we will report on (struct RequestContext*)
347 * @param tc why were we called (reason says if ARM is running)
350 arm_service_report (void *cls,
351 const struct GNUNET_SCHEDULER_TaskContext *tc)
353 struct RequestContext *pos = cls;
354 struct GNUNET_OS_Process *proc;
360 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
364 "Looks like `%s' is already running.\n",
365 "gnunet-service-arm");
367 /* arm is running! */
368 if (pos->callback != NULL)
369 pos->callback (pos->cls, GNUNET_YES);
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
375 "Looks like `%s' is not running, will start it.\n",
376 "gnunet-service-arm");
378 /* FIXME: should we check that HOSTNAME for 'arm' is localhost? */
381 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
382 "arm", "PREFIX", &loprefix))
383 loprefix = GNUNET_strdup ("");
385 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
386 "arm", "OPTIONS", &lopostfix))
387 lopostfix = GNUNET_strdup ("");
389 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
394 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
395 _("Configuration failes to specify option `%s' in section `%s'!\n"),
398 if (pos->callback != NULL)
399 pos->callback (pos->cls, GNUNET_SYSERR);
401 GNUNET_free (loprefix);
402 GNUNET_free (lopostfix);
406 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg,
407 "arm", "CONFIG", &config))
409 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
410 _("Configuration fails to specify option `%s' in section `%s'!\n"),
413 if (pos->callback != NULL)
414 pos->callback (pos->cls, GNUNET_SYSERR);
415 GNUNET_free (binary);
417 GNUNET_free (loprefix);
418 GNUNET_free (lopostfix);
421 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (pos->h->cfg,
424 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg,
427 (GNUNET_NO == GNUNET_CONFIGURATION_have_value (pos->h->cfg,
429 "HOSTFILE")) /* Means we are ONLY running locally */
432 /* we're clearly running a test, don't daemonize */
433 proc = do_start_process (NULL,
440 /* no daemonization! */
446 proc = do_start_process (NULL,
457 GNUNET_free (binary);
458 GNUNET_free (config);
459 GNUNET_free (loprefix);
460 GNUNET_free (lopostfix);
463 if (pos->callback != NULL)
464 pos->callback (pos->cls, GNUNET_SYSERR);
468 if (pos->callback != NULL)
469 pos->callback (pos->cls, GNUNET_YES);
476 * Process a response from ARM to a request for a change in service
479 * @param cls the request context
480 * @param msg the response
483 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
485 struct RequestContext *sc = cls;
490 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
491 _("Error receiving response to `%s' request from ARM for service `%s'\n"),
492 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START)
495 (const char*) &sc[1]);
496 GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
497 sc->h->client = GNUNET_CLIENT_connect ("arm",
499 GNUNET_assert (NULL != sc->h->client);
500 GNUNET_CLIENT_ignore_shutdown (sc->h->client, GNUNET_YES);
501 if (sc->callback != NULL)
502 sc->callback (sc->cls, GNUNET_SYSERR);
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "Received response from ARM for service `%s': %u\n",
509 (const char*) &sc[1],
512 switch (ntohs (msg->type))
514 case GNUNET_MESSAGE_TYPE_ARM_IS_UP:
517 case GNUNET_MESSAGE_TYPE_ARM_IS_DOWN:
520 case GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN:
527 if (sc->callback != NULL)
528 sc->callback (sc->cls, ret);
534 * Start or stop a service.
536 * @param h handle to ARM
537 * @param service_name name of the service
538 * @param timeout how long to wait before failing for good
539 * @param cb callback to invoke when service is ready
540 * @param cb_cls closure for callback
541 * @param type type of the request
544 change_service (struct GNUNET_ARM_Handle *h,
545 const char *service_name,
546 struct GNUNET_TIME_Relative timeout,
547 GNUNET_ARM_Callback cb, void *cb_cls, uint16_t type)
549 struct RequestContext *sctx;
551 struct GNUNET_MessageHeader *msg;
553 slen = strlen (service_name) + 1;
554 if (slen + sizeof (struct GNUNET_MessageHeader) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
558 cb (cb_cls, GNUNET_NO);
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
563 (type == GNUNET_MESSAGE_TYPE_ARM_START)
564 ? _("Requesting start of service `%s'.\n")
565 : _("Requesting termination of service `%s'.\n"),
568 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
572 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
574 memcpy (&sctx[1], service_name, slen);
575 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
576 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
577 msg->type = htons (sctx->type);
578 memcpy (&msg[1], service_name, slen);
580 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
582 GNUNET_TIME_absolute_get_remaining (sctx->timeout),
587 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
588 (type == GNUNET_MESSAGE_TYPE_ARM_START)
589 ? _("Error while trying to transmit request to start `%s' to ARM\n")
590 : _("Error while trying to transmit request to stop `%s' to ARM\n"),
591 (const char*) &service_name);
593 cb (cb_cls, GNUNET_SYSERR);
605 * @param h handle to ARM
606 * @param service_name name of the service
607 * @param timeout how long to wait before failing for good
608 * @param cb callback to invoke when service is ready
609 * @param cb_cls closure for callback
612 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
613 const char *service_name,
614 struct GNUNET_TIME_Relative timeout,
615 GNUNET_ARM_Callback cb, void *cb_cls)
617 struct RequestContext *sctx;
618 struct GNUNET_CLIENT_Connection *client;
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
622 _("Asked to start service `%s' within %llu ms\n"), service_name,
623 (unsigned long long) timeout.rel_value);
625 if (0 == strcasecmp ("arm", service_name))
627 slen = strlen ("arm") + 1;
628 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
632 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
633 memcpy (&sctx[1], service_name, slen);
634 GNUNET_CLIENT_service_test ("arm",
635 h->cfg, timeout, &arm_service_report, sctx);
638 if (h->client == NULL)
640 client = GNUNET_CLIENT_connect ("arm", h->cfg);
643 cb (cb_cls, GNUNET_SYSERR);
646 GNUNET_CLIENT_ignore_shutdown (client, GNUNET_YES);
649 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_START);
653 * Callback from the arm stop service call, indicates that the arm service
654 * is well and truly dead, won't die, or an error occurred.
656 * @param cls closure for the callback
657 * @param reason reason for callback, GNUNET_NO if arm is shutdown
658 * GNUNET_YES if arm remains running, and GNUNET_SYSERR on error
660 void arm_shutdown_callback (void *cls,
663 struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
665 if (arm_shutdown_ctx->cb != NULL)
666 arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
668 GNUNET_free(arm_shutdown_ctx);
675 * @param h handle to ARM
676 * @param service_name name of the service
677 * @param timeout how long to wait before failing for good
678 * @param cb callback to invoke when service is ready
679 * @param cb_cls closure for callback
682 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
683 const char *service_name,
684 struct GNUNET_TIME_Relative timeout,
685 GNUNET_ARM_Callback cb, void *cb_cls)
687 struct ARM_ShutdownContext *arm_shutdown_ctx;
688 struct GNUNET_CLIENT_Connection *client;
690 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
691 _("Stopping service `%s' within %llu ms\n"), service_name,
692 (unsigned long long) timeout.rel_value);
693 if (h->client == NULL)
695 client = GNUNET_CLIENT_connect ("arm", h->cfg);
698 cb (cb_cls, GNUNET_SYSERR);
701 GNUNET_CLIENT_ignore_shutdown (client, GNUNET_YES);
704 if (0 == strcasecmp ("arm", service_name))
706 arm_shutdown_ctx = GNUNET_malloc(sizeof(struct ARM_ShutdownContext));
707 arm_shutdown_ctx->cb = cb;
708 arm_shutdown_ctx->cb_cls = cb_cls;
709 arm_service_shutdown (h->client, timeout, &arm_shutdown_callback, arm_shutdown_ctx);
713 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP);
717 /* end of arm_api.c */