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"
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 * Result of the operation
88 enum GNUNET_ARM_ProcessStatus confirmed;
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;
108 const struct GNUNET_ARM_ResultMessage *rmsg;
112 if (shutdown_ctx->cont != NULL)
114 if (shutdown_ctx->confirmed == GNUNET_ARM_PROCESS_SHUTDOWN)
116 /* shutdown is now complete, as we waited for the network disconnect... */
117 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_DOWN);
121 /* communication error */
122 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
125 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
126 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
127 GNUNET_free (shutdown_ctx);
130 if (ntohs (msg->size) ==
131 sizeof (struct GNUNET_ARM_ResultMessage))
133 rmsg = (const struct GNUNET_ARM_ResultMessage*) msg;
134 shutdown_ctx->confirmed = (enum GNUNET_ARM_ProcessStatus) ntohl (rmsg->status);
135 if (shutdown_ctx->confirmed != GNUNET_ARM_PROCESS_SHUTDOWN)
137 /* ARM is not shutting down, well, report the error and be done with it... */
138 shutdown_ctx->cont (shutdown_ctx->cont_cls, shutdown_ctx->confirmed);
139 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
140 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
141 GNUNET_free (shutdown_ctx);
145 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
146 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
151 * Shutting down took too long, cancel receive and return error.
154 * @param tc context information (why was this task triggered now)
157 service_shutdown_cancel (void *cls,
158 const struct GNUNET_SCHEDULER_TaskContext *tc)
160 struct ShutdownContext *shutdown_ctx = cls;
162 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT);
163 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
164 GNUNET_free (shutdown_ctx);
169 * If possible, write a shutdown message to the target
170 * buffer and destroy the client connection.
172 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
173 * @param size number of bytes available in buf
174 * @param buf NULL on error, otherwise target buffer
175 * @return number of bytes written to buf
178 write_shutdown (void *cls, size_t size, void *buf)
180 struct GNUNET_MessageHeader *msg;
181 struct ShutdownContext *shutdown_ctx = cls;
183 if (size < sizeof (struct GNUNET_MessageHeader))
185 LOG (GNUNET_ERROR_TYPE_WARNING,
186 _("Failed to transmit shutdown request to client.\n"));
187 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
188 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
189 GNUNET_free (shutdown_ctx);
190 return 0; /* client disconnected */
193 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
194 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
195 shutdown_ctx->cancel_task =
196 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
197 (shutdown_ctx->timeout),
198 &service_shutdown_cancel, shutdown_ctx);
199 msg = (struct GNUNET_MessageHeader *) buf;
200 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
201 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
202 return sizeof (struct GNUNET_MessageHeader);
207 * Request that the service should shutdown.
208 * Afterwards, the connection will automatically be
209 * disconnected. Hence the "sock" should not
210 * be used by the caller after this call
211 * (calling this function frees "sock" after a while).
213 * @param sock the socket connected to the service
214 * @param timeout how long to wait before giving up on transmission
215 * @param cont continuation to call once the service is really down
216 * @param cont_cls closure for continuation
220 arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
221 struct GNUNET_TIME_Relative timeout,
222 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
224 struct ShutdownContext *shutdown_ctx;
226 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
227 shutdown_ctx->cont = cont;
228 shutdown_ctx->cont_cls = cont_cls;
229 shutdown_ctx->sock = sock;
230 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
231 shutdown_ctx->confirmed = GNUNET_ARM_PROCESS_COMMUNICATION_ERROR;
232 /* FIXME: store return value? */
233 GNUNET_CLIENT_notify_transmit_ready (sock,
234 sizeof (struct GNUNET_MessageHeader),
235 timeout, GNUNET_YES, &write_shutdown,
241 * Setup a context for communicating with ARM. Note that this
242 * can be done even if the ARM service is not yet running.
244 * @param cfg configuration to use (needed to contact ARM;
245 * the ARM service may internally use a different
246 * configuration to determine how to start the service).
247 * @param service service that *this* process is implementing/providing, can be NULL
248 * @return context to use for further ARM operations, NULL on error
250 struct GNUNET_ARM_Handle *
251 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
254 struct GNUNET_ARM_Handle *ret;
256 ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
257 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
263 * Disconnect from the ARM service.
265 * @param h the handle that was being used
268 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
270 if (h->client != NULL)
271 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
272 GNUNET_CONFIGURATION_destroy (h->cfg);
277 struct ARM_ShutdownContext
280 * Callback to call once shutdown complete.
282 GNUNET_ARM_Callback cb;
285 * Closure for callback.
292 * Internal state for a request with ARM.
294 struct RequestContext
298 * Pointer to our handle with ARM.
300 struct GNUNET_ARM_Handle *h;
303 * Function to call with a status code for the requested operation.
305 GNUNET_ARM_Callback callback;
308 * Closure for "callback".
313 * Timeout for the operation.
315 struct GNUNET_TIME_Absolute timeout;
318 * Type of the request expressed as a message type (start or stop).
324 #include "do_start_process.c"
328 * A client specifically requested starting of ARM itself.
329 * This function is called with information about whether
330 * or not ARM is running; if it is, report success. If
331 * it is not, start the ARM process.
333 * @param cls the context for the request that we will report on (struct RequestContext*)
334 * @param tc why were we called (reason says if ARM is running)
337 arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
339 struct RequestContext *pos = cls;
340 struct GNUNET_OS_Process *proc;
346 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
349 LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
350 "gnunet-service-arm");
352 /* arm is running! */
353 if (pos->callback != NULL)
354 pos->callback (pos->cls, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
359 LOG (GNUNET_ERROR_TYPE_DEBUG,
360 "Looks like `%s' is not running, will start it.\n",
361 "gnunet-service-arm");
364 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "PREFIX",
366 loprefix = GNUNET_strdup ("");
368 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "OPTIONS",
370 lopostfix = GNUNET_strdup ("");
372 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "BINARY",
375 LOG (GNUNET_ERROR_TYPE_WARNING,
377 ("Configuration failes to specify option `%s' in section `%s'!\n"),
379 if (pos->callback != NULL)
380 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
382 GNUNET_free (loprefix);
383 GNUNET_free (lopostfix);
387 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg, "arm", "CONFIG",
390 LOG (GNUNET_ERROR_TYPE_WARNING,
391 _("Configuration fails to specify option `%s' in section `%s'!\n"),
393 if (pos->callback != NULL)
394 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
395 GNUNET_free (binary);
397 GNUNET_free (loprefix);
398 GNUNET_free (lopostfix);
402 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING", "WEAKRANDOM"))
404 GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg, "TESTING",
407 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING",
410 /* Means we are ONLY running locally */
411 /* we're clearly running a test, don't daemonize */
412 proc = do_start_process (GNUNET_NO,
413 NULL, loprefix, binary, "-c", config,
417 /* no daemonization! */
422 proc = do_start_process (GNUNET_NO,
423 NULL, loprefix, binary, "-c", config,
427 "-d", lopostfix, NULL);
429 GNUNET_free (binary);
430 GNUNET_free (config);
431 GNUNET_free (loprefix);
432 GNUNET_free (lopostfix);
435 if (pos->callback != NULL)
436 pos->callback (pos->cls, GNUNET_ARM_PROCESS_FAILURE);
440 if (pos->callback != NULL)
441 pos->callback (pos->cls, GNUNET_ARM_PROCESS_STARTING);
448 * Process a response from ARM to a request for a change in service
451 * @param cls the request context
452 * @param msg the response
455 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
457 struct RequestContext *sc = cls;
458 const struct GNUNET_ARM_ResultMessage *res;
459 enum GNUNET_ARM_ProcessStatus status;
462 (ntohs (msg->size) != sizeof (struct GNUNET_ARM_ResultMessage)))
464 LOG (GNUNET_ERROR_TYPE_WARNING,
466 ("Error receiving response to `%s' request from ARM for service `%s'\n"),
467 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START) ? "START" : "STOP",
468 (const char *) &sc[1]);
469 GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
470 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
471 GNUNET_assert (NULL != sc->h->client);
472 if (sc->callback != NULL)
473 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
477 res = (const struct GNUNET_ARM_ResultMessage *) msg;
479 LOG (GNUNET_ERROR_TYPE_DEBUG,
480 "Received response from ARM for service `%s': %u\n",
481 (const char *) &sc[1], ntohs (msg->type));
483 status = (enum GNUNET_ARM_ProcessStatus) ntohl (res->status);
484 if (sc->callback != NULL)
485 sc->callback (sc->cls, status);
491 * Start or stop a service.
493 * @param h handle to ARM
494 * @param service_name name of the service
495 * @param timeout how long to wait before failing for good
496 * @param cb callback to invoke when service is ready
497 * @param cb_cls closure for callback
498 * @param type type of the request
501 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
502 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_Callback cb,
503 void *cb_cls, uint16_t type)
505 struct RequestContext *sctx;
507 struct GNUNET_MessageHeader *msg;
509 slen = strlen (service_name) + 1;
510 if (slen + sizeof (struct GNUNET_MessageHeader) >=
511 GNUNET_SERVER_MAX_MESSAGE_SIZE)
515 cb (cb_cls, GNUNET_NO);
519 LOG (GNUNET_ERROR_TYPE_DEBUG,
521 GNUNET_MESSAGE_TYPE_ARM_START) ?
522 _("Requesting start of service `%s'.\n") :
523 _("Requesting termination of service `%s'.\n"), service_name);
525 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
529 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
531 memcpy (&sctx[1], service_name, slen);
532 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
533 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
534 msg->type = htons (sctx->type);
535 memcpy (&msg[1], service_name, slen);
537 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, msg,
538 GNUNET_TIME_absolute_get_remaining
539 (sctx->timeout), GNUNET_YES,
540 &handle_response, sctx))
542 LOG (GNUNET_ERROR_TYPE_WARNING,
544 GNUNET_MESSAGE_TYPE_ARM_START) ?
545 _("Error while trying to transmit request to start `%s' to ARM\n")
547 _("Error while trying to transmit request to stop `%s' to ARM\n"),
548 (const char *) &service_name);
550 cb (cb_cls, GNUNET_SYSERR);
562 * @param h handle to ARM
563 * @param service_name name of the service
564 * @param timeout how long to wait before failing for good
565 * @param cb callback to invoke when service is ready
566 * @param cb_cls closure for callback
569 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
570 const char *service_name,
571 struct GNUNET_TIME_Relative timeout,
572 GNUNET_ARM_Callback cb, void *cb_cls)
574 struct RequestContext *sctx;
575 struct GNUNET_CLIENT_Connection *client;
579 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 _("Asked to start service `%s' within %llu ms\n"), service_name,
581 (unsigned long long) timeout.rel_value);
583 if (0 == strcasecmp ("arm", service_name))
585 slen = strlen ("arm") + 1;
586 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
590 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
591 memcpy (&sctx[1], service_name, slen);
592 GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
596 if (h->client == NULL)
598 client = GNUNET_CLIENT_connect ("arm", h->cfg);
601 LOG (GNUNET_ERROR_TYPE_DEBUG,
602 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
603 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
606 LOG (GNUNET_ERROR_TYPE_DEBUG,
607 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
610 LOG (GNUNET_ERROR_TYPE_DEBUG, "arm_api, h->client non-NULL\n");
611 change_service (h, service_name, timeout, cb, cb_cls,
612 GNUNET_MESSAGE_TYPE_ARM_START);
617 * Callback from the arm stop service call, indicates that the arm service
618 * is well and truly dead, won't die, or an error occurred.
620 * @param cls closure for the callback
621 * @param reason reason for callback
624 arm_shutdown_callback (void *cls, enum GNUNET_ARM_ProcessStatus reason)
626 struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
628 if (arm_shutdown_ctx->cb != NULL)
629 arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
631 GNUNET_free (arm_shutdown_ctx);
638 * @param h handle to ARM
639 * @param service_name name of the service
640 * @param timeout how long to wait before failing for good
641 * @param cb callback to invoke when service is ready
642 * @param cb_cls closure for callback
645 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
646 const char *service_name,
647 struct GNUNET_TIME_Relative timeout,
648 GNUNET_ARM_Callback cb, void *cb_cls)
650 struct ARM_ShutdownContext *arm_shutdown_ctx;
651 struct GNUNET_CLIENT_Connection *client;
653 LOG (GNUNET_ERROR_TYPE_INFO, _("Stopping service `%s' within %llu ms\n"),
654 service_name, (unsigned long long) timeout.rel_value);
655 if (h->client == NULL)
657 client = GNUNET_CLIENT_connect ("arm", h->cfg);
660 cb (cb_cls, GNUNET_SYSERR);
665 if (0 == strcasecmp ("arm", service_name))
667 arm_shutdown_ctx = GNUNET_malloc (sizeof (struct ARM_ShutdownContext));
668 arm_shutdown_ctx->cb = cb;
669 arm_shutdown_ctx->cb_cls = cb_cls;
670 arm_service_shutdown (h->client, timeout, &arm_shutdown_callback,
675 change_service (h, service_name, timeout, cb, cb_cls,
676 GNUNET_MESSAGE_TYPE_ARM_STOP);
681 * Internal state for a list request with ARM.
683 struct ListRequestContext
687 * Pointer to our handle with ARM.
689 struct GNUNET_ARM_Handle *h;
692 * Function to call with a status code for the requested operation.
694 GNUNET_ARM_List_Callback callback;
697 * Closure for "callback".
702 * Timeout for the operation.
704 struct GNUNET_TIME_Absolute timeout;
709 * Process a response from ARM for the list request.
711 * @param cls the list request context
712 * @param msg the response
715 handle_list_response (void *cls, const struct GNUNET_MessageHeader *msg)
717 struct ListRequestContext *sc = cls;
718 const struct GNUNET_ARM_ListResultMessage *res;
726 LOG (GNUNET_ERROR_TYPE_WARNING,
727 "Error receiving response to LIST request from ARM\n");
728 GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
729 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
730 GNUNET_assert (NULL != sc->h->client);
731 if (sc->callback != NULL)
732 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
737 if (NULL == sc->callback)
743 msize = ntohs (msg->size);
744 if ( (msize < sizeof ( struct GNUNET_ARM_ListResultMessage)) ||
745 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT) )
748 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
753 res = (const struct GNUNET_ARM_ListResultMessage *) msg;
754 rcount = ntohs (res->count);
756 const char *list[rcount];
759 pos = (const char *)&res[1];
760 for (i=0; i<rcount; i++)
762 const char *end = memchr (pos, 0, msize - size_check);
767 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
771 size_check += (end - pos) + 1;
774 sc->callback (sc->cls, GNUNET_YES, rcount, list);
781 * List all running services.
783 * @param h handle to ARM
784 * @param timeout how long to wait before failing for good
785 * @param cb callback to invoke when service is ready
786 * @param cb_cls closure for callback
789 GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
790 struct GNUNET_TIME_Relative timeout,
791 GNUNET_ARM_List_Callback cb, void *cb_cls)
793 struct ListRequestContext *sctx;
794 struct GNUNET_MessageHeader msg;
795 struct GNUNET_CLIENT_Connection *client;
797 if (h->client == NULL)
799 client = GNUNET_CLIENT_connect ("arm", h->cfg);
802 LOG (GNUNET_ERROR_TYPE_DEBUG,
803 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
804 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
807 LOG (GNUNET_ERROR_TYPE_DEBUG,
808 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
812 sctx = GNUNET_malloc (sizeof (struct RequestContext));
816 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
817 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
818 msg.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
820 LOG (GNUNET_ERROR_TYPE_DEBUG,
821 "Requesting LIST from ARM service with timeout: %llu ms\n",
822 (unsigned long long)timeout.rel_value);
825 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
827 GNUNET_TIME_absolute_get_remaining
830 &handle_list_response,
833 LOG (GNUNET_ERROR_TYPE_WARNING,
834 "Error while trying to transmit request to list services to ARM\n");
836 cb (cb_cls, GNUNET_SYSERR, 0, NULL);
842 /* end of arm_api.c */