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 const char *servicename;
1241 size = ntohs (amsg->header.size) - sizeof (struct GNUNET_ARM_Message);
1242 servicename = (const char *) &amsg[1];
1244 (servicename[size - 1] != '\0') )
1247 return GNUNET_SYSERR;
1254 * Handle START-message.
1256 * @param cls identification of the client
1257 * @param amsg the actual message
1260 handle_start (void *cls,
1261 const struct GNUNET_ARM_Message *amsg)
1263 struct GNUNET_SERVICE_Client *client = cls;
1264 const char *servicename;
1265 struct ServiceList *sl;
1266 uint64_t request_id;
1268 request_id = GNUNET_ntohll (amsg->request_id);
1269 servicename = (const char *) &amsg[1];
1270 GNUNET_SERVICE_client_continue (client);
1271 if (GNUNET_YES == in_shutdown)
1273 signal_result (client,
1276 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1279 sl = find_service (servicename);
1282 signal_result (client,
1285 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1288 sl->force_start = GNUNET_YES;
1289 if (NULL != sl->proc)
1291 signal_result (client,
1294 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
1304 * Start a shutdown sequence.
1306 * @param cls closure (refers to service)
1309 trigger_shutdown (void *cls)
1312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1313 "Triggering shutdown\n");
1314 GNUNET_SCHEDULER_shutdown ();
1319 * Check STOP-message.
1321 * @param cls identification of the client
1322 * @param amsg the actual message
1323 * @return #GNUNET_OK to keep the connection open,
1324 * #GNUNET_SYSERR to close it (signal serious error)
1327 check_stop (void *cls,
1328 const struct GNUNET_ARM_Message *amsg)
1331 const char *servicename;
1334 size = ntohs (amsg->header.size) - sizeof (struct GNUNET_ARM_Message);
1335 servicename = (const char *) &amsg[1];
1337 (servicename[size - 1] != '\0') )
1340 return GNUNET_SYSERR;
1347 * Handle STOP-message.
1349 * @param cls identification of the client
1350 * @param amsg the actual message
1353 handle_stop (void *cls,
1354 const struct GNUNET_ARM_Message *amsg)
1356 struct GNUNET_SERVICE_Client *client = cls;
1357 struct ServiceList *sl;
1358 const char *servicename;
1359 uint64_t request_id;
1361 request_id = GNUNET_ntohll (amsg->request_id);
1362 servicename = (const char *) &amsg[1];
1363 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1364 _("Preparing to stop `%s'\n"),
1366 GNUNET_SERVICE_client_continue (client);
1367 if (0 == strcasecmp (servicename,
1370 broadcast_status (servicename,
1371 GNUNET_ARM_SERVICE_STOPPING,
1373 signal_result (client,
1376 GNUNET_ARM_RESULT_STOPPING);
1377 GNUNET_SERVICE_client_persist (client);
1378 GNUNET_SCHEDULER_add_now (&trigger_shutdown,
1382 sl = find_service (servicename);
1385 signal_result (client,
1388 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1391 sl->force_start = GNUNET_NO;
1392 if (GNUNET_YES == in_shutdown)
1394 /* shutdown in progress */
1395 signal_result (client,
1398 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1401 if (NULL != sl->killing_client)
1403 /* killing already in progress */
1404 signal_result (client,
1407 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
1410 if (NULL == sl->proc)
1412 /* process is down */
1413 signal_result (client,
1416 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420 "Sending kill signal to service `%s', waiting for process to die.\n",
1422 broadcast_status (servicename,
1423 GNUNET_ARM_SERVICE_STOPPING,
1425 /* no signal_start - only when it's STOPPED */
1426 sl->killed_at = GNUNET_TIME_absolute_get ();
1427 if (0 != GNUNET_OS_process_kill (sl->proc,
1429 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1431 sl->killing_client = client;
1432 sl->killing_client_request_id = request_id;
1437 * Handle LIST-message.
1439 * @param cls identification of the client
1440 * @param message the actual message
1443 handle_list (void *cls,
1444 const struct GNUNET_ARM_Message *request)
1446 struct GNUNET_SERVICE_Client *client = cls;
1447 struct GNUNET_MQ_Envelope *env;
1448 struct GNUNET_ARM_ListResultMessage *msg;
1449 size_t string_list_size;
1450 struct ServiceList *sl;
1454 GNUNET_break (0 == ntohl (request->reserved));
1456 string_list_size = 0;
1458 /* first count the running processes get their name's size */
1459 for (sl = running_head; NULL != sl; sl = sl->next)
1461 if (NULL != sl->proc)
1463 string_list_size += strlen (sl->name);
1464 string_list_size += strlen (sl->binary);
1465 string_list_size += 4;
1470 env = GNUNET_MQ_msg_extra (msg,
1472 GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT);
1473 msg->arm_msg.request_id = request->request_id;
1474 msg->count = htons (count);
1476 pos = (char *) &msg[1];
1477 for (sl = running_head; NULL != sl; sl = sl->next)
1479 if (NULL != sl->proc)
1481 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
1482 GNUNET_snprintf (pos,
1490 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1492 GNUNET_SERVICE_client_continue (client);
1497 * Handle TEST-message by sending back TEST.
1499 * @param cls identification of the client
1500 * @param message the actual message
1503 handle_test (void *cls,
1504 const struct GNUNET_MessageHeader *message)
1506 struct GNUNET_SERVICE_Client *client = cls;
1507 struct GNUNET_MQ_Envelope *env;
1508 struct GNUNET_MessageHeader *msg;
1511 env = GNUNET_MQ_msg (msg,
1512 GNUNET_MESSAGE_TYPE_ARM_TEST);
1513 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1515 GNUNET_SERVICE_client_continue (client);
1520 * We are done with everything. Stop remaining
1521 * tasks, signal handler and the server.
1526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1527 "Last shutdown phase\n");
1528 if (NULL != notifier)
1530 GNUNET_notification_context_destroy (notifier);
1533 if (NULL != service)
1535 GNUNET_SERVICE_shutdown (service);
1538 if (NULL != child_death_task)
1540 GNUNET_SCHEDULER_cancel (child_death_task);
1541 child_death_task = NULL;
1547 * Count how many services are still active.
1549 * @param running_head list of services
1550 * @return number of active services found
1553 list_count (struct ServiceList *running_head)
1555 struct ServiceList *i;
1558 for (res = 0, i = running_head; NULL != i; i = i->next, res++)
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1567 * Task run for shutdown.
1569 * @param cls closure, NULL if we need to self-restart
1572 shutdown_task (void *cls)
1574 struct ServiceList *pos;
1575 struct ServiceList *nxt;
1576 struct ServiceListeningInfo *sli;
1579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1580 "First shutdown phase\n");
1581 if (NULL != child_restart_task)
1583 GNUNET_SCHEDULER_cancel (child_restart_task);
1584 child_restart_task = NULL;
1586 in_shutdown = GNUNET_YES;
1587 /* first, stop listening */
1588 for (pos = running_head; NULL != pos; pos = pos->next)
1590 while (NULL != (sli = pos->listen_head))
1592 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
1595 if (NULL != sli->accept_task)
1597 GNUNET_SCHEDULER_cancel (sli->accept_task);
1598 sli->accept_task = NULL;
1600 GNUNET_break (GNUNET_OK ==
1601 GNUNET_NETWORK_socket_close (sli->listen_socket));
1602 GNUNET_free (sli->service_addr);
1606 /* then, shutdown all existing service processes */
1608 while (NULL != (pos = nxt))
1611 if (NULL != pos->proc)
1613 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1614 "Stopping service `%s'\n",
1616 pos->killed_at = GNUNET_TIME_absolute_get ();
1617 if (0 != GNUNET_OS_process_kill (pos->proc,
1619 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1627 /* finally, should all service processes be already gone, terminate for real */
1628 if (NULL == running_head)
1631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1632 "Delaying shutdown, have %u childs still running\n",
1633 list_count (running_head));
1638 * Task run whenever it is time to restart a child that died.
1640 * @param cls closure, always NULL
1643 delayed_restart_task (void *cls)
1646 struct ServiceList *sl;
1647 struct GNUNET_TIME_Relative lowestRestartDelay;
1648 struct ServiceListeningInfo *sli;
1651 child_restart_task = NULL;
1652 GNUNET_assert (GNUNET_NO == in_shutdown);
1653 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1655 /* check for services that need to be restarted due to
1656 * configuration changes or because the last restart failed */
1657 for (sl = running_head; NULL != sl; sl = sl->next)
1659 if (NULL != sl->proc)
1661 /* service is currently not running */
1662 if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
1664 /* restart is now allowed */
1665 if (sl->force_start)
1667 /* process should run by default, start immediately */
1668 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1669 _("Restarting service `%s'.\n"),
1677 /* process is run on-demand, ensure it is re-started if there is demand */
1678 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1679 if (NULL == sli->accept_task)
1681 /* accept was actually paused, so start it again */
1683 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1692 /* update calculation for earliest time to reactivate a service */
1693 lowestRestartDelay =
1694 GNUNET_TIME_relative_min (lowestRestartDelay,
1695 GNUNET_TIME_absolute_get_remaining
1699 if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1702 "Will restart process in %s\n",
1703 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay,
1705 child_restart_task =
1706 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1707 GNUNET_SCHEDULER_PRIORITY_IDLE,
1708 &delayed_restart_task,
1715 * Task triggered whenever we receive a SIGCHLD (child
1718 * @param cls closure, NULL
1721 maint_child_death (void *cls)
1723 struct ServiceList *pos;
1724 struct ServiceList *next;
1725 struct ServiceListeningInfo *sli;
1726 const char *statstr;
1730 enum GNUNET_OS_ProcessStatusType statusType;
1731 unsigned long statusCode;
1732 const struct GNUNET_DISK_FileHandle *pr;
1735 pr = GNUNET_DISK_pipe_handle (sigpipe,
1736 GNUNET_DISK_PIPE_END_READ);
1737 child_death_task = NULL;
1738 /* consume the signal */
1739 GNUNET_break (0 < GNUNET_DISK_file_read (pr,
1743 /* check for services that died (WAITPID) */
1744 next = running_head;
1745 while (NULL != (pos = next))
1749 if (NULL == pos->proc)
1751 if (GNUNET_YES == in_shutdown)
1756 if (NULL != wait_file)
1758 /* need to use 'wait4()' to obtain and log performance data */
1763 pid = GNUNET_OS_process_get_pid (pos->proc);
1769 continue; /* no process done */
1770 if (WIFEXITED (status))
1772 statusType = GNUNET_OS_PROCESS_EXITED;
1773 statusCode = WEXITSTATUS (status);
1775 else if (WIFSIGNALED (status))
1777 statusType = GNUNET_OS_PROCESS_SIGNALED;
1778 statusCode = WTERMSIG (status);
1780 else if (WIFSTOPPED (status))
1782 statusType = GNUNET_OS_PROCESS_SIGNALED;
1783 statusCode = WSTOPSIG (status);
1786 else if (WIFCONTINUED (status))
1788 statusType = GNUNET_OS_PROCESS_RUNNING;
1794 statusType = GNUNET_OS_PROCESS_UNKNOWN;
1797 if ( (GNUNET_OS_PROCESS_EXITED == statusType) ||
1798 (GNUNET_OS_PROCESS_SIGNALED == statusType) )
1800 double utime = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 10e6);
1801 double stime = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 10e6);
1803 "%s(%u) %.3f %.3f %llu %llu %llu %llu %llu\n",
1808 (unsigned long long) ru.ru_maxrss,
1809 (unsigned long long) ru.ru_inblock,
1810 (unsigned long long) ru.ru_oublock,
1811 (unsigned long long) ru.ru_nvcsw,
1812 (unsigned long long) ru.ru_nivcsw);
1815 else /* continue with JUST this "if" as "else" (intentionally no brackets!) */
1817 if ( (GNUNET_SYSERR ==
1819 GNUNET_OS_process_status (pos->proc,
1822 (ret == GNUNET_NO) ||
1823 (statusType == GNUNET_OS_PROCESS_STOPPED) ||
1824 (statusType == GNUNET_OS_PROCESS_UNKNOWN) ||
1825 (statusType == GNUNET_OS_PROCESS_RUNNING) )
1828 if (statusType == GNUNET_OS_PROCESS_EXITED)
1830 statstr = _( /* process termination method */ "exit");
1831 statcode = statusCode;
1833 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1835 statstr = _( /* process termination method */ "signal");
1836 statcode = statusCode;
1840 statstr = _( /* process termination method */ "unknown");
1843 if (0 != pos->killed_at.abs_value_us)
1845 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1846 _("Service `%s' took %s to terminate\n"),
1848 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at),
1851 GNUNET_OS_process_destroy (pos->proc);
1853 broadcast_status (pos->name,
1854 GNUNET_ARM_SERVICE_STOPPED,
1856 if (NULL != pos->killing_client)
1858 signal_result (pos->killing_client, pos->name,
1859 pos->killing_client_request_id,
1860 GNUNET_ARM_RESULT_STOPPED);
1861 pos->killing_client = NULL;
1862 pos->killing_client_request_id = 0;
1864 if (GNUNET_YES != in_shutdown)
1866 if ( (statusType == GNUNET_OS_PROCESS_EXITED) &&
1869 /* process terminated normally, allow restart at any time */
1870 pos->restart_at.abs_value_us = 0;
1871 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1872 _("Service `%s' terminated normally, will restart at any time\n"),
1874 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1875 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1877 GNUNET_break (NULL == sli->accept_task);
1879 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1887 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1888 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1892 GNUNET_STRINGS_relative_time_to_string (pos->backoff,
1895 /* Reduce backoff based on runtime of the process,
1896 so that there is a cool-down if a process actually
1897 runs for a while. */
1898 struct GNUNET_TIME_Relative runtime;
1899 unsigned int minutes;
1901 runtime = GNUNET_TIME_absolute_get_duration (pos->restart_at);
1902 minutes = runtime.rel_value_us / GNUNET_TIME_UNIT_MINUTES.rel_value_us;
1904 pos->backoff = GNUNET_TIME_UNIT_ZERO;
1906 pos->backoff.rel_value_us <<= minutes;
1908 /* schedule restart */
1909 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1910 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1911 if (NULL != child_restart_task)
1912 GNUNET_SCHEDULER_cancel (child_restart_task);
1914 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1915 &delayed_restart_task,
1924 child_death_task = GNUNET_SCHEDULER_add_read_file (
1925 GNUNET_TIME_UNIT_FOREVER_REL,
1927 &maint_child_death, NULL);
1928 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1930 else if (GNUNET_YES == in_shutdown)
1931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1932 "Delaying shutdown after child's death, still have %u children\n",
1933 list_count (running_head));
1939 * Signal handler called for SIGCHLD. Triggers the
1940 * respective handler by writing to the trigger pipe.
1943 sighandler_child_death ()
1946 int old_errno = errno; /* back-up errno */
1949 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
1950 GNUNET_DISK_PIPE_END_WRITE),
1953 errno = old_errno; /* restore errno */
1958 * Setup our service record for the given section in the configuration file
1959 * (assuming the section is for a service).
1962 * @param section a section in the configuration file
1963 * @return #GNUNET_OK (continue)
1966 setup_service (void *cls,
1967 const char *section)
1969 struct ServiceList *sl;
1973 struct sockaddr **addrs;
1974 socklen_t *addr_lens;
1978 if (0 == strcasecmp (section,
1982 GNUNET_CONFIGURATION_get_value_string (cfg,
1987 /* not a service section */
1991 GNUNET_CONFIGURATION_have_value (cfg,
1995 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1999 if (GNUNET_NO == start_user)
2001 GNUNET_free (binary);
2002 return; /* user service, and we don't deal with those */
2007 if (GNUNET_NO == start_system)
2009 GNUNET_free (binary);
2010 return; /* system service, and we don't deal with those */
2013 sl = find_service (section);
2016 /* got the same section twice!? */
2018 GNUNET_free (binary);
2023 GNUNET_CONFIGURATION_get_value_filename (cfg,
2028 GNUNET_CONFIGURATION_get_value_filename (cfg,
2032 (0 != STAT (config, &sbuf)))
2036 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
2039 GNUNET_free (config);
2043 sl = GNUNET_new (struct ServiceList);
2044 sl->name = GNUNET_strdup (section);
2045 sl->binary = binary;
2046 sl->config = config;
2047 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
2048 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
2050 sl->pipe_control = GNUNET_YES;
2052 if (GNUNET_CONFIGURATION_have_value (cfg,
2055 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2059 GNUNET_CONTAINER_DLL_insert (running_head,
2063 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2067 sl->force_start = GNUNET_YES;
2069 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2077 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2082 if (0 >= (ret = get_server_addresses (section,
2087 /* this will free (or capture) addrs[i] */
2088 for (unsigned int i = 0; i < (unsigned int) ret; i++)
2089 create_listen_socket (addrs[i],
2092 GNUNET_free (addrs);
2093 GNUNET_free (addr_lens);
2098 * A client connected, mark as a monitoring client.
2100 * @param cls closure
2101 * @param client identification of the client
2102 * @param mq queue to talk to @a client
2106 client_connect_cb (void *cls,
2107 struct GNUNET_SERVICE_Client *client,
2108 struct GNUNET_MQ_Handle *mq)
2110 /* All clients are considered to be of the "monitor" kind
2111 * (that is, they don't affect ARM shutdown).
2115 GNUNET_SERVICE_client_mark_monitor (client);
2121 * A client disconnected, clean up associated state.
2123 * @param cls closure
2124 * @param client identification of the client
2125 * @param app_ctx must match @a client
2128 client_disconnect_cb (void *cls,
2129 struct GNUNET_SERVICE_Client *client,
2133 GNUNET_assert (client == app_ctx);
2134 for (struct ServiceList *sl = running_head; NULL != sl; sl = sl->next)
2135 if (sl->killing_client == client)
2136 sl->killing_client = NULL;
2141 * Handle MONITOR-message.
2143 * @param cls identification of the client
2144 * @param message the actual message
2145 * @return #GNUNET_OK to keep the connection open,
2146 * #GNUNET_SYSERR to close it (signal serious error)
2149 handle_monitor (void *cls,
2150 const struct GNUNET_MessageHeader *message)
2152 struct GNUNET_SERVICE_Client *client = cls;
2155 /* FIXME: might want to start by letting monitor know about
2156 services that are already running */
2157 /* Removal is handled by the server implementation, internally. */
2158 GNUNET_notification_context_add (notifier,
2159 GNUNET_SERVICE_client_get_mq (client));
2160 broadcast_status ("arm",
2161 GNUNET_ARM_SERVICE_MONITORING_STARTED,
2163 GNUNET_SERVICE_client_continue (client);
2168 * Process arm requests.
2170 * @param cls closure, NULL
2171 * @param serv the initialized service
2172 * @param c configuration to use
2176 const struct GNUNET_CONFIGURATION_Handle *c,
2177 struct GNUNET_SERVICE_Handle *serv)
2179 struct ServiceList *sl;
2184 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
2187 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2188 GNUNET_DISK_pipe_handle (sigpipe,
2189 GNUNET_DISK_PIPE_END_READ),
2194 GNUNET_CONFIGURATION_get_value_filename (cfg,
2196 "RESOURCE_DIAGNOSTICS",
2199 wait_file = fopen (wait_filename,
2201 if (NULL == wait_file)
2203 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2210 GNUNET_CONFIGURATION_get_value_string (cfg,
2214 prefix_command = GNUNET_strdup ("");
2216 prefix_command = GNUNET_CONFIGURATION_expand_dollar (cfg,
2219 GNUNET_CONFIGURATION_get_value_string (cfg,
2223 final_option = GNUNET_strdup ("");
2225 final_option = GNUNET_CONFIGURATION_expand_dollar (cfg,
2227 start_user = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2229 "START_USER_SERVICES");
2230 start_system = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2232 "START_SYSTEM_SERVICES");
2233 if ( (GNUNET_NO == start_user) &&
2234 (GNUNET_NO == start_system) )
2236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2237 "Please configure either START_USER_SERVICES or START_SYSTEM_SERVICES or both.\n");
2238 GNUNET_SCHEDULER_shutdown ();
2242 GNUNET_CONFIGURATION_iterate_sections (cfg,
2246 /* start default services... */
2247 for (sl = running_head; NULL != sl; sl = sl->next)
2248 if (GNUNET_YES == sl->force_start)
2252 notifier = GNUNET_notification_context_create (MAX_NOTIFY_QUEUE);
2257 * The main function for the arm service.
2259 * @param argc number of arguments from the command line
2260 * @param argv command line arguments
2261 * @return 0 ok, 1 on error
2267 struct GNUNET_SIGNAL_Context *shc_chld;
2268 struct GNUNET_MQ_MessageHandler handlers[] = {
2269 GNUNET_MQ_hd_var_size (start,
2270 GNUNET_MESSAGE_TYPE_ARM_START,
2271 struct GNUNET_ARM_Message,
2273 GNUNET_MQ_hd_var_size (stop,
2274 GNUNET_MESSAGE_TYPE_ARM_STOP,
2275 struct GNUNET_ARM_Message,
2277 GNUNET_MQ_hd_fixed_size (monitor,
2278 GNUNET_MESSAGE_TYPE_ARM_MONITOR,
2279 struct GNUNET_MessageHeader,
2281 GNUNET_MQ_hd_fixed_size (list,
2282 GNUNET_MESSAGE_TYPE_ARM_LIST,
2283 struct GNUNET_ARM_Message,
2285 GNUNET_MQ_hd_fixed_size (test,
2286 GNUNET_MESSAGE_TYPE_ARM_TEST,
2287 struct GNUNET_MessageHeader,
2289 GNUNET_MQ_handler_end ()
2292 sigpipe = GNUNET_DISK_pipe (GNUNET_NO,
2296 GNUNET_assert (NULL != sigpipe);
2298 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD,
2299 &sighandler_child_death);
2300 if ( GNUNET_OK != GNUNET_SERVICE_run_ (argc,
2303 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN,
2306 &client_disconnect_cb,
2311 if (NULL != wait_file)
2316 if (NULL != wait_filename)
2318 GNUNET_free (wait_filename);
2319 wait_filename = NULL;
2322 GNUNET_SIGNAL_handler_uninstall (shc_chld);
2324 GNUNET_DISK_pipe_close (sigpipe);
2330 #if defined(LINUX) && defined(__GLIBC__)
2334 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
2336 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
2338 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
2339 mallopt (M_TOP_PAD, 1 * 1024);
2345 /* end of gnunet-service-arm.c */