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 /* shutdown is now complete, as we waited for the network disconnect... */
120 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);
127 if (ntohs (msg->size) ==
128 sizeof (struct GNUNET_ARM_ResultMessage))
130 rmsg = (const struct GNUNET_ARM_ResultMessage*) msg;
131 shutdown_ctx->confirmed = (enum GNUNET_ARM_ProcessStatus) ntohl (rmsg->status);
132 if (shutdown_ctx->confirmed != GNUNET_ARM_PROCESS_SHUTDOWN)
134 /* ARM is not shutting down, well, report the error and be done with it... */
135 shutdown_ctx->cont (shutdown_ctx->cont_cls, shutdown_ctx->confirmed);
136 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
137 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
138 GNUNET_free (shutdown_ctx);
142 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
143 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
148 * Shutting down took too long, cancel receive and return error.
151 * @param tc context information (why was this task triggered now)
154 service_shutdown_cancel (void *cls,
155 const struct GNUNET_SCHEDULER_TaskContext *tc)
157 struct ShutdownContext *shutdown_ctx = cls;
159 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT);
160 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
161 GNUNET_free (shutdown_ctx);
166 * If possible, write a shutdown message to the target
167 * buffer and destroy the client connection.
169 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
170 * @param size number of bytes available in buf
171 * @param buf NULL on error, otherwise target buffer
172 * @return number of bytes written to buf
175 write_shutdown (void *cls, size_t size, void *buf)
177 struct ShutdownContext *shutdown_ctx = cls;
178 struct GNUNET_MessageHeader *msg;
180 shutdown_ctx->th = NULL;
181 if (size < sizeof (struct GNUNET_MessageHeader))
183 LOG (GNUNET_ERROR_TYPE_WARNING,
184 _("Failed to transmit shutdown request to client.\n"));
185 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
186 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
187 GNUNET_free (shutdown_ctx);
188 return 0; /* client disconnected */
190 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
191 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
192 shutdown_ctx->cancel_task =
193 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
194 (shutdown_ctx->timeout),
195 &service_shutdown_cancel, shutdown_ctx);
196 msg = (struct GNUNET_MessageHeader *) buf;
197 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
198 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
199 return sizeof (struct GNUNET_MessageHeader);
204 * Request that the service should shutdown.
205 * Afterwards, the connection will automatically be
206 * disconnected. Hence the "sock" should not
207 * be used by the caller after this call
208 * (calling this function frees "sock" after a while).
210 * @param sock the socket connected to the service
211 * @param timeout how long to wait before giving up on transmission
212 * @param cont continuation to call once the service is really down
213 * @param cont_cls closure for continuation
217 arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
218 struct GNUNET_TIME_Relative timeout,
219 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
221 struct ShutdownContext *shutdown_ctx;
223 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
224 shutdown_ctx->cont = cont;
225 shutdown_ctx->cont_cls = cont_cls;
226 shutdown_ctx->sock = sock;
227 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
228 shutdown_ctx->confirmed = GNUNET_ARM_PROCESS_COMMUNICATION_ERROR;
229 shutdown_ctx->th = GNUNET_CLIENT_notify_transmit_ready (sock,
230 sizeof (struct GNUNET_MessageHeader),
231 timeout, GNUNET_NO, &write_shutdown,
237 * Setup a context for communicating with ARM. Note that this
238 * can be done even if the ARM service is not yet running.
240 * @param cfg configuration to use (needed to contact ARM;
241 * the ARM service may internally use a different
242 * configuration to determine how to start the service).
243 * @param service service that *this* process is implementing/providing, can be NULL
244 * @return context to use for further ARM operations, NULL on error
246 struct GNUNET_ARM_Handle *
247 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
250 struct GNUNET_ARM_Handle *ret;
252 ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
253 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
259 * Disconnect from the ARM service.
261 * @param h the handle that was being used
264 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
266 if (h->client != NULL)
267 GNUNET_CLIENT_disconnect (h->client);
268 GNUNET_CONFIGURATION_destroy (h->cfg);
273 struct ARM_ShutdownContext
276 * Callback to call once shutdown complete.
278 GNUNET_ARM_Callback cb;
281 * Closure for callback.
288 * Internal state for a request with ARM.
290 struct RequestContext
294 * Pointer to our handle with ARM.
296 struct GNUNET_ARM_Handle *h;
299 * Function to call with a status code for the requested operation.
301 GNUNET_ARM_Callback callback;
304 * Closure for "callback".
309 * Timeout for the operation.
311 struct GNUNET_TIME_Absolute timeout;
314 * Type of the request expressed as a message type (start or stop).
320 #include "do_start_process.c"
324 * A client specifically requested starting of ARM itself.
325 * This function is called with information about whether
326 * or not ARM is running; if it is, report success. If
327 * it is not, start the ARM process.
329 * @param cls the context for the request that we will report on (struct RequestContext*)
330 * @param tc why were we called (reason says if ARM is running)
333 arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
335 struct RequestContext *pos = cls;
336 struct GNUNET_OS_Process *proc;
342 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
344 LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
345 "gnunet-service-arm");
346 /* arm is running! */
347 if (pos->callback != NULL)
348 pos->callback (pos->cls, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
352 LOG (GNUNET_ERROR_TYPE_DEBUG,
353 "Looks like `%s' is not running, will start it.\n",
354 "gnunet-service-arm");
356 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "PREFIX",
358 loprefix = GNUNET_strdup ("");
360 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "OPTIONS",
362 lopostfix = GNUNET_strdup ("");
364 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "BINARY",
367 LOG (GNUNET_ERROR_TYPE_WARNING,
369 ("Configuration failes to specify option `%s' in section `%s'!\n"),
371 if (pos->callback != NULL)
372 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
374 GNUNET_free (loprefix);
375 GNUNET_free (lopostfix);
379 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg, "arm", "CONFIG",
382 LOG (GNUNET_ERROR_TYPE_WARNING,
383 _("Configuration fails to specify option `%s' in section `%s'!\n"),
385 if (pos->callback != NULL)
386 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
387 GNUNET_free (binary);
389 GNUNET_free (loprefix);
390 GNUNET_free (lopostfix);
394 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING", "WEAKRANDOM"))
396 GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg, "TESTING",
399 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING",
402 /* Means we are ONLY running locally */
403 /* we're clearly running a test, don't daemonize */
404 proc = do_start_process (GNUNET_NO,
405 NULL, loprefix, binary, "-c", config,
409 /* no daemonization! */
414 proc = do_start_process (GNUNET_NO,
415 NULL, loprefix, binary, "-c", config,
419 "-d", lopostfix, NULL);
421 GNUNET_free (binary);
422 GNUNET_free (config);
423 GNUNET_free (loprefix);
424 GNUNET_free (lopostfix);
427 if (pos->callback != NULL)
428 pos->callback (pos->cls, GNUNET_ARM_PROCESS_FAILURE);
432 if (pos->callback != NULL)
433 pos->callback (pos->cls, GNUNET_ARM_PROCESS_STARTING);
440 * Process a response from ARM to a request for a change in service
443 * @param cls the request context
444 * @param msg the response
447 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
449 struct RequestContext *sc = cls;
450 const struct GNUNET_ARM_ResultMessage *res;
451 enum GNUNET_ARM_ProcessStatus status;
454 (ntohs (msg->size) != sizeof (struct GNUNET_ARM_ResultMessage)))
456 LOG (GNUNET_ERROR_TYPE_WARNING,
458 ("Error receiving response to `%s' request from ARM for service `%s'\n"),
459 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START) ? "START" : "STOP",
460 (const char *) &sc[1]);
461 GNUNET_CLIENT_disconnect (sc->h->client);
462 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
463 GNUNET_assert (NULL != sc->h->client);
464 if (sc->callback != NULL)
465 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
469 res = (const struct GNUNET_ARM_ResultMessage *) msg;
470 LOG (GNUNET_ERROR_TYPE_DEBUG,
471 "Received response from ARM for service `%s': %u\n",
472 (const char *) &sc[1], ntohs (msg->type));
473 status = (enum GNUNET_ARM_ProcessStatus) ntohl (res->status);
474 if (sc->callback != NULL)
475 sc->callback (sc->cls, status);
481 * Start or stop a service.
483 * @param h handle to ARM
484 * @param service_name name of the service
485 * @param timeout how long to wait before failing for good
486 * @param cb callback to invoke when service is ready
487 * @param cb_cls closure for callback
488 * @param type type of the request
491 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
492 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_Callback cb,
493 void *cb_cls, uint16_t type)
495 struct RequestContext *sctx;
497 struct GNUNET_MessageHeader *msg;
499 slen = strlen (service_name) + 1;
500 if (slen + sizeof (struct GNUNET_MessageHeader) >=
501 GNUNET_SERVER_MAX_MESSAGE_SIZE)
505 cb (cb_cls, GNUNET_NO);
508 LOG (GNUNET_ERROR_TYPE_DEBUG,
510 GNUNET_MESSAGE_TYPE_ARM_START) ?
511 _("Requesting start of service `%s'.\n") :
512 _("Requesting termination of service `%s'.\n"), service_name);
513 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
517 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
519 memcpy (&sctx[1], service_name, slen);
520 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
521 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
522 msg->type = htons (sctx->type);
523 memcpy (&msg[1], service_name, slen);
525 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, msg,
526 GNUNET_TIME_absolute_get_remaining
527 (sctx->timeout), GNUNET_YES,
528 &handle_response, sctx))
530 LOG (GNUNET_ERROR_TYPE_WARNING,
532 GNUNET_MESSAGE_TYPE_ARM_START)
533 ? _("Error while trying to transmit request to start `%s' to ARM\n")
534 : _("Error while trying to transmit request to stop `%s' to ARM\n"),
535 (const char *) &service_name);
537 cb (cb_cls, GNUNET_SYSERR);
549 * @param h handle to ARM
550 * @param service_name name of the service
551 * @param timeout how long to wait before failing for good
552 * @param cb callback to invoke when service is ready
553 * @param cb_cls closure for callback
556 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
557 const char *service_name,
558 struct GNUNET_TIME_Relative timeout,
559 GNUNET_ARM_Callback cb, void *cb_cls)
561 struct RequestContext *sctx;
562 struct GNUNET_CLIENT_Connection *client;
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
566 _("Asked to start service `%s' within %llu ms\n"), service_name,
567 (unsigned long long) timeout.rel_value);
568 if (0 == strcasecmp ("arm", service_name))
570 slen = strlen ("arm") + 1;
571 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
575 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
576 memcpy (&sctx[1], service_name, slen);
577 GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
581 if (h->client == NULL)
583 client = GNUNET_CLIENT_connect ("arm", h->cfg);
586 LOG (GNUNET_ERROR_TYPE_DEBUG,
587 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
588 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
591 LOG (GNUNET_ERROR_TYPE_DEBUG,
592 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
595 LOG (GNUNET_ERROR_TYPE_DEBUG, "arm_api, h->client non-NULL\n");
596 change_service (h, service_name, timeout, cb, cb_cls,
597 GNUNET_MESSAGE_TYPE_ARM_START);
602 * Callback from the arm stop service call, indicates that the arm service
603 * is well and truly dead, won't die, or an error occurred.
605 * @param cls closure for the callback
606 * @param reason reason for callback
609 arm_shutdown_callback (void *cls, enum GNUNET_ARM_ProcessStatus reason)
611 struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
613 if (arm_shutdown_ctx->cb != NULL)
614 arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
616 GNUNET_free (arm_shutdown_ctx);
623 * @param h handle to ARM
624 * @param service_name name of the service
625 * @param timeout how long to wait before failing for good
626 * @param cb callback to invoke when service is ready
627 * @param cb_cls closure for callback
630 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
631 const char *service_name,
632 struct GNUNET_TIME_Relative timeout,
633 GNUNET_ARM_Callback cb, void *cb_cls)
635 struct ARM_ShutdownContext *arm_shutdown_ctx;
636 struct GNUNET_CLIENT_Connection *client;
638 LOG (GNUNET_ERROR_TYPE_INFO, _("Stopping service `%s' within %llu ms\n"),
639 service_name, (unsigned long long) timeout.rel_value);
640 if (h->client == NULL)
642 client = GNUNET_CLIENT_connect ("arm", h->cfg);
645 cb (cb_cls, GNUNET_SYSERR);
650 if (0 == strcasecmp ("arm", service_name))
652 arm_shutdown_ctx = GNUNET_malloc (sizeof (struct ARM_ShutdownContext));
653 arm_shutdown_ctx->cb = cb;
654 arm_shutdown_ctx->cb_cls = cb_cls;
655 arm_service_shutdown (h->client, timeout, &arm_shutdown_callback,
660 change_service (h, service_name, timeout, cb, cb_cls,
661 GNUNET_MESSAGE_TYPE_ARM_STOP);
666 * Internal state for a list request with ARM.
668 struct ListRequestContext
672 * Pointer to our handle with ARM.
674 struct GNUNET_ARM_Handle *h;
677 * Function to call with a status code for the requested operation.
679 GNUNET_ARM_List_Callback callback;
682 * Closure for "callback".
687 * Timeout for the operation.
689 struct GNUNET_TIME_Absolute timeout;
694 * Process a response from ARM for the list request.
696 * @param cls the list request context
697 * @param msg the response
700 handle_list_response (void *cls, const struct GNUNET_MessageHeader *msg)
702 struct ListRequestContext *sc = cls;
703 const struct GNUNET_ARM_ListResultMessage *res;
711 LOG (GNUNET_ERROR_TYPE_WARNING,
712 "Error receiving response to LIST request from ARM\n");
713 GNUNET_CLIENT_disconnect (sc->h->client);
714 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
715 GNUNET_assert (NULL != sc->h->client);
716 if (sc->callback != NULL)
717 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
722 if (NULL == sc->callback)
728 msize = ntohs (msg->size);
729 if ( (msize < sizeof ( struct GNUNET_ARM_ListResultMessage)) ||
730 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT) )
733 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
738 res = (const struct GNUNET_ARM_ListResultMessage *) msg;
739 rcount = ntohs (res->count);
741 const char *list[rcount];
744 pos = (const char *)&res[1];
745 for (i=0; i<rcount; i++)
747 const char *end = memchr (pos, 0, msize - size_check);
751 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
756 size_check += (end - pos) + 1;
759 sc->callback (sc->cls, GNUNET_YES, rcount, list);
766 * List all running services.
768 * @param h handle to ARM
769 * @param timeout how long to wait before failing for good
770 * @param cb callback to invoke when service is ready
771 * @param cb_cls closure for callback
774 GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
775 struct GNUNET_TIME_Relative timeout,
776 GNUNET_ARM_List_Callback cb, void *cb_cls)
778 struct ListRequestContext *sctx;
779 struct GNUNET_MessageHeader msg;
780 struct GNUNET_CLIENT_Connection *client;
782 if (h->client == NULL)
784 client = GNUNET_CLIENT_connect ("arm", h->cfg);
787 LOG (GNUNET_ERROR_TYPE_DEBUG,
788 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
789 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
792 LOG (GNUNET_ERROR_TYPE_DEBUG,
793 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
797 sctx = GNUNET_malloc (sizeof (struct RequestContext));
801 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
802 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
803 msg.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
805 LOG (GNUNET_ERROR_TYPE_DEBUG,
806 "Requesting LIST from ARM service with timeout: %llu ms\n",
807 (unsigned long long)timeout.rel_value);
810 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
812 GNUNET_TIME_absolute_get_remaining
815 &handle_list_response,
818 LOG (GNUNET_ERROR_TYPE_WARNING,
819 "Error while trying to transmit request to list services to ARM\n");
821 cb (cb_cls, GNUNET_SYSERR, 0, NULL);
827 /* end of arm_api.c */