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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file arm/gnunet-service-arm.c
23 * @brief the automated restart manager service
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_protocols.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
39 * Name of the file for writing resource utilization summaries to.
41 static char *wait_filename;
44 * Handle for the file for writing resource summaries.
46 static FILE *wait_file;
51 * How many messages do we queue up at most for optional
52 * notifications to a client? (this can cause notifications
53 * about outgoing messages to be dropped).
55 #define MAX_NOTIFY_QUEUE 1024
59 * List of our services.
65 * Record with information about a listen socket we have open.
67 struct ServiceListeningInfo
70 * This is a linked list.
72 struct ServiceListeningInfo *next;
75 * This is a linked list.
77 struct ServiceListeningInfo *prev;
80 * Address this socket is listening on.
82 struct sockaddr *service_addr;
85 * Service this listen socket is for.
87 struct ServiceList *sl;
90 * Number of bytes in @e service_addr
92 socklen_t service_addr_len;
95 * Our listening socket.
97 struct GNUNET_NETWORK_Handle *listen_socket;
100 * Task doing the accepting.
102 struct GNUNET_SCHEDULER_Task *accept_task;
108 * List of our services.
113 * This is a doubly-linked list.
115 struct ServiceList *next;
118 * This is a doubly-linked list.
120 struct ServiceList *prev;
123 * Linked list of listen sockets associated with this service.
125 struct ServiceListeningInfo *listen_head;
128 * Linked list of listen sockets associated with this service.
130 struct ServiceListeningInfo *listen_tail;
133 * Name of the service.
138 * Name of the binary used.
143 * Name of the configuration file used.
148 * Client to notify upon kill completion (waitpid), NULL
149 * if we should simply restart the process.
151 struct GNUNET_SERVICE_Client *killing_client;
154 * ID of the request that killed the service (for reporting back).
156 uint64_t killing_client_request_id;
159 * Process structure pointer of the child.
161 struct GNUNET_OS_Process *proc;
164 * Process exponential backoff time
166 struct GNUNET_TIME_Relative backoff;
169 * Absolute time at which the process is scheduled to restart in case of death
171 struct GNUNET_TIME_Absolute restart_at;
174 * Time we asked the service to shut down (used to calculate time it took
175 * the service to terminate).
177 struct GNUNET_TIME_Absolute killed_at;
180 * Is this service to be started by default (or did a client tell us explicitly
181 * to start it)? #GNUNET_NO if the service is started only upon 'accept' on a
182 * listen socket or possibly explicitly by a client changing the value.
187 * Should we use pipes to signal this process? (YES for Java binaries and if we
194 * List of running services.
196 static struct ServiceList *running_head;
199 * List of running services.
201 static struct ServiceList *running_tail;
206 static const struct GNUNET_CONFIGURATION_Handle *cfg;
209 * Command to prepend to each actual command.
211 static char *prefix_command;
214 * Option to append to each actual command.
216 static char *final_option;
219 * ID of task called whenever we get a SIGCHILD.
221 static struct GNUNET_SCHEDULER_Task *child_death_task;
224 * ID of task called whenever the timeout for restarting a child
227 static struct GNUNET_SCHEDULER_Task *child_restart_task;
230 * Pipe used to communicate shutdown via signal.
232 static struct GNUNET_DISK_PipeHandle *sigpipe;
235 * Are we in shutdown mode?
237 static int in_shutdown;
240 * Are we starting user services?
242 static int start_user = GNUNET_YES;
245 * Are we starting system services?
247 static int start_system = GNUNET_YES;
250 * Handle to our service instance. Our service is a bit special in that
251 * its service is not immediately stopped once we get a shutdown
252 * request (since we need to continue service until all of our child
253 * processes are dead). This handle is used to shut down the service
254 * (and thus trigger process termination) once all child processes are
255 * also dead. A special option in the ARM configuration modifies the
256 * behaviour of the service implementation to not do the shutdown
259 static struct GNUNET_SERVICE_Handle *service;
262 * Context for notifications we need to send to our clients.
264 static struct GNUNET_NotificationContext *notifier;
268 * Add the given UNIX domain path as an address to the
269 * list (as the first entry).
271 * @param saddrs array to update
272 * @param saddrlens where to store the address length
273 * @param unixpath path to add
274 * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
275 * parameter is ignore on systems other than LINUX
278 add_unixpath (struct sockaddr **saddrs,
279 socklen_t *saddrlens,
280 const char *unixpath,
284 struct sockaddr_un *un;
286 un = GNUNET_new (struct sockaddr_un);
287 un->sun_family = AF_UNIX;
288 strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
290 if (GNUNET_YES == abstract)
291 un->sun_path[0] = '\0';
293 #if HAVE_SOCKADDR_UN_SUN_LEN
294 un->sun_len = (u_char) sizeof (struct sockaddr_un);
296 *saddrs = (struct sockaddr *) un;
297 *saddrlens = sizeof (struct sockaddr_un);
299 /* this function should never be called
300 * unless AF_UNIX is defined! */
307 * Get the list of addresses that a server for the given service
310 * @param service_name name of the service
311 * @param cfg configuration (which specifies the addresses)
312 * @param addrs set (call by reference) to an array of pointers to the
313 * addresses the server should bind to and listen on; the
314 * array will be NULL-terminated (on success)
315 * @param addr_lens set (call by reference) to an array of the lengths
316 * of the respective `struct sockaddr` struct in the @a addrs
318 * @return number of addresses found on success,
319 * #GNUNET_SYSERR if the configuration
320 * did not specify reasonable finding information or
321 * if it specified a hostname that could not be resolved;
322 * #GNUNET_NO if the number of addresses configured is
323 * zero (in this case, `*addrs` and `*addr_lens` will be
327 get_server_addresses (const char *service_name,
328 const struct GNUNET_CONFIGURATION_Handle *cfg,
329 struct sockaddr ***addrs,
330 socklen_t ** addr_lens)
333 struct GNUNET_NETWORK_Handle *desc;
334 unsigned long long port;
336 struct addrinfo hints;
337 struct addrinfo *res;
338 struct addrinfo *pos;
339 struct addrinfo *next;
344 struct sockaddr **saddrs;
345 socklen_t *saddrlens;
351 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
355 GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
356 return GNUNET_SYSERR;
359 disablev6 = GNUNET_NO;
363 /* probe IPv6 support */
364 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
367 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
370 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
371 return GNUNET_SYSERR;
373 LOG (GNUNET_ERROR_TYPE_INFO,
374 _("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
375 service_name, STRERROR (errno));
376 disablev6 = GNUNET_YES;
380 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
386 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
389 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
392 LOG (GNUNET_ERROR_TYPE_ERROR,
393 _("Require valid port number for service `%s' in configuration!\n"),
398 LOG (GNUNET_ERROR_TYPE_ERROR,
399 _("Require valid port number for service `%s' in configuration!\n"),
401 return GNUNET_SYSERR;
405 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
407 GNUNET_break (GNUNET_OK ==
408 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
409 "BINDTO", &hostname));
415 abstract = GNUNET_NO;
418 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
420 GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH",
422 (0 < strlen (unixpath)))
424 /* probe UNIX support */
425 struct sockaddr_un s_un;
427 if (strlen (unixpath) >= sizeof (s_un.sun_path))
429 LOG (GNUNET_ERROR_TYPE_WARNING,
430 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
431 (unsigned long long) sizeof (s_un.sun_path));
432 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
433 LOG (GNUNET_ERROR_TYPE_INFO,
434 _("Using `%s' instead\n"),
438 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
440 "USE_ABSTRACT_SOCKETS");
441 if (GNUNET_SYSERR == abstract)
442 abstract = GNUNET_NO;
444 if ((GNUNET_YES != abstract)
446 GNUNET_DISK_directory_create_for_file (unixpath)))
447 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
451 if (NULL != unixpath)
453 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
456 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
459 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
460 GNUNET_free_non_null (hostname);
461 GNUNET_free (unixpath);
462 return GNUNET_SYSERR;
464 LOG (GNUNET_ERROR_TYPE_INFO,
465 _("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
468 GNUNET_free (unixpath);
473 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
479 if ((0 == port) && (NULL == unixpath))
481 LOG (GNUNET_ERROR_TYPE_ERROR,
482 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
484 GNUNET_free_non_null (hostname);
485 return GNUNET_SYSERR;
489 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
490 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
491 add_unixpath (saddrs, saddrlens, unixpath, abstract);
492 GNUNET_free_non_null (unixpath);
493 GNUNET_free_non_null (hostname);
495 *addr_lens = saddrlens;
499 if (NULL != hostname)
501 LOG (GNUNET_ERROR_TYPE_DEBUG,
502 "Resolving `%s' since that is where `%s' will bind to.\n",
505 memset (&hints, 0, sizeof (struct addrinfo));
507 hints.ai_family = AF_INET;
508 hints.ai_protocol = IPPROTO_TCP;
509 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
512 LOG (GNUNET_ERROR_TYPE_ERROR,
513 _("Failed to resolve `%s': %s\n"),
516 GNUNET_free (hostname);
517 GNUNET_free_non_null (unixpath);
518 return GNUNET_SYSERR;
522 while (NULL != (pos = next))
525 if ((disablev6) && (pos->ai_family == AF_INET6))
531 LOG (GNUNET_ERROR_TYPE_ERROR,
532 _("Failed to find %saddress for `%s'.\n"),
533 disablev6 ? "IPv4 " : "",
536 GNUNET_free (hostname);
537 GNUNET_free_non_null (unixpath);
538 return GNUNET_SYSERR;
541 if (NULL != unixpath)
543 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
544 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
546 if (NULL != unixpath)
548 add_unixpath (saddrs, saddrlens, unixpath, abstract);
552 while (NULL != (pos = next))
555 if ((disablev6) && (AF_INET6 == pos->ai_family))
557 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
558 continue; /* not TCP */
559 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
561 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
562 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
563 if (AF_INET == pos->ai_family)
565 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
566 saddrlens[i] = pos->ai_addrlen;
567 saddrs[i] = GNUNET_malloc (saddrlens[i]);
568 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
569 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
573 GNUNET_assert (AF_INET6 == pos->ai_family);
574 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
575 saddrlens[i] = pos->ai_addrlen;
576 saddrs[i] = GNUNET_malloc (saddrlens[i]);
577 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
578 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
582 GNUNET_free (hostname);
588 /* will bind against everything, just set port */
593 if (NULL != unixpath)
596 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
597 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
598 if (NULL != unixpath)
600 add_unixpath (saddrs, saddrlens, unixpath, abstract);
603 saddrlens[i] = sizeof (struct sockaddr_in);
604 saddrs[i] = GNUNET_malloc (saddrlens[i]);
605 #if HAVE_SOCKADDR_IN_SIN_LEN
606 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
608 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
609 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
615 if (NULL != unixpath)
617 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
618 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
620 if (NULL != unixpath)
622 add_unixpath (saddrs, saddrlens, unixpath, abstract);
625 saddrlens[i] = sizeof (struct sockaddr_in6);
626 saddrs[i] = GNUNET_malloc (saddrlens[i]);
627 #if HAVE_SOCKADDR_IN_SIN_LEN
628 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
630 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
631 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
633 saddrlens[i] = sizeof (struct sockaddr_in);
634 saddrs[i] = GNUNET_malloc (saddrlens[i]);
635 #if HAVE_SOCKADDR_IN_SIN_LEN
636 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
638 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
639 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
642 GNUNET_free_non_null (unixpath);
644 *addr_lens = saddrlens;
650 * Signal our client that we will start or stop the
653 * @param client who is being signalled
654 * @param name name of the service
655 * @param request_id id of the request that is being responded to.
656 * @param result message type to send
657 * @return NULL if it was not found
660 signal_result (struct GNUNET_SERVICE_Client *client,
663 enum GNUNET_ARM_Result result)
665 struct GNUNET_MQ_Envelope *env;
666 struct GNUNET_ARM_ResultMessage *msg;
668 env = GNUNET_MQ_msg (msg,
669 GNUNET_MESSAGE_TYPE_ARM_RESULT);
670 msg->result = htonl (result);
671 msg->arm_msg.request_id = GNUNET_htonll (request_id);
672 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
678 * Tell all clients about status change of a service.
680 * @param name name of the service
681 * @param status message type to send
682 * @param unicast if not NULL, send to this client only.
683 * otherwise, send to all clients in the notifier
686 broadcast_status (const char *name,
687 enum GNUNET_ARM_ServiceStatus status,
688 struct GNUNET_SERVICE_Client *unicast)
690 struct GNUNET_MQ_Envelope *env;
691 struct GNUNET_ARM_StatusMessage *msg;
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 "Sending status %u of service `%s' to client\n",
696 (unsigned int) status,
698 namelen = strlen (name) + 1;
699 env = GNUNET_MQ_msg_extra (msg,
701 GNUNET_MESSAGE_TYPE_ARM_STATUS);
702 msg->status = htonl ((uint32_t) (status));
703 GNUNET_memcpy ((char *) &msg[1],
708 if (NULL != notifier)
709 GNUNET_notification_context_broadcast (notifier,
712 GNUNET_MQ_discard (env);
716 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (unicast),
723 * Actually start the process for the given service.
725 * @param sl identifies service to start
726 * @param client that asked to start the service (may be NULL)
727 * @param request_id id of the request in response to which the process is
728 * being started. 0 if starting was not requested.
731 start_process (struct ServiceList *sl,
732 struct GNUNET_SERVICE_Client *client,
738 int is_simple_service;
739 struct ServiceListeningInfo *sli;
745 /* calculate listen socket list */
748 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
750 GNUNET_array_append (lsocks, ls,
751 GNUNET_NETWORK_get_fd (sli->listen_socket));
752 if (NULL != sli->accept_task)
754 GNUNET_SCHEDULER_cancel (sli->accept_task);
755 sli->accept_task = NULL;
759 GNUNET_array_append (lsocks,
763 GNUNET_array_append (lsocks,
768 /* obtain configuration */
770 GNUNET_CONFIGURATION_get_value_string (cfg,
774 loprefix = GNUNET_strdup (prefix_command);
776 loprefix = GNUNET_CONFIGURATION_expand_dollar (cfg,
779 GNUNET_CONFIGURATION_get_value_string (cfg,
785 options = GNUNET_CONFIGURATION_expand_dollar (cfg,
792 fin_options = GNUNET_strdup (final_option);
793 /* replace '{}' with service name */
794 while (NULL != (optpos = strstr (fin_options,
797 /* terminate string at opening parenthesis */
799 GNUNET_asprintf (&new_options,
804 GNUNET_free (fin_options);
805 fin_options = new_options;
809 /* combine "fin_options" with "options" */
811 GNUNET_asprintf (&options,
815 GNUNET_free (fin_options);
816 GNUNET_free (optpos);
820 /* only have "fin_options", use that */
821 options = fin_options;
824 options = GNUNET_CONFIGURATION_expand_dollar (cfg,
826 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg,
830 const char *service_type = NULL;
831 const char *choices[] = { "GNUNET", "SIMPLE", NULL };
833 is_simple_service = GNUNET_NO;
835 GNUNET_CONFIGURATION_get_value_choice (cfg,
840 (0 == strcasecmp (service_type, "SIMPLE")) )
841 is_simple_service = GNUNET_YES;
844 GNUNET_assert (NULL == sl->proc);
845 if (GNUNET_YES == is_simple_service)
847 /* A simple service will receive no GNUnet specific
848 command line options. */
849 binary = GNUNET_strdup (sl->binary);
850 binary = GNUNET_CONFIGURATION_expand_dollar (cfg, binary);
851 GNUNET_asprintf ("edbinary,
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855 "Starting simple service `%s' using binary `%s'\n",
856 sl->name, sl->binary);
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);
862 GNUNET_OS_start_process_s (sl->pipe_control,
863 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
872 /* actually start process */
873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
874 "Starting service `%s' using binary `%s' and configuration `%s'\n",
875 sl->name, sl->binary, sl->config);
876 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
877 GNUNET_asprintf ("edbinary,
881 if (GNUNET_YES == use_debug)
883 if (NULL == sl->config)
885 GNUNET_OS_start_process_s (sl->pipe_control,
886 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
895 GNUNET_OS_start_process_s (sl->pipe_control,
896 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
907 if (NULL == sl->config)
909 GNUNET_OS_start_process_s (sl->pipe_control,
910 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
918 GNUNET_OS_start_process_s (sl->pipe_control,
919 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
928 GNUNET_free (binary);
929 GNUNET_free (quotedbinary);
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,
947 GNUNET_ARM_SERVICE_STARTING,
950 signal_result (client,
953 GNUNET_ARM_RESULT_STARTING);
956 GNUNET_free (loprefix);
957 GNUNET_free (options);
958 GNUNET_array_grow (lsocks,
965 * Find the process with the given service
966 * name in the given list and return it.
968 * @param name which service entry to look up
969 * @return NULL if it was not found
971 static struct ServiceList *
972 find_service (const char *name)
974 struct ServiceList *sl;
979 if (0 == strcasecmp (sl->name, name))
988 * First connection has come to the listening socket associated with the service,
989 * create the service in order to relay the incoming connection to it
991 * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
994 accept_connection (void *cls)
996 struct ServiceListeningInfo *sli = cls;
997 struct ServiceList *sl = sli->sl;
999 sli->accept_task = NULL;
1000 GNUNET_assert (GNUNET_NO == in_shutdown);
1001 start_process (sl, NULL, 0);
1006 * Creating a listening socket for each of the service's addresses and
1007 * wait for the first incoming connection to it
1009 * @param sa address associated with the service
1010 * @param addr_len length of @a sa
1011 * @param sl service entry for the service in question
1014 create_listen_socket (struct sockaddr *sa,
1016 struct ServiceList *sl)
1019 struct GNUNET_NETWORK_Handle *sock;
1020 struct ServiceListeningInfo *sli;
1026 switch (sa->sa_family)
1029 sock = GNUNET_NETWORK_socket_create (PF_INET,
1034 sock = GNUNET_NETWORK_socket_create (PF_INET6,
1039 if (0 == strcmp (GNUNET_a2s (sa,
1041 "@")) /* Do not bind to blank UNIX path! */
1043 sock = GNUNET_NETWORK_socket_create (PF_UNIX,
1050 errno = EAFNOSUPPORT;
1055 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1056 _("Unable to create socket for service `%s': %s\n"),
1063 GNUNET_NETWORK_socket_setsockopt (sock,
1068 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1071 if ( (sa->sa_family == AF_INET6) &&
1073 GNUNET_NETWORK_socket_setsockopt (sock,
1078 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1082 if (AF_UNIX == sa->sa_family)
1083 GNUNET_NETWORK_unix_precheck ((struct sockaddr_un *) sa);
1086 GNUNET_NETWORK_socket_bind (sock,
1087 (const struct sockaddr *) sa,
1090 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1091 _("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
1096 GNUNET_break (GNUNET_OK ==
1097 GNUNET_NETWORK_socket_close (sock));
1102 if ((AF_UNIX == sa->sa_family)
1104 /* Permission settings are not required when abstract sockets are used */
1105 && ('\0' != ((const struct sockaddr_un *)sa)->sun_path[0])
1110 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1114 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1117 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sa)->sun_path,
1124 GNUNET_NETWORK_socket_listen (sock, 5))
1126 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1128 GNUNET_break (GNUNET_OK ==
1129 GNUNET_NETWORK_socket_close (sock));
1133 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1134 _("ARM now monitors connections to service `%s' at `%s'\n"),
1138 sli = GNUNET_new (struct ServiceListeningInfo);
1139 sli->service_addr = sa;
1140 sli->service_addr_len = addr_len;
1141 sli->listen_socket = sock;
1144 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1146 &accept_connection, sli);
1147 GNUNET_CONTAINER_DLL_insert (sl->listen_head,
1154 * Remove and free an entry in the service list. Listen sockets
1155 * must have already been cleaned up. Only to be called during shutdown.
1157 * @param sl entry to free
1160 free_service (struct ServiceList *sl)
1162 GNUNET_assert (GNUNET_YES == in_shutdown);
1163 GNUNET_CONTAINER_DLL_remove (running_head,
1166 GNUNET_assert (NULL == sl->listen_head);
1167 GNUNET_free_non_null (sl->config);
1168 GNUNET_free_non_null (sl->binary);
1169 GNUNET_free (sl->name);
1175 * Check START-message.
1177 * @param cls identification of the client
1178 * @param amsg the actual message
1179 * @return #GNUNET_OK to keep the connection open,
1180 * #GNUNET_SYSERR to close it (signal serious error)
1183 check_start (void *cls,
1184 const struct GNUNET_ARM_Message *amsg)
1187 const char *servicename;
1189 size = ntohs (amsg->header.size) - sizeof (struct GNUNET_ARM_Message);
1190 servicename = (const char *) &amsg[1];
1192 (servicename[size - 1] != '\0') )
1195 return GNUNET_SYSERR;
1202 * Handle START-message.
1204 * @param cls identification of the client
1205 * @param amsg the actual message
1208 handle_start (void *cls,
1209 const struct GNUNET_ARM_Message *amsg)
1211 struct GNUNET_SERVICE_Client *client = cls;
1212 const char *servicename;
1213 struct ServiceList *sl;
1214 uint64_t request_id;
1216 request_id = GNUNET_ntohll (amsg->request_id);
1217 servicename = (const char *) &amsg[1];
1218 GNUNET_SERVICE_client_continue (client);
1219 if (GNUNET_YES == in_shutdown)
1221 signal_result (client,
1224 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1227 sl = find_service (servicename);
1230 signal_result (client,
1233 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1236 sl->force_start = GNUNET_YES;
1237 if (NULL != sl->proc)
1239 signal_result (client,
1242 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
1252 * Start a shutdown sequence.
1254 * @param cls closure (refers to service)
1257 trigger_shutdown (void *cls)
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1260 "Triggering shutdown\n");
1261 GNUNET_SCHEDULER_shutdown ();
1266 * Check STOP-message.
1268 * @param cls identification of the client
1269 * @param amsg the actual message
1270 * @return #GNUNET_OK to keep the connection open,
1271 * #GNUNET_SYSERR to close it (signal serious error)
1274 check_stop (void *cls,
1275 const struct GNUNET_ARM_Message *amsg)
1278 const char *servicename;
1280 size = ntohs (amsg->header.size) - sizeof (struct GNUNET_ARM_Message);
1281 servicename = (const char *) &amsg[1];
1283 (servicename[size - 1] != '\0') )
1286 return GNUNET_SYSERR;
1293 * Handle STOP-message.
1295 * @param cls identification of the client
1296 * @param amsg the actual message
1299 handle_stop (void *cls,
1300 const struct GNUNET_ARM_Message *amsg)
1302 struct GNUNET_SERVICE_Client *client = cls;
1303 struct ServiceList *sl;
1304 const char *servicename;
1305 uint64_t request_id;
1307 request_id = GNUNET_ntohll (amsg->request_id);
1308 servicename = (const char *) &amsg[1];
1309 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1310 _("Preparing to stop `%s'\n"),
1312 GNUNET_SERVICE_client_continue (client);
1313 if (0 == strcasecmp (servicename,
1316 broadcast_status (servicename,
1317 GNUNET_ARM_SERVICE_STOPPING,
1319 signal_result (client,
1322 GNUNET_ARM_RESULT_STOPPING);
1323 GNUNET_SERVICE_client_persist (client);
1324 GNUNET_SCHEDULER_add_now (&trigger_shutdown,
1328 sl = find_service (servicename);
1331 signal_result (client,
1334 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1337 sl->force_start = GNUNET_NO;
1338 if (GNUNET_YES == in_shutdown)
1340 /* shutdown in progress */
1341 signal_result (client,
1344 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1347 if (NULL != sl->killing_client)
1349 /* killing already in progress */
1350 signal_result (client,
1353 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
1356 if (NULL == sl->proc)
1358 /* process is down */
1359 signal_result (client,
1362 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
1365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1366 "Sending kill signal to service `%s', waiting for process to die.\n",
1368 broadcast_status (servicename,
1369 GNUNET_ARM_SERVICE_STOPPING,
1371 /* no signal_start - only when it's STOPPED */
1372 sl->killed_at = GNUNET_TIME_absolute_get ();
1373 if (0 != GNUNET_OS_process_kill (sl->proc,
1375 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1377 sl->killing_client = client;
1378 sl->killing_client_request_id = request_id;
1383 * Handle LIST-message.
1385 * @param cls identification of the client
1386 * @param message the actual message
1389 handle_list (void *cls,
1390 const struct GNUNET_ARM_Message *request)
1392 struct GNUNET_SERVICE_Client *client = cls;
1393 struct GNUNET_MQ_Envelope *env;
1394 struct GNUNET_ARM_ListResultMessage *msg;
1395 size_t string_list_size;
1396 struct ServiceList *sl;
1400 GNUNET_break (0 == ntohl (request->reserved));
1402 string_list_size = 0;
1404 /* first count the running processes get their name's size */
1405 for (sl = running_head; NULL != sl; sl = sl->next)
1407 if (NULL != sl->proc)
1409 string_list_size += strlen (sl->name);
1410 string_list_size += strlen (sl->binary);
1411 string_list_size += 4;
1416 env = GNUNET_MQ_msg_extra (msg,
1418 GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT);
1419 msg->arm_msg.request_id = request->request_id;
1420 msg->count = htons (count);
1422 pos = (char *) &msg[1];
1423 for (sl = running_head; NULL != sl; sl = sl->next)
1425 if (NULL != sl->proc)
1427 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
1428 GNUNET_snprintf (pos,
1436 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1438 GNUNET_SERVICE_client_continue (client);
1443 * Handle TEST-message by sending back TEST.
1445 * @param cls identification of the client
1446 * @param message the actual message
1449 handle_test (void *cls,
1450 const struct GNUNET_MessageHeader *message)
1452 struct GNUNET_SERVICE_Client *client = cls;
1453 struct GNUNET_MQ_Envelope *env;
1454 struct GNUNET_MessageHeader *msg;
1456 env = GNUNET_MQ_msg (msg,
1457 GNUNET_MESSAGE_TYPE_ARM_TEST);
1458 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1460 GNUNET_SERVICE_client_continue (client);
1465 * We are done with everything. Stop remaining
1466 * tasks, signal handler and the server.
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "Last shutdown phase\n");
1473 if (NULL != notifier)
1475 GNUNET_notification_context_destroy (notifier);
1478 if (NULL != service)
1480 GNUNET_SERVICE_shutdown (service);
1483 if (NULL != child_death_task)
1485 GNUNET_SCHEDULER_cancel (child_death_task);
1486 child_death_task = NULL;
1492 * Count how many services are still active.
1494 * @param running_head list of services
1495 * @return number of active services found
1498 list_count (struct ServiceList *running_head)
1500 struct ServiceList *i;
1503 for (res = 0, i = running_head; i; i = i->next, res++)
1504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 * Task run for shutdown.
1514 * @param cls closure, NULL if we need to self-restart
1517 shutdown_task (void *cls)
1519 struct ServiceList *pos;
1520 struct ServiceList *nxt;
1521 struct ServiceListeningInfo *sli;
1523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1524 "First shutdown phase\n");
1525 if (NULL != child_restart_task)
1527 GNUNET_SCHEDULER_cancel (child_restart_task);
1528 child_restart_task = NULL;
1530 in_shutdown = GNUNET_YES;
1531 /* first, stop listening */
1532 for (pos = running_head; NULL != pos; pos = pos->next)
1534 while (NULL != (sli = pos->listen_head))
1536 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
1539 if (NULL != sli->accept_task)
1541 GNUNET_SCHEDULER_cancel (sli->accept_task);
1542 sli->accept_task = NULL;
1544 GNUNET_break (GNUNET_OK ==
1545 GNUNET_NETWORK_socket_close (sli->listen_socket));
1546 GNUNET_free (sli->service_addr);
1550 /* then, shutdown all existing service processes */
1552 while (NULL != (pos = nxt))
1555 if (NULL != pos->proc)
1557 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1558 "Stopping service `%s'\n",
1560 pos->killed_at = GNUNET_TIME_absolute_get ();
1561 if (0 != GNUNET_OS_process_kill (pos->proc,
1563 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1571 /* finally, should all service processes be already gone, terminate for real */
1572 if (NULL == running_head)
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1576 "Delaying shutdown, have %u childs still running\n",
1577 list_count (running_head));
1582 * Task run whenever it is time to restart a child that died.
1584 * @param cls closure, always NULL
1587 delayed_restart_task (void *cls)
1590 struct ServiceList *sl;
1591 struct GNUNET_TIME_Relative lowestRestartDelay;
1592 struct ServiceListeningInfo *sli;
1594 child_restart_task = NULL;
1595 GNUNET_assert (GNUNET_NO == in_shutdown);
1596 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1598 /* check for services that need to be restarted due to
1599 * configuration changes or because the last restart failed */
1600 for (sl = running_head; NULL != sl; sl = sl->next)
1602 if (NULL != sl->proc)
1604 /* service is currently not running */
1605 if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
1607 /* restart is now allowed */
1608 if (sl->force_start)
1610 /* process should run by default, start immediately */
1611 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1612 _("Restarting service `%s'.\n"),
1620 /* process is run on-demand, ensure it is re-started if there is demand */
1621 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1622 if (NULL == sli->accept_task)
1624 /* accept was actually paused, so start it again */
1626 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1635 /* update calculation for earliest time to reactivate a service */
1636 lowestRestartDelay =
1637 GNUNET_TIME_relative_min (lowestRestartDelay,
1638 GNUNET_TIME_absolute_get_remaining
1642 if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1645 "Will restart process in %s\n",
1646 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay,
1648 child_restart_task =
1649 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1650 GNUNET_SCHEDULER_PRIORITY_IDLE,
1651 &delayed_restart_task,
1658 * Task triggered whenever we receive a SIGCHLD (child
1661 * @param cls closure, NULL if we need to self-restart
1664 maint_child_death (void *cls)
1666 struct ServiceList *pos;
1667 struct ServiceList *next;
1668 struct ServiceListeningInfo *sli;
1669 const char *statstr;
1673 enum GNUNET_OS_ProcessStatusType statusType;
1674 unsigned long statusCode;
1675 const struct GNUNET_DISK_FileHandle *pr;
1677 pr = GNUNET_DISK_pipe_handle (sigpipe,
1678 GNUNET_DISK_PIPE_END_READ);
1679 child_death_task = NULL;
1680 /* consume the signal */
1681 GNUNET_break (0 < GNUNET_DISK_file_read (pr,
1685 /* check for services that died (WAITPID) */
1686 next = running_head;
1687 while (NULL != (pos = next))
1691 if (NULL == pos->proc)
1693 if (GNUNET_YES == in_shutdown)
1698 if (NULL != wait_file)
1700 /* need to use 'wait4()' to obtain and log performance data */
1705 pid = GNUNET_OS_process_get_pid (pos->proc);
1711 continue; /* no process done */
1712 if (WIFEXITED (status))
1714 statusType = GNUNET_OS_PROCESS_EXITED;
1715 statusCode = WEXITSTATUS (status);
1717 else if (WIFSIGNALED (status))
1719 statusType = GNUNET_OS_PROCESS_SIGNALED;
1720 statusCode = WTERMSIG (status);
1722 else if (WIFSTOPPED (status))
1724 statusType = GNUNET_OS_PROCESS_SIGNALED;
1725 statusCode = WSTOPSIG (status);
1728 else if (WIFCONTINUED (status))
1730 statusType = GNUNET_OS_PROCESS_RUNNING;
1736 statusType = GNUNET_OS_PROCESS_UNKNOWN;
1739 if ( (GNUNET_OS_PROCESS_EXITED == statusType) ||
1740 (GNUNET_OS_PROCESS_SIGNALED == statusType) )
1742 double utime = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 10e6);
1743 double stime = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 10e6);
1745 "%s(%u) %.3f %.3f %llu %llu %llu %llu %llu\n",
1750 (unsigned long long) ru.ru_maxrss,
1751 (unsigned long long) ru.ru_inblock,
1752 (unsigned long long) ru.ru_oublock,
1753 (unsigned long long) ru.ru_nvcsw,
1754 (unsigned long long) ru.ru_nivcsw);
1757 else /* continue with JUST this "if" as "else" (intentionally no brackets!) */
1759 if ( (GNUNET_SYSERR ==
1761 GNUNET_OS_process_status (pos->proc,
1764 (ret == GNUNET_NO) ||
1765 (statusType == GNUNET_OS_PROCESS_STOPPED) ||
1766 (statusType == GNUNET_OS_PROCESS_UNKNOWN) ||
1767 (statusType == GNUNET_OS_PROCESS_RUNNING) )
1770 if (statusType == GNUNET_OS_PROCESS_EXITED)
1772 statstr = _( /* process termination method */ "exit");
1773 statcode = statusCode;
1775 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1777 statstr = _( /* process termination method */ "signal");
1778 statcode = statusCode;
1782 statstr = _( /* process termination method */ "unknown");
1785 if (0 != pos->killed_at.abs_value_us)
1787 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1788 _("Service `%s' took %s to terminate\n"),
1790 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at),
1793 GNUNET_OS_process_destroy (pos->proc);
1795 broadcast_status (pos->name,
1796 GNUNET_ARM_SERVICE_STOPPED,
1798 if (NULL != pos->killing_client)
1800 signal_result (pos->killing_client, pos->name,
1801 pos->killing_client_request_id,
1802 GNUNET_ARM_RESULT_STOPPED);
1803 pos->killing_client = NULL;
1804 pos->killing_client_request_id = 0;
1806 if (GNUNET_YES != in_shutdown)
1808 if ( (statusType == GNUNET_OS_PROCESS_EXITED) &&
1811 /* process terminated normally, allow restart at any time */
1812 pos->restart_at.abs_value_us = 0;
1813 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1814 _("Service `%s' terminated normally, will restart at any time\n"),
1816 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1817 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1819 GNUNET_break (NULL == sli->accept_task);
1821 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1829 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1830 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1834 GNUNET_STRINGS_relative_time_to_string (pos->backoff,
1837 /* Reduce backoff based on runtime of the process,
1838 so that there is a cool-down if a process actually
1839 runs for a while. */
1840 struct GNUNET_TIME_Relative runtime;
1841 unsigned int minutes;
1843 runtime = GNUNET_TIME_absolute_get_duration (pos->restart_at);
1844 minutes = runtime.rel_value_us / GNUNET_TIME_UNIT_MINUTES.rel_value_us;
1846 pos->backoff = GNUNET_TIME_UNIT_ZERO;
1848 pos->backoff.rel_value_us <<= minutes;
1850 /* schedule restart */
1851 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1852 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1853 if (NULL != child_restart_task)
1854 GNUNET_SCHEDULER_cancel (child_restart_task);
1856 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1857 &delayed_restart_task,
1866 child_death_task = GNUNET_SCHEDULER_add_read_file (
1867 GNUNET_TIME_UNIT_FOREVER_REL,
1869 &maint_child_death, NULL);
1870 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1872 else if (GNUNET_YES == in_shutdown)
1873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1874 "Delaying shutdown after child's death, still have %u children\n",
1875 list_count (running_head));
1881 * Signal handler called for SIGCHLD. Triggers the
1882 * respective handler by writing to the trigger pipe.
1885 sighandler_child_death ()
1888 int old_errno = errno; /* back-up errno */
1891 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
1892 GNUNET_DISK_PIPE_END_WRITE),
1895 errno = old_errno; /* restore errno */
1900 * Setup our service record for the given section in the configuration file
1901 * (assuming the section is for a service).
1904 * @param section a section in the configuration file
1905 * @return #GNUNET_OK (continue)
1908 setup_service (void *cls,
1909 const char *section)
1911 struct ServiceList *sl;
1915 struct sockaddr **addrs;
1916 socklen_t *addr_lens;
1919 if (0 == strcasecmp (section,
1923 GNUNET_CONFIGURATION_get_value_string (cfg,
1928 /* not a service section */
1932 GNUNET_CONFIGURATION_have_value (cfg,
1936 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1940 if (GNUNET_NO == start_user)
1942 GNUNET_free (binary);
1943 return; /* user service, and we don't deal with those */
1948 if (GNUNET_NO == start_system)
1950 GNUNET_free (binary);
1951 return; /* system service, and we don't deal with those */
1954 sl = find_service (section);
1957 /* got the same section twice!? */
1959 GNUNET_free (binary);
1964 GNUNET_CONFIGURATION_get_value_filename (cfg,
1969 GNUNET_CONFIGURATION_get_value_filename (cfg,
1973 (0 != STAT (config, &sbuf)))
1977 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1980 GNUNET_free (config);
1984 sl = GNUNET_new (struct ServiceList);
1985 sl->name = GNUNET_strdup (section);
1986 sl->binary = binary;
1987 sl->config = config;
1988 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1989 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1991 sl->pipe_control = GNUNET_YES;
1993 if (GNUNET_CONFIGURATION_have_value (cfg,
1996 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2000 GNUNET_CONTAINER_DLL_insert (running_head,
2004 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2008 sl->force_start = GNUNET_YES;
2010 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2018 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2023 if (0 >= (ret = get_server_addresses (section,
2028 /* this will free (or capture) addrs[i] */
2029 for (unsigned int i = 0; i < ret; i++)
2030 create_listen_socket (addrs[i],
2033 GNUNET_free (addrs);
2034 GNUNET_free (addr_lens);
2039 * A client connected, mark as a monitoring client.
2041 * @param cls closure
2042 * @param client identification of the client
2043 * @param mq queue to talk to @a client
2047 client_connect_cb (void *cls,
2048 struct GNUNET_SERVICE_Client *client,
2049 struct GNUNET_MQ_Handle *mq)
2051 /* All clients are considered to be of the "monitor" kind
2052 * (that is, they don't affect ARM shutdown).
2054 GNUNET_SERVICE_client_mark_monitor (client);
2060 * A client disconnected, clean up associated state.
2062 * @param cls closure
2063 * @param client identification of the client
2064 * @param app_ctx must match @a client
2067 client_disconnect_cb (void *cls,
2068 struct GNUNET_SERVICE_Client *client,
2071 struct ServiceList *sl;
2073 GNUNET_assert (client == app_ctx);
2075 for (sl = running_head; NULL != sl; sl = sl->next)
2076 if (sl->killing_client == client)
2077 sl->killing_client = NULL;
2082 * Handle MONITOR-message.
2084 * @param cls identification of the client
2085 * @param message the actual message
2086 * @return #GNUNET_OK to keep the connection open,
2087 * #GNUNET_SYSERR to close it (signal serious error)
2090 handle_monitor (void *cls,
2091 const struct GNUNET_MessageHeader *message)
2093 struct GNUNET_SERVICE_Client *client = cls;
2095 /* FIXME: might want to start by letting monitor know about
2096 services that are already running */
2097 /* Removal is handled by the server implementation, internally. */
2098 GNUNET_notification_context_add (notifier,
2099 GNUNET_SERVICE_client_get_mq (client));
2100 broadcast_status ("arm",
2101 GNUNET_ARM_SERVICE_MONITORING_STARTED,
2103 GNUNET_SERVICE_client_continue (client);
2108 * Process arm requests.
2110 * @param cls closure
2111 * @param serv the initialized service
2112 * @param c configuration to use
2116 const struct GNUNET_CONFIGURATION_Handle *c,
2117 struct GNUNET_SERVICE_Handle *serv)
2119 struct ServiceList *sl;
2123 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
2126 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2127 GNUNET_DISK_pipe_handle (sigpipe,
2128 GNUNET_DISK_PIPE_END_READ),
2133 GNUNET_CONFIGURATION_get_value_filename (cfg,
2135 "RESOURCE_DIAGNOSTICS",
2138 wait_file = fopen (wait_filename,
2140 if (NULL == wait_file)
2142 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2149 GNUNET_CONFIGURATION_get_value_string (cfg,
2153 prefix_command = GNUNET_strdup ("");
2155 prefix_command = GNUNET_CONFIGURATION_expand_dollar (cfg,
2158 GNUNET_CONFIGURATION_get_value_string (cfg,
2162 final_option = GNUNET_strdup ("");
2164 final_option = GNUNET_CONFIGURATION_expand_dollar (cfg,
2167 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2171 GNUNET_break (GNUNET_YES == start_user);
2172 start_system = GNUNET_NO;
2175 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2179 GNUNET_break (GNUNET_YES == start_system);
2180 start_user = GNUNET_NO;
2182 GNUNET_CONFIGURATION_iterate_sections (cfg,
2186 /* start default services... */
2187 for (sl = running_head; NULL != sl; sl = sl->next)
2188 if (GNUNET_YES == sl->force_start)
2192 notifier = GNUNET_notification_context_create (MAX_NOTIFY_QUEUE);
2197 * The main function for the arm service.
2199 * @param argc number of arguments from the command line
2200 * @param argv command line arguments
2201 * @return 0 ok, 1 on error
2208 struct GNUNET_SIGNAL_Context *shc_chld;
2209 struct GNUNET_MQ_MessageHandler handlers[] = {
2210 GNUNET_MQ_hd_var_size (start,
2211 GNUNET_MESSAGE_TYPE_ARM_START,
2212 struct GNUNET_ARM_Message,
2214 GNUNET_MQ_hd_var_size (stop,
2215 GNUNET_MESSAGE_TYPE_ARM_STOP,
2216 struct GNUNET_ARM_Message,
2218 GNUNET_MQ_hd_fixed_size (monitor,
2219 GNUNET_MESSAGE_TYPE_ARM_MONITOR,
2220 struct GNUNET_MessageHeader,
2222 GNUNET_MQ_hd_fixed_size (list,
2223 GNUNET_MESSAGE_TYPE_ARM_LIST,
2224 struct GNUNET_ARM_Message,
2226 GNUNET_MQ_hd_fixed_size (test,
2227 GNUNET_MESSAGE_TYPE_ARM_TEST,
2228 struct GNUNET_MessageHeader,
2230 GNUNET_MQ_handler_end ()
2233 sigpipe = GNUNET_DISK_pipe (GNUNET_NO,
2237 GNUNET_assert (NULL != sigpipe);
2239 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD,
2240 &sighandler_child_death);
2241 ret = GNUNET_SERVICE_run_ (argc,
2244 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN,
2247 &client_disconnect_cb,
2251 if (NULL != wait_file)
2256 if (NULL != wait_filename)
2258 GNUNET_free (wait_filename);
2259 wait_filename = NULL;
2262 GNUNET_SIGNAL_handler_uninstall (shc_chld);
2264 GNUNET_DISK_pipe_close (sigpipe);
2270 #if defined(LINUX) && defined(__GLIBC__)
2274 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
2276 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
2278 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
2279 mallopt (M_TOP_PAD, 1 * 1024);
2285 /* end of gnunet-service-arm.c */