2 This file is part of GNUnet.
3 (C) 2009, 2010, 2012 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"
35 #define LOG(kind,...) GNUNET_log_from (kind, "arm-api",__VA_ARGS__)
38 * Handle for interacting with ARM.
40 struct GNUNET_ARM_Handle
44 * Our connection to the ARM service.
46 struct GNUNET_CLIENT_Connection *client;
49 * The configuration that we are using.
51 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 * Handle for transmission request.
88 struct GNUNET_CLIENT_TransmitHandle *th;
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.
102 * @param msg NULL, indicating socket closure.
105 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
107 struct ShutdownContext *shutdown_ctx = cls;
111 /* We just expected a disconnect! Report the error and be done with it... */
113 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
114 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
115 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
116 GNUNET_free (shutdown_ctx);
119 if (NULL != shutdown_ctx->cont)
120 /* shutdown is now complete, as we waited for the network disconnect... */
121 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_DOWN);
122 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
123 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
124 GNUNET_free (shutdown_ctx);
129 * Shutting down took too long, cancel receive and return error.
132 * @param tc context information (why was this task triggered now)
135 service_shutdown_cancel (void *cls,
136 const struct GNUNET_SCHEDULER_TaskContext *tc)
138 struct ShutdownContext *shutdown_ctx = cls;
140 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT);
141 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
142 GNUNET_free (shutdown_ctx);
147 * If possible, write a shutdown message to the target
148 * buffer and destroy the client connection.
150 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
151 * @param size number of bytes available in buf
152 * @param buf NULL on error, otherwise target buffer
153 * @return number of bytes written to buf
156 write_shutdown (void *cls, size_t size, void *buf)
158 struct ShutdownContext *shutdown_ctx = cls;
159 struct GNUNET_MessageHeader *msg;
161 shutdown_ctx->th = NULL;
162 if (size < sizeof (struct GNUNET_MessageHeader))
164 LOG (GNUNET_ERROR_TYPE_WARNING,
165 _("Failed to transmit shutdown request to client.\n"));
166 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
167 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
168 GNUNET_free (shutdown_ctx);
169 return 0; /* client disconnected */
171 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
172 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
173 shutdown_ctx->cancel_task =
174 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
175 (shutdown_ctx->timeout),
176 &service_shutdown_cancel, shutdown_ctx);
177 msg = (struct GNUNET_MessageHeader *) buf;
178 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
179 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
180 return sizeof (struct GNUNET_MessageHeader);
185 * Request that the service should shutdown.
186 * Afterwards, the connection will automatically be
187 * disconnected. Hence the "sock" should not
188 * be used by the caller after this call
189 * (calling this function frees "sock" after a while).
191 * @param sock the socket connected to the service
192 * @param timeout how long to wait before giving up on transmission
193 * @param cont continuation to call once the service is really down
194 * @param cont_cls closure for continuation
198 arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
199 struct GNUNET_TIME_Relative timeout,
200 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
202 struct ShutdownContext *shutdown_ctx;
204 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
205 shutdown_ctx->cont = cont;
206 shutdown_ctx->cont_cls = cont_cls;
207 shutdown_ctx->sock = sock;
208 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
209 shutdown_ctx->th = GNUNET_CLIENT_notify_transmit_ready (sock,
210 sizeof (struct GNUNET_MessageHeader),
211 timeout, GNUNET_NO, &write_shutdown,
217 * Setup a context for communicating with ARM. Note that this
218 * can be done even if the ARM service is not yet running.
220 * @param cfg configuration to use (needed to contact ARM;
221 * the ARM service may internally use a different
222 * configuration to determine how to start the service).
223 * @param service service that *this* process is implementing/providing, can be NULL
224 * @return context to use for further ARM operations, NULL on error
226 struct GNUNET_ARM_Handle *
227 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
230 struct GNUNET_ARM_Handle *ret;
232 ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
233 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
239 * Disconnect from the ARM service.
241 * @param h the handle that was being used
244 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
246 if (h->client != NULL)
247 GNUNET_CLIENT_disconnect (h->client);
248 GNUNET_CONFIGURATION_destroy (h->cfg);
253 struct ARM_ShutdownContext
256 * Callback to call once shutdown complete.
258 GNUNET_ARM_Callback cb;
261 * Closure for callback.
268 * Internal state for a request with ARM.
270 struct RequestContext
274 * Pointer to our handle with ARM.
276 struct GNUNET_ARM_Handle *h;
279 * Function to call with a status code for the requested operation.
281 GNUNET_ARM_Callback callback;
284 * Closure for "callback".
289 * Timeout for the operation.
291 struct GNUNET_TIME_Absolute timeout;
294 * Type of the request expressed as a message type (start or stop).
300 #include "do_start_process.c"
304 * A client specifically requested starting of ARM itself.
305 * This function is called with information about whether
306 * or not ARM is running; if it is, report success. If
307 * it is not, start the ARM process.
309 * @param cls the context for the request that we will report on (struct RequestContext*)
310 * @param tc why were we called (reason says if ARM is running)
313 arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
315 struct RequestContext *pos = cls;
316 struct GNUNET_OS_Process *proc;
322 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
324 LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
325 "gnunet-service-arm");
326 /* arm is running! */
327 if (pos->callback != NULL)
328 pos->callback (pos->cls, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
332 LOG (GNUNET_ERROR_TYPE_DEBUG,
333 "Looks like `%s' is not running, will start it.\n",
334 "gnunet-service-arm");
336 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "PREFIX",
338 loprefix = GNUNET_strdup ("");
340 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "OPTIONS",
342 lopostfix = GNUNET_strdup ("");
344 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "BINARY",
347 LOG (GNUNET_ERROR_TYPE_WARNING,
349 ("Configuration failes to specify option `%s' in section `%s'!\n"),
351 if (pos->callback != NULL)
352 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
354 GNUNET_free (loprefix);
355 GNUNET_free (lopostfix);
359 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg, "arm", "CONFIG",
362 LOG (GNUNET_ERROR_TYPE_WARNING,
363 _("Configuration fails to specify option `%s' in section `%s'!\n"),
365 if (pos->callback != NULL)
366 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
367 GNUNET_free (binary);
369 GNUNET_free (loprefix);
370 GNUNET_free (lopostfix);
374 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING", "WEAKRANDOM"))
376 GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg, "TESTING",
379 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING",
382 /* Means we are ONLY running locally */
383 /* we're clearly running a test, don't daemonize */
384 proc = do_start_process (GNUNET_NO,
385 NULL, loprefix, binary, "-c", config,
386 /* no daemonization! */
391 proc = do_start_process (GNUNET_NO,
392 NULL, loprefix, binary, "-c", config,
393 "-d", lopostfix, NULL);
395 GNUNET_free (binary);
396 GNUNET_free (config);
397 GNUNET_free (loprefix);
398 GNUNET_free (lopostfix);
401 if (pos->callback != NULL)
402 pos->callback (pos->cls, GNUNET_ARM_PROCESS_FAILURE);
406 if (pos->callback != NULL)
407 pos->callback (pos->cls, GNUNET_ARM_PROCESS_STARTING);
414 * Process a response from ARM to a request for a change in service
417 * @param cls the request context
418 * @param msg the response
421 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
423 struct RequestContext *sc = cls;
424 const struct GNUNET_ARM_ResultMessage *res;
425 enum GNUNET_ARM_ProcessStatus status;
428 (ntohs (msg->size) != sizeof (struct GNUNET_ARM_ResultMessage)))
430 LOG (GNUNET_ERROR_TYPE_WARNING,
432 ("Error receiving response to `%s' request from ARM for service `%s'\n"),
433 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START) ? "START" : "STOP",
434 (const char *) &sc[1]);
435 GNUNET_CLIENT_disconnect (sc->h->client);
436 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
437 GNUNET_assert (NULL != sc->h->client);
438 if (sc->callback != NULL)
439 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
443 res = (const struct GNUNET_ARM_ResultMessage *) msg;
444 LOG (GNUNET_ERROR_TYPE_DEBUG,
445 "Received response from ARM for service `%s': %u\n",
446 (const char *) &sc[1], ntohs (msg->type));
447 status = (enum GNUNET_ARM_ProcessStatus) ntohl (res->status);
448 if (sc->callback != NULL)
449 sc->callback (sc->cls, status);
455 * Start or stop a service.
457 * @param h handle to ARM
458 * @param service_name name of the service
459 * @param timeout how long to wait before failing for good
460 * @param cb callback to invoke when service is ready
461 * @param cb_cls closure for callback
462 * @param type type of the request
465 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
466 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_Callback cb,
467 void *cb_cls, uint16_t type)
469 struct RequestContext *sctx;
471 struct GNUNET_MessageHeader *msg;
473 slen = strlen (service_name) + 1;
474 if (slen + sizeof (struct GNUNET_MessageHeader) >=
475 GNUNET_SERVER_MAX_MESSAGE_SIZE)
479 cb (cb_cls, GNUNET_NO);
482 LOG (GNUNET_ERROR_TYPE_DEBUG,
484 GNUNET_MESSAGE_TYPE_ARM_START) ?
485 _("Requesting start of service `%s'.\n") :
486 _("Requesting termination of service `%s'.\n"), service_name);
487 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
491 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
493 memcpy (&sctx[1], service_name, slen);
494 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
495 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
496 msg->type = htons (sctx->type);
497 memcpy (&msg[1], service_name, slen);
499 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, msg,
500 GNUNET_TIME_absolute_get_remaining
501 (sctx->timeout), GNUNET_YES,
502 &handle_response, sctx))
504 LOG (GNUNET_ERROR_TYPE_WARNING,
506 GNUNET_MESSAGE_TYPE_ARM_START)
507 ? _("Error while trying to transmit request to start `%s' to ARM\n")
508 : _("Error while trying to transmit request to stop `%s' to ARM\n"),
509 (const char *) &service_name);
511 cb (cb_cls, GNUNET_SYSERR);
523 * @param h handle to ARM
524 * @param service_name name of the service
525 * @param timeout how long to wait before failing for good
526 * @param cb callback to invoke when service is ready
527 * @param cb_cls closure for callback
530 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
531 const char *service_name,
532 struct GNUNET_TIME_Relative timeout,
533 GNUNET_ARM_Callback cb, void *cb_cls)
535 struct RequestContext *sctx;
536 struct GNUNET_CLIENT_Connection *client;
539 LOG (GNUNET_ERROR_TYPE_DEBUG,
540 _("Asked to start service `%s' within %llu ms\n"), service_name,
541 (unsigned long long) timeout.rel_value);
542 if (0 == strcasecmp ("arm", service_name))
544 slen = strlen ("arm") + 1;
545 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
549 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
550 memcpy (&sctx[1], service_name, slen);
551 GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
555 if (h->client == NULL)
557 client = GNUNET_CLIENT_connect ("arm", h->cfg);
560 LOG (GNUNET_ERROR_TYPE_DEBUG,
561 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
562 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
566 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "arm_api, h->client non-NULL\n");
570 change_service (h, service_name, timeout, cb, cb_cls,
571 GNUNET_MESSAGE_TYPE_ARM_START);
576 * Callback from the arm stop service call, indicates that the arm service
577 * is well and truly dead, won't die, or an error occurred.
579 * @param cls closure for the callback
580 * @param reason reason for callback
583 arm_shutdown_callback (void *cls, enum GNUNET_ARM_ProcessStatus reason)
585 struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
587 if (arm_shutdown_ctx->cb != NULL)
588 arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
590 GNUNET_free (arm_shutdown_ctx);
597 * @param h handle to ARM
598 * @param service_name name of the service
599 * @param timeout how long to wait before failing for good
600 * @param cb callback to invoke when service is ready
601 * @param cb_cls closure for callback
604 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
605 const char *service_name,
606 struct GNUNET_TIME_Relative timeout,
607 GNUNET_ARM_Callback cb, void *cb_cls)
609 struct ARM_ShutdownContext *arm_shutdown_ctx;
610 struct GNUNET_CLIENT_Connection *client;
612 LOG (GNUNET_ERROR_TYPE_INFO, _("Stopping service `%s' within %llu ms\n"),
613 service_name, (unsigned long long) timeout.rel_value);
614 if (h->client == NULL)
616 client = GNUNET_CLIENT_connect ("arm", h->cfg);
619 cb (cb_cls, GNUNET_SYSERR);
624 if (0 == strcasecmp ("arm", service_name))
626 arm_shutdown_ctx = GNUNET_malloc (sizeof (struct ARM_ShutdownContext));
627 arm_shutdown_ctx->cb = cb;
628 arm_shutdown_ctx->cb_cls = cb_cls;
629 arm_service_shutdown (h->client, timeout, &arm_shutdown_callback,
634 change_service (h, service_name, timeout, cb, cb_cls,
635 GNUNET_MESSAGE_TYPE_ARM_STOP);
640 * Internal state for a list request with ARM.
642 struct ListRequestContext
646 * Pointer to our handle with ARM.
648 struct GNUNET_ARM_Handle *h;
651 * Function to call with a status code for the requested operation.
653 GNUNET_ARM_List_Callback callback;
656 * Closure for "callback".
661 * Timeout for the operation.
663 struct GNUNET_TIME_Absolute timeout;
668 * Process a response from ARM for the list request.
670 * @param cls the list request context
671 * @param msg the response
674 handle_list_response (void *cls, const struct GNUNET_MessageHeader *msg)
676 struct ListRequestContext *sc = cls;
677 const struct GNUNET_ARM_ListResultMessage *res;
685 LOG (GNUNET_ERROR_TYPE_WARNING,
686 "Error receiving response to LIST request from ARM\n");
687 GNUNET_CLIENT_disconnect (sc->h->client);
688 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
689 GNUNET_assert (NULL != sc->h->client);
690 if (sc->callback != NULL)
691 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
696 if (NULL == sc->callback)
702 msize = ntohs (msg->size);
703 if ( (msize < sizeof ( struct GNUNET_ARM_ListResultMessage)) ||
704 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT) )
707 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
712 res = (const struct GNUNET_ARM_ListResultMessage *) msg;
713 rcount = ntohs (res->count);
715 const char *list[rcount];
718 pos = (const char *)&res[1];
719 for (i=0; i<rcount; i++)
721 const char *end = memchr (pos, 0, msize - size_check);
725 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
730 size_check += (end - pos) + 1;
733 sc->callback (sc->cls, GNUNET_YES, rcount, list);
740 * List all running services.
742 * @param h handle to ARM
743 * @param timeout how long to wait before failing for good
744 * @param cb callback to invoke when service is ready
745 * @param cb_cls closure for callback
748 GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
749 struct GNUNET_TIME_Relative timeout,
750 GNUNET_ARM_List_Callback cb, void *cb_cls)
752 struct ListRequestContext *sctx;
753 struct GNUNET_MessageHeader msg;
754 struct GNUNET_CLIENT_Connection *client;
756 if (h->client == NULL)
758 client = GNUNET_CLIENT_connect ("arm", h->cfg);
761 LOG (GNUNET_ERROR_TYPE_DEBUG,
762 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
763 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
766 LOG (GNUNET_ERROR_TYPE_DEBUG,
767 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
771 sctx = GNUNET_malloc (sizeof (struct RequestContext));
775 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
776 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
777 msg.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
779 LOG (GNUNET_ERROR_TYPE_DEBUG,
780 "Requesting LIST from ARM service with timeout: %llu ms\n",
781 (unsigned long long)timeout.rel_value);
784 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
786 GNUNET_TIME_absolute_get_remaining
789 &handle_list_response,
792 LOG (GNUNET_ERROR_TYPE_WARNING,
793 "Error while trying to transmit request to list services to ARM\n");
795 cb (cb_cls, GNUNET_SYSERR, 0, NULL);
801 /* end of arm_api.c */