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) \
35 GNUNET_log_from_strerror (kind, "util", syscall)
40 * Name of the file for writing resource utilization summaries to.
42 static char *wait_filename;
45 * Handle for the file for writing resource summaries.
47 static FILE *wait_file;
52 * How many messages do we queue up at most for optional
53 * notifications to a client? (this can cause notifications
54 * about outgoing messages to be dropped).
56 #define MAX_NOTIFY_QUEUE 1024
60 * List of our services.
66 * Record with information about a listen socket we have open.
68 struct ServiceListeningInfo
71 * This is a linked list.
73 struct ServiceListeningInfo *next;
76 * This is a linked list.
78 struct ServiceListeningInfo *prev;
81 * Address this socket is listening on.
83 struct sockaddr *service_addr;
86 * Service this listen socket is for.
88 struct ServiceList *sl;
91 * Number of bytes in @e service_addr
93 socklen_t service_addr_len;
96 * Our listening socket.
98 struct GNUNET_NETWORK_Handle *listen_socket;
101 * Task doing the accepting.
103 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 was (re-)started last.
171 struct GNUNET_TIME_Absolute last_started_at;
174 * Absolute time at which the process is scheduled to restart in case of death
176 struct GNUNET_TIME_Absolute restart_at;
179 * Time we asked the service to shut down (used to calculate time it took
180 * the service to terminate).
182 struct GNUNET_TIME_Absolute killed_at;
185 * Is this service to be started by default (or did a client tell us explicitly
186 * to start it)? #GNUNET_NO if the service is started only upon 'accept' on a
187 * listen socket or possibly explicitly by a client changing the value.
192 * Should we use pipes to signal this process? (YES for Java binaries and if we
198 * Last exit status of the process.
200 int last_exit_status;
204 * List of running services.
206 static struct ServiceList *running_head;
209 * List of running services.
211 static struct ServiceList *running_tail;
216 static const struct GNUNET_CONFIGURATION_Handle *cfg;
219 * Command to prepend to each actual command.
221 static char *prefix_command;
224 * Option to append to each actual command.
226 static char *final_option;
229 * ID of task called whenever we get a SIGCHILD.
231 static struct GNUNET_SCHEDULER_Task *child_death_task;
234 * ID of task called whenever the timeout for restarting a child
237 static struct GNUNET_SCHEDULER_Task *child_restart_task;
240 * Pipe used to communicate shutdown via signal.
242 static struct GNUNET_DISK_PipeHandle *sigpipe;
245 * Are we in shutdown mode?
247 static int in_shutdown;
250 * Return value from main
252 static int global_ret;
255 * Are we starting user services?
257 static int start_user = GNUNET_YES;
260 * Are we starting system services?
262 static int start_system = GNUNET_YES;
265 * Handle to our service instance. Our service is a bit special in that
266 * its service is not immediately stopped once we get a shutdown
267 * request (since we need to continue service until all of our child
268 * processes are dead). This handle is used to shut down the service
269 * (and thus trigger process termination) once all child processes are
270 * also dead. A special option in the ARM configuration modifies the
271 * behaviour of the service implementation to not do the shutdown
274 static struct GNUNET_SERVICE_Handle *service;
277 * Context for notifications we need to send to our clients.
279 static struct GNUNET_NotificationContext *notifier;
283 * Add the given UNIX domain path as an address to the
284 * list (as the first entry).
286 * @param saddrs array to update
287 * @param saddrlens where to store the address length
288 * @param unixpath path to add
289 * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
290 * parameter is ignore on systems other than LINUX
293 add_unixpath (struct sockaddr **saddrs,
294 socklen_t *saddrlens,
295 const char *unixpath,
299 struct sockaddr_un *un;
301 un = GNUNET_new (struct sockaddr_un);
302 un->sun_family = AF_UNIX;
303 GNUNET_strlcpy (un->sun_path, unixpath, sizeof(un->sun_path));
305 if (GNUNET_YES == abstract)
306 un->sun_path[0] = '\0';
308 #if HAVE_SOCKADDR_UN_SUN_LEN
309 un->sun_len = (u_char) sizeof(struct sockaddr_un);
311 *saddrs = (struct sockaddr *) un;
312 *saddrlens = sizeof(struct sockaddr_un);
314 /* this function should never be called
315 * unless AF_UNIX is defined! */
322 * Get the list of addresses that a server for the given service
325 * @param service_name name of the service
326 * @param cfg configuration (which specifies the addresses)
327 * @param addrs set (call by reference) to an array of pointers to the
328 * addresses the server should bind to and listen on; the
329 * array will be NULL-terminated (on success)
330 * @param addr_lens set (call by reference) to an array of the lengths
331 * of the respective `struct sockaddr` struct in the @a addrs
333 * @return number of addresses found on success,
334 * #GNUNET_SYSERR if the configuration
335 * did not specify reasonable finding information or
336 * if it specified a hostname that could not be resolved;
337 * #GNUNET_NO if the number of addresses configured is
338 * zero (in this case, `*addrs` and `*addr_lens` will be
342 get_server_addresses (const char *service_name,
343 const struct GNUNET_CONFIGURATION_Handle *cfg,
344 struct sockaddr ***addrs,
345 socklen_t **addr_lens)
348 struct GNUNET_NETWORK_Handle *desc;
349 unsigned long long port;
351 struct addrinfo hints;
352 struct addrinfo *res;
353 struct addrinfo *pos;
354 struct addrinfo *next;
359 struct sockaddr **saddrs;
360 socklen_t *saddrlens;
366 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
369 (disablev6 = GNUNET_CONFIGURATION_get_value_yesno (cfg,
372 return GNUNET_SYSERR;
375 disablev6 = GNUNET_NO;
379 /* probe IPv6 support */
380 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
383 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
386 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
387 return GNUNET_SYSERR;
389 LOG (GNUNET_ERROR_TYPE_INFO,
391 "Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
394 disablev6 = GNUNET_YES;
398 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
404 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
406 if (GNUNET_OK != 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, service_name, "BINDTO"))
426 GNUNET_break (GNUNET_OK ==
427 GNUNET_CONFIGURATION_get_value_string (cfg,
436 abstract = GNUNET_NO;
439 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
440 (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg,
444 (0 < strlen (unixpath)))
446 /* probe UNIX support */
447 struct sockaddr_un s_un;
449 if (strlen (unixpath) >= sizeof(s_un.sun_path))
451 LOG (GNUNET_ERROR_TYPE_WARNING,
452 _ ("UNIXPATH `%s' too long, maximum length is %llu\n"),
454 (unsigned long long) sizeof(s_un.sun_path));
455 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
456 LOG (GNUNET_ERROR_TYPE_INFO, _ ("Using `%s' instead\n"), unixpath);
459 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
461 "USE_ABSTRACT_SOCKETS");
462 if (GNUNET_SYSERR == abstract)
463 abstract = GNUNET_NO;
465 if ((GNUNET_YES != abstract) &&
466 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (unixpath)))
467 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkdir", unixpath);
469 if (NULL != unixpath)
471 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
474 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
477 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
478 GNUNET_free_non_null (hostname);
479 GNUNET_free (unixpath);
480 return GNUNET_SYSERR;
482 LOG (GNUNET_ERROR_TYPE_INFO,
484 "Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
487 GNUNET_free (unixpath);
492 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
498 if ((0 == port) && (NULL == unixpath))
500 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
503 LOG (GNUNET_ERROR_TYPE_ERROR,
505 "Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
507 GNUNET_free_non_null (hostname);
508 return GNUNET_SYSERR;
512 saddrs = GNUNET_new_array (2, struct sockaddr *);
513 saddrlens = GNUNET_new_array (2, socklen_t);
514 add_unixpath (saddrs, saddrlens, unixpath, abstract);
515 GNUNET_free_non_null (unixpath);
516 GNUNET_free_non_null (hostname);
518 *addr_lens = saddrlens;
522 if (NULL != hostname)
524 LOG (GNUNET_ERROR_TYPE_DEBUG,
525 "Resolving `%s' since that is where `%s' will bind to.\n",
528 memset (&hints, 0, sizeof(struct addrinfo));
530 hints.ai_family = AF_INET;
531 hints.ai_protocol = IPPROTO_TCP;
532 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
535 LOG (GNUNET_ERROR_TYPE_ERROR,
536 _ ("Failed to resolve `%s': %s\n"),
539 GNUNET_free (hostname);
540 GNUNET_free_non_null (unixpath);
541 return GNUNET_SYSERR;
545 while (NULL != (pos = next))
548 if ((disablev6) && (pos->ai_family == AF_INET6))
554 LOG (GNUNET_ERROR_TYPE_ERROR,
555 _ ("Failed to find %saddress for `%s'.\n"),
556 disablev6 ? "IPv4 " : "",
559 GNUNET_free (hostname);
560 GNUNET_free_non_null (unixpath);
561 return GNUNET_SYSERR;
564 if (NULL != unixpath)
566 saddrs = GNUNET_new_array (resi + 1, struct sockaddr *);
567 saddrlens = GNUNET_new_array (resi + 1, socklen_t);
569 if (NULL != unixpath)
571 add_unixpath (saddrs, saddrlens, unixpath, abstract);
575 while (NULL != (pos = next))
578 if ((disablev6) && (AF_INET6 == pos->ai_family))
580 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
581 continue; /* not TCP */
582 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
584 LOG (GNUNET_ERROR_TYPE_DEBUG,
585 "Service `%s' will bind to `%s'\n",
587 GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
588 if (AF_INET == pos->ai_family)
590 GNUNET_assert (sizeof(struct sockaddr_in) == pos->ai_addrlen);
591 saddrlens[i] = pos->ai_addrlen;
592 saddrs[i] = GNUNET_malloc (saddrlens[i]);
593 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
594 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
598 GNUNET_assert (AF_INET6 == pos->ai_family);
599 GNUNET_assert (sizeof(struct sockaddr_in6) == pos->ai_addrlen);
600 saddrlens[i] = pos->ai_addrlen;
601 saddrs[i] = GNUNET_malloc (saddrlens[i]);
602 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
603 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
607 GNUNET_free (hostname);
613 /* will bind against everything, just set port */
618 if (NULL != unixpath)
621 saddrs = GNUNET_new_array (resi + 1, struct sockaddr *);
622 saddrlens = GNUNET_new_array (resi + 1, socklen_t);
623 if (NULL != unixpath)
625 add_unixpath (saddrs, saddrlens, unixpath, abstract);
628 saddrlens[i] = sizeof(struct sockaddr_in);
629 saddrs[i] = GNUNET_malloc (saddrlens[i]);
630 #if HAVE_SOCKADDR_IN_SIN_LEN
631 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
633 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
634 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
640 if (NULL != unixpath)
642 saddrs = GNUNET_new_array (resi + 1, struct sockaddr *);
643 saddrlens = GNUNET_new_array (resi + 1, socklen_t);
645 if (NULL != unixpath)
647 add_unixpath (saddrs, saddrlens, unixpath, abstract);
650 saddrlens[i] = sizeof(struct sockaddr_in6);
651 saddrs[i] = GNUNET_malloc (saddrlens[i]);
652 #if HAVE_SOCKADDR_IN_SIN_LEN
653 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
655 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
656 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
658 saddrlens[i] = sizeof(struct sockaddr_in);
659 saddrs[i] = GNUNET_malloc (saddrlens[i]);
660 #if HAVE_SOCKADDR_IN_SIN_LEN
661 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
663 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
664 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
667 GNUNET_free_non_null (unixpath);
669 *addr_lens = saddrlens;
675 * Signal our client that we will start or stop the
678 * @param client who is being signalled
679 * @param name name of the service
680 * @param request_id id of the request that is being responded to.
681 * @param result message type to send
682 * @return NULL if it was not found
685 signal_result (struct GNUNET_SERVICE_Client *client,
688 enum GNUNET_ARM_Result result)
690 struct GNUNET_MQ_Envelope *env;
691 struct GNUNET_ARM_ResultMessage *msg;
694 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_ARM_RESULT);
695 msg->result = htonl (result);
696 msg->arm_msg.request_id = GNUNET_htonll (request_id);
697 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
702 * Tell all clients about status change of a service.
704 * @param name name of the service
705 * @param status message type to send
706 * @param unicast if not NULL, send to this client only.
707 * otherwise, send to all clients in the notifier
710 broadcast_status (const char *name,
711 enum GNUNET_ARM_ServiceMonitorStatus status,
712 struct GNUNET_SERVICE_Client *unicast)
714 struct GNUNET_MQ_Envelope *env;
715 struct GNUNET_ARM_StatusMessage *msg;
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "Sending status %u of service `%s' to client\n",
720 (unsigned int) status,
722 namelen = strlen (name) + 1;
723 env = GNUNET_MQ_msg_extra (msg, namelen, GNUNET_MESSAGE_TYPE_ARM_STATUS);
724 msg->status = htonl ((uint32_t) (status));
725 GNUNET_memcpy ((char *) &msg[1], name, namelen);
728 if (NULL != notifier)
729 GNUNET_notification_context_broadcast (notifier,
732 GNUNET_MQ_discard (env);
736 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (unicast), env);
742 * Actually start the process for the given service.
744 * @param sl identifies service to start
745 * @param client that asked to start the service (may be NULL)
746 * @param request_id id of the request in response to which the process is
747 * being started. 0 if starting was not requested.
750 start_process (struct ServiceList *sl,
751 struct GNUNET_SERVICE_Client *client,
757 int is_simple_service;
758 struct ServiceListeningInfo *sli;
764 /* calculate listen socket list */
767 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
769 GNUNET_array_append (lsocks,
771 GNUNET_NETWORK_get_fd (sli->listen_socket));
772 if (NULL != sli->accept_task)
774 GNUNET_SCHEDULER_cancel (sli->accept_task);
775 sli->accept_task = NULL;
779 GNUNET_array_append (lsocks, ls, -1);
781 /* obtain configuration */
782 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
786 loprefix = GNUNET_strdup (prefix_command);
788 loprefix = GNUNET_CONFIGURATION_expand_dollar (cfg, loprefix);
789 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
795 options = GNUNET_CONFIGURATION_expand_dollar (cfg, options);
801 fin_options = GNUNET_strdup (final_option);
802 /* replace '{}' with service name */
803 while (NULL != (optpos = strstr (fin_options, "{}")))
805 /* terminate string at opening parenthesis */
807 GNUNET_asprintf (&new_options,
812 GNUNET_free (fin_options);
813 fin_options = new_options;
817 /* combine "fin_options" with "options" */
819 GNUNET_asprintf (&options, "%s %s", fin_options, optpos);
820 GNUNET_free (fin_options);
821 GNUNET_free (optpos);
825 /* only have "fin_options", use that */
826 options = fin_options;
829 options = GNUNET_CONFIGURATION_expand_dollar (cfg, options);
830 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
832 const char *service_type = NULL;
833 const char *choices[] = { "GNUNET", "SIMPLE", NULL };
835 is_simple_service = GNUNET_NO;
836 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_choice (cfg,
841 (0 == strcasecmp (service_type, "SIMPLE")))
842 is_simple_service = GNUNET_YES;
845 GNUNET_assert (NULL == sl->proc);
846 if (GNUNET_YES == is_simple_service)
848 /* A simple service will receive no GNUnet specific
849 command line options. */
850 binary = GNUNET_strdup (sl->binary);
851 binary = GNUNET_CONFIGURATION_expand_dollar (cfg, binary);
852 GNUNET_asprintf ("edbinary, "\"%s\"", sl->binary);
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "Starting simple service `%s' using binary `%s'\n",
857 /* FIXME: dollar expansion should only be done outside
858 * of ''-quoted strings, escaping should be considered. */
860 options = GNUNET_CONFIGURATION_expand_dollar (cfg, options);
861 sl->proc = GNUNET_OS_start_process_s (sl->pipe_control,
862 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
871 /* actually start process */
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "Starting service `%s' using binary `%s' and configuration `%s'\n",
877 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
878 GNUNET_asprintf ("edbinary, "\"%s\"", binary);
880 if (GNUNET_YES == use_debug)
882 if (NULL == sl->config)
883 sl->proc = GNUNET_OS_start_process_s (sl->pipe_control,
884 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
893 sl->proc = GNUNET_OS_start_process_s (sl->pipe_control,
894 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
907 if (NULL == sl->config)
908 sl->proc = GNUNET_OS_start_process_s (sl->pipe_control,
909 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
916 sl->proc = GNUNET_OS_start_process_s (sl->pipe_control,
917 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
927 GNUNET_free (binary);
928 GNUNET_free (quotedbinary);
929 sl->last_started_at = GNUNET_TIME_absolute_get ();
930 if (NULL == sl->proc)
932 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
933 _ ("Failed to start service `%s'\n"),
936 signal_result (client,
939 GNUNET_ARM_RESULT_START_FAILED);
943 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
944 _ ("Starting service `%s'\n"),
946 broadcast_status (sl->name, GNUNET_ARM_SERVICE_STARTING, NULL);
948 signal_result (client, sl->name, request_id, GNUNET_ARM_RESULT_STARTING);
951 GNUNET_free (loprefix);
952 GNUNET_free (options);
953 GNUNET_array_grow (lsocks, ls, 0);
958 * Find the process with the given service
959 * name in the given list and return it.
961 * @param name which service entry to look up
962 * @return NULL if it was not found
964 static struct ServiceList *
965 find_service (const char *name)
967 struct ServiceList *sl;
972 if (0 == strcasecmp (sl->name, name))
981 * First connection has come to the listening socket associated with the service,
982 * create the service in order to relay the incoming connection to it
984 * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
987 accept_connection (void *cls)
989 struct ServiceListeningInfo *sli = cls;
990 struct ServiceList *sl = sli->sl;
992 sli->accept_task = NULL;
993 GNUNET_assert (GNUNET_NO == in_shutdown);
994 start_process (sl, NULL, 0);
999 * Creating a listening socket for each of the service's addresses and
1000 * wait for the first incoming connection to it
1002 * @param sa address associated with the service
1003 * @param addr_len length of @a sa
1004 * @param sl service entry for the service in question
1007 create_listen_socket (struct sockaddr *sa,
1009 struct ServiceList *sl)
1012 struct GNUNET_NETWORK_Handle *sock;
1013 struct ServiceListeningInfo *sli;
1018 switch (sa->sa_family)
1021 sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
1025 sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
1029 if (0 == strcmp (GNUNET_a2s (sa, addr_len),
1030 "@")) /* Do not bind to blank UNIX path! */
1032 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
1038 errno = EAFNOSUPPORT;
1043 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1044 _ ("Unable to create socket for service `%s': %s\n"),
1050 if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (sock,
1055 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1058 if ((sa->sa_family == AF_INET6) &&
1059 (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (sock,
1064 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1067 if (AF_UNIX == sa->sa_family)
1068 GNUNET_NETWORK_unix_precheck ((struct sockaddr_un *) sa);
1070 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) sa, addr_len))
1073 GNUNET_ERROR_TYPE_WARNING,
1075 "Unable to bind listening socket for service `%s' to address `%s': %s\n"),
1077 GNUNET_a2s (sa, addr_len),
1079 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
1083 if ((AF_UNIX == sa->sa_family)
1085 /* Permission settings are not required when abstract sockets are used */
1086 && ('\0' != ((const struct sockaddr_un *) sa)->sun_path[0])
1091 GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "UNIX_MATCH_UID");
1093 GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "UNIX_MATCH_GID");
1094 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *) sa)->sun_path,
1098 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
1100 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
1101 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
1105 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1106 _ ("ARM now monitors connections to service `%s' at `%s'\n"),
1108 GNUNET_a2s (sa, addr_len));
1109 sli = GNUNET_new (struct ServiceListeningInfo);
1110 sli->service_addr = sa;
1111 sli->service_addr_len = addr_len;
1112 sli->listen_socket = sock;
1115 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1119 GNUNET_CONTAINER_DLL_insert (sl->listen_head, sl->listen_tail, sli);
1124 * Remove and free an entry in the service list. Listen sockets
1125 * must have already been cleaned up. Only to be called during shutdown.
1127 * @param sl entry to free
1130 free_service (struct ServiceList *sl)
1132 GNUNET_assert (GNUNET_YES == in_shutdown);
1133 GNUNET_CONTAINER_DLL_remove (running_head, running_tail, sl);
1134 GNUNET_assert (NULL == sl->listen_head);
1135 GNUNET_free_non_null (sl->config);
1136 GNUNET_free_non_null (sl->binary);
1137 GNUNET_free (sl->name);
1143 * Check START-message.
1145 * @param cls identification of the client
1146 * @param amsg the actual message
1147 * @return #GNUNET_OK to keep the connection open,
1148 * #GNUNET_SYSERR to close it (signal serious error)
1151 check_start (void *cls, const struct GNUNET_ARM_Message *amsg)
1154 GNUNET_MQ_check_zero_termination (amsg);
1160 * Handle START-message.
1162 * @param cls identification of the client
1163 * @param amsg the actual message
1166 handle_start (void *cls, const struct GNUNET_ARM_Message *amsg)
1168 struct GNUNET_SERVICE_Client *client = cls;
1169 const char *servicename;
1170 struct ServiceList *sl;
1171 uint64_t request_id;
1173 request_id = GNUNET_ntohll (amsg->request_id);
1174 servicename = (const char *) &amsg[1];
1175 GNUNET_SERVICE_client_continue (client);
1176 if (GNUNET_YES == in_shutdown)
1178 signal_result (client,
1181 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1184 sl = find_service (servicename);
1187 signal_result (client,
1190 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1193 sl->force_start = GNUNET_YES;
1194 if (NULL != sl->proc)
1196 signal_result (client,
1199 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
1202 start_process (sl, client, request_id);
1207 * Start a shutdown sequence.
1209 * @param cls closure (refers to service)
1212 trigger_shutdown (void *cls)
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Triggering shutdown\n");
1216 GNUNET_SCHEDULER_shutdown ();
1221 * Check STOP-message.
1223 * @param cls identification of the client
1224 * @param amsg the actual message
1225 * @return #GNUNET_OK to keep the connection open,
1226 * #GNUNET_SYSERR to close it (signal serious error)
1229 check_stop (void *cls, const struct GNUNET_ARM_Message *amsg)
1232 GNUNET_MQ_check_zero_termination (amsg);
1238 * Handle STOP-message.
1240 * @param cls identification of the client
1241 * @param amsg the actual message
1244 handle_stop (void *cls, const struct GNUNET_ARM_Message *amsg)
1246 struct GNUNET_SERVICE_Client *client = cls;
1247 struct ServiceList *sl;
1248 const char *servicename;
1249 uint64_t request_id;
1251 request_id = GNUNET_ntohll (amsg->request_id);
1252 servicename = (const char *) &amsg[1];
1253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1254 _ ("Preparing to stop `%s'\n"),
1256 GNUNET_SERVICE_client_continue (client);
1257 if (0 == strcasecmp (servicename, "arm"))
1259 broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
1260 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_STOPPING);
1261 GNUNET_SERVICE_client_persist (client);
1262 GNUNET_SCHEDULER_add_now (&trigger_shutdown, NULL);
1265 sl = find_service (servicename);
1268 signal_result (client,
1271 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1274 sl->force_start = GNUNET_NO;
1275 if (GNUNET_YES == in_shutdown)
1277 /* shutdown in progress */
1278 signal_result (client,
1281 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1284 if (NULL != sl->killing_client)
1286 /* killing already in progress */
1287 signal_result (client,
1290 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
1293 if (NULL == sl->proc)
1295 /* process is down */
1296 signal_result (client,
1299 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 "Sending kill signal to service `%s', waiting for process to die.\n",
1305 broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
1306 /* no signal_start - only when it's STOPPED */
1307 sl->killed_at = GNUNET_TIME_absolute_get ();
1308 if (0 != GNUNET_OS_process_kill (sl->proc, GNUNET_TERM_SIG))
1309 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1310 sl->killing_client = client;
1311 sl->killing_client_request_id = request_id;
1316 * Write a string to a string pool.
1318 * @param pool_start pointer to the start of the string pool
1319 * @param pool_size size of the string pool
1320 * @param[in,out] pool_pos current position index in the string pool,
1322 * @param str string to write to the string pool
1323 * @returns GNUNET_OK if the string fits into the pool,
1324 * GNUNET_SYSERR otherwise
1327 pool_write (char *pool_start, size_t pool_size, size_t *pool_pos, char *str)
1329 size_t next_pos = (*pool_pos) + strlen (str) + 1;
1331 if (next_pos > pool_size)
1332 return GNUNET_SYSERR;
1333 memcpy (pool_start + *pool_pos, str, strlen (str) + 1);
1334 *pool_pos = next_pos;
1340 * Handle LIST-message.
1342 * @param cls identification of the client
1343 * @param message the actual message
1346 handle_list (void *cls, const struct GNUNET_ARM_Message *request)
1348 struct GNUNET_SERVICE_Client *client = cls;
1349 struct GNUNET_MQ_Envelope *env;
1350 struct GNUNET_ARM_ListResultMessage *msg;
1352 struct ServiceList *sl;
1357 struct GNUNET_ARM_ServiceInfoMessage *ssm;
1359 GNUNET_break_op (0 == ntohl (request->reserved));
1363 /* Do one pass over the list to compute the number of services
1364 * and the string pool size */
1365 for (sl = running_head; NULL != sl; sl = sl->next)
1367 pool_size += strlen (sl->name) + 1;
1368 pool_size += strlen (sl->binary) + 1;
1372 extra_size = pool_size + (count * sizeof (struct
1373 GNUNET_ARM_ServiceInfoMessage));
1374 env = GNUNET_MQ_msg_extra (msg,
1376 GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT);
1377 msg->arm_msg.request_id = request->request_id;
1378 msg->count = htons (count);
1380 ssm = (struct GNUNET_ARM_ServiceInfoMessage *) &msg[1];
1381 pool_start = (char *) (ssm + count);
1384 for (sl = running_head; NULL != sl; sl = sl->next)
1386 ssm->name_index = htons ((uint16_t) pool_pos);
1387 GNUNET_assert (GNUNET_OK == pool_write (pool_start, pool_size, &pool_pos,
1389 ssm->binary_index = htons ((uint16_t) pool_pos);
1390 GNUNET_assert (GNUNET_OK == pool_write (pool_start, pool_size, &pool_pos,
1392 if (NULL == sl->proc)
1394 if (0 == sl->last_started_at.abs_value_us)
1396 /* Process never started */
1397 ssm->status = htonl (GNUNET_ARM_SERVICE_STATUS_STOPPED);
1399 else if (0 == sl->last_exit_status)
1401 ssm->status = htonl (GNUNET_ARM_SERVICE_STATUS_FINISHED);
1405 ssm->status = htonl (GNUNET_ARM_SERVICE_STATUS_FAILED);
1406 ssm->last_exit_status = htons (sl->last_exit_status);
1409 else if ((NULL != sl->killing_client) || (GNUNET_YES == in_shutdown))
1411 ssm->status = htonl (GNUNET_ARM_SERVICE_STATUS_STOPPING);
1415 ssm->status = htonl (GNUNET_ARM_SERVICE_STATUS_STARTED);
1417 ssm->last_started_at = GNUNET_TIME_absolute_hton (sl->last_started_at);
1418 ssm->restart_at = GNUNET_TIME_absolute_hton (sl->restart_at);
1421 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
1422 GNUNET_SERVICE_client_continue (client);
1427 * Handle TEST-message by sending back TEST.
1429 * @param cls identification of the client
1430 * @param message the actual message
1433 handle_test (void *cls, const struct GNUNET_MessageHeader *message)
1435 struct GNUNET_SERVICE_Client *client = cls;
1436 struct GNUNET_MQ_Envelope *env;
1437 struct GNUNET_MessageHeader *msg;
1440 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_ARM_TEST);
1441 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
1442 GNUNET_SERVICE_client_continue (client);
1447 * We are done with everything. Stop remaining
1448 * tasks, signal handler and the server.
1453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last shutdown phase\n");
1454 if (NULL != notifier)
1456 GNUNET_notification_context_destroy (notifier);
1459 if (NULL != service)
1461 GNUNET_SERVICE_shutdown (service);
1464 if (NULL != child_death_task)
1466 GNUNET_SCHEDULER_cancel (child_death_task);
1467 child_death_task = NULL;
1473 * Count how many services are still active.
1475 * @param running_head list of services
1476 * @return number of active services found
1479 list_count (struct ServiceList *running_head)
1481 struct ServiceList *i;
1484 for (res = 0, i = running_head; NULL != i; i = i->next, res++)
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s\n", i->name);
1491 * Task run for shutdown.
1493 * @param cls closure, NULL if we need to self-restart
1496 shutdown_task (void *cls)
1498 struct ServiceList *pos;
1499 struct ServiceList *nxt;
1500 struct ServiceListeningInfo *sli;
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "First shutdown phase\n");
1504 if (NULL != child_restart_task)
1506 GNUNET_SCHEDULER_cancel (child_restart_task);
1507 child_restart_task = NULL;
1509 in_shutdown = GNUNET_YES;
1510 /* first, stop listening */
1511 for (pos = running_head; NULL != pos; pos = pos->next)
1513 while (NULL != (sli = pos->listen_head))
1515 GNUNET_CONTAINER_DLL_remove (pos->listen_head, pos->listen_tail, sli);
1516 if (NULL != sli->accept_task)
1518 GNUNET_SCHEDULER_cancel (sli->accept_task);
1519 sli->accept_task = NULL;
1521 GNUNET_break (GNUNET_OK ==
1522 GNUNET_NETWORK_socket_close (sli->listen_socket));
1523 GNUNET_free (sli->service_addr);
1527 /* then, shutdown all existing service processes */
1529 while (NULL != (pos = nxt))
1532 if (NULL != pos->proc)
1534 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping service `%s'\n", pos->name);
1535 pos->killed_at = GNUNET_TIME_absolute_get ();
1536 if (0 != GNUNET_OS_process_kill (pos->proc, GNUNET_TERM_SIG))
1537 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1544 /* finally, should all service processes be already gone, terminate for real */
1545 if (NULL == running_head)
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1549 "Delaying shutdown, have %u childs still running\n",
1550 list_count (running_head));
1555 * Task run whenever it is time to restart a child that died.
1557 * @param cls closure, always NULL
1560 delayed_restart_task (void *cls)
1563 struct ServiceList *sl;
1564 struct GNUNET_TIME_Relative lowestRestartDelay;
1565 struct ServiceListeningInfo *sli;
1568 child_restart_task = NULL;
1569 GNUNET_assert (GNUNET_NO == in_shutdown);
1570 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1572 /* check for services that need to be restarted due to
1573 * configuration changes or because the last restart failed */
1574 for (sl = running_head; NULL != sl; sl = sl->next)
1576 if (NULL != sl->proc)
1578 /* service is currently not running */
1579 if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
1581 /* restart is now allowed */
1582 if (sl->force_start)
1584 /* process should run by default, start immediately */
1585 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1586 _ ("Restarting service `%s'.\n"),
1588 start_process (sl, NULL, 0);
1592 /* process is run on-demand, ensure it is re-started if there is demand */
1593 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1594 if (NULL == sli->accept_task)
1596 /* accept was actually paused, so start it again */
1598 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1607 /* update calculation for earliest time to reactivate a service */
1608 lowestRestartDelay =
1609 GNUNET_TIME_relative_min (lowestRestartDelay,
1610 GNUNET_TIME_absolute_get_remaining (
1614 if (lowestRestartDelay.rel_value_us !=
1615 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1618 "Will restart process in %s\n",
1619 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay,
1621 child_restart_task =
1622 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1623 GNUNET_SCHEDULER_PRIORITY_IDLE,
1624 &delayed_restart_task,
1631 * Task triggered whenever we receive a SIGCHLD (child
1634 * @param cls closure, NULL
1637 maint_child_death (void *cls)
1639 struct ServiceList *pos;
1640 struct ServiceList *next;
1641 struct ServiceListeningInfo *sli;
1642 const char *statstr;
1646 enum GNUNET_OS_ProcessStatusType statusType;
1647 unsigned long statusCode;
1648 const struct GNUNET_DISK_FileHandle *pr;
1651 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
1652 child_death_task = NULL;
1653 /* consume the signal */
1654 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof(c)));
1656 /* check for services that died (WAITPID) */
1657 next = running_head;
1658 while (NULL != (pos = next))
1662 if (NULL == pos->proc)
1664 if (GNUNET_YES == in_shutdown)
1669 if (NULL != wait_file)
1671 /* need to use 'wait4()' to obtain and log performance data */
1676 pid = GNUNET_OS_process_get_pid (pos->proc);
1677 ret = wait4 (pid, &status, WNOHANG, &ru);
1679 continue; /* no process done */
1680 if (WIFEXITED (status))
1682 statusType = GNUNET_OS_PROCESS_EXITED;
1683 statusCode = WEXITSTATUS (status);
1685 else if (WIFSIGNALED (status))
1687 statusType = GNUNET_OS_PROCESS_SIGNALED;
1688 statusCode = WTERMSIG (status);
1690 else if (WIFSTOPPED (status))
1692 statusType = GNUNET_OS_PROCESS_SIGNALED;
1693 statusCode = WSTOPSIG (status);
1696 else if (WIFCONTINUED (status))
1698 statusType = GNUNET_OS_PROCESS_RUNNING;
1704 statusType = GNUNET_OS_PROCESS_UNKNOWN;
1707 if ((GNUNET_OS_PROCESS_EXITED == statusType) ||
1708 (GNUNET_OS_PROCESS_SIGNALED == statusType))
1710 double utime = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 10e6);
1711 double stime = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 10e6);
1713 "%s(%u) %.3f %.3f %llu %llu %llu %llu %llu\n",
1718 (unsigned long long) ru.ru_maxrss,
1719 (unsigned long long) ru.ru_inblock,
1720 (unsigned long long) ru.ru_oublock,
1721 (unsigned long long) ru.ru_nvcsw,
1722 (unsigned long long) ru.ru_nivcsw);
1725 else /* continue with JUST this "if" as "else" (intentionally no brackets!) */
1727 if ((GNUNET_SYSERR == (ret = GNUNET_OS_process_status (pos->proc,
1730 (ret == GNUNET_NO) || (statusType == GNUNET_OS_PROCESS_STOPPED) ||
1731 (statusType == GNUNET_OS_PROCESS_UNKNOWN) ||
1732 (statusType == GNUNET_OS_PROCESS_RUNNING))
1735 if (statusType == GNUNET_OS_PROCESS_EXITED)
1737 statstr = _ (/* process termination method */ "exit");
1738 statcode = statusCode;
1740 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1742 statstr = _ (/* process termination method */ "signal");
1743 statcode = statusCode;
1747 statstr = _ (/* process termination method */ "unknown");
1750 if (0 != pos->killed_at.abs_value_us)
1752 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1753 _ ("Service `%s' took %s to terminate\n"),
1755 GNUNET_STRINGS_relative_time_to_string (
1756 GNUNET_TIME_absolute_get_duration (pos->killed_at),
1759 GNUNET_OS_process_destroy (pos->proc);
1761 broadcast_status (pos->name, GNUNET_ARM_SERVICE_STOPPED, NULL);
1762 if (NULL != pos->killing_client)
1764 signal_result (pos->killing_client,
1766 pos->killing_client_request_id,
1767 GNUNET_ARM_RESULT_STOPPED);
1768 pos->killing_client = NULL;
1769 pos->killing_client_request_id = 0;
1771 if (GNUNET_YES != in_shutdown)
1773 pos->last_exit_status = statcode;
1774 if ((statusType == GNUNET_OS_PROCESS_EXITED) && (statcode == 0))
1776 /* process terminated normally, allow restart at any time */
1777 pos->restart_at.abs_value_us = 0;
1779 GNUNET_ERROR_TYPE_INFO,
1780 _ ("Service `%s' terminated normally, will restart at any time\n"),
1782 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1783 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1785 GNUNET_break (NULL == sli->accept_task);
1787 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1796 GNUNET_ERROR_TYPE_WARNING,
1797 _ ("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1801 GNUNET_STRINGS_relative_time_to_string (pos->backoff, GNUNET_YES));
1803 /* Reduce backoff based on runtime of the process,
1804 so that there is a cool-down if a process actually
1805 runs for a while. */
1806 struct GNUNET_TIME_Relative runtime;
1807 unsigned int minutes;
1809 runtime = GNUNET_TIME_absolute_get_duration (pos->restart_at);
1811 runtime.rel_value_us / GNUNET_TIME_UNIT_MINUTES.rel_value_us;
1813 pos->backoff = GNUNET_TIME_UNIT_ZERO;
1815 pos->backoff.rel_value_us <<= minutes;
1817 /* schedule restart */
1818 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1819 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1820 if (NULL != child_restart_task)
1821 GNUNET_SCHEDULER_cancel (child_restart_task);
1822 child_restart_task =
1823 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1824 &delayed_restart_task,
1834 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1838 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1840 else if (GNUNET_YES == in_shutdown)
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1842 "Delaying shutdown after child's death, still have %u children\n",
1843 list_count (running_head));
1848 * Signal handler called for SIGCHLD. Triggers the
1849 * respective handler by writing to the trigger pipe.
1852 sighandler_child_death ()
1855 int old_errno = errno; /* back-up errno */
1859 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
1860 GNUNET_DISK_PIPE_END_WRITE),
1863 errno = old_errno; /* restore errno */
1868 * Setup our service record for the given section in the configuration file
1869 * (assuming the section is for a service).
1872 * @param section a section in the configuration file
1873 * @return #GNUNET_OK (continue)
1876 setup_service (void *cls, const char *section)
1878 struct ServiceList *sl;
1882 struct sockaddr **addrs;
1883 socklen_t *addr_lens;
1887 if (0 == strcasecmp (section, "arm"))
1890 GNUNET_CONFIGURATION_get_value_string (cfg, section, "BINARY", &binary))
1892 /* not a service section */
1896 GNUNET_CONFIGURATION_have_value (cfg, section, "RUN_PER_USER")) &&
1898 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "RUN_PER_USER")))
1900 if (GNUNET_NO == start_user)
1902 GNUNET_free (binary);
1903 return; /* user service, and we don't deal with those */
1908 if (GNUNET_NO == start_system)
1910 GNUNET_free (binary);
1911 return; /* system service, and we don't deal with those */
1914 sl = find_service (section);
1917 /* got the same section twice!? */
1919 GNUNET_free (binary);
1923 if (((GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
1927 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
1931 (0 != stat (config, &sbuf)))
1935 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1939 GNUNET_free (config);
1943 sl = GNUNET_new (struct ServiceList);
1944 sl->name = GNUNET_strdup (section);
1945 sl->binary = binary;
1946 sl->config = config;
1947 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1948 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1949 if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
1951 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
1952 GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
1954 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "IMMEDIATE_START"))
1956 sl->force_start = GNUNET_YES;
1958 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "NOARMBIND"))
1964 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "START_ON_DEMAND"))
1967 if (0 >= (ret = get_server_addresses (section, cfg, &addrs, &addr_lens)))
1969 /* this will free (or capture) addrs[i] */
1970 for (unsigned int i = 0; i < (unsigned int) ret; i++)
1971 create_listen_socket (addrs[i], addr_lens[i], sl);
1972 GNUNET_free (addrs);
1973 GNUNET_free (addr_lens);
1978 * A client connected, mark as a monitoring client.
1980 * @param cls closure
1981 * @param client identification of the client
1982 * @param mq queue to talk to @a client
1986 client_connect_cb (void *cls,
1987 struct GNUNET_SERVICE_Client *client,
1988 struct GNUNET_MQ_Handle *mq)
1990 /* All clients are considered to be of the "monitor" kind
1991 * (that is, they don't affect ARM shutdown).
1995 GNUNET_SERVICE_client_mark_monitor (client);
2001 * A client disconnected, clean up associated state.
2003 * @param cls closure
2004 * @param client identification of the client
2005 * @param app_ctx must match @a client
2008 client_disconnect_cb (void *cls,
2009 struct GNUNET_SERVICE_Client *client,
2013 GNUNET_assert (client == app_ctx);
2014 for (struct ServiceList *sl = running_head; NULL != sl; sl = sl->next)
2015 if (sl->killing_client == client)
2016 sl->killing_client = NULL;
2021 * Handle MONITOR-message.
2023 * @param cls identification of the client
2024 * @param message the actual message
2025 * @return #GNUNET_OK to keep the connection open,
2026 * #GNUNET_SYSERR to close it (signal serious error)
2029 handle_monitor (void *cls, const struct GNUNET_MessageHeader *message)
2031 struct GNUNET_SERVICE_Client *client = cls;
2034 /* FIXME: might want to start by letting monitor know about
2035 services that are already running */
2036 /* Removal is handled by the server implementation, internally. */
2037 GNUNET_notification_context_add (notifier,
2038 GNUNET_SERVICE_client_get_mq (client));
2039 broadcast_status ("arm", GNUNET_ARM_SERVICE_MONITORING_STARTED, client);
2040 GNUNET_SERVICE_client_continue (client);
2045 * Process arm requests.
2047 * @param cls closure, NULL
2048 * @param serv the initialized service
2049 * @param c configuration to use
2053 const struct GNUNET_CONFIGURATION_Handle *c,
2054 struct GNUNET_SERVICE_Handle *serv)
2056 struct ServiceList *sl;
2061 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
2062 child_death_task = GNUNET_SCHEDULER_add_read_file (
2063 GNUNET_TIME_UNIT_FOREVER_REL,
2064 GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ),
2069 GNUNET_CONFIGURATION_get_value_filename (cfg,
2071 "RESOURCE_DIAGNOSTICS",
2074 wait_file = fopen (wait_filename, "w");
2075 if (NULL == wait_file)
2077 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2083 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
2087 prefix_command = GNUNET_strdup ("");
2089 prefix_command = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix_command);
2090 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
2094 final_option = GNUNET_strdup ("");
2096 final_option = GNUNET_CONFIGURATION_expand_dollar (cfg, final_option);
2098 GNUNET_CONFIGURATION_get_value_yesno (cfg, "ARM", "START_USER_SERVICES");
2100 GNUNET_CONFIGURATION_get_value_yesno (cfg, "ARM", "START_SYSTEM_SERVICES");
2101 if ((GNUNET_NO == start_user) && (GNUNET_NO == start_system))
2104 GNUNET_ERROR_TYPE_ERROR,
2105 "Please configure either START_USER_SERVICES or START_SYSTEM_SERVICES or both.\n");
2106 GNUNET_SCHEDULER_shutdown ();
2110 GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
2112 /* start default services... */
2113 for (sl = running_head; NULL != sl; sl = sl->next)
2114 if (GNUNET_YES == sl->force_start)
2115 start_process (sl, NULL, 0);
2116 notifier = GNUNET_notification_context_create (MAX_NOTIFY_QUEUE);
2121 * The main function for the arm service.
2123 * @param argc number of arguments from the command line
2124 * @param argv command line arguments
2125 * @return 0 ok, 1 on error
2128 main (int argc, char *const *argv)
2130 struct GNUNET_SIGNAL_Context *shc_chld;
2131 struct GNUNET_MQ_MessageHandler handlers[] = {
2132 GNUNET_MQ_hd_var_size (start,
2133 GNUNET_MESSAGE_TYPE_ARM_START,
2134 struct GNUNET_ARM_Message,
2136 GNUNET_MQ_hd_var_size (stop,
2137 GNUNET_MESSAGE_TYPE_ARM_STOP,
2138 struct GNUNET_ARM_Message,
2140 GNUNET_MQ_hd_fixed_size (monitor,
2141 GNUNET_MESSAGE_TYPE_ARM_MONITOR,
2142 struct GNUNET_MessageHeader,
2144 GNUNET_MQ_hd_fixed_size (list,
2145 GNUNET_MESSAGE_TYPE_ARM_LIST,
2146 struct GNUNET_ARM_Message,
2148 GNUNET_MQ_hd_fixed_size (test,
2149 GNUNET_MESSAGE_TYPE_ARM_TEST,
2150 struct GNUNET_MessageHeader,
2152 GNUNET_MQ_handler_end ()
2155 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
2156 GNUNET_assert (NULL != sigpipe);
2158 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD,
2159 &sighandler_child_death);
2160 if (0 != GNUNET_SERVICE_run_ (argc,
2163 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN
2164 | GNUNET_SERVICE_OPTION_CLOSE_LSOCKS,
2167 &client_disconnect_cb,
2172 if (NULL != wait_file)
2177 if (NULL != wait_filename)
2179 GNUNET_free (wait_filename);
2180 wait_filename = NULL;
2183 GNUNET_SIGNAL_handler_uninstall (shc_chld);
2185 GNUNET_DISK_pipe_close (sigpipe);
2191 #if defined(__linux__) && defined(__GLIBC__)
2195 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
2197 void __attribute__ ((constructor))
2198 GNUNET_ARM_memory_init ()
2200 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
2201 mallopt (M_TOP_PAD, 1 * 1024);
2209 /* end of gnunet-service-arm.c */