2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 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.
22 * @file arm/gnunet-service-arm.c
23 * @brief the automated restart manager service
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_protocols.h"
33 * Threshold after which exponential backoff shouldn't increase (in ms); 30m
35 #define EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
39 * List of our services.
45 * Record with information about a listen socket we have open.
47 struct ServiceListeningInfo
50 * This is a linked list.
52 struct ServiceListeningInfo *next;
55 * This is a linked list.
57 struct ServiceListeningInfo *prev;
60 * Address this socket is listening on.
62 struct sockaddr *service_addr;
65 * Service this listen socket is for.
67 struct ServiceList *sl;
70 * Number of bytes in 'service_addr'
72 socklen_t service_addr_len;
75 * Our listening socket.
77 struct GNUNET_NETWORK_Handle *listen_socket;
80 * Task doing the accepting.
82 GNUNET_SCHEDULER_TaskIdentifier accept_task;
88 * List of our services.
93 * This is a doubly-linked list.
95 struct ServiceList *next;
98 * This is a doubly-linked list.
100 struct ServiceList *prev;
103 * Linked list of listen sockets associated with this service.
105 struct ServiceListeningInfo *listen_head;
108 * Linked list of listen sockets associated with this service.
110 struct ServiceListeningInfo *listen_tail;
113 * Name of the service.
118 * Name of the binary used.
123 * Name of the configuration file used.
128 * Client to notify upon kill completion (waitpid), NULL
129 * if we should simply restart the process.
131 struct GNUNET_SERVER_Client *killing_client;
134 * Process structure pointer of the child.
136 struct GNUNET_OS_Process *proc;
139 * Process exponential backoff time
141 struct GNUNET_TIME_Relative backoff;
144 * Absolute time at which the process is scheduled to restart in case of death
146 struct GNUNET_TIME_Absolute restart_at;
149 * Is this service to be started by default (or did a client tell us explicitly
150 * to start it)? GNUNET_NO if the service is started only upon 'accept' on a
151 * listen socket or possibly explicitly by a client changing the value.
156 * Should we use pipes to signal this process? (YES for Java binaries and if we
163 * List of running services.
165 static struct ServiceList *running_head;
168 * List of running services.
170 static struct ServiceList *running_tail;
175 static const struct GNUNET_CONFIGURATION_Handle *cfg;
178 * Command to prepend to each actual command.
180 static char *prefix_command;
183 * Option to append to each actual command.
185 static char *final_option;
188 * ID of task called whenever we get a SIGCHILD.
190 static GNUNET_SCHEDULER_TaskIdentifier child_death_task;
193 * ID of task called whenever the timeout for restarting a child
196 static GNUNET_SCHEDULER_TaskIdentifier child_restart_task;
199 * Pipe used to communicate shutdown via signal.
201 static struct GNUNET_DISK_PipeHandle *sigpipe;
204 * Are we in shutdown mode?
206 static int in_shutdown;
209 * Handle to our server instance. Our server is a bit special in that
210 * its service is not immediately stopped once we get a shutdown
211 * request (since we need to continue service until all of our child
212 * processes are dead). This handle is used to shut down the server
213 * (and thus trigger process termination) once all child processes are
214 * also dead. A special option in the ARM configuration modifies the
215 * behaviour of the service implementation to not do the shutdown
218 static struct GNUNET_SERVER_Handle *server;
221 #include "do_start_process.c"
225 * Actually start the process for the given service.
227 * @param sl identifies service to start
230 start_process (struct ServiceList *sl)
240 struct ServiceListeningInfo *sli;
244 /* calculate listen socket list */
247 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
249 GNUNET_array_append (lsocks, ls,
250 GNUNET_NETWORK_get_fd (sli->listen_socket));
251 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
253 GNUNET_SCHEDULER_cancel (sli->accept_task);
254 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
258 GNUNET_array_append (lsocks, ls, INVALID_SOCKET);
260 GNUNET_array_append (lsocks, ls, -1);
263 /* obtain configuration */
265 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "PREFIX",
267 loprefix = GNUNET_strdup (prefix_command);
269 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "OPTIONS",
272 options = GNUNET_strdup (final_option);
273 if (NULL == strstr (options, "%"))
275 /* replace '{}' with service name */
276 while (NULL != (optpos = strstr (options, "{}")))
280 GNUNET_asprintf (&optpos, options, sl->name);
281 GNUNET_free (options);
284 /* replace '$PATH' with value associated with "PATH" */
285 while (NULL != (optpos = strstr (options, "$")))
288 while (isupper ((unsigned char) *optend))
297 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
299 val = GNUNET_strdup ("");
301 GNUNET_asprintf (&optpos, "%s%s%c%s", options, val, b, next);
302 GNUNET_free (options);
308 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
310 /* actually start process */
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 "Starting service `%s' using binary `%s' and configuration `%s'\n",
314 sl->name, sl->binary, sl->config);
316 GNUNET_assert (NULL == sl->proc);
317 if (GNUNET_YES == use_debug)
319 do_start_process (sl->pipe_control,
320 lsocks, loprefix, sl->binary, "-c", sl->config, "-L",
321 "DEBUG", options, NULL);
324 do_start_process (sl->pipe_control,
325 lsocks, loprefix, sl->binary, "-c", sl->config,
327 if (sl->proc == NULL)
328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to start service `%s'\n"),
331 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting service `%s'\n"),
334 GNUNET_free (loprefix);
335 GNUNET_free (options);
336 GNUNET_array_grow (lsocks, ls, 0);
341 * Transmit a status result message.
343 * @param cls pointer to "unit16_t*" with message type
344 * @param size number of bytes available in buf
345 * @param buf where to copy the message, NULL on error
346 * @return number of bytes copied to buf
349 write_result (void *cls, size_t size, void *buf)
351 enum GNUNET_ARM_ProcessStatus *res = cls;
352 struct GNUNET_ARM_ResultMessage *msg;
356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
357 _("Could not send status result to client\n"));
358 return 0; /* error, not much we can do */
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Sending status response %u to client\n", (unsigned int) *res);
364 GNUNET_assert (size >= sizeof (struct GNUNET_ARM_ResultMessage));
366 msg->header.size = htons (sizeof (struct GNUNET_ARM_ResultMessage));
367 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_RESULT);
368 msg->status = htonl ((uint32_t) (*res));
370 return sizeof (struct GNUNET_ARM_ResultMessage);
375 * Signal our client that we will start or stop the
378 * @param client who is being signalled
379 * @param name name of the service
380 * @param result message type to send
381 * @return NULL if it was not found
384 signal_result (struct GNUNET_SERVER_Client *client, const char *name,
385 enum GNUNET_ARM_ProcessStatus result)
387 enum GNUNET_ARM_ProcessStatus *res;
391 /* FIXME: this is not super-clean yet... */
392 res = GNUNET_malloc (sizeof (enum GNUNET_ARM_ProcessStatus));
394 GNUNET_SERVER_notify_transmit_ready (client,
396 GNUNET_ARM_ResultMessage),
397 GNUNET_TIME_UNIT_FOREVER_REL,
399 GNUNET_SERVER_receive_done (client, GNUNET_OK);
404 * Find the process with the given service
405 * name in the given list and return it.
407 * @param name which service entry to look up
408 * @return NULL if it was not found
410 static struct ServiceList *
411 find_service (const char *name)
413 struct ServiceList *sl;
418 if (0 == strcasecmp (sl->name, name))
427 * First connection has come to the listening socket associated with the service,
428 * create the service in order to relay the incoming connection to it
430 * @param cls callback data, struct ServiceListeningInfo describing a listen socket
434 accept_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
436 struct ServiceListeningInfo *sli = cls;
437 struct ServiceList *sl = sli->sl;
439 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
440 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
447 * Creating a listening socket for each of the service's addresses and
448 * wait for the first incoming connection to it
450 * @param sa address associated with the service
451 * @param addr_len length of sa
452 * @param sl service entry for the service in question
455 create_listen_socket (struct sockaddr *sa, socklen_t addr_len,
456 struct ServiceList *sl)
458 const static int on = 1;
459 struct GNUNET_NETWORK_Handle *sock;
460 struct ServiceListeningInfo *sli;
462 switch (sa->sa_family)
465 sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
468 sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
471 if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */
473 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
478 errno = EAFNOSUPPORT;
483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
484 _("Unable to create socket for service `%s': %s\n"),
485 sl->name, STRERROR (errno));
489 if (GNUNET_NETWORK_socket_setsockopt
490 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
491 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
494 if ((sa->sa_family == AF_INET6) &&
495 (GNUNET_NETWORK_socket_setsockopt
496 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
497 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
501 if (GNUNET_NETWORK_socket_bind
502 (sock, (const struct sockaddr *) sa, addr_len) != GNUNET_OK)
504 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
506 ("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
507 sl->name, GNUNET_a2s (sa, addr_len), STRERROR (errno));
508 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
512 if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
514 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
515 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
519 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
520 _("ARM now monitors connections to service `%s' at `%s'\n"),
521 sl->name, GNUNET_a2s (sa, addr_len));
522 sli = GNUNET_malloc (sizeof (struct ServiceListeningInfo));
523 sli->service_addr = sa;
524 sli->service_addr_len = addr_len;
525 sli->listen_socket = sock;
528 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock,
529 &accept_connection, sli);
530 GNUNET_CONTAINER_DLL_insert (sl->listen_head, sl->listen_tail, sli);
535 * Remove and free an entry in the service list. Listen sockets
536 * must have already been cleaned up. Only to be called during shutdown.
538 * @param sl entry to free
541 free_service (struct ServiceList *sl)
543 GNUNET_assert (GNUNET_YES == in_shutdown);
544 GNUNET_CONTAINER_DLL_remove (running_head, running_tail, sl);
545 GNUNET_assert (NULL == sl->listen_head);
546 GNUNET_free_non_null (sl->config);
547 GNUNET_free_non_null (sl->binary);
548 GNUNET_free (sl->name);
554 * Handle START-message.
556 * @param cls closure (always NULL)
557 * @param client identification of the client
558 * @param message the actual message
559 * @return GNUNET_OK to keep the connection open,
560 * GNUNET_SYSERR to close it (signal serious error)
563 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
564 const struct GNUNET_MessageHeader *message)
566 const char *servicename;
567 struct ServiceList *sl;
570 size = ntohs (message->size);
571 size -= sizeof (struct GNUNET_MessageHeader);
572 servicename = (const char *) &message[1];
573 if ((size == 0) || (servicename[size - 1] != '\0'))
576 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
579 if (GNUNET_YES == in_shutdown)
581 signal_result (client, servicename, GNUNET_ARM_PROCESS_SHUTDOWN);
584 sl = find_service (servicename);
587 signal_result (client, servicename, GNUNET_ARM_PROCESS_UNKNOWN);
590 sl->is_default = GNUNET_YES;
591 if (sl->proc != NULL)
593 signal_result (client, servicename, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
597 signal_result (client, servicename, GNUNET_ARM_PROCESS_STARTING);
602 * Handle STOP-message.
604 * @param cls closure (always NULL)
605 * @param client identification of the client
606 * @param message the actual message
607 * @return GNUNET_OK to keep the connection open,
608 * GNUNET_SYSERR to close it (signal serious error)
611 handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
612 const struct GNUNET_MessageHeader *message)
614 struct ServiceList *sl;
615 const char *servicename;
618 size = ntohs (message->size);
619 size -= sizeof (struct GNUNET_MessageHeader);
620 servicename = (const char *) &message[1];
621 if ((size == 0) || (servicename[size - 1] != '\0'))
624 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
627 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
628 _("Preparing to stop `%s'\n"), servicename);
629 sl = find_service (servicename);
632 signal_result (client, servicename, GNUNET_ARM_PROCESS_UNKNOWN);
635 sl->is_default = GNUNET_NO;
636 if (GNUNET_YES == in_shutdown)
638 /* shutdown in progress */
639 signal_result (client, servicename, GNUNET_ARM_PROCESS_SHUTDOWN);
642 if (sl->killing_client != NULL)
644 /* killing already in progress */
645 signal_result (client, servicename,
646 GNUNET_ARM_PROCESS_ALREADY_STOPPING);
649 if (sl->proc == NULL)
651 /* process is down */
652 signal_result (client, servicename, GNUNET_ARM_PROCESS_ALREADY_DOWN);
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "Sending kill signal to service `%s', waiting for process to die.\n",
660 if (0 != GNUNET_OS_process_kill (sl->proc, SIGTERM))
661 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
662 sl->killing_client = client;
663 GNUNET_SERVER_client_keep (client);
668 * We are done with everything. Stop remaining
669 * tasks, signal handler and the server.
676 GNUNET_SERVER_destroy (server);
679 if (GNUNET_SCHEDULER_NO_TASK != child_death_task)
681 GNUNET_SCHEDULER_cancel (child_death_task);
682 child_death_task = GNUNET_SCHEDULER_NO_TASK;
688 * Task run for shutdown.
690 * @param cls closure, NULL if we need to self-restart
694 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
696 struct ServiceList *pos;
697 struct ServiceList *nxt;
698 struct ServiceListeningInfo *sli;
700 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
702 GNUNET_SCHEDULER_cancel (child_restart_task);
703 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
705 in_shutdown = GNUNET_YES;
706 /* first, stop listening */
707 for (pos = running_head; NULL != pos; pos = pos->next)
709 while (NULL != (sli = pos->listen_head))
711 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
712 pos->listen_tail, sli);
713 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
715 GNUNET_SCHEDULER_cancel (sli->accept_task);
716 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
718 GNUNET_break (GNUNET_OK ==
719 GNUNET_NETWORK_socket_close (sli->listen_socket));
720 GNUNET_free (sli->service_addr);
724 /* then, shutdown all existing service processes */
726 while (NULL != (pos = nxt))
729 if (pos->proc != NULL)
731 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping service `%s'\n",
733 if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
734 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
741 /* finally, should all service processes be already gone, terminate for real */
742 if (running_head == NULL)
748 * Task run whenever it is time to restart a child that died.
750 * @param cls closure, always NULL
754 delayed_restart_task (void *cls,
755 const struct GNUNET_SCHEDULER_TaskContext *tc)
757 struct ServiceList *sl;
758 struct GNUNET_TIME_Relative lowestRestartDelay;
759 struct ServiceListeningInfo *sli;
761 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
762 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
764 GNUNET_assert (GNUNET_NO == in_shutdown);
765 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
767 /* check for services that need to be restarted due to
768 * configuration changes or because the last restart failed */
769 for (sl = running_head; NULL != sl; sl = sl->next)
771 if (sl->proc == NULL)
773 /* service is currently not running */
774 if (GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value ==
777 /* restart is now allowed */
780 /* process should run by default, start immediately */
781 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
782 _("Restarting service `%s'.\n"), sl->name);
787 /* process is run on-demand, ensure it is re-started if there is demand */
788 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
789 if (GNUNET_SCHEDULER_NO_TASK == sli->accept_task)
791 /* accept was actually paused, so start it again */
793 GNUNET_SCHEDULER_add_read_net
794 (GNUNET_TIME_UNIT_FOREVER_REL, sli->listen_socket,
795 &accept_connection, sli);
801 /* update calculation for earliest time to reactivate a service */
803 GNUNET_TIME_relative_min (lowestRestartDelay,
804 GNUNET_TIME_absolute_get_remaining
809 if (lowestRestartDelay.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will restart process in %llums\n",
813 (unsigned long long) lowestRestartDelay.rel_value);
816 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
817 GNUNET_SCHEDULER_PRIORITY_IDLE,
818 &delayed_restart_task, NULL);
824 * Task triggered whenever we receive a SIGCHLD (child
827 * @param cls closure, NULL if we need to self-restart
831 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
833 struct ServiceList *pos;
834 struct ServiceList *next;
835 struct ServiceListeningInfo *sli;
840 enum GNUNET_OS_ProcessStatusType statusType;
841 unsigned long statusCode;
842 const struct GNUNET_DISK_FileHandle *pr;
844 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
845 child_death_task = GNUNET_SCHEDULER_NO_TASK;
846 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
848 /* shutdown scheduled us, ignore! */
850 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
851 pr, &maint_child_death, NULL);
854 /* consume the signal */
855 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
857 /* check for services that died (WAITPID) */
859 while (NULL != (pos = next))
863 if (pos->proc == NULL)
865 if (GNUNET_YES == in_shutdown)
869 if ((GNUNET_SYSERR ==
871 GNUNET_OS_process_status (pos->proc, &statusType, &statusCode)))
872 || ((ret == GNUNET_NO) || (statusType == GNUNET_OS_PROCESS_STOPPED)
873 || (statusType == GNUNET_OS_PROCESS_RUNNING)))
875 if (statusType == GNUNET_OS_PROCESS_EXITED)
877 statstr = _( /* process termination method */ "exit");
878 statcode = statusCode;
880 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
882 statstr = _( /* process termination method */ "signal");
883 statcode = statusCode;
887 statstr = _( /* process termination method */ "unknown");
890 GNUNET_OS_process_close (pos->proc);
892 if (NULL != pos->killing_client)
894 signal_result (pos->killing_client, pos->name,
895 GNUNET_ARM_PROCESS_DOWN);
896 GNUNET_SERVER_client_drop (pos->killing_client);
897 pos->killing_client = NULL;
898 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
899 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
901 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sli->accept_task);
903 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
905 &accept_connection, sli);
909 if (GNUNET_YES != in_shutdown)
911 if ((statusType == GNUNET_OS_PROCESS_EXITED) && (statcode == 0))
913 /* process terminated normally, allow restart at any time */
914 pos->restart_at.abs_value = 0;
918 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
919 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
921 ("Service `%s' terminated with status %s/%d, will restart in %llu ms\n"),
922 pos->name, statstr, statcode, pos->backoff.rel_value);
923 /* schedule restart */
924 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
926 GNUNET_TIME_relative_min (EXPONENTIAL_BACKOFF_THRESHOLD,
927 GNUNET_TIME_relative_multiply
930 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
931 GNUNET_SCHEDULER_cancel (child_restart_task);
933 GNUNET_SCHEDULER_add_with_priority
934 (GNUNET_SCHEDULER_PRIORITY_IDLE,
935 &delayed_restart_task, NULL);
943 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
944 pr, &maint_child_death, NULL);
945 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
951 * Transmit our shutdown acknowledgement to the client.
953 * @param cls the 'struct GNUNET_SERVER_Client'
954 * @param size number of bytes available in buf
955 * @param buf where to write the message
956 * @return number of bytes written
959 transmit_shutdown_ack (void *cls, size_t size, void *buf)
961 struct GNUNET_SERVER_Client *client = cls;
962 struct GNUNET_ARM_ResultMessage *msg;
964 if (size < sizeof (struct GNUNET_ARM_ResultMessage))
966 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
967 _("Failed to transmit shutdown ACK.\n"));
968 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
969 return 0; /* client disconnected */
971 /* Make the connection flushing for the purpose of ACK transmitting,
972 * needed on W32 to ensure that the message is even received, harmless
973 * on other platforms... */
974 GNUNET_break (GNUNET_OK == GNUNET_SERVER_client_disable_corking (client));
975 msg = (struct GNUNET_ARM_ResultMessage *) buf;
976 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_RESULT);
977 msg->header.size = htons (sizeof (struct GNUNET_ARM_ResultMessage));
978 msg->status = htonl ((uint32_t) GNUNET_ARM_PROCESS_SHUTDOWN);
979 GNUNET_SERVER_receive_done (client, GNUNET_OK);
980 GNUNET_SERVER_client_drop (client);
981 return sizeof (struct GNUNET_ARM_ResultMessage);
986 * Handler for SHUTDOWN message.
988 * @param cls closure (refers to service)
989 * @param client identification of the client
990 * @param message the actual message
993 handle_shutdown (void *cls, struct GNUNET_SERVER_Client *client,
994 const struct GNUNET_MessageHeader *message)
996 GNUNET_SCHEDULER_shutdown ();
997 GNUNET_SERVER_client_keep (client);
998 GNUNET_SERVER_notify_transmit_ready (client,
999 sizeof (struct GNUNET_ARM_ResultMessage),
1000 GNUNET_TIME_UNIT_FOREVER_REL,
1001 &transmit_shutdown_ack, client);
1002 GNUNET_SERVER_client_persist_ (client);
1007 * Signal handler called for SIGCHLD. Triggers the
1008 * respective handler by writing to the trigger pipe.
1011 sighandler_child_death ()
1014 int old_errno = errno; /* back-up errno */
1017 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
1018 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
1020 errno = old_errno; /* restore errno */
1025 * Setup our service record for the given section in the configuration file
1026 * (assuming the section is for a service).
1029 * @param section a section in the configuration file
1030 * @return GNUNET_OK (continue)
1033 setup_service (void *cls, const char *section)
1035 struct ServiceList *sl;
1039 struct sockaddr **addrs;
1040 socklen_t *addr_lens;
1044 if (strcasecmp (section, "arm") == 0)
1047 GNUNET_CONFIGURATION_get_value_string (cfg, section, "BINARY", &binary))
1049 /* not a service section */
1052 sl = find_service (section);
1055 /* got the same section twice!? */
1061 GNUNET_CONFIGURATION_get_value_filename (cfg, section, "CONFIG",
1063 (0 != STAT (config, &sbuf)))
1065 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1067 ("Configuration file `%s' for service `%s' not valid: %s\n"),
1069 (config == NULL) ? _("option missing") : STRERROR (errno));
1070 GNUNET_free (binary);
1071 GNUNET_free_non_null (config);
1074 sl = GNUNET_malloc (sizeof (struct ServiceList));
1075 sl->name = GNUNET_strdup (section);
1076 sl->binary = binary;
1077 sl->config = config;
1078 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1079 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1081 sl->pipe_control = GNUNET_YES;
1083 if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
1084 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
1086 GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
1088 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "AUTOSTART"))
1090 if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section, cfg,
1091 &addrs, &addr_lens)))
1093 /* this will free (or capture) addrs[i] */
1094 for (i = 0; i < ret; i++)
1095 create_listen_socket (addrs[i], addr_lens[i], sl);
1096 GNUNET_free (addrs);
1097 GNUNET_free (addr_lens);
1102 * Process arm requests.
1104 * @param cls closure
1105 * @param serv the initialized server
1106 * @param c configuration to use
1109 run (void *cls, struct GNUNET_SERVER_Handle *serv,
1110 const struct GNUNET_CONFIGURATION_Handle *c)
1112 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1113 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
1114 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
1115 {&handle_shutdown, NULL, GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN,
1116 sizeof (struct GNUNET_MessageHeader)},
1119 char *defaultservices;
1121 struct ServiceList *sl;
1125 GNUNET_assert (serv != NULL);
1126 GNUNET_SERVER_ignore_shutdown (serv, GNUNET_YES);
1127 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1130 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1131 GNUNET_DISK_pipe_handle (sigpipe,
1132 GNUNET_DISK_PIPE_END_READ),
1133 &maint_child_death, NULL);
1136 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_PREFIX",
1138 prefix_command = GNUNET_strdup ("");
1140 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
1142 final_option = GNUNET_strdup ("");
1144 GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
1146 /* start default services... */
1148 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "DEFAULTSERVICES",
1151 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1152 _("Starting default services `%s'\n"), defaultservices);
1153 if (0 < strlen (defaultservices))
1155 for (pos = strtok (defaultservices, " "); NULL != pos;
1156 pos = strtok (NULL, " "))
1158 sl = find_service (pos);
1161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1163 ("Default service `%s' not configured correctly!\n"),
1167 sl->is_default = GNUNET_YES;
1171 GNUNET_free (defaultservices);
1175 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1177 ("No default services configured, GNUnet will not really start right now.\n"));
1180 /* process client requests */
1181 GNUNET_SERVER_add_handlers (server, handlers);
1186 * The main function for the arm service.
1188 * @param argc number of arguments from the command line
1189 * @param argv command line arguments
1190 * @return 0 ok, 1 on error
1193 main (int argc, char *const *argv)
1196 struct GNUNET_SIGNAL_Context *shc_chld;
1198 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
1199 GNUNET_assert (sigpipe != NULL);
1201 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
1204 GNUNET_SERVICE_run (argc, argv, "arm", GNUNET_YES, &run, NULL)) ? 0 : 1;
1205 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1207 GNUNET_DISK_pipe_close (sigpipe);
1217 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1219 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1221 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1222 mallopt (M_TOP_PAD, 1 * 1024);
1228 /* end of gnunet-service-arm.c */