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_IN_SIN_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 (optpos);
819 /* only have "fin_options", use that */
820 options = fin_options;
823 options = GNUNET_CONFIGURATION_expand_dollar (cfg,
825 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg,
829 const char *service_type = NULL;
830 const char *choices[] = { "GNUNET", "SIMPLE", NULL };
832 is_simple_service = GNUNET_NO;
834 GNUNET_CONFIGURATION_get_value_choice (cfg,
839 (0 == strcasecmp (service_type, "SIMPLE")) )
840 is_simple_service = GNUNET_YES;
843 GNUNET_assert (NULL == sl->proc);
844 if (GNUNET_YES == is_simple_service)
846 /* A simple service will receive no GNUnet specific
847 command line options. */
848 binary = GNUNET_strdup (sl->binary);
849 binary = GNUNET_CONFIGURATION_expand_dollar (cfg, binary);
850 GNUNET_asprintf ("edbinary,
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "Starting simple service `%s' using binary `%s'\n",
855 sl->name, sl->binary);
856 /* FIXME: dollar expansion should only be done outside
857 * of ''-quoted strings, escaping should be considered. */
859 options = GNUNET_CONFIGURATION_expand_dollar (cfg, options);
861 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",
874 sl->name, sl->binary, sl->config);
875 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
876 GNUNET_asprintf ("edbinary,
880 if (GNUNET_YES == use_debug)
882 if (NULL == sl->config)
884 GNUNET_OS_start_process_s (sl->pipe_control,
885 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
894 GNUNET_OS_start_process_s (sl->pipe_control,
895 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
906 if (NULL == sl->config)
908 GNUNET_OS_start_process_s (sl->pipe_control,
909 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
917 GNUNET_OS_start_process_s (sl->pipe_control,
918 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
927 GNUNET_free (binary);
928 GNUNET_free (quotedbinary);
929 if (NULL == sl->proc)
931 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
932 _("Failed to start service `%s'\n"),
935 signal_result (client,
938 GNUNET_ARM_RESULT_START_FAILED);
942 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
943 _("Starting service `%s'\n"),
945 broadcast_status (sl->name,
946 GNUNET_ARM_SERVICE_STARTING,
949 signal_result (client,
952 GNUNET_ARM_RESULT_STARTING);
955 GNUNET_free (loprefix);
956 GNUNET_free (options);
957 GNUNET_array_grow (lsocks,
964 * Find the process with the given service
965 * name in the given list and return it.
967 * @param name which service entry to look up
968 * @return NULL if it was not found
970 static struct ServiceList *
971 find_service (const char *name)
973 struct ServiceList *sl;
978 if (0 == strcasecmp (sl->name, name))
987 * First connection has come to the listening socket associated with the service,
988 * create the service in order to relay the incoming connection to it
990 * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
993 accept_connection (void *cls)
995 struct ServiceListeningInfo *sli = cls;
996 struct ServiceList *sl = sli->sl;
998 sli->accept_task = NULL;
999 GNUNET_assert (GNUNET_NO == in_shutdown);
1000 start_process (sl, NULL, 0);
1005 * Creating a listening socket for each of the service's addresses and
1006 * wait for the first incoming connection to it
1008 * @param sa address associated with the service
1009 * @param addr_len length of @a sa
1010 * @param sl service entry for the service in question
1013 create_listen_socket (struct sockaddr *sa,
1015 struct ServiceList *sl)
1018 struct GNUNET_NETWORK_Handle *sock;
1019 struct ServiceListeningInfo *sli;
1025 switch (sa->sa_family)
1028 sock = GNUNET_NETWORK_socket_create (PF_INET,
1033 sock = GNUNET_NETWORK_socket_create (PF_INET6,
1038 if (0 == strcmp (GNUNET_a2s (sa,
1040 "@")) /* Do not bind to blank UNIX path! */
1042 sock = GNUNET_NETWORK_socket_create (PF_UNIX,
1049 errno = EAFNOSUPPORT;
1054 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1055 _("Unable to create socket for service `%s': %s\n"),
1062 GNUNET_NETWORK_socket_setsockopt (sock,
1067 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1070 if ( (sa->sa_family == AF_INET6) &&
1072 GNUNET_NETWORK_socket_setsockopt (sock,
1077 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1081 if (AF_UNIX == sa->sa_family)
1082 GNUNET_NETWORK_unix_precheck ((struct sockaddr_un *) sa);
1085 GNUNET_NETWORK_socket_bind (sock,
1086 (const struct sockaddr *) sa,
1089 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1090 _("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
1095 GNUNET_break (GNUNET_OK ==
1096 GNUNET_NETWORK_socket_close (sock));
1101 if ((AF_UNIX == sa->sa_family)
1103 /* Permission settings are not required when abstract sockets are used */
1104 && ('\0' != ((const struct sockaddr_un *)sa)->sun_path[0])
1109 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1113 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1116 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sa)->sun_path,
1123 GNUNET_NETWORK_socket_listen (sock, 5))
1125 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1127 GNUNET_break (GNUNET_OK ==
1128 GNUNET_NETWORK_socket_close (sock));
1132 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1133 _("ARM now monitors connections to service `%s' at `%s'\n"),
1137 sli = GNUNET_new (struct ServiceListeningInfo);
1138 sli->service_addr = sa;
1139 sli->service_addr_len = addr_len;
1140 sli->listen_socket = sock;
1143 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1145 &accept_connection, sli);
1146 GNUNET_CONTAINER_DLL_insert (sl->listen_head,
1153 * Remove and free an entry in the service list. Listen sockets
1154 * must have already been cleaned up. Only to be called during shutdown.
1156 * @param sl entry to free
1159 free_service (struct ServiceList *sl)
1161 GNUNET_assert (GNUNET_YES == in_shutdown);
1162 GNUNET_CONTAINER_DLL_remove (running_head,
1165 GNUNET_assert (NULL == sl->listen_head);
1166 GNUNET_free_non_null (sl->config);
1167 GNUNET_free_non_null (sl->binary);
1168 GNUNET_free (sl->name);
1174 * Check START-message.
1176 * @param cls identification of the client
1177 * @param amsg the actual message
1178 * @return #GNUNET_OK to keep the connection open,
1179 * #GNUNET_SYSERR to close it (signal serious error)
1182 check_start (void *cls,
1183 const struct GNUNET_ARM_Message *amsg)
1186 const char *servicename;
1188 size = ntohs (amsg->header.size) - sizeof (struct GNUNET_ARM_Message);
1189 servicename = (const char *) &amsg[1];
1191 (servicename[size - 1] != '\0') )
1194 return GNUNET_SYSERR;
1201 * Handle START-message.
1203 * @param cls identification of the client
1204 * @param amsg the actual message
1207 handle_start (void *cls,
1208 const struct GNUNET_ARM_Message *amsg)
1210 struct GNUNET_SERVICE_Client *client = cls;
1211 const char *servicename;
1212 struct ServiceList *sl;
1213 uint64_t request_id;
1215 request_id = GNUNET_ntohll (amsg->request_id);
1216 servicename = (const char *) &amsg[1];
1217 GNUNET_SERVICE_client_continue (client);
1218 if (GNUNET_YES == in_shutdown)
1220 signal_result (client,
1223 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1226 sl = find_service (servicename);
1229 signal_result (client,
1232 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1235 sl->force_start = GNUNET_YES;
1236 if (NULL != sl->proc)
1238 signal_result (client,
1241 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
1251 * Start a shutdown sequence.
1253 * @param cls closure (refers to service)
1256 trigger_shutdown (void *cls)
1258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1259 "Triggering shutdown\n");
1260 GNUNET_SCHEDULER_shutdown ();
1265 * Check STOP-message.
1267 * @param cls identification of the client
1268 * @param amsg the actual message
1269 * @return #GNUNET_OK to keep the connection open,
1270 * #GNUNET_SYSERR to close it (signal serious error)
1273 check_stop (void *cls,
1274 const struct GNUNET_ARM_Message *amsg)
1277 const char *servicename;
1279 size = ntohs (amsg->header.size) - sizeof (struct GNUNET_ARM_Message);
1280 servicename = (const char *) &amsg[1];
1282 (servicename[size - 1] != '\0') )
1285 return GNUNET_SYSERR;
1292 * Handle STOP-message.
1294 * @param cls identification of the client
1295 * @param amsg the actual message
1298 handle_stop (void *cls,
1299 const struct GNUNET_ARM_Message *amsg)
1301 struct GNUNET_SERVICE_Client *client = cls;
1302 struct ServiceList *sl;
1303 const char *servicename;
1304 uint64_t request_id;
1306 request_id = GNUNET_ntohll (amsg->request_id);
1307 servicename = (const char *) &amsg[1];
1308 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1309 _("Preparing to stop `%s'\n"),
1311 GNUNET_SERVICE_client_continue (client);
1312 if (0 == strcasecmp (servicename,
1315 broadcast_status (servicename,
1316 GNUNET_ARM_SERVICE_STOPPING,
1318 signal_result (client,
1321 GNUNET_ARM_RESULT_STOPPING);
1322 GNUNET_SERVICE_client_persist (client);
1323 GNUNET_SCHEDULER_add_now (&trigger_shutdown,
1327 sl = find_service (servicename);
1330 signal_result (client,
1333 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1336 sl->force_start = GNUNET_NO;
1337 if (GNUNET_YES == in_shutdown)
1339 /* shutdown in progress */
1340 signal_result (client,
1343 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1346 if (NULL != sl->killing_client)
1348 /* killing already in progress */
1349 signal_result (client,
1352 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
1355 if (NULL == sl->proc)
1357 /* process is down */
1358 signal_result (client,
1361 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
1364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1365 "Sending kill signal to service `%s', waiting for process to die.\n",
1367 broadcast_status (servicename,
1368 GNUNET_ARM_SERVICE_STOPPING,
1370 /* no signal_start - only when it's STOPPED */
1371 sl->killed_at = GNUNET_TIME_absolute_get ();
1372 if (0 != GNUNET_OS_process_kill (sl->proc,
1374 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1376 sl->killing_client = client;
1377 sl->killing_client_request_id = request_id;
1382 * Handle LIST-message.
1384 * @param cls identification of the client
1385 * @param message the actual message
1388 handle_list (void *cls,
1389 const struct GNUNET_ARM_Message *request)
1391 struct GNUNET_SERVICE_Client *client = cls;
1392 struct GNUNET_MQ_Envelope *env;
1393 struct GNUNET_ARM_ListResultMessage *msg;
1394 size_t string_list_size;
1395 struct ServiceList *sl;
1399 GNUNET_break (0 == ntohl (request->reserved));
1401 string_list_size = 0;
1403 /* first count the running processes get their name's size */
1404 for (sl = running_head; NULL != sl; sl = sl->next)
1406 if (NULL != sl->proc)
1408 string_list_size += strlen (sl->name);
1409 string_list_size += strlen (sl->binary);
1410 string_list_size += 4;
1415 env = GNUNET_MQ_msg_extra (msg,
1417 GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT);
1418 msg->arm_msg.request_id = request->request_id;
1419 msg->count = htons (count);
1421 pos = (char *) &msg[1];
1422 for (sl = running_head; NULL != sl; sl = sl->next)
1424 if (NULL != sl->proc)
1426 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
1427 GNUNET_snprintf (pos,
1435 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1437 GNUNET_SERVICE_client_continue (client);
1442 * Handle TEST-message by sending back TEST.
1444 * @param cls identification of the client
1445 * @param message the actual message
1448 handle_test (void *cls,
1449 const struct GNUNET_MessageHeader *message)
1451 struct GNUNET_SERVICE_Client *client = cls;
1452 struct GNUNET_MQ_Envelope *env;
1453 struct GNUNET_MessageHeader *msg;
1455 env = GNUNET_MQ_msg (msg,
1456 GNUNET_MESSAGE_TYPE_ARM_TEST);
1457 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1459 GNUNET_SERVICE_client_continue (client);
1464 * We are done with everything. Stop remaining
1465 * tasks, signal handler and the server.
1470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1471 "Last shutdown phase\n");
1472 if (NULL != notifier)
1474 GNUNET_notification_context_destroy (notifier);
1477 if (NULL != service)
1479 GNUNET_SERVICE_shutdown (service);
1482 if (NULL != child_death_task)
1484 GNUNET_SCHEDULER_cancel (child_death_task);
1485 child_death_task = NULL;
1491 * Count how many services are still active.
1493 * @param running_head list of services
1494 * @return number of active services found
1497 list_count (struct ServiceList *running_head)
1499 struct ServiceList *i;
1502 for (res = 0, i = running_head; i; i = i->next, res++)
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 * Task run for shutdown.
1513 * @param cls closure, NULL if we need to self-restart
1516 shutdown_task (void *cls)
1518 struct ServiceList *pos;
1519 struct ServiceList *nxt;
1520 struct ServiceListeningInfo *sli;
1522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1523 "First shutdown phase\n");
1524 if (NULL != child_restart_task)
1526 GNUNET_SCHEDULER_cancel (child_restart_task);
1527 child_restart_task = NULL;
1529 in_shutdown = GNUNET_YES;
1530 /* first, stop listening */
1531 for (pos = running_head; NULL != pos; pos = pos->next)
1533 while (NULL != (sli = pos->listen_head))
1535 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
1538 if (NULL != sli->accept_task)
1540 GNUNET_SCHEDULER_cancel (sli->accept_task);
1541 sli->accept_task = NULL;
1543 GNUNET_break (GNUNET_OK ==
1544 GNUNET_NETWORK_socket_close (sli->listen_socket));
1545 GNUNET_free (sli->service_addr);
1549 /* then, shutdown all existing service processes */
1551 while (NULL != (pos = nxt))
1554 if (NULL != pos->proc)
1556 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1557 "Stopping service `%s'\n",
1559 pos->killed_at = GNUNET_TIME_absolute_get ();
1560 if (0 != GNUNET_OS_process_kill (pos->proc,
1562 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1570 /* finally, should all service processes be already gone, terminate for real */
1571 if (NULL == running_head)
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1575 "Delaying shutdown, have %u childs still running\n",
1576 list_count (running_head));
1581 * Task run whenever it is time to restart a child that died.
1583 * @param cls closure, always NULL
1586 delayed_restart_task (void *cls)
1589 struct ServiceList *sl;
1590 struct GNUNET_TIME_Relative lowestRestartDelay;
1591 struct ServiceListeningInfo *sli;
1593 child_restart_task = NULL;
1594 GNUNET_assert (GNUNET_NO == in_shutdown);
1595 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1597 /* check for services that need to be restarted due to
1598 * configuration changes or because the last restart failed */
1599 for (sl = running_head; NULL != sl; sl = sl->next)
1601 if (NULL != sl->proc)
1603 /* service is currently not running */
1604 if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
1606 /* restart is now allowed */
1607 if (sl->force_start)
1609 /* process should run by default, start immediately */
1610 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1611 _("Restarting service `%s'.\n"),
1619 /* process is run on-demand, ensure it is re-started if there is demand */
1620 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1621 if (NULL == sli->accept_task)
1623 /* accept was actually paused, so start it again */
1625 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1634 /* update calculation for earliest time to reactivate a service */
1635 lowestRestartDelay =
1636 GNUNET_TIME_relative_min (lowestRestartDelay,
1637 GNUNET_TIME_absolute_get_remaining
1641 if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1644 "Will restart process in %s\n",
1645 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay,
1647 child_restart_task =
1648 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1649 GNUNET_SCHEDULER_PRIORITY_IDLE,
1650 &delayed_restart_task,
1657 * Task triggered whenever we receive a SIGCHLD (child
1660 * @param cls closure, NULL if we need to self-restart
1663 maint_child_death (void *cls)
1665 struct ServiceList *pos;
1666 struct ServiceList *next;
1667 struct ServiceListeningInfo *sli;
1668 const char *statstr;
1672 enum GNUNET_OS_ProcessStatusType statusType;
1673 unsigned long statusCode;
1674 const struct GNUNET_DISK_FileHandle *pr;
1676 pr = GNUNET_DISK_pipe_handle (sigpipe,
1677 GNUNET_DISK_PIPE_END_READ);
1678 child_death_task = NULL;
1679 /* consume the signal */
1680 GNUNET_break (0 < GNUNET_DISK_file_read (pr,
1684 /* check for services that died (WAITPID) */
1685 next = running_head;
1686 while (NULL != (pos = next))
1690 if (NULL == pos->proc)
1692 if (GNUNET_YES == in_shutdown)
1697 if (NULL != wait_file)
1699 /* need to use 'wait4()' to obtain and log performance data */
1704 pid = GNUNET_OS_process_get_pid (pos->proc);
1710 continue; /* no process done */
1711 if (WIFEXITED (status))
1713 statusType = GNUNET_OS_PROCESS_EXITED;
1714 statusCode = WEXITSTATUS (status);
1716 else if (WIFSIGNALED (status))
1718 statusType = GNUNET_OS_PROCESS_SIGNALED;
1719 statusCode = WTERMSIG (status);
1721 else if (WIFSTOPPED (status))
1723 statusType = GNUNET_OS_PROCESS_SIGNALED;
1724 statusCode = WSTOPSIG (status);
1727 else if (WIFCONTINUED (status))
1729 statusType = GNUNET_OS_PROCESS_RUNNING;
1735 statusType = GNUNET_OS_PROCESS_UNKNOWN;
1738 if ( (GNUNET_OS_PROCESS_EXITED == statusType) ||
1739 (GNUNET_OS_PROCESS_SIGNALED == statusType) )
1741 double utime = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 10e6);
1742 double stime = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 10e6);
1744 "%s(%u) %.3f %.3f %llu %llu %llu %llu %llu\n",
1749 (unsigned long long) ru.ru_maxrss,
1750 (unsigned long long) ru.ru_inblock,
1751 (unsigned long long) ru.ru_oublock,
1752 (unsigned long long) ru.ru_nvcsw,
1753 (unsigned long long) ru.ru_nivcsw);
1756 else /* continue with JUST this "if" as "else" (intentionally no brackets!) */
1758 if ( (GNUNET_SYSERR ==
1760 GNUNET_OS_process_status (pos->proc,
1763 (ret == GNUNET_NO) ||
1764 (statusType == GNUNET_OS_PROCESS_STOPPED) ||
1765 (statusType == GNUNET_OS_PROCESS_UNKNOWN) ||
1766 (statusType == GNUNET_OS_PROCESS_RUNNING) )
1769 if (statusType == GNUNET_OS_PROCESS_EXITED)
1771 statstr = _( /* process termination method */ "exit");
1772 statcode = statusCode;
1774 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1776 statstr = _( /* process termination method */ "signal");
1777 statcode = statusCode;
1781 statstr = _( /* process termination method */ "unknown");
1784 if (0 != pos->killed_at.abs_value_us)
1786 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1787 _("Service `%s' took %s to terminate\n"),
1789 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at),
1792 GNUNET_OS_process_destroy (pos->proc);
1794 broadcast_status (pos->name,
1795 GNUNET_ARM_SERVICE_STOPPED,
1797 if (NULL != pos->killing_client)
1799 signal_result (pos->killing_client, pos->name,
1800 pos->killing_client_request_id,
1801 GNUNET_ARM_RESULT_STOPPED);
1802 pos->killing_client = NULL;
1803 pos->killing_client_request_id = 0;
1805 if (GNUNET_YES != in_shutdown)
1807 if ( (statusType == GNUNET_OS_PROCESS_EXITED) &&
1810 /* process terminated normally, allow restart at any time */
1811 pos->restart_at.abs_value_us = 0;
1812 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1813 _("Service `%s' terminated normally, will restart at any time\n"),
1815 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1816 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1818 GNUNET_break (NULL == sli->accept_task);
1820 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1828 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1829 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1833 GNUNET_STRINGS_relative_time_to_string (pos->backoff,
1835 /* schedule restart */
1836 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1837 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1838 if (NULL != child_restart_task)
1839 GNUNET_SCHEDULER_cancel (child_restart_task);
1841 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1842 &delayed_restart_task,
1851 child_death_task = GNUNET_SCHEDULER_add_read_file (
1852 GNUNET_TIME_UNIT_FOREVER_REL,
1854 &maint_child_death, NULL);
1855 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1857 else if (GNUNET_YES == in_shutdown)
1858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1859 "Delaying shutdown after child's death, still have %u children\n",
1860 list_count (running_head));
1866 * Signal handler called for SIGCHLD. Triggers the
1867 * respective handler by writing to the trigger pipe.
1870 sighandler_child_death ()
1873 int old_errno = errno; /* back-up errno */
1876 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
1877 GNUNET_DISK_PIPE_END_WRITE),
1880 errno = old_errno; /* restore errno */
1885 * Setup our service record for the given section in the configuration file
1886 * (assuming the section is for a service).
1889 * @param section a section in the configuration file
1890 * @return #GNUNET_OK (continue)
1893 setup_service (void *cls,
1894 const char *section)
1896 struct ServiceList *sl;
1900 struct sockaddr **addrs;
1901 socklen_t *addr_lens;
1905 if (0 == strcasecmp (section,
1909 GNUNET_CONFIGURATION_get_value_string (cfg,
1914 /* not a service section */
1918 GNUNET_CONFIGURATION_have_value (cfg,
1922 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1926 if (GNUNET_NO == start_user)
1928 GNUNET_free (binary);
1929 return; /* user service, and we don't deal with those */
1934 if (GNUNET_NO == start_system)
1936 GNUNET_free (binary);
1937 return; /* system service, and we don't deal with those */
1940 sl = find_service (section);
1943 /* got the same section twice!? */
1945 GNUNET_free (binary);
1950 GNUNET_CONFIGURATION_get_value_filename (cfg,
1955 GNUNET_CONFIGURATION_get_value_filename (cfg,
1959 (0 != STAT (config, &sbuf)))
1963 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1966 GNUNET_free (config);
1970 sl = GNUNET_new (struct ServiceList);
1971 sl->name = GNUNET_strdup (section);
1972 sl->binary = binary;
1973 sl->config = config;
1974 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1975 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1977 sl->pipe_control = GNUNET_YES;
1979 if (GNUNET_CONFIGURATION_have_value (cfg,
1982 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1986 GNUNET_CONTAINER_DLL_insert (running_head,
1990 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1994 sl->force_start = GNUNET_YES;
1996 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2004 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2009 if (0 >= (ret = get_server_addresses (section,
2014 /* this will free (or capture) addrs[i] */
2015 for (i = 0; i < ret; i++)
2016 create_listen_socket (addrs[i],
2019 GNUNET_free (addrs);
2020 GNUNET_free (addr_lens);
2025 * A client connected, mark as a monitoring client.
2027 * @param cls closure
2028 * @param client identification of the client
2029 * @param mq queue to talk to @a client
2033 client_connect_cb (void *cls,
2034 struct GNUNET_SERVICE_Client *client,
2035 struct GNUNET_MQ_Handle *mq)
2037 /* All clients are considered to be of the "monitor" kind
2038 * (that is, they don't affect ARM shutdown).
2040 GNUNET_SERVICE_client_mark_monitor (client);
2046 * A client disconnected, clean up associated state.
2048 * @param cls closure
2049 * @param client identification of the client
2050 * @param app_ctx must match @a client
2053 client_disconnect_cb (void *cls,
2054 struct GNUNET_SERVICE_Client *client,
2057 struct ServiceList *sl;
2059 GNUNET_assert (client == app_ctx);
2061 for (sl = running_head; NULL != sl; sl = sl->next)
2062 if (sl->killing_client == client)
2063 sl->killing_client = NULL;
2068 * Handle MONITOR-message.
2070 * @param cls identification of the client
2071 * @param message the actual message
2072 * @return #GNUNET_OK to keep the connection open,
2073 * #GNUNET_SYSERR to close it (signal serious error)
2076 handle_monitor (void *cls,
2077 const struct GNUNET_MessageHeader *message)
2079 struct GNUNET_SERVICE_Client *client = cls;
2081 /* FIXME: might want to start by letting monitor know about
2082 services that are already running */
2083 /* Removal is handled by the server implementation, internally. */
2084 GNUNET_notification_context_add (notifier,
2085 GNUNET_SERVICE_client_get_mq (client));
2086 broadcast_status ("arm",
2087 GNUNET_ARM_SERVICE_MONITORING_STARTED,
2089 GNUNET_SERVICE_client_continue (client);
2094 * Process arm requests.
2096 * @param cls closure
2097 * @param serv the initialized service
2098 * @param c configuration to use
2102 const struct GNUNET_CONFIGURATION_Handle *c,
2103 struct GNUNET_SERVICE_Handle *serv)
2105 struct ServiceList *sl;
2109 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
2112 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2113 GNUNET_DISK_pipe_handle (sigpipe,
2114 GNUNET_DISK_PIPE_END_READ),
2119 GNUNET_CONFIGURATION_get_value_filename (cfg,
2121 "RESOURCE_DIAGNOSTICS",
2124 wait_file = fopen (wait_filename,
2126 if (NULL == wait_file)
2128 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2135 GNUNET_CONFIGURATION_get_value_string (cfg,
2139 prefix_command = GNUNET_strdup ("");
2141 prefix_command = GNUNET_CONFIGURATION_expand_dollar (cfg,
2144 GNUNET_CONFIGURATION_get_value_string (cfg,
2148 final_option = GNUNET_strdup ("");
2150 final_option = GNUNET_CONFIGURATION_expand_dollar (cfg,
2153 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2157 GNUNET_break (GNUNET_YES == start_user);
2158 start_system = GNUNET_NO;
2161 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2165 GNUNET_break (GNUNET_YES == start_system);
2166 start_user = GNUNET_NO;
2168 GNUNET_CONFIGURATION_iterate_sections (cfg,
2172 /* start default services... */
2173 for (sl = running_head; NULL != sl; sl = sl->next)
2174 if (GNUNET_YES == sl->force_start)
2178 notifier = GNUNET_notification_context_create (MAX_NOTIFY_QUEUE);
2183 * The main function for the arm service.
2185 * @param argc number of arguments from the command line
2186 * @param argv command line arguments
2187 * @return 0 ok, 1 on error
2194 struct GNUNET_SIGNAL_Context *shc_chld;
2195 struct GNUNET_MQ_MessageHandler handlers[] = {
2196 GNUNET_MQ_hd_var_size (start,
2197 GNUNET_MESSAGE_TYPE_ARM_START,
2198 struct GNUNET_ARM_Message,
2200 GNUNET_MQ_hd_var_size (stop,
2201 GNUNET_MESSAGE_TYPE_ARM_STOP,
2202 struct GNUNET_ARM_Message,
2204 GNUNET_MQ_hd_fixed_size (monitor,
2205 GNUNET_MESSAGE_TYPE_ARM_MONITOR,
2206 struct GNUNET_MessageHeader,
2208 GNUNET_MQ_hd_fixed_size (list,
2209 GNUNET_MESSAGE_TYPE_ARM_LIST,
2210 struct GNUNET_ARM_Message,
2212 GNUNET_MQ_hd_fixed_size (test,
2213 GNUNET_MESSAGE_TYPE_ARM_TEST,
2214 struct GNUNET_MessageHeader,
2216 GNUNET_MQ_handler_end ()
2219 sigpipe = GNUNET_DISK_pipe (GNUNET_NO,
2223 GNUNET_assert (NULL != sigpipe);
2225 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD,
2226 &sighandler_child_death);
2227 ret = GNUNET_SERVICE_ruN_ (argc,
2230 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN,
2233 &client_disconnect_cb,
2237 if (NULL != wait_file)
2242 if (NULL != wait_filename)
2244 GNUNET_free (wait_filename);
2245 wait_filename = NULL;
2248 GNUNET_SIGNAL_handler_uninstall (shc_chld);
2250 GNUNET_DISK_pipe_close (sigpipe);
2256 #if defined(LINUX) && defined(__GLIBC__)
2260 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
2262 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
2264 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
2265 mallopt (M_TOP_PAD, 1 * 1024);
2271 /* end of gnunet-service-arm.c */