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.
17 * @file arm/gnunet-service-arm.c
18 * @brief the automated restart manager service
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_arm_service.h"
24 #include "gnunet_protocols.h"
27 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
29 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
34 * Name of the file for writing resource utilization summaries to.
36 static char *wait_filename;
39 * Handle for the file for writing resource summaries.
41 static FILE *wait_file;
46 * How many messages do we queue up at most for optional
47 * notifications to a client? (this can cause notifications
48 * about outgoing messages to be dropped).
50 #define MAX_NOTIFY_QUEUE 1024
54 * List of our services.
60 * Record with information about a listen socket we have open.
62 struct ServiceListeningInfo
65 * This is a linked list.
67 struct ServiceListeningInfo *next;
70 * This is a linked list.
72 struct ServiceListeningInfo *prev;
75 * Address this socket is listening on.
77 struct sockaddr *service_addr;
80 * Service this listen socket is for.
82 struct ServiceList *sl;
85 * Number of bytes in @e service_addr
87 socklen_t service_addr_len;
90 * Our listening socket.
92 struct GNUNET_NETWORK_Handle *listen_socket;
95 * Task doing the accepting.
97 struct GNUNET_SCHEDULER_Task *accept_task;
103 * List of our services.
108 * This is a doubly-linked list.
110 struct ServiceList *next;
113 * This is a doubly-linked list.
115 struct ServiceList *prev;
118 * Linked list of listen sockets associated with this service.
120 struct ServiceListeningInfo *listen_head;
123 * Linked list of listen sockets associated with this service.
125 struct ServiceListeningInfo *listen_tail;
128 * Name of the service.
133 * Name of the binary used.
138 * Name of the configuration file used.
143 * Client to notify upon kill completion (waitpid), NULL
144 * if we should simply restart the process.
146 struct GNUNET_SERVICE_Client *killing_client;
149 * ID of the request that killed the service (for reporting back).
151 uint64_t killing_client_request_id;
154 * Process structure pointer of the child.
156 struct GNUNET_OS_Process *proc;
159 * Process exponential backoff time
161 struct GNUNET_TIME_Relative backoff;
164 * Absolute time at which the process is scheduled to restart in case of death
166 struct GNUNET_TIME_Absolute restart_at;
169 * Time we asked the service to shut down (used to calculate time it took
170 * the service to terminate).
172 struct GNUNET_TIME_Absolute killed_at;
175 * Is this service to be started by default (or did a client tell us explicitly
176 * to start it)? #GNUNET_NO if the service is started only upon 'accept' on a
177 * listen socket or possibly explicitly by a client changing the value.
182 * Should we use pipes to signal this process? (YES for Java binaries and if we
189 * List of running services.
191 static struct ServiceList *running_head;
194 * List of running services.
196 static struct ServiceList *running_tail;
201 static const struct GNUNET_CONFIGURATION_Handle *cfg;
204 * Command to prepend to each actual command.
206 static char *prefix_command;
209 * Option to append to each actual command.
211 static char *final_option;
214 * ID of task called whenever we get a SIGCHILD.
216 static struct GNUNET_SCHEDULER_Task *child_death_task;
219 * ID of task called whenever the timeout for restarting a child
222 static struct GNUNET_SCHEDULER_Task *child_restart_task;
225 * Pipe used to communicate shutdown via signal.
227 static struct GNUNET_DISK_PipeHandle *sigpipe;
230 * Are we in shutdown mode?
232 static int in_shutdown;
235 * Are we starting user services?
237 static int start_user = GNUNET_YES;
240 * Are we starting system services?
242 static int start_system = GNUNET_YES;
245 * Handle to our service instance. Our service is a bit special in that
246 * its service is not immediately stopped once we get a shutdown
247 * request (since we need to continue service until all of our child
248 * processes are dead). This handle is used to shut down the service
249 * (and thus trigger process termination) once all child processes are
250 * also dead. A special option in the ARM configuration modifies the
251 * behaviour of the service implementation to not do the shutdown
254 static struct GNUNET_SERVICE_Handle *service;
257 * Context for notifications we need to send to our clients.
259 static struct GNUNET_NotificationContext *notifier;
263 * Add the given UNIX domain path as an address to the
264 * list (as the first entry).
266 * @param saddrs array to update
267 * @param saddrlens where to store the address length
268 * @param unixpath path to add
269 * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
270 * parameter is ignore on systems other than LINUX
273 add_unixpath (struct sockaddr **saddrs,
274 socklen_t *saddrlens,
275 const char *unixpath,
279 struct sockaddr_un *un;
281 un = GNUNET_new (struct sockaddr_un);
282 un->sun_family = AF_UNIX;
283 strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
285 if (GNUNET_YES == abstract)
286 un->sun_path[0] = '\0';
288 #if HAVE_SOCKADDR_UN_SUN_LEN
289 un->sun_len = (u_char) sizeof (struct sockaddr_un);
291 *saddrs = (struct sockaddr *) un;
292 *saddrlens = sizeof (struct sockaddr_un);
294 /* this function should never be called
295 * unless AF_UNIX is defined! */
302 * Get the list of addresses that a server for the given service
305 * @param service_name name of the service
306 * @param cfg configuration (which specifies the addresses)
307 * @param addrs set (call by reference) to an array of pointers to the
308 * addresses the server should bind to and listen on; the
309 * array will be NULL-terminated (on success)
310 * @param addr_lens set (call by reference) to an array of the lengths
311 * of the respective `struct sockaddr` struct in the @a addrs
313 * @return number of addresses found on success,
314 * #GNUNET_SYSERR if the configuration
315 * did not specify reasonable finding information or
316 * if it specified a hostname that could not be resolved;
317 * #GNUNET_NO if the number of addresses configured is
318 * zero (in this case, `*addrs` and `*addr_lens` will be
322 get_server_addresses (const char *service_name,
323 const struct GNUNET_CONFIGURATION_Handle *cfg,
324 struct sockaddr ***addrs,
325 socklen_t ** addr_lens)
328 struct GNUNET_NETWORK_Handle *desc;
329 unsigned long long port;
331 struct addrinfo hints;
332 struct addrinfo *res;
333 struct addrinfo *pos;
334 struct addrinfo *next;
339 struct sockaddr **saddrs;
340 socklen_t *saddrlens;
346 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
350 GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
351 return GNUNET_SYSERR;
354 disablev6 = GNUNET_NO;
358 /* probe IPv6 support */
359 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
362 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
365 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
366 return GNUNET_SYSERR;
368 LOG (GNUNET_ERROR_TYPE_INFO,
369 _("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
370 service_name, STRERROR (errno));
371 disablev6 = GNUNET_YES;
375 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
381 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
384 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
387 LOG (GNUNET_ERROR_TYPE_ERROR,
388 _("Require valid port number for service `%s' in configuration!\n"),
393 LOG (GNUNET_ERROR_TYPE_ERROR,
394 _("Require valid port number for service `%s' in configuration!\n"),
396 return GNUNET_SYSERR;
400 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
402 GNUNET_break (GNUNET_OK ==
403 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
404 "BINDTO", &hostname));
410 abstract = GNUNET_NO;
413 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
415 GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH",
417 (0 < strlen (unixpath)))
419 /* probe UNIX support */
420 struct sockaddr_un s_un;
422 if (strlen (unixpath) >= sizeof (s_un.sun_path))
424 LOG (GNUNET_ERROR_TYPE_WARNING,
425 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
426 (unsigned long long) sizeof (s_un.sun_path));
427 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
428 LOG (GNUNET_ERROR_TYPE_INFO,
429 _("Using `%s' instead\n"),
433 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
435 "USE_ABSTRACT_SOCKETS");
436 if (GNUNET_SYSERR == abstract)
437 abstract = GNUNET_NO;
439 if ((GNUNET_YES != abstract)
441 GNUNET_DISK_directory_create_for_file (unixpath)))
442 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
446 if (NULL != unixpath)
448 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
451 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
454 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
455 GNUNET_free_non_null (hostname);
456 GNUNET_free (unixpath);
457 return GNUNET_SYSERR;
459 LOG (GNUNET_ERROR_TYPE_INFO,
460 _("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
463 GNUNET_free (unixpath);
468 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
474 if ((0 == port) && (NULL == unixpath))
476 LOG (GNUNET_ERROR_TYPE_ERROR,
477 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
479 GNUNET_free_non_null (hostname);
480 return GNUNET_SYSERR;
484 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
485 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
486 add_unixpath (saddrs, saddrlens, unixpath, abstract);
487 GNUNET_free_non_null (unixpath);
488 GNUNET_free_non_null (hostname);
490 *addr_lens = saddrlens;
494 if (NULL != hostname)
496 LOG (GNUNET_ERROR_TYPE_DEBUG,
497 "Resolving `%s' since that is where `%s' will bind to.\n",
500 memset (&hints, 0, sizeof (struct addrinfo));
502 hints.ai_family = AF_INET;
503 hints.ai_protocol = IPPROTO_TCP;
504 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
507 LOG (GNUNET_ERROR_TYPE_ERROR,
508 _("Failed to resolve `%s': %s\n"),
511 GNUNET_free (hostname);
512 GNUNET_free_non_null (unixpath);
513 return GNUNET_SYSERR;
517 while (NULL != (pos = next))
520 if ((disablev6) && (pos->ai_family == AF_INET6))
526 LOG (GNUNET_ERROR_TYPE_ERROR,
527 _("Failed to find %saddress for `%s'.\n"),
528 disablev6 ? "IPv4 " : "",
531 GNUNET_free (hostname);
532 GNUNET_free_non_null (unixpath);
533 return GNUNET_SYSERR;
536 if (NULL != unixpath)
538 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
539 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
541 if (NULL != unixpath)
543 add_unixpath (saddrs, saddrlens, unixpath, abstract);
547 while (NULL != (pos = next))
550 if ((disablev6) && (AF_INET6 == pos->ai_family))
552 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
553 continue; /* not TCP */
554 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
556 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
557 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
558 if (AF_INET == pos->ai_family)
560 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
561 saddrlens[i] = pos->ai_addrlen;
562 saddrs[i] = GNUNET_malloc (saddrlens[i]);
563 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
564 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
568 GNUNET_assert (AF_INET6 == pos->ai_family);
569 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
570 saddrlens[i] = pos->ai_addrlen;
571 saddrs[i] = GNUNET_malloc (saddrlens[i]);
572 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
573 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
577 GNUNET_free (hostname);
583 /* will bind against everything, just set port */
588 if (NULL != unixpath)
591 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
592 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
593 if (NULL != unixpath)
595 add_unixpath (saddrs, saddrlens, unixpath, abstract);
598 saddrlens[i] = sizeof (struct sockaddr_in);
599 saddrs[i] = GNUNET_malloc (saddrlens[i]);
600 #if HAVE_SOCKADDR_IN_SIN_LEN
601 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
603 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
604 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
610 if (NULL != unixpath)
612 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
613 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
615 if (NULL != unixpath)
617 add_unixpath (saddrs, saddrlens, unixpath, abstract);
620 saddrlens[i] = sizeof (struct sockaddr_in6);
621 saddrs[i] = GNUNET_malloc (saddrlens[i]);
622 #if HAVE_SOCKADDR_IN_SIN_LEN
623 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
625 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
626 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
628 saddrlens[i] = sizeof (struct sockaddr_in);
629 saddrs[i] = GNUNET_malloc (saddrlens[i]);
630 #if HAVE_SOCKADDR_IN_SIN_LEN
631 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
633 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
634 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
637 GNUNET_free_non_null (unixpath);
639 *addr_lens = saddrlens;
645 * Signal our client that we will start or stop the
648 * @param client who is being signalled
649 * @param name name of the service
650 * @param request_id id of the request that is being responded to.
651 * @param result message type to send
652 * @return NULL if it was not found
655 signal_result (struct GNUNET_SERVICE_Client *client,
658 enum GNUNET_ARM_Result result)
660 struct GNUNET_MQ_Envelope *env;
661 struct GNUNET_ARM_ResultMessage *msg;
663 env = GNUNET_MQ_msg (msg,
664 GNUNET_MESSAGE_TYPE_ARM_RESULT);
665 msg->result = htonl (result);
666 msg->arm_msg.request_id = GNUNET_htonll (request_id);
667 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
673 * Tell all clients about status change of a service.
675 * @param name name of the service
676 * @param status message type to send
677 * @param unicast if not NULL, send to this client only.
678 * otherwise, send to all clients in the notifier
681 broadcast_status (const char *name,
682 enum GNUNET_ARM_ServiceStatus status,
683 struct GNUNET_SERVICE_Client *unicast)
685 struct GNUNET_MQ_Envelope *env;
686 struct GNUNET_ARM_StatusMessage *msg;
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Sending status %u of service `%s' to client\n",
691 (unsigned int) status,
693 namelen = strlen (name) + 1;
694 env = GNUNET_MQ_msg_extra (msg,
696 GNUNET_MESSAGE_TYPE_ARM_STATUS);
697 msg->status = htonl ((uint32_t) (status));
698 GNUNET_memcpy ((char *) &msg[1],
703 if (NULL != notifier)
704 GNUNET_notification_context_broadcast (notifier,
707 GNUNET_MQ_discard (env);
711 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (unicast),
718 * Actually start the process for the given service.
720 * @param sl identifies service to start
721 * @param client that asked to start the service (may be NULL)
722 * @param request_id id of the request in response to which the process is
723 * being started. 0 if starting was not requested.
726 start_process (struct ServiceList *sl,
727 struct GNUNET_SERVICE_Client *client,
733 int is_simple_service;
734 struct ServiceListeningInfo *sli;
740 /* calculate listen socket list */
743 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
745 GNUNET_array_append (lsocks, ls,
746 GNUNET_NETWORK_get_fd (sli->listen_socket));
747 if (NULL != sli->accept_task)
749 GNUNET_SCHEDULER_cancel (sli->accept_task);
750 sli->accept_task = NULL;
754 GNUNET_array_append (lsocks,
758 GNUNET_array_append (lsocks,
763 /* obtain configuration */
765 GNUNET_CONFIGURATION_get_value_string (cfg,
769 loprefix = GNUNET_strdup (prefix_command);
771 loprefix = GNUNET_CONFIGURATION_expand_dollar (cfg,
774 GNUNET_CONFIGURATION_get_value_string (cfg,
780 options = GNUNET_CONFIGURATION_expand_dollar (cfg,
787 fin_options = GNUNET_strdup (final_option);
788 /* replace '{}' with service name */
789 while (NULL != (optpos = strstr (fin_options,
792 /* terminate string at opening parenthesis */
794 GNUNET_asprintf (&new_options,
799 GNUNET_free (fin_options);
800 fin_options = new_options;
804 /* combine "fin_options" with "options" */
806 GNUNET_asprintf (&options,
810 GNUNET_free (fin_options);
811 GNUNET_free (optpos);
815 /* only have "fin_options", use that */
816 options = fin_options;
819 options = GNUNET_CONFIGURATION_expand_dollar (cfg,
821 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg,
825 const char *service_type = NULL;
826 const char *choices[] = { "GNUNET", "SIMPLE", NULL };
828 is_simple_service = GNUNET_NO;
830 GNUNET_CONFIGURATION_get_value_choice (cfg,
835 (0 == strcasecmp (service_type, "SIMPLE")) )
836 is_simple_service = GNUNET_YES;
839 GNUNET_assert (NULL == sl->proc);
840 if (GNUNET_YES == is_simple_service)
842 /* A simple service will receive no GNUnet specific
843 command line options. */
844 binary = GNUNET_strdup (sl->binary);
845 binary = GNUNET_CONFIGURATION_expand_dollar (cfg, binary);
846 GNUNET_asprintf ("edbinary,
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850 "Starting simple service `%s' using binary `%s'\n",
851 sl->name, sl->binary);
852 /* FIXME: dollar expansion should only be done outside
853 * of ''-quoted strings, escaping should be considered. */
855 options = GNUNET_CONFIGURATION_expand_dollar (cfg, options);
857 GNUNET_OS_start_process_s (sl->pipe_control,
858 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
867 /* actually start process */
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
869 "Starting service `%s' using binary `%s' and configuration `%s'\n",
870 sl->name, sl->binary, sl->config);
871 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
872 GNUNET_asprintf ("edbinary,
876 if (GNUNET_YES == use_debug)
878 if (NULL == sl->config)
880 GNUNET_OS_start_process_s (sl->pipe_control,
881 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
890 GNUNET_OS_start_process_s (sl->pipe_control,
891 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
902 if (NULL == sl->config)
904 GNUNET_OS_start_process_s (sl->pipe_control,
905 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
913 GNUNET_OS_start_process_s (sl->pipe_control,
914 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
923 GNUNET_free (binary);
924 GNUNET_free (quotedbinary);
925 if (NULL == sl->proc)
927 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
928 _("Failed to start service `%s'\n"),
931 signal_result (client,
934 GNUNET_ARM_RESULT_START_FAILED);
938 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
939 _("Starting service `%s'\n"),
941 broadcast_status (sl->name,
942 GNUNET_ARM_SERVICE_STARTING,
945 signal_result (client,
948 GNUNET_ARM_RESULT_STARTING);
951 GNUNET_free (loprefix);
952 GNUNET_free (options);
953 GNUNET_array_grow (lsocks,
960 * Find the process with the given service
961 * name in the given list and return it.
963 * @param name which service entry to look up
964 * @return NULL if it was not found
966 static struct ServiceList *
967 find_service (const char *name)
969 struct ServiceList *sl;
974 if (0 == strcasecmp (sl->name, name))
983 * First connection has come to the listening socket associated with the service,
984 * create the service in order to relay the incoming connection to it
986 * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
989 accept_connection (void *cls)
991 struct ServiceListeningInfo *sli = cls;
992 struct ServiceList *sl = sli->sl;
994 sli->accept_task = NULL;
995 GNUNET_assert (GNUNET_NO == in_shutdown);
996 start_process (sl, NULL, 0);
1001 * Creating a listening socket for each of the service's addresses and
1002 * wait for the first incoming connection to it
1004 * @param sa address associated with the service
1005 * @param addr_len length of @a sa
1006 * @param sl service entry for the service in question
1009 create_listen_socket (struct sockaddr *sa,
1011 struct ServiceList *sl)
1014 struct GNUNET_NETWORK_Handle *sock;
1015 struct ServiceListeningInfo *sli;
1021 switch (sa->sa_family)
1024 sock = GNUNET_NETWORK_socket_create (PF_INET,
1029 sock = GNUNET_NETWORK_socket_create (PF_INET6,
1034 if (0 == strcmp (GNUNET_a2s (sa,
1036 "@")) /* Do not bind to blank UNIX path! */
1038 sock = GNUNET_NETWORK_socket_create (PF_UNIX,
1045 errno = EAFNOSUPPORT;
1050 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1051 _("Unable to create socket for service `%s': %s\n"),
1058 GNUNET_NETWORK_socket_setsockopt (sock,
1063 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1066 if ( (sa->sa_family == AF_INET6) &&
1068 GNUNET_NETWORK_socket_setsockopt (sock,
1073 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1077 if (AF_UNIX == sa->sa_family)
1078 GNUNET_NETWORK_unix_precheck ((struct sockaddr_un *) sa);
1081 GNUNET_NETWORK_socket_bind (sock,
1082 (const struct sockaddr *) sa,
1085 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1086 _("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
1091 GNUNET_break (GNUNET_OK ==
1092 GNUNET_NETWORK_socket_close (sock));
1097 if ((AF_UNIX == sa->sa_family)
1099 /* Permission settings are not required when abstract sockets are used */
1100 && ('\0' != ((const struct sockaddr_un *)sa)->sun_path[0])
1105 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1109 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1112 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sa)->sun_path,
1119 GNUNET_NETWORK_socket_listen (sock, 5))
1121 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1123 GNUNET_break (GNUNET_OK ==
1124 GNUNET_NETWORK_socket_close (sock));
1128 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1129 _("ARM now monitors connections to service `%s' at `%s'\n"),
1133 sli = GNUNET_new (struct ServiceListeningInfo);
1134 sli->service_addr = sa;
1135 sli->service_addr_len = addr_len;
1136 sli->listen_socket = sock;
1139 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1141 &accept_connection, sli);
1142 GNUNET_CONTAINER_DLL_insert (sl->listen_head,
1149 * Remove and free an entry in the service list. Listen sockets
1150 * must have already been cleaned up. Only to be called during shutdown.
1152 * @param sl entry to free
1155 free_service (struct ServiceList *sl)
1157 GNUNET_assert (GNUNET_YES == in_shutdown);
1158 GNUNET_CONTAINER_DLL_remove (running_head,
1161 GNUNET_assert (NULL == sl->listen_head);
1162 GNUNET_free_non_null (sl->config);
1163 GNUNET_free_non_null (sl->binary);
1164 GNUNET_free (sl->name);
1170 * Check START-message.
1172 * @param cls identification of the client
1173 * @param amsg the actual message
1174 * @return #GNUNET_OK to keep the connection open,
1175 * #GNUNET_SYSERR to close it (signal serious error)
1178 check_start (void *cls,
1179 const struct GNUNET_ARM_Message *amsg)
1182 const char *servicename;
1184 size = ntohs (amsg->header.size) - sizeof (struct GNUNET_ARM_Message);
1185 servicename = (const char *) &amsg[1];
1187 (servicename[size - 1] != '\0') )
1190 return GNUNET_SYSERR;
1197 * Handle START-message.
1199 * @param cls identification of the client
1200 * @param amsg the actual message
1203 handle_start (void *cls,
1204 const struct GNUNET_ARM_Message *amsg)
1206 struct GNUNET_SERVICE_Client *client = cls;
1207 const char *servicename;
1208 struct ServiceList *sl;
1209 uint64_t request_id;
1211 request_id = GNUNET_ntohll (amsg->request_id);
1212 servicename = (const char *) &amsg[1];
1213 GNUNET_SERVICE_client_continue (client);
1214 if (GNUNET_YES == in_shutdown)
1216 signal_result (client,
1219 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1222 sl = find_service (servicename);
1225 signal_result (client,
1228 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1231 sl->force_start = GNUNET_YES;
1232 if (NULL != sl->proc)
1234 signal_result (client,
1237 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
1247 * Start a shutdown sequence.
1249 * @param cls closure (refers to service)
1252 trigger_shutdown (void *cls)
1254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1255 "Triggering shutdown\n");
1256 GNUNET_SCHEDULER_shutdown ();
1261 * Check STOP-message.
1263 * @param cls identification of the client
1264 * @param amsg the actual message
1265 * @return #GNUNET_OK to keep the connection open,
1266 * #GNUNET_SYSERR to close it (signal serious error)
1269 check_stop (void *cls,
1270 const struct GNUNET_ARM_Message *amsg)
1273 const char *servicename;
1275 size = ntohs (amsg->header.size) - sizeof (struct GNUNET_ARM_Message);
1276 servicename = (const char *) &amsg[1];
1278 (servicename[size - 1] != '\0') )
1281 return GNUNET_SYSERR;
1288 * Handle STOP-message.
1290 * @param cls identification of the client
1291 * @param amsg the actual message
1294 handle_stop (void *cls,
1295 const struct GNUNET_ARM_Message *amsg)
1297 struct GNUNET_SERVICE_Client *client = cls;
1298 struct ServiceList *sl;
1299 const char *servicename;
1300 uint64_t request_id;
1302 request_id = GNUNET_ntohll (amsg->request_id);
1303 servicename = (const char *) &amsg[1];
1304 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1305 _("Preparing to stop `%s'\n"),
1307 GNUNET_SERVICE_client_continue (client);
1308 if (0 == strcasecmp (servicename,
1311 broadcast_status (servicename,
1312 GNUNET_ARM_SERVICE_STOPPING,
1314 signal_result (client,
1317 GNUNET_ARM_RESULT_STOPPING);
1318 GNUNET_SERVICE_client_persist (client);
1319 GNUNET_SCHEDULER_add_now (&trigger_shutdown,
1323 sl = find_service (servicename);
1326 signal_result (client,
1329 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
1332 sl->force_start = GNUNET_NO;
1333 if (GNUNET_YES == in_shutdown)
1335 /* shutdown in progress */
1336 signal_result (client,
1339 GNUNET_ARM_RESULT_IN_SHUTDOWN);
1342 if (NULL != sl->killing_client)
1344 /* killing already in progress */
1345 signal_result (client,
1348 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
1351 if (NULL == sl->proc)
1353 /* process is down */
1354 signal_result (client,
1357 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
1360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1361 "Sending kill signal to service `%s', waiting for process to die.\n",
1363 broadcast_status (servicename,
1364 GNUNET_ARM_SERVICE_STOPPING,
1366 /* no signal_start - only when it's STOPPED */
1367 sl->killed_at = GNUNET_TIME_absolute_get ();
1368 if (0 != GNUNET_OS_process_kill (sl->proc,
1370 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1372 sl->killing_client = client;
1373 sl->killing_client_request_id = request_id;
1378 * Handle LIST-message.
1380 * @param cls identification of the client
1381 * @param message the actual message
1384 handle_list (void *cls,
1385 const struct GNUNET_ARM_Message *request)
1387 struct GNUNET_SERVICE_Client *client = cls;
1388 struct GNUNET_MQ_Envelope *env;
1389 struct GNUNET_ARM_ListResultMessage *msg;
1390 size_t string_list_size;
1391 struct ServiceList *sl;
1395 GNUNET_break (0 == ntohl (request->reserved));
1397 string_list_size = 0;
1399 /* first count the running processes get their name's size */
1400 for (sl = running_head; NULL != sl; sl = sl->next)
1402 if (NULL != sl->proc)
1404 string_list_size += strlen (sl->name);
1405 string_list_size += strlen (sl->binary);
1406 string_list_size += 4;
1411 env = GNUNET_MQ_msg_extra (msg,
1413 GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT);
1414 msg->arm_msg.request_id = request->request_id;
1415 msg->count = htons (count);
1417 pos = (char *) &msg[1];
1418 for (sl = running_head; NULL != sl; sl = sl->next)
1420 if (NULL != sl->proc)
1422 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
1423 GNUNET_snprintf (pos,
1431 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1433 GNUNET_SERVICE_client_continue (client);
1438 * Handle TEST-message by sending back TEST.
1440 * @param cls identification of the client
1441 * @param message the actual message
1444 handle_test (void *cls,
1445 const struct GNUNET_MessageHeader *message)
1447 struct GNUNET_SERVICE_Client *client = cls;
1448 struct GNUNET_MQ_Envelope *env;
1449 struct GNUNET_MessageHeader *msg;
1451 env = GNUNET_MQ_msg (msg,
1452 GNUNET_MESSAGE_TYPE_ARM_TEST);
1453 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1455 GNUNET_SERVICE_client_continue (client);
1460 * We are done with everything. Stop remaining
1461 * tasks, signal handler and the server.
1466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1467 "Last shutdown phase\n");
1468 if (NULL != notifier)
1470 GNUNET_notification_context_destroy (notifier);
1473 if (NULL != service)
1475 GNUNET_SERVICE_shutdown (service);
1478 if (NULL != child_death_task)
1480 GNUNET_SCHEDULER_cancel (child_death_task);
1481 child_death_task = NULL;
1487 * Count how many services are still active.
1489 * @param running_head list of services
1490 * @return number of active services found
1493 list_count (struct ServiceList *running_head)
1495 struct ServiceList *i;
1498 for (res = 0, i = running_head; i; i = i->next, res++)
1499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507 * Task run for shutdown.
1509 * @param cls closure, NULL if we need to self-restart
1512 shutdown_task (void *cls)
1514 struct ServiceList *pos;
1515 struct ServiceList *nxt;
1516 struct ServiceListeningInfo *sli;
1518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1519 "First shutdown phase\n");
1520 if (NULL != child_restart_task)
1522 GNUNET_SCHEDULER_cancel (child_restart_task);
1523 child_restart_task = NULL;
1525 in_shutdown = GNUNET_YES;
1526 /* first, stop listening */
1527 for (pos = running_head; NULL != pos; pos = pos->next)
1529 while (NULL != (sli = pos->listen_head))
1531 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
1534 if (NULL != sli->accept_task)
1536 GNUNET_SCHEDULER_cancel (sli->accept_task);
1537 sli->accept_task = NULL;
1539 GNUNET_break (GNUNET_OK ==
1540 GNUNET_NETWORK_socket_close (sli->listen_socket));
1541 GNUNET_free (sli->service_addr);
1545 /* then, shutdown all existing service processes */
1547 while (NULL != (pos = nxt))
1550 if (NULL != pos->proc)
1552 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1553 "Stopping service `%s'\n",
1555 pos->killed_at = GNUNET_TIME_absolute_get ();
1556 if (0 != GNUNET_OS_process_kill (pos->proc,
1558 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1566 /* finally, should all service processes be already gone, terminate for real */
1567 if (NULL == running_head)
1570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1571 "Delaying shutdown, have %u childs still running\n",
1572 list_count (running_head));
1577 * Task run whenever it is time to restart a child that died.
1579 * @param cls closure, always NULL
1582 delayed_restart_task (void *cls)
1585 struct ServiceList *sl;
1586 struct GNUNET_TIME_Relative lowestRestartDelay;
1587 struct ServiceListeningInfo *sli;
1589 child_restart_task = NULL;
1590 GNUNET_assert (GNUNET_NO == in_shutdown);
1591 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1593 /* check for services that need to be restarted due to
1594 * configuration changes or because the last restart failed */
1595 for (sl = running_head; NULL != sl; sl = sl->next)
1597 if (NULL != sl->proc)
1599 /* service is currently not running */
1600 if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
1602 /* restart is now allowed */
1603 if (sl->force_start)
1605 /* process should run by default, start immediately */
1606 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1607 _("Restarting service `%s'.\n"),
1615 /* process is run on-demand, ensure it is re-started if there is demand */
1616 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1617 if (NULL == sli->accept_task)
1619 /* accept was actually paused, so start it again */
1621 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1630 /* update calculation for earliest time to reactivate a service */
1631 lowestRestartDelay =
1632 GNUNET_TIME_relative_min (lowestRestartDelay,
1633 GNUNET_TIME_absolute_get_remaining
1637 if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1640 "Will restart process in %s\n",
1641 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay,
1643 child_restart_task =
1644 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1645 GNUNET_SCHEDULER_PRIORITY_IDLE,
1646 &delayed_restart_task,
1653 * Task triggered whenever we receive a SIGCHLD (child
1656 * @param cls closure, NULL if we need to self-restart
1659 maint_child_death (void *cls)
1661 struct ServiceList *pos;
1662 struct ServiceList *next;
1663 struct ServiceListeningInfo *sli;
1664 const char *statstr;
1668 enum GNUNET_OS_ProcessStatusType statusType;
1669 unsigned long statusCode;
1670 const struct GNUNET_DISK_FileHandle *pr;
1672 pr = GNUNET_DISK_pipe_handle (sigpipe,
1673 GNUNET_DISK_PIPE_END_READ);
1674 child_death_task = NULL;
1675 /* consume the signal */
1676 GNUNET_break (0 < GNUNET_DISK_file_read (pr,
1680 /* check for services that died (WAITPID) */
1681 next = running_head;
1682 while (NULL != (pos = next))
1686 if (NULL == pos->proc)
1688 if (GNUNET_YES == in_shutdown)
1693 if (NULL != wait_file)
1695 /* need to use 'wait4()' to obtain and log performance data */
1700 pid = GNUNET_OS_process_get_pid (pos->proc);
1706 continue; /* no process done */
1707 if (WIFEXITED (status))
1709 statusType = GNUNET_OS_PROCESS_EXITED;
1710 statusCode = WEXITSTATUS (status);
1712 else if (WIFSIGNALED (status))
1714 statusType = GNUNET_OS_PROCESS_SIGNALED;
1715 statusCode = WTERMSIG (status);
1717 else if (WIFSTOPPED (status))
1719 statusType = GNUNET_OS_PROCESS_SIGNALED;
1720 statusCode = WSTOPSIG (status);
1723 else if (WIFCONTINUED (status))
1725 statusType = GNUNET_OS_PROCESS_RUNNING;
1731 statusType = GNUNET_OS_PROCESS_UNKNOWN;
1734 if ( (GNUNET_OS_PROCESS_EXITED == statusType) ||
1735 (GNUNET_OS_PROCESS_SIGNALED == statusType) )
1737 double utime = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 10e6);
1738 double stime = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 10e6);
1740 "%s(%u) %.3f %.3f %llu %llu %llu %llu %llu\n",
1745 (unsigned long long) ru.ru_maxrss,
1746 (unsigned long long) ru.ru_inblock,
1747 (unsigned long long) ru.ru_oublock,
1748 (unsigned long long) ru.ru_nvcsw,
1749 (unsigned long long) ru.ru_nivcsw);
1752 else /* continue with JUST this "if" as "else" (intentionally no brackets!) */
1754 if ( (GNUNET_SYSERR ==
1756 GNUNET_OS_process_status (pos->proc,
1759 (ret == GNUNET_NO) ||
1760 (statusType == GNUNET_OS_PROCESS_STOPPED) ||
1761 (statusType == GNUNET_OS_PROCESS_UNKNOWN) ||
1762 (statusType == GNUNET_OS_PROCESS_RUNNING) )
1765 if (statusType == GNUNET_OS_PROCESS_EXITED)
1767 statstr = _( /* process termination method */ "exit");
1768 statcode = statusCode;
1770 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1772 statstr = _( /* process termination method */ "signal");
1773 statcode = statusCode;
1777 statstr = _( /* process termination method */ "unknown");
1780 if (0 != pos->killed_at.abs_value_us)
1782 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1783 _("Service `%s' took %s to terminate\n"),
1785 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at),
1788 GNUNET_OS_process_destroy (pos->proc);
1790 broadcast_status (pos->name,
1791 GNUNET_ARM_SERVICE_STOPPED,
1793 if (NULL != pos->killing_client)
1795 signal_result (pos->killing_client, pos->name,
1796 pos->killing_client_request_id,
1797 GNUNET_ARM_RESULT_STOPPED);
1798 pos->killing_client = NULL;
1799 pos->killing_client_request_id = 0;
1801 if (GNUNET_YES != in_shutdown)
1803 if ( (statusType == GNUNET_OS_PROCESS_EXITED) &&
1806 /* process terminated normally, allow restart at any time */
1807 pos->restart_at.abs_value_us = 0;
1808 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1809 _("Service `%s' terminated normally, will restart at any time\n"),
1811 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1812 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1814 GNUNET_break (NULL == sli->accept_task);
1816 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1824 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1825 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1829 GNUNET_STRINGS_relative_time_to_string (pos->backoff,
1832 /* Reduce backoff based on runtime of the process,
1833 so that there is a cool-down if a process actually
1834 runs for a while. */
1835 struct GNUNET_TIME_Relative runtime;
1836 unsigned int minutes;
1838 runtime = GNUNET_TIME_absolute_get_duration (pos->restart_at);
1839 minutes = runtime.rel_value_us / GNUNET_TIME_UNIT_MINUTES.rel_value_us;
1841 pos->backoff = GNUNET_TIME_UNIT_ZERO;
1843 pos->backoff.rel_value_us <<= minutes;
1845 /* schedule restart */
1846 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1847 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1848 if (NULL != child_restart_task)
1849 GNUNET_SCHEDULER_cancel (child_restart_task);
1851 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1852 &delayed_restart_task,
1861 child_death_task = GNUNET_SCHEDULER_add_read_file (
1862 GNUNET_TIME_UNIT_FOREVER_REL,
1864 &maint_child_death, NULL);
1865 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1867 else if (GNUNET_YES == in_shutdown)
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1869 "Delaying shutdown after child's death, still have %u children\n",
1870 list_count (running_head));
1876 * Signal handler called for SIGCHLD. Triggers the
1877 * respective handler by writing to the trigger pipe.
1880 sighandler_child_death ()
1883 int old_errno = errno; /* back-up errno */
1886 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
1887 GNUNET_DISK_PIPE_END_WRITE),
1890 errno = old_errno; /* restore errno */
1895 * Setup our service record for the given section in the configuration file
1896 * (assuming the section is for a service).
1899 * @param section a section in the configuration file
1900 * @return #GNUNET_OK (continue)
1903 setup_service (void *cls,
1904 const char *section)
1906 struct ServiceList *sl;
1910 struct sockaddr **addrs;
1911 socklen_t *addr_lens;
1914 if (0 == strcasecmp (section,
1918 GNUNET_CONFIGURATION_get_value_string (cfg,
1923 /* not a service section */
1927 GNUNET_CONFIGURATION_have_value (cfg,
1931 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1935 if (GNUNET_NO == start_user)
1937 GNUNET_free (binary);
1938 return; /* user service, and we don't deal with those */
1943 if (GNUNET_NO == start_system)
1945 GNUNET_free (binary);
1946 return; /* system service, and we don't deal with those */
1949 sl = find_service (section);
1952 /* got the same section twice!? */
1954 GNUNET_free (binary);
1959 GNUNET_CONFIGURATION_get_value_filename (cfg,
1964 GNUNET_CONFIGURATION_get_value_filename (cfg,
1968 (0 != STAT (config, &sbuf)))
1972 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1975 GNUNET_free (config);
1979 sl = GNUNET_new (struct ServiceList);
1980 sl->name = GNUNET_strdup (section);
1981 sl->binary = binary;
1982 sl->config = config;
1983 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1984 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1986 sl->pipe_control = GNUNET_YES;
1988 if (GNUNET_CONFIGURATION_have_value (cfg,
1991 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1995 GNUNET_CONTAINER_DLL_insert (running_head,
1999 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2003 sl->force_start = GNUNET_YES;
2005 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2013 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2018 if (0 >= (ret = get_server_addresses (section,
2023 /* this will free (or capture) addrs[i] */
2024 for (unsigned int i = 0; i < ret; i++)
2025 create_listen_socket (addrs[i],
2028 GNUNET_free (addrs);
2029 GNUNET_free (addr_lens);
2034 * A client connected, mark as a monitoring client.
2036 * @param cls closure
2037 * @param client identification of the client
2038 * @param mq queue to talk to @a client
2042 client_connect_cb (void *cls,
2043 struct GNUNET_SERVICE_Client *client,
2044 struct GNUNET_MQ_Handle *mq)
2046 /* All clients are considered to be of the "monitor" kind
2047 * (that is, they don't affect ARM shutdown).
2049 GNUNET_SERVICE_client_mark_monitor (client);
2055 * A client disconnected, clean up associated state.
2057 * @param cls closure
2058 * @param client identification of the client
2059 * @param app_ctx must match @a client
2062 client_disconnect_cb (void *cls,
2063 struct GNUNET_SERVICE_Client *client,
2066 struct ServiceList *sl;
2068 GNUNET_assert (client == app_ctx);
2070 for (sl = running_head; NULL != sl; sl = sl->next)
2071 if (sl->killing_client == client)
2072 sl->killing_client = NULL;
2077 * Handle MONITOR-message.
2079 * @param cls identification of the client
2080 * @param message the actual message
2081 * @return #GNUNET_OK to keep the connection open,
2082 * #GNUNET_SYSERR to close it (signal serious error)
2085 handle_monitor (void *cls,
2086 const struct GNUNET_MessageHeader *message)
2088 struct GNUNET_SERVICE_Client *client = cls;
2090 /* FIXME: might want to start by letting monitor know about
2091 services that are already running */
2092 /* Removal is handled by the server implementation, internally. */
2093 GNUNET_notification_context_add (notifier,
2094 GNUNET_SERVICE_client_get_mq (client));
2095 broadcast_status ("arm",
2096 GNUNET_ARM_SERVICE_MONITORING_STARTED,
2098 GNUNET_SERVICE_client_continue (client);
2103 * Process arm requests.
2105 * @param cls closure
2106 * @param serv the initialized service
2107 * @param c configuration to use
2111 const struct GNUNET_CONFIGURATION_Handle *c,
2112 struct GNUNET_SERVICE_Handle *serv)
2114 struct ServiceList *sl;
2118 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
2121 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2122 GNUNET_DISK_pipe_handle (sigpipe,
2123 GNUNET_DISK_PIPE_END_READ),
2128 GNUNET_CONFIGURATION_get_value_filename (cfg,
2130 "RESOURCE_DIAGNOSTICS",
2133 wait_file = fopen (wait_filename,
2135 if (NULL == wait_file)
2137 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2144 GNUNET_CONFIGURATION_get_value_string (cfg,
2148 prefix_command = GNUNET_strdup ("");
2150 prefix_command = GNUNET_CONFIGURATION_expand_dollar (cfg,
2153 GNUNET_CONFIGURATION_get_value_string (cfg,
2157 final_option = GNUNET_strdup ("");
2159 final_option = GNUNET_CONFIGURATION_expand_dollar (cfg,
2162 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2166 GNUNET_break (GNUNET_YES == start_user);
2167 start_system = GNUNET_NO;
2170 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2174 GNUNET_break (GNUNET_YES == start_system);
2175 start_user = GNUNET_NO;
2177 GNUNET_CONFIGURATION_iterate_sections (cfg,
2181 /* start default services... */
2182 for (sl = running_head; NULL != sl; sl = sl->next)
2183 if (GNUNET_YES == sl->force_start)
2187 notifier = GNUNET_notification_context_create (MAX_NOTIFY_QUEUE);
2192 * The main function for the arm service.
2194 * @param argc number of arguments from the command line
2195 * @param argv command line arguments
2196 * @return 0 ok, 1 on error
2203 struct GNUNET_SIGNAL_Context *shc_chld;
2204 struct GNUNET_MQ_MessageHandler handlers[] = {
2205 GNUNET_MQ_hd_var_size (start,
2206 GNUNET_MESSAGE_TYPE_ARM_START,
2207 struct GNUNET_ARM_Message,
2209 GNUNET_MQ_hd_var_size (stop,
2210 GNUNET_MESSAGE_TYPE_ARM_STOP,
2211 struct GNUNET_ARM_Message,
2213 GNUNET_MQ_hd_fixed_size (monitor,
2214 GNUNET_MESSAGE_TYPE_ARM_MONITOR,
2215 struct GNUNET_MessageHeader,
2217 GNUNET_MQ_hd_fixed_size (list,
2218 GNUNET_MESSAGE_TYPE_ARM_LIST,
2219 struct GNUNET_ARM_Message,
2221 GNUNET_MQ_hd_fixed_size (test,
2222 GNUNET_MESSAGE_TYPE_ARM_TEST,
2223 struct GNUNET_MessageHeader,
2225 GNUNET_MQ_handler_end ()
2228 sigpipe = GNUNET_DISK_pipe (GNUNET_NO,
2232 GNUNET_assert (NULL != sigpipe);
2234 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD,
2235 &sighandler_child_death);
2236 ret = GNUNET_SERVICE_run_ (argc,
2239 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN,
2242 &client_disconnect_cb,
2246 if (NULL != wait_file)
2251 if (NULL != wait_filename)
2253 GNUNET_free (wait_filename);
2254 wait_filename = NULL;
2257 GNUNET_SIGNAL_handler_uninstall (shc_chld);
2259 GNUNET_DISK_pipe_close (sigpipe);
2265 #if defined(LINUX) && defined(__GLIBC__)
2269 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
2271 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
2273 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
2274 mallopt (M_TOP_PAD, 1 * 1024);
2280 /* end of gnunet-service-arm.c */