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
40 * List of our services.
46 * Record with information about a listen socket we have open.
48 struct ServiceListeningInfo
51 * This is a linked list.
53 struct ServiceListeningInfo *next;
56 * This is a linked list.
58 struct ServiceListeningInfo *prev;
61 * Address this socket is listening on.
63 struct sockaddr *service_addr;
66 * Service this listen socket is for.
68 struct ServiceList *sl;
71 * Number of bytes in 'service_addr'
73 socklen_t service_addr_len;
76 * Our listening socket.
78 struct GNUNET_NETWORK_Handle *listen_socket;
81 * Task doing the accepting.
83 GNUNET_SCHEDULER_TaskIdentifier accept_task;
89 * List of our services.
94 * This is a doubly-linked list.
96 struct ServiceList *next;
99 * This is a doubly-linked list.
101 struct ServiceList *prev;
104 * Linked list of listen sockets associated with this service.
106 struct ServiceListeningInfo *listen_head;
109 * Linked list of listen sockets associated with this service.
111 struct ServiceListeningInfo *listen_tail;
114 * Name of the service.
119 * Name of the binary used.
124 * Name of the configuration file used.
129 * Client to notify upon kill completion (waitpid), NULL
130 * if we should simply restart the process.
132 struct GNUNET_SERVER_Client *killing_client;
135 * ID of the request that killed the service (for reporting back).
137 uint64_t killing_client_request_id;
140 * Process structure pointer of the child.
142 struct GNUNET_OS_Process *proc;
145 * Process exponential backoff time
147 struct GNUNET_TIME_Relative backoff;
150 * Absolute time at which the process is scheduled to restart in case of death
152 struct GNUNET_TIME_Absolute restart_at;
155 * Time we asked the service to shut down (used to calculate time it took
156 * the service to terminate).
158 struct GNUNET_TIME_Absolute killed_at;
161 * Is this service to be started by default (or did a client tell us explicitly
162 * to start it)? GNUNET_NO if the service is started only upon 'accept' on a
163 * listen socket or possibly explicitly by a client changing the value.
168 * Should we use pipes to signal this process? (YES for Java binaries and if we
175 * List of running services.
177 static struct ServiceList *running_head;
180 * List of running services.
182 static struct ServiceList *running_tail;
187 static const struct GNUNET_CONFIGURATION_Handle *cfg;
190 * Command to prepend to each actual command.
192 static char *prefix_command;
195 * Option to append to each actual command.
197 static char *final_option;
200 * ID of task called whenever we get a SIGCHILD.
202 static GNUNET_SCHEDULER_TaskIdentifier child_death_task;
205 * ID of task called whenever the timeout for restarting a child
208 static GNUNET_SCHEDULER_TaskIdentifier child_restart_task;
211 * Pipe used to communicate shutdown via signal.
213 static struct GNUNET_DISK_PipeHandle *sigpipe;
216 * Are we in shutdown mode?
218 static int in_shutdown;
221 * Handle to our server instance. Our server is a bit special in that
222 * its service is not immediately stopped once we get a shutdown
223 * request (since we need to continue service until all of our child
224 * processes are dead). This handle is used to shut down the server
225 * (and thus trigger process termination) once all child processes are
226 * also dead. A special option in the ARM configuration modifies the
227 * behaviour of the service implementation to not do the shutdown
230 static struct GNUNET_SERVER_Handle *server;
233 * Context for notifications we need to send to our clients.
235 static struct GNUNET_SERVER_NotificationContext *notifier;
238 #include "do_start_process.c"
241 * Transmit a status result message.
243 * @param cls a `unit16_t *` with message type
244 * @param size number of bytes available in @a buf
245 * @param buf where to copy the message, NULL on error
246 * @return number of bytes copied to @a buf
249 write_result (void *cls, size_t size, void *buf)
251 struct GNUNET_ARM_ResultMessage *msg = cls;
256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
257 _("Could not send status result to client\n"));
259 return 0; /* error, not much we can do */
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262 "Sending status response %u to client\n",
263 (unsigned int) msg->result);
264 msize = msg->arm_msg.header.size;
265 GNUNET_assert (size >= msize);
266 msg->arm_msg.header.size = htons (msg->arm_msg.header.size);
267 msg->arm_msg.header.type = htons (msg->arm_msg.header.type);
268 msg->result = htonl (msg->result);
269 msg->arm_msg.request_id = GNUNET_htonll (msg->arm_msg.request_id);
270 memcpy (buf, msg, msize);
277 * Transmit the list of running services.
279 * @param cls pointer to `struct GNUNET_ARM_ListResultMessage` with the message
280 * @param size number of bytes available in @a buf
281 * @param buf where to copy the message, NULL on error
282 * @return number of bytes copied to @a buf
285 write_list_result (void *cls, size_t size, void *buf)
287 struct GNUNET_ARM_ListResultMessage *msg = cls;
292 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
293 _("Could not send list result to client\n"));
295 return 0; /* error, not much we can do */
298 rslt_size = msg->arm_msg.header.size;
299 GNUNET_assert (size >= rslt_size);
300 msg->arm_msg.header.size = htons (msg->arm_msg.header.size);
301 msg->arm_msg.header.type = htons (msg->arm_msg.header.type);
302 msg->arm_msg.request_id = GNUNET_htonll (msg->arm_msg.request_id);
303 msg->count = htons (msg->count);
305 memcpy (buf, msg, rslt_size);
312 * Signal our client that we will start or stop the
315 * @param client who is being signalled
316 * @param name name of the service
317 * @param request_id id of the request that is being responded to.
318 * @param result message type to send
319 * @return NULL if it was not found
322 signal_result (struct GNUNET_SERVER_Client *client,
325 enum GNUNET_ARM_Result result)
327 struct GNUNET_ARM_ResultMessage *msg;
330 msize = sizeof (struct GNUNET_ARM_ResultMessage);
331 msg = GNUNET_malloc (msize);
332 msg->arm_msg.header.size = msize;
333 msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_RESULT;
334 msg->result = result;
335 msg->arm_msg.request_id = request_id;
337 GNUNET_SERVER_notify_transmit_ready (client, msize,
338 GNUNET_TIME_UNIT_FOREVER_REL,
344 * Tell all clients about status change of a service.
346 * @param name name of the service
347 * @param status message type to send
348 * @param unicast if not NULL, send to this client only.
349 * otherwise, send to all clients in the notifier
352 broadcast_status (const char *name,
353 enum GNUNET_ARM_ServiceStatus status,
354 struct GNUNET_SERVER_Client *unicast)
356 struct GNUNET_ARM_StatusMessage *msg;
359 if (NULL == notifier)
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Sending status %u of service `%s' to client\n",
363 (unsigned int) status, name);
364 namelen = strlen (name);
365 msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_StatusMessage) + namelen + 1);
366 msg->header.size = htons (sizeof (struct GNUNET_ARM_StatusMessage) + namelen + 1);
367 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_STATUS);
368 msg->status = htonl ((uint32_t) (status));
369 memcpy ((char *) &msg[1], name, namelen + 1);
372 GNUNET_SERVER_notification_context_broadcast (notifier,
373 (struct GNUNET_MessageHeader *) msg, GNUNET_YES);
375 GNUNET_SERVER_notification_context_unicast (notifier, unicast,
376 (const struct GNUNET_MessageHeader *) msg, GNUNET_NO);
382 * Actually start the process for the given service.
384 * @param sl identifies service to start
385 * @param client that asked to start the service (may be NULL)
386 * @param request_id id of the request in response to which the process is
387 * being started. 0 if starting was not requested.
390 start_process (struct ServiceList *sl, struct GNUNET_SERVER_Client *client, uint64_t request_id)
400 struct ServiceListeningInfo *sli;
405 /* calculate listen socket list */
408 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
410 GNUNET_array_append (lsocks, ls,
411 GNUNET_NETWORK_get_fd (sli->listen_socket));
412 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
414 GNUNET_SCHEDULER_cancel (sli->accept_task);
415 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
419 GNUNET_array_append (lsocks, ls, INVALID_SOCKET);
421 GNUNET_array_append (lsocks, ls, -1);
424 /* obtain configuration */
426 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "PREFIX",
428 loprefix = GNUNET_strdup (prefix_command);
430 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "OPTIONS",
433 options = GNUNET_strdup (final_option);
434 if (NULL == strstr (options, "%"))
436 /* replace '{}' with service name */
437 while (NULL != (optpos = strstr (options, "{}")))
441 GNUNET_asprintf (&optpos, options, sl->name);
442 GNUNET_free (options);
445 /* replace '$PATH' with value associated with "PATH" */
446 while (NULL != (optpos = strstr (options, "$")))
449 while (isupper ((unsigned char) *optend))
458 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
460 val = GNUNET_strdup ("");
462 GNUNET_asprintf (&optpos, "%s%s%c%s", options, val, b, next);
463 GNUNET_free (options);
469 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
471 /* actually start process */
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Starting service `%s' using binary `%s' and configuration `%s'\n",
474 sl->name, sl->binary, sl->config);
475 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
476 GNUNET_assert (NULL == sl->proc);
477 if (GNUNET_YES == use_debug)
479 if (NULL == sl->config)
481 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
482 lsocks, loprefix, binary, "-L",
483 "DEBUG", options, NULL);
486 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
487 lsocks, loprefix, binary, "-c", sl->config, "-L",
488 "DEBUG", options, NULL);
492 if (NULL == sl->config)
494 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
495 lsocks, loprefix, binary,
499 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
500 lsocks, loprefix, binary, "-c", sl->config,
503 GNUNET_free (binary);
504 if (sl->proc == NULL)
506 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to start service `%s'\n"),
509 signal_result (client, sl->name, request_id, GNUNET_ARM_RESULT_START_FAILED);
513 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting service `%s'\n"),
515 broadcast_status (sl->name, GNUNET_ARM_SERVICE_STARTING, NULL);
517 signal_result (client, sl->name, request_id, GNUNET_ARM_RESULT_STARTING);
520 GNUNET_free (loprefix);
521 GNUNET_free (options);
522 GNUNET_array_grow (lsocks, ls, 0);
527 * Find the process with the given service
528 * name in the given list and return it.
530 * @param name which service entry to look up
531 * @return NULL if it was not found
533 static struct ServiceList *
534 find_service (const char *name)
536 struct ServiceList *sl;
541 if (0 == strcasecmp (sl->name, name))
550 * First connection has come to the listening socket associated with the service,
551 * create the service in order to relay the incoming connection to it
553 * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
557 accept_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
559 struct ServiceListeningInfo *sli = cls;
560 struct ServiceList *sl = sli->sl;
562 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
563 GNUNET_assert (GNUNET_NO == in_shutdown);
564 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
566 start_process (sl, NULL, 0);
571 * Creating a listening socket for each of the service's addresses and
572 * wait for the first incoming connection to it
574 * @param sa address associated with the service
575 * @param addr_len length of @a sa
576 * @param sl service entry for the service in question
579 create_listen_socket (struct sockaddr *sa, socklen_t addr_len,
580 struct ServiceList *sl)
583 struct GNUNET_NETWORK_Handle *sock;
584 struct ServiceListeningInfo *sli;
586 switch (sa->sa_family)
589 sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
592 sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
595 if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */
597 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
602 errno = EAFNOSUPPORT;
607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
608 _("Unable to create socket for service `%s': %s\n"),
609 sl->name, STRERROR (errno));
613 if (GNUNET_NETWORK_socket_setsockopt
614 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
615 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
618 if ((sa->sa_family == AF_INET6) &&
619 (GNUNET_NETWORK_socket_setsockopt
620 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
621 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
625 if (GNUNET_NETWORK_socket_bind
626 (sock, (const struct sockaddr *) sa, addr_len, 0) != GNUNET_OK)
628 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
630 ("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
631 sl->name, GNUNET_a2s (sa, addr_len), STRERROR (errno));
632 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
636 if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
638 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
639 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
643 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
644 _("ARM now monitors connections to service `%s' at `%s'\n"),
645 sl->name, GNUNET_a2s (sa, addr_len));
646 sli = GNUNET_malloc (sizeof (struct ServiceListeningInfo));
647 sli->service_addr = sa;
648 sli->service_addr_len = addr_len;
649 sli->listen_socket = sock;
652 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock,
653 &accept_connection, sli);
654 GNUNET_CONTAINER_DLL_insert (sl->listen_head, sl->listen_tail, sli);
659 * Remove and free an entry in the service list. Listen sockets
660 * must have already been cleaned up. Only to be called during shutdown.
662 * @param sl entry to free
665 free_service (struct ServiceList *sl)
667 GNUNET_assert (GNUNET_YES == in_shutdown);
668 GNUNET_CONTAINER_DLL_remove (running_head, running_tail, sl);
669 GNUNET_assert (NULL == sl->listen_head);
670 GNUNET_free_non_null (sl->config);
671 GNUNET_free_non_null (sl->binary);
672 GNUNET_free (sl->name);
678 * Handle START-message.
680 * @param cls closure (always NULL)
681 * @param client identification of the client
682 * @param message the actual message
683 * @return #GNUNET_OK to keep the connection open,
684 * #GNUNET_SYSERR to close it (signal serious error)
687 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
688 const struct GNUNET_MessageHeader *message)
690 const char *servicename;
691 struct ServiceList *sl;
694 struct GNUNET_ARM_Message *amsg;
696 amsg = (struct GNUNET_ARM_Message *) message;
697 request_id = GNUNET_ntohll (amsg->request_id);
698 size = ntohs (amsg->header.size);
699 size -= sizeof (struct GNUNET_ARM_Message);
700 servicename = (const char *) &amsg[1];
701 if ((size == 0) || (servicename[size - 1] != '\0'))
704 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
707 if (GNUNET_YES == in_shutdown)
709 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IN_SHUTDOWN);
710 GNUNET_SERVER_receive_done (client, GNUNET_OK);
713 sl = find_service (servicename);
716 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_NOT_KNOWN);
717 GNUNET_SERVER_receive_done (client, GNUNET_OK);
720 sl->is_default = GNUNET_YES;
721 if (sl->proc != NULL)
723 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
724 GNUNET_SERVER_receive_done (client, GNUNET_OK);
727 start_process (sl, client, request_id);
728 GNUNET_SERVER_receive_done (client, GNUNET_OK);
733 * Start a shutdown sequence.
735 * @param cls closure (refers to service)
736 * @param tc task context
739 trigger_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Triggering shutdown\n");
742 GNUNET_SCHEDULER_shutdown ();
747 * Handle STOP-message.
749 * @param cls closure (always NULL)
750 * @param client identification of the client
751 * @param message the actual message
752 * @return #GNUNET_OK to keep the connection open,
753 * #GNUNET_SYSERR to close it (signal serious error)
756 handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
757 const struct GNUNET_MessageHeader *message)
759 struct ServiceList *sl;
760 const char *servicename;
763 struct GNUNET_ARM_Message *amsg;
765 amsg = (struct GNUNET_ARM_Message *) message;
766 request_id = GNUNET_ntohll (amsg->request_id);
767 size = ntohs (amsg->header.size);
768 size -= sizeof (struct GNUNET_ARM_Message);
769 servicename = (const char *) &amsg[1];
770 if ((size == 0) || (servicename[size - 1] != '\0'))
773 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
776 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
777 _("Preparing to stop `%s'\n"), servicename);
778 if (0 == strcasecmp (servicename, "arm"))
780 broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
781 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_STOPPING);
782 GNUNET_SERVER_client_persist_ (client);
783 GNUNET_SCHEDULER_add_now (trigger_shutdown, NULL);
784 GNUNET_SERVER_receive_done (client, GNUNET_OK);
787 sl = find_service (servicename);
790 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_NOT_KNOWN);
791 GNUNET_SERVER_receive_done (client, GNUNET_OK);
794 sl->is_default = GNUNET_NO;
795 if (GNUNET_YES == in_shutdown)
797 /* shutdown in progress */
798 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IN_SHUTDOWN);
799 GNUNET_SERVER_receive_done (client, GNUNET_OK);
802 if (sl->killing_client != NULL)
804 /* killing already in progress */
805 signal_result (client, servicename, request_id,
806 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
807 GNUNET_SERVER_receive_done (client, GNUNET_OK);
810 if (sl->proc == NULL)
812 /* process is down */
813 signal_result (client, servicename, request_id,
814 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
815 GNUNET_SERVER_receive_done (client, GNUNET_OK);
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819 "Sending kill signal to service `%s', waiting for process to die.\n",
821 broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
822 /* no signal_start - only when it's STOPPED */
823 sl->killed_at = GNUNET_TIME_absolute_get ();
824 if (0 != GNUNET_OS_process_kill (sl->proc, SIGTERM))
825 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
826 sl->killing_client = client;
827 sl->killing_client_request_id = request_id;
828 GNUNET_SERVER_client_keep (client);
829 GNUNET_SERVER_receive_done (client, GNUNET_OK);
834 * Handle LIST-message.
836 * @param cls closure (always NULL)
837 * @param client identification of the client
838 * @param message the actual message
841 handle_list (void *cls, struct GNUNET_SERVER_Client *client,
842 const struct GNUNET_MessageHeader *message)
844 struct GNUNET_ARM_ListResultMessage *msg;
845 struct GNUNET_ARM_Message *request;
846 size_t string_list_size;
848 struct ServiceList *sl;
854 request = (struct GNUNET_ARM_Message *) message;
856 string_list_size = 0;
857 /* first count the running processes get their name's size */
858 for (sl = running_head; sl != NULL; sl = sl->next)
860 if (sl->proc != NULL)
862 string_list_size += strlen (sl->name);
863 string_list_size += strlen (sl->binary);
864 string_list_size += 4;
869 total_size = sizeof (struct GNUNET_ARM_ListResultMessage)
871 msg = GNUNET_malloc (total_size);
872 msg->arm_msg.header.size = total_size;
873 msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT;
874 msg->arm_msg.request_id = GNUNET_ntohll (request->request_id);
877 char *pos = (char *)&msg[1];
878 for (sl = running_head; sl != NULL; sl = sl->next)
880 if (sl->proc != NULL)
882 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
883 GNUNET_snprintf(pos, s, "%s (%s)", sl->name, sl->binary);
888 GNUNET_SERVER_notify_transmit_ready (client,
890 GNUNET_TIME_UNIT_FOREVER_REL,
891 write_list_result, msg);
892 GNUNET_SERVER_receive_done (client, GNUNET_OK);
897 * We are done with everything. Stop remaining
898 * tasks, signal handler and the server.
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last shutdown phase\n");
904 if (NULL != notifier)
906 GNUNET_SERVER_notification_context_destroy (notifier);
911 GNUNET_SERVER_destroy (server);
914 if (GNUNET_SCHEDULER_NO_TASK != child_death_task)
916 GNUNET_SCHEDULER_cancel (child_death_task);
917 child_death_task = GNUNET_SCHEDULER_NO_TASK;
923 list_count (struct ServiceList *running_head)
925 struct ServiceList *i;
926 unsigned int res = 0;
928 for (res = 0, i = running_head; i; i = i->next, res++)
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
937 * Task run for shutdown.
939 * @param cls closure, NULL if we need to self-restart
943 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
945 struct ServiceList *pos;
946 struct ServiceList *nxt;
947 struct ServiceListeningInfo *sli;
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "First shutdown phase\n");
951 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
953 GNUNET_SCHEDULER_cancel (child_restart_task);
954 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
956 in_shutdown = GNUNET_YES;
957 /* first, stop listening */
958 for (pos = running_head; NULL != pos; pos = pos->next)
960 while (NULL != (sli = pos->listen_head))
962 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
963 pos->listen_tail, sli);
964 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
966 GNUNET_SCHEDULER_cancel (sli->accept_task);
967 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
969 GNUNET_break (GNUNET_OK ==
970 GNUNET_NETWORK_socket_close (sli->listen_socket));
971 GNUNET_free (sli->service_addr);
975 /* then, shutdown all existing service processes */
977 while (NULL != (pos = nxt))
980 if (pos->proc != NULL)
982 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
983 "Stopping service `%s'\n",
985 pos->killed_at = GNUNET_TIME_absolute_get ();
986 if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
987 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
994 /* finally, should all service processes be already gone, terminate for real */
995 if (running_head == NULL)
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "Delaying shutdown, have %u childs still running\n",
1000 list_count (running_head));
1005 * Task run whenever it is time to restart a child that died.
1007 * @param cls closure, always NULL
1011 delayed_restart_task (void *cls,
1012 const struct GNUNET_SCHEDULER_TaskContext *tc)
1014 struct ServiceList *sl;
1015 struct GNUNET_TIME_Relative lowestRestartDelay;
1016 struct ServiceListeningInfo *sli;
1018 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
1019 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1021 GNUNET_assert (GNUNET_NO == in_shutdown);
1022 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1024 /* check for services that need to be restarted due to
1025 * configuration changes or because the last restart failed */
1026 for (sl = running_head; NULL != sl; sl = sl->next)
1028 if (NULL != sl->proc)
1030 /* service is currently not running */
1031 if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
1033 /* restart is now allowed */
1036 /* process should run by default, start immediately */
1037 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1038 _("Restarting service `%s'.\n"), sl->name);
1039 start_process (sl, NULL, 0);
1043 /* process is run on-demand, ensure it is re-started if there is demand */
1044 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1045 if (GNUNET_SCHEDULER_NO_TASK == sli->accept_task)
1047 /* accept was actually paused, so start it again */
1049 GNUNET_SCHEDULER_add_read_net
1050 (GNUNET_TIME_UNIT_FOREVER_REL, sli->listen_socket,
1051 &accept_connection, sli);
1057 /* update calculation for earliest time to reactivate a service */
1058 lowestRestartDelay =
1059 GNUNET_TIME_relative_min (lowestRestartDelay,
1060 GNUNET_TIME_absolute_get_remaining
1064 if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1067 "Will restart process in %s\n",
1068 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay, GNUNET_YES));
1069 child_restart_task =
1070 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1071 GNUNET_SCHEDULER_PRIORITY_IDLE,
1072 &delayed_restart_task, NULL);
1078 * Task triggered whenever we receive a SIGCHLD (child
1081 * @param cls closure, NULL if we need to self-restart
1085 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1087 struct ServiceList *pos;
1088 struct ServiceList *next;
1089 struct ServiceListeningInfo *sli;
1090 const char *statstr;
1094 enum GNUNET_OS_ProcessStatusType statusType;
1095 unsigned long statusCode;
1096 const struct GNUNET_DISK_FileHandle *pr;
1098 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
1099 child_death_task = GNUNET_SCHEDULER_NO_TASK;
1100 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1102 /* shutdown scheduled us, ignore! */
1104 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1105 pr, &maint_child_death, NULL);
1108 /* consume the signal */
1109 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
1111 /* check for services that died (WAITPID) */
1112 next = running_head;
1113 while (NULL != (pos = next))
1117 if (pos->proc == NULL)
1119 if (GNUNET_YES == in_shutdown)
1123 if ((GNUNET_SYSERR ==
1125 GNUNET_OS_process_status (pos->proc, &statusType, &statusCode)))
1126 || ((ret == GNUNET_NO) || (statusType == GNUNET_OS_PROCESS_STOPPED)
1127 || (statusType == GNUNET_OS_PROCESS_RUNNING)))
1129 if (statusType == GNUNET_OS_PROCESS_EXITED)
1131 statstr = _( /* process termination method */ "exit");
1132 statcode = statusCode;
1134 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1136 statstr = _( /* process termination method */ "signal");
1137 statcode = statusCode;
1141 statstr = _( /* process termination method */ "unknown");
1144 if (0 != pos->killed_at.abs_value_us)
1146 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1147 _("Service `%s' took %s to terminate\n"),
1149 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at), GNUNET_YES));
1151 GNUNET_OS_process_destroy (pos->proc);
1153 broadcast_status (pos->name, GNUNET_ARM_SERVICE_STOPPED, NULL);
1154 if (NULL != pos->killing_client)
1156 signal_result (pos->killing_client, pos->name,
1157 pos->killing_client_request_id, GNUNET_ARM_RESULT_STOPPED);
1158 GNUNET_SERVER_client_drop (pos->killing_client);
1159 pos->killing_client = NULL;
1160 pos->killing_client_request_id = 0;
1162 if (GNUNET_YES != in_shutdown)
1164 if ((statusType == GNUNET_OS_PROCESS_EXITED) && (statcode == 0))
1166 /* process terminated normally, allow restart at any time */
1167 pos->restart_at.abs_value_us = 0;
1168 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1169 _("Service `%s' terminated normally, will restart at any time\n"),
1171 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1172 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1174 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sli->accept_task);
1176 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1177 sli->listen_socket, &accept_connection, sli);
1182 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1183 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1184 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1185 pos->name, statstr, statcode,
1186 GNUNET_STRINGS_relative_time_to_string (pos->backoff, GNUNET_YES));
1187 /* schedule restart */
1188 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1189 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1190 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
1191 GNUNET_SCHEDULER_cancel (child_restart_task);
1192 child_restart_task = GNUNET_SCHEDULER_add_with_priority (
1193 GNUNET_SCHEDULER_PRIORITY_IDLE, &delayed_restart_task, NULL);
1201 child_death_task = GNUNET_SCHEDULER_add_read_file (
1202 GNUNET_TIME_UNIT_FOREVER_REL, pr, &maint_child_death, NULL);
1203 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1205 else if (GNUNET_YES == in_shutdown)
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "Delaying shutdown after child's death, still have %u children\n",
1208 list_count (running_head));
1214 * Signal handler called for SIGCHLD. Triggers the
1215 * respective handler by writing to the trigger pipe.
1218 sighandler_child_death ()
1221 int old_errno = errno; /* back-up errno */
1224 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
1225 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
1227 errno = old_errno; /* restore errno */
1232 * Setup our service record for the given section in the configuration file
1233 * (assuming the section is for a service).
1236 * @param section a section in the configuration file
1237 * @return #GNUNET_OK (continue)
1240 setup_service (void *cls, const char *section)
1242 struct ServiceList *sl;
1246 struct sockaddr **addrs;
1247 socklen_t *addr_lens;
1251 if (strcasecmp (section, "arm") == 0)
1254 GNUNET_CONFIGURATION_get_value_string (cfg, section, "BINARY", &binary))
1256 /* not a service section */
1259 sl = find_service (section);
1262 /* got the same section twice!? */
1264 GNUNET_free (binary);
1269 GNUNET_CONFIGURATION_get_value_filename (cfg, section, "CONFIG",
1272 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", "DEFAULTCONFIG",
1274 (0 != STAT (config, &sbuf)))
1278 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1281 GNUNET_free (config);
1285 sl = GNUNET_new (struct ServiceList);
1286 sl->name = GNUNET_strdup (section);
1287 sl->binary = binary;
1288 sl->config = config;
1289 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1290 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1292 sl->pipe_control = GNUNET_YES;
1294 if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
1295 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
1297 GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
1299 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "AUTOSTART"))
1301 if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section, cfg,
1302 &addrs, &addr_lens)))
1304 /* this will free (or capture) addrs[i] */
1305 for (i = 0; i < ret; i++)
1306 create_listen_socket (addrs[i], addr_lens[i], sl);
1307 GNUNET_free (addrs);
1308 GNUNET_free (addr_lens);
1313 * A client connected, add it to the notification context.
1315 * @param cls closure
1316 * @param client identification of the client
1319 handle_client_connecting (void *cls, struct GNUNET_SERVER_Client *client)
1321 /* All clients are considered to be of the "monitor" kind
1322 * (that is, they don't affect ARM shutdown).
1325 GNUNET_SERVER_client_mark_monitor (client);
1330 * Handle MONITOR-message.
1332 * @param cls closure (always NULL)
1333 * @param client identification of the client
1334 * @param message the actual message
1335 * @return #GNUNET_OK to keep the connection open,
1336 * #GNUNET_SYSERR to close it (signal serious error)
1339 handle_monitor (void *cls, struct GNUNET_SERVER_Client *client,
1340 const struct GNUNET_MessageHeader *message)
1342 /* Removal is handled by the server implementation, internally. */
1343 if ((NULL != client) && (NULL != notifier))
1345 GNUNET_SERVER_notification_context_add (notifier, client);
1346 broadcast_status ("arm", GNUNET_ARM_SERVICE_MONITORING_STARTED, client);
1347 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1353 * Process arm requests.
1355 * @param cls closure
1356 * @param serv the initialized server
1357 * @param c configuration to use
1360 run (void *cls, struct GNUNET_SERVER_Handle *serv,
1361 const struct GNUNET_CONFIGURATION_Handle *c)
1363 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1364 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
1365 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
1366 {&handle_monitor, NULL, GNUNET_MESSAGE_TYPE_ARM_MONITOR,
1367 sizeof (struct GNUNET_MessageHeader)},
1368 {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST,
1369 sizeof (struct GNUNET_ARM_Message)},
1372 char *defaultservices;
1374 struct ServiceList *sl;
1378 GNUNET_assert (serv != NULL);
1379 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1382 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1383 GNUNET_DISK_pipe_handle (sigpipe,
1384 GNUNET_DISK_PIPE_END_READ),
1385 &maint_child_death, NULL);
1388 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_PREFIX",
1390 prefix_command = GNUNET_strdup ("");
1392 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
1394 final_option = GNUNET_strdup ("");
1396 GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
1398 /* start default services... */
1400 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "DEFAULTSERVICES",
1403 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1404 _("Starting default services `%s'\n"), defaultservices);
1405 if (0 < strlen (defaultservices))
1407 for (pos = strtok (defaultservices, " "); NULL != pos;
1408 pos = strtok (NULL, " "))
1410 sl = find_service (pos);
1413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1415 ("Default service `%s' not configured correctly!\n"),
1419 sl->is_default = GNUNET_YES;
1420 start_process (sl, NULL, 0);
1423 GNUNET_free (defaultservices);
1427 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1429 ("No default services configured, GNUnet will not really start right now.\n"));
1433 GNUNET_SERVER_notification_context_create (server, MAX_NOTIFY_QUEUE);
1434 GNUNET_SERVER_connect_notify (server, handle_client_connecting, NULL);
1435 /* process client requests */
1436 GNUNET_SERVER_add_handlers (server, handlers);
1441 * The main function for the arm service.
1443 * @param argc number of arguments from the command line
1444 * @param argv command line arguments
1445 * @return 0 ok, 1 on error
1448 main (int argc, char *const *argv)
1451 struct GNUNET_SIGNAL_Context *shc_chld;
1453 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
1454 GNUNET_assert (sigpipe != NULL);
1456 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
1459 GNUNET_SERVICE_run (argc, argv, "arm",
1460 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, &run, NULL)) ? 0 : 1;
1461 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1463 GNUNET_DISK_pipe_close (sigpipe);
1473 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1475 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1477 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1478 mallopt (M_TOP_PAD, 1 * 1024);
1484 /* end of gnunet-service-arm.c */