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).
299 * Flags for passing std descriptors to ARM (when starting ARM).
301 enum GNUNET_OS_InheritStdioFlags std_inheritance;
305 #include "do_start_process.c"
309 * A client specifically requested starting of ARM itself.
310 * This function is called with information about whether
311 * or not ARM is running; if it is, report success. If
312 * it is not, start the ARM process.
314 * @param cls the context for the request that we will report on (struct RequestContext*)
315 * @param tc why were we called (reason says if ARM is running)
318 arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
320 struct RequestContext *pos = cls;
321 struct GNUNET_OS_Process *proc;
327 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
329 LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
330 "gnunet-service-arm");
331 /* arm is running! */
332 if (pos->callback != NULL)
333 pos->callback (pos->cls, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
337 LOG (GNUNET_ERROR_TYPE_DEBUG,
338 "Looks like `%s' is not running, will start it.\n",
339 "gnunet-service-arm");
341 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "PREFIX",
343 loprefix = GNUNET_strdup ("");
345 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "OPTIONS",
347 lopostfix = GNUNET_strdup ("");
349 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "BINARY",
352 LOG (GNUNET_ERROR_TYPE_WARNING,
354 ("Configuration failes to specify option `%s' in section `%s'!\n"),
356 if (pos->callback != NULL)
357 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
359 GNUNET_free (loprefix);
360 GNUNET_free (lopostfix);
364 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg, "arm", "CONFIG",
367 LOG (GNUNET_ERROR_TYPE_WARNING,
368 _("Configuration fails to specify option `%s' in section `%s'!\n"),
370 if (pos->callback != NULL)
371 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
372 GNUNET_free (binary);
374 GNUNET_free (loprefix);
375 GNUNET_free (lopostfix);
379 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING", "WEAKRANDOM"))
381 GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg, "TESTING",
384 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING",
387 /* Means we are ONLY running locally */
388 /* we're clearly running a test, don't daemonize */
389 proc = do_start_process (GNUNET_NO, pos->std_inheritance,
390 NULL, loprefix, binary, "-c", config,
391 /* no daemonization! */
396 proc = do_start_process (GNUNET_NO, pos->std_inheritance,
397 NULL, loprefix, binary, "-c", config,
398 "-d", lopostfix, NULL);
400 GNUNET_free (binary);
401 GNUNET_free (config);
402 GNUNET_free (loprefix);
403 GNUNET_free (lopostfix);
406 if (pos->callback != NULL)
407 pos->callback (pos->cls, GNUNET_ARM_PROCESS_FAILURE);
411 if (pos->callback != NULL)
412 pos->callback (pos->cls, GNUNET_ARM_PROCESS_STARTING);
419 * Process a response from ARM to a request for a change in service
422 * @param cls the request context
423 * @param msg the response
426 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
428 struct RequestContext *sc = cls;
429 const struct GNUNET_ARM_ResultMessage *res;
430 enum GNUNET_ARM_ProcessStatus status;
433 (ntohs (msg->size) != sizeof (struct GNUNET_ARM_ResultMessage)))
435 LOG (GNUNET_ERROR_TYPE_WARNING,
437 ("Error receiving response to `%s' request from ARM for service `%s'\n"),
438 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START) ? "START" : "STOP",
439 (const char *) &sc[1]);
440 GNUNET_CLIENT_disconnect (sc->h->client);
441 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
442 GNUNET_assert (NULL != sc->h->client);
443 if (sc->callback != NULL)
444 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
448 res = (const struct GNUNET_ARM_ResultMessage *) msg;
449 LOG (GNUNET_ERROR_TYPE_DEBUG,
450 "Received response from ARM for service `%s': %u\n",
451 (const char *) &sc[1], ntohs (msg->type));
452 status = (enum GNUNET_ARM_ProcessStatus) ntohl (res->status);
453 if (sc->callback != NULL)
454 sc->callback (sc->cls, status);
460 * Start or stop a service.
462 * @param h handle to ARM
463 * @param service_name name of the service
464 * @param timeout how long to wait before failing for good
465 * @param cb callback to invoke when service is ready
466 * @param cb_cls closure for callback
467 * @param type type of the request
470 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
471 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_Callback cb,
472 void *cb_cls, uint16_t type)
474 struct RequestContext *sctx;
476 struct GNUNET_MessageHeader *msg;
478 slen = strlen (service_name) + 1;
479 if (slen + sizeof (struct GNUNET_MessageHeader) >=
480 GNUNET_SERVER_MAX_MESSAGE_SIZE)
484 cb (cb_cls, GNUNET_NO);
487 LOG (GNUNET_ERROR_TYPE_DEBUG,
489 GNUNET_MESSAGE_TYPE_ARM_START) ?
490 _("Requesting start of service `%s'.\n") :
491 _("Requesting termination of service `%s'.\n"), service_name);
492 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
496 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
498 memcpy (&sctx[1], service_name, slen);
499 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
500 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
501 msg->type = htons (sctx->type);
502 memcpy (&msg[1], service_name, slen);
504 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, msg,
505 GNUNET_TIME_absolute_get_remaining
506 (sctx->timeout), GNUNET_YES,
507 &handle_response, sctx))
509 LOG (GNUNET_ERROR_TYPE_WARNING,
511 GNUNET_MESSAGE_TYPE_ARM_START)
512 ? _("Error while trying to transmit request to start `%s' to ARM\n")
513 : _("Error while trying to transmit request to stop `%s' to ARM\n"),
514 (const char *) &service_name);
516 cb (cb_cls, GNUNET_SYSERR);
528 * @param h handle to ARM
529 * @param service_name name of the service
530 * @param std_inheritance inheritance of std streams
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 enum GNUNET_OS_InheritStdioFlags std_inheritance,
539 struct GNUNET_TIME_Relative timeout,
540 GNUNET_ARM_Callback cb, void *cb_cls)
542 struct RequestContext *sctx;
543 struct GNUNET_CLIENT_Connection *client;
546 LOG (GNUNET_ERROR_TYPE_DEBUG,
547 _("Asked to start service `%s' within %llu ms\n"), service_name,
548 (unsigned long long) timeout.rel_value);
549 if (0 == strcasecmp ("arm", service_name))
551 slen = strlen ("arm") + 1;
552 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
556 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
557 sctx->std_inheritance = std_inheritance;
558 memcpy (&sctx[1], service_name, slen);
559 GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
563 if (NULL == h->client)
565 client = GNUNET_CLIENT_connect ("arm", h->cfg);
568 LOG (GNUNET_ERROR_TYPE_DEBUG,
569 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
570 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
573 LOG (GNUNET_ERROR_TYPE_DEBUG,
574 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
577 LOG (GNUNET_ERROR_TYPE_DEBUG, "arm_api, h->client non-NULL\n");
578 change_service (h, service_name, timeout, cb, cb_cls,
579 GNUNET_MESSAGE_TYPE_ARM_START);
584 * Callback from the arm stop service call, indicates that the arm service
585 * is well and truly dead, won't die, or an error occurred.
587 * @param cls closure for the callback
588 * @param reason reason for callback
591 arm_shutdown_callback (void *cls, enum GNUNET_ARM_ProcessStatus reason)
593 struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
595 if (arm_shutdown_ctx->cb != NULL)
596 arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
598 GNUNET_free (arm_shutdown_ctx);
605 * @param h handle to ARM
606 * @param service_name name of the service
607 * @param timeout how long to wait before failing for good
608 * @param cb callback to invoke when service is ready
609 * @param cb_cls closure for callback
612 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
613 const char *service_name,
614 struct GNUNET_TIME_Relative timeout,
615 GNUNET_ARM_Callback cb, void *cb_cls)
617 struct ARM_ShutdownContext *arm_shutdown_ctx;
618 struct GNUNET_CLIENT_Connection *client;
620 LOG (GNUNET_ERROR_TYPE_INFO, _("Stopping service `%s' within %llu ms\n"),
621 service_name, (unsigned long long) timeout.rel_value);
622 if (h->client == NULL)
624 client = GNUNET_CLIENT_connect ("arm", h->cfg);
627 cb (cb_cls, GNUNET_SYSERR);
632 if (0 == strcasecmp ("arm", service_name))
634 arm_shutdown_ctx = GNUNET_malloc (sizeof (struct ARM_ShutdownContext));
635 arm_shutdown_ctx->cb = cb;
636 arm_shutdown_ctx->cb_cls = cb_cls;
637 arm_service_shutdown (h->client, timeout, &arm_shutdown_callback,
642 change_service (h, service_name, timeout, cb, cb_cls,
643 GNUNET_MESSAGE_TYPE_ARM_STOP);
648 * Internal state for a list request with ARM.
650 struct ListRequestContext
654 * Pointer to our handle with ARM.
656 struct GNUNET_ARM_Handle *h;
659 * Function to call with a status code for the requested operation.
661 GNUNET_ARM_List_Callback callback;
664 * Closure for "callback".
669 * Timeout for the operation.
671 struct GNUNET_TIME_Absolute timeout;
676 * Process a response from ARM for the list request.
678 * @param cls the list request context
679 * @param msg the response
682 handle_list_response (void *cls, const struct GNUNET_MessageHeader *msg)
684 struct ListRequestContext *sc = cls;
685 const struct GNUNET_ARM_ListResultMessage *res;
693 LOG (GNUNET_ERROR_TYPE_WARNING,
694 "Error receiving response to LIST request from ARM\n");
695 GNUNET_CLIENT_disconnect (sc->h->client);
696 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
697 GNUNET_assert (NULL != sc->h->client);
698 if (sc->callback != NULL)
699 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
704 if (NULL == sc->callback)
710 msize = ntohs (msg->size);
711 if ( (msize < sizeof ( struct GNUNET_ARM_ListResultMessage)) ||
712 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT) )
715 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
720 res = (const struct GNUNET_ARM_ListResultMessage *) msg;
721 rcount = ntohs (res->count);
723 const char *list[rcount];
726 pos = (const char *)&res[1];
727 for (i=0; i<rcount; i++)
729 const char *end = memchr (pos, 0, msize - size_check);
733 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
738 size_check += (end - pos) + 1;
741 sc->callback (sc->cls, GNUNET_YES, rcount, list);
748 * List all running services.
750 * @param h handle to ARM
751 * @param timeout how long to wait before failing for good
752 * @param cb callback to invoke when service is ready
753 * @param cb_cls closure for callback
756 GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
757 struct GNUNET_TIME_Relative timeout,
758 GNUNET_ARM_List_Callback cb, void *cb_cls)
760 struct ListRequestContext *sctx;
761 struct GNUNET_MessageHeader msg;
762 struct GNUNET_CLIENT_Connection *client;
764 if (h->client == NULL)
766 client = GNUNET_CLIENT_connect ("arm", h->cfg);
769 LOG (GNUNET_ERROR_TYPE_DEBUG,
770 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
771 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
774 LOG (GNUNET_ERROR_TYPE_DEBUG,
775 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
779 sctx = GNUNET_malloc (sizeof (struct RequestContext));
783 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
784 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
785 msg.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
787 LOG (GNUNET_ERROR_TYPE_DEBUG,
788 "Requesting LIST from ARM service with timeout: %llu ms\n",
789 (unsigned long long)timeout.rel_value);
792 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
794 GNUNET_TIME_absolute_get_remaining
797 &handle_list_response,
800 LOG (GNUNET_ERROR_TYPE_WARNING,
801 "Error while trying to transmit request to list services to ARM\n");
803 cb (cb_cls, GNUNET_SYSERR, 0, NULL);
809 /* end of arm_api.c */