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 request_id id of the request that is being responded to.
316 * @param result message type to send
317 * @return NULL if it was not found
320 signal_result (struct GNUNET_SERVER_Client *client, const char *name,
321 uint64_t request_id, enum GNUNET_ARM_Result result)
323 struct GNUNET_ARM_ResultMessage *msg;
326 msize = sizeof (struct GNUNET_ARM_ResultMessage);
327 msg = GNUNET_malloc (msize);
328 msg->arm_msg.header.size = msize;
329 msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_RESULT;
330 msg->result = result;
331 msg->arm_msg.request_id = request_id;
333 GNUNET_SERVER_notify_transmit_ready (client, msize,
334 GNUNET_TIME_UNIT_FOREVER_REL, write_result, msg);
339 * Tell all clients about status change of a service.
341 * @param name name of the service
342 * @param status message type to send
343 * @param unicast if not NULL, send to this client only.
344 * otherwise, send to all clients in the notifier
347 broadcast_status (const char *name, enum GNUNET_ARM_ServiceStatus status,
348 struct GNUNET_SERVER_Client *unicast)
350 struct GNUNET_ARM_StatusMessage *msg;
353 if (NULL == notifier)
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "Sending status %u of service `%s' to client\n",
357 (unsigned int) status, name);
358 namelen = strlen (name);
359 msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_StatusMessage) + namelen + 1);
360 msg->header.size = htons (sizeof (struct GNUNET_ARM_StatusMessage) + namelen + 1);
361 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_STATUS);
362 msg->status = htonl ((uint32_t) (status));
363 memcpy ((char *) &msg[1], name, namelen + 1);
366 GNUNET_SERVER_notification_context_broadcast (notifier,
367 (struct GNUNET_MessageHeader *) msg, GNUNET_YES);
369 GNUNET_SERVER_notification_context_unicast (notifier, unicast,
370 (const struct GNUNET_MessageHeader *) msg, GNUNET_NO);
376 * Actually start the process for the given service.
378 * @param sl identifies service to start
379 * @param client that asked to start the service (may be NULL)
380 * @param request_id id of the request in response to which the process is
381 * being started. 0 if starting was not requested.
384 start_process (struct ServiceList *sl, struct GNUNET_SERVER_Client *client, uint64_t request_id)
394 struct ServiceListeningInfo *sli;
399 /* calculate listen socket list */
402 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
404 GNUNET_array_append (lsocks, ls,
405 GNUNET_NETWORK_get_fd (sli->listen_socket));
406 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
408 GNUNET_SCHEDULER_cancel (sli->accept_task);
409 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
413 GNUNET_array_append (lsocks, ls, INVALID_SOCKET);
415 GNUNET_array_append (lsocks, ls, -1);
418 /* obtain configuration */
420 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "PREFIX",
422 loprefix = GNUNET_strdup (prefix_command);
424 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "OPTIONS",
427 options = GNUNET_strdup (final_option);
428 if (NULL == strstr (options, "%"))
430 /* replace '{}' with service name */
431 while (NULL != (optpos = strstr (options, "{}")))
435 GNUNET_asprintf (&optpos, options, sl->name);
436 GNUNET_free (options);
439 /* replace '$PATH' with value associated with "PATH" */
440 while (NULL != (optpos = strstr (options, "$")))
443 while (isupper ((unsigned char) *optend))
452 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
454 val = GNUNET_strdup ("");
456 GNUNET_asprintf (&optpos, "%s%s%c%s", options, val, b, next);
457 GNUNET_free (options);
463 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
465 /* actually start process */
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
467 "Starting service `%s' using binary `%s' and configuration `%s'\n",
468 sl->name, sl->binary, sl->config);
469 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
470 GNUNET_assert (NULL == sl->proc);
471 if (GNUNET_YES == use_debug)
473 if (NULL == sl->config)
475 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
476 lsocks, loprefix, binary, "-L",
477 "DEBUG", options, NULL);
480 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
481 lsocks, loprefix, binary, "-c", sl->config, "-L",
482 "DEBUG", options, NULL);
486 if (NULL == sl->config)
488 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
489 lsocks, loprefix, binary,
493 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
494 lsocks, loprefix, binary, "-c", sl->config,
497 GNUNET_free (binary);
498 if (sl->proc == NULL)
500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to start service `%s'\n"),
503 signal_result (client, sl->name, request_id, GNUNET_ARM_RESULT_START_FAILED);
507 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting service `%s'\n"),
509 broadcast_status (sl->name, GNUNET_ARM_SERVICE_STARTING, NULL);
511 signal_result (client, sl->name, request_id, GNUNET_ARM_RESULT_STARTING);
514 GNUNET_free (loprefix);
515 GNUNET_free (options);
516 GNUNET_array_grow (lsocks, ls, 0);
521 * Find the process with the given service
522 * name in the given list and return it.
524 * @param name which service entry to look up
525 * @return NULL if it was not found
527 static struct ServiceList *
528 find_service (const char *name)
530 struct ServiceList *sl;
535 if (0 == strcasecmp (sl->name, name))
544 * First connection has come to the listening socket associated with the service,
545 * create the service in order to relay the incoming connection to it
547 * @param cls callback data, struct ServiceListeningInfo describing a listen socket
551 accept_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
553 struct ServiceListeningInfo *sli = cls;
554 struct ServiceList *sl = sli->sl;
556 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
557 GNUNET_assert (GNUNET_NO == in_shutdown);
558 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
560 start_process (sl, NULL, 0);
565 * Creating a listening socket for each of the service's addresses and
566 * wait for the first incoming connection to it
568 * @param sa address associated with the service
569 * @param addr_len length of sa
570 * @param sl service entry for the service in question
573 create_listen_socket (struct sockaddr *sa, socklen_t addr_len,
574 struct ServiceList *sl)
577 struct GNUNET_NETWORK_Handle *sock;
578 struct ServiceListeningInfo *sli;
580 switch (sa->sa_family)
583 sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
586 sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
589 if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */
591 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
596 errno = EAFNOSUPPORT;
601 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
602 _("Unable to create socket for service `%s': %s\n"),
603 sl->name, STRERROR (errno));
607 if (GNUNET_NETWORK_socket_setsockopt
608 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
609 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
612 if ((sa->sa_family == AF_INET6) &&
613 (GNUNET_NETWORK_socket_setsockopt
614 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
615 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
619 if (GNUNET_NETWORK_socket_bind
620 (sock, (const struct sockaddr *) sa, addr_len) != GNUNET_OK)
622 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
624 ("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
625 sl->name, GNUNET_a2s (sa, addr_len), STRERROR (errno));
626 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
630 if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
632 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
633 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
637 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
638 _("ARM now monitors connections to service `%s' at `%s'\n"),
639 sl->name, GNUNET_a2s (sa, addr_len));
640 sli = GNUNET_malloc (sizeof (struct ServiceListeningInfo));
641 sli->service_addr = sa;
642 sli->service_addr_len = addr_len;
643 sli->listen_socket = sock;
646 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock,
647 &accept_connection, sli);
648 GNUNET_CONTAINER_DLL_insert (sl->listen_head, sl->listen_tail, sli);
653 * Remove and free an entry in the service list. Listen sockets
654 * must have already been cleaned up. Only to be called during shutdown.
656 * @param sl entry to free
659 free_service (struct ServiceList *sl)
661 GNUNET_assert (GNUNET_YES == in_shutdown);
662 GNUNET_CONTAINER_DLL_remove (running_head, running_tail, sl);
663 GNUNET_assert (NULL == sl->listen_head);
664 GNUNET_free_non_null (sl->config);
665 GNUNET_free_non_null (sl->binary);
666 GNUNET_free (sl->name);
672 * Handle START-message.
674 * @param cls closure (always NULL)
675 * @param client identification of the client
676 * @param message the actual message
677 * @return GNUNET_OK to keep the connection open,
678 * GNUNET_SYSERR to close it (signal serious error)
681 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
682 const struct GNUNET_MessageHeader *message)
684 const char *servicename;
685 struct ServiceList *sl;
688 struct GNUNET_ARM_Message *amsg;
690 amsg = (struct GNUNET_ARM_Message *) message;
691 request_id = GNUNET_ntohll (amsg->request_id);
692 size = ntohs (amsg->header.size);
693 size -= sizeof (struct GNUNET_ARM_Message);
694 servicename = (const char *) &amsg[1];
695 if ((size == 0) || (servicename[size - 1] != '\0'))
698 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
701 if (GNUNET_YES == in_shutdown)
703 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IN_SHUTDOWN);
704 GNUNET_SERVER_receive_done (client, GNUNET_OK);
707 sl = find_service (servicename);
710 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_NOT_KNOWN);
711 GNUNET_SERVER_receive_done (client, GNUNET_OK);
714 sl->is_default = GNUNET_YES;
715 if (sl->proc != NULL)
717 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
718 GNUNET_SERVER_receive_done (client, GNUNET_OK);
721 start_process (sl, client, request_id);
722 GNUNET_SERVER_receive_done (client, GNUNET_OK);
727 * Start a shutdown sequence.
729 * @param cls closure (refers to service)
730 * @param tc task context
733 trigger_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Triggering shutdown\n");
736 GNUNET_SCHEDULER_shutdown ();
741 * Handle STOP-message.
743 * @param cls closure (always NULL)
744 * @param client identification of the client
745 * @param message the actual message
746 * @return GNUNET_OK to keep the connection open,
747 * GNUNET_SYSERR to close it (signal serious error)
750 handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
751 const struct GNUNET_MessageHeader *message)
753 struct ServiceList *sl;
754 const char *servicename;
757 struct GNUNET_ARM_Message *amsg;
759 amsg = (struct GNUNET_ARM_Message *) message;
760 request_id = GNUNET_ntohll (amsg->request_id);
761 size = ntohs (amsg->header.size);
762 size -= sizeof (struct GNUNET_ARM_Message);
763 servicename = (const char *) &amsg[1];
764 if ((size == 0) || (servicename[size - 1] != '\0'))
767 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
770 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
771 _("Preparing to stop `%s'\n"), servicename);
772 if (0 == strcasecmp (servicename, "arm"))
774 broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
775 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_STOPPING);
776 GNUNET_SERVER_client_persist_ (client);
777 GNUNET_SCHEDULER_add_now (trigger_shutdown, NULL);
778 GNUNET_SERVER_receive_done (client, GNUNET_OK);
781 sl = find_service (servicename);
784 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_NOT_KNOWN);
785 GNUNET_SERVER_receive_done (client, GNUNET_OK);
788 sl->is_default = GNUNET_NO;
789 if (GNUNET_YES == in_shutdown)
791 /* shutdown in progress */
792 signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IN_SHUTDOWN);
793 GNUNET_SERVER_receive_done (client, GNUNET_OK);
796 if (sl->killing_client != NULL)
798 /* killing already in progress */
799 signal_result (client, servicename, request_id,
800 GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
801 GNUNET_SERVER_receive_done (client, GNUNET_OK);
804 if (sl->proc == NULL)
806 /* process is down */
807 signal_result (client, servicename, request_id,
808 GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
809 GNUNET_SERVER_receive_done (client, GNUNET_OK);
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
813 "Sending kill signal to service `%s', waiting for process to die.\n",
815 broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
816 /* no signal_start - only when it's STOPPED */
817 sl->killed_at = GNUNET_TIME_absolute_get ();
818 if (0 != GNUNET_OS_process_kill (sl->proc, SIGTERM))
819 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
820 sl->killing_client = client;
821 sl->killing_client_request_id = request_id;
822 GNUNET_SERVER_client_keep (client);
823 GNUNET_SERVER_receive_done (client, GNUNET_OK);
827 * Handle LIST-message.
829 * @param cls closure (always NULL)
830 * @param client identification of the client
831 * @param message the actual message
834 handle_list (void *cls, struct GNUNET_SERVER_Client *client,
835 const struct GNUNET_MessageHeader *message)
837 struct GNUNET_ARM_ListResultMessage *msg;
838 struct GNUNET_ARM_Message *request;
839 size_t string_list_size;
841 struct ServiceList *sl;
847 request = (struct GNUNET_ARM_Message *) message;
849 string_list_size = 0;
850 /* first count the running processes get their name's size */
851 for (sl = running_head; sl != NULL; sl = sl->next)
853 if (sl->proc != NULL)
855 string_list_size += strlen (sl->name);
856 string_list_size += strlen (sl->binary);
857 string_list_size += 4;
862 total_size = sizeof (struct GNUNET_ARM_ListResultMessage)
864 msg = GNUNET_malloc (total_size);
865 msg->arm_msg.header.size = total_size;
866 msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT;
867 msg->arm_msg.request_id = GNUNET_ntohll (request->request_id);
870 char *pos = (char *)&msg[1];
871 for (sl = running_head; sl != NULL; sl = sl->next)
873 if (sl->proc != NULL)
875 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
876 GNUNET_snprintf(pos, s, "%s (%s)", sl->name, sl->binary);
881 GNUNET_SERVER_notify_transmit_ready (client,
883 GNUNET_TIME_UNIT_FOREVER_REL,
884 write_list_result, msg);
885 GNUNET_SERVER_receive_done (client, GNUNET_OK);
889 * We are done with everything. Stop remaining
890 * tasks, signal handler and the server.
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last shutdown phase\n");
896 if (NULL != notifier)
898 GNUNET_SERVER_notification_context_destroy (notifier);
903 GNUNET_SERVER_destroy (server);
906 if (GNUNET_SCHEDULER_NO_TASK != child_death_task)
908 GNUNET_SCHEDULER_cancel (child_death_task);
909 child_death_task = GNUNET_SCHEDULER_NO_TASK;
914 list_count (struct ServiceList *running_head)
916 struct ServiceList *i;
917 unsigned int res = 0;
918 for (res = 0, i = running_head; i; i = i->next, res++)
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s\n", i->name);
924 * Task run for shutdown.
926 * @param cls closure, NULL if we need to self-restart
930 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
932 struct ServiceList *pos;
933 struct ServiceList *nxt;
934 struct ServiceListeningInfo *sli;
936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "First shutdown phase\n");
937 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
939 GNUNET_SCHEDULER_cancel (child_restart_task);
940 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
942 in_shutdown = GNUNET_YES;
943 /* first, stop listening */
944 for (pos = running_head; NULL != pos; pos = pos->next)
946 while (NULL != (sli = pos->listen_head))
948 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
949 pos->listen_tail, sli);
950 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
952 GNUNET_SCHEDULER_cancel (sli->accept_task);
953 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
955 GNUNET_break (GNUNET_OK ==
956 GNUNET_NETWORK_socket_close (sli->listen_socket));
957 GNUNET_free (sli->service_addr);
961 /* then, shutdown all existing service processes */
963 while (NULL != (pos = nxt))
966 if (pos->proc != NULL)
968 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping service `%s'\n",
970 pos->killed_at = GNUNET_TIME_absolute_get ();
971 if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
972 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
979 /* finally, should all service processes be already gone, terminate for real */
980 if (running_head == NULL)
983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
984 "Delaying shutdown, have %u childs still running\n", list_count (running_head));
989 * Task run whenever it is time to restart a child that died.
991 * @param cls closure, always NULL
995 delayed_restart_task (void *cls,
996 const struct GNUNET_SCHEDULER_TaskContext *tc)
998 struct ServiceList *sl;
999 struct GNUNET_TIME_Relative lowestRestartDelay;
1000 struct ServiceListeningInfo *sli;
1002 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
1003 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1005 GNUNET_assert (GNUNET_NO == in_shutdown);
1006 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
1008 /* check for services that need to be restarted due to
1009 * configuration changes or because the last restart failed */
1010 for (sl = running_head; NULL != sl; sl = sl->next)
1012 if (NULL != sl->proc)
1014 /* service is currently not running */
1015 if (GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value ==
1018 /* restart is now allowed */
1021 /* process should run by default, start immediately */
1022 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1023 _("Restarting service `%s'.\n"), sl->name);
1024 start_process (sl, NULL, 0);
1028 /* process is run on-demand, ensure it is re-started if there is demand */
1029 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
1030 if (GNUNET_SCHEDULER_NO_TASK == sli->accept_task)
1032 /* accept was actually paused, so start it again */
1034 GNUNET_SCHEDULER_add_read_net
1035 (GNUNET_TIME_UNIT_FOREVER_REL, sli->listen_socket,
1036 &accept_connection, sli);
1042 /* update calculation for earliest time to reactivate a service */
1043 lowestRestartDelay =
1044 GNUNET_TIME_relative_min (lowestRestartDelay,
1045 GNUNET_TIME_absolute_get_remaining
1049 if (lowestRestartDelay.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 "Will restart process in %s\n",
1053 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay, GNUNET_YES));
1054 child_restart_task =
1055 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
1056 GNUNET_SCHEDULER_PRIORITY_IDLE,
1057 &delayed_restart_task, NULL);
1063 * Task triggered whenever we receive a SIGCHLD (child
1066 * @param cls closure, NULL if we need to self-restart
1070 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1072 struct ServiceList *pos;
1073 struct ServiceList *next;
1074 struct ServiceListeningInfo *sli;
1075 const char *statstr;
1079 enum GNUNET_OS_ProcessStatusType statusType;
1080 unsigned long statusCode;
1081 const struct GNUNET_DISK_FileHandle *pr;
1083 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
1084 child_death_task = GNUNET_SCHEDULER_NO_TASK;
1085 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1087 /* shutdown scheduled us, ignore! */
1089 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1090 pr, &maint_child_death, NULL);
1093 /* consume the signal */
1094 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
1096 /* check for services that died (WAITPID) */
1097 next = running_head;
1098 while (NULL != (pos = next))
1102 if (pos->proc == NULL)
1104 if (GNUNET_YES == in_shutdown)
1108 if ((GNUNET_SYSERR ==
1110 GNUNET_OS_process_status (pos->proc, &statusType, &statusCode)))
1111 || ((ret == GNUNET_NO) || (statusType == GNUNET_OS_PROCESS_STOPPED)
1112 || (statusType == GNUNET_OS_PROCESS_RUNNING)))
1114 if (statusType == GNUNET_OS_PROCESS_EXITED)
1116 statstr = _( /* process termination method */ "exit");
1117 statcode = statusCode;
1119 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
1121 statstr = _( /* process termination method */ "signal");
1122 statcode = statusCode;
1126 statstr = _( /* process termination method */ "unknown");
1129 if (0 != pos->killed_at.abs_value)
1131 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1132 _("Service `%s' took %s to terminate\n"),
1134 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at), GNUNET_YES));
1136 GNUNET_OS_process_destroy (pos->proc);
1138 broadcast_status (pos->name, GNUNET_ARM_SERVICE_STOPPED, NULL);
1139 if (NULL != pos->killing_client)
1141 signal_result (pos->killing_client, pos->name,
1142 pos->killing_client_request_id, GNUNET_ARM_RESULT_STOPPED);
1143 GNUNET_SERVER_client_drop (pos->killing_client);
1144 pos->killing_client = NULL;
1145 pos->killing_client_request_id = 0;
1147 if (GNUNET_YES != in_shutdown)
1149 if ((statusType == GNUNET_OS_PROCESS_EXITED) && (statcode == 0))
1151 /* process terminated normally, allow restart at any time */
1152 pos->restart_at.abs_value = 0;
1153 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1154 _("Service `%s' terminated normally, will restart at any time\n"),
1156 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1157 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1159 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sli->accept_task);
1161 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1162 sli->listen_socket, &accept_connection, sli);
1167 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1168 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1169 _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
1170 pos->name, statstr, statcode,
1171 GNUNET_STRINGS_relative_time_to_string (pos->backoff, GNUNET_YES));
1172 /* schedule restart */
1173 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1174 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1175 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
1176 GNUNET_SCHEDULER_cancel (child_restart_task);
1177 child_restart_task = GNUNET_SCHEDULER_add_with_priority (
1178 GNUNET_SCHEDULER_PRIORITY_IDLE, &delayed_restart_task, NULL);
1186 child_death_task = GNUNET_SCHEDULER_add_read_file (
1187 GNUNET_TIME_UNIT_FOREVER_REL, pr, &maint_child_death, NULL);
1188 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1190 else if (GNUNET_YES == in_shutdown)
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1192 "Delaying shutdown after child's death, still have %u children\n",
1193 list_count (running_head));
1199 * Signal handler called for SIGCHLD. Triggers the
1200 * respective handler by writing to the trigger pipe.
1203 sighandler_child_death ()
1206 int old_errno = errno; /* back-up errno */
1209 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
1210 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
1212 errno = old_errno; /* restore errno */
1217 * Setup our service record for the given section in the configuration file
1218 * (assuming the section is for a service).
1221 * @param section a section in the configuration file
1222 * @return GNUNET_OK (continue)
1225 setup_service (void *cls, const char *section)
1227 struct ServiceList *sl;
1231 struct sockaddr **addrs;
1232 socklen_t *addr_lens;
1236 if (strcasecmp (section, "arm") == 0)
1239 GNUNET_CONFIGURATION_get_value_string (cfg, section, "BINARY", &binary))
1241 /* not a service section */
1244 sl = find_service (section);
1247 /* got the same section twice!? */
1253 GNUNET_CONFIGURATION_get_value_filename (cfg, section, "CONFIG",
1256 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", "DEFAULTCONFIG",
1258 (0 != STAT (config, &sbuf)))
1262 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1265 GNUNET_free (config);
1269 sl = GNUNET_malloc (sizeof (struct ServiceList));
1270 sl->name = GNUNET_strdup (section);
1271 sl->binary = binary;
1272 sl->config = config;
1273 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1274 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1276 sl->pipe_control = GNUNET_YES;
1278 if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
1279 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
1281 GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
1283 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "AUTOSTART"))
1285 if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section, cfg,
1286 &addrs, &addr_lens)))
1288 /* this will free (or capture) addrs[i] */
1289 for (i = 0; i < ret; i++)
1290 create_listen_socket (addrs[i], addr_lens[i], sl);
1291 GNUNET_free (addrs);
1292 GNUNET_free (addr_lens);
1297 * A client connected, add it to the notification context.
1299 * @param cls closure
1300 * @param client identification of the client
1303 handle_client_connecting (void *cls, struct GNUNET_SERVER_Client *client)
1305 /* All clients are considered to be of the "monitor" kind
1306 * (that is, they don't affect ARM shutdown).
1309 GNUNET_SERVER_client_mark_monitor (client);
1313 * Handle MONITOR-message.
1315 * @param cls closure (always NULL)
1316 * @param client identification of the client
1317 * @param message the actual message
1318 * @return GNUNET_OK to keep the connection open,
1319 * GNUNET_SYSERR to close it (signal serious error)
1322 handle_monitor (void *cls, struct GNUNET_SERVER_Client *client,
1323 const struct GNUNET_MessageHeader *message)
1325 /* Removal is handled by the server implementation, internally. */
1326 if ((NULL != client) && (NULL != notifier))
1328 GNUNET_SERVER_notification_context_add (notifier, client);
1329 broadcast_status ("arm", GNUNET_ARM_SERVICE_MONITORING_STARTED, client);
1330 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1335 * Process arm requests.
1337 * @param cls closure
1338 * @param serv the initialized server
1339 * @param c configuration to use
1342 run (void *cls, struct GNUNET_SERVER_Handle *serv,
1343 const struct GNUNET_CONFIGURATION_Handle *c)
1345 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1346 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
1347 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
1348 {&handle_monitor, NULL, GNUNET_MESSAGE_TYPE_ARM_MONITOR,
1349 sizeof (struct GNUNET_MessageHeader)},
1350 {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST,
1351 sizeof (struct GNUNET_ARM_Message)},
1354 char *defaultservices;
1356 struct ServiceList *sl;
1360 GNUNET_assert (serv != NULL);
1361 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1364 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1365 GNUNET_DISK_pipe_handle (sigpipe,
1366 GNUNET_DISK_PIPE_END_READ),
1367 &maint_child_death, NULL);
1370 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_PREFIX",
1372 prefix_command = GNUNET_strdup ("");
1374 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
1376 final_option = GNUNET_strdup ("");
1378 GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
1380 /* start default services... */
1382 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "DEFAULTSERVICES",
1385 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1386 _("Starting default services `%s'\n"), defaultservices);
1387 if (0 < strlen (defaultservices))
1389 for (pos = strtok (defaultservices, " "); NULL != pos;
1390 pos = strtok (NULL, " "))
1392 sl = find_service (pos);
1395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1397 ("Default service `%s' not configured correctly!\n"),
1401 sl->is_default = GNUNET_YES;
1402 start_process (sl, NULL, 0);
1405 GNUNET_free (defaultservices);
1409 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1411 ("No default services configured, GNUnet will not really start right now.\n"));
1415 GNUNET_SERVER_notification_context_create (server, MAX_NOTIFY_QUEUE);
1416 GNUNET_SERVER_connect_notify (server, handle_client_connecting, NULL);
1417 /* process client requests */
1418 GNUNET_SERVER_add_handlers (server, handlers);
1423 * The main function for the arm service.
1425 * @param argc number of arguments from the command line
1426 * @param argv command line arguments
1427 * @return 0 ok, 1 on error
1430 main (int argc, char *const *argv)
1433 struct GNUNET_SIGNAL_Context *shc_chld;
1435 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
1436 GNUNET_assert (sigpipe != NULL);
1438 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
1441 GNUNET_SERVICE_run (argc, argv, "arm",
1442 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, &run, NULL)) ? 0 : 1;
1443 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1445 GNUNET_DISK_pipe_close (sigpipe);
1455 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1457 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1459 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1460 mallopt (M_TOP_PAD, 1 * 1024);
1466 /* end of gnunet-service-arm.c */