2 This file is part of GNUnet.
3 Copyright (C) 2009-2011, 2015, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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"
34 * Name of the file for writing resource utilization summaries to.
36 static char *wait_filename;
39 * Handle for the file for writing resource summaries.
41 static FILE *wait_file;
46 * How many messages do we queue up at most for optional
47 * notifications to a client? (this can cause notifications
48 * about outgoing messages to be dropped).
50 #define MAX_NOTIFY_QUEUE 1024
54 * List of our services.
60 * Record with information about a listen socket we have open.
62 struct ServiceListeningInfo
65 * This is a linked list.
67 struct ServiceListeningInfo *next;
70 * This is a linked list.
72 struct ServiceListeningInfo *prev;
75 * Address this socket is listening on.
77 struct sockaddr *service_addr;
80 * Service this listen socket is for.
82 struct ServiceList *sl;
85 * Number of bytes in @e service_addr
87 socklen_t service_addr_len;
90 * Our listening socket.
92 struct GNUNET_NETWORK_Handle *listen_socket;
95 * Task doing the accepting.
97 struct GNUNET_SCHEDULER_Task *accept_task;
103 * List of our services.
108 * This is a doubly-linked list.
110 struct ServiceList *next;
113 * This is a doubly-linked list.
115 struct ServiceList *prev;
118 * Linked list of listen sockets associated with this service.
120 struct ServiceListeningInfo *listen_head;
123 * Linked list of listen sockets associated with this service.
125 struct ServiceListeningInfo *listen_tail;
128 * Name of the service.
133 * Name of the binary used.
138 * Name of the configuration file used.
143 * Client to notify upon kill completion (waitpid), NULL
144 * if we should simply restart the process.
146 struct GNUNET_SERVICE_Client *killing_client;
149 * ID of the request that killed the service (for reporting back).
151 uint64_t killing_client_request_id;
154 * Process structure pointer of the child.
156 struct GNUNET_OS_Process *proc;
159 * Process exponential backoff time
161 struct GNUNET_TIME_Relative backoff;
164 * Absolute time at which the process is scheduled to restart in case of death
166 struct GNUNET_TIME_Absolute restart_at;
169 * Time we asked the service to shut down (used to calculate time it took
170 * the service to terminate).
172 struct GNUNET_TIME_Absolute killed_at;
175 * Is this service to be started by default (or did a client tell us explicitly
176 * to start it)? #GNUNET_NO if the service is started only upon 'accept' on a
177 * listen socket or possibly explicitly by a client changing the value.
182 * Should we use pipes to signal this process? (YES for Java binaries and if we
189 * List of running services.
191 static struct ServiceList *running_head;
194 * List of running services.
196 static struct ServiceList *running_tail;
201 static const struct GNUNET_CONFIGURATION_Handle *cfg;
204 * Command to prepend to each actual command.
206 static char *prefix_command;
209 * Option to append to each actual command.
211 static char *final_option;
214 * ID of task called whenever we get a SIGCHILD.
216 static struct GNUNET_SCHEDULER_Task *child_death_task;
219 * ID of task called whenever the timeout for restarting a child
222 static struct GNUNET_SCHEDULER_Task *child_restart_task;
225 * Pipe used to communicate shutdown via signal.
227 static struct GNUNET_DISK_PipeHandle *sigpipe;
230 * Are we in shutdown mode?
232 static int in_shutdown;
235 * Are we starting user services?
237 static int start_user = GNUNET_YES;
240 * Are we starting system services?
242 static int start_system = GNUNET_YES;
245 * Handle to our service instance. Our service is a bit special in that
246 * its service is not immediately stopped once we get a shutdown
247 * request (since we need to continue service until all of our child
248 * processes are dead). This handle is used to shut down the service
249 * (and thus trigger process termination) once all child processes are
250 * also dead. A special option in the ARM configuration modifies the
251 * behaviour of the service implementation to not do the shutdown
254 static struct GNUNET_SERVICE_Handle *service;
257 * Context for notifications we need to send to our clients.
259 static struct GNUNET_NotificationContext *notifier;
263 * Signal our client that we will start or stop the
266 * @param client who is being signalled
267 * @param name name of the service
268 * @param request_id id of the request that is being responded to.
269 * @param result message type to send
270 * @return NULL if it was not found
273 signal_result (struct GNUNET_SERVICE_Client *client,
276 enum GNUNET_ARM_Result result)
278 struct GNUNET_MQ_Envelope *env;
279 struct GNUNET_ARM_ResultMessage *msg;
281 env = GNUNET_MQ_msg (msg,
282 GNUNET_MESSAGE_TYPE_ARM_RESULT);
283 msg->result = htonl (result);
284 msg->arm_msg.request_id = GNUNET_htonll (request_id);
285 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
291 * Tell all clients about status change of a service.
293 * @param name name of the service
294 * @param status message type to send
295 * @param unicast if not NULL, send to this client only.
296 * otherwise, send to all clients in the notifier
299 broadcast_status (const char *name,
300 enum GNUNET_ARM_ServiceStatus status,
301 struct GNUNET_SERVICE_Client *unicast)
303 struct GNUNET_MQ_Envelope *env;
304 struct GNUNET_ARM_StatusMessage *msg;
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "Sending status %u of service `%s' to client\n",
309 (unsigned int) status,
311 namelen = strlen (name) + 1;
312 env = GNUNET_MQ_msg_extra (msg,
314 GNUNET_MESSAGE_TYPE_ARM_STATUS);
315 msg->status = htonl ((uint32_t) (status));
316 GNUNET_memcpy ((char *) &msg[1],
321 GNUNET_notification_context_broadcast (notifier,
324 GNUNET_MQ_discard (env);
328 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (unicast),
335 * Actually start the process for the given service.
337 * @param sl identifies service to start
338 * @param client that asked to start the service (may be NULL)
339 * @param request_id id of the request in response to which the process is
340 * being started. 0 if starting was not requested.
343 start_process (struct ServiceList *sl,
344 struct GNUNET_SERVICE_Client *client,
350 int is_simple_service;
351 struct ServiceListeningInfo *sli;
357 /* calculate listen socket list */
360 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
362 GNUNET_array_append (lsocks, ls,
363 GNUNET_NETWORK_get_fd (sli->listen_socket));
364 if (NULL != sli->accept_task)
366 GNUNET_SCHEDULER_cancel (sli->accept_task);
367 sli->accept_task = NULL;
371 GNUNET_array_append (lsocks,
375 GNUNET_array_append (lsocks,
380 /* obtain configuration */
382 GNUNET_CONFIGURATION_get_value_filename (cfg,
386 loprefix = GNUNET_strdup (prefix_command);
388 GNUNET_CONFIGURATION_get_value_filename (cfg,
399 fin_options = GNUNET_strdup (final_option);
400 /* replace '{}' with service name */
401 while (NULL != (optpos = strstr (fin_options,
404 /* terminate string at opening parenthesis */
406 GNUNET_asprintf (&new_options,
411 GNUNET_free (fin_options);
412 fin_options = new_options;
416 /* combine "fin_options" with "options" */
418 GNUNET_asprintf (&options,
422 GNUNET_free (optpos);
426 /* only have "fin_options", use that */
427 options = fin_options;
430 options = GNUNET_CONFIGURATION_expand_dollar (cfg,
432 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg,
436 const char *service_type = NULL;
437 const char *choices[] = { "GNUNET", "SIMPLE", NULL };
439 is_simple_service = GNUNET_NO;
441 GNUNET_CONFIGURATION_get_value_choice (cfg,
446 (0 == strcasecmp (service_type, "SIMPLE")) )
447 is_simple_service = GNUNET_YES;
450 GNUNET_assert (NULL == sl->proc);
451 if (GNUNET_YES == is_simple_service)
453 /* A simple service will receive no GNUnet specific
454 command line options. */
455 binary = GNUNET_strdup (sl->binary);
456 binary = GNUNET_CONFIGURATION_expand_dollar (cfg, binary);
457 GNUNET_asprintf ("edbinary,
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
461 "Starting simple service `%s' using binary `%s'\n",
462 sl->name, sl->binary);
463 /* FIXME: dollar expansion should only be done outside
464 * of ''-quoted strings, escaping should be considered. */
466 options = GNUNET_CONFIGURATION_expand_dollar (cfg, options);
468 GNUNET_OS_start_process_s (sl->pipe_control,
469 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
478 /* actually start process */
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Starting service `%s' using binary `%s' and configuration `%s'\n",
481 sl->name, sl->binary, sl->config);
482 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
483 GNUNET_asprintf ("edbinary,
487 if (GNUNET_YES == use_debug)
489 if (NULL == sl->config)
491 GNUNET_OS_start_process_s (sl->pipe_control,
492 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
501 GNUNET_OS_start_process_s (sl->pipe_control,
502 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
513 if (NULL == sl->config)
515 GNUNET_OS_start_process_s (sl->pipe_control,
516 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
524 GNUNET_OS_start_process_s (sl->pipe_control,
525 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
534 GNUNET_free (binary);
535 GNUNET_free (quotedbinary);
536 if (NULL == sl->proc)
538 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
539 _("Failed to start service `%s'\n"),
542 signal_result (client,
545 GNUNET_ARM_RESULT_START_FAILED);
549 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
550 _("Starting service `%s'\n"),
552 broadcast_status (sl->name,
553 GNUNET_ARM_SERVICE_STARTING,
556 signal_result (client,
559 GNUNET_ARM_RESULT_STARTING);
562 GNUNET_free (loprefix);
563 GNUNET_free (options);
564 GNUNET_array_grow (lsocks,
571 * Find the process with the given service
572 * name in the given list and return it.
574 * @param name which service entry to look up
575 * @return NULL if it was not found
577 static struct ServiceList *
578 find_service (const char *name)
580 struct ServiceList *sl;
585 if (0 == strcasecmp (sl->name, name))
594 * First connection has come to the listening socket associated with the service,
595 * create the service in order to relay the incoming connection to it
597 * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
600 accept_connection (void *cls)
602 struct ServiceListeningInfo *sli = cls;
603 struct ServiceList *sl = sli->sl;
605 sli->accept_task = NULL;
606 GNUNET_assert (GNUNET_NO == in_shutdown);
607 start_process (sl, NULL, 0);
612 * Creating a listening socket for each of the service's addresses and
613 * wait for the first incoming connection to it
615 * @param sa address associated with the service
616 * @param addr_len length of @a sa
617 * @param sl service entry for the service in question
620 create_listen_socket (struct sockaddr *sa,
622 struct ServiceList *sl)
625 struct GNUNET_NETWORK_Handle *sock;
626 struct ServiceListeningInfo *sli;
632 switch (sa->sa_family)
635 sock = GNUNET_NETWORK_socket_create (PF_INET,
640 sock = GNUNET_NETWORK_socket_create (PF_INET6,
645 if (0 == strcmp (GNUNET_a2s (sa,
647 "@")) /* Do not bind to blank UNIX path! */
649 sock = GNUNET_NETWORK_socket_create (PF_UNIX,
656 errno = EAFNOSUPPORT;
661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
662 _("Unable to create socket for service `%s': %s\n"),
669 GNUNET_NETWORK_socket_setsockopt (sock,
674 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
677 if ( (sa->sa_family == AF_INET6) &&
679 GNUNET_NETWORK_socket_setsockopt (sock,
684 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
688 if (AF_UNIX == sa->sa_family)
689 GNUNET_NETWORK_unix_precheck ((struct sockaddr_un *) sa);
692 GNUNET_NETWORK_socket_bind (sock,
693 (const struct sockaddr *) sa,
696 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
697 _("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
702 GNUNET_break (GNUNET_OK ==
703 GNUNET_NETWORK_socket_close (sock));
708 if ((AF_UNIX == sa->sa_family)
710 /* Permission settings are not required when abstract sockets are used */
711 && ('\0' != ((const struct sockaddr_un *)sa)->sun_path[0])
716 GNUNET_CONFIGURATION_get_value_yesno (cfg,
720 GNUNET_CONFIGURATION_get_value_yesno (cfg,
723 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sa)->sun_path,
730 GNUNET_NETWORK_socket_listen (sock, 5))
732 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
734 GNUNET_break (GNUNET_OK ==
735 GNUNET_NETWORK_socket_close (sock));
739 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
740 _("ARM now monitors connections to service `%s' at `%s'\n"),
744 sli = GNUNET_new (struct ServiceListeningInfo);
745 sli->service_addr = sa;
746 sli->service_addr_len = addr_len;
747 sli->listen_socket = sock;
750 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
752 &accept_connection, sli);
753 GNUNET_CONTAINER_DLL_insert (sl->listen_head,
760 * Remove and free an entry in the service list. Listen sockets
761 * must have already been cleaned up. Only to be called during shutdown.
763 * @param sl entry to free
766 free_service (struct ServiceList *sl)
768 GNUNET_assert (GNUNET_YES == in_shutdown);
769 GNUNET_CONTAINER_DLL_remove (running_head,
772 GNUNET_assert (NULL == sl->listen_head);
773 GNUNET_free_non_null (sl->config);
774 GNUNET_free_non_null (sl->binary);
775 GNUNET_free (sl->name);
781 * Check START-message.
783 * @param cls identification of the client
784 * @param amsg the actual message
785 * @return #GNUNET_OK to keep the connection open,
786 * #GNUNET_SYSERR to close it (signal serious error)
789 check_start (void *cls,
790 const struct GNUNET_ARM_Message *amsg)
793 const char *servicename;
795 size = ntohs (amsg->header.size) - sizeof (struct GNUNET_ARM_Message);
796 servicename = (const char *) &amsg[1];
798 (servicename[size - 1] != '\0') )
801 return GNUNET_SYSERR;
808 * Handle START-message.
810 * @param cls identification of the client
811 * @param amsg the actual message
814 handle_start (void *cls,
815 const struct GNUNET_ARM_Message *amsg)
817 struct GNUNET_SERVICE_Client *client = cls;
818 const char *servicename;
819 struct ServiceList *sl;
822 request_id = GNUNET_ntohll (amsg->request_id);
823 servicename = (const char *) &amsg[1];
824 GNUNET_SERVICE_client_continue (client);
825 if (GNUNET_YES == in_shutdown)
827 signal_result (client,
830 GNUNET_ARM_RESULT_IN_SHUTDOWN);
833 sl = find_service (servicename);
836 signal_result (client,
839 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
842 sl->force_start = GNUNET_YES;
843 if (NULL != sl->proc)
845 signal_result (client,
848 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
858 * Start a shutdown sequence.
860 * @param cls closure (refers to service)
863 trigger_shutdown (void *cls)
865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
866 "Triggering shutdown\n");
867 GNUNET_SCHEDULER_shutdown ();
872 * Check STOP-message.
874 * @param cls identification of the client
875 * @param amsg the actual message
876 * @return #GNUNET_OK to keep the connection open,
877 * #GNUNET_SYSERR to close it (signal serious error)
880 check_stop (void *cls,
881 const struct GNUNET_ARM_Message *amsg)
884 const char *servicename;
886 size = ntohs (amsg->header.size) - sizeof (struct GNUNET_ARM_Message);
887 servicename = (const char *) &amsg[1];
889 (servicename[size - 1] != '\0') )
892 return GNUNET_SYSERR;
899 * Handle STOP-message.
901 * @param cls identification of the client
902 * @param amsg the actual message
905 handle_stop (void *cls,
906 const struct GNUNET_ARM_Message *amsg)
908 struct GNUNET_SERVICE_Client *client = cls;
909 struct ServiceList *sl;
910 const char *servicename;
913 request_id = GNUNET_ntohll (amsg->request_id);
914 servicename = (const char *) &amsg[1];
915 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
916 _("Preparing to stop `%s'\n"),
918 GNUNET_SERVICE_client_continue (client);
919 if (0 == strcasecmp (servicename,
922 broadcast_status (servicename,
923 GNUNET_ARM_SERVICE_STOPPING,
925 signal_result (client,
928 GNUNET_ARM_RESULT_STOPPING);
929 GNUNET_SERVICE_client_persist (client);
930 GNUNET_SCHEDULER_add_now (&trigger_shutdown,
934 sl = find_service (servicename);
937 signal_result (client,
940 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
943 sl->force_start = GNUNET_NO;
944 if (GNUNET_YES == in_shutdown)
946 /* shutdown in progress */
947 signal_result (client,
950 GNUNET_ARM_RESULT_IN_SHUTDOWN);
953 if (NULL != sl->killing_client)
955 /* killing already in progress */
956 signal_result (client,
959 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
962 if (NULL == sl->proc)
964 /* process is down */
965 signal_result (client,
968 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 "Sending kill signal to service `%s', waiting for process to die.\n",
974 broadcast_status (servicename,
975 GNUNET_ARM_SERVICE_STOPPING,
977 /* no signal_start - only when it's STOPPED */
978 sl->killed_at = GNUNET_TIME_absolute_get ();
979 if (0 != GNUNET_OS_process_kill (sl->proc,
981 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
983 sl->killing_client = client;
984 sl->killing_client_request_id = request_id;
989 * Handle LIST-message.
991 * @param cls identification of the client
992 * @param message the actual message
995 handle_list (void *cls,
996 const struct GNUNET_ARM_Message *request)
998 struct GNUNET_SERVICE_Client *client = cls;
999 struct GNUNET_MQ_Envelope *env;
1000 struct GNUNET_ARM_ListResultMessage *msg;
1001 size_t string_list_size;
1002 struct ServiceList *sl;
1006 GNUNET_break (0 == ntohl (request->reserved));
1008 string_list_size = 0;
1010 /* first count the running processes get their name's size */
1011 for (sl = running_head; NULL != sl; sl = sl->next)
1013 if (NULL != sl->proc)
1015 string_list_size += strlen (sl->name);
1016 string_list_size += strlen (sl->binary);
1017 string_list_size += 4;
1022 env = GNUNET_MQ_msg_extra (msg,
1024 GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT);
1025 msg->arm_msg.request_id = request->request_id;
1026 msg->count = htons (count);
1028 pos = (char *) &msg[1];
1029 for (sl = running_head; NULL != sl; sl = sl->next)
1031 if (NULL != sl->proc)
1033 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
1034 GNUNET_snprintf (pos,
1042 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1044 GNUNET_SERVICE_client_continue (client);
1049 * Handle TEST-message by sending back TEST.
1051 * @param cls identification of the client
1052 * @param message the actual message
1055 handle_test (void *cls,
1056 const struct GNUNET_MessageHeader *message)
1058 struct GNUNET_SERVICE_Client *client = cls;
1059 struct GNUNET_MQ_Envelope *env;
1060 struct GNUNET_MessageHeader *msg;
1062 env = GNUNET_MQ_msg (msg,
1063 GNUNET_MESSAGE_TYPE_ARM_TEST);
1064 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1066 GNUNET_SERVICE_client_continue (client);
1071 * We are done with everything. Stop remaining
1072 * tasks, signal handler and the server.
1077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1078 "Last shutdown phase\n");
1079 if (NULL != notifier)
1081 GNUNET_notification_context_destroy (notifier);
1084 if (NULL != service)
1086 GNUNET_SERVICE_shutdown (service);
1089 if (NULL != child_death_task)
1091 GNUNET_SCHEDULER_cancel (child_death_task);
1092 child_death_task = NULL;
1098 * Count how many services are still active.
1100 * @param running_head list of services
1101 * @return number of active services found
1104 list_count (struct ServiceList *running_head)
1106 struct ServiceList *i;
1109 for (res = 0, i = running_head; i; i = i->next, res++)
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1118 * Task run for shutdown.
1120 * @param cls closure, NULL if we need to self-restart
1123 shutdown_task (void *cls)
1125 struct ServiceList *pos;
1126 struct ServiceList *nxt;
1127 struct ServiceListeningInfo *sli;
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130 "First shutdown phase\n");
1131 if (NULL != child_restart_task)
1133 GNUNET_SCHEDULER_cancel (child_restart_task);
1134 child_restart_task = NULL;
1136 in_shutdown = GNUNET_YES;
1137 /* first, stop listening */
1138 for (pos = running_head; NULL != pos; pos = pos->next)
1140 while (NULL != (sli = pos->listen_head))
1142 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
1145 if (NULL != sli->accept_task)
1147 GNUNET_SCHEDULER_cancel (sli->accept_task);
1148 sli->accept_task = NULL;
1150 GNUNET_break (GNUNET_OK ==
1151 GNUNET_NETWORK_socket_close (sli->listen_socket));
1152 GNUNET_free (sli->service_addr);
1156 /* then, shutdown all existing service processes */
1158 while (NULL != (pos = nxt))
1161 if (NULL != pos->proc)
1163 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1164 "Stopping service `%s'\n",
1166 pos->killed_at = GNUNET_TIME_absolute_get ();
1167 if (0 != GNUNET_OS_process_kill (pos->proc,
1169 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1177 /* finally, should all service processes be already gone, terminate for real */
1178 if (NULL == running_head)
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1182 "Delaying shutdown, have %u childs still running\n",
1183 list_count (running_head));
1188 * Task run whenever it is time to restart a child that died.
1190 * @param cls closure, always NULL
1193 delayed_restart_task (void *cls)
1196 struct ServiceList *sl;
1197 struct GNUNET_TIME_Relative lowestRestartDelay;
1198 struct ServiceListeningInfo *sli;
1200 child_restart_task = NULL;
1201 GNUNET_assert (GNUNET_NO == in_shutdown);
1202 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1204 /* check for services that need to be restarted due to
1205 * configuration changes or because the last restart failed */
1206 for (sl = running_head; NULL != sl; sl = sl->next)
1208 if (NULL != sl->proc)
1210 /* service is currently not running */
1211 if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
1213 /* restart is now allowed */
1214 if (sl->force_start)
1216 /* process should run by default, start immediately */
1217 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1218 _("Restarting service `%s'.\n"),
1226 /* process is run on-demand, ensure it is re-started if there is demand */
1227 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1228 if (NULL == sli->accept_task)
1230 /* accept was actually paused, so start it again */
1232 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1241 /* update calculation for earliest time to reactivate a service */
1242 lowestRestartDelay =
1243 GNUNET_TIME_relative_min (lowestRestartDelay,
1244 GNUNET_TIME_absolute_get_remaining
1248 if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1251 "Will restart process in %s\n",
1252 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay,
1254 child_restart_task =
1255 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1256 GNUNET_SCHEDULER_PRIORITY_IDLE,
1257 &delayed_restart_task,
1264 * Task triggered whenever we receive a SIGCHLD (child
1267 * @param cls closure, NULL if we need to self-restart
1270 maint_child_death (void *cls)
1272 struct ServiceList *pos;
1273 struct ServiceList *next;
1274 struct ServiceListeningInfo *sli;
1275 const char *statstr;
1279 enum GNUNET_OS_ProcessStatusType statusType;
1280 unsigned long statusCode;
1281 const struct GNUNET_DISK_FileHandle *pr;
1283 pr = GNUNET_DISK_pipe_handle (sigpipe,
1284 GNUNET_DISK_PIPE_END_READ);
1285 child_death_task = NULL;
1286 /* consume the signal */
1287 GNUNET_break (0 < GNUNET_DISK_file_read (pr,
1291 /* check for services that died (WAITPID) */
1292 next = running_head;
1293 while (NULL != (pos = next))
1297 if (NULL == pos->proc)
1299 if (GNUNET_YES == in_shutdown)
1304 if (NULL != wait_file)
1306 /* need to use 'wait4()' to obtain and log performance data */
1311 pid = GNUNET_OS_process_get_pid (pos->proc);
1317 continue; /* no process done */
1318 if (WIFEXITED (status))
1320 statusType = GNUNET_OS_PROCESS_EXITED;
1321 statusCode = WEXITSTATUS (status);
1323 else if (WIFSIGNALED (status))
1325 statusType = GNUNET_OS_PROCESS_SIGNALED;
1326 statusCode = WTERMSIG (status);
1328 else if (WIFSTOPPED (status))
1330 statusType = GNUNET_OS_PROCESS_SIGNALED;
1331 statusCode = WSTOPSIG (status);
1334 else if (WIFCONTINUED (status))
1336 statusType = GNUNET_OS_PROCESS_RUNNING;
1342 statusType = GNUNET_OS_PROCESS_UNKNOWN;
1345 if ( (GNUNET_OS_PROCESS_EXITED == statusType) ||
1346 (GNUNET_OS_PROCESS_SIGNALED == statusType) )
1348 double utime = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 10e6);
1349 double stime = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 10e6);
1351 "%s(%u) %.3f %.3f %llu %llu %llu %llu %llu\n",
1356 (unsigned long long) ru.ru_maxrss,
1357 (unsigned long long) ru.ru_inblock,
1358 (unsigned long long) ru.ru_oublock,
1359 (unsigned long long) ru.ru_nvcsw,
1360 (unsigned long long) ru.ru_nivcsw);
1363 else /* continue with JUST this "if" as "else" (intentionally no brackets!) */
1365 if ( (GNUNET_SYSERR ==
1367 GNUNET_OS_process_status (pos->proc,
1370 (ret == GNUNET_NO) ||
1371 (statusType == GNUNET_OS_PROCESS_STOPPED) ||
1372 (statusType == GNUNET_OS_PROCESS_UNKNOWN) ||
1373 (statusType == GNUNET_OS_PROCESS_RUNNING) )
1376 if (statusType == GNUNET_OS_PROCESS_EXITED)
1378 statstr = _( /* process termination method */ "exit");
1379 statcode = statusCode;
1381 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1383 statstr = _( /* process termination method */ "signal");
1384 statcode = statusCode;
1388 statstr = _( /* process termination method */ "unknown");
1391 if (0 != pos->killed_at.abs_value_us)
1393 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1394 _("Service `%s' took %s to terminate\n"),
1396 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at),
1399 GNUNET_OS_process_destroy (pos->proc);
1401 broadcast_status (pos->name,
1402 GNUNET_ARM_SERVICE_STOPPED,
1404 if (NULL != pos->killing_client)
1406 signal_result (pos->killing_client, pos->name,
1407 pos->killing_client_request_id,
1408 GNUNET_ARM_RESULT_STOPPED);
1409 pos->killing_client = NULL;
1410 pos->killing_client_request_id = 0;
1412 if (GNUNET_YES != in_shutdown)
1414 if ( (statusType == GNUNET_OS_PROCESS_EXITED) &&
1417 /* process terminated normally, allow restart at any time */
1418 pos->restart_at.abs_value_us = 0;
1419 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1420 _("Service `%s' terminated normally, will restart at any time\n"),
1422 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1423 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1425 GNUNET_break (NULL == sli->accept_task);
1427 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1435 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1436 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1440 GNUNET_STRINGS_relative_time_to_string (pos->backoff,
1442 /* schedule restart */
1443 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1444 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1445 if (NULL != child_restart_task)
1446 GNUNET_SCHEDULER_cancel (child_restart_task);
1448 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1449 &delayed_restart_task,
1458 child_death_task = GNUNET_SCHEDULER_add_read_file (
1459 GNUNET_TIME_UNIT_FOREVER_REL,
1461 &maint_child_death, NULL);
1462 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1464 else if (GNUNET_YES == in_shutdown)
1465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1466 "Delaying shutdown after child's death, still have %u children\n",
1467 list_count (running_head));
1473 * Signal handler called for SIGCHLD. Triggers the
1474 * respective handler by writing to the trigger pipe.
1477 sighandler_child_death ()
1480 int old_errno = errno; /* back-up errno */
1483 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
1484 GNUNET_DISK_PIPE_END_WRITE),
1487 errno = old_errno; /* restore errno */
1492 * Setup our service record for the given section in the configuration file
1493 * (assuming the section is for a service).
1496 * @param section a section in the configuration file
1497 * @return #GNUNET_OK (continue)
1500 setup_service (void *cls,
1501 const char *section)
1503 struct ServiceList *sl;
1507 struct sockaddr **addrs;
1508 socklen_t *addr_lens;
1512 if (0 == strcasecmp (section,
1516 GNUNET_CONFIGURATION_get_value_string (cfg,
1521 /* not a service section */
1525 GNUNET_CONFIGURATION_have_value (cfg,
1529 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1533 if (GNUNET_NO == start_user)
1535 GNUNET_free (binary);
1536 return; /* user service, and we don't deal with those */
1541 if (GNUNET_NO == start_system)
1543 GNUNET_free (binary);
1544 return; /* system service, and we don't deal with those */
1547 sl = find_service (section);
1550 /* got the same section twice!? */
1552 GNUNET_free (binary);
1557 GNUNET_CONFIGURATION_get_value_filename (cfg, section,
1561 GNUNET_CONFIGURATION_get_value_filename (cfg,
1565 (0 != STAT (config, &sbuf)))
1569 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1572 GNUNET_free (config);
1576 sl = GNUNET_new (struct ServiceList);
1577 sl->name = GNUNET_strdup (section);
1578 sl->binary = binary;
1579 sl->config = config;
1580 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1581 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1583 sl->pipe_control = GNUNET_YES;
1585 if (GNUNET_CONFIGURATION_have_value (cfg,
1588 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1592 GNUNET_CONTAINER_DLL_insert (running_head,
1596 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1600 sl->force_start = GNUNET_YES;
1602 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1610 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1615 if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section,
1620 /* this will free (or capture) addrs[i] */
1621 for (i = 0; i < ret; i++)
1622 create_listen_socket (addrs[i],
1625 GNUNET_free (addrs);
1626 GNUNET_free (addr_lens);
1631 * A client connected, mark as a monitoring client.
1633 * @param cls closure
1634 * @param client identification of the client
1635 * @param mq queue to talk to @a client
1639 client_connect_cb (void *cls,
1640 struct GNUNET_SERVICE_Client *client,
1641 struct GNUNET_MQ_Handle *mq)
1643 /* All clients are considered to be of the "monitor" kind
1644 * (that is, they don't affect ARM shutdown).
1646 GNUNET_SERVICE_client_mark_monitor (client);
1652 * A client disconnected, clean up associated state.
1654 * @param cls closure
1655 * @param client identification of the client
1656 * @param app_ctx must match @a client
1659 client_disconnect_cb (void *cls,
1660 struct GNUNET_SERVICE_Client *client,
1663 struct ServiceList *sl;
1665 GNUNET_assert (client == app_ctx);
1667 for (sl = running_head; NULL != sl; sl = sl->next)
1668 if (sl->killing_client == client)
1669 sl->killing_client = NULL;
1674 * Handle MONITOR-message.
1676 * @param cls identification of the client
1677 * @param message the actual message
1678 * @return #GNUNET_OK to keep the connection open,
1679 * #GNUNET_SYSERR to close it (signal serious error)
1682 handle_monitor (void *cls,
1683 const struct GNUNET_MessageHeader *message)
1685 struct GNUNET_SERVICE_Client *client = cls;
1687 /* FIXME: might want to start by letting monitor know about
1688 services that are already running */
1689 /* Removal is handled by the server implementation, internally. */
1690 GNUNET_notification_context_add (notifier,
1691 GNUNET_SERVICE_client_get_mq (client));
1692 broadcast_status ("arm",
1693 GNUNET_ARM_SERVICE_MONITORING_STARTED,
1695 GNUNET_SERVICE_client_continue (client);
1700 * Process arm requests.
1702 * @param cls closure
1703 * @param serv the initialized service
1704 * @param c configuration to use
1708 const struct GNUNET_CONFIGURATION_Handle *c,
1709 struct GNUNET_SERVICE_Handle *serv)
1711 struct ServiceList *sl;
1715 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1718 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1719 GNUNET_DISK_pipe_handle (sigpipe,
1720 GNUNET_DISK_PIPE_END_READ),
1725 GNUNET_CONFIGURATION_get_value_filename (cfg,
1727 "RESOURCE_DIAGNOSTICS",
1730 wait_file = fopen (wait_filename,
1732 if (NULL == wait_file)
1734 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1741 GNUNET_CONFIGURATION_get_value_string (cfg,
1745 prefix_command = GNUNET_strdup ("");
1747 GNUNET_CONFIGURATION_get_value_string (cfg,
1751 final_option = GNUNET_strdup ("");
1753 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1757 GNUNET_break (GNUNET_YES == start_user);
1758 start_system = GNUNET_NO;
1761 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1765 GNUNET_break (GNUNET_YES == start_system);
1766 start_user = GNUNET_NO;
1768 GNUNET_CONFIGURATION_iterate_sections (cfg,
1772 /* start default services... */
1773 for (sl = running_head; NULL != sl; sl = sl->next)
1774 if (GNUNET_YES == sl->force_start)
1778 notifier = GNUNET_notification_context_create (MAX_NOTIFY_QUEUE);
1783 * The main function for the arm service.
1785 * @param argc number of arguments from the command line
1786 * @param argv command line arguments
1787 * @return 0 ok, 1 on error
1794 struct GNUNET_SIGNAL_Context *shc_chld;
1795 struct GNUNET_MQ_MessageHandler handlers[] = {
1796 GNUNET_MQ_hd_var_size (start,
1797 GNUNET_MESSAGE_TYPE_ARM_START,
1798 struct GNUNET_ARM_Message,
1800 GNUNET_MQ_hd_var_size (stop,
1801 GNUNET_MESSAGE_TYPE_ARM_STOP,
1802 struct GNUNET_ARM_Message,
1804 GNUNET_MQ_hd_fixed_size (monitor,
1805 GNUNET_MESSAGE_TYPE_ARM_MONITOR,
1806 struct GNUNET_MessageHeader,
1808 GNUNET_MQ_hd_fixed_size (list,
1809 GNUNET_MESSAGE_TYPE_ARM_LIST,
1810 struct GNUNET_ARM_Message,
1812 GNUNET_MQ_hd_fixed_size (test,
1813 GNUNET_MESSAGE_TYPE_ARM_TEST,
1814 struct GNUNET_MessageHeader,
1816 GNUNET_MQ_handler_end ()
1819 sigpipe = GNUNET_DISK_pipe (GNUNET_NO,
1823 GNUNET_assert (NULL != sigpipe);
1825 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD,
1826 &sighandler_child_death);
1827 ret = GNUNET_SERVICE_ruN_ (argc,
1830 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN,
1833 &client_disconnect_cb,
1837 if (NULL != wait_file)
1842 if (NULL != wait_filename)
1844 GNUNET_free (wait_filename);
1845 wait_filename = NULL;
1848 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1850 GNUNET_DISK_pipe_close (sigpipe);
1856 #if defined(LINUX) && defined(__GLIBC__)
1860 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1862 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1864 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1865 mallopt (M_TOP_PAD, 1 * 1024);
1871 /* end of gnunet-service-arm.c */