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;
57 * Context for handling the shutdown of a service.
59 struct ShutdownContext
62 * Connection to the service that is being shutdown.
64 struct GNUNET_CLIENT_Connection *sock;
67 * Time allowed for shutdown to happen.
69 struct GNUNET_TIME_Absolute timeout;
72 * Task set up to cancel the shutdown request on timeout.
74 GNUNET_SCHEDULER_TaskIdentifier cancel_task;
77 * Task to call once shutdown complete
79 GNUNET_CLIENT_ShutdownTask cont;
82 * Closure for shutdown continuation
87 * Result of the operation
89 enum GNUNET_ARM_ProcessStatus confirmed;
95 * Handler receiving response to service shutdown requests.
96 * First call with NULL: service misbehaving, or something.
97 * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
98 * - service will shutdown
99 * Second call with NULL:
100 * - service has now really shut down.
103 * @param msg NULL, indicating socket closure.
106 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
108 struct ShutdownContext *shutdown_ctx = cls;
109 const struct GNUNET_ARM_ResultMessage *rmsg;
113 if (shutdown_ctx->cont != NULL)
115 if (shutdown_ctx->confirmed == GNUNET_ARM_PROCESS_SHUTDOWN)
117 /* shutdown is now complete, as we waited for the network disconnect... */
118 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_DOWN);
122 /* communication error */
123 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
126 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
127 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
128 GNUNET_free (shutdown_ctx);
131 if (ntohs (msg->size) ==
132 sizeof (struct GNUNET_ARM_ResultMessage))
134 rmsg = (const struct GNUNET_ARM_ResultMessage*) msg;
135 shutdown_ctx->confirmed = (enum GNUNET_ARM_ProcessStatus) ntohl (rmsg->status);
136 if (shutdown_ctx->confirmed != GNUNET_ARM_PROCESS_SHUTDOWN)
138 /* ARM is not shutting down, well, report the error and be done with it... */
139 shutdown_ctx->cont (shutdown_ctx->cont_cls, shutdown_ctx->confirmed);
140 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
141 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
142 GNUNET_free (shutdown_ctx);
146 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
147 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
152 * Shutting down took too long, cancel receive and return error.
155 * @param tc context information (why was this task triggered now)
158 service_shutdown_cancel (void *cls,
159 const struct GNUNET_SCHEDULER_TaskContext *tc)
161 struct ShutdownContext *shutdown_ctx = cls;
163 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT);
164 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
165 GNUNET_free (shutdown_ctx);
170 * If possible, write a shutdown message to the target
171 * buffer and destroy the client connection.
173 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
174 * @param size number of bytes available in buf
175 * @param buf NULL on error, otherwise target buffer
176 * @return number of bytes written to buf
179 write_shutdown (void *cls, size_t size, void *buf)
181 struct GNUNET_MessageHeader *msg;
182 struct ShutdownContext *shutdown_ctx = cls;
184 if (size < sizeof (struct GNUNET_MessageHeader))
186 LOG (GNUNET_ERROR_TYPE_WARNING,
187 _("Failed to transmit shutdown request to client.\n"));
188 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
189 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
190 GNUNET_free (shutdown_ctx);
191 return 0; /* client disconnected */
194 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
195 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
196 shutdown_ctx->cancel_task =
197 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
198 (shutdown_ctx->timeout),
199 &service_shutdown_cancel, shutdown_ctx);
200 msg = (struct GNUNET_MessageHeader *) buf;
201 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
202 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
203 return sizeof (struct GNUNET_MessageHeader);
208 * Request that the service should shutdown.
209 * Afterwards, the connection will automatically be
210 * disconnected. Hence the "sock" should not
211 * be used by the caller after this call
212 * (calling this function frees "sock" after a while).
214 * @param sock the socket connected to the service
215 * @param timeout how long to wait before giving up on transmission
216 * @param cont continuation to call once the service is really down
217 * @param cont_cls closure for continuation
221 arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
222 struct GNUNET_TIME_Relative timeout,
223 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
225 struct ShutdownContext *shutdown_ctx;
227 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
228 shutdown_ctx->cont = cont;
229 shutdown_ctx->cont_cls = cont_cls;
230 shutdown_ctx->sock = sock;
231 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
232 shutdown_ctx->confirmed = GNUNET_ARM_PROCESS_COMMUNICATION_ERROR;
233 /* FIXME: store return value? */
234 GNUNET_CLIENT_notify_transmit_ready (sock,
235 sizeof (struct GNUNET_MessageHeader),
236 timeout, GNUNET_YES, &write_shutdown,
242 * Setup a context for communicating with ARM. Note that this
243 * can be done even if the ARM service is not yet running.
245 * @param cfg configuration to use (needed to contact ARM;
246 * the ARM service may internally use a different
247 * configuration to determine how to start the service).
248 * @param service service that *this* process is implementing/providing, can be NULL
249 * @return context to use for further ARM operations, NULL on error
251 struct GNUNET_ARM_Handle *
252 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
255 struct GNUNET_ARM_Handle *ret;
257 ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
258 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
264 * Disconnect from the ARM service.
266 * @param h the handle that was being used
269 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
271 if (h->client != NULL)
272 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
273 GNUNET_CONFIGURATION_destroy (h->cfg);
278 struct ARM_ShutdownContext
281 * Callback to call once shutdown complete.
283 GNUNET_ARM_Callback cb;
286 * Closure for callback.
293 * Internal state for a request with ARM.
295 struct RequestContext
299 * Pointer to our handle with ARM.
301 struct GNUNET_ARM_Handle *h;
304 * Function to call with a status code for the requested operation.
306 GNUNET_ARM_Callback callback;
309 * Closure for "callback".
314 * Timeout for the operation.
316 struct GNUNET_TIME_Absolute timeout;
319 * Type of the request expressed as a message type (start or stop).
325 #include "do_start_process.c"
329 * A client specifically requested starting of ARM itself.
330 * This function is called with information about whether
331 * or not ARM is running; if it is, report success. If
332 * it is not, start the ARM process.
334 * @param cls the context for the request that we will report on (struct RequestContext*)
335 * @param tc why were we called (reason says if ARM is running)
338 arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
340 struct RequestContext *pos = cls;
341 struct GNUNET_OS_Process *proc;
347 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
350 LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
351 "gnunet-service-arm");
353 /* arm is running! */
354 if (pos->callback != NULL)
355 pos->callback (pos->cls, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
360 LOG (GNUNET_ERROR_TYPE_DEBUG,
361 "Looks like `%s' is not running, will start it.\n",
362 "gnunet-service-arm");
365 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "PREFIX",
367 loprefix = GNUNET_strdup ("");
369 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "OPTIONS",
371 lopostfix = GNUNET_strdup ("");
373 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "BINARY",
376 LOG (GNUNET_ERROR_TYPE_WARNING,
378 ("Configuration failes to specify option `%s' in section `%s'!\n"),
380 if (pos->callback != NULL)
381 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
383 GNUNET_free (loprefix);
384 GNUNET_free (lopostfix);
388 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg, "arm", "CONFIG",
391 LOG (GNUNET_ERROR_TYPE_WARNING,
392 _("Configuration fails to specify option `%s' in section `%s'!\n"),
394 if (pos->callback != NULL)
395 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
396 GNUNET_free (binary);
398 GNUNET_free (loprefix);
399 GNUNET_free (lopostfix);
403 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING", "WEAKRANDOM"))
405 GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg, "TESTING",
408 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING",
411 /* Means we are ONLY running locally */
412 /* we're clearly running a test, don't daemonize */
413 proc = do_start_process (NULL, loprefix, binary, "-c", config,
417 /* no daemonization! */
422 proc = do_start_process (NULL, loprefix, binary, "-c", config,
426 "-d", lopostfix, NULL);
428 GNUNET_free (binary);
429 GNUNET_free (config);
430 GNUNET_free (loprefix);
431 GNUNET_free (lopostfix);
434 if (pos->callback != NULL)
435 pos->callback (pos->cls, GNUNET_ARM_PROCESS_FAILURE);
439 if (pos->callback != NULL)
440 pos->callback (pos->cls, GNUNET_ARM_PROCESS_STARTING);
447 * Process a response from ARM to a request for a change in service
450 * @param cls the request context
451 * @param msg the response
454 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
456 struct RequestContext *sc = cls;
457 const struct GNUNET_ARM_ResultMessage *res;
458 enum GNUNET_ARM_ProcessStatus status;
461 (ntohs (msg->size) != sizeof (struct GNUNET_ARM_ResultMessage)))
463 LOG (GNUNET_ERROR_TYPE_WARNING,
465 ("Error receiving response to `%s' request from ARM for service `%s'\n"),
466 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START) ? "START" : "STOP",
467 (const char *) &sc[1]);
468 GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
469 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
470 GNUNET_assert (NULL != sc->h->client);
471 if (sc->callback != NULL)
472 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
476 res = (const struct GNUNET_ARM_ResultMessage *) msg;
478 LOG (GNUNET_ERROR_TYPE_DEBUG,
479 "Received response from ARM for service `%s': %u\n",
480 (const char *) &sc[1], ntohs (msg->type));
482 status = (enum GNUNET_ARM_ProcessStatus) ntohl (res->status);
483 if (sc->callback != NULL)
484 sc->callback (sc->cls, status);
490 * Start or stop a service.
492 * @param h handle to ARM
493 * @param service_name name of the service
494 * @param timeout how long to wait before failing for good
495 * @param cb callback to invoke when service is ready
496 * @param cb_cls closure for callback
497 * @param type type of the request
500 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
501 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_Callback cb,
502 void *cb_cls, uint16_t type)
504 struct RequestContext *sctx;
506 struct GNUNET_MessageHeader *msg;
508 slen = strlen (service_name) + 1;
509 if (slen + sizeof (struct GNUNET_MessageHeader) >=
510 GNUNET_SERVER_MAX_MESSAGE_SIZE)
514 cb (cb_cls, GNUNET_NO);
518 LOG (GNUNET_ERROR_TYPE_DEBUG,
520 GNUNET_MESSAGE_TYPE_ARM_START) ?
521 _("Requesting start of service `%s'.\n") :
522 _("Requesting termination of service `%s'.\n"), service_name);
524 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
528 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
530 memcpy (&sctx[1], service_name, slen);
531 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
532 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
533 msg->type = htons (sctx->type);
534 memcpy (&msg[1], service_name, slen);
536 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, msg,
537 GNUNET_TIME_absolute_get_remaining
538 (sctx->timeout), GNUNET_YES,
539 &handle_response, sctx))
541 LOG (GNUNET_ERROR_TYPE_WARNING,
543 GNUNET_MESSAGE_TYPE_ARM_START) ?
544 _("Error while trying to transmit request to start `%s' to ARM\n")
546 _("Error while trying to transmit request to stop `%s' to ARM\n"),
547 (const char *) &service_name);
549 cb (cb_cls, GNUNET_SYSERR);
561 * @param h handle to ARM
562 * @param service_name name of the service
563 * @param timeout how long to wait before failing for good
564 * @param cb callback to invoke when service is ready
565 * @param cb_cls closure for callback
568 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
569 const char *service_name,
570 struct GNUNET_TIME_Relative timeout,
571 GNUNET_ARM_Callback cb, void *cb_cls)
573 struct RequestContext *sctx;
574 struct GNUNET_CLIENT_Connection *client;
578 LOG (GNUNET_ERROR_TYPE_DEBUG,
579 _("Asked to start service `%s' within %llu ms\n"), service_name,
580 (unsigned long long) timeout.rel_value);
582 if (0 == strcasecmp ("arm", service_name))
584 slen = strlen ("arm") + 1;
585 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
589 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
590 memcpy (&sctx[1], service_name, slen);
591 GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
595 if (h->client == NULL)
597 client = GNUNET_CLIENT_connect ("arm", h->cfg);
600 LOG (GNUNET_ERROR_TYPE_DEBUG,
601 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
602 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
605 LOG (GNUNET_ERROR_TYPE_DEBUG,
606 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
609 LOG (GNUNET_ERROR_TYPE_DEBUG, "arm_api, h->client non-NULL\n");
610 change_service (h, service_name, timeout, cb, cb_cls,
611 GNUNET_MESSAGE_TYPE_ARM_START);
616 * Callback from the arm stop service call, indicates that the arm service
617 * is well and truly dead, won't die, or an error occurred.
619 * @param cls closure for the callback
620 * @param reason reason for callback
623 arm_shutdown_callback (void *cls, enum GNUNET_ARM_ProcessStatus reason)
625 struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
627 if (arm_shutdown_ctx->cb != NULL)
628 arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
630 GNUNET_free (arm_shutdown_ctx);
637 * @param h handle to ARM
638 * @param service_name name of the service
639 * @param timeout how long to wait before failing for good
640 * @param cb callback to invoke when service is ready
641 * @param cb_cls closure for callback
644 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
645 const char *service_name,
646 struct GNUNET_TIME_Relative timeout,
647 GNUNET_ARM_Callback cb, void *cb_cls)
649 struct ARM_ShutdownContext *arm_shutdown_ctx;
650 struct GNUNET_CLIENT_Connection *client;
652 LOG (GNUNET_ERROR_TYPE_INFO, _("Stopping service `%s' within %llu ms\n"),
653 service_name, (unsigned long long) timeout.rel_value);
654 if (h->client == NULL)
656 client = GNUNET_CLIENT_connect ("arm", h->cfg);
659 cb (cb_cls, GNUNET_SYSERR);
664 if (0 == strcasecmp ("arm", service_name))
666 arm_shutdown_ctx = GNUNET_malloc (sizeof (struct ARM_ShutdownContext));
667 arm_shutdown_ctx->cb = cb;
668 arm_shutdown_ctx->cb_cls = cb_cls;
669 arm_service_shutdown (h->client, timeout, &arm_shutdown_callback,
674 change_service (h, service_name, timeout, cb, cb_cls,
675 GNUNET_MESSAGE_TYPE_ARM_STOP);
679 /* end of arm_api.c */