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_util_lib.h"
29 #include "gnunet_protocols.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "arm-api",__VA_ARGS__)
35 * Handle for interacting with ARM.
37 struct GNUNET_ARM_Handle
41 * Our connection to the ARM service.
43 struct GNUNET_CLIENT_Connection *client;
46 * The configuration that we are using.
48 struct GNUNET_CONFIGURATION_Handle *cfg;
53 * Context for handling the shutdown of a service.
55 struct ShutdownContext
58 * Connection to the service that is being shutdown.
60 struct GNUNET_CLIENT_Connection *sock;
63 * Time allowed for shutdown to happen.
65 struct GNUNET_TIME_Absolute timeout;
68 * Task set up to cancel the shutdown request on timeout.
70 GNUNET_SCHEDULER_TaskIdentifier cancel_task;
73 * Task to call once shutdown complete
75 GNUNET_CLIENT_ShutdownTask cont;
78 * Closure for shutdown continuation
83 * Handle for transmission request.
85 struct GNUNET_CLIENT_TransmitHandle *th;
91 * Handler receiving response to service shutdown requests.
92 * First call with NULL: service misbehaving, or something.
93 * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
94 * - service will shutdown
95 * Second call with NULL:
96 * - service has now really shut down.
99 * @param msg NULL, indicating socket closure.
102 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
104 struct ShutdownContext *shutdown_ctx = cls;
108 /* We just expected a disconnect! Report the error and be done with it... */
110 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
111 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
112 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
113 GNUNET_free (shutdown_ctx);
116 if (NULL != shutdown_ctx->cont)
117 /* shutdown is now complete, as we waited for the network disconnect... */
118 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_DOWN);
119 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
120 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
121 GNUNET_free (shutdown_ctx);
126 * Shutting down took too long, cancel receive and return error.
129 * @param tc context information (why was this task triggered now)
132 service_shutdown_cancel (void *cls,
133 const struct GNUNET_SCHEDULER_TaskContext *tc)
135 struct ShutdownContext *shutdown_ctx = cls;
137 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT);
138 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
139 GNUNET_free (shutdown_ctx);
144 * If possible, write a shutdown message to the target
145 * buffer and destroy the client connection.
147 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
148 * @param size number of bytes available in buf
149 * @param buf NULL on error, otherwise target buffer
150 * @return number of bytes written to buf
153 write_shutdown (void *cls, size_t size, void *buf)
155 struct ShutdownContext *shutdown_ctx = cls;
156 struct GNUNET_MessageHeader *msg;
158 shutdown_ctx->th = NULL;
159 if (size < sizeof (struct GNUNET_MessageHeader))
161 LOG (GNUNET_ERROR_TYPE_WARNING,
162 _("Failed to transmit shutdown request to client.\n"));
163 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
164 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
165 GNUNET_free (shutdown_ctx);
166 return 0; /* client disconnected */
168 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
169 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
170 shutdown_ctx->cancel_task =
171 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
172 (shutdown_ctx->timeout),
173 &service_shutdown_cancel, shutdown_ctx);
174 msg = (struct GNUNET_MessageHeader *) buf;
175 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
176 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
177 return sizeof (struct GNUNET_MessageHeader);
182 * Request that the service should shutdown.
183 * Afterwards, the connection will automatically be
184 * disconnected. Hence the "sock" should not
185 * be used by the caller after this call
186 * (calling this function frees "sock" after a while).
188 * @param sock the socket connected to the service
189 * @param timeout how long to wait before giving up on transmission
190 * @param cont continuation to call once the service is really down
191 * @param cont_cls closure for continuation
195 arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
196 struct GNUNET_TIME_Relative timeout,
197 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
199 struct ShutdownContext *shutdown_ctx;
201 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
202 shutdown_ctx->cont = cont;
203 shutdown_ctx->cont_cls = cont_cls;
204 shutdown_ctx->sock = sock;
205 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
206 shutdown_ctx->th = GNUNET_CLIENT_notify_transmit_ready (sock,
207 sizeof (struct GNUNET_MessageHeader),
208 timeout, GNUNET_NO, &write_shutdown,
214 * Setup a context for communicating with ARM. Note that this
215 * can be done even if the ARM service is not yet running.
217 * @param cfg configuration to use (needed to contact ARM;
218 * the ARM service may internally use a different
219 * configuration to determine how to start the service).
220 * @param service service that *this* process is implementing/providing, can be NULL
221 * @return context to use for further ARM operations, NULL on error
223 struct GNUNET_ARM_Handle *
224 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
227 struct GNUNET_ARM_Handle *ret;
229 ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
230 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
236 * Disconnect from the ARM service.
238 * @param h the handle that was being used
241 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
243 if (h->client != NULL)
244 GNUNET_CLIENT_disconnect (h->client);
245 GNUNET_CONFIGURATION_destroy (h->cfg);
250 struct ARM_ShutdownContext
253 * Callback to call once shutdown complete.
255 GNUNET_ARM_Callback cb;
258 * Closure for callback.
265 * Internal state for a request with ARM.
267 struct RequestContext
271 * Pointer to our handle with ARM.
273 struct GNUNET_ARM_Handle *h;
276 * Function to call with a status code for the requested operation.
278 GNUNET_ARM_Callback callback;
281 * Closure for "callback".
286 * Timeout for the operation.
288 struct GNUNET_TIME_Absolute timeout;
291 * Type of the request expressed as a message type (start or stop).
296 * Flags for passing std descriptors to ARM (when starting ARM).
298 enum GNUNET_OS_InheritStdioFlags std_inheritance;
302 #include "do_start_process.c"
306 * A client specifically requested starting of ARM itself.
307 * This function is called with information about whether
308 * or not ARM is running; if it is, report success. If
309 * it is not, start the ARM process.
311 * @param cls the context for the request that we will report on (struct RequestContext*)
312 * @param tc why were we called (reason says if ARM is running)
315 arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
317 struct RequestContext *pos = cls;
318 struct GNUNET_OS_Process *proc;
324 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
326 LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
327 "gnunet-service-arm");
328 /* arm is running! */
329 if (pos->callback != NULL)
330 pos->callback (pos->cls, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
334 LOG (GNUNET_ERROR_TYPE_DEBUG,
335 "Looks like `%s' is not running, will start it.\n",
336 "gnunet-service-arm");
338 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "PREFIX",
340 loprefix = GNUNET_strdup ("");
342 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "OPTIONS",
344 lopostfix = GNUNET_strdup ("");
346 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "BINARY",
349 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
351 if (pos->callback != NULL)
352 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
354 GNUNET_free (loprefix);
355 GNUNET_free (lopostfix);
359 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg, "arm", "CONFIG",
362 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
364 if (pos->callback != NULL)
365 pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
366 GNUNET_free (binary);
368 GNUNET_free (loprefix);
369 GNUNET_free (lopostfix);
373 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING", "WEAKRANDOM"))
375 GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg, "TESTING",
378 GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING",
381 /* Means we are ONLY running locally */
382 /* we're clearly running a test, don't daemonize */
383 proc = do_start_process (GNUNET_NO, pos->std_inheritance,
384 NULL, loprefix, binary, "-c", config,
385 /* no daemonization! */
390 proc = do_start_process (GNUNET_NO, pos->std_inheritance,
391 NULL, loprefix, binary, "-c", config,
392 "-d", lopostfix, NULL);
394 GNUNET_free (binary);
395 GNUNET_free (config);
396 GNUNET_free (loprefix);
397 GNUNET_free (lopostfix);
400 if (pos->callback != NULL)
401 pos->callback (pos->cls, GNUNET_ARM_PROCESS_FAILURE);
405 if (pos->callback != NULL)
406 pos->callback (pos->cls, GNUNET_ARM_PROCESS_STARTING);
413 * Process a response from ARM to a request for a change in service
416 * @param cls the request context
417 * @param msg the response
420 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
422 struct RequestContext *sc = cls;
423 const struct GNUNET_ARM_ResultMessage *res;
424 enum GNUNET_ARM_ProcessStatus status;
427 (ntohs (msg->size) != sizeof (struct GNUNET_ARM_ResultMessage)))
430 GNUNET_CLIENT_disconnect (sc->h->client);
431 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
432 GNUNET_assert (NULL != sc->h->client);
433 if (sc->callback != NULL)
434 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
438 res = (const struct GNUNET_ARM_ResultMessage *) msg;
439 LOG (GNUNET_ERROR_TYPE_DEBUG,
440 "Received response from ARM for service `%s': %u\n",
441 (const char *) &sc[1], ntohs (msg->type));
442 status = (enum GNUNET_ARM_ProcessStatus) ntohl (res->status);
443 if (sc->callback != NULL)
444 sc->callback (sc->cls, status);
450 * Start or stop a service.
452 * @param h handle to ARM
453 * @param service_name name of the service
454 * @param timeout how long to wait before failing for good
455 * @param cb callback to invoke when service is ready
456 * @param cb_cls closure for callback
457 * @param type type of the request
460 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
461 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_Callback cb,
462 void *cb_cls, uint16_t type)
464 struct RequestContext *sctx;
466 struct GNUNET_MessageHeader *msg;
468 slen = strlen (service_name) + 1;
469 if (slen + sizeof (struct GNUNET_MessageHeader) >=
470 GNUNET_SERVER_MAX_MESSAGE_SIZE)
474 cb (cb_cls, GNUNET_NO);
477 LOG (GNUNET_ERROR_TYPE_DEBUG,
479 GNUNET_MESSAGE_TYPE_ARM_START) ?
480 "Requesting start of service `%s'.\n" :
481 "Requesting termination of service `%s'.\n", service_name);
482 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
486 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
488 memcpy (&sctx[1], service_name, slen);
489 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
490 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
491 msg->type = htons (sctx->type);
492 memcpy (&msg[1], service_name, slen);
494 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, msg,
495 GNUNET_TIME_absolute_get_remaining
496 (sctx->timeout), GNUNET_YES,
497 &handle_response, sctx))
501 cb (cb_cls, GNUNET_SYSERR);
513 * @param h handle to ARM
514 * @param service_name name of the service
515 * @param std_inheritance inheritance of std streams
516 * @param timeout how long to wait before failing for good
517 * @param cb callback to invoke when service is ready
518 * @param cb_cls closure for callback
521 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
522 const char *service_name,
523 enum GNUNET_OS_InheritStdioFlags std_inheritance,
524 struct GNUNET_TIME_Relative timeout,
525 GNUNET_ARM_Callback cb, void *cb_cls)
527 struct RequestContext *sctx;
528 struct GNUNET_CLIENT_Connection *client;
531 LOG (GNUNET_ERROR_TYPE_DEBUG,
532 "Asked to start service `%s' within %s\n", service_name,
533 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
534 if (0 == strcasecmp ("arm", service_name))
536 slen = strlen ("arm") + 1;
537 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
541 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
542 sctx->std_inheritance = std_inheritance;
543 memcpy (&sctx[1], service_name, slen);
544 GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
548 if (NULL == h->client)
550 client = GNUNET_CLIENT_connect ("arm", h->cfg);
553 LOG (GNUNET_ERROR_TYPE_DEBUG,
554 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
555 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
558 LOG (GNUNET_ERROR_TYPE_DEBUG,
559 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
562 LOG (GNUNET_ERROR_TYPE_DEBUG, "arm_api, h->client non-NULL\n");
563 change_service (h, service_name, timeout, cb, cb_cls,
564 GNUNET_MESSAGE_TYPE_ARM_START);
569 * Callback from the arm stop service call, indicates that the arm service
570 * is well and truly dead, won't die, or an error occurred.
572 * @param cls closure for the callback
573 * @param reason reason for callback
576 arm_shutdown_callback (void *cls, enum GNUNET_ARM_ProcessStatus reason)
578 struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
580 if (arm_shutdown_ctx->cb != NULL)
581 arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
582 GNUNET_free (arm_shutdown_ctx);
589 * @param h handle to ARM
590 * @param service_name name of the service
591 * @param timeout how long to wait before failing for good
592 * @param cb callback to invoke when service is ready
593 * @param cb_cls closure for callback
596 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
597 const char *service_name,
598 struct GNUNET_TIME_Relative timeout,
599 GNUNET_ARM_Callback cb, void *cb_cls)
601 struct ARM_ShutdownContext *arm_shutdown_ctx;
602 struct GNUNET_CLIENT_Connection *client;
604 LOG (GNUNET_ERROR_TYPE_DEBUG,
605 "Stopping service `%s' within %s\n",
607 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
608 if (h->client == NULL)
610 client = GNUNET_CLIENT_connect ("arm", h->cfg);
613 cb (cb_cls, GNUNET_SYSERR);
618 if (0 == strcasecmp ("arm", service_name))
620 arm_shutdown_ctx = GNUNET_malloc (sizeof (struct ARM_ShutdownContext));
621 arm_shutdown_ctx->cb = cb;
622 arm_shutdown_ctx->cb_cls = cb_cls;
623 arm_service_shutdown (h->client, timeout, &arm_shutdown_callback,
628 change_service (h, service_name, timeout, cb, cb_cls,
629 GNUNET_MESSAGE_TYPE_ARM_STOP);
634 * Internal state for a list request with ARM.
636 struct ListRequestContext
640 * Pointer to our handle with ARM.
642 struct GNUNET_ARM_Handle *h;
645 * Function to call with a status code for the requested operation.
647 GNUNET_ARM_List_Callback callback;
650 * Closure for "callback".
655 * Timeout for the operation.
657 struct GNUNET_TIME_Absolute timeout;
662 * Process a response from ARM for the list request.
664 * @param cls the list request context
665 * @param msg the response
668 handle_list_response (void *cls, const struct GNUNET_MessageHeader *msg)
670 struct ListRequestContext *sc = cls;
671 const struct GNUNET_ARM_ListResultMessage *res;
680 GNUNET_CLIENT_disconnect (sc->h->client);
681 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
682 GNUNET_assert (NULL != sc->h->client);
683 if (sc->callback != NULL)
684 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
689 if (NULL == sc->callback)
695 msize = ntohs (msg->size);
696 if ( (msize < sizeof ( struct GNUNET_ARM_ListResultMessage)) ||
697 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT) )
700 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
705 res = (const struct GNUNET_ARM_ListResultMessage *) msg;
706 rcount = ntohs (res->count);
708 const char *list[rcount];
711 pos = (const char *)&res[1];
712 for (i=0; i<rcount; i++)
714 const char *end = memchr (pos, 0, msize - size_check);
718 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
723 size_check += (end - pos) + 1;
726 sc->callback (sc->cls, GNUNET_YES, rcount, list);
733 * List all running services.
735 * @param h handle to ARM
736 * @param timeout how long to wait before failing for good
737 * @param cb callback to invoke when service is ready
738 * @param cb_cls closure for callback
741 GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
742 struct GNUNET_TIME_Relative timeout,
743 GNUNET_ARM_List_Callback cb, void *cb_cls)
745 struct ListRequestContext *sctx;
746 struct GNUNET_MessageHeader msg;
747 struct GNUNET_CLIENT_Connection *client;
749 if (h->client == NULL)
751 client = GNUNET_CLIENT_connect ("arm", h->cfg);
755 cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
761 sctx = GNUNET_malloc (sizeof (struct RequestContext));
765 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
766 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
767 msg.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
769 LOG (GNUNET_ERROR_TYPE_DEBUG,
770 "Requesting LIST from ARM service with timeout: %s\n",
771 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
774 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
776 GNUNET_TIME_absolute_get_remaining
779 &handle_list_response,
784 cb (cb_cls, GNUNET_SYSERR, 0, NULL);
790 /* end of arm_api.c */