2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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"
33 * How many messages do we queue up at most for optional
34 * notifications to a client? (this can cause notifications
35 * about outgoing messages to be dropped).
37 #define MAX_NOTIFY_QUEUE 1024
41 * List of our services.
47 * Record with information about a listen socket we have open.
49 struct ServiceListeningInfo
52 * This is a linked list.
54 struct ServiceListeningInfo *next;
57 * This is a linked list.
59 struct ServiceListeningInfo *prev;
62 * Address this socket is listening on.
64 struct sockaddr *service_addr;
67 * Service this listen socket is for.
69 struct ServiceList *sl;
72 * Number of bytes in 'service_addr'
74 socklen_t service_addr_len;
77 * Our listening socket.
79 struct GNUNET_NETWORK_Handle *listen_socket;
82 * Task doing the accepting.
84 GNUNET_SCHEDULER_TaskIdentifier accept_task;
90 * List of our services.
95 * This is a doubly-linked list.
97 struct ServiceList *next;
100 * This is a doubly-linked list.
102 struct ServiceList *prev;
105 * Linked list of listen sockets associated with this service.
107 struct ServiceListeningInfo *listen_head;
110 * Linked list of listen sockets associated with this service.
112 struct ServiceListeningInfo *listen_tail;
115 * Name of the service.
120 * Name of the binary used.
125 * Name of the configuration file used.
130 * Client to notify upon kill completion (waitpid), NULL
131 * if we should simply restart the process.
133 struct GNUNET_SERVER_Client *killing_client;
136 * ID of the request that killed the service (for reporting back).
138 uint64_t killing_client_request_id;
141 * Process structure pointer of the child.
143 struct GNUNET_OS_Process *proc;
146 * Process exponential backoff time
148 struct GNUNET_TIME_Relative backoff;
151 * Absolute time at which the process is scheduled to restart in case of death
153 struct GNUNET_TIME_Absolute restart_at;
156 * Time we asked the service to shut down (used to calculate time it took
157 * the service to terminate).
159 struct GNUNET_TIME_Absolute killed_at;
162 * Is this service to be started by default (or did a client tell us explicitly
163 * to start it)? #GNUNET_NO if the service is started only upon 'accept' on a
164 * listen socket or possibly explicitly by a client changing the value.
169 * Should we use pipes to signal this process? (YES for Java binaries and if we
176 * List of running services.
178 static struct ServiceList *running_head;
181 * List of running services.
183 static struct ServiceList *running_tail;
188 static const struct GNUNET_CONFIGURATION_Handle *cfg;
191 * Command to prepend to each actual command.
193 static char *prefix_command;
196 * Option to append to each actual command.
198 static char *final_option;
201 * ID of task called whenever we get a SIGCHILD.
203 static GNUNET_SCHEDULER_TaskIdentifier child_death_task;
206 * ID of task called whenever the timeout for restarting a child
209 static GNUNET_SCHEDULER_TaskIdentifier child_restart_task;
212 * Pipe used to communicate shutdown via signal.
214 static struct GNUNET_DISK_PipeHandle *sigpipe;
217 * Are we in shutdown mode?
219 static int in_shutdown;
222 * Are we starting user services?
224 static int start_user = GNUNET_YES;
227 * Are we starting system services?
229 static int start_system = GNUNET_YES;
232 * Handle to our server instance. Our server is a bit special in that
233 * its service is not immediately stopped once we get a shutdown
234 * request (since we need to continue service until all of our child
235 * processes are dead). This handle is used to shut down the server
236 * (and thus trigger process termination) once all child processes are
237 * also dead. A special option in the ARM configuration modifies the
238 * behaviour of the service implementation to not do the shutdown
241 static struct GNUNET_SERVER_Handle *server;
244 * Context for notifications we need to send to our clients.
246 static struct GNUNET_SERVER_NotificationContext *notifier;
249 #include "do_start_process.c"
252 * Transmit a status result message.
254 * @param cls a `unit16_t *` with message type
255 * @param size number of bytes available in @a buf
256 * @param buf where to copy the message, NULL on error
257 * @return number of bytes copied to @a buf
260 write_result (void *cls, size_t size, void *buf)
262 struct GNUNET_ARM_ResultMessage *msg = cls;
267 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
268 _("Could not send status result to client\n"));
270 return 0; /* error, not much we can do */
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "Sending status response %u to client\n",
274 (unsigned int) msg->result);
275 msize = msg->arm_msg.header.size;
276 GNUNET_assert (size >= msize);
277 msg->arm_msg.header.size = htons (msg->arm_msg.header.size);
278 msg->arm_msg.header.type = htons (msg->arm_msg.header.type);
279 msg->result = htonl (msg->result);
280 msg->arm_msg.request_id = GNUNET_htonll (msg->arm_msg.request_id);
281 memcpy (buf, msg, msize);
288 * Transmit the list of running services.
290 * @param cls pointer to `struct GNUNET_ARM_ListResultMessage` with the message
291 * @param size number of bytes available in @a buf
292 * @param buf where to copy the message, NULL on error
293 * @return number of bytes copied to @a buf
296 write_list_result (void *cls, size_t size, void *buf)
298 struct GNUNET_ARM_ListResultMessage *msg = cls;
303 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
304 _("Could not send list result to client\n"));
306 return 0; /* error, not much we can do */
309 rslt_size = msg->arm_msg.header.size;
310 GNUNET_assert (size >= rslt_size);
311 msg->arm_msg.header.size = htons (msg->arm_msg.header.size);
312 msg->arm_msg.header.type = htons (msg->arm_msg.header.type);
313 msg->arm_msg.request_id = GNUNET_htonll (msg->arm_msg.request_id);
314 msg->count = htons (msg->count);
316 memcpy (buf, msg, rslt_size);
323 * Signal our client that we will start or stop the
326 * @param client who is being signalled
327 * @param name name of the service
328 * @param request_id id of the request that is being responded to.
329 * @param result message type to send
330 * @return NULL if it was not found
333 signal_result (struct GNUNET_SERVER_Client *client,
336 enum GNUNET_ARM_Result result)
338 struct GNUNET_ARM_ResultMessage *msg;
341 msize = sizeof (struct GNUNET_ARM_ResultMessage);
342 msg = GNUNET_malloc (msize);
343 msg->arm_msg.header.size = msize;
344 msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_RESULT;
345 msg->result = result;
346 msg->arm_msg.request_id = request_id;
348 GNUNET_SERVER_notify_transmit_ready (client, msize,
349 GNUNET_TIME_UNIT_FOREVER_REL,
355 * Tell all clients about status change of a service.
357 * @param name name of the service
358 * @param status message type to send
359 * @param unicast if not NULL, send to this client only.
360 * otherwise, send to all clients in the notifier
363 broadcast_status (const char *name,
364 enum GNUNET_ARM_ServiceStatus status,
365 struct GNUNET_SERVER_Client *unicast)
367 struct GNUNET_ARM_StatusMessage *msg;
370 if (NULL == notifier)
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Sending status %u of service `%s' to client\n",
374 (unsigned int) status, name);
375 namelen = strlen (name);
376 msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_StatusMessage) + namelen + 1);
377 msg->header.size = htons (sizeof (struct GNUNET_ARM_StatusMessage) + namelen + 1);
378 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_STATUS);
379 msg->status = htonl ((uint32_t) (status));
380 memcpy ((char *) &msg[1], name, namelen + 1);
383 GNUNET_SERVER_notification_context_broadcast (notifier,
384 (struct GNUNET_MessageHeader *) msg, GNUNET_YES);
386 GNUNET_SERVER_notification_context_unicast (notifier, unicast,
387 (const struct GNUNET_MessageHeader *) msg, GNUNET_NO);
393 * Actually start the process for the given service.
395 * @param sl identifies service to start
396 * @param client that asked to start the service (may be NULL)
397 * @param request_id id of the request in response to which the process is
398 * being started. 0 if starting was not requested.
401 start_process (struct ServiceList *sl,
402 struct GNUNET_SERVER_Client *client,
413 struct ServiceListeningInfo *sli;
418 /* calculate listen socket list */
421 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
423 GNUNET_array_append (lsocks, ls,
424 GNUNET_NETWORK_get_fd (sli->listen_socket));
425 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
427 GNUNET_SCHEDULER_cancel (sli->accept_task);
428 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
432 GNUNET_array_append (lsocks, ls, INVALID_SOCKET);
434 GNUNET_array_append (lsocks, ls, -1);
437 /* obtain configuration */
439 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "PREFIX",
441 loprefix = GNUNET_strdup (prefix_command);
443 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "OPTIONS",
446 options = GNUNET_strdup (final_option);
447 if (NULL == strstr (options, "%"))
449 /* replace '{}' with service name */
450 while (NULL != (optpos = strstr (options, "{}")))
454 GNUNET_asprintf (&optpos, options, sl->name);
455 GNUNET_free (options);
458 /* replace '$PATH' with value associated with "PATH" */
459 while (NULL != (optpos = strstr (options, "$")))
462 while (isupper ((unsigned char) *optend))
471 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
473 val = GNUNET_strdup ("");
475 GNUNET_asprintf (&optpos, "%s%s%c%s", options, val, b, next);
476 GNUNET_free (options);
482 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
484 /* actually start process */
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486 "Starting service `%s' using binary `%s' and configuration `%s'\n",
487 sl->name, sl->binary, sl->config);
488 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
489 GNUNET_assert (NULL == sl->proc);
490 if (GNUNET_YES == use_debug)
492 if (NULL == sl->config)
494 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
495 lsocks, loprefix, binary, "-L",
496 "DEBUG", options, NULL);
499 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
500 lsocks, loprefix, binary, "-c", sl->config, "-L",
501 "DEBUG", options, NULL);
505 if (NULL == sl->config)
507 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
508 lsocks, loprefix, binary,
512 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
513 lsocks, loprefix, binary, "-c", sl->config,
516 GNUNET_free (binary);
517 if (sl->proc == NULL)
519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to start service `%s'\n"),
522 signal_result (client, sl->name, request_id, GNUNET_ARM_RESULT_START_FAILED);
526 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting service `%s'\n"),
528 broadcast_status (sl->name, GNUNET_ARM_SERVICE_STARTING, NULL);
530 signal_result (client, sl->name, request_id, GNUNET_ARM_RESULT_STARTING);
533 GNUNET_free (loprefix);
534 GNUNET_free (options);
535 GNUNET_array_grow (lsocks, ls, 0);
540 * Find the process with the given service
541 * name in the given list and return it.
543 * @param name which service entry to look up
544 * @return NULL if it was not found
546 static struct ServiceList *
547 find_service (const char *name)
549 struct ServiceList *sl;
554 if (0 == strcasecmp (sl->name, name))
563 * First connection has come to the listening socket associated with the service,
564 * create the service in order to relay the incoming connection to it
566 * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
570 accept_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
572 struct ServiceListeningInfo *sli = cls;
573 struct ServiceList *sl = sli->sl;
575 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
576 GNUNET_assert (GNUNET_NO == in_shutdown);
577 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
579 start_process (sl, NULL, 0);
584 * Creating a listening socket for each of the service's addresses and
585 * wait for the first incoming connection to it
587 * @param sa address associated with the service
588 * @param addr_len length of @a sa
589 * @param sl service entry for the service in question
592 create_listen_socket (struct sockaddr *sa, socklen_t addr_len,
593 struct ServiceList *sl)
596 struct GNUNET_NETWORK_Handle *sock;
597 struct ServiceListeningInfo *sli;
601 switch (sa->sa_family)
604 sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
607 sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
610 if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */
612 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
617 errno = EAFNOSUPPORT;
622 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
623 _("Unable to create socket for service `%s': %s\n"),
624 sl->name, STRERROR (errno));
628 if (GNUNET_NETWORK_socket_setsockopt
629 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
630 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
633 if ((sa->sa_family == AF_INET6) &&
634 (GNUNET_NETWORK_socket_setsockopt
635 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
636 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
641 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) sa, addr_len))
643 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
645 ("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
646 sl->name, GNUNET_a2s (sa, addr_len), STRERROR (errno));
647 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
652 if (AF_UNIX == sa->sa_family)
655 GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name,
658 GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name,
660 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sa)->sun_path,
666 if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
668 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
669 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
673 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
674 _("ARM now monitors connections to service `%s' at `%s'\n"),
675 sl->name, GNUNET_a2s (sa, addr_len));
676 sli = GNUNET_malloc (sizeof (struct ServiceListeningInfo));
677 sli->service_addr = sa;
678 sli->service_addr_len = addr_len;
679 sli->listen_socket = sock;
682 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock,
683 &accept_connection, sli);
684 GNUNET_CONTAINER_DLL_insert (sl->listen_head, sl->listen_tail, sli);
689 * Remove and free an entry in the service list. Listen sockets
690 * must have already been cleaned up. Only to be called during shutdown.
692 * @param sl entry to free
695 free_service (struct ServiceList *sl)
697 GNUNET_assert (GNUNET_YES == in_shutdown);
698 GNUNET_CONTAINER_DLL_remove (running_head, running_tail, sl);
699 GNUNET_assert (NULL == sl->listen_head);
700 GNUNET_free_non_null (sl->config);
701 GNUNET_free_non_null (sl->binary);
702 GNUNET_free (sl->name);
708 * Handle START-message.
710 * @param cls closure (always NULL)
711 * @param client identification of the client
712 * @param message the actual message
713 * @return #GNUNET_OK to keep the connection open,
714 * #GNUNET_SYSERR to close it (signal serious error)
717 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
718 const struct GNUNET_MessageHeader *message)
720 const char *servicename;
721 struct ServiceList *sl;
724 struct GNUNET_ARM_Message *amsg;
726 amsg = (struct GNUNET_ARM_Message *) message;
727 request_id = GNUNET_ntohll (amsg->request_id);
728 size = ntohs (amsg->header.size);
729 size -= sizeof (struct GNUNET_ARM_Message);
730 servicename = (const char *) &amsg[1];
731 if ((size == 0) || (servicename[size - 1] != '\0'))
734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
737 if (GNUNET_YES == in_shutdown)
739 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IN_SHUTDOWN);
740 GNUNET_SERVER_receive_done (client, GNUNET_OK);
743 sl = find_service (servicename);
746 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_NOT_KNOWN);
747 GNUNET_SERVER_receive_done (client, GNUNET_OK);
750 sl->is_default = GNUNET_YES;
751 if (sl->proc != NULL)
753 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
754 GNUNET_SERVER_receive_done (client, GNUNET_OK);
757 start_process (sl, client, request_id);
758 GNUNET_SERVER_receive_done (client, GNUNET_OK);
763 * Start a shutdown sequence.
765 * @param cls closure (refers to service)
766 * @param tc task context
769 trigger_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Triggering shutdown\n");
772 GNUNET_SCHEDULER_shutdown ();
777 * Handle STOP-message.
779 * @param cls closure (always NULL)
780 * @param client identification of the client
781 * @param message the actual message
782 * @return #GNUNET_OK to keep the connection open,
783 * #GNUNET_SYSERR to close it (signal serious error)
786 handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
787 const struct GNUNET_MessageHeader *message)
789 struct ServiceList *sl;
790 const char *servicename;
793 struct GNUNET_ARM_Message *amsg;
795 amsg = (struct GNUNET_ARM_Message *) message;
796 request_id = GNUNET_ntohll (amsg->request_id);
797 size = ntohs (amsg->header.size);
798 size -= sizeof (struct GNUNET_ARM_Message);
799 servicename = (const char *) &amsg[1];
800 if ((size == 0) || (servicename[size - 1] != '\0'))
803 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
806 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
807 _("Preparing to stop `%s'\n"), servicename);
808 if (0 == strcasecmp (servicename, "arm"))
810 broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
811 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_STOPPING);
812 GNUNET_SERVER_client_persist_ (client);
813 GNUNET_SCHEDULER_add_now (trigger_shutdown, NULL);
814 GNUNET_SERVER_receive_done (client, GNUNET_OK);
817 sl = find_service (servicename);
820 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_NOT_KNOWN);
821 GNUNET_SERVER_receive_done (client, GNUNET_OK);
824 sl->is_default = GNUNET_NO;
825 if (GNUNET_YES == in_shutdown)
827 /* shutdown in progress */
828 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IN_SHUTDOWN);
829 GNUNET_SERVER_receive_done (client, GNUNET_OK);
832 if (sl->killing_client != NULL)
834 /* killing already in progress */
835 signal_result (client, servicename, request_id,
836 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
837 GNUNET_SERVER_receive_done (client, GNUNET_OK);
840 if (sl->proc == NULL)
842 /* process is down */
843 signal_result (client, servicename, request_id,
844 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
845 GNUNET_SERVER_receive_done (client, GNUNET_OK);
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "Sending kill signal to service `%s', waiting for process to die.\n",
851 broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
852 /* no signal_start - only when it's STOPPED */
853 sl->killed_at = GNUNET_TIME_absolute_get ();
854 if (0 != GNUNET_OS_process_kill (sl->proc, GNUNET_TERM_SIG))
855 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
856 sl->killing_client = client;
857 sl->killing_client_request_id = request_id;
858 GNUNET_SERVER_client_keep (client);
859 GNUNET_SERVER_receive_done (client, GNUNET_OK);
864 * Handle LIST-message.
866 * @param cls closure (always NULL)
867 * @param client identification of the client
868 * @param message the actual message
871 handle_list (void *cls, struct GNUNET_SERVER_Client *client,
872 const struct GNUNET_MessageHeader *message)
874 struct GNUNET_ARM_ListResultMessage *msg;
875 struct GNUNET_ARM_Message *request;
876 size_t string_list_size;
878 struct ServiceList *sl;
884 request = (struct GNUNET_ARM_Message *) message;
886 string_list_size = 0;
887 /* first count the running processes get their name's size */
888 for (sl = running_head; sl != NULL; sl = sl->next)
890 if (sl->proc != NULL)
892 string_list_size += strlen (sl->name);
893 string_list_size += strlen (sl->binary);
894 string_list_size += 4;
899 total_size = sizeof (struct GNUNET_ARM_ListResultMessage)
901 msg = GNUNET_malloc (total_size);
902 msg->arm_msg.header.size = total_size;
903 msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT;
904 msg->arm_msg.request_id = GNUNET_ntohll (request->request_id);
907 char *pos = (char *)&msg[1];
908 for (sl = running_head; sl != NULL; sl = sl->next)
910 if (sl->proc != NULL)
912 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
913 GNUNET_snprintf(pos, s, "%s (%s)", sl->name, sl->binary);
918 GNUNET_SERVER_notify_transmit_ready (client,
920 GNUNET_TIME_UNIT_FOREVER_REL,
921 write_list_result, msg);
922 GNUNET_SERVER_receive_done (client, GNUNET_OK);
927 * We are done with everything. Stop remaining
928 * tasks, signal handler and the server.
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last shutdown phase\n");
934 if (NULL != notifier)
936 GNUNET_SERVER_notification_context_destroy (notifier);
941 GNUNET_SERVER_destroy (server);
944 if (GNUNET_SCHEDULER_NO_TASK != child_death_task)
946 GNUNET_SCHEDULER_cancel (child_death_task);
947 child_death_task = GNUNET_SCHEDULER_NO_TASK;
953 list_count (struct ServiceList *running_head)
955 struct ServiceList *i;
956 unsigned int res = 0;
958 for (res = 0, i = running_head; i; i = i->next, res++)
959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967 * Task run for shutdown.
969 * @param cls closure, NULL if we need to self-restart
973 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
975 struct ServiceList *pos;
976 struct ServiceList *nxt;
977 struct ServiceListeningInfo *sli;
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "First shutdown phase\n");
981 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
983 GNUNET_SCHEDULER_cancel (child_restart_task);
984 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
986 in_shutdown = GNUNET_YES;
987 /* first, stop listening */
988 for (pos = running_head; NULL != pos; pos = pos->next)
990 while (NULL != (sli = pos->listen_head))
992 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
993 pos->listen_tail, sli);
994 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
996 GNUNET_SCHEDULER_cancel (sli->accept_task);
997 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
999 GNUNET_break (GNUNET_OK ==
1000 GNUNET_NETWORK_socket_close (sli->listen_socket));
1001 GNUNET_free (sli->service_addr);
1005 /* then, shutdown all existing service processes */
1007 while (NULL != (pos = nxt))
1010 if (pos->proc != NULL)
1012 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1013 "Stopping service `%s'\n",
1015 pos->killed_at = GNUNET_TIME_absolute_get ();
1016 if (0 != GNUNET_OS_process_kill (pos->proc, GNUNET_TERM_SIG))
1017 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1024 /* finally, should all service processes be already gone, terminate for real */
1025 if (running_head == NULL)
1028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1029 "Delaying shutdown, have %u childs still running\n",
1030 list_count (running_head));
1035 * Task run whenever it is time to restart a child that died.
1037 * @param cls closure, always NULL
1041 delayed_restart_task (void *cls,
1042 const struct GNUNET_SCHEDULER_TaskContext *tc)
1044 struct ServiceList *sl;
1045 struct GNUNET_TIME_Relative lowestRestartDelay;
1046 struct ServiceListeningInfo *sli;
1048 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
1049 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1051 GNUNET_assert (GNUNET_NO == in_shutdown);
1052 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1054 /* check for services that need to be restarted due to
1055 * configuration changes or because the last restart failed */
1056 for (sl = running_head; NULL != sl; sl = sl->next)
1058 if (NULL != sl->proc)
1060 /* service is currently not running */
1061 if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
1063 /* restart is now allowed */
1066 /* process should run by default, start immediately */
1067 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1068 _("Restarting service `%s'.\n"), sl->name);
1069 start_process (sl, NULL, 0);
1073 /* process is run on-demand, ensure it is re-started if there is demand */
1074 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1075 if (GNUNET_SCHEDULER_NO_TASK == sli->accept_task)
1077 /* accept was actually paused, so start it again */
1079 GNUNET_SCHEDULER_add_read_net
1080 (GNUNET_TIME_UNIT_FOREVER_REL, sli->listen_socket,
1081 &accept_connection, sli);
1087 /* update calculation for earliest time to reactivate a service */
1088 lowestRestartDelay =
1089 GNUNET_TIME_relative_min (lowestRestartDelay,
1090 GNUNET_TIME_absolute_get_remaining
1094 if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1097 "Will restart process in %s\n",
1098 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay, GNUNET_YES));
1099 child_restart_task =
1100 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1101 GNUNET_SCHEDULER_PRIORITY_IDLE,
1102 &delayed_restart_task, NULL);
1108 * Task triggered whenever we receive a SIGCHLD (child
1111 * @param cls closure, NULL if we need to self-restart
1115 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1117 struct ServiceList *pos;
1118 struct ServiceList *next;
1119 struct ServiceListeningInfo *sli;
1120 const char *statstr;
1124 enum GNUNET_OS_ProcessStatusType statusType;
1125 unsigned long statusCode;
1126 const struct GNUNET_DISK_FileHandle *pr;
1128 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
1129 child_death_task = GNUNET_SCHEDULER_NO_TASK;
1130 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1132 /* shutdown scheduled us, ignore! */
1134 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1135 pr, &maint_child_death, NULL);
1138 /* consume the signal */
1139 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
1141 /* check for services that died (WAITPID) */
1142 next = running_head;
1143 while (NULL != (pos = next))
1147 if (pos->proc == NULL)
1149 if (GNUNET_YES == in_shutdown)
1153 if ((GNUNET_SYSERR ==
1155 GNUNET_OS_process_status (pos->proc, &statusType, &statusCode)))
1156 || ((ret == GNUNET_NO) || (statusType == GNUNET_OS_PROCESS_STOPPED)
1157 || (statusType == GNUNET_OS_PROCESS_RUNNING)))
1159 if (statusType == GNUNET_OS_PROCESS_EXITED)
1161 statstr = _( /* process termination method */ "exit");
1162 statcode = statusCode;
1164 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1166 statstr = _( /* process termination method */ "signal");
1167 statcode = statusCode;
1171 statstr = _( /* process termination method */ "unknown");
1174 if (0 != pos->killed_at.abs_value_us)
1176 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1177 _("Service `%s' took %s to terminate\n"),
1179 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at), GNUNET_YES));
1181 GNUNET_OS_process_destroy (pos->proc);
1183 broadcast_status (pos->name, GNUNET_ARM_SERVICE_STOPPED, NULL);
1184 if (NULL != pos->killing_client)
1186 signal_result (pos->killing_client, pos->name,
1187 pos->killing_client_request_id, GNUNET_ARM_RESULT_STOPPED);
1188 GNUNET_SERVER_client_drop (pos->killing_client);
1189 pos->killing_client = NULL;
1190 pos->killing_client_request_id = 0;
1192 if (GNUNET_YES != in_shutdown)
1194 if ((statusType == GNUNET_OS_PROCESS_EXITED) && (statcode == 0))
1196 /* process terminated normally, allow restart at any time */
1197 pos->restart_at.abs_value_us = 0;
1198 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1199 _("Service `%s' terminated normally, will restart at any time\n"),
1201 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1202 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1204 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sli->accept_task);
1206 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1207 sli->listen_socket, &accept_connection, sli);
1212 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1213 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1214 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1215 pos->name, statstr, statcode,
1216 GNUNET_STRINGS_relative_time_to_string (pos->backoff, GNUNET_YES));
1217 /* schedule restart */
1218 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1219 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1220 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
1221 GNUNET_SCHEDULER_cancel (child_restart_task);
1222 child_restart_task = GNUNET_SCHEDULER_add_with_priority (
1223 GNUNET_SCHEDULER_PRIORITY_IDLE, &delayed_restart_task, NULL);
1231 child_death_task = GNUNET_SCHEDULER_add_read_file (
1232 GNUNET_TIME_UNIT_FOREVER_REL, pr, &maint_child_death, NULL);
1233 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1235 else if (GNUNET_YES == in_shutdown)
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 "Delaying shutdown after child's death, still have %u children\n",
1238 list_count (running_head));
1244 * Signal handler called for SIGCHLD. Triggers the
1245 * respective handler by writing to the trigger pipe.
1248 sighandler_child_death ()
1251 int old_errno = errno; /* back-up errno */
1254 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
1255 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
1257 errno = old_errno; /* restore errno */
1262 * Setup our service record for the given section in the configuration file
1263 * (assuming the section is for a service).
1266 * @param section a section in the configuration file
1267 * @return #GNUNET_OK (continue)
1270 setup_service (void *cls, const char *section)
1272 struct ServiceList *sl;
1276 struct sockaddr **addrs;
1277 socklen_t *addr_lens;
1281 if (strcasecmp (section, "arm") == 0)
1284 GNUNET_CONFIGURATION_get_value_string (cfg, section, "BINARY", &binary))
1286 /* not a service section */
1290 GNUNET_CONFIGURATION_have_value (cfg, section, "USER_SERVICE")) &&
1292 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "USER_SERVICE")))
1294 if (GNUNET_NO == start_user)
1295 return; /* user service, and we don't deal with those */
1299 if (GNUNET_NO == start_system)
1300 return; /* system service, and we don't deal with those */
1302 sl = find_service (section);
1305 /* got the same section twice!? */
1307 GNUNET_free (binary);
1312 GNUNET_CONFIGURATION_get_value_filename (cfg, section, "CONFIG",
1315 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", "DEFAULTCONFIG",
1317 (0 != STAT (config, &sbuf)))
1321 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1324 GNUNET_free (config);
1328 sl = GNUNET_new (struct ServiceList);
1329 sl->name = GNUNET_strdup (section);
1330 sl->binary = binary;
1331 sl->config = config;
1332 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1333 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1335 sl->pipe_control = GNUNET_YES;
1337 if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
1338 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
1340 GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
1343 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "AUTOSTART"))
1345 if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section, cfg,
1346 &addrs, &addr_lens)))
1348 /* this will free (or capture) addrs[i] */
1349 for (i = 0; i < ret; i++)
1350 create_listen_socket (addrs[i], addr_lens[i], sl);
1351 GNUNET_free (addrs);
1352 GNUNET_free (addr_lens);
1357 * A client connected, add it to the notification context.
1359 * @param cls closure
1360 * @param client identification of the client
1363 handle_client_connecting (void *cls, struct GNUNET_SERVER_Client *client)
1365 /* All clients are considered to be of the "monitor" kind
1366 * (that is, they don't affect ARM shutdown).
1369 GNUNET_SERVER_client_mark_monitor (client);
1374 * Handle MONITOR-message.
1376 * @param cls closure (always NULL)
1377 * @param client identification of the client
1378 * @param message the actual message
1379 * @return #GNUNET_OK to keep the connection open,
1380 * #GNUNET_SYSERR to close it (signal serious error)
1383 handle_monitor (void *cls, struct GNUNET_SERVER_Client *client,
1384 const struct GNUNET_MessageHeader *message)
1386 /* Removal is handled by the server implementation, internally. */
1387 if ((NULL != client) && (NULL != notifier))
1389 GNUNET_SERVER_notification_context_add (notifier, client);
1390 broadcast_status ("arm", GNUNET_ARM_SERVICE_MONITORING_STARTED, client);
1391 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1397 * Process arm requests.
1399 * @param cls closure
1400 * @param serv the initialized server
1401 * @param c configuration to use
1404 run (void *cls, struct GNUNET_SERVER_Handle *serv,
1405 const struct GNUNET_CONFIGURATION_Handle *c)
1407 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1408 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
1409 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
1410 {&handle_monitor, NULL, GNUNET_MESSAGE_TYPE_ARM_MONITOR,
1411 sizeof (struct GNUNET_MessageHeader)},
1412 {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST,
1413 sizeof (struct GNUNET_ARM_Message)},
1416 char *defaultservices;
1418 struct ServiceList *sl;
1422 GNUNET_assert (serv != NULL);
1423 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1426 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1427 GNUNET_DISK_pipe_handle (sigpipe,
1428 GNUNET_DISK_PIPE_END_READ),
1429 &maint_child_death, NULL);
1432 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_PREFIX",
1434 prefix_command = GNUNET_strdup ("");
1436 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
1438 final_option = GNUNET_strdup ("");
1440 GNUNET_CONFIGURATION_get_value_yesno (cfg, "ARM", "USER_ONLY"))
1442 GNUNET_break (GNUNET_YES == start_user);
1443 start_system = GNUNET_NO;
1447 GNUNET_CONFIGURATION_get_value_yesno (cfg, "ARM", "SYSTEM_ONLY"))
1449 GNUNET_break (GNUNET_YES == start_system);
1450 start_user = GNUNET_NO;
1453 GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
1455 /* start default services... */
1457 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "DEFAULTSERVICES",
1460 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1461 _("Starting default services `%s'\n"), defaultservices);
1462 if (0 < strlen (defaultservices))
1464 for (pos = strtok (defaultservices, " "); NULL != pos;
1465 pos = strtok (NULL, " "))
1467 sl = find_service (pos);
1470 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1472 ("Default service `%s' not configured correctly!\n"),
1476 sl->is_default = GNUNET_YES;
1477 start_process (sl, NULL, 0);
1480 GNUNET_free (defaultservices);
1484 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1486 ("No default services configured, GNUnet will not really start right now.\n"));
1490 GNUNET_SERVER_notification_context_create (server, MAX_NOTIFY_QUEUE);
1491 GNUNET_SERVER_connect_notify (server, handle_client_connecting, NULL);
1492 /* process client requests */
1493 GNUNET_SERVER_add_handlers (server, handlers);
1498 * The main function for the arm service.
1500 * @param argc number of arguments from the command line
1501 * @param argv command line arguments
1502 * @return 0 ok, 1 on error
1505 main (int argc, char *const *argv)
1508 struct GNUNET_SIGNAL_Context *shc_chld;
1510 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
1511 GNUNET_assert (sigpipe != NULL);
1513 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
1516 GNUNET_SERVICE_run (argc, argv, "arm",
1517 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, &run, NULL)) ? 0 : 1;
1518 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1520 GNUNET_DISK_pipe_close (sigpipe);
1530 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1532 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1534 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1535 mallopt (M_TOP_PAD, 1 * 1024);
1541 /* end of gnunet-service-arm.c */