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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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"
32 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
39 * Name of the file for writing resource utilization summaries to.
41 static char *wait_filename;
44 * Handle for the file for writing resource summaries.
46 static FILE *wait_file;
51 * How many messages do we queue up at most for optional
52 * notifications to a client? (this can cause notifications
53 * about outgoing messages to be dropped).
55 #define MAX_NOTIFY_QUEUE 1024
59 * List of our services.
65 * Record with information about a listen socket we have open.
67 struct ServiceListeningInfo
70 * This is a linked list.
72 struct ServiceListeningInfo *next;
75 * This is a linked list.
77 struct ServiceListeningInfo *prev;
80 * Address this socket is listening on.
82 struct sockaddr *service_addr;
85 * Service this listen socket is for.
87 struct ServiceList *sl;
90 * Number of bytes in @e service_addr
92 socklen_t service_addr_len;
95 * Our listening socket.
97 struct GNUNET_NETWORK_Handle *listen_socket;
100 * Task doing the accepting.
102 struct GNUNET_SCHEDULER_Task *accept_task;
108 * List of our services.
113 * This is a doubly-linked list.
115 struct ServiceList *next;
118 * This is a doubly-linked list.
120 struct ServiceList *prev;
123 * Linked list of listen sockets associated with this service.
125 struct ServiceListeningInfo *listen_head;
128 * Linked list of listen sockets associated with this service.
130 struct ServiceListeningInfo *listen_tail;
133 * Name of the service.
138 * Name of the binary used.
143 * Name of the configuration file used.
148 * Client to notify upon kill completion (waitpid), NULL
149 * if we should simply restart the process.
151 struct GNUNET_SERVICE_Client *killing_client;
154 * ID of the request that killed the service (for reporting back).
156 uint64_t killing_client_request_id;
159 * Process structure pointer of the child.
161 struct GNUNET_OS_Process *proc;
164 * Process exponential backoff time
166 struct GNUNET_TIME_Relative backoff;
169 * Absolute time at which the process is scheduled to restart in case of death
171 struct GNUNET_TIME_Absolute restart_at;
174 * Time we asked the service to shut down (used to calculate time it took
175 * the service to terminate).
177 struct GNUNET_TIME_Absolute killed_at;
180 * Is this service to be started by default (or did a client tell us explicitly
181 * to start it)? #GNUNET_NO if the service is started only upon 'accept' on a
182 * listen socket or possibly explicitly by a client changing the value.
187 * Should we use pipes to signal this process? (YES for Java binaries and if we
194 * List of running services.
196 static struct ServiceList *running_head;
199 * List of running services.
201 static struct ServiceList *running_tail;
206 static const struct GNUNET_CONFIGURATION_Handle *cfg;
209 * Command to prepend to each actual command.
211 static char *prefix_command;
214 * Option to append to each actual command.
216 static char *final_option;
219 * ID of task called whenever we get a SIGCHILD.
221 static struct GNUNET_SCHEDULER_Task *child_death_task;
224 * ID of task called whenever the timeout for restarting a child
227 static struct GNUNET_SCHEDULER_Task *child_restart_task;
230 * Pipe used to communicate shutdown via signal.
232 static struct GNUNET_DISK_PipeHandle *sigpipe;
235 * Are we in shutdown mode?
237 static int in_shutdown;
240 * Return value from main
242 static int global_ret;
245 * Are we starting user services?
247 static int start_user = GNUNET_YES;
250 * Are we starting system services?
252 static int start_system = GNUNET_YES;
255 * Handle to our service instance. Our service is a bit special in that
256 * its service is not immediately stopped once we get a shutdown
257 * request (since we need to continue service until all of our child
258 * processes are dead). This handle is used to shut down the service
259 * (and thus trigger process termination) once all child processes are
260 * also dead. A special option in the ARM configuration modifies the
261 * behaviour of the service implementation to not do the shutdown
264 static struct GNUNET_SERVICE_Handle *service;
267 * Context for notifications we need to send to our clients.
269 static struct GNUNET_NotificationContext *notifier;
273 * Add the given UNIX domain path as an address to the
274 * list (as the first entry).
276 * @param saddrs array to update
277 * @param saddrlens where to store the address length
278 * @param unixpath path to add
279 * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
280 * parameter is ignore on systems other than LINUX
283 add_unixpath (struct sockaddr **saddrs,
284 socklen_t *saddrlens,
285 const char *unixpath,
289 struct sockaddr_un *un;
291 un = GNUNET_new (struct sockaddr_un);
292 un->sun_family = AF_UNIX;
293 strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
295 if (GNUNET_YES == abstract)
296 un->sun_path[0] = '\0';
298 #if HAVE_SOCKADDR_UN_SUN_LEN
299 un->sun_len = (u_char) sizeof (struct sockaddr_un);
301 *saddrs = (struct sockaddr *) un;
302 *saddrlens = sizeof (struct sockaddr_un);
304 /* this function should never be called
305 * unless AF_UNIX is defined! */
312 * Get the list of addresses that a server for the given service
315 * @param service_name name of the service
316 * @param cfg configuration (which specifies the addresses)
317 * @param addrs set (call by reference) to an array of pointers to the
318 * addresses the server should bind to and listen on; the
319 * array will be NULL-terminated (on success)
320 * @param addr_lens set (call by reference) to an array of the lengths
321 * of the respective `struct sockaddr` struct in the @a addrs
323 * @return number of addresses found on success,
324 * #GNUNET_SYSERR if the configuration
325 * did not specify reasonable finding information or
326 * if it specified a hostname that could not be resolved;
327 * #GNUNET_NO if the number of addresses configured is
328 * zero (in this case, `*addrs` and `*addr_lens` will be
332 get_server_addresses (const char *service_name,
333 const struct GNUNET_CONFIGURATION_Handle *cfg,
334 struct sockaddr ***addrs,
335 socklen_t ** addr_lens)
338 struct GNUNET_NETWORK_Handle *desc;
339 unsigned long long port;
341 struct addrinfo hints;
342 struct addrinfo *res;
343 struct addrinfo *pos;
344 struct addrinfo *next;
349 struct sockaddr **saddrs;
350 socklen_t *saddrlens;
356 if (GNUNET_CONFIGURATION_have_value (cfg,
362 GNUNET_CONFIGURATION_get_value_yesno (cfg,
365 return GNUNET_SYSERR;
368 disablev6 = GNUNET_NO;
372 /* probe IPv6 support */
373 desc = GNUNET_NETWORK_socket_create (PF_INET6,
378 if ( (ENOBUFS == errno) ||
383 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
385 return GNUNET_SYSERR;
387 LOG (GNUNET_ERROR_TYPE_INFO,
388 _("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
391 disablev6 = GNUNET_YES;
395 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
401 if (GNUNET_CONFIGURATION_have_value (cfg,
406 GNUNET_CONFIGURATION_get_value_number (cfg,
411 LOG (GNUNET_ERROR_TYPE_ERROR,
412 _("Require valid port number for service `%s' in configuration!\n"),
417 LOG (GNUNET_ERROR_TYPE_ERROR,
418 _("Require valid port number for service `%s' in configuration!\n"),
420 return GNUNET_SYSERR;
424 if (GNUNET_CONFIGURATION_have_value (cfg,
428 GNUNET_break (GNUNET_OK ==
429 GNUNET_CONFIGURATION_get_value_string (cfg,
438 abstract = GNUNET_NO;
441 GNUNET_CONFIGURATION_have_value (cfg,
445 GNUNET_CONFIGURATION_get_value_filename (cfg,
449 (0 < strlen (unixpath)))
451 /* probe UNIX support */
452 struct sockaddr_un s_un;
454 if (strlen (unixpath) >= sizeof (s_un.sun_path))
456 LOG (GNUNET_ERROR_TYPE_WARNING,
457 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
459 (unsigned long long) sizeof (s_un.sun_path));
460 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
461 LOG (GNUNET_ERROR_TYPE_INFO,
462 _("Using `%s' instead\n"),
466 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
468 "USE_ABSTRACT_SOCKETS");
469 if (GNUNET_SYSERR == abstract)
470 abstract = GNUNET_NO;
472 if ( (GNUNET_YES != abstract) &&
474 GNUNET_DISK_directory_create_for_file (unixpath)) )
475 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
479 if (NULL != unixpath)
481 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
484 if ( (ENOBUFS == errno) ||
489 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
490 GNUNET_free_non_null (hostname);
491 GNUNET_free (unixpath);
492 return GNUNET_SYSERR;
494 LOG (GNUNET_ERROR_TYPE_INFO,
495 _("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
498 GNUNET_free (unixpath);
503 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
513 GNUNET_CONFIGURATION_get_value_yesno (cfg,
516 LOG (GNUNET_ERROR_TYPE_ERROR,
517 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
519 GNUNET_free_non_null (hostname);
520 return GNUNET_SYSERR;
524 saddrs = GNUNET_new_array (2,
526 saddrlens = GNUNET_new_array (2,
528 add_unixpath (saddrs,
532 GNUNET_free_non_null (unixpath);
533 GNUNET_free_non_null (hostname);
535 *addr_lens = saddrlens;
539 if (NULL != hostname)
541 LOG (GNUNET_ERROR_TYPE_DEBUG,
542 "Resolving `%s' since that is where `%s' will bind to.\n",
545 memset (&hints, 0, sizeof (struct addrinfo));
547 hints.ai_family = AF_INET;
548 hints.ai_protocol = IPPROTO_TCP;
549 if ((0 != (ret = getaddrinfo (hostname,
555 LOG (GNUNET_ERROR_TYPE_ERROR,
556 _("Failed to resolve `%s': %s\n"),
559 GNUNET_free (hostname);
560 GNUNET_free_non_null (unixpath);
561 return GNUNET_SYSERR;
565 while (NULL != (pos = next))
568 if ((disablev6) && (pos->ai_family == AF_INET6))
574 LOG (GNUNET_ERROR_TYPE_ERROR,
575 _("Failed to find %saddress for `%s'.\n"),
576 disablev6 ? "IPv4 " : "",
579 GNUNET_free (hostname);
580 GNUNET_free_non_null (unixpath);
581 return GNUNET_SYSERR;
584 if (NULL != unixpath)
586 saddrs = GNUNET_new_array (resi + 1,
588 saddrlens = GNUNET_new_array (resi + 1,
591 if (NULL != unixpath)
593 add_unixpath (saddrs, saddrlens, unixpath, abstract);
597 while (NULL != (pos = next))
600 if ((disablev6) && (AF_INET6 == pos->ai_family))
602 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
603 continue; /* not TCP */
604 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
606 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
607 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
608 if (AF_INET == pos->ai_family)
610 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
611 saddrlens[i] = pos->ai_addrlen;
612 saddrs[i] = GNUNET_malloc (saddrlens[i]);
613 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
614 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
618 GNUNET_assert (AF_INET6 == pos->ai_family);
619 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
620 saddrlens[i] = pos->ai_addrlen;
621 saddrs[i] = GNUNET_malloc (saddrlens[i]);
622 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
623 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
627 GNUNET_free (hostname);
633 /* will bind against everything, just set port */
638 if (NULL != unixpath)
641 saddrs = GNUNET_new_array (resi + 1,
643 saddrlens = GNUNET_new_array (resi + 1,
645 if (NULL != unixpath)
647 add_unixpath (saddrs, saddrlens, unixpath, abstract);
650 saddrlens[i] = sizeof (struct sockaddr_in);
651 saddrs[i] = GNUNET_malloc (saddrlens[i]);
652 #if HAVE_SOCKADDR_IN_SIN_LEN
653 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
655 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
656 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
662 if (NULL != unixpath)
664 saddrs = GNUNET_new_array (resi + 1,
666 saddrlens = GNUNET_new_array (resi + 1,
669 if (NULL != unixpath)
671 add_unixpath (saddrs,
677 saddrlens[i] = sizeof (struct sockaddr_in6);
678 saddrs[i] = GNUNET_malloc (saddrlens[i]);
679 #if HAVE_SOCKADDR_IN_SIN_LEN
680 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
682 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
683 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
685 saddrlens[i] = sizeof (struct sockaddr_in);
686 saddrs[i] = GNUNET_malloc (saddrlens[i]);
687 #if HAVE_SOCKADDR_IN_SIN_LEN
688 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
690 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
691 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
694 GNUNET_free_non_null (unixpath);
696 *addr_lens = saddrlens;
702 * Signal our client that we will start or stop the
705 * @param client who is being signalled
706 * @param name name of the service
707 * @param request_id id of the request that is being responded to.
708 * @param result message type to send
709 * @return NULL if it was not found
712 signal_result (struct GNUNET_SERVICE_Client *client,
715 enum GNUNET_ARM_Result result)
717 struct GNUNET_MQ_Envelope *env;
718 struct GNUNET_ARM_ResultMessage *msg;
721 env = GNUNET_MQ_msg (msg,
722 GNUNET_MESSAGE_TYPE_ARM_RESULT);
723 msg->result = htonl (result);
724 msg->arm_msg.request_id = GNUNET_htonll (request_id);
725 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
731 * Tell all clients about status change of a service.
733 * @param name name of the service
734 * @param status message type to send
735 * @param unicast if not NULL, send to this client only.
736 * otherwise, send to all clients in the notifier
739 broadcast_status (const char *name,
740 enum GNUNET_ARM_ServiceStatus status,
741 struct GNUNET_SERVICE_Client *unicast)
743 struct GNUNET_MQ_Envelope *env;
744 struct GNUNET_ARM_StatusMessage *msg;
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748 "Sending status %u of service `%s' to client\n",
749 (unsigned int) status,
751 namelen = strlen (name) + 1;
752 env = GNUNET_MQ_msg_extra (msg,
754 GNUNET_MESSAGE_TYPE_ARM_STATUS);
755 msg->status = htonl ((uint32_t) (status));
756 GNUNET_memcpy ((char *) &msg[1],
761 if (NULL != notifier)
762 GNUNET_notification_context_broadcast (notifier,
765 GNUNET_MQ_discard (env);
769 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (unicast),
776 * Actually start the process for the given service.
778 * @param sl identifies service to start
779 * @param client that asked to start the service (may be NULL)
780 * @param request_id id of the request in response to which the process is
781 * being started. 0 if starting was not requested.
784 start_process (struct ServiceList *sl,
785 struct GNUNET_SERVICE_Client *client,
791 int is_simple_service;
792 struct ServiceListeningInfo *sli;
798 /* calculate listen socket list */
801 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
803 GNUNET_array_append (lsocks, ls,
804 GNUNET_NETWORK_get_fd (sli->listen_socket));
805 if (NULL != sli->accept_task)
807 GNUNET_SCHEDULER_cancel (sli->accept_task);
808 sli->accept_task = NULL;
812 GNUNET_array_append (lsocks,
816 GNUNET_array_append (lsocks,
821 /* obtain configuration */
823 GNUNET_CONFIGURATION_get_value_string (cfg,
827 loprefix = GNUNET_strdup (prefix_command);
829 loprefix = GNUNET_CONFIGURATION_expand_dollar (cfg,
832 GNUNET_CONFIGURATION_get_value_string (cfg,
838 options = GNUNET_CONFIGURATION_expand_dollar (cfg,
845 fin_options = GNUNET_strdup (final_option);
846 /* replace '{}' with service name */
847 while (NULL != (optpos = strstr (fin_options,
850 /* terminate string at opening parenthesis */
852 GNUNET_asprintf (&new_options,
857 GNUNET_free (fin_options);
858 fin_options = new_options;
862 /* combine "fin_options" with "options" */
864 GNUNET_asprintf (&options,
868 GNUNET_free (fin_options);
869 GNUNET_free (optpos);
873 /* only have "fin_options", use that */
874 options = fin_options;
877 options = GNUNET_CONFIGURATION_expand_dollar (cfg,
879 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg,
883 const char *service_type = NULL;
884 const char *choices[] = { "GNUNET", "SIMPLE", NULL };
886 is_simple_service = GNUNET_NO;
888 GNUNET_CONFIGURATION_get_value_choice (cfg,
893 (0 == strcasecmp (service_type, "SIMPLE")) )
894 is_simple_service = GNUNET_YES;
897 GNUNET_assert (NULL == sl->proc);
898 if (GNUNET_YES == is_simple_service)
900 /* A simple service will receive no GNUnet specific
901 command line options. */
902 binary = GNUNET_strdup (sl->binary);
903 binary = GNUNET_CONFIGURATION_expand_dollar (cfg, binary);
904 GNUNET_asprintf ("edbinary,
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
908 "Starting simple service `%s' using binary `%s'\n",
909 sl->name, sl->binary);
910 /* FIXME: dollar expansion should only be done outside
911 * of ''-quoted strings, escaping should be considered. */
913 options = GNUNET_CONFIGURATION_expand_dollar (cfg, options);
915 GNUNET_OS_start_process_s (sl->pipe_control,
916 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
925 /* actually start process */
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
927 "Starting service `%s' using binary `%s' and configuration `%s'\n",
928 sl->name, sl->binary, sl->config);
929 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
930 GNUNET_asprintf ("edbinary,
934 if (GNUNET_YES == use_debug)
936 if (NULL == sl->config)
938 GNUNET_OS_start_process_s (sl->pipe_control,
939 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
948 GNUNET_OS_start_process_s (sl->pipe_control,
949 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
960 if (NULL == sl->config)
962 GNUNET_OS_start_process_s (sl->pipe_control,
963 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
971 GNUNET_OS_start_process_s (sl->pipe_control,
972 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
981 GNUNET_free (binary);
982 GNUNET_free (quotedbinary);
983 if (NULL == sl->proc)
985 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
986 _("Failed to start service `%s'\n"),
989 signal_result (client,
992 GNUNET_ARM_RESULT_START_FAILED);
996 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
997 _("Starting service `%s'\n"),
999 broadcast_status (sl->name,
1000 GNUNET_ARM_SERVICE_STARTING,
1003 signal_result (client,
1006 GNUNET_ARM_RESULT_STARTING);
1009 GNUNET_free (loprefix);
1010 GNUNET_free (options);
1011 GNUNET_array_grow (lsocks,
1018 * Find the process with the given service
1019 * name in the given list and return it.
1021 * @param name which service entry to look up
1022 * @return NULL if it was not found
1024 static struct ServiceList *
1025 find_service (const char *name)
1027 struct ServiceList *sl;
1032 if (0 == strcasecmp (sl->name, name))
1041 * First connection has come to the listening socket associated with the service,
1042 * create the service in order to relay the incoming connection to it
1044 * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
1047 accept_connection (void *cls)
1049 struct ServiceListeningInfo *sli = cls;
1050 struct ServiceList *sl = sli->sl;
1052 sli->accept_task = NULL;
1053 GNUNET_assert (GNUNET_NO == in_shutdown);
1054 start_process (sl, NULL, 0);
1059 * Creating a listening socket for each of the service's addresses and
1060 * wait for the first incoming connection to it
1062 * @param sa address associated with the service
1063 * @param addr_len length of @a sa
1064 * @param sl service entry for the service in question
1067 create_listen_socket (struct sockaddr *sa,
1069 struct ServiceList *sl)
1072 struct GNUNET_NETWORK_Handle *sock;
1073 struct ServiceListeningInfo *sli;
1079 switch (sa->sa_family)
1082 sock = GNUNET_NETWORK_socket_create (PF_INET,
1087 sock = GNUNET_NETWORK_socket_create (PF_INET6,
1092 if (0 == strcmp (GNUNET_a2s (sa,
1094 "@")) /* Do not bind to blank UNIX path! */
1096 sock = GNUNET_NETWORK_socket_create (PF_UNIX,
1103 errno = EAFNOSUPPORT;
1108 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1109 _("Unable to create socket for service `%s': %s\n"),
1116 GNUNET_NETWORK_socket_setsockopt (sock,
1121 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1124 if ( (sa->sa_family == AF_INET6) &&
1126 GNUNET_NETWORK_socket_setsockopt (sock,
1131 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1135 if (AF_UNIX == sa->sa_family)
1136 GNUNET_NETWORK_unix_precheck ((struct sockaddr_un *) sa);
1139 GNUNET_NETWORK_socket_bind (sock,
1140 (const struct sockaddr *) sa,
1143 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1144 _("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
1149 GNUNET_break (GNUNET_OK ==
1150 GNUNET_NETWORK_socket_close (sock));
1155 if ((AF_UNIX == sa->sa_family)
1157 /* Permission settings are not required when abstract sockets are used */
1158 && ('\0' != ((const struct sockaddr_un *)sa)->sun_path[0])
1163 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1167 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1170 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sa)->sun_path,
1177 GNUNET_NETWORK_socket_listen (sock, 5))
1179 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1181 GNUNET_break (GNUNET_OK ==
1182 GNUNET_NETWORK_socket_close (sock));
1186 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1187 _("ARM now monitors connections to service `%s' at `%s'\n"),
1191 sli = GNUNET_new (struct ServiceListeningInfo);
1192 sli->service_addr = sa;
1193 sli->service_addr_len = addr_len;
1194 sli->listen_socket = sock;
1197 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1199 &accept_connection, sli);
1200 GNUNET_CONTAINER_DLL_insert (sl->listen_head,
1207 * Remove and free an entry in the service list. Listen sockets
1208 * must have already been cleaned up. Only to be called during shutdown.
1210 * @param sl entry to free
1213 free_service (struct ServiceList *sl)
1215 GNUNET_assert (GNUNET_YES == in_shutdown);
1216 GNUNET_CONTAINER_DLL_remove (running_head,
1219 GNUNET_assert (NULL == sl->listen_head);
1220 GNUNET_free_non_null (sl->config);
1221 GNUNET_free_non_null (sl->binary);
1222 GNUNET_free (sl->name);
1228 * Check START-message.
1230 * @param cls identification of the client
1231 * @param amsg the actual message
1232 * @return #GNUNET_OK to keep the connection open,
1233 * #GNUNET_SYSERR to close it (signal serious error)
1236 check_start (void *cls,
1237 const struct GNUNET_ARM_Message *amsg)
1240 GNUNET_MQ_check_zero_termination (amsg);
1246 * Handle START-message.
1248 * @param cls identification of the client
1249 * @param amsg the actual message
1252 handle_start (void *cls,
1253 const struct GNUNET_ARM_Message *amsg)
1255 struct GNUNET_SERVICE_Client *client = cls;
1256 const char *servicename;
1257 struct ServiceList *sl;
1258 uint64_t request_id;
1260 request_id = GNUNET_ntohll (amsg->request_id);
1261 servicename = (const char *) &amsg[1];
1262 GNUNET_SERVICE_client_continue (client);
1263 if (GNUNET_YES == in_shutdown)
1265 signal_result (client,
1268 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1271 sl = find_service (servicename);
1274 signal_result (client,
1277 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1280 sl->force_start = GNUNET_YES;
1281 if (NULL != sl->proc)
1283 signal_result (client,
1286 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
1296 * Start a shutdown sequence.
1298 * @param cls closure (refers to service)
1301 trigger_shutdown (void *cls)
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1305 "Triggering shutdown\n");
1306 GNUNET_SCHEDULER_shutdown ();
1311 * Check STOP-message.
1313 * @param cls identification of the client
1314 * @param amsg the actual message
1315 * @return #GNUNET_OK to keep the connection open,
1316 * #GNUNET_SYSERR to close it (signal serious error)
1319 check_stop (void *cls,
1320 const struct GNUNET_ARM_Message *amsg)
1323 GNUNET_MQ_check_zero_termination (amsg);
1329 * Handle STOP-message.
1331 * @param cls identification of the client
1332 * @param amsg the actual message
1335 handle_stop (void *cls,
1336 const struct GNUNET_ARM_Message *amsg)
1338 struct GNUNET_SERVICE_Client *client = cls;
1339 struct ServiceList *sl;
1340 const char *servicename;
1341 uint64_t request_id;
1343 request_id = GNUNET_ntohll (amsg->request_id);
1344 servicename = (const char *) &amsg[1];
1345 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1346 _("Preparing to stop `%s'\n"),
1348 GNUNET_SERVICE_client_continue (client);
1349 if (0 == strcasecmp (servicename,
1352 broadcast_status (servicename,
1353 GNUNET_ARM_SERVICE_STOPPING,
1355 signal_result (client,
1358 GNUNET_ARM_RESULT_STOPPING);
1359 GNUNET_SERVICE_client_persist (client);
1360 GNUNET_SCHEDULER_add_now (&trigger_shutdown,
1364 sl = find_service (servicename);
1367 signal_result (client,
1370 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1373 sl->force_start = GNUNET_NO;
1374 if (GNUNET_YES == in_shutdown)
1376 /* shutdown in progress */
1377 signal_result (client,
1380 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1383 if (NULL != sl->killing_client)
1385 /* killing already in progress */
1386 signal_result (client,
1389 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
1392 if (NULL == sl->proc)
1394 /* process is down */
1395 signal_result (client,
1398 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "Sending kill signal to service `%s', waiting for process to die.\n",
1404 broadcast_status (servicename,
1405 GNUNET_ARM_SERVICE_STOPPING,
1407 /* no signal_start - only when it's STOPPED */
1408 sl->killed_at = GNUNET_TIME_absolute_get ();
1409 if (0 != GNUNET_OS_process_kill (sl->proc,
1411 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1413 sl->killing_client = client;
1414 sl->killing_client_request_id = request_id;
1419 * Handle LIST-message.
1421 * @param cls identification of the client
1422 * @param message the actual message
1425 handle_list (void *cls,
1426 const struct GNUNET_ARM_Message *request)
1428 struct GNUNET_SERVICE_Client *client = cls;
1429 struct GNUNET_MQ_Envelope *env;
1430 struct GNUNET_ARM_ListResultMessage *msg;
1431 size_t string_list_size;
1432 struct ServiceList *sl;
1436 GNUNET_break (0 == ntohl (request->reserved));
1438 string_list_size = 0;
1440 /* first count the running processes get their name's size */
1441 for (sl = running_head; NULL != sl; sl = sl->next)
1443 if (NULL != sl->proc)
1445 string_list_size += strlen (sl->name);
1446 string_list_size += strlen (sl->binary);
1447 string_list_size += 4;
1452 env = GNUNET_MQ_msg_extra (msg,
1454 GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT);
1455 msg->arm_msg.request_id = request->request_id;
1456 msg->count = htons (count);
1458 pos = (char *) &msg[1];
1459 for (sl = running_head; NULL != sl; sl = sl->next)
1461 if (NULL != sl->proc)
1463 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
1464 GNUNET_snprintf (pos,
1472 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1474 GNUNET_SERVICE_client_continue (client);
1479 * Handle TEST-message by sending back TEST.
1481 * @param cls identification of the client
1482 * @param message the actual message
1485 handle_test (void *cls,
1486 const struct GNUNET_MessageHeader *message)
1488 struct GNUNET_SERVICE_Client *client = cls;
1489 struct GNUNET_MQ_Envelope *env;
1490 struct GNUNET_MessageHeader *msg;
1493 env = GNUNET_MQ_msg (msg,
1494 GNUNET_MESSAGE_TYPE_ARM_TEST);
1495 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1497 GNUNET_SERVICE_client_continue (client);
1502 * We are done with everything. Stop remaining
1503 * tasks, signal handler and the server.
1508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1509 "Last shutdown phase\n");
1510 if (NULL != notifier)
1512 GNUNET_notification_context_destroy (notifier);
1515 if (NULL != service)
1517 GNUNET_SERVICE_shutdown (service);
1520 if (NULL != child_death_task)
1522 GNUNET_SCHEDULER_cancel (child_death_task);
1523 child_death_task = NULL;
1529 * Count how many services are still active.
1531 * @param running_head list of services
1532 * @return number of active services found
1535 list_count (struct ServiceList *running_head)
1537 struct ServiceList *i;
1540 for (res = 0, i = running_head; NULL != i; i = i->next, res++)
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1549 * Task run for shutdown.
1551 * @param cls closure, NULL if we need to self-restart
1554 shutdown_task (void *cls)
1556 struct ServiceList *pos;
1557 struct ServiceList *nxt;
1558 struct ServiceListeningInfo *sli;
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1562 "First shutdown phase\n");
1563 if (NULL != child_restart_task)
1565 GNUNET_SCHEDULER_cancel (child_restart_task);
1566 child_restart_task = NULL;
1568 in_shutdown = GNUNET_YES;
1569 /* first, stop listening */
1570 for (pos = running_head; NULL != pos; pos = pos->next)
1572 while (NULL != (sli = pos->listen_head))
1574 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
1577 if (NULL != sli->accept_task)
1579 GNUNET_SCHEDULER_cancel (sli->accept_task);
1580 sli->accept_task = NULL;
1582 GNUNET_break (GNUNET_OK ==
1583 GNUNET_NETWORK_socket_close (sli->listen_socket));
1584 GNUNET_free (sli->service_addr);
1588 /* then, shutdown all existing service processes */
1590 while (NULL != (pos = nxt))
1593 if (NULL != pos->proc)
1595 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1596 "Stopping service `%s'\n",
1598 pos->killed_at = GNUNET_TIME_absolute_get ();
1599 if (0 != GNUNET_OS_process_kill (pos->proc,
1601 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1609 /* finally, should all service processes be already gone, terminate for real */
1610 if (NULL == running_head)
1613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1614 "Delaying shutdown, have %u childs still running\n",
1615 list_count (running_head));
1620 * Task run whenever it is time to restart a child that died.
1622 * @param cls closure, always NULL
1625 delayed_restart_task (void *cls)
1628 struct ServiceList *sl;
1629 struct GNUNET_TIME_Relative lowestRestartDelay;
1630 struct ServiceListeningInfo *sli;
1633 child_restart_task = NULL;
1634 GNUNET_assert (GNUNET_NO == in_shutdown);
1635 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1637 /* check for services that need to be restarted due to
1638 * configuration changes or because the last restart failed */
1639 for (sl = running_head; NULL != sl; sl = sl->next)
1641 if (NULL != sl->proc)
1643 /* service is currently not running */
1644 if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
1646 /* restart is now allowed */
1647 if (sl->force_start)
1649 /* process should run by default, start immediately */
1650 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1651 _("Restarting service `%s'.\n"),
1659 /* process is run on-demand, ensure it is re-started if there is demand */
1660 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1661 if (NULL == sli->accept_task)
1663 /* accept was actually paused, so start it again */
1665 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1674 /* update calculation for earliest time to reactivate a service */
1675 lowestRestartDelay =
1676 GNUNET_TIME_relative_min (lowestRestartDelay,
1677 GNUNET_TIME_absolute_get_remaining
1681 if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1684 "Will restart process in %s\n",
1685 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay,
1687 child_restart_task =
1688 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1689 GNUNET_SCHEDULER_PRIORITY_IDLE,
1690 &delayed_restart_task,
1697 * Task triggered whenever we receive a SIGCHLD (child
1700 * @param cls closure, NULL
1703 maint_child_death (void *cls)
1705 struct ServiceList *pos;
1706 struct ServiceList *next;
1707 struct ServiceListeningInfo *sli;
1708 const char *statstr;
1712 enum GNUNET_OS_ProcessStatusType statusType;
1713 unsigned long statusCode;
1714 const struct GNUNET_DISK_FileHandle *pr;
1717 pr = GNUNET_DISK_pipe_handle (sigpipe,
1718 GNUNET_DISK_PIPE_END_READ);
1719 child_death_task = NULL;
1720 /* consume the signal */
1721 GNUNET_break (0 < GNUNET_DISK_file_read (pr,
1725 /* check for services that died (WAITPID) */
1726 next = running_head;
1727 while (NULL != (pos = next))
1731 if (NULL == pos->proc)
1733 if (GNUNET_YES == in_shutdown)
1738 if (NULL != wait_file)
1740 /* need to use 'wait4()' to obtain and log performance data */
1745 pid = GNUNET_OS_process_get_pid (pos->proc);
1751 continue; /* no process done */
1752 if (WIFEXITED (status))
1754 statusType = GNUNET_OS_PROCESS_EXITED;
1755 statusCode = WEXITSTATUS (status);
1757 else if (WIFSIGNALED (status))
1759 statusType = GNUNET_OS_PROCESS_SIGNALED;
1760 statusCode = WTERMSIG (status);
1762 else if (WIFSTOPPED (status))
1764 statusType = GNUNET_OS_PROCESS_SIGNALED;
1765 statusCode = WSTOPSIG (status);
1768 else if (WIFCONTINUED (status))
1770 statusType = GNUNET_OS_PROCESS_RUNNING;
1776 statusType = GNUNET_OS_PROCESS_UNKNOWN;
1779 if ( (GNUNET_OS_PROCESS_EXITED == statusType) ||
1780 (GNUNET_OS_PROCESS_SIGNALED == statusType) )
1782 double utime = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 10e6);
1783 double stime = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 10e6);
1785 "%s(%u) %.3f %.3f %llu %llu %llu %llu %llu\n",
1790 (unsigned long long) ru.ru_maxrss,
1791 (unsigned long long) ru.ru_inblock,
1792 (unsigned long long) ru.ru_oublock,
1793 (unsigned long long) ru.ru_nvcsw,
1794 (unsigned long long) ru.ru_nivcsw);
1797 else /* continue with JUST this "if" as "else" (intentionally no brackets!) */
1799 if ( (GNUNET_SYSERR ==
1801 GNUNET_OS_process_status (pos->proc,
1804 (ret == GNUNET_NO) ||
1805 (statusType == GNUNET_OS_PROCESS_STOPPED) ||
1806 (statusType == GNUNET_OS_PROCESS_UNKNOWN) ||
1807 (statusType == GNUNET_OS_PROCESS_RUNNING) )
1810 if (statusType == GNUNET_OS_PROCESS_EXITED)
1812 statstr = _( /* process termination method */ "exit");
1813 statcode = statusCode;
1815 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1817 statstr = _( /* process termination method */ "signal");
1818 statcode = statusCode;
1822 statstr = _( /* process termination method */ "unknown");
1825 if (0 != pos->killed_at.abs_value_us)
1827 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1828 _("Service `%s' took %s to terminate\n"),
1830 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at),
1833 GNUNET_OS_process_destroy (pos->proc);
1835 broadcast_status (pos->name,
1836 GNUNET_ARM_SERVICE_STOPPED,
1838 if (NULL != pos->killing_client)
1840 signal_result (pos->killing_client, pos->name,
1841 pos->killing_client_request_id,
1842 GNUNET_ARM_RESULT_STOPPED);
1843 pos->killing_client = NULL;
1844 pos->killing_client_request_id = 0;
1846 if (GNUNET_YES != in_shutdown)
1848 if ( (statusType == GNUNET_OS_PROCESS_EXITED) &&
1851 /* process terminated normally, allow restart at any time */
1852 pos->restart_at.abs_value_us = 0;
1853 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1854 _("Service `%s' terminated normally, will restart at any time\n"),
1856 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1857 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1859 GNUNET_break (NULL == sli->accept_task);
1861 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1869 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1870 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1874 GNUNET_STRINGS_relative_time_to_string (pos->backoff,
1877 /* Reduce backoff based on runtime of the process,
1878 so that there is a cool-down if a process actually
1879 runs for a while. */
1880 struct GNUNET_TIME_Relative runtime;
1881 unsigned int minutes;
1883 runtime = GNUNET_TIME_absolute_get_duration (pos->restart_at);
1884 minutes = runtime.rel_value_us / GNUNET_TIME_UNIT_MINUTES.rel_value_us;
1886 pos->backoff = GNUNET_TIME_UNIT_ZERO;
1888 pos->backoff.rel_value_us <<= minutes;
1890 /* schedule restart */
1891 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1892 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1893 if (NULL != child_restart_task)
1894 GNUNET_SCHEDULER_cancel (child_restart_task);
1896 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1897 &delayed_restart_task,
1906 child_death_task = GNUNET_SCHEDULER_add_read_file (
1907 GNUNET_TIME_UNIT_FOREVER_REL,
1909 &maint_child_death, NULL);
1910 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1912 else if (GNUNET_YES == in_shutdown)
1913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1914 "Delaying shutdown after child's death, still have %u children\n",
1915 list_count (running_head));
1921 * Signal handler called for SIGCHLD. Triggers the
1922 * respective handler by writing to the trigger pipe.
1925 sighandler_child_death ()
1928 int old_errno = errno; /* back-up errno */
1931 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
1932 GNUNET_DISK_PIPE_END_WRITE),
1935 errno = old_errno; /* restore errno */
1940 * Setup our service record for the given section in the configuration file
1941 * (assuming the section is for a service).
1944 * @param section a section in the configuration file
1945 * @return #GNUNET_OK (continue)
1948 setup_service (void *cls,
1949 const char *section)
1951 struct ServiceList *sl;
1955 struct sockaddr **addrs;
1956 socklen_t *addr_lens;
1960 if (0 == strcasecmp (section,
1964 GNUNET_CONFIGURATION_get_value_string (cfg,
1969 /* not a service section */
1973 GNUNET_CONFIGURATION_have_value (cfg,
1977 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1981 if (GNUNET_NO == start_user)
1983 GNUNET_free (binary);
1984 return; /* user service, and we don't deal with those */
1989 if (GNUNET_NO == start_system)
1991 GNUNET_free (binary);
1992 return; /* system service, and we don't deal with those */
1995 sl = find_service (section);
1998 /* got the same section twice!? */
2000 GNUNET_free (binary);
2005 GNUNET_CONFIGURATION_get_value_filename (cfg,
2010 GNUNET_CONFIGURATION_get_value_filename (cfg,
2014 (0 != STAT (config, &sbuf)))
2018 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
2021 GNUNET_free (config);
2025 sl = GNUNET_new (struct ServiceList);
2026 sl->name = GNUNET_strdup (section);
2027 sl->binary = binary;
2028 sl->config = config;
2029 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
2030 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
2032 sl->pipe_control = GNUNET_YES;
2034 if (GNUNET_CONFIGURATION_have_value (cfg,
2037 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2041 GNUNET_CONTAINER_DLL_insert (running_head,
2045 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2049 sl->force_start = GNUNET_YES;
2051 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2059 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2064 if (0 >= (ret = get_server_addresses (section,
2069 /* this will free (or capture) addrs[i] */
2070 for (unsigned int i = 0; i < (unsigned int) ret; i++)
2071 create_listen_socket (addrs[i],
2074 GNUNET_free (addrs);
2075 GNUNET_free (addr_lens);
2080 * A client connected, mark as a monitoring client.
2082 * @param cls closure
2083 * @param client identification of the client
2084 * @param mq queue to talk to @a client
2088 client_connect_cb (void *cls,
2089 struct GNUNET_SERVICE_Client *client,
2090 struct GNUNET_MQ_Handle *mq)
2092 /* All clients are considered to be of the "monitor" kind
2093 * (that is, they don't affect ARM shutdown).
2097 GNUNET_SERVICE_client_mark_monitor (client);
2103 * A client disconnected, clean up associated state.
2105 * @param cls closure
2106 * @param client identification of the client
2107 * @param app_ctx must match @a client
2110 client_disconnect_cb (void *cls,
2111 struct GNUNET_SERVICE_Client *client,
2115 GNUNET_assert (client == app_ctx);
2116 for (struct ServiceList *sl = running_head; NULL != sl; sl = sl->next)
2117 if (sl->killing_client == client)
2118 sl->killing_client = NULL;
2123 * Handle MONITOR-message.
2125 * @param cls identification of the client
2126 * @param message the actual message
2127 * @return #GNUNET_OK to keep the connection open,
2128 * #GNUNET_SYSERR to close it (signal serious error)
2131 handle_monitor (void *cls,
2132 const struct GNUNET_MessageHeader *message)
2134 struct GNUNET_SERVICE_Client *client = cls;
2137 /* FIXME: might want to start by letting monitor know about
2138 services that are already running */
2139 /* Removal is handled by the server implementation, internally. */
2140 GNUNET_notification_context_add (notifier,
2141 GNUNET_SERVICE_client_get_mq (client));
2142 broadcast_status ("arm",
2143 GNUNET_ARM_SERVICE_MONITORING_STARTED,
2145 GNUNET_SERVICE_client_continue (client);
2150 * Process arm requests.
2152 * @param cls closure, NULL
2153 * @param serv the initialized service
2154 * @param c configuration to use
2158 const struct GNUNET_CONFIGURATION_Handle *c,
2159 struct GNUNET_SERVICE_Handle *serv)
2161 struct ServiceList *sl;
2166 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
2169 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2170 GNUNET_DISK_pipe_handle (sigpipe,
2171 GNUNET_DISK_PIPE_END_READ),
2176 GNUNET_CONFIGURATION_get_value_filename (cfg,
2178 "RESOURCE_DIAGNOSTICS",
2181 wait_file = fopen (wait_filename,
2183 if (NULL == wait_file)
2185 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2192 GNUNET_CONFIGURATION_get_value_string (cfg,
2196 prefix_command = GNUNET_strdup ("");
2198 prefix_command = GNUNET_CONFIGURATION_expand_dollar (cfg,
2201 GNUNET_CONFIGURATION_get_value_string (cfg,
2205 final_option = GNUNET_strdup ("");
2207 final_option = GNUNET_CONFIGURATION_expand_dollar (cfg,
2209 start_user = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2211 "START_USER_SERVICES");
2212 start_system = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2214 "START_SYSTEM_SERVICES");
2215 if ( (GNUNET_NO == start_user) &&
2216 (GNUNET_NO == start_system) )
2218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2219 "Please configure either START_USER_SERVICES or START_SYSTEM_SERVICES or both.\n");
2220 GNUNET_SCHEDULER_shutdown ();
2224 GNUNET_CONFIGURATION_iterate_sections (cfg,
2228 /* start default services... */
2229 for (sl = running_head; NULL != sl; sl = sl->next)
2230 if (GNUNET_YES == sl->force_start)
2234 notifier = GNUNET_notification_context_create (MAX_NOTIFY_QUEUE);
2239 * The main function for the arm service.
2241 * @param argc number of arguments from the command line
2242 * @param argv command line arguments
2243 * @return 0 ok, 1 on error
2249 struct GNUNET_SIGNAL_Context *shc_chld;
2250 struct GNUNET_MQ_MessageHandler handlers[] = {
2251 GNUNET_MQ_hd_var_size (start,
2252 GNUNET_MESSAGE_TYPE_ARM_START,
2253 struct GNUNET_ARM_Message,
2255 GNUNET_MQ_hd_var_size (stop,
2256 GNUNET_MESSAGE_TYPE_ARM_STOP,
2257 struct GNUNET_ARM_Message,
2259 GNUNET_MQ_hd_fixed_size (monitor,
2260 GNUNET_MESSAGE_TYPE_ARM_MONITOR,
2261 struct GNUNET_MessageHeader,
2263 GNUNET_MQ_hd_fixed_size (list,
2264 GNUNET_MESSAGE_TYPE_ARM_LIST,
2265 struct GNUNET_ARM_Message,
2267 GNUNET_MQ_hd_fixed_size (test,
2268 GNUNET_MESSAGE_TYPE_ARM_TEST,
2269 struct GNUNET_MessageHeader,
2271 GNUNET_MQ_handler_end ()
2274 sigpipe = GNUNET_DISK_pipe (GNUNET_NO,
2278 GNUNET_assert (NULL != sigpipe);
2280 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD,
2281 &sighandler_child_death);
2282 if ( GNUNET_OK != GNUNET_SERVICE_run_ (argc,
2285 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN,
2288 &client_disconnect_cb,
2293 if (NULL != wait_file)
2298 if (NULL != wait_filename)
2300 GNUNET_free (wait_filename);
2301 wait_filename = NULL;
2304 GNUNET_SIGNAL_handler_uninstall (shc_chld);
2306 GNUNET_DISK_pipe_close (sigpipe);
2312 #if defined(LINUX) && defined(__GLIBC__)
2316 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
2318 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
2320 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
2321 mallopt (M_TOP_PAD, 1 * 1024);
2327 /* end of gnunet-service-arm.c */