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 pointer to "unit16_t*" with message type
244 * @param size number of bytes available in buf
245 * @param buf where to copy the message, NULL on error
246 * @return number of bytes copied to 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", (unsigned int) msg->result);
263 msize = msg->arm_msg.header.size;
264 GNUNET_assert (size >= msize);
265 msg->arm_msg.header.size = htons (msg->arm_msg.header.size);
266 msg->arm_msg.header.type = htons (msg->arm_msg.header.type);
267 msg->result = htonl (msg->result);
268 msg->arm_msg.request_id = GNUNET_htonll (msg->arm_msg.request_id);
269 memcpy (buf, msg, msize);
275 * Transmit the list of running services.
277 * @param cls pointer to struct GNUNET_ARM_ListResultMessage with the message
278 * @param size number of bytes available in buf
279 * @param buf where to copy the message, NULL on error
280 * @return number of bytes copied to buf
283 write_list_result (void *cls, size_t size, void *buf)
285 struct GNUNET_ARM_ListResultMessage *msg = cls;
290 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
291 _("Could not send list result to client\n"));
293 return 0; /* error, not much we can do */
296 rslt_size = msg->arm_msg.header.size;
297 GNUNET_assert (size >= rslt_size);
298 msg->arm_msg.header.size = htons (msg->arm_msg.header.size);
299 msg->arm_msg.header.type = htons (msg->arm_msg.header.type);
300 msg->arm_msg.request_id = GNUNET_htonll (msg->arm_msg.request_id);
301 msg->count = htons (msg->count);
303 memcpy (buf, msg, rslt_size);
310 * Signal our client that we will start or stop the
313 * @param client who is being signalled
314 * @param name name of the service
315 * @param result message type to send
316 * @return NULL if it was not found
319 signal_result (struct GNUNET_SERVER_Client *client, const char *name,
320 uint64_t request_id, enum GNUNET_ARM_Result result)
322 struct GNUNET_ARM_ResultMessage *msg;
325 msize = sizeof (struct GNUNET_ARM_ResultMessage);
326 msg = GNUNET_malloc (msize);
327 msg->arm_msg.header.size = msize;
328 msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_RESULT;
329 msg->result = result;
330 msg->arm_msg.request_id = request_id;
332 GNUNET_SERVER_notify_transmit_ready (client, msize,
333 GNUNET_TIME_UNIT_FOREVER_REL, write_result, msg);
338 * Tell all clients about status change of a service.
340 * @param name name of the service
341 * @param status message type to send
344 broadcast_status (const char *name, enum GNUNET_ARM_ServiceStatus status,
345 struct GNUNET_SERVER_Client *unicast)
347 struct GNUNET_ARM_StatusMessage *msg;
350 if (NULL == notifier)
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Sending status %u of service `%s' to client\n",
354 (unsigned int) status, name);
355 namelen = strlen (name);
356 msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_StatusMessage) + namelen + 1);
357 msg->header.size = htons (sizeof (struct GNUNET_ARM_StatusMessage) + namelen + 1);
358 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_STATUS);
359 msg->status = htonl ((uint32_t) (status));
360 memcpy ((char *) &msg[1], name, namelen + 1);
363 GNUNET_SERVER_notification_context_broadcast (notifier,
364 (struct GNUNET_MessageHeader *) msg, GNUNET_YES);
366 GNUNET_SERVER_notification_context_unicast (notifier, unicast,
367 (const struct GNUNET_MessageHeader *) msg, GNUNET_NO);
373 * Actually start the process for the given service.
375 * @param sl identifies service to start
376 * @param client that asked to start the service (may be NULL)
379 start_process (struct ServiceList *sl, struct GNUNET_SERVER_Client *client, uint64_t request_id)
389 struct ServiceListeningInfo *sli;
394 /* calculate listen socket list */
397 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
399 GNUNET_array_append (lsocks, ls,
400 GNUNET_NETWORK_get_fd (sli->listen_socket));
401 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
403 GNUNET_SCHEDULER_cancel (sli->accept_task);
404 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
408 GNUNET_array_append (lsocks, ls, INVALID_SOCKET);
410 GNUNET_array_append (lsocks, ls, -1);
413 /* obtain configuration */
415 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "PREFIX",
417 loprefix = GNUNET_strdup (prefix_command);
419 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "OPTIONS",
422 options = GNUNET_strdup (final_option);
423 if (NULL == strstr (options, "%"))
425 /* replace '{}' with service name */
426 while (NULL != (optpos = strstr (options, "{}")))
430 GNUNET_asprintf (&optpos, options, sl->name);
431 GNUNET_free (options);
434 /* replace '$PATH' with value associated with "PATH" */
435 while (NULL != (optpos = strstr (options, "$")))
438 while (isupper ((unsigned char) *optend))
447 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
449 val = GNUNET_strdup ("");
451 GNUNET_asprintf (&optpos, "%s%s%c%s", options, val, b, next);
452 GNUNET_free (options);
458 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
460 /* actually start process */
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 "Starting service `%s' using binary `%s' and configuration `%s'\n",
463 sl->name, sl->binary, sl->config);
464 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
465 GNUNET_assert (NULL == sl->proc);
466 if (GNUNET_YES == use_debug)
468 if (NULL == sl->config)
470 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
471 lsocks, loprefix, binary, "-L",
472 "DEBUG", options, NULL);
475 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
476 lsocks, loprefix, binary, "-c", sl->config, "-L",
477 "DEBUG", options, NULL);
481 if (NULL == sl->config)
483 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
484 lsocks, loprefix, binary,
488 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
489 lsocks, loprefix, binary, "-c", sl->config,
492 GNUNET_free (binary);
493 if (sl->proc == NULL)
495 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to start service `%s'\n"),
498 signal_result (client, sl->name, request_id, GNUNET_ARM_RESULT_START_FAILED);
502 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting service `%s'\n"),
504 broadcast_status (sl->name, GNUNET_ARM_SERVICE_STARTING, NULL);
506 signal_result (client, sl->name, request_id, GNUNET_ARM_RESULT_STARTING);
509 GNUNET_free (loprefix);
510 GNUNET_free (options);
511 GNUNET_array_grow (lsocks, ls, 0);
516 * Find the process with the given service
517 * name in the given list and return it.
519 * @param name which service entry to look up
520 * @return NULL if it was not found
522 static struct ServiceList *
523 find_service (const char *name)
525 struct ServiceList *sl;
530 if (0 == strcasecmp (sl->name, name))
539 * First connection has come to the listening socket associated with the service,
540 * create the service in order to relay the incoming connection to it
542 * @param cls callback data, struct ServiceListeningInfo describing a listen socket
546 accept_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
548 struct ServiceListeningInfo *sli = cls;
549 struct ServiceList *sl = sli->sl;
551 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
552 GNUNET_assert (GNUNET_NO == in_shutdown);
553 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
555 start_process (sl, NULL, 0);
560 * Creating a listening socket for each of the service's addresses and
561 * wait for the first incoming connection to it
563 * @param sa address associated with the service
564 * @param addr_len length of sa
565 * @param sl service entry for the service in question
568 create_listen_socket (struct sockaddr *sa, socklen_t addr_len,
569 struct ServiceList *sl)
572 struct GNUNET_NETWORK_Handle *sock;
573 struct ServiceListeningInfo *sli;
575 switch (sa->sa_family)
578 sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
581 sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
584 if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */
586 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
591 errno = EAFNOSUPPORT;
596 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
597 _("Unable to create socket for service `%s': %s\n"),
598 sl->name, STRERROR (errno));
602 if (GNUNET_NETWORK_socket_setsockopt
603 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
604 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
607 if ((sa->sa_family == AF_INET6) &&
608 (GNUNET_NETWORK_socket_setsockopt
609 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
610 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
614 if (GNUNET_NETWORK_socket_bind
615 (sock, (const struct sockaddr *) sa, addr_len) != GNUNET_OK)
617 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
619 ("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
620 sl->name, GNUNET_a2s (sa, addr_len), STRERROR (errno));
621 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
625 if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
627 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
628 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
632 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
633 _("ARM now monitors connections to service `%s' at `%s'\n"),
634 sl->name, GNUNET_a2s (sa, addr_len));
635 sli = GNUNET_malloc (sizeof (struct ServiceListeningInfo));
636 sli->service_addr = sa;
637 sli->service_addr_len = addr_len;
638 sli->listen_socket = sock;
641 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock,
642 &accept_connection, sli);
643 GNUNET_CONTAINER_DLL_insert (sl->listen_head, sl->listen_tail, sli);
648 * Remove and free an entry in the service list. Listen sockets
649 * must have already been cleaned up. Only to be called during shutdown.
651 * @param sl entry to free
654 free_service (struct ServiceList *sl)
656 GNUNET_assert (GNUNET_YES == in_shutdown);
657 GNUNET_CONTAINER_DLL_remove (running_head, running_tail, sl);
658 GNUNET_assert (NULL == sl->listen_head);
659 GNUNET_free_non_null (sl->config);
660 GNUNET_free_non_null (sl->binary);
661 GNUNET_free (sl->name);
667 * Handle START-message.
669 * @param cls closure (always NULL)
670 * @param client identification of the client
671 * @param message the actual message
672 * @return GNUNET_OK to keep the connection open,
673 * GNUNET_SYSERR to close it (signal serious error)
676 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
677 const struct GNUNET_MessageHeader *message)
679 const char *servicename;
680 struct ServiceList *sl;
683 struct GNUNET_ARM_Message *amsg;
685 amsg = (struct GNUNET_ARM_Message *) message;
686 request_id = GNUNET_ntohll (amsg->request_id);
687 size = ntohs (amsg->header.size);
688 size -= sizeof (struct GNUNET_ARM_Message);
689 servicename = (const char *) &amsg[1];
690 if ((size == 0) || (servicename[size - 1] != '\0'))
693 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
696 if (GNUNET_YES == in_shutdown)
698 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IN_SHUTDOWN);
699 GNUNET_SERVER_receive_done (client, GNUNET_OK);
702 sl = find_service (servicename);
705 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_NOT_KNOWN);
706 GNUNET_SERVER_receive_done (client, GNUNET_OK);
709 sl->is_default = GNUNET_YES;
710 if (sl->proc != NULL)
712 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
713 GNUNET_SERVER_receive_done (client, GNUNET_OK);
716 start_process (sl, client, request_id);
717 GNUNET_SERVER_receive_done (client, GNUNET_OK);
722 * Start a shutdown sequence.
724 * @param cls closure (refers to service)
725 * @param tc task context
728 trigger_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Triggering shutdown\n");
731 GNUNET_SCHEDULER_shutdown ();
736 * Handle STOP-message.
738 * @param cls closure (always NULL)
739 * @param client identification of the client
740 * @param message the actual message
741 * @return GNUNET_OK to keep the connection open,
742 * GNUNET_SYSERR to close it (signal serious error)
745 handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
746 const struct GNUNET_MessageHeader *message)
748 struct ServiceList *sl;
749 const char *servicename;
752 struct GNUNET_ARM_Message *amsg;
754 amsg = (struct GNUNET_ARM_Message *) message;
755 request_id = GNUNET_ntohll (amsg->request_id);
756 size = ntohs (amsg->header.size);
757 size -= sizeof (struct GNUNET_ARM_Message);
758 servicename = (const char *) &amsg[1];
759 if ((size == 0) || (servicename[size - 1] != '\0'))
762 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
765 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
766 _("Preparing to stop `%s'\n"), servicename);
767 if (0 == strcasecmp (servicename, "arm"))
769 broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
770 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_STOPPING);
771 GNUNET_SERVER_client_persist_ (client);
772 GNUNET_SCHEDULER_add_now (trigger_shutdown, NULL);
773 GNUNET_SERVER_receive_done (client, GNUNET_OK);
776 sl = find_service (servicename);
779 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_NOT_KNOWN);
780 GNUNET_SERVER_receive_done (client, GNUNET_OK);
783 sl->is_default = GNUNET_NO;
784 if (GNUNET_YES == in_shutdown)
786 /* shutdown in progress */
787 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IN_SHUTDOWN);
788 GNUNET_SERVER_receive_done (client, GNUNET_OK);
791 if (sl->killing_client != NULL)
793 /* killing already in progress */
794 signal_result (client, servicename, request_id,
795 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
796 GNUNET_SERVER_receive_done (client, GNUNET_OK);
799 if (sl->proc == NULL)
801 /* process is down */
802 signal_result (client, servicename, request_id,
803 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
804 GNUNET_SERVER_receive_done (client, GNUNET_OK);
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808 "Sending kill signal to service `%s', waiting for process to die.\n",
810 broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
811 /* no signal_start - only when it's STOPPED */
812 sl->killed_at = GNUNET_TIME_absolute_get ();
813 if (0 != GNUNET_OS_process_kill (sl->proc, SIGTERM))
814 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
815 sl->killing_client = client;
816 sl->killing_client_request_id = request_id;
817 GNUNET_SERVER_client_keep (client);
818 GNUNET_SERVER_receive_done (client, GNUNET_OK);
822 * Handle LIST-message.
824 * @param cls closure (always NULL)
825 * @param client identification of the client
826 * @param message the actual message
829 handle_list (void *cls, struct GNUNET_SERVER_Client *client,
830 const struct GNUNET_MessageHeader *message)
832 struct GNUNET_ARM_ListResultMessage *msg;
833 struct GNUNET_ARM_Message *request;
834 size_t string_list_size;
836 struct ServiceList *sl;
842 request = (struct GNUNET_ARM_Message *) message;
844 string_list_size = 0;
845 /* first count the running processes get their name's size */
846 for (sl = running_head; sl != NULL; sl = sl->next)
848 if (sl->proc != NULL)
850 string_list_size += strlen (sl->name);
851 string_list_size += strlen (sl->binary);
852 string_list_size += 4;
857 total_size = sizeof (struct GNUNET_ARM_ListResultMessage)
859 msg = GNUNET_malloc (total_size);
860 msg->arm_msg.header.size = total_size;
861 msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT;
862 msg->arm_msg.request_id = GNUNET_ntohll (request->request_id);
865 char *pos = (char *)&msg[1];
866 for (sl = running_head; sl != NULL; sl = sl->next)
868 if (sl->proc != NULL)
870 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
871 GNUNET_snprintf(pos, s, "%s (%s)", sl->name, sl->binary);
876 GNUNET_SERVER_notify_transmit_ready (client,
878 GNUNET_TIME_UNIT_FOREVER_REL,
879 write_list_result, msg);
880 GNUNET_SERVER_receive_done (client, GNUNET_OK);
884 * We are done with everything. Stop remaining
885 * tasks, signal handler and the server.
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last shutdown phase\n");
891 if (NULL != notifier)
893 GNUNET_SERVER_notification_context_destroy (notifier);
898 GNUNET_SERVER_destroy (server);
901 if (GNUNET_SCHEDULER_NO_TASK != child_death_task)
903 GNUNET_SCHEDULER_cancel (child_death_task);
904 child_death_task = GNUNET_SCHEDULER_NO_TASK;
909 list_count (struct ServiceList *running_head)
911 struct ServiceList *i;
912 unsigned int res = 0;
913 for (res = 0, i = running_head; i; i = i->next, res++)
914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s\n", i->name);
919 * Task run for shutdown.
921 * @param cls closure, NULL if we need to self-restart
925 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
927 struct ServiceList *pos;
928 struct ServiceList *nxt;
929 struct ServiceListeningInfo *sli;
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "First shutdown phase\n");
932 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
934 GNUNET_SCHEDULER_cancel (child_restart_task);
935 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
937 in_shutdown = GNUNET_YES;
938 /* first, stop listening */
939 for (pos = running_head; NULL != pos; pos = pos->next)
941 while (NULL != (sli = pos->listen_head))
943 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
944 pos->listen_tail, sli);
945 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
947 GNUNET_SCHEDULER_cancel (sli->accept_task);
948 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
950 GNUNET_break (GNUNET_OK ==
951 GNUNET_NETWORK_socket_close (sli->listen_socket));
952 GNUNET_free (sli->service_addr);
956 /* then, shutdown all existing service processes */
958 while (NULL != (pos = nxt))
961 if (pos->proc != NULL)
963 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping service `%s'\n",
965 pos->killed_at = GNUNET_TIME_absolute_get ();
966 if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
967 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
974 /* finally, should all service processes be already gone, terminate for real */
975 if (running_head == NULL)
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "Delaying shutdown, have %u childs still running\n", list_count (running_head));
984 * Task run whenever it is time to restart a child that died.
986 * @param cls closure, always NULL
990 delayed_restart_task (void *cls,
991 const struct GNUNET_SCHEDULER_TaskContext *tc)
993 struct ServiceList *sl;
994 struct GNUNET_TIME_Relative lowestRestartDelay;
995 struct ServiceListeningInfo *sli;
997 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
998 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1000 GNUNET_assert (GNUNET_NO == in_shutdown);
1001 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1003 /* check for services that need to be restarted due to
1004 * configuration changes or because the last restart failed */
1005 for (sl = running_head; NULL != sl; sl = sl->next)
1007 if (NULL != sl->proc)
1009 /* service is currently not running */
1010 if (GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value ==
1013 /* restart is now allowed */
1016 /* process should run by default, start immediately */
1017 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1018 _("Restarting service `%s'.\n"), sl->name);
1019 start_process (sl, NULL, 0);
1023 /* process is run on-demand, ensure it is re-started if there is demand */
1024 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1025 if (GNUNET_SCHEDULER_NO_TASK == sli->accept_task)
1027 /* accept was actually paused, so start it again */
1029 GNUNET_SCHEDULER_add_read_net
1030 (GNUNET_TIME_UNIT_FOREVER_REL, sli->listen_socket,
1031 &accept_connection, sli);
1037 /* update calculation for earliest time to reactivate a service */
1038 lowestRestartDelay =
1039 GNUNET_TIME_relative_min (lowestRestartDelay,
1040 GNUNET_TIME_absolute_get_remaining
1044 if (lowestRestartDelay.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1047 "Will restart process in %s\n",
1048 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay, GNUNET_YES));
1049 child_restart_task =
1050 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1051 GNUNET_SCHEDULER_PRIORITY_IDLE,
1052 &delayed_restart_task, NULL);
1058 * Task triggered whenever we receive a SIGCHLD (child
1061 * @param cls closure, NULL if we need to self-restart
1065 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1067 struct ServiceList *pos;
1068 struct ServiceList *next;
1069 struct ServiceListeningInfo *sli;
1070 const char *statstr;
1074 enum GNUNET_OS_ProcessStatusType statusType;
1075 unsigned long statusCode;
1076 const struct GNUNET_DISK_FileHandle *pr;
1078 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
1079 child_death_task = GNUNET_SCHEDULER_NO_TASK;
1080 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1082 /* shutdown scheduled us, ignore! */
1084 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1085 pr, &maint_child_death, NULL);
1088 /* consume the signal */
1089 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
1091 /* check for services that died (WAITPID) */
1092 next = running_head;
1093 while (NULL != (pos = next))
1097 if (pos->proc == NULL)
1099 if (GNUNET_YES == in_shutdown)
1103 if ((GNUNET_SYSERR ==
1105 GNUNET_OS_process_status (pos->proc, &statusType, &statusCode)))
1106 || ((ret == GNUNET_NO) || (statusType == GNUNET_OS_PROCESS_STOPPED)
1107 || (statusType == GNUNET_OS_PROCESS_RUNNING)))
1109 if (statusType == GNUNET_OS_PROCESS_EXITED)
1111 statstr = _( /* process termination method */ "exit");
1112 statcode = statusCode;
1114 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1116 statstr = _( /* process termination method */ "signal");
1117 statcode = statusCode;
1121 statstr = _( /* process termination method */ "unknown");
1124 if (0 != pos->killed_at.abs_value)
1126 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1127 _("Service `%s' took %s to terminate\n"),
1129 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at), GNUNET_YES));
1131 GNUNET_OS_process_destroy (pos->proc);
1133 broadcast_status (pos->name, GNUNET_ARM_SERVICE_STOPPED, NULL);
1134 if (NULL != pos->killing_client)
1136 signal_result (pos->killing_client, pos->name,
1137 pos->killing_client_request_id, GNUNET_ARM_RESULT_STOPPED);
1138 GNUNET_SERVER_client_drop (pos->killing_client);
1139 pos->killing_client = NULL;
1140 pos->killing_client_request_id = 0;
1141 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1142 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1144 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sli->accept_task);
1146 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1147 sli->listen_socket, &accept_connection, sli);
1150 if (GNUNET_YES != in_shutdown)
1152 if ((statusType == GNUNET_OS_PROCESS_EXITED) && (statcode == 0))
1154 /* process terminated normally, allow restart at any time */
1155 pos->restart_at.abs_value = 0;
1156 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1157 _("Service `%s' terminated normally, will restart at any time\n"),
1162 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1163 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1164 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1165 pos->name, statstr, statcode,
1166 GNUNET_STRINGS_relative_time_to_string (pos->backoff, GNUNET_YES));
1167 /* schedule restart */
1168 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1169 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1171 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
1172 GNUNET_SCHEDULER_cancel (child_restart_task);
1173 child_restart_task = GNUNET_SCHEDULER_add_with_priority (
1174 GNUNET_SCHEDULER_PRIORITY_IDLE, &delayed_restart_task, NULL);
1181 child_death_task = GNUNET_SCHEDULER_add_read_file (
1182 GNUNET_TIME_UNIT_FOREVER_REL, pr, &maint_child_death, NULL);
1183 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1185 else if (GNUNET_YES == in_shutdown)
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1187 "Delaying shutdown after child's death, still have %u children\n",
1188 list_count (running_head));
1194 * Signal handler called for SIGCHLD. Triggers the
1195 * respective handler by writing to the trigger pipe.
1198 sighandler_child_death ()
1201 int old_errno = errno; /* back-up errno */
1204 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
1205 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
1207 errno = old_errno; /* restore errno */
1212 * Setup our service record for the given section in the configuration file
1213 * (assuming the section is for a service).
1216 * @param section a section in the configuration file
1217 * @return GNUNET_OK (continue)
1220 setup_service (void *cls, const char *section)
1222 struct ServiceList *sl;
1226 struct sockaddr **addrs;
1227 socklen_t *addr_lens;
1231 if (strcasecmp (section, "arm") == 0)
1234 GNUNET_CONFIGURATION_get_value_string (cfg, section, "BINARY", &binary))
1236 /* not a service section */
1239 sl = find_service (section);
1242 /* got the same section twice!? */
1248 GNUNET_CONFIGURATION_get_value_filename (cfg, section, "CONFIG",
1251 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", "DEFAULTCONFIG",
1253 (0 != STAT (config, &sbuf)))
1257 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1260 GNUNET_free (config);
1264 sl = GNUNET_malloc (sizeof (struct ServiceList));
1265 sl->name = GNUNET_strdup (section);
1266 sl->binary = binary;
1267 sl->config = config;
1268 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1269 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1271 sl->pipe_control = GNUNET_YES;
1273 if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
1274 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
1276 GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
1278 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "AUTOSTART"))
1280 if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section, cfg,
1281 &addrs, &addr_lens)))
1283 /* this will free (or capture) addrs[i] */
1284 for (i = 0; i < ret; i++)
1285 create_listen_socket (addrs[i], addr_lens[i], sl);
1286 GNUNET_free (addrs);
1287 GNUNET_free (addr_lens);
1292 * A client connected, add it to the notification context.
1294 * @param cls closure
1295 * @param client identification of the client
1298 handle_client_connecting (void *cls, struct GNUNET_SERVER_Client *client)
1300 /* All clients are considered to be of the "monitor" kind
1301 * (that is, they don't affect ARM shutdown).
1304 GNUNET_SERVER_client_mark_monitor (client);
1308 * Handle MONITOR-message.
1310 * @param cls closure (always NULL)
1311 * @param client identification of the client
1312 * @param message the actual message
1313 * @return GNUNET_OK to keep the connection open,
1314 * GNUNET_SYSERR to close it (signal serious error)
1317 handle_monitor (void *cls, struct GNUNET_SERVER_Client *client,
1318 const struct GNUNET_MessageHeader *message)
1320 /* Removal is handled by the server implementation, internally. */
1321 if ((NULL != client) && (NULL != notifier))
1323 GNUNET_SERVER_notification_context_add (notifier, client);
1324 broadcast_status ("arm", GNUNET_ARM_SERVICE_MONITORING_STARTED, client);
1325 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1330 * Process arm requests.
1332 * @param cls closure
1333 * @param serv the initialized server
1334 * @param c configuration to use
1337 run (void *cls, struct GNUNET_SERVER_Handle *serv,
1338 const struct GNUNET_CONFIGURATION_Handle *c)
1340 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1341 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
1342 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
1343 {&handle_monitor, NULL, GNUNET_MESSAGE_TYPE_ARM_MONITOR,
1344 sizeof (struct GNUNET_ARM_Message)},
1345 {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST,
1346 sizeof (struct GNUNET_ARM_Message)},
1349 char *defaultservices;
1351 struct ServiceList *sl;
1355 GNUNET_assert (serv != NULL);
1356 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1359 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1360 GNUNET_DISK_pipe_handle (sigpipe,
1361 GNUNET_DISK_PIPE_END_READ),
1362 &maint_child_death, NULL);
1365 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_PREFIX",
1367 prefix_command = GNUNET_strdup ("");
1369 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
1371 final_option = GNUNET_strdup ("");
1373 GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
1375 /* start default services... */
1377 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "DEFAULTSERVICES",
1380 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1381 _("Starting default services `%s'\n"), defaultservices);
1382 if (0 < strlen (defaultservices))
1384 for (pos = strtok (defaultservices, " "); NULL != pos;
1385 pos = strtok (NULL, " "))
1387 sl = find_service (pos);
1390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1392 ("Default service `%s' not configured correctly!\n"),
1396 sl->is_default = GNUNET_YES;
1397 start_process (sl, NULL, 0);
1400 GNUNET_free (defaultservices);
1404 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1406 ("No default services configured, GNUnet will not really start right now.\n"));
1410 GNUNET_SERVER_notification_context_create (server, MAX_NOTIFY_QUEUE);
1411 GNUNET_SERVER_connect_notify (server, handle_client_connecting, NULL);
1412 /* process client requests */
1413 GNUNET_SERVER_add_handlers (server, handlers);
1418 * The main function for the arm service.
1420 * @param argc number of arguments from the command line
1421 * @param argv command line arguments
1422 * @return 0 ok, 1 on error
1425 main (int argc, char *const *argv)
1428 struct GNUNET_SIGNAL_Context *shc_chld;
1430 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
1431 GNUNET_assert (sigpipe != NULL);
1433 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
1436 GNUNET_SERVICE_run (argc, argv, "arm",
1437 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, &run, NULL)) ? 0 : 1;
1438 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1440 GNUNET_DISK_pipe_close (sigpipe);
1450 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1452 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1454 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1455 mallopt (M_TOP_PAD, 1 * 1024);
1461 /* end of gnunet-service-arm.c */