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 if (NULL != notifier)
322 GNUNET_notification_context_broadcast (notifier,
325 GNUNET_MQ_discard (env);
329 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (unicast),
336 * Actually start the process for the given service.
338 * @param sl identifies service to start
339 * @param client that asked to start the service (may be NULL)
340 * @param request_id id of the request in response to which the process is
341 * being started. 0 if starting was not requested.
344 start_process (struct ServiceList *sl,
345 struct GNUNET_SERVICE_Client *client,
351 int is_simple_service;
352 struct ServiceListeningInfo *sli;
358 /* calculate listen socket list */
361 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
363 GNUNET_array_append (lsocks, ls,
364 GNUNET_NETWORK_get_fd (sli->listen_socket));
365 if (NULL != sli->accept_task)
367 GNUNET_SCHEDULER_cancel (sli->accept_task);
368 sli->accept_task = NULL;
372 GNUNET_array_append (lsocks,
376 GNUNET_array_append (lsocks,
381 /* obtain configuration */
383 GNUNET_CONFIGURATION_get_value_string (cfg,
387 loprefix = GNUNET_strdup (prefix_command);
389 loprefix = GNUNET_CONFIGURATION_expand_dollar (cfg,
392 GNUNET_CONFIGURATION_get_value_string (cfg,
398 options = GNUNET_CONFIGURATION_expand_dollar (cfg,
405 fin_options = GNUNET_strdup (final_option);
406 /* replace '{}' with service name */
407 while (NULL != (optpos = strstr (fin_options,
410 /* terminate string at opening parenthesis */
412 GNUNET_asprintf (&new_options,
417 GNUNET_free (fin_options);
418 fin_options = new_options;
422 /* combine "fin_options" with "options" */
424 GNUNET_asprintf (&options,
428 GNUNET_free (optpos);
432 /* only have "fin_options", use that */
433 options = fin_options;
436 options = GNUNET_CONFIGURATION_expand_dollar (cfg,
438 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg,
442 const char *service_type = NULL;
443 const char *choices[] = { "GNUNET", "SIMPLE", NULL };
445 is_simple_service = GNUNET_NO;
447 GNUNET_CONFIGURATION_get_value_choice (cfg,
452 (0 == strcasecmp (service_type, "SIMPLE")) )
453 is_simple_service = GNUNET_YES;
456 GNUNET_assert (NULL == sl->proc);
457 if (GNUNET_YES == is_simple_service)
459 /* A simple service will receive no GNUnet specific
460 command line options. */
461 binary = GNUNET_strdup (sl->binary);
462 binary = GNUNET_CONFIGURATION_expand_dollar (cfg, binary);
463 GNUNET_asprintf ("edbinary,
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
467 "Starting simple service `%s' using binary `%s'\n",
468 sl->name, sl->binary);
469 /* FIXME: dollar expansion should only be done outside
470 * of ''-quoted strings, escaping should be considered. */
472 options = GNUNET_CONFIGURATION_expand_dollar (cfg, options);
474 GNUNET_OS_start_process_s (sl->pipe_control,
475 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
484 /* actually start process */
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486 "Starting service `%s' using binary `%s' and configuration `%s'\n",
487 sl->name, sl->binary, sl->config);
488 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
489 GNUNET_asprintf ("edbinary,
493 if (GNUNET_YES == use_debug)
495 if (NULL == sl->config)
497 GNUNET_OS_start_process_s (sl->pipe_control,
498 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
507 GNUNET_OS_start_process_s (sl->pipe_control,
508 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
519 if (NULL == sl->config)
521 GNUNET_OS_start_process_s (sl->pipe_control,
522 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
530 GNUNET_OS_start_process_s (sl->pipe_control,
531 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
540 GNUNET_free (binary);
541 GNUNET_free (quotedbinary);
542 if (NULL == sl->proc)
544 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
545 _("Failed to start service `%s'\n"),
548 signal_result (client,
551 GNUNET_ARM_RESULT_START_FAILED);
555 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
556 _("Starting service `%s'\n"),
558 broadcast_status (sl->name,
559 GNUNET_ARM_SERVICE_STARTING,
562 signal_result (client,
565 GNUNET_ARM_RESULT_STARTING);
568 GNUNET_free (loprefix);
569 GNUNET_free (options);
570 GNUNET_array_grow (lsocks,
577 * Find the process with the given service
578 * name in the given list and return it.
580 * @param name which service entry to look up
581 * @return NULL if it was not found
583 static struct ServiceList *
584 find_service (const char *name)
586 struct ServiceList *sl;
591 if (0 == strcasecmp (sl->name, name))
600 * First connection has come to the listening socket associated with the service,
601 * create the service in order to relay the incoming connection to it
603 * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
606 accept_connection (void *cls)
608 struct ServiceListeningInfo *sli = cls;
609 struct ServiceList *sl = sli->sl;
611 sli->accept_task = NULL;
612 GNUNET_assert (GNUNET_NO == in_shutdown);
613 start_process (sl, NULL, 0);
618 * Creating a listening socket for each of the service's addresses and
619 * wait for the first incoming connection to it
621 * @param sa address associated with the service
622 * @param addr_len length of @a sa
623 * @param sl service entry for the service in question
626 create_listen_socket (struct sockaddr *sa,
628 struct ServiceList *sl)
631 struct GNUNET_NETWORK_Handle *sock;
632 struct ServiceListeningInfo *sli;
638 switch (sa->sa_family)
641 sock = GNUNET_NETWORK_socket_create (PF_INET,
646 sock = GNUNET_NETWORK_socket_create (PF_INET6,
651 if (0 == strcmp (GNUNET_a2s (sa,
653 "@")) /* Do not bind to blank UNIX path! */
655 sock = GNUNET_NETWORK_socket_create (PF_UNIX,
662 errno = EAFNOSUPPORT;
667 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
668 _("Unable to create socket for service `%s': %s\n"),
675 GNUNET_NETWORK_socket_setsockopt (sock,
680 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
683 if ( (sa->sa_family == AF_INET6) &&
685 GNUNET_NETWORK_socket_setsockopt (sock,
690 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
694 if (AF_UNIX == sa->sa_family)
695 GNUNET_NETWORK_unix_precheck ((struct sockaddr_un *) sa);
698 GNUNET_NETWORK_socket_bind (sock,
699 (const struct sockaddr *) sa,
702 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
703 _("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
708 GNUNET_break (GNUNET_OK ==
709 GNUNET_NETWORK_socket_close (sock));
714 if ((AF_UNIX == sa->sa_family)
716 /* Permission settings are not required when abstract sockets are used */
717 && ('\0' != ((const struct sockaddr_un *)sa)->sun_path[0])
722 GNUNET_CONFIGURATION_get_value_yesno (cfg,
726 GNUNET_CONFIGURATION_get_value_yesno (cfg,
729 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sa)->sun_path,
736 GNUNET_NETWORK_socket_listen (sock, 5))
738 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
740 GNUNET_break (GNUNET_OK ==
741 GNUNET_NETWORK_socket_close (sock));
745 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
746 _("ARM now monitors connections to service `%s' at `%s'\n"),
750 sli = GNUNET_new (struct ServiceListeningInfo);
751 sli->service_addr = sa;
752 sli->service_addr_len = addr_len;
753 sli->listen_socket = sock;
756 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
758 &accept_connection, sli);
759 GNUNET_CONTAINER_DLL_insert (sl->listen_head,
766 * Remove and free an entry in the service list. Listen sockets
767 * must have already been cleaned up. Only to be called during shutdown.
769 * @param sl entry to free
772 free_service (struct ServiceList *sl)
774 GNUNET_assert (GNUNET_YES == in_shutdown);
775 GNUNET_CONTAINER_DLL_remove (running_head,
778 GNUNET_assert (NULL == sl->listen_head);
779 GNUNET_free_non_null (sl->config);
780 GNUNET_free_non_null (sl->binary);
781 GNUNET_free (sl->name);
787 * Check START-message.
789 * @param cls identification of the client
790 * @param amsg the actual message
791 * @return #GNUNET_OK to keep the connection open,
792 * #GNUNET_SYSERR to close it (signal serious error)
795 check_start (void *cls,
796 const struct GNUNET_ARM_Message *amsg)
799 const char *servicename;
801 size = ntohs (amsg->header.size) - sizeof (struct GNUNET_ARM_Message);
802 servicename = (const char *) &amsg[1];
804 (servicename[size - 1] != '\0') )
807 return GNUNET_SYSERR;
814 * Handle START-message.
816 * @param cls identification of the client
817 * @param amsg the actual message
820 handle_start (void *cls,
821 const struct GNUNET_ARM_Message *amsg)
823 struct GNUNET_SERVICE_Client *client = cls;
824 const char *servicename;
825 struct ServiceList *sl;
828 request_id = GNUNET_ntohll (amsg->request_id);
829 servicename = (const char *) &amsg[1];
830 GNUNET_SERVICE_client_continue (client);
831 if (GNUNET_YES == in_shutdown)
833 signal_result (client,
836 GNUNET_ARM_RESULT_IN_SHUTDOWN);
839 sl = find_service (servicename);
842 signal_result (client,
845 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
848 sl->force_start = GNUNET_YES;
849 if (NULL != sl->proc)
851 signal_result (client,
854 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
864 * Start a shutdown sequence.
866 * @param cls closure (refers to service)
869 trigger_shutdown (void *cls)
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
872 "Triggering shutdown\n");
873 GNUNET_SCHEDULER_shutdown ();
878 * Check STOP-message.
880 * @param cls identification of the client
881 * @param amsg the actual message
882 * @return #GNUNET_OK to keep the connection open,
883 * #GNUNET_SYSERR to close it (signal serious error)
886 check_stop (void *cls,
887 const struct GNUNET_ARM_Message *amsg)
890 const char *servicename;
892 size = ntohs (amsg->header.size) - sizeof (struct GNUNET_ARM_Message);
893 servicename = (const char *) &amsg[1];
895 (servicename[size - 1] != '\0') )
898 return GNUNET_SYSERR;
905 * Handle STOP-message.
907 * @param cls identification of the client
908 * @param amsg the actual message
911 handle_stop (void *cls,
912 const struct GNUNET_ARM_Message *amsg)
914 struct GNUNET_SERVICE_Client *client = cls;
915 struct ServiceList *sl;
916 const char *servicename;
919 request_id = GNUNET_ntohll (amsg->request_id);
920 servicename = (const char *) &amsg[1];
921 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
922 _("Preparing to stop `%s'\n"),
924 GNUNET_SERVICE_client_continue (client);
925 if (0 == strcasecmp (servicename,
928 broadcast_status (servicename,
929 GNUNET_ARM_SERVICE_STOPPING,
931 signal_result (client,
934 GNUNET_ARM_RESULT_STOPPING);
935 GNUNET_SERVICE_client_persist (client);
936 GNUNET_SCHEDULER_add_now (&trigger_shutdown,
940 sl = find_service (servicename);
943 signal_result (client,
946 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
949 sl->force_start = GNUNET_NO;
950 if (GNUNET_YES == in_shutdown)
952 /* shutdown in progress */
953 signal_result (client,
956 GNUNET_ARM_RESULT_IN_SHUTDOWN);
959 if (NULL != sl->killing_client)
961 /* killing already in progress */
962 signal_result (client,
965 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
968 if (NULL == sl->proc)
970 /* process is down */
971 signal_result (client,
974 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "Sending kill signal to service `%s', waiting for process to die.\n",
980 broadcast_status (servicename,
981 GNUNET_ARM_SERVICE_STOPPING,
983 /* no signal_start - only when it's STOPPED */
984 sl->killed_at = GNUNET_TIME_absolute_get ();
985 if (0 != GNUNET_OS_process_kill (sl->proc,
987 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
989 sl->killing_client = client;
990 sl->killing_client_request_id = request_id;
995 * Handle LIST-message.
997 * @param cls identification of the client
998 * @param message the actual message
1001 handle_list (void *cls,
1002 const struct GNUNET_ARM_Message *request)
1004 struct GNUNET_SERVICE_Client *client = cls;
1005 struct GNUNET_MQ_Envelope *env;
1006 struct GNUNET_ARM_ListResultMessage *msg;
1007 size_t string_list_size;
1008 struct ServiceList *sl;
1012 GNUNET_break (0 == ntohl (request->reserved));
1014 string_list_size = 0;
1016 /* first count the running processes get their name's size */
1017 for (sl = running_head; NULL != sl; sl = sl->next)
1019 if (NULL != sl->proc)
1021 string_list_size += strlen (sl->name);
1022 string_list_size += strlen (sl->binary);
1023 string_list_size += 4;
1028 env = GNUNET_MQ_msg_extra (msg,
1030 GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT);
1031 msg->arm_msg.request_id = request->request_id;
1032 msg->count = htons (count);
1034 pos = (char *) &msg[1];
1035 for (sl = running_head; NULL != sl; sl = sl->next)
1037 if (NULL != sl->proc)
1039 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
1040 GNUNET_snprintf (pos,
1048 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1050 GNUNET_SERVICE_client_continue (client);
1055 * Handle TEST-message by sending back TEST.
1057 * @param cls identification of the client
1058 * @param message the actual message
1061 handle_test (void *cls,
1062 const struct GNUNET_MessageHeader *message)
1064 struct GNUNET_SERVICE_Client *client = cls;
1065 struct GNUNET_MQ_Envelope *env;
1066 struct GNUNET_MessageHeader *msg;
1068 env = GNUNET_MQ_msg (msg,
1069 GNUNET_MESSAGE_TYPE_ARM_TEST);
1070 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1072 GNUNET_SERVICE_client_continue (client);
1077 * We are done with everything. Stop remaining
1078 * tasks, signal handler and the server.
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "Last shutdown phase\n");
1085 if (NULL != notifier)
1087 GNUNET_notification_context_destroy (notifier);
1090 if (NULL != service)
1092 GNUNET_SERVICE_shutdown (service);
1095 if (NULL != child_death_task)
1097 GNUNET_SCHEDULER_cancel (child_death_task);
1098 child_death_task = NULL;
1104 * Count how many services are still active.
1106 * @param running_head list of services
1107 * @return number of active services found
1110 list_count (struct ServiceList *running_head)
1112 struct ServiceList *i;
1115 for (res = 0, i = running_head; i; i = i->next, res++)
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 * Task run for shutdown.
1126 * @param cls closure, NULL if we need to self-restart
1129 shutdown_task (void *cls)
1131 struct ServiceList *pos;
1132 struct ServiceList *nxt;
1133 struct ServiceListeningInfo *sli;
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136 "First shutdown phase\n");
1137 if (NULL != child_restart_task)
1139 GNUNET_SCHEDULER_cancel (child_restart_task);
1140 child_restart_task = NULL;
1142 in_shutdown = GNUNET_YES;
1143 /* first, stop listening */
1144 for (pos = running_head; NULL != pos; pos = pos->next)
1146 while (NULL != (sli = pos->listen_head))
1148 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
1151 if (NULL != sli->accept_task)
1153 GNUNET_SCHEDULER_cancel (sli->accept_task);
1154 sli->accept_task = NULL;
1156 GNUNET_break (GNUNET_OK ==
1157 GNUNET_NETWORK_socket_close (sli->listen_socket));
1158 GNUNET_free (sli->service_addr);
1162 /* then, shutdown all existing service processes */
1164 while (NULL != (pos = nxt))
1167 if (NULL != pos->proc)
1169 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1170 "Stopping service `%s'\n",
1172 pos->killed_at = GNUNET_TIME_absolute_get ();
1173 if (0 != GNUNET_OS_process_kill (pos->proc,
1175 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1183 /* finally, should all service processes be already gone, terminate for real */
1184 if (NULL == running_head)
1187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1188 "Delaying shutdown, have %u childs still running\n",
1189 list_count (running_head));
1194 * Task run whenever it is time to restart a child that died.
1196 * @param cls closure, always NULL
1199 delayed_restart_task (void *cls)
1202 struct ServiceList *sl;
1203 struct GNUNET_TIME_Relative lowestRestartDelay;
1204 struct ServiceListeningInfo *sli;
1206 child_restart_task = NULL;
1207 GNUNET_assert (GNUNET_NO == in_shutdown);
1208 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1210 /* check for services that need to be restarted due to
1211 * configuration changes or because the last restart failed */
1212 for (sl = running_head; NULL != sl; sl = sl->next)
1214 if (NULL != sl->proc)
1216 /* service is currently not running */
1217 if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
1219 /* restart is now allowed */
1220 if (sl->force_start)
1222 /* process should run by default, start immediately */
1223 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1224 _("Restarting service `%s'.\n"),
1232 /* process is run on-demand, ensure it is re-started if there is demand */
1233 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1234 if (NULL == sli->accept_task)
1236 /* accept was actually paused, so start it again */
1238 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1247 /* update calculation for earliest time to reactivate a service */
1248 lowestRestartDelay =
1249 GNUNET_TIME_relative_min (lowestRestartDelay,
1250 GNUNET_TIME_absolute_get_remaining
1254 if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 "Will restart process in %s\n",
1258 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay,
1260 child_restart_task =
1261 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1262 GNUNET_SCHEDULER_PRIORITY_IDLE,
1263 &delayed_restart_task,
1270 * Task triggered whenever we receive a SIGCHLD (child
1273 * @param cls closure, NULL if we need to self-restart
1276 maint_child_death (void *cls)
1278 struct ServiceList *pos;
1279 struct ServiceList *next;
1280 struct ServiceListeningInfo *sli;
1281 const char *statstr;
1285 enum GNUNET_OS_ProcessStatusType statusType;
1286 unsigned long statusCode;
1287 const struct GNUNET_DISK_FileHandle *pr;
1289 pr = GNUNET_DISK_pipe_handle (sigpipe,
1290 GNUNET_DISK_PIPE_END_READ);
1291 child_death_task = NULL;
1292 /* consume the signal */
1293 GNUNET_break (0 < GNUNET_DISK_file_read (pr,
1297 /* check for services that died (WAITPID) */
1298 next = running_head;
1299 while (NULL != (pos = next))
1303 if (NULL == pos->proc)
1305 if (GNUNET_YES == in_shutdown)
1310 if (NULL != wait_file)
1312 /* need to use 'wait4()' to obtain and log performance data */
1317 pid = GNUNET_OS_process_get_pid (pos->proc);
1323 continue; /* no process done */
1324 if (WIFEXITED (status))
1326 statusType = GNUNET_OS_PROCESS_EXITED;
1327 statusCode = WEXITSTATUS (status);
1329 else if (WIFSIGNALED (status))
1331 statusType = GNUNET_OS_PROCESS_SIGNALED;
1332 statusCode = WTERMSIG (status);
1334 else if (WIFSTOPPED (status))
1336 statusType = GNUNET_OS_PROCESS_SIGNALED;
1337 statusCode = WSTOPSIG (status);
1340 else if (WIFCONTINUED (status))
1342 statusType = GNUNET_OS_PROCESS_RUNNING;
1348 statusType = GNUNET_OS_PROCESS_UNKNOWN;
1351 if ( (GNUNET_OS_PROCESS_EXITED == statusType) ||
1352 (GNUNET_OS_PROCESS_SIGNALED == statusType) )
1354 double utime = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 10e6);
1355 double stime = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 10e6);
1357 "%s(%u) %.3f %.3f %llu %llu %llu %llu %llu\n",
1362 (unsigned long long) ru.ru_maxrss,
1363 (unsigned long long) ru.ru_inblock,
1364 (unsigned long long) ru.ru_oublock,
1365 (unsigned long long) ru.ru_nvcsw,
1366 (unsigned long long) ru.ru_nivcsw);
1369 else /* continue with JUST this "if" as "else" (intentionally no brackets!) */
1371 if ( (GNUNET_SYSERR ==
1373 GNUNET_OS_process_status (pos->proc,
1376 (ret == GNUNET_NO) ||
1377 (statusType == GNUNET_OS_PROCESS_STOPPED) ||
1378 (statusType == GNUNET_OS_PROCESS_UNKNOWN) ||
1379 (statusType == GNUNET_OS_PROCESS_RUNNING) )
1382 if (statusType == GNUNET_OS_PROCESS_EXITED)
1384 statstr = _( /* process termination method */ "exit");
1385 statcode = statusCode;
1387 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1389 statstr = _( /* process termination method */ "signal");
1390 statcode = statusCode;
1394 statstr = _( /* process termination method */ "unknown");
1397 if (0 != pos->killed_at.abs_value_us)
1399 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1400 _("Service `%s' took %s to terminate\n"),
1402 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at),
1405 GNUNET_OS_process_destroy (pos->proc);
1407 broadcast_status (pos->name,
1408 GNUNET_ARM_SERVICE_STOPPED,
1410 if (NULL != pos->killing_client)
1412 signal_result (pos->killing_client, pos->name,
1413 pos->killing_client_request_id,
1414 GNUNET_ARM_RESULT_STOPPED);
1415 pos->killing_client = NULL;
1416 pos->killing_client_request_id = 0;
1418 if (GNUNET_YES != in_shutdown)
1420 if ( (statusType == GNUNET_OS_PROCESS_EXITED) &&
1423 /* process terminated normally, allow restart at any time */
1424 pos->restart_at.abs_value_us = 0;
1425 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1426 _("Service `%s' terminated normally, will restart at any time\n"),
1428 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1429 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1431 GNUNET_break (NULL == sli->accept_task);
1433 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1441 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1442 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1446 GNUNET_STRINGS_relative_time_to_string (pos->backoff,
1448 /* schedule restart */
1449 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1450 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1451 if (NULL != child_restart_task)
1452 GNUNET_SCHEDULER_cancel (child_restart_task);
1454 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1455 &delayed_restart_task,
1464 child_death_task = GNUNET_SCHEDULER_add_read_file (
1465 GNUNET_TIME_UNIT_FOREVER_REL,
1467 &maint_child_death, NULL);
1468 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1470 else if (GNUNET_YES == in_shutdown)
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "Delaying shutdown after child's death, still have %u children\n",
1473 list_count (running_head));
1479 * Signal handler called for SIGCHLD. Triggers the
1480 * respective handler by writing to the trigger pipe.
1483 sighandler_child_death ()
1486 int old_errno = errno; /* back-up errno */
1489 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
1490 GNUNET_DISK_PIPE_END_WRITE),
1493 errno = old_errno; /* restore errno */
1498 * Setup our service record for the given section in the configuration file
1499 * (assuming the section is for a service).
1502 * @param section a section in the configuration file
1503 * @return #GNUNET_OK (continue)
1506 setup_service (void *cls,
1507 const char *section)
1509 struct ServiceList *sl;
1513 struct sockaddr **addrs;
1514 socklen_t *addr_lens;
1518 if (0 == strcasecmp (section,
1522 GNUNET_CONFIGURATION_get_value_string (cfg,
1527 /* not a service section */
1531 GNUNET_CONFIGURATION_have_value (cfg,
1535 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1539 if (GNUNET_NO == start_user)
1541 GNUNET_free (binary);
1542 return; /* user service, and we don't deal with those */
1547 if (GNUNET_NO == start_system)
1549 GNUNET_free (binary);
1550 return; /* system service, and we don't deal with those */
1553 sl = find_service (section);
1556 /* got the same section twice!? */
1558 GNUNET_free (binary);
1563 GNUNET_CONFIGURATION_get_value_filename (cfg,
1568 GNUNET_CONFIGURATION_get_value_filename (cfg,
1572 (0 != STAT (config, &sbuf)))
1576 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1579 GNUNET_free (config);
1583 sl = GNUNET_new (struct ServiceList);
1584 sl->name = GNUNET_strdup (section);
1585 sl->binary = binary;
1586 sl->config = config;
1587 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1588 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1590 sl->pipe_control = GNUNET_YES;
1592 if (GNUNET_CONFIGURATION_have_value (cfg,
1595 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1599 GNUNET_CONTAINER_DLL_insert (running_head,
1603 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1607 sl->force_start = GNUNET_YES;
1609 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1617 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1622 if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section,
1627 /* this will free (or capture) addrs[i] */
1628 for (i = 0; i < ret; i++)
1629 create_listen_socket (addrs[i],
1632 GNUNET_free (addrs);
1633 GNUNET_free (addr_lens);
1638 * A client connected, mark as a monitoring client.
1640 * @param cls closure
1641 * @param client identification of the client
1642 * @param mq queue to talk to @a client
1646 client_connect_cb (void *cls,
1647 struct GNUNET_SERVICE_Client *client,
1648 struct GNUNET_MQ_Handle *mq)
1650 /* All clients are considered to be of the "monitor" kind
1651 * (that is, they don't affect ARM shutdown).
1653 GNUNET_SERVICE_client_mark_monitor (client);
1659 * A client disconnected, clean up associated state.
1661 * @param cls closure
1662 * @param client identification of the client
1663 * @param app_ctx must match @a client
1666 client_disconnect_cb (void *cls,
1667 struct GNUNET_SERVICE_Client *client,
1670 struct ServiceList *sl;
1672 GNUNET_assert (client == app_ctx);
1674 for (sl = running_head; NULL != sl; sl = sl->next)
1675 if (sl->killing_client == client)
1676 sl->killing_client = NULL;
1681 * Handle MONITOR-message.
1683 * @param cls identification of the client
1684 * @param message the actual message
1685 * @return #GNUNET_OK to keep the connection open,
1686 * #GNUNET_SYSERR to close it (signal serious error)
1689 handle_monitor (void *cls,
1690 const struct GNUNET_MessageHeader *message)
1692 struct GNUNET_SERVICE_Client *client = cls;
1694 /* FIXME: might want to start by letting monitor know about
1695 services that are already running */
1696 /* Removal is handled by the server implementation, internally. */
1697 GNUNET_notification_context_add (notifier,
1698 GNUNET_SERVICE_client_get_mq (client));
1699 broadcast_status ("arm",
1700 GNUNET_ARM_SERVICE_MONITORING_STARTED,
1702 GNUNET_SERVICE_client_continue (client);
1707 * Process arm requests.
1709 * @param cls closure
1710 * @param serv the initialized service
1711 * @param c configuration to use
1715 const struct GNUNET_CONFIGURATION_Handle *c,
1716 struct GNUNET_SERVICE_Handle *serv)
1718 struct ServiceList *sl;
1722 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1725 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1726 GNUNET_DISK_pipe_handle (sigpipe,
1727 GNUNET_DISK_PIPE_END_READ),
1732 GNUNET_CONFIGURATION_get_value_filename (cfg,
1734 "RESOURCE_DIAGNOSTICS",
1737 wait_file = fopen (wait_filename,
1739 if (NULL == wait_file)
1741 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1748 GNUNET_CONFIGURATION_get_value_string (cfg,
1752 prefix_command = GNUNET_strdup ("");
1754 prefix_command = GNUNET_CONFIGURATION_expand_dollar (cfg,
1757 GNUNET_CONFIGURATION_get_value_string (cfg,
1761 final_option = GNUNET_strdup ("");
1763 final_option = GNUNET_CONFIGURATION_expand_dollar (cfg,
1766 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1770 GNUNET_break (GNUNET_YES == start_user);
1771 start_system = GNUNET_NO;
1774 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1778 GNUNET_break (GNUNET_YES == start_system);
1779 start_user = GNUNET_NO;
1781 GNUNET_CONFIGURATION_iterate_sections (cfg,
1785 /* start default services... */
1786 for (sl = running_head; NULL != sl; sl = sl->next)
1787 if (GNUNET_YES == sl->force_start)
1791 notifier = GNUNET_notification_context_create (MAX_NOTIFY_QUEUE);
1796 * The main function for the arm service.
1798 * @param argc number of arguments from the command line
1799 * @param argv command line arguments
1800 * @return 0 ok, 1 on error
1807 struct GNUNET_SIGNAL_Context *shc_chld;
1808 struct GNUNET_MQ_MessageHandler handlers[] = {
1809 GNUNET_MQ_hd_var_size (start,
1810 GNUNET_MESSAGE_TYPE_ARM_START,
1811 struct GNUNET_ARM_Message,
1813 GNUNET_MQ_hd_var_size (stop,
1814 GNUNET_MESSAGE_TYPE_ARM_STOP,
1815 struct GNUNET_ARM_Message,
1817 GNUNET_MQ_hd_fixed_size (monitor,
1818 GNUNET_MESSAGE_TYPE_ARM_MONITOR,
1819 struct GNUNET_MessageHeader,
1821 GNUNET_MQ_hd_fixed_size (list,
1822 GNUNET_MESSAGE_TYPE_ARM_LIST,
1823 struct GNUNET_ARM_Message,
1825 GNUNET_MQ_hd_fixed_size (test,
1826 GNUNET_MESSAGE_TYPE_ARM_TEST,
1827 struct GNUNET_MessageHeader,
1829 GNUNET_MQ_handler_end ()
1832 sigpipe = GNUNET_DISK_pipe (GNUNET_NO,
1836 GNUNET_assert (NULL != sigpipe);
1838 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD,
1839 &sighandler_child_death);
1840 ret = GNUNET_SERVICE_ruN_ (argc,
1843 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN,
1846 &client_disconnect_cb,
1850 if (NULL != wait_file)
1855 if (NULL != wait_filename)
1857 GNUNET_free (wait_filename);
1858 wait_filename = NULL;
1861 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1863 GNUNET_DISK_pipe_close (sigpipe);
1869 #if defined(LINUX) && defined(__GLIBC__)
1873 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1875 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1877 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1878 mallopt (M_TOP_PAD, 1 * 1024);
1884 /* end of gnunet-service-arm.c */