2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011, 2015 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., 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 '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_SERVER_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 server instance. Our server 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 server
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_SERVER_Handle *server;
257 * Context for notifications we need to send to our clients.
259 static struct GNUNET_SERVER_NotificationContext *notifier;
263 * Transmit a status result message.
265 * @param cls a `unit16_t *` with message type
266 * @param size number of bytes available in @a buf
267 * @param buf where to copy the message, NULL on error
268 * @return number of bytes copied to @a buf
271 write_result (void *cls, size_t size, void *buf)
273 struct GNUNET_ARM_ResultMessage *msg = cls;
278 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
279 _("Could not send status result to client\n"));
281 return 0; /* error, not much we can do */
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "Sending status response %u to client\n",
285 (unsigned int) msg->result);
286 msize = msg->arm_msg.header.size;
287 GNUNET_assert (size >= msize);
288 msg->arm_msg.header.size = htons (msg->arm_msg.header.size);
289 msg->arm_msg.header.type = htons (msg->arm_msg.header.type);
290 msg->result = htonl (msg->result);
291 msg->arm_msg.request_id = GNUNET_htonll (msg->arm_msg.request_id);
292 memcpy (buf, msg, msize);
299 * Transmit the list of running services.
301 * @param cls pointer to `struct GNUNET_ARM_ListResultMessage` with the message
302 * @param size number of bytes available in @a buf
303 * @param buf where to copy the message, NULL on error
304 * @return number of bytes copied to @a buf
307 write_list_result (void *cls, size_t size, void *buf)
309 struct GNUNET_ARM_ListResultMessage *msg = cls;
314 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
315 _("Could not send list result to client\n"));
317 return 0; /* error, not much we can do */
320 rslt_size = msg->arm_msg.header.size;
321 GNUNET_assert (size >= rslt_size);
322 msg->arm_msg.header.size = htons (msg->arm_msg.header.size);
323 msg->arm_msg.header.type = htons (msg->arm_msg.header.type);
324 msg->arm_msg.request_id = GNUNET_htonll (msg->arm_msg.request_id);
325 msg->count = htons (msg->count);
327 memcpy (buf, msg, rslt_size);
334 * Signal our client that we will start or stop the
337 * @param client who is being signalled
338 * @param name name of the service
339 * @param request_id id of the request that is being responded to.
340 * @param result message type to send
341 * @return NULL if it was not found
344 signal_result (struct GNUNET_SERVER_Client *client,
347 enum GNUNET_ARM_Result result)
349 struct GNUNET_ARM_ResultMessage *msg;
352 msize = sizeof (struct GNUNET_ARM_ResultMessage);
353 msg = GNUNET_malloc (msize);
354 msg->arm_msg.header.size = msize;
355 msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_RESULT;
356 msg->result = result;
357 msg->arm_msg.request_id = request_id;
359 GNUNET_SERVER_notify_transmit_ready (client, msize,
360 GNUNET_TIME_UNIT_FOREVER_REL,
366 * Tell all clients about status change of a service.
368 * @param name name of the service
369 * @param status message type to send
370 * @param unicast if not NULL, send to this client only.
371 * otherwise, send to all clients in the notifier
374 broadcast_status (const char *name,
375 enum GNUNET_ARM_ServiceStatus status,
376 struct GNUNET_SERVER_Client *unicast)
378 struct GNUNET_ARM_StatusMessage *msg;
381 if (NULL == notifier)
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Sending status %u of service `%s' to client\n",
385 (unsigned int) status, name);
386 namelen = strlen (name);
387 msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_StatusMessage) + namelen + 1);
388 msg->header.size = htons (sizeof (struct GNUNET_ARM_StatusMessage) + namelen + 1);
389 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_STATUS);
390 msg->status = htonl ((uint32_t) (status));
391 memcpy ((char *) &msg[1], name, namelen + 1);
394 GNUNET_SERVER_notification_context_broadcast (notifier,
395 (struct GNUNET_MessageHeader *) msg, GNUNET_YES);
397 GNUNET_SERVER_notification_context_unicast (notifier, unicast,
398 (const struct GNUNET_MessageHeader *) msg, GNUNET_NO);
404 * Actually start the process for the given service.
406 * @param sl identifies service to start
407 * @param client that asked to start the service (may be NULL)
408 * @param request_id id of the request in response to which the process is
409 * being started. 0 if starting was not requested.
412 start_process (struct ServiceList *sl,
413 struct GNUNET_SERVER_Client *client,
424 struct ServiceListeningInfo *sli;
430 /* calculate listen socket list */
433 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
435 GNUNET_array_append (lsocks, ls,
436 GNUNET_NETWORK_get_fd (sli->listen_socket));
437 if (sli->accept_task != NULL)
439 GNUNET_SCHEDULER_cancel (sli->accept_task);
440 sli->accept_task = NULL;
444 GNUNET_array_append (lsocks, ls, INVALID_SOCKET);
446 GNUNET_array_append (lsocks, ls, -1);
449 /* obtain configuration */
451 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "PREFIX",
453 loprefix = GNUNET_strdup (prefix_command);
455 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "OPTIONS",
458 options = GNUNET_strdup (final_option);
459 if (NULL == strstr (options, "%"))
461 /* replace '{}' with service name */
462 while (NULL != (optpos = strstr (options, "{}")))
466 GNUNET_asprintf (&optpos, options, sl->name);
467 GNUNET_free (options);
470 /* replace '$PATH' with value associated with "PATH" */
471 while (NULL != (optpos = strstr (options, "$")))
474 while (isupper ((unsigned char) *optend))
483 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
485 val = GNUNET_strdup ("");
487 GNUNET_asprintf (&optpos, "%s%s%c%s", options, val, b, next);
488 GNUNET_free (options);
494 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
496 /* actually start process */
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Starting service `%s' using binary `%s' and configuration `%s'\n",
499 sl->name, sl->binary, sl->config);
500 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
501 GNUNET_asprintf ("edbinary,
505 GNUNET_assert (NULL == sl->proc);
506 if (GNUNET_YES == use_debug)
508 if (NULL == sl->config)
510 GNUNET_OS_start_process_s (sl->pipe_control,
511 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
512 lsocks, loprefix, quotedbinary, "-L",
513 "DEBUG", options, NULL);
516 GNUNET_OS_start_process_s (sl->pipe_control,
517 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
518 lsocks, loprefix, quotedbinary, "-c",
520 "DEBUG", options, NULL);
524 if (NULL == sl->config)
526 GNUNET_OS_start_process_s (sl->pipe_control,
527 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
528 lsocks, loprefix, quotedbinary,
532 GNUNET_OS_start_process_s (sl->pipe_control,
533 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
534 lsocks, loprefix, quotedbinary, "-c",
535 sl->config, options, NULL);
537 GNUNET_free (binary);
538 GNUNET_free (quotedbinary);
539 if (sl->proc == NULL)
541 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
542 _("Failed to start service `%s'\n"),
545 signal_result (client,
548 GNUNET_ARM_RESULT_START_FAILED);
552 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
553 _("Starting service `%s'\n"),
555 broadcast_status (sl->name, GNUNET_ARM_SERVICE_STARTING, NULL);
557 signal_result (client, sl->name, request_id, GNUNET_ARM_RESULT_STARTING);
560 GNUNET_free (loprefix);
561 GNUNET_free (options);
562 GNUNET_array_grow (lsocks, ls, 0);
567 * Find the process with the given service
568 * name in the given list and return it.
570 * @param name which service entry to look up
571 * @return NULL if it was not found
573 static struct ServiceList *
574 find_service (const char *name)
576 struct ServiceList *sl;
581 if (0 == strcasecmp (sl->name, name))
590 * First connection has come to the listening socket associated with the service,
591 * create the service in order to relay the incoming connection to it
593 * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
597 accept_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
599 struct ServiceListeningInfo *sli = cls;
600 struct ServiceList *sl = sli->sl;
602 sli->accept_task = NULL;
603 GNUNET_assert (GNUNET_NO == in_shutdown);
604 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
606 start_process (sl, NULL, 0);
611 * Creating a listening socket for each of the service's addresses and
612 * wait for the first incoming connection to it
614 * @param sa address associated with the service
615 * @param addr_len length of @a sa
616 * @param sl service entry for the service in question
619 create_listen_socket (struct sockaddr *sa,
621 struct ServiceList *sl)
624 struct GNUNET_NETWORK_Handle *sock;
625 struct ServiceListeningInfo *sli;
631 switch (sa->sa_family)
634 sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
637 sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
640 if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */
642 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
647 errno = EAFNOSUPPORT;
652 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
653 _("Unable to create socket for service `%s': %s\n"),
654 sl->name, STRERROR (errno));
658 if (GNUNET_NETWORK_socket_setsockopt
659 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
660 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
663 if ((sa->sa_family == AF_INET6) &&
664 (GNUNET_NETWORK_socket_setsockopt
665 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
666 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
670 if (AF_UNIX == sa->sa_family)
671 GNUNET_NETWORK_unix_precheck ((struct sockaddr_un *) sa);
674 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) sa, addr_len))
676 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
677 _("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
679 GNUNET_a2s (sa, addr_len),
681 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
686 if ((AF_UNIX == sa->sa_family)
688 /* Permission settings are not required when abstract sockets are used */
689 && ('\0' != ((const struct sockaddr_un *)sa)->sun_path[0])
694 GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name,
697 GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name,
699 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sa)->sun_path,
705 if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
707 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
708 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
712 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
713 _("ARM now monitors connections to service `%s' at `%s'\n"),
714 sl->name, GNUNET_a2s (sa, addr_len));
715 sli = GNUNET_new (struct ServiceListeningInfo);
716 sli->service_addr = sa;
717 sli->service_addr_len = addr_len;
718 sli->listen_socket = sock;
721 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock,
722 &accept_connection, sli);
723 GNUNET_CONTAINER_DLL_insert (sl->listen_head, sl->listen_tail, sli);
728 * Remove and free an entry in the service list. Listen sockets
729 * must have already been cleaned up. Only to be called during shutdown.
731 * @param sl entry to free
734 free_service (struct ServiceList *sl)
736 GNUNET_assert (GNUNET_YES == in_shutdown);
737 GNUNET_CONTAINER_DLL_remove (running_head, running_tail, sl);
738 GNUNET_assert (NULL == sl->listen_head);
739 GNUNET_free_non_null (sl->config);
740 GNUNET_free_non_null (sl->binary);
741 GNUNET_free (sl->name);
747 * Handle START-message.
749 * @param cls closure (always NULL)
750 * @param client identification of the client
751 * @param message the actual message
752 * @return #GNUNET_OK to keep the connection open,
753 * #GNUNET_SYSERR to close it (signal serious error)
756 handle_start (void *cls,
757 struct GNUNET_SERVER_Client *client,
758 const struct GNUNET_MessageHeader *message)
760 const char *servicename;
761 struct ServiceList *sl;
764 struct GNUNET_ARM_Message *amsg;
766 amsg = (struct GNUNET_ARM_Message *) message;
767 request_id = GNUNET_ntohll (amsg->request_id);
768 size = ntohs (amsg->header.size);
769 size -= sizeof (struct GNUNET_ARM_Message);
770 servicename = (const char *) &amsg[1];
771 if ((size == 0) || (servicename[size - 1] != '\0'))
774 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
777 if (GNUNET_YES == in_shutdown)
779 signal_result (client, servicename, request_id,
780 GNUNET_ARM_RESULT_IN_SHUTDOWN);
781 GNUNET_SERVER_receive_done (client, GNUNET_OK);
784 sl = find_service (servicename);
787 signal_result (client, servicename, request_id,
788 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
789 GNUNET_SERVER_receive_done (client, GNUNET_OK);
792 sl->force_start = GNUNET_YES;
793 if (NULL != sl->proc)
795 signal_result (client, servicename, request_id,
796 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
797 GNUNET_SERVER_receive_done (client, GNUNET_OK);
800 start_process (sl, client, request_id);
801 GNUNET_SERVER_receive_done (client, GNUNET_OK);
806 * Start a shutdown sequence.
808 * @param cls closure (refers to service)
809 * @param tc task context
812 trigger_shutdown (void *cls,
813 const struct GNUNET_SCHEDULER_TaskContext *tc)
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 "Triggering shutdown\n");
817 GNUNET_SCHEDULER_shutdown ();
822 * Handle STOP-message.
824 * @param cls closure (always NULL)
825 * @param client identification of the client
826 * @param message the actual message
827 * @return #GNUNET_OK to keep the connection open,
828 * #GNUNET_SYSERR to close it (signal serious error)
831 handle_stop (void *cls,
832 struct GNUNET_SERVER_Client *client,
833 const struct GNUNET_MessageHeader *message)
835 struct ServiceList *sl;
836 const char *servicename;
839 struct GNUNET_ARM_Message *amsg;
841 amsg = (struct GNUNET_ARM_Message *) message;
842 request_id = GNUNET_ntohll (amsg->request_id);
843 size = ntohs (amsg->header.size);
844 size -= sizeof (struct GNUNET_ARM_Message);
845 servicename = (const char *) &amsg[1];
846 if ((size == 0) || (servicename[size - 1] != '\0'))
849 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
852 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
853 _("Preparing to stop `%s'\n"),
855 if (0 == strcasecmp (servicename, "arm"))
857 broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
858 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_STOPPING);
859 GNUNET_SERVER_client_persist_ (client);
860 GNUNET_SCHEDULER_add_now (trigger_shutdown, NULL);
861 GNUNET_SERVER_receive_done (client, GNUNET_OK);
864 sl = find_service (servicename);
867 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_NOT_KNOWN);
868 GNUNET_SERVER_receive_done (client, GNUNET_OK);
871 sl->force_start = GNUNET_NO;
872 if (GNUNET_YES == in_shutdown)
874 /* shutdown in progress */
875 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IN_SHUTDOWN);
876 GNUNET_SERVER_receive_done (client, GNUNET_OK);
879 if (NULL != sl->killing_client)
881 /* killing already in progress */
882 signal_result (client, servicename, request_id,
883 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
884 GNUNET_SERVER_receive_done (client, GNUNET_OK);
887 if (NULL == sl->proc)
889 /* process is down */
890 signal_result (client, servicename, request_id,
891 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
892 GNUNET_SERVER_receive_done (client, GNUNET_OK);
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "Sending kill signal to service `%s', waiting for process to die.\n",
898 broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
899 /* no signal_start - only when it's STOPPED */
900 sl->killed_at = GNUNET_TIME_absolute_get ();
901 if (0 != GNUNET_OS_process_kill (sl->proc, GNUNET_TERM_SIG))
902 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
903 sl->killing_client = client;
904 sl->killing_client_request_id = request_id;
905 GNUNET_SERVER_client_keep (client);
906 GNUNET_SERVER_receive_done (client, GNUNET_OK);
911 * Handle LIST-message.
913 * @param cls closure (always NULL)
914 * @param client identification of the client
915 * @param message the actual message
918 handle_list (void *cls, struct GNUNET_SERVER_Client *client,
919 const struct GNUNET_MessageHeader *message)
921 struct GNUNET_ARM_ListResultMessage *msg;
922 struct GNUNET_ARM_Message *request;
923 size_t string_list_size;
925 struct ServiceList *sl;
931 request = (struct GNUNET_ARM_Message *) message;
932 GNUNET_break (0 == ntohl (request->reserved));
934 string_list_size = 0;
935 /* first count the running processes get their name's size */
936 for (sl = running_head; NULL != sl; sl = sl->next)
938 if (NULL != sl->proc)
940 string_list_size += strlen (sl->name);
941 string_list_size += strlen (sl->binary);
942 string_list_size += 4;
947 total_size = sizeof (struct GNUNET_ARM_ListResultMessage)
949 msg = GNUNET_malloc (total_size);
950 msg->arm_msg.header.size = total_size;
951 msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT;
952 msg->arm_msg.reserved = htonl (0);
953 msg->arm_msg.request_id = GNUNET_ntohll (request->request_id);
956 char *pos = (char *)&msg[1];
957 for (sl = running_head; NULL != sl; sl = sl->next)
959 if (NULL != sl->proc)
961 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
962 GNUNET_snprintf (pos, s, "%s (%s)", sl->name, sl->binary);
966 GNUNET_SERVER_notify_transmit_ready (client,
968 GNUNET_TIME_UNIT_FOREVER_REL,
969 &write_list_result, msg);
970 GNUNET_SERVER_receive_done (client, GNUNET_OK);
975 * We are done with everything. Stop remaining
976 * tasks, signal handler and the server.
981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last shutdown phase\n");
982 if (NULL != notifier)
984 GNUNET_SERVER_notification_context_destroy (notifier);
989 GNUNET_SERVER_destroy (server);
992 if (NULL != child_death_task)
994 GNUNET_SCHEDULER_cancel (child_death_task);
995 child_death_task = NULL;
1001 * Count how many services are still active.
1003 * @param running_head list of services
1004 * @return number of active services found
1007 list_count (struct ServiceList *running_head)
1009 struct ServiceList *i;
1010 unsigned int res = 0;
1012 for (res = 0, i = running_head; i; i = i->next, res++)
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021 * Task run for shutdown.
1023 * @param cls closure, NULL if we need to self-restart
1027 shutdown_task (void *cls,
1028 const struct GNUNET_SCHEDULER_TaskContext *tc)
1030 struct ServiceList *pos;
1031 struct ServiceList *nxt;
1032 struct ServiceListeningInfo *sli;
1034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1035 "First shutdown phase\n");
1036 if (NULL != child_restart_task)
1038 GNUNET_SCHEDULER_cancel (child_restart_task);
1039 child_restart_task = NULL;
1041 in_shutdown = GNUNET_YES;
1042 /* first, stop listening */
1043 for (pos = running_head; NULL != pos; pos = pos->next)
1045 while (NULL != (sli = pos->listen_head))
1047 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
1048 pos->listen_tail, sli);
1049 if (NULL != sli->accept_task)
1051 GNUNET_SCHEDULER_cancel (sli->accept_task);
1052 sli->accept_task = NULL;
1054 GNUNET_break (GNUNET_OK ==
1055 GNUNET_NETWORK_socket_close (sli->listen_socket));
1056 GNUNET_free (sli->service_addr);
1060 /* then, shutdown all existing service processes */
1062 while (NULL != (pos = nxt))
1065 if (pos->proc != NULL)
1067 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1068 "Stopping service `%s'\n",
1070 pos->killed_at = GNUNET_TIME_absolute_get ();
1071 if (0 != GNUNET_OS_process_kill (pos->proc, GNUNET_TERM_SIG))
1072 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1079 /* finally, should all service processes be already gone, terminate for real */
1080 if (running_head == NULL)
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "Delaying shutdown, have %u childs still running\n",
1085 list_count (running_head));
1090 * Task run whenever it is time to restart a child that died.
1092 * @param cls closure, always NULL
1096 delayed_restart_task (void *cls,
1097 const struct GNUNET_SCHEDULER_TaskContext *tc)
1099 struct ServiceList *sl;
1100 struct GNUNET_TIME_Relative lowestRestartDelay;
1101 struct ServiceListeningInfo *sli;
1103 child_restart_task = NULL;
1104 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1106 GNUNET_assert (GNUNET_NO == in_shutdown);
1107 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1109 /* check for services that need to be restarted due to
1110 * configuration changes or because the last restart failed */
1111 for (sl = running_head; NULL != sl; sl = sl->next)
1113 if (NULL != sl->proc)
1115 /* service is currently not running */
1116 if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
1118 /* restart is now allowed */
1119 if (sl->force_start)
1121 /* process should run by default, start immediately */
1122 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1123 _("Restarting service `%s'.\n"),
1125 start_process (sl, NULL, 0);
1129 /* process is run on-demand, ensure it is re-started if there is demand */
1130 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1131 if (NULL == sli->accept_task)
1133 /* accept was actually paused, so start it again */
1135 GNUNET_SCHEDULER_add_read_net
1136 (GNUNET_TIME_UNIT_FOREVER_REL, sli->listen_socket,
1137 &accept_connection, sli);
1143 /* update calculation for earliest time to reactivate a service */
1144 lowestRestartDelay =
1145 GNUNET_TIME_relative_min (lowestRestartDelay,
1146 GNUNET_TIME_absolute_get_remaining
1150 if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "Will restart process in %s\n",
1154 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay, GNUNET_YES));
1155 child_restart_task =
1156 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1157 GNUNET_SCHEDULER_PRIORITY_IDLE,
1158 &delayed_restart_task, NULL);
1164 * Task triggered whenever we receive a SIGCHLD (child
1167 * @param cls closure, NULL if we need to self-restart
1171 maint_child_death (void *cls,
1172 const struct GNUNET_SCHEDULER_TaskContext *tc)
1174 struct ServiceList *pos;
1175 struct ServiceList *next;
1176 struct ServiceListeningInfo *sli;
1177 const char *statstr;
1181 enum GNUNET_OS_ProcessStatusType statusType;
1182 unsigned long statusCode;
1183 const struct GNUNET_DISK_FileHandle *pr;
1185 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
1186 child_death_task = NULL;
1187 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1189 /* shutdown scheduled us, ignore! */
1191 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1197 /* consume the signal */
1198 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
1200 /* check for services that died (WAITPID) */
1201 next = running_head;
1202 while (NULL != (pos = next))
1206 if (NULL == pos->proc)
1208 if (GNUNET_YES == in_shutdown)
1213 if (NULL != wait_file)
1215 /* need to use 'wait4()' to obtain and log performance data */
1220 pid = GNUNET_OS_process_get_pid (pos->proc);
1226 continue; /* no process done */
1227 if (WIFEXITED (status))
1229 statusType = GNUNET_OS_PROCESS_EXITED;
1230 statusCode = WEXITSTATUS (status);
1232 else if (WIFSIGNALED (status))
1234 statusType = GNUNET_OS_PROCESS_SIGNALED;
1235 statusCode = WTERMSIG (status);
1237 else if (WIFSTOPPED (status))
1239 statusType = GNUNET_OS_PROCESS_SIGNALED;
1240 statusCode = WSTOPSIG (status);
1243 else if (WIFCONTINUED (status))
1245 statusType = GNUNET_OS_PROCESS_RUNNING;
1251 statusType = GNUNET_OS_PROCESS_UNKNOWN;
1254 if ( (GNUNET_OS_PROCESS_EXITED == statusType) ||
1255 (GNUNET_OS_PROCESS_SIGNALED == statusType) )
1257 double utime = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 10e6);
1258 double stime = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 10e6);
1260 "%s(%u) %.3f %.3f %llu %llu %llu %llu %llu\n",
1265 (unsigned long long) ru.ru_maxrss,
1266 (unsigned long long) ru.ru_inblock,
1267 (unsigned long long) ru.ru_oublock,
1268 (unsigned long long) ru.ru_nvcsw,
1269 (unsigned long long) ru.ru_nivcsw);
1272 else /* continue with JUST this "if" as "else" (intentionally no brackets!) */
1274 if ( (GNUNET_SYSERR ==
1276 GNUNET_OS_process_status (pos->proc,
1279 (ret == GNUNET_NO) ||
1280 (statusType == GNUNET_OS_PROCESS_STOPPED) ||
1281 (statusType == GNUNET_OS_PROCESS_UNKNOWN) ||
1282 (statusType == GNUNET_OS_PROCESS_RUNNING) )
1285 if (statusType == GNUNET_OS_PROCESS_EXITED)
1287 statstr = _( /* process termination method */ "exit");
1288 statcode = statusCode;
1290 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1292 statstr = _( /* process termination method */ "signal");
1293 statcode = statusCode;
1297 statstr = _( /* process termination method */ "unknown");
1300 if (0 != pos->killed_at.abs_value_us)
1302 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1303 _("Service `%s' took %s to terminate\n"),
1305 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at),
1308 GNUNET_OS_process_destroy (pos->proc);
1310 broadcast_status (pos->name,
1311 GNUNET_ARM_SERVICE_STOPPED,
1313 if (NULL != pos->killing_client)
1315 signal_result (pos->killing_client, pos->name,
1316 pos->killing_client_request_id,
1317 GNUNET_ARM_RESULT_STOPPED);
1318 GNUNET_SERVER_client_drop (pos->killing_client);
1319 pos->killing_client = NULL;
1320 pos->killing_client_request_id = 0;
1322 if (GNUNET_YES != in_shutdown)
1324 if ( (statusType == GNUNET_OS_PROCESS_EXITED) &&
1327 /* process terminated normally, allow restart at any time */
1328 pos->restart_at.abs_value_us = 0;
1329 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1330 _("Service `%s' terminated normally, will restart at any time\n"),
1332 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1333 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1335 GNUNET_break (NULL == sli->accept_task);
1337 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1345 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1346 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1347 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1351 GNUNET_STRINGS_relative_time_to_string (pos->backoff,
1353 /* schedule restart */
1354 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1355 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1356 if (NULL != child_restart_task)
1357 GNUNET_SCHEDULER_cancel (child_restart_task);
1359 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1360 &delayed_restart_task,
1369 child_death_task = GNUNET_SCHEDULER_add_read_file (
1370 GNUNET_TIME_UNIT_FOREVER_REL,
1372 &maint_child_death, NULL);
1373 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1375 else if (GNUNET_YES == in_shutdown)
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1377 "Delaying shutdown after child's death, still have %u children\n",
1378 list_count (running_head));
1384 * Signal handler called for SIGCHLD. Triggers the
1385 * respective handler by writing to the trigger pipe.
1388 sighandler_child_death ()
1391 int old_errno = errno; /* back-up errno */
1394 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
1395 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
1397 errno = old_errno; /* restore errno */
1402 * Setup our service record for the given section in the configuration file
1403 * (assuming the section is for a service).
1406 * @param section a section in the configuration file
1407 * @return #GNUNET_OK (continue)
1410 setup_service (void *cls,
1411 const char *section)
1413 struct ServiceList *sl;
1417 struct sockaddr **addrs;
1418 socklen_t *addr_lens;
1422 if (strcasecmp (section, "arm") == 0)
1425 GNUNET_CONFIGURATION_get_value_string (cfg, section, "BINARY", &binary))
1427 /* not a service section */
1431 GNUNET_CONFIGURATION_have_value (cfg, section, "USER_SERVICE")) &&
1433 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "USER_SERVICE")))
1435 if (GNUNET_NO == start_user)
1437 GNUNET_free (binary);
1438 return; /* user service, and we don't deal with those */
1443 if (GNUNET_NO == start_system)
1445 GNUNET_free (binary);
1446 return; /* system service, and we don't deal with those */
1449 sl = find_service (section);
1452 /* got the same section twice!? */
1454 GNUNET_free (binary);
1459 GNUNET_CONFIGURATION_get_value_filename (cfg, section,
1463 GNUNET_CONFIGURATION_get_value_filename (cfg,
1467 (0 != STAT (config, &sbuf)))
1471 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1474 GNUNET_free (config);
1478 sl = GNUNET_new (struct ServiceList);
1479 sl->name = GNUNET_strdup (section);
1480 sl->binary = binary;
1481 sl->config = config;
1482 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1483 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1485 sl->pipe_control = GNUNET_YES;
1487 if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
1488 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
1490 GNUNET_CONTAINER_DLL_insert (running_head,
1494 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1498 sl->force_start = GNUNET_YES;
1500 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1508 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1513 if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section,
1518 /* this will free (or capture) addrs[i] */
1519 for (i = 0; i < ret; i++)
1520 create_listen_socket (addrs[i],
1523 GNUNET_free (addrs);
1524 GNUNET_free (addr_lens);
1529 * A client connected, add it to the notification context.
1531 * @param cls closure
1532 * @param client identification of the client
1535 handle_client_connecting (void *cls, struct GNUNET_SERVER_Client *client)
1537 /* All clients are considered to be of the "monitor" kind
1538 * (that is, they don't affect ARM shutdown).
1541 GNUNET_SERVER_client_mark_monitor (client);
1546 * Handle MONITOR-message.
1548 * @param cls closure (always NULL)
1549 * @param client identification of the client
1550 * @param message the actual message
1551 * @return #GNUNET_OK to keep the connection open,
1552 * #GNUNET_SYSERR to close it (signal serious error)
1555 handle_monitor (void *cls, struct GNUNET_SERVER_Client *client,
1556 const struct GNUNET_MessageHeader *message)
1558 /* Removal is handled by the server implementation, internally. */
1559 if ((NULL != client) && (NULL != notifier))
1561 GNUNET_SERVER_notification_context_add (notifier, client);
1562 broadcast_status ("arm", GNUNET_ARM_SERVICE_MONITORING_STARTED, client);
1563 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1569 * Process arm requests.
1571 * @param cls closure
1572 * @param serv the initialized server
1573 * @param c configuration to use
1576 run (void *cls, struct GNUNET_SERVER_Handle *serv,
1577 const struct GNUNET_CONFIGURATION_Handle *c)
1579 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1580 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
1581 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
1582 {&handle_monitor, NULL, GNUNET_MESSAGE_TYPE_ARM_MONITOR,
1583 sizeof (struct GNUNET_MessageHeader)},
1584 {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST,
1585 sizeof (struct GNUNET_ARM_Message)},
1588 struct ServiceList *sl;
1592 GNUNET_assert (NULL != serv);
1593 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1597 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1598 GNUNET_DISK_pipe_handle (sigpipe,
1599 GNUNET_DISK_PIPE_END_READ),
1600 &maint_child_death, NULL);
1603 GNUNET_CONFIGURATION_get_value_filename (cfg,
1605 "RESOURCE_DIAGNOSTICS",
1608 wait_file = fopen (wait_filename,
1610 if (NULL == wait_file)
1612 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1619 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_PREFIX",
1621 prefix_command = GNUNET_strdup ("");
1623 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
1625 final_option = GNUNET_strdup ("");
1627 GNUNET_CONFIGURATION_get_value_yesno (cfg, "ARM", "USER_ONLY"))
1629 GNUNET_break (GNUNET_YES == start_user);
1630 start_system = GNUNET_NO;
1633 GNUNET_CONFIGURATION_get_value_yesno (cfg, "ARM", "SYSTEM_ONLY"))
1635 GNUNET_break (GNUNET_YES == start_system);
1636 start_user = GNUNET_NO;
1638 GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
1640 /* start default services... */
1641 for (sl = running_head; NULL != sl; sl = sl->next)
1642 if (GNUNET_YES == sl->force_start)
1643 start_process (sl, NULL, 0);
1645 = GNUNET_SERVER_notification_context_create (server,
1647 GNUNET_SERVER_connect_notify (server,
1648 &handle_client_connecting, NULL);
1649 /* process client requests */
1650 GNUNET_SERVER_add_handlers (server,
1656 * The main function for the arm service.
1658 * @param argc number of arguments from the command line
1659 * @param argv command line arguments
1660 * @return 0 ok, 1 on error
1663 main (int argc, char *const *argv)
1666 struct GNUNET_SIGNAL_Context *shc_chld;
1668 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
1669 GNUNET_assert (sigpipe != NULL);
1671 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
1674 GNUNET_SERVICE_run (argc, argv, "arm",
1675 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, &run, NULL)) ? 0 : 1;
1677 if (NULL != wait_file)
1682 if (NULL != wait_filename)
1684 GNUNET_free (wait_filename);
1685 wait_filename = NULL;
1688 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1690 GNUNET_DISK_pipe_close (sigpipe);
1696 #if defined(LINUX) && defined(__GLIBC__)
1700 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1702 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1704 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1705 mallopt (M_TOP_PAD, 1 * 1024);
1711 /* end of gnunet-service-arm.c */