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 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 * 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;
55 struct GNUNET_SCHEDULER_Handle *sched;
61 * Context for handling the shutdown of a service.
63 struct ShutdownContext
66 * Scheduler to be used to call continuation
68 struct GNUNET_SCHEDULER_Handle *sched;
70 * Connection to the service that is being shutdown.
72 struct GNUNET_CLIENT_Connection *sock;
75 * Time allowed for shutdown to happen.
77 struct GNUNET_TIME_Absolute timeout;
80 * Task set up to cancel the shutdown request on timeout.
82 GNUNET_SCHEDULER_TaskIdentifier cancel_task;
85 * Task to call once shutdown complete
87 GNUNET_CLIENT_ShutdownTask cont;
90 * Closure for shutdown continuation
95 * We received a confirmation that the service will shut down.
102 * Handler receiving response to service shutdown requests.
103 * First call with NULL: service misbehaving, or something.
104 * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
105 * - service will shutdown
106 * Second call with NULL:
107 * - service has now really shut down.
110 * @param msg NULL, indicating socket closure.
113 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
115 struct ShutdownContext *shutdown_ctx = cls;
117 if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))
119 /* Means the other side closed the connection and never confirmed a shutdown */
120 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
121 "Service handle shutdown before ACK!\n");
122 if (shutdown_ctx->cont != NULL)
123 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
124 GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
125 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
126 GNUNET_free(shutdown_ctx);
128 else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES))
131 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
132 "Service shutdown complete.\n");
134 if (shutdown_ctx->cont != NULL)
135 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_NO);
137 GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
138 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
139 GNUNET_free(shutdown_ctx);
143 GNUNET_assert(ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
144 switch (ntohs(msg->type))
146 case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
148 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
149 "Received confirmation for service shutdown.\n");
151 shutdown_ctx->confirmed = GNUNET_YES;
152 GNUNET_CLIENT_receive (shutdown_ctx->sock,
153 &service_shutdown_handler,
155 GNUNET_TIME_UNIT_FOREVER_REL);
157 default: /* Fall through */
159 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
160 "Service shutdown refused!\n");
162 if (shutdown_ctx->cont != NULL)
163 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_YES);
165 GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
166 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
167 GNUNET_free(shutdown_ctx);
174 * Shutting down took too long, cancel receive and return error.
177 * @param tc context information (why was this task triggered now)
179 void service_shutdown_cancel (void *cls,
180 const struct GNUNET_SCHEDULER_TaskContext * tc)
182 struct ShutdownContext *shutdown_ctx = cls;
183 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
184 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
185 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
186 GNUNET_free(shutdown_ctx);
191 * If possible, write a shutdown message to the target
192 * buffer and destroy the client connection.
194 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
195 * @param size number of bytes available in buf
196 * @param buf NULL on error, otherwise target buffer
197 * @return number of bytes written to buf
200 write_shutdown (void *cls, size_t size, void *buf)
202 struct GNUNET_MessageHeader *msg;
203 struct ShutdownContext *shutdown_ctx = cls;
205 if (size < sizeof (struct GNUNET_MessageHeader))
207 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
208 _("Failed to transmit shutdown request to client.\n"));
209 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
210 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
211 GNUNET_free(shutdown_ctx);
212 return 0; /* client disconnected */
215 GNUNET_CLIENT_receive (shutdown_ctx->sock,
216 &service_shutdown_handler, shutdown_ctx,
217 GNUNET_TIME_UNIT_FOREVER_REL);
218 shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (shutdown_ctx->sched,
219 GNUNET_TIME_absolute_get_remaining(shutdown_ctx->timeout),
220 &service_shutdown_cancel,
222 msg = (struct GNUNET_MessageHeader *) buf;
223 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
224 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
225 return sizeof (struct GNUNET_MessageHeader);
230 * Request that the service should shutdown.
231 * Afterwards, the connection will automatically be
232 * disconnected. Hence the "sock" should not
233 * be used by the caller after this call
234 * (calling this function frees "sock" after a while).
236 * @param sched the scheduler to use for calling shutdown continuation
237 * @param sock the socket connected to the service
238 * @param timeout how long to wait before giving up on transmission
239 * @param cont continuation to call once the service is really down
240 * @param cont_cls closure for continuation
244 arm_service_shutdown (struct GNUNET_SCHEDULER_Handle *sched,
245 struct GNUNET_CLIENT_Connection *sock,
246 struct GNUNET_TIME_Relative timeout,
247 GNUNET_CLIENT_ShutdownTask cont,
250 struct ShutdownContext *shutdown_ctx;
251 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
252 shutdown_ctx->sched = sched;
253 shutdown_ctx->cont = cont;
254 shutdown_ctx->cont_cls = cont_cls;
255 shutdown_ctx->sock = sock;
256 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout);
257 GNUNET_CLIENT_notify_transmit_ready (sock,
259 GNUNET_MessageHeader),
262 &write_shutdown, shutdown_ctx);
267 * Setup a context for communicating with ARM. Note that this
268 * can be done even if the ARM service is not yet running.
270 * @param cfg configuration to use (needed to contact ARM;
271 * the ARM service may internally use a different
272 * configuration to determine how to start the service).
273 * @param sched scheduler to use
274 * @param service service that *this* process is implementing/providing, can be NULL
275 * @return context to use for further ARM operations, NULL on error
277 struct GNUNET_ARM_Handle *
278 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
279 struct GNUNET_SCHEDULER_Handle *sched,
282 struct GNUNET_ARM_Handle *ret;
284 ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
285 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
292 * Disconnect from the ARM service.
294 * @param h the handle that was being used
297 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
299 if (h->client != NULL)
300 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
301 GNUNET_CONFIGURATION_destroy (h->cfg);
305 struct ARM_ShutdownContext
308 * Callback to call once shutdown complete.
310 GNUNET_ARM_Callback cb;
313 * Closure for callback.
321 * Internal state for a request with ARM.
323 struct RequestContext
327 * Pointer to our handle with ARM.
329 struct GNUNET_ARM_Handle *h;
332 * Function to call with a status code for the requested operation.
334 GNUNET_ARM_Callback callback;
337 * Closure for "callback".
342 * Timeout for the operation.
344 struct GNUNET_TIME_Absolute timeout;
347 * Type of the request expressed as a message type (start or stop).
353 #include "do_start_process.c"
357 * A client specifically requested starting of ARM itself.
358 * This function is called with information about whether
359 * or not ARM is running; if it is, report success. If
360 * it is not, start the ARM process.
362 * @param cls the context for the request that we will report on (struct RequestContext*)
363 * @param tc why were we called (reason says if ARM is running)
366 arm_service_report (void *cls,
367 const struct GNUNET_SCHEDULER_TaskContext *tc)
369 struct RequestContext *pos = cls;
376 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "Looks like `%s' is already running.\n",
381 "gnunet-service-arm");
383 /* arm is running! */
384 if (pos->callback != NULL)
385 pos->callback (pos->cls, GNUNET_YES);
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Looks like `%s' is not running, will start it.\n",
392 "gnunet-service-arm");
394 /* FIXME: should we check that HOSTNAME for 'arm' is localhost? */
397 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
398 "arm", "PREFIX", &loprefix))
399 loprefix = GNUNET_strdup ("");
401 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
402 "arm", "OPTIONS", &lopostfix))
403 lopostfix = GNUNET_strdup ("");
405 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
410 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
411 _("Configuration failes to specify option `%s' in section `%s'!\n"),
414 if (pos->callback != NULL)
415 pos->callback (pos->cls, GNUNET_SYSERR);
417 GNUNET_free (loprefix);
418 GNUNET_free (lopostfix);
422 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg,
423 "arm", "CONFIG", &config))
425 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
426 _("Configuration fails to specify option `%s' in section `%s'!\n"),
429 if (pos->callback != NULL)
430 pos->callback (pos->cls, GNUNET_SYSERR);
431 GNUNET_free (binary);
433 GNUNET_free (loprefix);
434 GNUNET_free (lopostfix);
437 pid = do_start_process (NULL,
447 GNUNET_free (binary);
448 GNUNET_free (config);
449 GNUNET_free (loprefix);
450 GNUNET_free (lopostfix);
453 if (pos->callback != NULL)
454 pos->callback (pos->cls, GNUNET_SYSERR);
458 if (pos->callback != NULL)
459 pos->callback (pos->cls, GNUNET_YES);
465 * Process a response from ARM to a request for a change in service
468 * @param cls the request context
469 * @param msg the response
472 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
474 struct RequestContext *sc = cls;
479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
480 _("Error receiving response to `%s' request from ARM for service `%s'\n"),
481 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START)
484 (const char*) &sc[1]);
485 GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
486 sc->h->client = GNUNET_CLIENT_connect (sc->h->sched,
489 GNUNET_assert (NULL != sc->h->client);
490 GNUNET_CLIENT_ignore_shutdown (sc->h->client, GNUNET_YES);
491 if (sc->callback != NULL)
492 sc->callback (sc->cls, GNUNET_SYSERR);
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Received response from ARM for service `%s': %u\n",
499 (const char*) &sc[1],
502 switch (ntohs (msg->type))
504 case GNUNET_MESSAGE_TYPE_ARM_IS_UP:
507 case GNUNET_MESSAGE_TYPE_ARM_IS_DOWN:
510 case GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN:
517 if (sc->callback != NULL)
518 sc->callback (sc->cls, ret);
524 * Start or stop a service.
526 * @param h handle to ARM
527 * @param service_name name of the service
528 * @param timeout how long to wait before failing for good
529 * @param cb callback to invoke when service is ready
530 * @param cb_cls closure for callback
531 * @param type type of the request
534 change_service (struct GNUNET_ARM_Handle *h,
535 const char *service_name,
536 struct GNUNET_TIME_Relative timeout,
537 GNUNET_ARM_Callback cb, void *cb_cls, uint16_t type)
539 struct RequestContext *sctx;
541 struct GNUNET_MessageHeader *msg;
543 slen = strlen (service_name) + 1;
544 if (slen + sizeof (struct GNUNET_MessageHeader) >
545 GNUNET_SERVER_MAX_MESSAGE_SIZE)
549 cb (cb_cls, GNUNET_NO);
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554 (type == GNUNET_MESSAGE_TYPE_ARM_START)
555 ? _("Requesting start of service `%s'.\n")
556 : _("Requesting termination of service `%s'.\n"),
559 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
563 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
565 memcpy (&sctx[1], service_name, slen);
566 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
567 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
568 msg->type = htons (sctx->type);
569 memcpy (&msg[1], service_name, slen);
571 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
573 GNUNET_TIME_absolute_get_remaining (sctx->timeout),
578 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
579 (type == GNUNET_MESSAGE_TYPE_ARM_START)
580 ? _("Error while trying to transmit request to start `%s' to ARM\n")
581 : _("Error while trying to transmit request to stop `%s' to ARM\n"),
582 (const char*) &service_name);
584 cb (cb_cls, GNUNET_SYSERR);
596 * @param h handle to ARM
597 * @param service_name name of the service
598 * @param timeout how long to wait before failing for good
599 * @param cb callback to invoke when service is ready
600 * @param cb_cls closure for callback
603 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
604 const char *service_name,
605 struct GNUNET_TIME_Relative timeout,
606 GNUNET_ARM_Callback cb, void *cb_cls)
608 struct RequestContext *sctx;
609 struct GNUNET_CLIENT_Connection *client;
612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
613 _("Asked to start service `%s' within %llu ms\n"), service_name,
614 (unsigned long long) timeout.value);
616 if (0 == strcasecmp ("arm", service_name))
618 slen = strlen ("arm") + 1;
619 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
623 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
624 memcpy (&sctx[1], service_name, slen);
625 GNUNET_CLIENT_service_test (h->sched,
627 h->cfg, timeout, &arm_service_report, sctx);
630 if (h->client == NULL)
632 client = GNUNET_CLIENT_connect (h->sched, "arm", h->cfg);
635 cb (cb_cls, GNUNET_SYSERR);
638 GNUNET_CLIENT_ignore_shutdown (client, GNUNET_YES);
641 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_START);
645 * Callback from the arm stop service call, indicates that the arm service
646 * is well and truly dead, won't die, or an error occurred.
648 * @param cls closure for the callback
649 * @param reason reason for callback, GNUNET_NO if arm is shutdown
650 * GNUNET_YES if arm remains running, and GNUNET_SYSERR on error
652 void arm_shutdown_callback (void *cls,
655 struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
657 if (arm_shutdown_ctx->cb != NULL)
658 arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
660 GNUNET_free(arm_shutdown_ctx);
667 * @param h handle to ARM
668 * @param service_name name of the service
669 * @param timeout how long to wait before failing for good
670 * @param cb callback to invoke when service is ready
671 * @param cb_cls closure for callback
674 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
675 const char *service_name,
676 struct GNUNET_TIME_Relative timeout,
677 GNUNET_ARM_Callback cb, void *cb_cls)
679 struct ARM_ShutdownContext *arm_shutdown_ctx;
680 struct GNUNET_CLIENT_Connection *client;
682 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
683 _("Stopping service `%s' within %llu ms\n"), service_name,
684 (unsigned long long) timeout.value);
685 if (h->client == NULL)
687 client = GNUNET_CLIENT_connect (h->sched, "arm", h->cfg);
690 cb (cb_cls, GNUNET_SYSERR);
693 GNUNET_CLIENT_ignore_shutdown (client, GNUNET_YES);
696 if (0 == strcasecmp ("arm", service_name))
698 arm_shutdown_ctx = GNUNET_malloc(sizeof(struct ARM_ShutdownContext));
699 arm_shutdown_ctx->cb = cb;
700 arm_shutdown_ctx->cb_cls = cb_cls;
701 arm_service_shutdown (h->sched, h->client, timeout, &arm_shutdown_callback, arm_shutdown_ctx);
705 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP);
709 /* end of arm_api.c */