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;
91 * Result of the operation
93 enum GNUNET_ARM_ProcessStatus confirmed;
99 * Handler receiving response to service shutdown requests.
100 * First call with NULL: service misbehaving, or something.
101 * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
102 * - service will shutdown
103 * Second call with NULL:
104 * - service has now really shut down.
107 * @param msg NULL, indicating socket closure.
110 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
112 struct ShutdownContext *shutdown_ctx = cls;
113 const struct GNUNET_ARM_ResultMessage *rmsg;
117 if (shutdown_ctx->cont != NULL)
119 if (shutdown_ctx->confirmed == GNUNET_ARM_PROCESS_SHUTDOWN)
121 /* shutdown is now complete, as we waited for the network disconnect... */
122 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_DOWN);
126 /* communication error */
127 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
130 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
131 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
132 GNUNET_free (shutdown_ctx);
135 if (ntohs (msg->size) ==
136 sizeof (struct GNUNET_ARM_ResultMessage))
138 rmsg = (const struct GNUNET_ARM_ResultMessage*) msg;
139 shutdown_ctx->confirmed = (enum GNUNET_ARM_ProcessStatus) ntohl (rmsg->status);
140 if (shutdown_ctx->confirmed != GNUNET_ARM_PROCESS_SHUTDOWN)
142 /* ARM is not shutting down, well, report the error and be done with it... */
143 shutdown_ctx->cont (shutdown_ctx->cont_cls, shutdown_ctx->confirmed);
144 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
145 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
146 GNUNET_free (shutdown_ctx);
150 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
151 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
156 * Shutting down took too long, cancel receive and return error.
159 * @param tc context information (why was this task triggered now)
162 service_shutdown_cancel (void *cls,
163 const struct GNUNET_SCHEDULER_TaskContext *tc)
165 struct ShutdownContext *shutdown_ctx = cls;
167 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT);
168 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
169 GNUNET_free (shutdown_ctx);
174 * If possible, write a shutdown message to the target
175 * buffer and destroy the client connection.
177 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
178 * @param size number of bytes available in buf
179 * @param buf NULL on error, otherwise target buffer
180 * @return number of bytes written to buf
183 write_shutdown (void *cls, size_t size, void *buf)
185 struct ShutdownContext *shutdown_ctx = cls;
186 struct GNUNET_MessageHeader *msg;
188 shutdown_ctx->th = NULL;
189 if (size < sizeof (struct GNUNET_MessageHeader))
191 LOG (GNUNET_ERROR_TYPE_WARNING,
192 _("Failed to transmit shutdown request to client.\n"));
193 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
194 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
195 GNUNET_free (shutdown_ctx);
196 return 0; /* client disconnected */
199 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
200 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
201 shutdown_ctx->cancel_task =
202 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
203 (shutdown_ctx->timeout),
204 &service_shutdown_cancel, shutdown_ctx);
205 msg = (struct GNUNET_MessageHeader *) buf;
206 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
207 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
208 return sizeof (struct GNUNET_MessageHeader);
213 * Request that the service should shutdown.
214 * Afterwards, the connection will automatically be
215 * disconnected. Hence the "sock" should not
216 * be used by the caller after this call
217 * (calling this function frees "sock" after a while).
219 * @param sock the socket connected to the service
220 * @param timeout how long to wait before giving up on transmission
221 * @param cont continuation to call once the service is really down
222 * @param cont_cls closure for continuation
226 arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
227 struct GNUNET_TIME_Relative timeout,
228 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
230 struct ShutdownContext *shutdown_ctx;
232 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
233 shutdown_ctx->cont = cont;
234 shutdown_ctx->cont_cls = cont_cls;
235 shutdown_ctx->sock = sock;
236 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
237 shutdown_ctx->confirmed = GNUNET_ARM_PROCESS_COMMUNICATION_ERROR;
238 shutdown_ctx->th = GNUNET_CLIENT_notify_transmit_ready (sock,
239 sizeof (struct GNUNET_MessageHeader),
240 timeout, GNUNET_NO, &write_shutdown,
246 * Setup a context for communicating with ARM. Note that this
247 * can be done even if the ARM service is not yet running.
249 * @param cfg configuration to use (needed to contact ARM;
250 * the ARM service may internally use a different
251 * configuration to determine how to start the service).
252 * @param service service that *this* process is implementing/providing, can be NULL
253 * @return context to use for further ARM operations, NULL on error
255 struct GNUNET_ARM_Handle *
256 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
259 struct GNUNET_ARM_Handle *ret;
261 ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
262 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
268 * Disconnect from the ARM service.
270 * @param h the handle that was being used
273 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
275 if (h->client != NULL)
276 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
277 GNUNET_CONFIGURATION_destroy (h->cfg);
282 struct ARM_ShutdownContext
285 * Callback to call once shutdown complete.
287 GNUNET_ARM_Callback cb;
290 * Closure for callback.
297 * Internal state for a request with ARM.
299 struct RequestContext
303 * Pointer to our handle with ARM.
305 struct GNUNET_ARM_Handle *h;
308 * Function to call with a status code for the requested operation.
310 GNUNET_ARM_Callback callback;
313 * Closure for "callback".
318 * Timeout for the operation.
320 struct GNUNET_TIME_Absolute timeout;
323 * Type of the request expressed as a message type (start or stop).
329 #include "do_start_process.c"
333 * A client specifically requested starting of ARM itself.
334 * This function is called with information about whether
335 * or not ARM is running; if it is, report success. If
336 * it is not, start the ARM process.
338 * @param cls the context for the request that we will report on (struct RequestContext*)
339 * @param tc why were we called (reason says if ARM is running)
342 arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
344 struct RequestContext *pos = cls;
345 struct GNUNET_OS_Process *proc;
351 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
354 LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
355 "gnunet-service-arm");
357 /* arm is running! */
358 if (pos->callback != NULL)
359 pos->callback (pos->cls, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
364 LOG (GNUNET_ERROR_TYPE_DEBUG,
365 "Looks like `%s' is not running, will start it.\n",
366 "gnunet-service-arm");
369 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "PREFIX",
371 loprefix = GNUNET_strdup ("");
373 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "OPTIONS",
375 lopostfix = GNUNET_strdup ("");
377 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "BINARY",
380 LOG (GNUNET_ERROR_TYPE_WARNING,
382 ("Configuration failes to specify option `%s' in section `%s'!\n"),
384 if (pos->callback != NULL)
385 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
387 GNUNET_free (loprefix);
388 GNUNET_free (lopostfix);
392 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg, "arm", "CONFIG",
395 LOG (GNUNET_ERROR_TYPE_WARNING,
396 _("Configuration fails to specify option `%s' in section `%s'!\n"),
398 if (pos->callback != NULL)
399 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
400 GNUNET_free (binary);
402 GNUNET_free (loprefix);
403 GNUNET_free (lopostfix);
407 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING", "WEAKRANDOM"))
409 GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg, "TESTING",
412 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING",
415 /* Means we are ONLY running locally */
416 /* we're clearly running a test, don't daemonize */
417 proc = do_start_process (GNUNET_NO,
418 NULL, loprefix, binary, "-c", config,
422 /* no daemonization! */
427 proc = do_start_process (GNUNET_NO,
428 NULL, loprefix, binary, "-c", config,
432 "-d", lopostfix, NULL);
434 GNUNET_free (binary);
435 GNUNET_free (config);
436 GNUNET_free (loprefix);
437 GNUNET_free (lopostfix);
440 if (pos->callback != NULL)
441 pos->callback (pos->cls, GNUNET_ARM_PROCESS_FAILURE);
445 if (pos->callback != NULL)
446 pos->callback (pos->cls, GNUNET_ARM_PROCESS_STARTING);
453 * Process a response from ARM to a request for a change in service
456 * @param cls the request context
457 * @param msg the response
460 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
462 struct RequestContext *sc = cls;
463 const struct GNUNET_ARM_ResultMessage *res;
464 enum GNUNET_ARM_ProcessStatus status;
467 (ntohs (msg->size) != sizeof (struct GNUNET_ARM_ResultMessage)))
469 LOG (GNUNET_ERROR_TYPE_WARNING,
471 ("Error receiving response to `%s' request from ARM for service `%s'\n"),
472 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START) ? "START" : "STOP",
473 (const char *) &sc[1]);
474 GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
475 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
476 GNUNET_assert (NULL != sc->h->client);
477 if (sc->callback != NULL)
478 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
482 res = (const struct GNUNET_ARM_ResultMessage *) msg;
484 LOG (GNUNET_ERROR_TYPE_DEBUG,
485 "Received response from ARM for service `%s': %u\n",
486 (const char *) &sc[1], ntohs (msg->type));
488 status = (enum GNUNET_ARM_ProcessStatus) ntohl (res->status);
489 if (sc->callback != NULL)
490 sc->callback (sc->cls, status);
496 * Start or stop a service.
498 * @param h handle to ARM
499 * @param service_name name of the service
500 * @param timeout how long to wait before failing for good
501 * @param cb callback to invoke when service is ready
502 * @param cb_cls closure for callback
503 * @param type type of the request
506 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
507 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_Callback cb,
508 void *cb_cls, uint16_t type)
510 struct RequestContext *sctx;
512 struct GNUNET_MessageHeader *msg;
514 slen = strlen (service_name) + 1;
515 if (slen + sizeof (struct GNUNET_MessageHeader) >=
516 GNUNET_SERVER_MAX_MESSAGE_SIZE)
520 cb (cb_cls, GNUNET_NO);
524 LOG (GNUNET_ERROR_TYPE_DEBUG,
526 GNUNET_MESSAGE_TYPE_ARM_START) ?
527 _("Requesting start of service `%s'.\n") :
528 _("Requesting termination of service `%s'.\n"), service_name);
530 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
534 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
536 memcpy (&sctx[1], service_name, slen);
537 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
538 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
539 msg->type = htons (sctx->type);
540 memcpy (&msg[1], service_name, slen);
542 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, msg,
543 GNUNET_TIME_absolute_get_remaining
544 (sctx->timeout), GNUNET_YES,
545 &handle_response, sctx))
547 LOG (GNUNET_ERROR_TYPE_WARNING,
549 GNUNET_MESSAGE_TYPE_ARM_START) ?
550 _("Error while trying to transmit request to start `%s' to ARM\n")
552 _("Error while trying to transmit request to stop `%s' to ARM\n"),
553 (const char *) &service_name);
555 cb (cb_cls, GNUNET_SYSERR);
567 * @param h handle to ARM
568 * @param service_name name of the service
569 * @param timeout how long to wait before failing for good
570 * @param cb callback to invoke when service is ready
571 * @param cb_cls closure for callback
574 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
575 const char *service_name,
576 struct GNUNET_TIME_Relative timeout,
577 GNUNET_ARM_Callback cb, void *cb_cls)
579 struct RequestContext *sctx;
580 struct GNUNET_CLIENT_Connection *client;
584 LOG (GNUNET_ERROR_TYPE_DEBUG,
585 _("Asked to start service `%s' within %llu ms\n"), service_name,
586 (unsigned long long) timeout.rel_value);
588 if (0 == strcasecmp ("arm", service_name))
590 slen = strlen ("arm") + 1;
591 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
595 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
596 memcpy (&sctx[1], service_name, slen);
597 GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
601 if (h->client == NULL)
603 client = GNUNET_CLIENT_connect ("arm", h->cfg);
606 LOG (GNUNET_ERROR_TYPE_DEBUG,
607 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
608 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
611 LOG (GNUNET_ERROR_TYPE_DEBUG,
612 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
615 LOG (GNUNET_ERROR_TYPE_DEBUG, "arm_api, h->client non-NULL\n");
616 change_service (h, service_name, timeout, cb, cb_cls,
617 GNUNET_MESSAGE_TYPE_ARM_START);
622 * Callback from the arm stop service call, indicates that the arm service
623 * is well and truly dead, won't die, or an error occurred.
625 * @param cls closure for the callback
626 * @param reason reason for callback
629 arm_shutdown_callback (void *cls, enum GNUNET_ARM_ProcessStatus reason)
631 struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
633 if (arm_shutdown_ctx->cb != NULL)
634 arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
636 GNUNET_free (arm_shutdown_ctx);
643 * @param h handle to ARM
644 * @param service_name name of the service
645 * @param timeout how long to wait before failing for good
646 * @param cb callback to invoke when service is ready
647 * @param cb_cls closure for callback
650 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
651 const char *service_name,
652 struct GNUNET_TIME_Relative timeout,
653 GNUNET_ARM_Callback cb, void *cb_cls)
655 struct ARM_ShutdownContext *arm_shutdown_ctx;
656 struct GNUNET_CLIENT_Connection *client;
658 LOG (GNUNET_ERROR_TYPE_INFO, _("Stopping service `%s' within %llu ms\n"),
659 service_name, (unsigned long long) timeout.rel_value);
660 if (h->client == NULL)
662 client = GNUNET_CLIENT_connect ("arm", h->cfg);
665 cb (cb_cls, GNUNET_SYSERR);
670 if (0 == strcasecmp ("arm", service_name))
672 arm_shutdown_ctx = GNUNET_malloc (sizeof (struct ARM_ShutdownContext));
673 arm_shutdown_ctx->cb = cb;
674 arm_shutdown_ctx->cb_cls = cb_cls;
675 arm_service_shutdown (h->client, timeout, &arm_shutdown_callback,
680 change_service (h, service_name, timeout, cb, cb_cls,
681 GNUNET_MESSAGE_TYPE_ARM_STOP);
686 * Internal state for a list request with ARM.
688 struct ListRequestContext
692 * Pointer to our handle with ARM.
694 struct GNUNET_ARM_Handle *h;
697 * Function to call with a status code for the requested operation.
699 GNUNET_ARM_List_Callback callback;
702 * Closure for "callback".
707 * Timeout for the operation.
709 struct GNUNET_TIME_Absolute timeout;
714 * Process a response from ARM for the list request.
716 * @param cls the list request context
717 * @param msg the response
720 handle_list_response (void *cls, const struct GNUNET_MessageHeader *msg)
722 struct ListRequestContext *sc = cls;
723 const struct GNUNET_ARM_ListResultMessage *res;
731 LOG (GNUNET_ERROR_TYPE_WARNING,
732 "Error receiving response to LIST request from ARM\n");
733 GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
734 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
735 GNUNET_assert (NULL != sc->h->client);
736 if (sc->callback != NULL)
737 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
742 if (NULL == sc->callback)
748 msize = ntohs (msg->size);
749 if ( (msize < sizeof ( struct GNUNET_ARM_ListResultMessage)) ||
750 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT) )
753 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
758 res = (const struct GNUNET_ARM_ListResultMessage *) msg;
759 rcount = ntohs (res->count);
761 const char *list[rcount];
764 pos = (const char *)&res[1];
765 for (i=0; i<rcount; i++)
767 const char *end = memchr (pos, 0, msize - size_check);
771 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
776 size_check += (end - pos) + 1;
779 sc->callback (sc->cls, GNUNET_YES, rcount, list);
786 * List all running services.
788 * @param h handle to ARM
789 * @param timeout how long to wait before failing for good
790 * @param cb callback to invoke when service is ready
791 * @param cb_cls closure for callback
794 GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
795 struct GNUNET_TIME_Relative timeout,
796 GNUNET_ARM_List_Callback cb, void *cb_cls)
798 struct ListRequestContext *sctx;
799 struct GNUNET_MessageHeader msg;
800 struct GNUNET_CLIENT_Connection *client;
802 if (h->client == NULL)
804 client = GNUNET_CLIENT_connect ("arm", h->cfg);
807 LOG (GNUNET_ERROR_TYPE_DEBUG,
808 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
809 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
812 LOG (GNUNET_ERROR_TYPE_DEBUG,
813 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
817 sctx = GNUNET_malloc (sizeof (struct RequestContext));
821 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
822 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
823 msg.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
825 LOG (GNUNET_ERROR_TYPE_DEBUG,
826 "Requesting LIST from ARM service with timeout: %llu ms\n",
827 (unsigned long long)timeout.rel_value);
830 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
832 GNUNET_TIME_absolute_get_remaining
835 &handle_list_response,
838 LOG (GNUNET_ERROR_TYPE_WARNING,
839 "Error while trying to transmit request to list services to ARM\n");
841 cb (cb_cls, GNUNET_SYSERR, 0, NULL);
847 /* end of arm_api.c */