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/>.
20 * @file arm/gnunet-service-arm.c
21 * @brief the automated restart manager service
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_arm_service.h"
27 #include "gnunet_protocols.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
32 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
37 * Name of the file for writing resource utilization summaries to.
39 static char *wait_filename;
42 * Handle for the file for writing resource summaries.
44 static FILE *wait_file;
49 * How many messages do we queue up at most for optional
50 * notifications to a client? (this can cause notifications
51 * about outgoing messages to be dropped).
53 #define MAX_NOTIFY_QUEUE 1024
57 * List of our services.
63 * Record with information about a listen socket we have open.
65 struct ServiceListeningInfo
68 * This is a linked list.
70 struct ServiceListeningInfo *next;
73 * This is a linked list.
75 struct ServiceListeningInfo *prev;
78 * Address this socket is listening on.
80 struct sockaddr *service_addr;
83 * Service this listen socket is for.
85 struct ServiceList *sl;
88 * Number of bytes in @e service_addr
90 socklen_t service_addr_len;
93 * Our listening socket.
95 struct GNUNET_NETWORK_Handle *listen_socket;
98 * Task doing the accepting.
100 struct GNUNET_SCHEDULER_Task *accept_task;
106 * List of our services.
111 * This is a doubly-linked list.
113 struct ServiceList *next;
116 * This is a doubly-linked list.
118 struct ServiceList *prev;
121 * Linked list of listen sockets associated with this service.
123 struct ServiceListeningInfo *listen_head;
126 * Linked list of listen sockets associated with this service.
128 struct ServiceListeningInfo *listen_tail;
131 * Name of the service.
136 * Name of the binary used.
141 * Name of the configuration file used.
146 * Client to notify upon kill completion (waitpid), NULL
147 * if we should simply restart the process.
149 struct GNUNET_SERVICE_Client *killing_client;
152 * ID of the request that killed the service (for reporting back).
154 uint64_t killing_client_request_id;
157 * Process structure pointer of the child.
159 struct GNUNET_OS_Process *proc;
162 * Process exponential backoff time
164 struct GNUNET_TIME_Relative backoff;
167 * Absolute time at which the process is scheduled to restart in case of death
169 struct GNUNET_TIME_Absolute restart_at;
172 * Time we asked the service to shut down (used to calculate time it took
173 * the service to terminate).
175 struct GNUNET_TIME_Absolute killed_at;
178 * Is this service to be started by default (or did a client tell us explicitly
179 * to start it)? #GNUNET_NO if the service is started only upon 'accept' on a
180 * listen socket or possibly explicitly by a client changing the value.
185 * Should we use pipes to signal this process? (YES for Java binaries and if we
192 * List of running services.
194 static struct ServiceList *running_head;
197 * List of running services.
199 static struct ServiceList *running_tail;
204 static const struct GNUNET_CONFIGURATION_Handle *cfg;
207 * Command to prepend to each actual command.
209 static char *prefix_command;
212 * Option to append to each actual command.
214 static char *final_option;
217 * ID of task called whenever we get a SIGCHILD.
219 static struct GNUNET_SCHEDULER_Task *child_death_task;
222 * ID of task called whenever the timeout for restarting a child
225 static struct GNUNET_SCHEDULER_Task *child_restart_task;
228 * Pipe used to communicate shutdown via signal.
230 static struct GNUNET_DISK_PipeHandle *sigpipe;
233 * Are we in shutdown mode?
235 static int in_shutdown;
238 * Return value from main
240 static int global_ret;
243 * Are we starting user services?
245 static int start_user = GNUNET_YES;
248 * Are we starting system services?
250 static int start_system = GNUNET_YES;
253 * Handle to our service instance. Our service is a bit special in that
254 * its service is not immediately stopped once we get a shutdown
255 * request (since we need to continue service until all of our child
256 * processes are dead). This handle is used to shut down the service
257 * (and thus trigger process termination) once all child processes are
258 * also dead. A special option in the ARM configuration modifies the
259 * behaviour of the service implementation to not do the shutdown
262 static struct GNUNET_SERVICE_Handle *service;
265 * Context for notifications we need to send to our clients.
267 static struct GNUNET_NotificationContext *notifier;
271 * Add the given UNIX domain path as an address to the
272 * list (as the first entry).
274 * @param saddrs array to update
275 * @param saddrlens where to store the address length
276 * @param unixpath path to add
277 * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
278 * parameter is ignore on systems other than LINUX
281 add_unixpath (struct sockaddr **saddrs,
282 socklen_t *saddrlens,
283 const char *unixpath,
287 struct sockaddr_un *un;
289 un = GNUNET_new (struct sockaddr_un);
290 un->sun_family = AF_UNIX;
291 strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
293 if (GNUNET_YES == abstract)
294 un->sun_path[0] = '\0';
296 #if HAVE_SOCKADDR_UN_SUN_LEN
297 un->sun_len = (u_char) sizeof (struct sockaddr_un);
299 *saddrs = (struct sockaddr *) un;
300 *saddrlens = sizeof (struct sockaddr_un);
302 /* this function should never be called
303 * unless AF_UNIX is defined! */
310 * Get the list of addresses that a server for the given service
313 * @param service_name name of the service
314 * @param cfg configuration (which specifies the addresses)
315 * @param addrs set (call by reference) to an array of pointers to the
316 * addresses the server should bind to and listen on; the
317 * array will be NULL-terminated (on success)
318 * @param addr_lens set (call by reference) to an array of the lengths
319 * of the respective `struct sockaddr` struct in the @a addrs
321 * @return number of addresses found on success,
322 * #GNUNET_SYSERR if the configuration
323 * did not specify reasonable finding information or
324 * if it specified a hostname that could not be resolved;
325 * #GNUNET_NO if the number of addresses configured is
326 * zero (in this case, `*addrs` and `*addr_lens` will be
330 get_server_addresses (const char *service_name,
331 const struct GNUNET_CONFIGURATION_Handle *cfg,
332 struct sockaddr ***addrs,
333 socklen_t ** addr_lens)
336 struct GNUNET_NETWORK_Handle *desc;
337 unsigned long long port;
339 struct addrinfo hints;
340 struct addrinfo *res;
341 struct addrinfo *pos;
342 struct addrinfo *next;
347 struct sockaddr **saddrs;
348 socklen_t *saddrlens;
354 if (GNUNET_CONFIGURATION_have_value (cfg,
360 GNUNET_CONFIGURATION_get_value_yesno (cfg,
363 return GNUNET_SYSERR;
366 disablev6 = GNUNET_NO;
370 /* probe IPv6 support */
371 desc = GNUNET_NETWORK_socket_create (PF_INET6,
376 if ( (ENOBUFS == errno) ||
381 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
383 return GNUNET_SYSERR;
385 LOG (GNUNET_ERROR_TYPE_INFO,
386 _("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
389 disablev6 = GNUNET_YES;
393 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
399 if (GNUNET_CONFIGURATION_have_value (cfg,
404 GNUNET_CONFIGURATION_get_value_number (cfg,
409 LOG (GNUNET_ERROR_TYPE_ERROR,
410 _("Require valid port number for service `%s' in configuration!\n"),
415 LOG (GNUNET_ERROR_TYPE_ERROR,
416 _("Require valid port number for service `%s' in configuration!\n"),
418 return GNUNET_SYSERR;
422 if (GNUNET_CONFIGURATION_have_value (cfg,
426 GNUNET_break (GNUNET_OK ==
427 GNUNET_CONFIGURATION_get_value_string (cfg,
436 abstract = GNUNET_NO;
439 GNUNET_CONFIGURATION_have_value (cfg,
443 GNUNET_CONFIGURATION_get_value_filename (cfg,
447 (0 < strlen (unixpath)))
449 /* probe UNIX support */
450 struct sockaddr_un s_un;
452 if (strlen (unixpath) >= sizeof (s_un.sun_path))
454 LOG (GNUNET_ERROR_TYPE_WARNING,
455 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
457 (unsigned long long) sizeof (s_un.sun_path));
458 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
459 LOG (GNUNET_ERROR_TYPE_INFO,
460 _("Using `%s' instead\n"),
464 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
466 "USE_ABSTRACT_SOCKETS");
467 if (GNUNET_SYSERR == abstract)
468 abstract = GNUNET_NO;
470 if ( (GNUNET_YES != abstract) &&
472 GNUNET_DISK_directory_create_for_file (unixpath)) )
473 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
477 if (NULL != unixpath)
479 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
482 if ( (ENOBUFS == errno) ||
487 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
488 GNUNET_free_non_null (hostname);
489 GNUNET_free (unixpath);
490 return GNUNET_SYSERR;
492 LOG (GNUNET_ERROR_TYPE_INFO,
493 _("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
496 GNUNET_free (unixpath);
501 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
511 GNUNET_CONFIGURATION_get_value_yesno (cfg,
514 LOG (GNUNET_ERROR_TYPE_ERROR,
515 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
517 GNUNET_free_non_null (hostname);
518 return GNUNET_SYSERR;
522 saddrs = GNUNET_new_array (2,
524 saddrlens = GNUNET_new_array (2,
526 add_unixpath (saddrs,
530 GNUNET_free_non_null (unixpath);
531 GNUNET_free_non_null (hostname);
533 *addr_lens = saddrlens;
537 if (NULL != hostname)
539 LOG (GNUNET_ERROR_TYPE_DEBUG,
540 "Resolving `%s' since that is where `%s' will bind to.\n",
543 memset (&hints, 0, sizeof (struct addrinfo));
545 hints.ai_family = AF_INET;
546 hints.ai_protocol = IPPROTO_TCP;
547 if ((0 != (ret = getaddrinfo (hostname,
553 LOG (GNUNET_ERROR_TYPE_ERROR,
554 _("Failed to resolve `%s': %s\n"),
557 GNUNET_free (hostname);
558 GNUNET_free_non_null (unixpath);
559 return GNUNET_SYSERR;
563 while (NULL != (pos = next))
566 if ((disablev6) && (pos->ai_family == AF_INET6))
572 LOG (GNUNET_ERROR_TYPE_ERROR,
573 _("Failed to find %saddress for `%s'.\n"),
574 disablev6 ? "IPv4 " : "",
577 GNUNET_free (hostname);
578 GNUNET_free_non_null (unixpath);
579 return GNUNET_SYSERR;
582 if (NULL != unixpath)
584 saddrs = GNUNET_new_array (resi + 1,
586 saddrlens = GNUNET_new_array (resi + 1,
589 if (NULL != unixpath)
591 add_unixpath (saddrs, saddrlens, unixpath, abstract);
595 while (NULL != (pos = next))
598 if ((disablev6) && (AF_INET6 == pos->ai_family))
600 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
601 continue; /* not TCP */
602 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
604 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
605 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
606 if (AF_INET == pos->ai_family)
608 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
609 saddrlens[i] = pos->ai_addrlen;
610 saddrs[i] = GNUNET_malloc (saddrlens[i]);
611 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
612 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
616 GNUNET_assert (AF_INET6 == pos->ai_family);
617 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
618 saddrlens[i] = pos->ai_addrlen;
619 saddrs[i] = GNUNET_malloc (saddrlens[i]);
620 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
621 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
625 GNUNET_free (hostname);
631 /* will bind against everything, just set port */
636 if (NULL != unixpath)
639 saddrs = GNUNET_new_array (resi + 1,
641 saddrlens = GNUNET_new_array (resi + 1,
643 if (NULL != unixpath)
645 add_unixpath (saddrs, saddrlens, unixpath, abstract);
648 saddrlens[i] = sizeof (struct sockaddr_in);
649 saddrs[i] = GNUNET_malloc (saddrlens[i]);
650 #if HAVE_SOCKADDR_IN_SIN_LEN
651 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
653 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
654 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
660 if (NULL != unixpath)
662 saddrs = GNUNET_new_array (resi + 1,
664 saddrlens = GNUNET_new_array (resi + 1,
667 if (NULL != unixpath)
669 add_unixpath (saddrs,
675 saddrlens[i] = sizeof (struct sockaddr_in6);
676 saddrs[i] = GNUNET_malloc (saddrlens[i]);
677 #if HAVE_SOCKADDR_IN_SIN_LEN
678 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
680 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
681 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
683 saddrlens[i] = sizeof (struct sockaddr_in);
684 saddrs[i] = GNUNET_malloc (saddrlens[i]);
685 #if HAVE_SOCKADDR_IN_SIN_LEN
686 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
688 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
689 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
692 GNUNET_free_non_null (unixpath);
694 *addr_lens = saddrlens;
700 * Signal our client that we will start or stop the
703 * @param client who is being signalled
704 * @param name name of the service
705 * @param request_id id of the request that is being responded to.
706 * @param result message type to send
707 * @return NULL if it was not found
710 signal_result (struct GNUNET_SERVICE_Client *client,
713 enum GNUNET_ARM_Result result)
715 struct GNUNET_MQ_Envelope *env;
716 struct GNUNET_ARM_ResultMessage *msg;
719 env = GNUNET_MQ_msg (msg,
720 GNUNET_MESSAGE_TYPE_ARM_RESULT);
721 msg->result = htonl (result);
722 msg->arm_msg.request_id = GNUNET_htonll (request_id);
723 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
729 * Tell all clients about status change of a service.
731 * @param name name of the service
732 * @param status message type to send
733 * @param unicast if not NULL, send to this client only.
734 * otherwise, send to all clients in the notifier
737 broadcast_status (const char *name,
738 enum GNUNET_ARM_ServiceStatus status,
739 struct GNUNET_SERVICE_Client *unicast)
741 struct GNUNET_MQ_Envelope *env;
742 struct GNUNET_ARM_StatusMessage *msg;
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
746 "Sending status %u of service `%s' to client\n",
747 (unsigned int) status,
749 namelen = strlen (name) + 1;
750 env = GNUNET_MQ_msg_extra (msg,
752 GNUNET_MESSAGE_TYPE_ARM_STATUS);
753 msg->status = htonl ((uint32_t) (status));
754 GNUNET_memcpy ((char *) &msg[1],
759 if (NULL != notifier)
760 GNUNET_notification_context_broadcast (notifier,
763 GNUNET_MQ_discard (env);
767 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (unicast),
774 * Actually start the process for the given service.
776 * @param sl identifies service to start
777 * @param client that asked to start the service (may be NULL)
778 * @param request_id id of the request in response to which the process is
779 * being started. 0 if starting was not requested.
782 start_process (struct ServiceList *sl,
783 struct GNUNET_SERVICE_Client *client,
789 int is_simple_service;
790 struct ServiceListeningInfo *sli;
796 /* calculate listen socket list */
799 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
801 GNUNET_array_append (lsocks, ls,
802 GNUNET_NETWORK_get_fd (sli->listen_socket));
803 if (NULL != sli->accept_task)
805 GNUNET_SCHEDULER_cancel (sli->accept_task);
806 sli->accept_task = NULL;
810 GNUNET_array_append (lsocks,
814 GNUNET_array_append (lsocks,
819 /* obtain configuration */
821 GNUNET_CONFIGURATION_get_value_string (cfg,
825 loprefix = GNUNET_strdup (prefix_command);
827 loprefix = GNUNET_CONFIGURATION_expand_dollar (cfg,
830 GNUNET_CONFIGURATION_get_value_string (cfg,
836 options = GNUNET_CONFIGURATION_expand_dollar (cfg,
843 fin_options = GNUNET_strdup (final_option);
844 /* replace '{}' with service name */
845 while (NULL != (optpos = strstr (fin_options,
848 /* terminate string at opening parenthesis */
850 GNUNET_asprintf (&new_options,
855 GNUNET_free (fin_options);
856 fin_options = new_options;
860 /* combine "fin_options" with "options" */
862 GNUNET_asprintf (&options,
866 GNUNET_free (fin_options);
867 GNUNET_free (optpos);
871 /* only have "fin_options", use that */
872 options = fin_options;
875 options = GNUNET_CONFIGURATION_expand_dollar (cfg,
877 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg,
881 const char *service_type = NULL;
882 const char *choices[] = { "GNUNET", "SIMPLE", NULL };
884 is_simple_service = GNUNET_NO;
886 GNUNET_CONFIGURATION_get_value_choice (cfg,
891 (0 == strcasecmp (service_type, "SIMPLE")) )
892 is_simple_service = GNUNET_YES;
895 GNUNET_assert (NULL == sl->proc);
896 if (GNUNET_YES == is_simple_service)
898 /* A simple service will receive no GNUnet specific
899 command line options. */
900 binary = GNUNET_strdup (sl->binary);
901 binary = GNUNET_CONFIGURATION_expand_dollar (cfg, binary);
902 GNUNET_asprintf ("edbinary,
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "Starting simple service `%s' using binary `%s'\n",
907 sl->name, sl->binary);
908 /* FIXME: dollar expansion should only be done outside
909 * of ''-quoted strings, escaping should be considered. */
911 options = GNUNET_CONFIGURATION_expand_dollar (cfg, options);
913 GNUNET_OS_start_process_s (sl->pipe_control,
914 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
923 /* actually start process */
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
925 "Starting service `%s' using binary `%s' and configuration `%s'\n",
926 sl->name, sl->binary, sl->config);
927 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
928 GNUNET_asprintf ("edbinary,
932 if (GNUNET_YES == use_debug)
934 if (NULL == sl->config)
936 GNUNET_OS_start_process_s (sl->pipe_control,
937 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
946 GNUNET_OS_start_process_s (sl->pipe_control,
947 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
958 if (NULL == sl->config)
960 GNUNET_OS_start_process_s (sl->pipe_control,
961 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
969 GNUNET_OS_start_process_s (sl->pipe_control,
970 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
979 GNUNET_free (binary);
980 GNUNET_free (quotedbinary);
981 if (NULL == sl->proc)
983 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
984 _("Failed to start service `%s'\n"),
987 signal_result (client,
990 GNUNET_ARM_RESULT_START_FAILED);
994 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
995 _("Starting service `%s'\n"),
997 broadcast_status (sl->name,
998 GNUNET_ARM_SERVICE_STARTING,
1001 signal_result (client,
1004 GNUNET_ARM_RESULT_STARTING);
1007 GNUNET_free (loprefix);
1008 GNUNET_free (options);
1009 GNUNET_array_grow (lsocks,
1016 * Find the process with the given service
1017 * name in the given list and return it.
1019 * @param name which service entry to look up
1020 * @return NULL if it was not found
1022 static struct ServiceList *
1023 find_service (const char *name)
1025 struct ServiceList *sl;
1030 if (0 == strcasecmp (sl->name, name))
1039 * First connection has come to the listening socket associated with the service,
1040 * create the service in order to relay the incoming connection to it
1042 * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
1045 accept_connection (void *cls)
1047 struct ServiceListeningInfo *sli = cls;
1048 struct ServiceList *sl = sli->sl;
1050 sli->accept_task = NULL;
1051 GNUNET_assert (GNUNET_NO == in_shutdown);
1052 start_process (sl, NULL, 0);
1057 * Creating a listening socket for each of the service's addresses and
1058 * wait for the first incoming connection to it
1060 * @param sa address associated with the service
1061 * @param addr_len length of @a sa
1062 * @param sl service entry for the service in question
1065 create_listen_socket (struct sockaddr *sa,
1067 struct ServiceList *sl)
1070 struct GNUNET_NETWORK_Handle *sock;
1071 struct ServiceListeningInfo *sli;
1077 switch (sa->sa_family)
1080 sock = GNUNET_NETWORK_socket_create (PF_INET,
1085 sock = GNUNET_NETWORK_socket_create (PF_INET6,
1090 if (0 == strcmp (GNUNET_a2s (sa,
1092 "@")) /* Do not bind to blank UNIX path! */
1094 sock = GNUNET_NETWORK_socket_create (PF_UNIX,
1101 errno = EAFNOSUPPORT;
1106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1107 _("Unable to create socket for service `%s': %s\n"),
1114 GNUNET_NETWORK_socket_setsockopt (sock,
1119 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1122 if ( (sa->sa_family == AF_INET6) &&
1124 GNUNET_NETWORK_socket_setsockopt (sock,
1129 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1133 if (AF_UNIX == sa->sa_family)
1134 GNUNET_NETWORK_unix_precheck ((struct sockaddr_un *) sa);
1137 GNUNET_NETWORK_socket_bind (sock,
1138 (const struct sockaddr *) sa,
1141 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1142 _("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
1147 GNUNET_break (GNUNET_OK ==
1148 GNUNET_NETWORK_socket_close (sock));
1153 if ((AF_UNIX == sa->sa_family)
1155 /* Permission settings are not required when abstract sockets are used */
1156 && ('\0' != ((const struct sockaddr_un *)sa)->sun_path[0])
1161 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1165 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1168 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sa)->sun_path,
1175 GNUNET_NETWORK_socket_listen (sock, 5))
1177 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1179 GNUNET_break (GNUNET_OK ==
1180 GNUNET_NETWORK_socket_close (sock));
1184 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1185 _("ARM now monitors connections to service `%s' at `%s'\n"),
1189 sli = GNUNET_new (struct ServiceListeningInfo);
1190 sli->service_addr = sa;
1191 sli->service_addr_len = addr_len;
1192 sli->listen_socket = sock;
1195 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1197 &accept_connection, sli);
1198 GNUNET_CONTAINER_DLL_insert (sl->listen_head,
1205 * Remove and free an entry in the service list. Listen sockets
1206 * must have already been cleaned up. Only to be called during shutdown.
1208 * @param sl entry to free
1211 free_service (struct ServiceList *sl)
1213 GNUNET_assert (GNUNET_YES == in_shutdown);
1214 GNUNET_CONTAINER_DLL_remove (running_head,
1217 GNUNET_assert (NULL == sl->listen_head);
1218 GNUNET_free_non_null (sl->config);
1219 GNUNET_free_non_null (sl->binary);
1220 GNUNET_free (sl->name);
1226 * Check START-message.
1228 * @param cls identification of the client
1229 * @param amsg the actual message
1230 * @return #GNUNET_OK to keep the connection open,
1231 * #GNUNET_SYSERR to close it (signal serious error)
1234 check_start (void *cls,
1235 const struct GNUNET_ARM_Message *amsg)
1238 GNUNET_MQ_check_zero_termination (amsg);
1244 * Handle START-message.
1246 * @param cls identification of the client
1247 * @param amsg the actual message
1250 handle_start (void *cls,
1251 const struct GNUNET_ARM_Message *amsg)
1253 struct GNUNET_SERVICE_Client *client = cls;
1254 const char *servicename;
1255 struct ServiceList *sl;
1256 uint64_t request_id;
1258 request_id = GNUNET_ntohll (amsg->request_id);
1259 servicename = (const char *) &amsg[1];
1260 GNUNET_SERVICE_client_continue (client);
1261 if (GNUNET_YES == in_shutdown)
1263 signal_result (client,
1266 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1269 sl = find_service (servicename);
1272 signal_result (client,
1275 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1278 sl->force_start = GNUNET_YES;
1279 if (NULL != sl->proc)
1281 signal_result (client,
1284 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
1294 * Start a shutdown sequence.
1296 * @param cls closure (refers to service)
1299 trigger_shutdown (void *cls)
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 "Triggering shutdown\n");
1304 GNUNET_SCHEDULER_shutdown ();
1309 * Check STOP-message.
1311 * @param cls identification of the client
1312 * @param amsg the actual message
1313 * @return #GNUNET_OK to keep the connection open,
1314 * #GNUNET_SYSERR to close it (signal serious error)
1317 check_stop (void *cls,
1318 const struct GNUNET_ARM_Message *amsg)
1321 GNUNET_MQ_check_zero_termination (amsg);
1327 * Handle STOP-message.
1329 * @param cls identification of the client
1330 * @param amsg the actual message
1333 handle_stop (void *cls,
1334 const struct GNUNET_ARM_Message *amsg)
1336 struct GNUNET_SERVICE_Client *client = cls;
1337 struct ServiceList *sl;
1338 const char *servicename;
1339 uint64_t request_id;
1341 request_id = GNUNET_ntohll (amsg->request_id);
1342 servicename = (const char *) &amsg[1];
1343 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1344 _("Preparing to stop `%s'\n"),
1346 GNUNET_SERVICE_client_continue (client);
1347 if (0 == strcasecmp (servicename,
1350 broadcast_status (servicename,
1351 GNUNET_ARM_SERVICE_STOPPING,
1353 signal_result (client,
1356 GNUNET_ARM_RESULT_STOPPING);
1357 GNUNET_SERVICE_client_persist (client);
1358 GNUNET_SCHEDULER_add_now (&trigger_shutdown,
1362 sl = find_service (servicename);
1365 signal_result (client,
1368 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1371 sl->force_start = GNUNET_NO;
1372 if (GNUNET_YES == in_shutdown)
1374 /* shutdown in progress */
1375 signal_result (client,
1378 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1381 if (NULL != sl->killing_client)
1383 /* killing already in progress */
1384 signal_result (client,
1387 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
1390 if (NULL == sl->proc)
1392 /* process is down */
1393 signal_result (client,
1396 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1400 "Sending kill signal to service `%s', waiting for process to die.\n",
1402 broadcast_status (servicename,
1403 GNUNET_ARM_SERVICE_STOPPING,
1405 /* no signal_start - only when it's STOPPED */
1406 sl->killed_at = GNUNET_TIME_absolute_get ();
1407 if (0 != GNUNET_OS_process_kill (sl->proc,
1409 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1411 sl->killing_client = client;
1412 sl->killing_client_request_id = request_id;
1417 * Handle LIST-message.
1419 * @param cls identification of the client
1420 * @param message the actual message
1423 handle_list (void *cls,
1424 const struct GNUNET_ARM_Message *request)
1426 struct GNUNET_SERVICE_Client *client = cls;
1427 struct GNUNET_MQ_Envelope *env;
1428 struct GNUNET_ARM_ListResultMessage *msg;
1429 size_t string_list_size;
1430 struct ServiceList *sl;
1434 GNUNET_break (0 == ntohl (request->reserved));
1436 string_list_size = 0;
1438 /* first count the running processes get their name's size */
1439 for (sl = running_head; NULL != sl; sl = sl->next)
1441 if (NULL != sl->proc)
1443 string_list_size += strlen (sl->name);
1444 string_list_size += strlen (sl->binary);
1445 string_list_size += 4;
1450 env = GNUNET_MQ_msg_extra (msg,
1452 GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT);
1453 msg->arm_msg.request_id = request->request_id;
1454 msg->count = htons (count);
1456 pos = (char *) &msg[1];
1457 for (sl = running_head; NULL != sl; sl = sl->next)
1459 if (NULL != sl->proc)
1461 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
1462 GNUNET_snprintf (pos,
1470 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1472 GNUNET_SERVICE_client_continue (client);
1477 * Handle TEST-message by sending back TEST.
1479 * @param cls identification of the client
1480 * @param message the actual message
1483 handle_test (void *cls,
1484 const struct GNUNET_MessageHeader *message)
1486 struct GNUNET_SERVICE_Client *client = cls;
1487 struct GNUNET_MQ_Envelope *env;
1488 struct GNUNET_MessageHeader *msg;
1491 env = GNUNET_MQ_msg (msg,
1492 GNUNET_MESSAGE_TYPE_ARM_TEST);
1493 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1495 GNUNET_SERVICE_client_continue (client);
1500 * We are done with everything. Stop remaining
1501 * tasks, signal handler and the server.
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507 "Last shutdown phase\n");
1508 if (NULL != notifier)
1510 GNUNET_notification_context_destroy (notifier);
1513 if (NULL != service)
1515 GNUNET_SERVICE_shutdown (service);
1518 if (NULL != child_death_task)
1520 GNUNET_SCHEDULER_cancel (child_death_task);
1521 child_death_task = NULL;
1527 * Count how many services are still active.
1529 * @param running_head list of services
1530 * @return number of active services found
1533 list_count (struct ServiceList *running_head)
1535 struct ServiceList *i;
1538 for (res = 0, i = running_head; NULL != i; i = i->next, res++)
1539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1547 * Task run for shutdown.
1549 * @param cls closure, NULL if we need to self-restart
1552 shutdown_task (void *cls)
1554 struct ServiceList *pos;
1555 struct ServiceList *nxt;
1556 struct ServiceListeningInfo *sli;
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 "First shutdown phase\n");
1561 if (NULL != child_restart_task)
1563 GNUNET_SCHEDULER_cancel (child_restart_task);
1564 child_restart_task = NULL;
1566 in_shutdown = GNUNET_YES;
1567 /* first, stop listening */
1568 for (pos = running_head; NULL != pos; pos = pos->next)
1570 while (NULL != (sli = pos->listen_head))
1572 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
1575 if (NULL != sli->accept_task)
1577 GNUNET_SCHEDULER_cancel (sli->accept_task);
1578 sli->accept_task = NULL;
1580 GNUNET_break (GNUNET_OK ==
1581 GNUNET_NETWORK_socket_close (sli->listen_socket));
1582 GNUNET_free (sli->service_addr);
1586 /* then, shutdown all existing service processes */
1588 while (NULL != (pos = nxt))
1591 if (NULL != pos->proc)
1593 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1594 "Stopping service `%s'\n",
1596 pos->killed_at = GNUNET_TIME_absolute_get ();
1597 if (0 != GNUNET_OS_process_kill (pos->proc,
1599 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1607 /* finally, should all service processes be already gone, terminate for real */
1608 if (NULL == running_head)
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1612 "Delaying shutdown, have %u childs still running\n",
1613 list_count (running_head));
1618 * Task run whenever it is time to restart a child that died.
1620 * @param cls closure, always NULL
1623 delayed_restart_task (void *cls)
1626 struct ServiceList *sl;
1627 struct GNUNET_TIME_Relative lowestRestartDelay;
1628 struct ServiceListeningInfo *sli;
1631 child_restart_task = NULL;
1632 GNUNET_assert (GNUNET_NO == in_shutdown);
1633 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1635 /* check for services that need to be restarted due to
1636 * configuration changes or because the last restart failed */
1637 for (sl = running_head; NULL != sl; sl = sl->next)
1639 if (NULL != sl->proc)
1641 /* service is currently not running */
1642 if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
1644 /* restart is now allowed */
1645 if (sl->force_start)
1647 /* process should run by default, start immediately */
1648 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1649 _("Restarting service `%s'.\n"),
1657 /* process is run on-demand, ensure it is re-started if there is demand */
1658 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1659 if (NULL == sli->accept_task)
1661 /* accept was actually paused, so start it again */
1663 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1672 /* update calculation for earliest time to reactivate a service */
1673 lowestRestartDelay =
1674 GNUNET_TIME_relative_min (lowestRestartDelay,
1675 GNUNET_TIME_absolute_get_remaining
1679 if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1682 "Will restart process in %s\n",
1683 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay,
1685 child_restart_task =
1686 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1687 GNUNET_SCHEDULER_PRIORITY_IDLE,
1688 &delayed_restart_task,
1695 * Task triggered whenever we receive a SIGCHLD (child
1698 * @param cls closure, NULL
1701 maint_child_death (void *cls)
1703 struct ServiceList *pos;
1704 struct ServiceList *next;
1705 struct ServiceListeningInfo *sli;
1706 const char *statstr;
1710 enum GNUNET_OS_ProcessStatusType statusType;
1711 unsigned long statusCode;
1712 const struct GNUNET_DISK_FileHandle *pr;
1715 pr = GNUNET_DISK_pipe_handle (sigpipe,
1716 GNUNET_DISK_PIPE_END_READ);
1717 child_death_task = NULL;
1718 /* consume the signal */
1719 GNUNET_break (0 < GNUNET_DISK_file_read (pr,
1723 /* check for services that died (WAITPID) */
1724 next = running_head;
1725 while (NULL != (pos = next))
1729 if (NULL == pos->proc)
1731 if (GNUNET_YES == in_shutdown)
1736 if (NULL != wait_file)
1738 /* need to use 'wait4()' to obtain and log performance data */
1743 pid = GNUNET_OS_process_get_pid (pos->proc);
1749 continue; /* no process done */
1750 if (WIFEXITED (status))
1752 statusType = GNUNET_OS_PROCESS_EXITED;
1753 statusCode = WEXITSTATUS (status);
1755 else if (WIFSIGNALED (status))
1757 statusType = GNUNET_OS_PROCESS_SIGNALED;
1758 statusCode = WTERMSIG (status);
1760 else if (WIFSTOPPED (status))
1762 statusType = GNUNET_OS_PROCESS_SIGNALED;
1763 statusCode = WSTOPSIG (status);
1766 else if (WIFCONTINUED (status))
1768 statusType = GNUNET_OS_PROCESS_RUNNING;
1774 statusType = GNUNET_OS_PROCESS_UNKNOWN;
1777 if ( (GNUNET_OS_PROCESS_EXITED == statusType) ||
1778 (GNUNET_OS_PROCESS_SIGNALED == statusType) )
1780 double utime = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 10e6);
1781 double stime = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 10e6);
1783 "%s(%u) %.3f %.3f %llu %llu %llu %llu %llu\n",
1788 (unsigned long long) ru.ru_maxrss,
1789 (unsigned long long) ru.ru_inblock,
1790 (unsigned long long) ru.ru_oublock,
1791 (unsigned long long) ru.ru_nvcsw,
1792 (unsigned long long) ru.ru_nivcsw);
1795 else /* continue with JUST this "if" as "else" (intentionally no brackets!) */
1797 if ( (GNUNET_SYSERR ==
1799 GNUNET_OS_process_status (pos->proc,
1802 (ret == GNUNET_NO) ||
1803 (statusType == GNUNET_OS_PROCESS_STOPPED) ||
1804 (statusType == GNUNET_OS_PROCESS_UNKNOWN) ||
1805 (statusType == GNUNET_OS_PROCESS_RUNNING) )
1808 if (statusType == GNUNET_OS_PROCESS_EXITED)
1810 statstr = _( /* process termination method */ "exit");
1811 statcode = statusCode;
1813 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1815 statstr = _( /* process termination method */ "signal");
1816 statcode = statusCode;
1820 statstr = _( /* process termination method */ "unknown");
1823 if (0 != pos->killed_at.abs_value_us)
1825 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1826 _("Service `%s' took %s to terminate\n"),
1828 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at),
1831 GNUNET_OS_process_destroy (pos->proc);
1833 broadcast_status (pos->name,
1834 GNUNET_ARM_SERVICE_STOPPED,
1836 if (NULL != pos->killing_client)
1838 signal_result (pos->killing_client, pos->name,
1839 pos->killing_client_request_id,
1840 GNUNET_ARM_RESULT_STOPPED);
1841 pos->killing_client = NULL;
1842 pos->killing_client_request_id = 0;
1844 if (GNUNET_YES != in_shutdown)
1846 if ( (statusType == GNUNET_OS_PROCESS_EXITED) &&
1849 /* process terminated normally, allow restart at any time */
1850 pos->restart_at.abs_value_us = 0;
1851 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1852 _("Service `%s' terminated normally, will restart at any time\n"),
1854 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1855 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1857 GNUNET_break (NULL == sli->accept_task);
1859 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1867 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1868 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1872 GNUNET_STRINGS_relative_time_to_string (pos->backoff,
1875 /* Reduce backoff based on runtime of the process,
1876 so that there is a cool-down if a process actually
1877 runs for a while. */
1878 struct GNUNET_TIME_Relative runtime;
1879 unsigned int minutes;
1881 runtime = GNUNET_TIME_absolute_get_duration (pos->restart_at);
1882 minutes = runtime.rel_value_us / GNUNET_TIME_UNIT_MINUTES.rel_value_us;
1884 pos->backoff = GNUNET_TIME_UNIT_ZERO;
1886 pos->backoff.rel_value_us <<= minutes;
1888 /* schedule restart */
1889 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1890 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1891 if (NULL != child_restart_task)
1892 GNUNET_SCHEDULER_cancel (child_restart_task);
1894 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1895 &delayed_restart_task,
1904 child_death_task = GNUNET_SCHEDULER_add_read_file (
1905 GNUNET_TIME_UNIT_FOREVER_REL,
1907 &maint_child_death, NULL);
1908 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1910 else if (GNUNET_YES == in_shutdown)
1911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1912 "Delaying shutdown after child's death, still have %u children\n",
1913 list_count (running_head));
1919 * Signal handler called for SIGCHLD. Triggers the
1920 * respective handler by writing to the trigger pipe.
1923 sighandler_child_death ()
1926 int old_errno = errno; /* back-up errno */
1929 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
1930 GNUNET_DISK_PIPE_END_WRITE),
1933 errno = old_errno; /* restore errno */
1938 * Setup our service record for the given section in the configuration file
1939 * (assuming the section is for a service).
1942 * @param section a section in the configuration file
1943 * @return #GNUNET_OK (continue)
1946 setup_service (void *cls,
1947 const char *section)
1949 struct ServiceList *sl;
1953 struct sockaddr **addrs;
1954 socklen_t *addr_lens;
1958 if (0 == strcasecmp (section,
1962 GNUNET_CONFIGURATION_get_value_string (cfg,
1967 /* not a service section */
1971 GNUNET_CONFIGURATION_have_value (cfg,
1975 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1979 if (GNUNET_NO == start_user)
1981 GNUNET_free (binary);
1982 return; /* user service, and we don't deal with those */
1987 if (GNUNET_NO == start_system)
1989 GNUNET_free (binary);
1990 return; /* system service, and we don't deal with those */
1993 sl = find_service (section);
1996 /* got the same section twice!? */
1998 GNUNET_free (binary);
2003 GNUNET_CONFIGURATION_get_value_filename (cfg,
2008 GNUNET_CONFIGURATION_get_value_filename (cfg,
2012 (0 != STAT (config, &sbuf)))
2016 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
2019 GNUNET_free (config);
2023 sl = GNUNET_new (struct ServiceList);
2024 sl->name = GNUNET_strdup (section);
2025 sl->binary = binary;
2026 sl->config = config;
2027 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
2028 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
2030 sl->pipe_control = GNUNET_YES;
2032 if (GNUNET_CONFIGURATION_have_value (cfg,
2035 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2039 GNUNET_CONTAINER_DLL_insert (running_head,
2043 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2047 sl->force_start = GNUNET_YES;
2049 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2057 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2062 if (0 >= (ret = get_server_addresses (section,
2067 /* this will free (or capture) addrs[i] */
2068 for (unsigned int i = 0; i < (unsigned int) ret; i++)
2069 create_listen_socket (addrs[i],
2072 GNUNET_free (addrs);
2073 GNUNET_free (addr_lens);
2078 * A client connected, mark as a monitoring client.
2080 * @param cls closure
2081 * @param client identification of the client
2082 * @param mq queue to talk to @a client
2086 client_connect_cb (void *cls,
2087 struct GNUNET_SERVICE_Client *client,
2088 struct GNUNET_MQ_Handle *mq)
2090 /* All clients are considered to be of the "monitor" kind
2091 * (that is, they don't affect ARM shutdown).
2095 GNUNET_SERVICE_client_mark_monitor (client);
2101 * A client disconnected, clean up associated state.
2103 * @param cls closure
2104 * @param client identification of the client
2105 * @param app_ctx must match @a client
2108 client_disconnect_cb (void *cls,
2109 struct GNUNET_SERVICE_Client *client,
2113 GNUNET_assert (client == app_ctx);
2114 for (struct ServiceList *sl = running_head; NULL != sl; sl = sl->next)
2115 if (sl->killing_client == client)
2116 sl->killing_client = NULL;
2121 * Handle MONITOR-message.
2123 * @param cls identification of the client
2124 * @param message the actual message
2125 * @return #GNUNET_OK to keep the connection open,
2126 * #GNUNET_SYSERR to close it (signal serious error)
2129 handle_monitor (void *cls,
2130 const struct GNUNET_MessageHeader *message)
2132 struct GNUNET_SERVICE_Client *client = cls;
2135 /* FIXME: might want to start by letting monitor know about
2136 services that are already running */
2137 /* Removal is handled by the server implementation, internally. */
2138 GNUNET_notification_context_add (notifier,
2139 GNUNET_SERVICE_client_get_mq (client));
2140 broadcast_status ("arm",
2141 GNUNET_ARM_SERVICE_MONITORING_STARTED,
2143 GNUNET_SERVICE_client_continue (client);
2148 * Process arm requests.
2150 * @param cls closure, NULL
2151 * @param serv the initialized service
2152 * @param c configuration to use
2156 const struct GNUNET_CONFIGURATION_Handle *c,
2157 struct GNUNET_SERVICE_Handle *serv)
2159 struct ServiceList *sl;
2164 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
2167 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2168 GNUNET_DISK_pipe_handle (sigpipe,
2169 GNUNET_DISK_PIPE_END_READ),
2174 GNUNET_CONFIGURATION_get_value_filename (cfg,
2176 "RESOURCE_DIAGNOSTICS",
2179 wait_file = fopen (wait_filename,
2181 if (NULL == wait_file)
2183 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2190 GNUNET_CONFIGURATION_get_value_string (cfg,
2194 prefix_command = GNUNET_strdup ("");
2196 prefix_command = GNUNET_CONFIGURATION_expand_dollar (cfg,
2199 GNUNET_CONFIGURATION_get_value_string (cfg,
2203 final_option = GNUNET_strdup ("");
2205 final_option = GNUNET_CONFIGURATION_expand_dollar (cfg,
2207 start_user = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2209 "START_USER_SERVICES");
2210 start_system = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2212 "START_SYSTEM_SERVICES");
2213 if ( (GNUNET_NO == start_user) &&
2214 (GNUNET_NO == start_system) )
2216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2217 "Please configure either START_USER_SERVICES or START_SYSTEM_SERVICES or both.\n");
2218 GNUNET_SCHEDULER_shutdown ();
2222 GNUNET_CONFIGURATION_iterate_sections (cfg,
2226 /* start default services... */
2227 for (sl = running_head; NULL != sl; sl = sl->next)
2228 if (GNUNET_YES == sl->force_start)
2232 notifier = GNUNET_notification_context_create (MAX_NOTIFY_QUEUE);
2237 * The main function for the arm service.
2239 * @param argc number of arguments from the command line
2240 * @param argv command line arguments
2241 * @return 0 ok, 1 on error
2247 struct GNUNET_SIGNAL_Context *shc_chld;
2248 struct GNUNET_MQ_MessageHandler handlers[] = {
2249 GNUNET_MQ_hd_var_size (start,
2250 GNUNET_MESSAGE_TYPE_ARM_START,
2251 struct GNUNET_ARM_Message,
2253 GNUNET_MQ_hd_var_size (stop,
2254 GNUNET_MESSAGE_TYPE_ARM_STOP,
2255 struct GNUNET_ARM_Message,
2257 GNUNET_MQ_hd_fixed_size (monitor,
2258 GNUNET_MESSAGE_TYPE_ARM_MONITOR,
2259 struct GNUNET_MessageHeader,
2261 GNUNET_MQ_hd_fixed_size (list,
2262 GNUNET_MESSAGE_TYPE_ARM_LIST,
2263 struct GNUNET_ARM_Message,
2265 GNUNET_MQ_hd_fixed_size (test,
2266 GNUNET_MESSAGE_TYPE_ARM_TEST,
2267 struct GNUNET_MessageHeader,
2269 GNUNET_MQ_handler_end ()
2272 sigpipe = GNUNET_DISK_pipe (GNUNET_NO,
2276 GNUNET_assert (NULL != sigpipe);
2278 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD,
2279 &sighandler_child_death);
2280 if ( GNUNET_OK != GNUNET_SERVICE_run_ (argc,
2283 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN,
2286 &client_disconnect_cb,
2291 if (NULL != wait_file)
2296 if (NULL != wait_filename)
2298 GNUNET_free (wait_filename);
2299 wait_filename = NULL;
2302 GNUNET_SIGNAL_handler_uninstall (shc_chld);
2304 GNUNET_DISK_pipe_close (sigpipe);
2310 #if defined(LINUX) && defined(__GLIBC__)
2314 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
2316 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
2318 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
2319 mallopt (M_TOP_PAD, 1 * 1024);
2325 /* end of gnunet-service-arm.c */