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,
389 /* no daemonization! */
394 proc = do_start_process (GNUNET_NO,
395 NULL, loprefix, binary, "-c", config,
399 "-d", lopostfix, NULL);
401 GNUNET_free (binary);
402 GNUNET_free (config);
403 GNUNET_free (loprefix);
404 GNUNET_free (lopostfix);
407 if (pos->callback != NULL)
408 pos->callback (pos->cls, GNUNET_ARM_PROCESS_FAILURE);
412 if (pos->callback != NULL)
413 pos->callback (pos->cls, GNUNET_ARM_PROCESS_STARTING);
420 * Process a response from ARM to a request for a change in service
423 * @param cls the request context
424 * @param msg the response
427 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
429 struct RequestContext *sc = cls;
430 const struct GNUNET_ARM_ResultMessage *res;
431 enum GNUNET_ARM_ProcessStatus status;
434 (ntohs (msg->size) != sizeof (struct GNUNET_ARM_ResultMessage)))
436 LOG (GNUNET_ERROR_TYPE_WARNING,
438 ("Error receiving response to `%s' request from ARM for service `%s'\n"),
439 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START) ? "START" : "STOP",
440 (const char *) &sc[1]);
441 GNUNET_CLIENT_disconnect (sc->h->client);
442 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
443 GNUNET_assert (NULL != sc->h->client);
444 if (sc->callback != NULL)
445 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
449 res = (const struct GNUNET_ARM_ResultMessage *) msg;
450 LOG (GNUNET_ERROR_TYPE_DEBUG,
451 "Received response from ARM for service `%s': %u\n",
452 (const char *) &sc[1], ntohs (msg->type));
453 status = (enum GNUNET_ARM_ProcessStatus) ntohl (res->status);
454 if (sc->callback != NULL)
455 sc->callback (sc->cls, status);
461 * Start or stop a service.
463 * @param h handle to ARM
464 * @param service_name name of the service
465 * @param timeout how long to wait before failing for good
466 * @param cb callback to invoke when service is ready
467 * @param cb_cls closure for callback
468 * @param type type of the request
471 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
472 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_Callback cb,
473 void *cb_cls, uint16_t type)
475 struct RequestContext *sctx;
477 struct GNUNET_MessageHeader *msg;
479 slen = strlen (service_name) + 1;
480 if (slen + sizeof (struct GNUNET_MessageHeader) >=
481 GNUNET_SERVER_MAX_MESSAGE_SIZE)
485 cb (cb_cls, GNUNET_NO);
488 LOG (GNUNET_ERROR_TYPE_DEBUG,
490 GNUNET_MESSAGE_TYPE_ARM_START) ?
491 _("Requesting start of service `%s'.\n") :
492 _("Requesting termination of service `%s'.\n"), service_name);
493 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
497 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
499 memcpy (&sctx[1], service_name, slen);
500 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
501 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
502 msg->type = htons (sctx->type);
503 memcpy (&msg[1], service_name, slen);
505 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, msg,
506 GNUNET_TIME_absolute_get_remaining
507 (sctx->timeout), GNUNET_YES,
508 &handle_response, sctx))
510 LOG (GNUNET_ERROR_TYPE_WARNING,
512 GNUNET_MESSAGE_TYPE_ARM_START)
513 ? _("Error while trying to transmit request to start `%s' to ARM\n")
514 : _("Error while trying to transmit request to stop `%s' to ARM\n"),
515 (const char *) &service_name);
517 cb (cb_cls, GNUNET_SYSERR);
529 * @param h handle to ARM
530 * @param service_name name of the service
531 * @param timeout how long to wait before failing for good
532 * @param cb callback to invoke when service is ready
533 * @param cb_cls closure for callback
536 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
537 const char *service_name,
538 struct GNUNET_TIME_Relative timeout,
539 GNUNET_ARM_Callback cb, void *cb_cls)
541 struct RequestContext *sctx;
542 struct GNUNET_CLIENT_Connection *client;
545 LOG (GNUNET_ERROR_TYPE_DEBUG,
546 _("Asked to start service `%s' within %llu ms\n"), service_name,
547 (unsigned long long) timeout.rel_value);
548 if (0 == strcasecmp ("arm", service_name))
550 slen = strlen ("arm") + 1;
551 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
555 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
556 memcpy (&sctx[1], service_name, slen);
557 GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
561 if (h->client == NULL)
563 client = GNUNET_CLIENT_connect ("arm", h->cfg);
566 LOG (GNUNET_ERROR_TYPE_DEBUG,
567 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
568 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
571 LOG (GNUNET_ERROR_TYPE_DEBUG,
572 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "arm_api, h->client non-NULL\n");
576 change_service (h, service_name, timeout, cb, cb_cls,
577 GNUNET_MESSAGE_TYPE_ARM_START);
582 * Callback from the arm stop service call, indicates that the arm service
583 * is well and truly dead, won't die, or an error occurred.
585 * @param cls closure for the callback
586 * @param reason reason for callback
589 arm_shutdown_callback (void *cls, enum GNUNET_ARM_ProcessStatus reason)
591 struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
593 if (arm_shutdown_ctx->cb != NULL)
594 arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
596 GNUNET_free (arm_shutdown_ctx);
603 * @param h handle to ARM
604 * @param service_name name of the service
605 * @param timeout how long to wait before failing for good
606 * @param cb callback to invoke when service is ready
607 * @param cb_cls closure for callback
610 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
611 const char *service_name,
612 struct GNUNET_TIME_Relative timeout,
613 GNUNET_ARM_Callback cb, void *cb_cls)
615 struct ARM_ShutdownContext *arm_shutdown_ctx;
616 struct GNUNET_CLIENT_Connection *client;
618 LOG (GNUNET_ERROR_TYPE_INFO, _("Stopping service `%s' within %llu ms\n"),
619 service_name, (unsigned long long) timeout.rel_value);
620 if (h->client == NULL)
622 client = GNUNET_CLIENT_connect ("arm", h->cfg);
625 cb (cb_cls, GNUNET_SYSERR);
630 if (0 == strcasecmp ("arm", service_name))
632 arm_shutdown_ctx = GNUNET_malloc (sizeof (struct ARM_ShutdownContext));
633 arm_shutdown_ctx->cb = cb;
634 arm_shutdown_ctx->cb_cls = cb_cls;
635 arm_service_shutdown (h->client, timeout, &arm_shutdown_callback,
640 change_service (h, service_name, timeout, cb, cb_cls,
641 GNUNET_MESSAGE_TYPE_ARM_STOP);
646 * Internal state for a list request with ARM.
648 struct ListRequestContext
652 * Pointer to our handle with ARM.
654 struct GNUNET_ARM_Handle *h;
657 * Function to call with a status code for the requested operation.
659 GNUNET_ARM_List_Callback callback;
662 * Closure for "callback".
667 * Timeout for the operation.
669 struct GNUNET_TIME_Absolute timeout;
674 * Process a response from ARM for the list request.
676 * @param cls the list request context
677 * @param msg the response
680 handle_list_response (void *cls, const struct GNUNET_MessageHeader *msg)
682 struct ListRequestContext *sc = cls;
683 const struct GNUNET_ARM_ListResultMessage *res;
691 LOG (GNUNET_ERROR_TYPE_WARNING,
692 "Error receiving response to LIST request from ARM\n");
693 GNUNET_CLIENT_disconnect (sc->h->client);
694 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
695 GNUNET_assert (NULL != sc->h->client);
696 if (sc->callback != NULL)
697 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
702 if (NULL == sc->callback)
708 msize = ntohs (msg->size);
709 if ( (msize < sizeof ( struct GNUNET_ARM_ListResultMessage)) ||
710 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT) )
713 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
718 res = (const struct GNUNET_ARM_ListResultMessage *) msg;
719 rcount = ntohs (res->count);
721 const char *list[rcount];
724 pos = (const char *)&res[1];
725 for (i=0; i<rcount; i++)
727 const char *end = memchr (pos, 0, msize - size_check);
731 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
736 size_check += (end - pos) + 1;
739 sc->callback (sc->cls, GNUNET_YES, rcount, list);
746 * List all running services.
748 * @param h handle to ARM
749 * @param timeout how long to wait before failing for good
750 * @param cb callback to invoke when service is ready
751 * @param cb_cls closure for callback
754 GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
755 struct GNUNET_TIME_Relative timeout,
756 GNUNET_ARM_List_Callback cb, void *cb_cls)
758 struct ListRequestContext *sctx;
759 struct GNUNET_MessageHeader msg;
760 struct GNUNET_CLIENT_Connection *client;
762 if (h->client == NULL)
764 client = GNUNET_CLIENT_connect ("arm", h->cfg);
767 LOG (GNUNET_ERROR_TYPE_DEBUG,
768 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
769 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
772 LOG (GNUNET_ERROR_TYPE_DEBUG,
773 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
777 sctx = GNUNET_malloc (sizeof (struct RequestContext));
781 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
782 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
783 msg.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
785 LOG (GNUNET_ERROR_TYPE_DEBUG,
786 "Requesting LIST from ARM service with timeout: %llu ms\n",
787 (unsigned long long)timeout.rel_value);
790 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
792 GNUNET_TIME_absolute_get_remaining
795 &handle_list_response,
798 LOG (GNUNET_ERROR_TYPE_WARNING,
799 "Error while trying to transmit request to list services to ARM\n");
801 cb (cb_cls, GNUNET_SYSERR, 0, NULL);
807 /* end of arm_api.c */