2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011, 2015 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 '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 GNUNET_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 GNUNET_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 GNUNET_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,
419 int is_simple_service;
420 struct ServiceListeningInfo *sli;
426 /* calculate listen socket list */
429 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
431 GNUNET_array_append (lsocks, ls,
432 GNUNET_NETWORK_get_fd (sli->listen_socket));
433 if (sli->accept_task != NULL)
435 GNUNET_SCHEDULER_cancel (sli->accept_task);
436 sli->accept_task = NULL;
440 GNUNET_array_append (lsocks, ls, INVALID_SOCKET);
442 GNUNET_array_append (lsocks, ls, -1);
445 /* obtain configuration */
447 GNUNET_CONFIGURATION_get_value_string (cfg,
451 loprefix = GNUNET_strdup (prefix_command);
453 GNUNET_CONFIGURATION_get_value_string (cfg,
464 fin_options = GNUNET_strdup (final_option);
465 /* replace '{}' with service name */
466 while (NULL != (optpos = strstr (fin_options, "{}")))
468 /* terminate string at opening parenthesis */
470 GNUNET_asprintf (&new_options,
475 GNUNET_free (fin_options);
476 fin_options = new_options;
480 /* combine "fin_options" with "options" */
482 GNUNET_asprintf (&options,
486 GNUNET_free (optpos);
490 /* only have "fin_options", use that */
491 options = fin_options;
494 options = GNUNET_CONFIGURATION_expand_dollar (cfg,
496 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg,
500 const char *service_type = NULL;
501 const char *choices[] = { "GNUNET", "SIMPLE", NULL };
503 is_simple_service = GNUNET_NO;
505 GNUNET_CONFIGURATION_get_value_choice (cfg,
510 (0 == strcasecmp (service_type, "SIMPLE")) )
511 is_simple_service = GNUNET_YES;
514 GNUNET_assert (NULL == sl->proc);
515 if (GNUNET_YES == is_simple_service)
517 /* A simple service will receive no GNUnet specific
518 command line options. */
519 binary = GNUNET_strdup (sl->binary);
520 binary = GNUNET_CONFIGURATION_expand_dollar (cfg, binary);
521 GNUNET_asprintf ("edbinary,
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
525 "Starting simple service `%s' using binary `%s'\n",
526 sl->name, sl->binary);
527 /* FIXME: dollar expansion should only be done outside
528 * of ''-quoted strings, escaping should be considered. */
530 options = GNUNET_CONFIGURATION_expand_dollar (cfg, options);
532 GNUNET_OS_start_process_s (sl->pipe_control,
533 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
542 /* actually start process */
543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
544 "Starting service `%s' using binary `%s' and configuration `%s'\n",
545 sl->name, sl->binary, sl->config);
546 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
547 GNUNET_asprintf ("edbinary,
551 if (GNUNET_YES == use_debug)
553 if (NULL == sl->config)
555 GNUNET_OS_start_process_s (sl->pipe_control,
556 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
565 GNUNET_OS_start_process_s (sl->pipe_control,
566 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
577 if (NULL == sl->config)
579 GNUNET_OS_start_process_s (sl->pipe_control,
580 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
588 GNUNET_OS_start_process_s (sl->pipe_control,
589 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
598 GNUNET_free (binary);
599 GNUNET_free (quotedbinary);
600 if (sl->proc == NULL)
602 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
603 _("Failed to start service `%s'\n"),
606 signal_result (client,
609 GNUNET_ARM_RESULT_START_FAILED);
613 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
614 _("Starting service `%s'\n"),
616 broadcast_status (sl->name, GNUNET_ARM_SERVICE_STARTING, NULL);
618 signal_result (client, sl->name, request_id, GNUNET_ARM_RESULT_STARTING);
621 GNUNET_free (loprefix);
622 GNUNET_free (options);
623 GNUNET_array_grow (lsocks, ls, 0);
628 * Find the process with the given service
629 * name in the given list and return it.
631 * @param name which service entry to look up
632 * @return NULL if it was not found
634 static struct ServiceList *
635 find_service (const char *name)
637 struct ServiceList *sl;
642 if (0 == strcasecmp (sl->name, name))
651 * First connection has come to the listening socket associated with the service,
652 * create the service in order to relay the incoming connection to it
654 * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
657 accept_connection (void *cls)
659 struct ServiceListeningInfo *sli = cls;
660 struct ServiceList *sl = sli->sl;
662 sli->accept_task = NULL;
663 GNUNET_assert (GNUNET_NO == in_shutdown);
664 start_process (sl, NULL, 0);
669 * Creating a listening socket for each of the service's addresses and
670 * wait for the first incoming connection to it
672 * @param sa address associated with the service
673 * @param addr_len length of @a sa
674 * @param sl service entry for the service in question
677 create_listen_socket (struct sockaddr *sa,
679 struct ServiceList *sl)
682 struct GNUNET_NETWORK_Handle *sock;
683 struct ServiceListeningInfo *sli;
689 switch (sa->sa_family)
692 sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
695 sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
698 if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */
700 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
705 errno = EAFNOSUPPORT;
710 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
711 _("Unable to create socket for service `%s': %s\n"),
712 sl->name, STRERROR (errno));
716 if (GNUNET_NETWORK_socket_setsockopt
717 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
718 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
721 if ((sa->sa_family == AF_INET6) &&
722 (GNUNET_NETWORK_socket_setsockopt
723 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
724 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
728 if (AF_UNIX == sa->sa_family)
729 GNUNET_NETWORK_unix_precheck ((struct sockaddr_un *) sa);
732 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) sa, addr_len))
734 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
735 _("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
737 GNUNET_a2s (sa, addr_len),
739 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
744 if ((AF_UNIX == sa->sa_family)
746 /* Permission settings are not required when abstract sockets are used */
747 && ('\0' != ((const struct sockaddr_un *)sa)->sun_path[0])
752 GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name,
755 GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name,
757 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sa)->sun_path,
763 if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
765 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
766 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
770 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
771 _("ARM now monitors connections to service `%s' at `%s'\n"),
772 sl->name, GNUNET_a2s (sa, addr_len));
773 sli = GNUNET_new (struct ServiceListeningInfo);
774 sli->service_addr = sa;
775 sli->service_addr_len = addr_len;
776 sli->listen_socket = sock;
779 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock,
780 &accept_connection, sli);
781 GNUNET_CONTAINER_DLL_insert (sl->listen_head,
788 * Remove and free an entry in the service list. Listen sockets
789 * must have already been cleaned up. Only to be called during shutdown.
791 * @param sl entry to free
794 free_service (struct ServiceList *sl)
796 GNUNET_assert (GNUNET_YES == in_shutdown);
797 GNUNET_CONTAINER_DLL_remove (running_head, running_tail, sl);
798 GNUNET_assert (NULL == sl->listen_head);
799 GNUNET_free_non_null (sl->config);
800 GNUNET_free_non_null (sl->binary);
801 GNUNET_free (sl->name);
807 * Handle START-message.
809 * @param cls closure (always NULL)
810 * @param client identification of the client
811 * @param message the actual message
812 * @return #GNUNET_OK to keep the connection open,
813 * #GNUNET_SYSERR to close it (signal serious error)
816 handle_start (void *cls,
817 struct GNUNET_SERVER_Client *client,
818 const struct GNUNET_MessageHeader *message)
820 const char *servicename;
821 struct ServiceList *sl;
824 struct GNUNET_ARM_Message *amsg;
826 amsg = (struct GNUNET_ARM_Message *) message;
827 request_id = GNUNET_ntohll (amsg->request_id);
828 size = ntohs (amsg->header.size);
829 size -= sizeof (struct GNUNET_ARM_Message);
830 servicename = (const char *) &amsg[1];
831 if ((size == 0) || (servicename[size - 1] != '\0'))
834 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
837 if (GNUNET_YES == in_shutdown)
839 signal_result (client, servicename, request_id,
840 GNUNET_ARM_RESULT_IN_SHUTDOWN);
841 GNUNET_SERVER_receive_done (client, GNUNET_OK);
844 sl = find_service (servicename);
847 signal_result (client, servicename, request_id,
848 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
849 GNUNET_SERVER_receive_done (client, GNUNET_OK);
852 sl->force_start = GNUNET_YES;
853 if (NULL != sl->proc)
855 signal_result (client, servicename, request_id,
856 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
857 GNUNET_SERVER_receive_done (client, GNUNET_OK);
860 start_process (sl, client, request_id);
861 GNUNET_SERVER_receive_done (client, GNUNET_OK);
866 * Start a shutdown sequence.
868 * @param cls closure (refers to service)
871 trigger_shutdown (void *cls)
873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
874 "Triggering shutdown\n");
875 GNUNET_SCHEDULER_shutdown ();
880 * Handle STOP-message.
882 * @param cls closure (always NULL)
883 * @param client identification of the client
884 * @param message the actual message
885 * @return #GNUNET_OK to keep the connection open,
886 * #GNUNET_SYSERR to close it (signal serious error)
889 handle_stop (void *cls,
890 struct GNUNET_SERVER_Client *client,
891 const struct GNUNET_MessageHeader *message)
893 struct ServiceList *sl;
894 const char *servicename;
897 struct GNUNET_ARM_Message *amsg;
899 amsg = (struct GNUNET_ARM_Message *) message;
900 request_id = GNUNET_ntohll (amsg->request_id);
901 size = ntohs (amsg->header.size);
902 size -= sizeof (struct GNUNET_ARM_Message);
903 servicename = (const char *) &amsg[1];
904 if ((size == 0) || (servicename[size - 1] != '\0'))
907 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
910 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
911 _("Preparing to stop `%s'\n"),
913 if (0 == strcasecmp (servicename, "arm"))
915 broadcast_status (servicename,
916 GNUNET_ARM_SERVICE_STOPPING, NULL);
917 signal_result (client,
920 GNUNET_ARM_RESULT_STOPPING);
921 GNUNET_SERVER_client_persist_ (client);
922 GNUNET_SCHEDULER_add_now (&trigger_shutdown, NULL);
923 GNUNET_SERVER_receive_done (client, GNUNET_OK);
926 sl = find_service (servicename);
929 signal_result (client,
932 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
933 GNUNET_SERVER_receive_done (client, GNUNET_OK);
936 sl->force_start = GNUNET_NO;
937 if (GNUNET_YES == in_shutdown)
939 /* shutdown in progress */
940 signal_result (client,
943 GNUNET_ARM_RESULT_IN_SHUTDOWN);
944 GNUNET_SERVER_receive_done (client, GNUNET_OK);
947 if (NULL != sl->killing_client)
949 /* killing already in progress */
950 signal_result (client,
953 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
954 GNUNET_SERVER_receive_done (client, GNUNET_OK);
957 if (NULL == sl->proc)
959 /* process is down */
960 signal_result (client,
963 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
964 GNUNET_SERVER_receive_done (client, GNUNET_OK);
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968 "Sending kill signal to service `%s', waiting for process to die.\n",
970 broadcast_status (servicename,
971 GNUNET_ARM_SERVICE_STOPPING,
973 /* no signal_start - only when it's STOPPED */
974 sl->killed_at = GNUNET_TIME_absolute_get ();
975 if (0 != GNUNET_OS_process_kill (sl->proc, GNUNET_TERM_SIG))
976 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
977 sl->killing_client = client;
978 sl->killing_client_request_id = request_id;
979 GNUNET_SERVER_client_keep (client);
980 GNUNET_SERVER_receive_done (client, GNUNET_OK);
985 * Handle LIST-message.
987 * @param cls closure (always NULL)
988 * @param client identification of the client
989 * @param message the actual message
992 handle_list (void *cls,
993 struct GNUNET_SERVER_Client *client,
994 const struct GNUNET_MessageHeader *message)
996 struct GNUNET_ARM_ListResultMessage *msg;
997 struct GNUNET_ARM_Message *request;
998 size_t string_list_size;
1000 struct ServiceList *sl;
1006 request = (struct GNUNET_ARM_Message *) message;
1007 GNUNET_break (0 == ntohl (request->reserved));
1009 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 total_size = sizeof (struct GNUNET_ARM_ListResultMessage)
1024 msg = GNUNET_malloc (total_size);
1025 msg->arm_msg.header.size = total_size;
1026 msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT;
1027 msg->arm_msg.reserved = htonl (0);
1028 msg->arm_msg.request_id = GNUNET_ntohll (request->request_id);
1031 char *pos = (char *)&msg[1];
1032 for (sl = running_head; NULL != sl; sl = sl->next)
1034 if (NULL != sl->proc)
1036 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
1037 GNUNET_snprintf (pos, s, "%s (%s)", sl->name, sl->binary);
1041 GNUNET_SERVER_notify_transmit_ready (client,
1043 GNUNET_TIME_UNIT_FOREVER_REL,
1044 &write_list_result, msg);
1045 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1050 * We are done with everything. Stop remaining
1051 * tasks, signal handler and the server.
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last shutdown phase\n");
1057 if (NULL != notifier)
1059 GNUNET_SERVER_notification_context_destroy (notifier);
1064 GNUNET_SERVER_destroy (server);
1067 if (NULL != child_death_task)
1069 GNUNET_SCHEDULER_cancel (child_death_task);
1070 child_death_task = NULL;
1076 * Count how many services are still active.
1078 * @param running_head list of services
1079 * @return number of active services found
1082 list_count (struct ServiceList *running_head)
1084 struct ServiceList *i;
1085 unsigned int res = 0;
1087 for (res = 0, i = running_head; i; i = i->next, res++)
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1096 * Task run for shutdown.
1098 * @param cls closure, NULL if we need to self-restart
1101 shutdown_task (void *cls)
1103 struct ServiceList *pos;
1104 struct ServiceList *nxt;
1105 struct ServiceListeningInfo *sli;
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1108 "First shutdown phase\n");
1109 if (NULL != child_restart_task)
1111 GNUNET_SCHEDULER_cancel (child_restart_task);
1112 child_restart_task = NULL;
1114 in_shutdown = GNUNET_YES;
1115 /* first, stop listening */
1116 for (pos = running_head; NULL != pos; pos = pos->next)
1118 while (NULL != (sli = pos->listen_head))
1120 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
1123 if (NULL != sli->accept_task)
1125 GNUNET_SCHEDULER_cancel (sli->accept_task);
1126 sli->accept_task = NULL;
1128 GNUNET_break (GNUNET_OK ==
1129 GNUNET_NETWORK_socket_close (sli->listen_socket));
1130 GNUNET_free (sli->service_addr);
1134 /* then, shutdown all existing service processes */
1136 while (NULL != (pos = nxt))
1139 if (NULL != pos->proc)
1141 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1142 "Stopping service `%s'\n",
1144 pos->killed_at = GNUNET_TIME_absolute_get ();
1145 if (0 != GNUNET_OS_process_kill (pos->proc, GNUNET_TERM_SIG))
1146 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1153 /* finally, should all service processes be already gone, terminate for real */
1154 if (NULL == running_head)
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158 "Delaying shutdown, have %u childs still running\n",
1159 list_count (running_head));
1164 * Task run whenever it is time to restart a child that died.
1166 * @param cls closure, always NULL
1169 delayed_restart_task (void *cls)
1172 struct ServiceList *sl;
1173 struct GNUNET_TIME_Relative lowestRestartDelay;
1174 struct ServiceListeningInfo *sli;
1176 child_restart_task = NULL;
1177 GNUNET_assert (GNUNET_NO == in_shutdown);
1178 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1180 /* check for services that need to be restarted due to
1181 * configuration changes or because the last restart failed */
1182 for (sl = running_head; NULL != sl; sl = sl->next)
1184 if (NULL != sl->proc)
1186 /* service is currently not running */
1187 if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
1189 /* restart is now allowed */
1190 if (sl->force_start)
1192 /* process should run by default, start immediately */
1193 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1194 _("Restarting service `%s'.\n"),
1196 start_process (sl, NULL, 0);
1200 /* process is run on-demand, ensure it is re-started if there is demand */
1201 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1202 if (NULL == sli->accept_task)
1204 /* accept was actually paused, so start it again */
1206 GNUNET_SCHEDULER_add_read_net
1207 (GNUNET_TIME_UNIT_FOREVER_REL,
1209 &accept_connection, sli);
1215 /* update calculation for earliest time to reactivate a service */
1216 lowestRestartDelay =
1217 GNUNET_TIME_relative_min (lowestRestartDelay,
1218 GNUNET_TIME_absolute_get_remaining
1222 if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "Will restart process in %s\n",
1226 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay, GNUNET_YES));
1227 child_restart_task =
1228 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1229 GNUNET_SCHEDULER_PRIORITY_IDLE,
1230 &delayed_restart_task, NULL);
1236 * Task triggered whenever we receive a SIGCHLD (child
1239 * @param cls closure, NULL if we need to self-restart
1242 maint_child_death (void *cls)
1244 struct ServiceList *pos;
1245 struct ServiceList *next;
1246 struct ServiceListeningInfo *sli;
1247 const char *statstr;
1251 enum GNUNET_OS_ProcessStatusType statusType;
1252 unsigned long statusCode;
1253 const struct GNUNET_DISK_FileHandle *pr;
1255 pr = GNUNET_DISK_pipe_handle (sigpipe,
1256 GNUNET_DISK_PIPE_END_READ);
1257 child_death_task = NULL;
1258 /* consume the signal */
1259 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
1261 /* check for services that died (WAITPID) */
1262 next = running_head;
1263 while (NULL != (pos = next))
1267 if (NULL == pos->proc)
1269 if (GNUNET_YES == in_shutdown)
1274 if (NULL != wait_file)
1276 /* need to use 'wait4()' to obtain and log performance data */
1281 pid = GNUNET_OS_process_get_pid (pos->proc);
1287 continue; /* no process done */
1288 if (WIFEXITED (status))
1290 statusType = GNUNET_OS_PROCESS_EXITED;
1291 statusCode = WEXITSTATUS (status);
1293 else if (WIFSIGNALED (status))
1295 statusType = GNUNET_OS_PROCESS_SIGNALED;
1296 statusCode = WTERMSIG (status);
1298 else if (WIFSTOPPED (status))
1300 statusType = GNUNET_OS_PROCESS_SIGNALED;
1301 statusCode = WSTOPSIG (status);
1304 else if (WIFCONTINUED (status))
1306 statusType = GNUNET_OS_PROCESS_RUNNING;
1312 statusType = GNUNET_OS_PROCESS_UNKNOWN;
1315 if ( (GNUNET_OS_PROCESS_EXITED == statusType) ||
1316 (GNUNET_OS_PROCESS_SIGNALED == statusType) )
1318 double utime = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 10e6);
1319 double stime = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 10e6);
1321 "%s(%u) %.3f %.3f %llu %llu %llu %llu %llu\n",
1326 (unsigned long long) ru.ru_maxrss,
1327 (unsigned long long) ru.ru_inblock,
1328 (unsigned long long) ru.ru_oublock,
1329 (unsigned long long) ru.ru_nvcsw,
1330 (unsigned long long) ru.ru_nivcsw);
1333 else /* continue with JUST this "if" as "else" (intentionally no brackets!) */
1335 if ( (GNUNET_SYSERR ==
1337 GNUNET_OS_process_status (pos->proc,
1340 (ret == GNUNET_NO) ||
1341 (statusType == GNUNET_OS_PROCESS_STOPPED) ||
1342 (statusType == GNUNET_OS_PROCESS_UNKNOWN) ||
1343 (statusType == GNUNET_OS_PROCESS_RUNNING) )
1346 if (statusType == GNUNET_OS_PROCESS_EXITED)
1348 statstr = _( /* process termination method */ "exit");
1349 statcode = statusCode;
1351 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1353 statstr = _( /* process termination method */ "signal");
1354 statcode = statusCode;
1358 statstr = _( /* process termination method */ "unknown");
1361 if (0 != pos->killed_at.abs_value_us)
1363 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1364 _("Service `%s' took %s to terminate\n"),
1366 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at),
1369 GNUNET_OS_process_destroy (pos->proc);
1371 broadcast_status (pos->name,
1372 GNUNET_ARM_SERVICE_STOPPED,
1374 if (NULL != pos->killing_client)
1376 signal_result (pos->killing_client, pos->name,
1377 pos->killing_client_request_id,
1378 GNUNET_ARM_RESULT_STOPPED);
1379 GNUNET_SERVER_client_drop (pos->killing_client);
1380 pos->killing_client = NULL;
1381 pos->killing_client_request_id = 0;
1383 if (GNUNET_YES != in_shutdown)
1385 if ( (statusType == GNUNET_OS_PROCESS_EXITED) &&
1388 /* process terminated normally, allow restart at any time */
1389 pos->restart_at.abs_value_us = 0;
1390 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1391 _("Service `%s' terminated normally, will restart at any time\n"),
1393 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1394 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1396 GNUNET_break (NULL == sli->accept_task);
1398 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1406 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1407 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1411 GNUNET_STRINGS_relative_time_to_string (pos->backoff,
1413 /* schedule restart */
1414 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1415 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1416 if (NULL != child_restart_task)
1417 GNUNET_SCHEDULER_cancel (child_restart_task);
1419 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1420 &delayed_restart_task,
1429 child_death_task = GNUNET_SCHEDULER_add_read_file (
1430 GNUNET_TIME_UNIT_FOREVER_REL,
1432 &maint_child_death, NULL);
1433 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1435 else if (GNUNET_YES == in_shutdown)
1436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1437 "Delaying shutdown after child's death, still have %u children\n",
1438 list_count (running_head));
1444 * Signal handler called for SIGCHLD. Triggers the
1445 * respective handler by writing to the trigger pipe.
1448 sighandler_child_death ()
1451 int old_errno = errno; /* back-up errno */
1454 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
1455 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
1457 errno = old_errno; /* restore errno */
1462 * Setup our service record for the given section in the configuration file
1463 * (assuming the section is for a service).
1466 * @param section a section in the configuration file
1467 * @return #GNUNET_OK (continue)
1470 setup_service (void *cls,
1471 const char *section)
1473 struct ServiceList *sl;
1477 struct sockaddr **addrs;
1478 socklen_t *addr_lens;
1482 if (strcasecmp (section, "arm") == 0)
1485 GNUNET_CONFIGURATION_get_value_string (cfg, section, "BINARY", &binary))
1487 /* not a service section */
1491 GNUNET_CONFIGURATION_have_value (cfg, section, "USER_SERVICE")) &&
1493 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "USER_SERVICE")))
1495 if (GNUNET_NO == start_user)
1497 GNUNET_free (binary);
1498 return; /* user service, and we don't deal with those */
1503 if (GNUNET_NO == start_system)
1505 GNUNET_free (binary);
1506 return; /* system service, and we don't deal with those */
1509 sl = find_service (section);
1512 /* got the same section twice!? */
1514 GNUNET_free (binary);
1519 GNUNET_CONFIGURATION_get_value_filename (cfg, section,
1523 GNUNET_CONFIGURATION_get_value_filename (cfg,
1527 (0 != STAT (config, &sbuf)))
1531 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1534 GNUNET_free (config);
1538 sl = GNUNET_new (struct ServiceList);
1539 sl->name = GNUNET_strdup (section);
1540 sl->binary = binary;
1541 sl->config = config;
1542 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1543 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1545 sl->pipe_control = GNUNET_YES;
1547 if (GNUNET_CONFIGURATION_have_value (cfg,
1550 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1554 GNUNET_CONTAINER_DLL_insert (running_head,
1558 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1562 sl->force_start = GNUNET_YES;
1564 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1572 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1577 if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section,
1582 /* this will free (or capture) addrs[i] */
1583 for (i = 0; i < ret; i++)
1584 create_listen_socket (addrs[i],
1587 GNUNET_free (addrs);
1588 GNUNET_free (addr_lens);
1593 * A client connected, add it to the notification context.
1595 * @param cls closure
1596 * @param client identification of the client
1599 handle_client_connecting (void *cls,
1600 struct GNUNET_SERVER_Client *client)
1602 /* All clients are considered to be of the "monitor" kind
1603 * (that is, they don't affect ARM shutdown).
1606 GNUNET_SERVER_client_mark_monitor (client);
1611 * Handle MONITOR-message.
1613 * @param cls closure (always NULL)
1614 * @param client identification of the client
1615 * @param message the actual message
1616 * @return #GNUNET_OK to keep the connection open,
1617 * #GNUNET_SYSERR to close it (signal serious error)
1620 handle_monitor (void *cls,
1621 struct GNUNET_SERVER_Client *client,
1622 const struct GNUNET_MessageHeader *message)
1624 /* FIXME: might want to start by letting monitor know about
1625 services that are already running */
1626 /* Removal is handled by the server implementation, internally. */
1627 if ((NULL != client) && (NULL != notifier))
1629 GNUNET_SERVER_notification_context_add (notifier, client);
1630 broadcast_status ("arm", GNUNET_ARM_SERVICE_MONITORING_STARTED, client);
1631 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1637 * Process arm requests.
1639 * @param cls closure
1640 * @param serv the initialized server
1641 * @param c configuration to use
1644 run (void *cls, struct GNUNET_SERVER_Handle *serv,
1645 const struct GNUNET_CONFIGURATION_Handle *c)
1647 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1648 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
1649 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
1650 {&handle_monitor, NULL, GNUNET_MESSAGE_TYPE_ARM_MONITOR,
1651 sizeof (struct GNUNET_MessageHeader)},
1652 {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST,
1653 sizeof (struct GNUNET_ARM_Message)},
1656 struct ServiceList *sl;
1660 GNUNET_assert (NULL != serv);
1661 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1664 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1665 GNUNET_DISK_pipe_handle (sigpipe,
1666 GNUNET_DISK_PIPE_END_READ),
1667 &maint_child_death, NULL);
1670 GNUNET_CONFIGURATION_get_value_filename (cfg,
1672 "RESOURCE_DIAGNOSTICS",
1675 wait_file = fopen (wait_filename,
1677 if (NULL == wait_file)
1679 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1686 GNUNET_CONFIGURATION_get_value_string (cfg,
1690 prefix_command = GNUNET_strdup ("");
1692 GNUNET_CONFIGURATION_get_value_string (cfg,
1696 final_option = GNUNET_strdup ("");
1698 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1702 GNUNET_break (GNUNET_YES == start_user);
1703 start_system = GNUNET_NO;
1706 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1710 GNUNET_break (GNUNET_YES == start_system);
1711 start_user = GNUNET_NO;
1713 GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
1715 /* start default services... */
1716 for (sl = running_head; NULL != sl; sl = sl->next)
1717 if (GNUNET_YES == sl->force_start)
1718 start_process (sl, NULL, 0);
1720 = GNUNET_SERVER_notification_context_create (server,
1722 GNUNET_SERVER_connect_notify (server,
1723 &handle_client_connecting, NULL);
1724 /* process client requests */
1725 GNUNET_SERVER_add_handlers (server,
1731 * The main function for the arm service.
1733 * @param argc number of arguments from the command line
1734 * @param argv command line arguments
1735 * @return 0 ok, 1 on error
1738 main (int argc, char *const *argv)
1741 struct GNUNET_SIGNAL_Context *shc_chld;
1743 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
1744 GNUNET_assert (sigpipe != NULL);
1746 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
1749 GNUNET_SERVICE_run (argc, argv, "arm",
1750 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, &run, NULL)) ? 0 : 1;
1752 if (NULL != wait_file)
1757 if (NULL != wait_filename)
1759 GNUNET_free (wait_filename);
1760 wait_filename = NULL;
1763 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1765 GNUNET_DISK_pipe_close (sigpipe);
1771 #if defined(LINUX) && defined(__GLIBC__)
1775 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1777 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1779 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1780 mallopt (M_TOP_PAD, 1 * 1024);
1786 /* end of gnunet-service-arm.c */