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 * List of our services.
39 * Record with information about a listen socket we have open.
41 struct ServiceListeningInfo
44 * This is a linked list.
46 struct ServiceListeningInfo *next;
49 * This is a linked list.
51 struct ServiceListeningInfo *prev;
54 * Address this socket is listening on.
56 struct sockaddr *service_addr;
59 * Service this listen socket is for.
61 struct ServiceList *sl;
64 * Number of bytes in 'service_addr'
66 socklen_t service_addr_len;
69 * Our listening socket.
71 struct GNUNET_NETWORK_Handle *listen_socket;
74 * Task doing the accepting.
76 GNUNET_SCHEDULER_TaskIdentifier accept_task;
82 * List of our services.
87 * This is a doubly-linked list.
89 struct ServiceList *next;
92 * This is a doubly-linked list.
94 struct ServiceList *prev;
97 * Linked list of listen sockets associated with this service.
99 struct ServiceListeningInfo *listen_head;
102 * Linked list of listen sockets associated with this service.
104 struct ServiceListeningInfo *listen_tail;
107 * Name of the service.
112 * Name of the binary used.
117 * Name of the configuration file used.
122 * Client to notify upon kill completion (waitpid), NULL
123 * if we should simply restart the process.
125 struct GNUNET_SERVER_Client *killing_client;
128 * Process structure pointer of the child.
130 struct GNUNET_OS_Process *proc;
133 * Process exponential backoff time
135 struct GNUNET_TIME_Relative backoff;
138 * Absolute time at which the process is scheduled to restart in case of death
140 struct GNUNET_TIME_Absolute restart_at;
143 * Time we asked the service to shut down (used to calculate time it took
144 * the service to terminate).
146 struct GNUNET_TIME_Absolute killed_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;
245 /* calculate listen socket list */
248 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
250 GNUNET_array_append (lsocks, ls,
251 GNUNET_NETWORK_get_fd (sli->listen_socket));
252 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
254 GNUNET_SCHEDULER_cancel (sli->accept_task);
255 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
259 GNUNET_array_append (lsocks, ls, INVALID_SOCKET);
261 GNUNET_array_append (lsocks, ls, -1);
264 /* obtain configuration */
266 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "PREFIX",
268 loprefix = GNUNET_strdup (prefix_command);
270 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "OPTIONS",
273 options = GNUNET_strdup (final_option);
274 if (NULL == strstr (options, "%"))
276 /* replace '{}' with service name */
277 while (NULL != (optpos = strstr (options, "{}")))
281 GNUNET_asprintf (&optpos, options, sl->name);
282 GNUNET_free (options);
285 /* replace '$PATH' with value associated with "PATH" */
286 while (NULL != (optpos = strstr (options, "$")))
289 while (isupper ((unsigned char) *optend))
298 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
300 val = GNUNET_strdup ("");
302 GNUNET_asprintf (&optpos, "%s%s%c%s", options, val, b, next);
303 GNUNET_free (options);
309 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
311 /* 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);
315 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
316 GNUNET_assert (NULL == sl->proc);
317 if (GNUNET_YES == use_debug)
319 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
320 lsocks, loprefix, binary, "-c", sl->config, "-L",
321 "DEBUG", options, NULL);
324 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
325 lsocks, loprefix, binary, "-c", sl->config,
327 GNUNET_free (binary);
328 if (sl->proc == NULL)
329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to start service `%s'\n"),
332 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting service `%s'\n"),
335 GNUNET_free (loprefix);
336 GNUNET_free (options);
337 GNUNET_array_grow (lsocks, ls, 0);
342 * Transmit a status result message.
344 * @param cls pointer to "unit16_t*" with message type
345 * @param size number of bytes available in buf
346 * @param buf where to copy the message, NULL on error
347 * @return number of bytes copied to buf
350 write_result (void *cls, size_t size, void *buf)
352 enum GNUNET_ARM_ProcessStatus *res = cls;
353 struct GNUNET_ARM_ResultMessage *msg;
357 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
358 _("Could not send status result to client\n"));
359 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);
363 GNUNET_assert (size >= sizeof (struct GNUNET_ARM_ResultMessage));
365 msg->header.size = htons (sizeof (struct GNUNET_ARM_ResultMessage));
366 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_RESULT);
367 msg->status = htonl ((uint32_t) (*res));
369 return sizeof (struct GNUNET_ARM_ResultMessage);
373 * Transmit the list of running services.
375 * @param cls pointer to struct GNUNET_ARM_ListResultMessage with the message
376 * @param size number of bytes available in buf
377 * @param buf where to copy the message, NULL on error
378 * @return number of bytes copied to buf
381 write_list_result (void *cls, size_t size, void *buf)
383 struct GNUNET_ARM_ListResultMessage *msg = cls;
384 struct GNUNET_ARM_ListResultMessage *rslt;
389 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
390 _("Could not send list result to client\n"));
391 return 0; /* error, not much we can do */
394 GNUNET_assert (size >= msg->header.size);
396 rslt->header.size = htons (msg->header.size);
397 rslt->header.type = htons (msg->header.type);
398 rslt->count = htons (msg->count);
400 size_t list_size = msg->header.size
401 - sizeof (struct GNUNET_ARM_ListResultMessage);
402 memcpy (&rslt[1], &msg[1], list_size);
404 rslt_size = msg->header.size;
411 * Signal our client that we will start or stop the
414 * @param client who is being signalled
415 * @param name name of the service
416 * @param result message type to send
417 * @return NULL if it was not found
420 signal_result (struct GNUNET_SERVER_Client *client, const char *name,
421 enum GNUNET_ARM_ProcessStatus result)
423 enum GNUNET_ARM_ProcessStatus *res;
427 /* FIXME: this is not super-clean yet... */
428 res = GNUNET_malloc (sizeof (enum GNUNET_ARM_ProcessStatus));
430 GNUNET_SERVER_notify_transmit_ready (client,
432 GNUNET_ARM_ResultMessage),
433 GNUNET_TIME_UNIT_FOREVER_REL,
435 GNUNET_SERVER_receive_done (client, GNUNET_OK);
440 * Find the process with the given service
441 * name in the given list and return it.
443 * @param name which service entry to look up
444 * @return NULL if it was not found
446 static struct ServiceList *
447 find_service (const char *name)
449 struct ServiceList *sl;
454 if (0 == strcasecmp (sl->name, name))
463 * First connection has come to the listening socket associated with the service,
464 * create the service in order to relay the incoming connection to it
466 * @param cls callback data, struct ServiceListeningInfo describing a listen socket
470 accept_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
472 struct ServiceListeningInfo *sli = cls;
473 struct ServiceList *sl = sli->sl;
475 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
476 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
483 * Creating a listening socket for each of the service's addresses and
484 * wait for the first incoming connection to it
486 * @param sa address associated with the service
487 * @param addr_len length of sa
488 * @param sl service entry for the service in question
491 create_listen_socket (struct sockaddr *sa, socklen_t addr_len,
492 struct ServiceList *sl)
495 struct GNUNET_NETWORK_Handle *sock;
496 struct ServiceListeningInfo *sli;
498 switch (sa->sa_family)
501 sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
504 sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
507 if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */
509 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
514 errno = EAFNOSUPPORT;
519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
520 _("Unable to create socket for service `%s': %s\n"),
521 sl->name, STRERROR (errno));
525 if (GNUNET_NETWORK_socket_setsockopt
526 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
527 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
530 if ((sa->sa_family == AF_INET6) &&
531 (GNUNET_NETWORK_socket_setsockopt
532 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
533 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
537 if (GNUNET_NETWORK_socket_bind
538 (sock, (const struct sockaddr *) sa, addr_len) != GNUNET_OK)
540 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
542 ("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
543 sl->name, GNUNET_a2s (sa, addr_len), STRERROR (errno));
544 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
548 if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
550 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
551 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
555 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
556 _("ARM now monitors connections to service `%s' at `%s'\n"),
557 sl->name, GNUNET_a2s (sa, addr_len));
558 sli = GNUNET_malloc (sizeof (struct ServiceListeningInfo));
559 sli->service_addr = sa;
560 sli->service_addr_len = addr_len;
561 sli->listen_socket = sock;
564 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock,
565 &accept_connection, sli);
566 GNUNET_CONTAINER_DLL_insert (sl->listen_head, sl->listen_tail, sli);
571 * Remove and free an entry in the service list. Listen sockets
572 * must have already been cleaned up. Only to be called during shutdown.
574 * @param sl entry to free
577 free_service (struct ServiceList *sl)
579 GNUNET_assert (GNUNET_YES == in_shutdown);
580 GNUNET_CONTAINER_DLL_remove (running_head, running_tail, sl);
581 GNUNET_assert (NULL == sl->listen_head);
582 GNUNET_free_non_null (sl->config);
583 GNUNET_free_non_null (sl->binary);
584 GNUNET_free (sl->name);
590 * Handle START-message.
592 * @param cls closure (always NULL)
593 * @param client identification of the client
594 * @param message the actual message
595 * @return GNUNET_OK to keep the connection open,
596 * GNUNET_SYSERR to close it (signal serious error)
599 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
600 const struct GNUNET_MessageHeader *message)
602 const char *servicename;
603 struct ServiceList *sl;
606 size = ntohs (message->size);
607 size -= sizeof (struct GNUNET_MessageHeader);
608 servicename = (const char *) &message[1];
609 if ((size == 0) || (servicename[size - 1] != '\0'))
612 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
615 if (GNUNET_YES == in_shutdown)
617 signal_result (client, servicename, GNUNET_ARM_PROCESS_SHUTDOWN);
620 sl = find_service (servicename);
623 signal_result (client, servicename, GNUNET_ARM_PROCESS_UNKNOWN);
626 sl->is_default = GNUNET_YES;
627 if (sl->proc != NULL)
629 signal_result (client, servicename, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
633 signal_result (client, servicename, GNUNET_ARM_PROCESS_STARTING);
638 * Handle STOP-message.
640 * @param cls closure (always NULL)
641 * @param client identification of the client
642 * @param message the actual message
643 * @return GNUNET_OK to keep the connection open,
644 * GNUNET_SYSERR to close it (signal serious error)
647 handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
648 const struct GNUNET_MessageHeader *message)
650 struct ServiceList *sl;
651 const char *servicename;
654 size = ntohs (message->size);
655 size -= sizeof (struct GNUNET_MessageHeader);
656 servicename = (const char *) &message[1];
657 if ((size == 0) || (servicename[size - 1] != '\0'))
660 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
663 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
664 _("Preparing to stop `%s'\n"), servicename);
665 sl = find_service (servicename);
668 signal_result (client, servicename, GNUNET_ARM_PROCESS_UNKNOWN);
671 sl->is_default = GNUNET_NO;
672 if (GNUNET_YES == in_shutdown)
674 /* shutdown in progress */
675 signal_result (client, servicename, GNUNET_ARM_PROCESS_SHUTDOWN);
678 if (sl->killing_client != NULL)
680 /* killing already in progress */
681 signal_result (client, servicename,
682 GNUNET_ARM_PROCESS_ALREADY_STOPPING);
685 if (sl->proc == NULL)
687 /* process is down */
688 signal_result (client, servicename, GNUNET_ARM_PROCESS_ALREADY_DOWN);
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
692 "Sending kill signal to service `%s', waiting for process to die.\n",
694 sl->killed_at = GNUNET_TIME_absolute_get ();
695 if (0 != GNUNET_OS_process_kill (sl->proc, SIGTERM))
696 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
697 sl->killing_client = client;
698 GNUNET_SERVER_client_keep (client);
702 * Handle LIST-message.
704 * @param cls closure (always NULL)
705 * @param client identification of the client
706 * @param message the actual message
709 handle_list (void *cls, struct GNUNET_SERVER_Client *client,
710 const struct GNUNET_MessageHeader *message)
712 struct GNUNET_ARM_ListResultMessage *msg;
713 size_t string_list_size;
715 struct ServiceList *sl;
722 string_list_size = 0;
723 /* first count the running processes get their name's size */
724 for (sl = running_head; sl != NULL; sl = sl->next)
726 if (sl->proc != NULL)
728 string_list_size += strlen (sl->name);
729 string_list_size += strlen (sl->binary);
730 string_list_size += 4;
734 total_size = sizeof (struct GNUNET_ARM_ListResultMessage)
736 msg = GNUNET_malloc (total_size);
737 msg->header.size = total_size;
738 msg->header.type = GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT;
741 char *pos = (char *)&msg[1];
742 for (sl = running_head; sl != NULL; sl = sl->next)
744 if (sl->proc != NULL)
746 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
747 GNUNET_snprintf(pos, s, "%s (%s)", sl->name, sl->binary);
752 GNUNET_SERVER_notify_transmit_ready (client,
754 GNUNET_TIME_UNIT_FOREVER_REL,
755 &write_list_result, msg);
756 GNUNET_SERVER_receive_done (client, GNUNET_OK);
760 * We are done with everything. Stop remaining
761 * tasks, signal handler and the server.
768 GNUNET_SERVER_destroy (server);
771 if (GNUNET_SCHEDULER_NO_TASK != child_death_task)
773 GNUNET_SCHEDULER_cancel (child_death_task);
774 child_death_task = GNUNET_SCHEDULER_NO_TASK;
780 * Task run for shutdown.
782 * @param cls closure, NULL if we need to self-restart
786 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
788 struct ServiceList *pos;
789 struct ServiceList *nxt;
790 struct ServiceListeningInfo *sli;
792 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
794 GNUNET_SCHEDULER_cancel (child_restart_task);
795 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
797 in_shutdown = GNUNET_YES;
798 /* first, stop listening */
799 for (pos = running_head; NULL != pos; pos = pos->next)
801 while (NULL != (sli = pos->listen_head))
803 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
804 pos->listen_tail, sli);
805 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
807 GNUNET_SCHEDULER_cancel (sli->accept_task);
808 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
810 GNUNET_break (GNUNET_OK ==
811 GNUNET_NETWORK_socket_close (sli->listen_socket));
812 GNUNET_free (sli->service_addr);
816 /* then, shutdown all existing service processes */
818 while (NULL != (pos = nxt))
821 if (pos->proc != NULL)
823 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping service `%s'\n",
825 pos->killed_at = GNUNET_TIME_absolute_get ();
826 if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
827 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
834 /* finally, should all service processes be already gone, terminate for real */
835 if (running_head == NULL)
841 * Task run whenever it is time to restart a child that died.
843 * @param cls closure, always NULL
847 delayed_restart_task (void *cls,
848 const struct GNUNET_SCHEDULER_TaskContext *tc)
850 struct ServiceList *sl;
851 struct GNUNET_TIME_Relative lowestRestartDelay;
852 struct ServiceListeningInfo *sli;
854 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
855 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
857 GNUNET_assert (GNUNET_NO == in_shutdown);
858 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
860 /* check for services that need to be restarted due to
861 * configuration changes or because the last restart failed */
862 for (sl = running_head; NULL != sl; sl = sl->next)
864 if (NULL != sl->proc)
866 /* service is currently not running */
867 if (GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value ==
870 /* restart is now allowed */
873 /* process should run by default, start immediately */
874 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
875 _("Restarting service `%s'.\n"), sl->name);
880 /* process is run on-demand, ensure it is re-started if there is demand */
881 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
882 if (GNUNET_SCHEDULER_NO_TASK == sli->accept_task)
884 /* accept was actually paused, so start it again */
886 GNUNET_SCHEDULER_add_read_net
887 (GNUNET_TIME_UNIT_FOREVER_REL, sli->listen_socket,
888 &accept_connection, sli);
894 /* update calculation for earliest time to reactivate a service */
896 GNUNET_TIME_relative_min (lowestRestartDelay,
897 GNUNET_TIME_absolute_get_remaining
901 if (lowestRestartDelay.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "Will restart process in %s\n",
905 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay, GNUNET_YES));
907 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
908 GNUNET_SCHEDULER_PRIORITY_IDLE,
909 &delayed_restart_task, NULL);
915 * Task triggered whenever we receive a SIGCHLD (child
918 * @param cls closure, NULL if we need to self-restart
922 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
924 struct ServiceList *pos;
925 struct ServiceList *next;
926 struct ServiceListeningInfo *sli;
931 enum GNUNET_OS_ProcessStatusType statusType;
932 unsigned long statusCode;
933 const struct GNUNET_DISK_FileHandle *pr;
935 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
936 child_death_task = GNUNET_SCHEDULER_NO_TASK;
937 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
939 /* shutdown scheduled us, ignore! */
941 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
942 pr, &maint_child_death, NULL);
945 /* consume the signal */
946 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
948 /* check for services that died (WAITPID) */
950 while (NULL != (pos = next))
954 if (pos->proc == NULL)
956 if (GNUNET_YES == in_shutdown)
960 if ((GNUNET_SYSERR ==
962 GNUNET_OS_process_status (pos->proc, &statusType, &statusCode)))
963 || ((ret == GNUNET_NO) || (statusType == GNUNET_OS_PROCESS_STOPPED)
964 || (statusType == GNUNET_OS_PROCESS_RUNNING)))
966 if (statusType == GNUNET_OS_PROCESS_EXITED)
968 statstr = _( /* process termination method */ "exit");
969 statcode = statusCode;
971 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
973 statstr = _( /* process termination method */ "signal");
974 statcode = statusCode;
978 statstr = _( /* process termination method */ "unknown");
981 if (0 != pos->killed_at.abs_value)
983 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
984 _("Service `%s' took %s to terminate\n"),
986 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at), GNUNET_YES));
988 GNUNET_OS_process_destroy (pos->proc);
990 if (NULL != pos->killing_client)
992 signal_result (pos->killing_client, pos->name,
993 GNUNET_ARM_PROCESS_DOWN);
994 GNUNET_SERVER_client_drop (pos->killing_client);
995 pos->killing_client = NULL;
996 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
997 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
999 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sli->accept_task);
1001 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1003 &accept_connection, sli);
1007 if (GNUNET_YES != in_shutdown)
1009 if ((statusType == GNUNET_OS_PROCESS_EXITED) && (statcode == 0))
1011 /* process terminated normally, allow restart at any time */
1012 pos->restart_at.abs_value = 0;
1016 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1017 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1019 ("Service `%s' terminated with status %s/%d, will restart in %llu ms\n"),
1020 pos->name, statstr, statcode, pos->backoff.rel_value);
1021 /* schedule restart */
1022 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1023 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1025 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
1026 GNUNET_SCHEDULER_cancel (child_restart_task);
1027 child_restart_task =
1028 GNUNET_SCHEDULER_add_with_priority
1029 (GNUNET_SCHEDULER_PRIORITY_IDLE,
1030 &delayed_restart_task, NULL);
1038 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1039 pr, &maint_child_death, NULL);
1040 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1046 * Handler for SHUTDOWN message.
1048 * @param cls closure (refers to service)
1049 * @param client identification of the client
1050 * @param message the actual message
1053 handle_shutdown (void *cls, struct GNUNET_SERVER_Client *client,
1054 const struct GNUNET_MessageHeader *message)
1056 GNUNET_SCHEDULER_shutdown ();
1057 GNUNET_SERVER_client_persist_ (client);
1062 * Signal handler called for SIGCHLD. Triggers the
1063 * respective handler by writing to the trigger pipe.
1066 sighandler_child_death ()
1069 int old_errno = errno; /* back-up errno */
1072 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
1073 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
1075 errno = old_errno; /* restore errno */
1080 * Setup our service record for the given section in the configuration file
1081 * (assuming the section is for a service).
1084 * @param section a section in the configuration file
1085 * @return GNUNET_OK (continue)
1088 setup_service (void *cls, const char *section)
1090 struct ServiceList *sl;
1094 struct sockaddr **addrs;
1095 socklen_t *addr_lens;
1099 if (strcasecmp (section, "arm") == 0)
1102 GNUNET_CONFIGURATION_get_value_string (cfg, section, "BINARY", &binary))
1104 /* not a service section */
1107 sl = find_service (section);
1110 /* got the same section twice!? */
1116 GNUNET_CONFIGURATION_get_value_filename (cfg, section, "CONFIG",
1119 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", "DEFAULTCONFIG",
1122 (0 != STAT (config, &sbuf)))
1125 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, section, "CONFIG");
1127 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1130 GNUNET_free (binary);
1131 GNUNET_free_non_null (config);
1134 sl = GNUNET_malloc (sizeof (struct ServiceList));
1135 sl->name = GNUNET_strdup (section);
1136 sl->binary = binary;
1137 sl->config = config;
1138 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1139 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1141 sl->pipe_control = GNUNET_YES;
1143 if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
1144 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
1146 GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
1148 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "AUTOSTART"))
1150 if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section, cfg,
1151 &addrs, &addr_lens)))
1153 /* this will free (or capture) addrs[i] */
1154 for (i = 0; i < ret; i++)
1155 create_listen_socket (addrs[i], addr_lens[i], sl);
1156 GNUNET_free (addrs);
1157 GNUNET_free (addr_lens);
1162 * Process arm requests.
1164 * @param cls closure
1165 * @param serv the initialized server
1166 * @param c configuration to use
1169 run (void *cls, struct GNUNET_SERVER_Handle *serv,
1170 const struct GNUNET_CONFIGURATION_Handle *c)
1172 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1173 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
1174 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
1175 {&handle_shutdown, NULL, GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN,
1176 sizeof (struct GNUNET_MessageHeader)},
1177 {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST,
1178 sizeof (struct GNUNET_MessageHeader)},
1181 char *defaultservices;
1183 struct ServiceList *sl;
1187 GNUNET_assert (serv != NULL);
1188 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1191 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1192 GNUNET_DISK_pipe_handle (sigpipe,
1193 GNUNET_DISK_PIPE_END_READ),
1194 &maint_child_death, NULL);
1197 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_PREFIX",
1199 prefix_command = GNUNET_strdup ("");
1201 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
1203 final_option = GNUNET_strdup ("");
1205 GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
1207 /* start default services... */
1209 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "DEFAULTSERVICES",
1212 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1213 _("Starting default services `%s'\n"), defaultservices);
1214 if (0 < strlen (defaultservices))
1216 for (pos = strtok (defaultservices, " "); NULL != pos;
1217 pos = strtok (NULL, " "))
1219 sl = find_service (pos);
1222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1224 ("Default service `%s' not configured correctly!\n"),
1228 sl->is_default = GNUNET_YES;
1232 GNUNET_free (defaultservices);
1236 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1238 ("No default services configured, GNUnet will not really start right now.\n"));
1241 /* process client requests */
1242 GNUNET_SERVER_add_handlers (server, handlers);
1247 * The main function for the arm service.
1249 * @param argc number of arguments from the command line
1250 * @param argv command line arguments
1251 * @return 0 ok, 1 on error
1254 main (int argc, char *const *argv)
1257 struct GNUNET_SIGNAL_Context *shc_chld;
1259 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
1260 GNUNET_assert (sigpipe != NULL);
1262 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
1265 GNUNET_SERVICE_run (argc, argv, "arm",
1266 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, &run, NULL)) ? 0 : 1;
1267 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1269 GNUNET_DISK_pipe_close (sigpipe);
1279 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1281 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1283 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1284 mallopt (M_TOP_PAD, 1 * 1024);
1290 /* end of gnunet-service-arm.c */