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 * List of our services.
39 * Record with information about a listen socket we have open.
41 struct ServiceListeningInfo
44 * This is a linked list.
46 struct ServiceListeningInfo *next;
49 * This is a linked list.
51 struct ServiceListeningInfo *prev;
54 * Address this socket is listening on.
56 struct sockaddr *service_addr;
59 * Service this listen socket is for.
61 struct ServiceList *sl;
64 * Number of bytes in 'service_addr'
66 socklen_t service_addr_len;
69 * Our listening socket.
71 struct GNUNET_NETWORK_Handle *listen_socket;
74 * Task doing the accepting.
76 GNUNET_SCHEDULER_TaskIdentifier accept_task;
82 * List of our services.
87 * This is a doubly-linked list.
89 struct ServiceList *next;
92 * This is a doubly-linked list.
94 struct ServiceList *prev;
97 * Linked list of listen sockets associated with this service.
99 struct ServiceListeningInfo *listen_head;
102 * Linked list of listen sockets associated with this service.
104 struct ServiceListeningInfo *listen_tail;
107 * Name of the service.
112 * Name of the binary used.
117 * Name of the configuration file used.
122 * Client to notify upon kill completion (waitpid), NULL
123 * if we should simply restart the process.
125 struct GNUNET_SERVER_Client *killing_client;
128 * Process structure pointer of the child.
130 struct GNUNET_OS_Process *proc;
133 * Process exponential backoff time
135 struct GNUNET_TIME_Relative backoff;
138 * Absolute time at which the process is scheduled to restart in case of death
140 struct GNUNET_TIME_Absolute restart_at;
143 * Time we asked the service to shut down (used to calculate time it took
144 * the service to terminate).
146 struct GNUNET_TIME_Absolute killed_at;
149 * Is this service to be started by default (or did a client tell us explicitly
150 * to start it)? GNUNET_NO if the service is started only upon 'accept' on a
151 * listen socket or possibly explicitly by a client changing the value.
156 * Should we use pipes to signal this process? (YES for Java binaries and if we
163 * List of running services.
165 static struct ServiceList *running_head;
168 * List of running services.
170 static struct ServiceList *running_tail;
175 static const struct GNUNET_CONFIGURATION_Handle *cfg;
178 * Command to prepend to each actual command.
180 static char *prefix_command;
183 * Option to append to each actual command.
185 static char *final_option;
188 * ID of task called whenever we get a SIGCHILD.
190 static GNUNET_SCHEDULER_TaskIdentifier child_death_task;
193 * ID of task called whenever the timeout for restarting a child
196 static GNUNET_SCHEDULER_TaskIdentifier child_restart_task;
199 * Pipe used to communicate shutdown via signal.
201 static struct GNUNET_DISK_PipeHandle *sigpipe;
204 * Are we in shutdown mode?
206 static int in_shutdown;
209 * Handle to our server instance. Our server is a bit special in that
210 * its service is not immediately stopped once we get a shutdown
211 * request (since we need to continue service until all of our child
212 * processes are dead). This handle is used to shut down the server
213 * (and thus trigger process termination) once all child processes are
214 * also dead. A special option in the ARM configuration modifies the
215 * behaviour of the service implementation to not do the shutdown
218 static struct GNUNET_SERVER_Handle *server;
221 #include "do_start_process.c"
225 * Actually start the process for the given service.
227 * @param sl identifies service to start
230 start_process (struct ServiceList *sl)
240 struct ServiceListeningInfo *sli;
245 /* calculate listen socket list */
248 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
250 GNUNET_array_append (lsocks, ls,
251 GNUNET_NETWORK_get_fd (sli->listen_socket));
252 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
254 GNUNET_SCHEDULER_cancel (sli->accept_task);
255 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
259 GNUNET_array_append (lsocks, ls, INVALID_SOCKET);
261 GNUNET_array_append (lsocks, ls, -1);
264 /* obtain configuration */
266 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "PREFIX",
268 loprefix = GNUNET_strdup (prefix_command);
270 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "OPTIONS",
273 options = GNUNET_strdup (final_option);
274 if (NULL == strstr (options, "%"))
276 /* replace '{}' with service name */
277 while (NULL != (optpos = strstr (options, "{}")))
281 GNUNET_asprintf (&optpos, options, sl->name);
282 GNUNET_free (options);
285 /* replace '$PATH' with value associated with "PATH" */
286 while (NULL != (optpos = strstr (options, "$")))
289 while (isupper ((unsigned char) *optend))
298 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
300 val = GNUNET_strdup ("");
302 GNUNET_asprintf (&optpos, "%s%s%c%s", options, val, b, next);
303 GNUNET_free (options);
309 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
311 /* actually start process */
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 "Starting service `%s' using binary `%s' and configuration `%s'\n",
314 sl->name, sl->binary, sl->config);
315 binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
316 GNUNET_assert (NULL == sl->proc);
317 if (GNUNET_YES == use_debug)
319 if (NULL == sl->config)
321 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
322 lsocks, loprefix, binary, "-L",
323 "DEBUG", options, NULL);
326 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
327 lsocks, loprefix, binary, "-c", sl->config, "-L",
328 "DEBUG", options, NULL);
332 if (NULL == sl->config)
334 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
335 lsocks, loprefix, binary,
339 do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
340 lsocks, loprefix, binary, "-c", sl->config,
343 GNUNET_free (binary);
344 if (sl->proc == NULL)
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to start service `%s'\n"),
348 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting service `%s'\n"),
351 GNUNET_free (loprefix);
352 GNUNET_free (options);
353 GNUNET_array_grow (lsocks, ls, 0);
358 * Transmit a status result message.
360 * @param cls pointer to "unit16_t*" with message type
361 * @param size number of bytes available in buf
362 * @param buf where to copy the message, NULL on error
363 * @return number of bytes copied to buf
366 write_result (void *cls, size_t size, void *buf)
368 enum GNUNET_ARM_ProcessStatus *res = cls;
369 struct GNUNET_ARM_ResultMessage *msg;
373 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
374 _("Could not send status result to client\n"));
375 return 0; /* error, not much we can do */
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "Sending status response %u to client\n", (unsigned int) *res);
379 GNUNET_assert (size >= sizeof (struct GNUNET_ARM_ResultMessage));
381 msg->header.size = htons (sizeof (struct GNUNET_ARM_ResultMessage));
382 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_RESULT);
383 msg->status = htonl ((uint32_t) (*res));
385 return sizeof (struct GNUNET_ARM_ResultMessage);
389 * Transmit the list of running services.
391 * @param cls pointer to struct GNUNET_ARM_ListResultMessage with the message
392 * @param size number of bytes available in buf
393 * @param buf where to copy the message, NULL on error
394 * @return number of bytes copied to buf
397 write_list_result (void *cls, size_t size, void *buf)
399 struct GNUNET_ARM_ListResultMessage *msg = cls;
400 struct GNUNET_ARM_ListResultMessage *rslt;
405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
406 _("Could not send list result to client\n"));
407 return 0; /* error, not much we can do */
410 GNUNET_assert (size >= msg->header.size);
412 rslt->header.size = htons (msg->header.size);
413 rslt->header.type = htons (msg->header.type);
414 rslt->count = htons (msg->count);
416 size_t list_size = msg->header.size
417 - sizeof (struct GNUNET_ARM_ListResultMessage);
418 memcpy (&rslt[1], &msg[1], list_size);
420 rslt_size = msg->header.size;
427 * Signal our client that we will start or stop the
430 * @param client who is being signalled
431 * @param name name of the service
432 * @param result message type to send
433 * @return NULL if it was not found
436 signal_result (struct GNUNET_SERVER_Client *client, const char *name,
437 enum GNUNET_ARM_ProcessStatus result)
439 enum GNUNET_ARM_ProcessStatus *res;
443 /* FIXME: this is not super-clean yet... */
444 res = GNUNET_malloc (sizeof (enum GNUNET_ARM_ProcessStatus));
446 GNUNET_SERVER_notify_transmit_ready (client,
448 GNUNET_ARM_ResultMessage),
449 GNUNET_TIME_UNIT_FOREVER_REL,
451 GNUNET_SERVER_receive_done (client, GNUNET_OK);
456 * Find the process with the given service
457 * name in the given list and return it.
459 * @param name which service entry to look up
460 * @return NULL if it was not found
462 static struct ServiceList *
463 find_service (const char *name)
465 struct ServiceList *sl;
470 if (0 == strcasecmp (sl->name, name))
479 * First connection has come to the listening socket associated with the service,
480 * create the service in order to relay the incoming connection to it
482 * @param cls callback data, struct ServiceListeningInfo describing a listen socket
486 accept_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
488 struct ServiceListeningInfo *sli = cls;
489 struct ServiceList *sl = sli->sl;
491 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
492 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
499 * Creating a listening socket for each of the service's addresses and
500 * wait for the first incoming connection to it
502 * @param sa address associated with the service
503 * @param addr_len length of sa
504 * @param sl service entry for the service in question
507 create_listen_socket (struct sockaddr *sa, socklen_t addr_len,
508 struct ServiceList *sl)
511 struct GNUNET_NETWORK_Handle *sock;
512 struct ServiceListeningInfo *sli;
514 switch (sa->sa_family)
517 sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
520 sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
523 if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */
525 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
530 errno = EAFNOSUPPORT;
535 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
536 _("Unable to create socket for service `%s': %s\n"),
537 sl->name, STRERROR (errno));
541 if (GNUNET_NETWORK_socket_setsockopt
542 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
543 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
546 if ((sa->sa_family == AF_INET6) &&
547 (GNUNET_NETWORK_socket_setsockopt
548 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
549 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
553 if (GNUNET_NETWORK_socket_bind
554 (sock, (const struct sockaddr *) sa, addr_len) != GNUNET_OK)
556 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
558 ("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
559 sl->name, GNUNET_a2s (sa, addr_len), STRERROR (errno));
560 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
564 if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
566 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
567 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
571 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
572 _("ARM now monitors connections to service `%s' at `%s'\n"),
573 sl->name, GNUNET_a2s (sa, addr_len));
574 sli = GNUNET_malloc (sizeof (struct ServiceListeningInfo));
575 sli->service_addr = sa;
576 sli->service_addr_len = addr_len;
577 sli->listen_socket = sock;
580 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock,
581 &accept_connection, sli);
582 GNUNET_CONTAINER_DLL_insert (sl->listen_head, sl->listen_tail, sli);
587 * Remove and free an entry in the service list. Listen sockets
588 * must have already been cleaned up. Only to be called during shutdown.
590 * @param sl entry to free
593 free_service (struct ServiceList *sl)
595 GNUNET_assert (GNUNET_YES == in_shutdown);
596 GNUNET_CONTAINER_DLL_remove (running_head, running_tail, sl);
597 GNUNET_assert (NULL == sl->listen_head);
598 GNUNET_free_non_null (sl->config);
599 GNUNET_free_non_null (sl->binary);
600 GNUNET_free (sl->name);
606 * Handle START-message.
608 * @param cls closure (always NULL)
609 * @param client identification of the client
610 * @param message the actual message
611 * @return GNUNET_OK to keep the connection open,
612 * GNUNET_SYSERR to close it (signal serious error)
615 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
616 const struct GNUNET_MessageHeader *message)
618 const char *servicename;
619 struct ServiceList *sl;
622 size = ntohs (message->size);
623 size -= sizeof (struct GNUNET_MessageHeader);
624 servicename = (const char *) &message[1];
625 if ((size == 0) || (servicename[size - 1] != '\0'))
628 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
631 if (GNUNET_YES == in_shutdown)
633 signal_result (client, servicename, GNUNET_ARM_PROCESS_SHUTDOWN);
636 sl = find_service (servicename);
639 signal_result (client, servicename, GNUNET_ARM_PROCESS_UNKNOWN);
642 sl->is_default = GNUNET_YES;
643 if (sl->proc != NULL)
645 signal_result (client, servicename, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
649 signal_result (client, servicename, GNUNET_ARM_PROCESS_STARTING);
654 * Handle STOP-message.
656 * @param cls closure (always NULL)
657 * @param client identification of the client
658 * @param message the actual message
659 * @return GNUNET_OK to keep the connection open,
660 * GNUNET_SYSERR to close it (signal serious error)
663 handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
664 const struct GNUNET_MessageHeader *message)
666 struct ServiceList *sl;
667 const char *servicename;
670 size = ntohs (message->size);
671 size -= sizeof (struct GNUNET_MessageHeader);
672 servicename = (const char *) &message[1];
673 if ((size == 0) || (servicename[size - 1] != '\0'))
676 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
679 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
680 _("Preparing to stop `%s'\n"), servicename);
681 sl = find_service (servicename);
684 signal_result (client, servicename, GNUNET_ARM_PROCESS_UNKNOWN);
687 sl->is_default = GNUNET_NO;
688 if (GNUNET_YES == in_shutdown)
690 /* shutdown in progress */
691 signal_result (client, servicename, GNUNET_ARM_PROCESS_SHUTDOWN);
694 if (sl->killing_client != NULL)
696 /* killing already in progress */
697 signal_result (client, servicename,
698 GNUNET_ARM_PROCESS_ALREADY_STOPPING);
701 if (sl->proc == NULL)
703 /* process is down */
704 signal_result (client, servicename, GNUNET_ARM_PROCESS_ALREADY_DOWN);
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "Sending kill signal to service `%s', waiting for process to die.\n",
710 sl->killed_at = GNUNET_TIME_absolute_get ();
711 if (0 != GNUNET_OS_process_kill (sl->proc, SIGTERM))
712 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
713 sl->killing_client = client;
714 GNUNET_SERVER_client_keep (client);
718 * Handle LIST-message.
720 * @param cls closure (always NULL)
721 * @param client identification of the client
722 * @param message the actual message
725 handle_list (void *cls, struct GNUNET_SERVER_Client *client,
726 const struct GNUNET_MessageHeader *message)
728 struct GNUNET_ARM_ListResultMessage *msg;
729 size_t string_list_size;
731 struct ServiceList *sl;
738 string_list_size = 0;
739 /* first count the running processes get their name's size */
740 for (sl = running_head; sl != NULL; sl = sl->next)
742 if (sl->proc != NULL)
744 string_list_size += strlen (sl->name);
745 string_list_size += strlen (sl->binary);
746 string_list_size += 4;
750 total_size = sizeof (struct GNUNET_ARM_ListResultMessage)
752 msg = GNUNET_malloc (total_size);
753 msg->header.size = total_size;
754 msg->header.type = GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT;
757 char *pos = (char *)&msg[1];
758 for (sl = running_head; sl != NULL; sl = sl->next)
760 if (sl->proc != NULL)
762 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
763 GNUNET_snprintf(pos, s, "%s (%s)", sl->name, sl->binary);
768 GNUNET_SERVER_notify_transmit_ready (client,
770 GNUNET_TIME_UNIT_FOREVER_REL,
771 &write_list_result, msg);
772 GNUNET_SERVER_receive_done (client, GNUNET_OK);
776 * We are done with everything. Stop remaining
777 * tasks, signal handler and the server.
784 GNUNET_SERVER_destroy (server);
787 if (GNUNET_SCHEDULER_NO_TASK != child_death_task)
789 GNUNET_SCHEDULER_cancel (child_death_task);
790 child_death_task = GNUNET_SCHEDULER_NO_TASK;
796 * Task run for shutdown.
798 * @param cls closure, NULL if we need to self-restart
802 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
804 struct ServiceList *pos;
805 struct ServiceList *nxt;
806 struct ServiceListeningInfo *sli;
808 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
810 GNUNET_SCHEDULER_cancel (child_restart_task);
811 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
813 in_shutdown = GNUNET_YES;
814 /* first, stop listening */
815 for (pos = running_head; NULL != pos; pos = pos->next)
817 while (NULL != (sli = pos->listen_head))
819 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
820 pos->listen_tail, sli);
821 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
823 GNUNET_SCHEDULER_cancel (sli->accept_task);
824 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
826 GNUNET_break (GNUNET_OK ==
827 GNUNET_NETWORK_socket_close (sli->listen_socket));
828 GNUNET_free (sli->service_addr);
832 /* then, shutdown all existing service processes */
834 while (NULL != (pos = nxt))
837 if (pos->proc != NULL)
839 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping service `%s'\n",
841 pos->killed_at = GNUNET_TIME_absolute_get ();
842 if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
843 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
850 /* finally, should all service processes be already gone, terminate for real */
851 if (running_head == NULL)
857 * Task run whenever it is time to restart a child that died.
859 * @param cls closure, always NULL
863 delayed_restart_task (void *cls,
864 const struct GNUNET_SCHEDULER_TaskContext *tc)
866 struct ServiceList *sl;
867 struct GNUNET_TIME_Relative lowestRestartDelay;
868 struct ServiceListeningInfo *sli;
870 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
871 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
873 GNUNET_assert (GNUNET_NO == in_shutdown);
874 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
876 /* check for services that need to be restarted due to
877 * configuration changes or because the last restart failed */
878 for (sl = running_head; NULL != sl; sl = sl->next)
880 if (NULL != sl->proc)
882 /* service is currently not running */
883 if (GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value ==
886 /* restart is now allowed */
889 /* process should run by default, start immediately */
890 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
891 _("Restarting service `%s'.\n"), sl->name);
896 /* process is run on-demand, ensure it is re-started if there is demand */
897 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
898 if (GNUNET_SCHEDULER_NO_TASK == sli->accept_task)
900 /* accept was actually paused, so start it again */
902 GNUNET_SCHEDULER_add_read_net
903 (GNUNET_TIME_UNIT_FOREVER_REL, sli->listen_socket,
904 &accept_connection, sli);
910 /* update calculation for earliest time to reactivate a service */
912 GNUNET_TIME_relative_min (lowestRestartDelay,
913 GNUNET_TIME_absolute_get_remaining
917 if (lowestRestartDelay.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
920 "Will restart process in %s\n",
921 GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay, GNUNET_YES));
923 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
924 GNUNET_SCHEDULER_PRIORITY_IDLE,
925 &delayed_restart_task, NULL);
931 * Task triggered whenever we receive a SIGCHLD (child
934 * @param cls closure, NULL if we need to self-restart
938 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
940 struct ServiceList *pos;
941 struct ServiceList *next;
942 struct ServiceListeningInfo *sli;
947 enum GNUNET_OS_ProcessStatusType statusType;
948 unsigned long statusCode;
949 const struct GNUNET_DISK_FileHandle *pr;
951 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
952 child_death_task = GNUNET_SCHEDULER_NO_TASK;
953 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
955 /* shutdown scheduled us, ignore! */
957 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
958 pr, &maint_child_death, NULL);
961 /* consume the signal */
962 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
964 /* check for services that died (WAITPID) */
966 while (NULL != (pos = next))
970 if (pos->proc == NULL)
972 if (GNUNET_YES == in_shutdown)
976 if ((GNUNET_SYSERR ==
978 GNUNET_OS_process_status (pos->proc, &statusType, &statusCode)))
979 || ((ret == GNUNET_NO) || (statusType == GNUNET_OS_PROCESS_STOPPED)
980 || (statusType == GNUNET_OS_PROCESS_RUNNING)))
982 if (statusType == GNUNET_OS_PROCESS_EXITED)
984 statstr = _( /* process termination method */ "exit");
985 statcode = statusCode;
987 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
989 statstr = _( /* process termination method */ "signal");
990 statcode = statusCode;
994 statstr = _( /* process termination method */ "unknown");
997 if (0 != pos->killed_at.abs_value)
999 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1000 _("Service `%s' took %s to terminate\n"),
1002 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at), GNUNET_YES));
1004 GNUNET_OS_process_destroy (pos->proc);
1006 if (NULL != pos->killing_client)
1008 signal_result (pos->killing_client, pos->name,
1009 GNUNET_ARM_PROCESS_DOWN);
1010 GNUNET_SERVER_client_drop (pos->killing_client);
1011 pos->killing_client = NULL;
1012 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
1013 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1015 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sli->accept_task);
1017 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1019 &accept_connection, sli);
1023 if (GNUNET_YES != in_shutdown)
1025 if ((statusType == GNUNET_OS_PROCESS_EXITED) && (statcode == 0))
1027 /* process terminated normally, allow restart at any time */
1028 pos->restart_at.abs_value = 0;
1032 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1033 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1035 ("Service `%s' terminated with status %s/%d, will restart in %llu ms\n"),
1036 pos->name, statstr, statcode, pos->backoff.rel_value);
1037 /* schedule restart */
1038 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1039 pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
1041 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
1042 GNUNET_SCHEDULER_cancel (child_restart_task);
1043 child_restart_task =
1044 GNUNET_SCHEDULER_add_with_priority
1045 (GNUNET_SCHEDULER_PRIORITY_IDLE,
1046 &delayed_restart_task, NULL);
1054 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1055 pr, &maint_child_death, NULL);
1056 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1062 * Handler for SHUTDOWN message.
1064 * @param cls closure (refers to service)
1065 * @param client identification of the client
1066 * @param message the actual message
1069 handle_shutdown (void *cls, struct GNUNET_SERVER_Client *client,
1070 const struct GNUNET_MessageHeader *message)
1072 GNUNET_SCHEDULER_shutdown ();
1073 GNUNET_SERVER_client_persist_ (client);
1078 * Signal handler called for SIGCHLD. Triggers the
1079 * respective handler by writing to the trigger pipe.
1082 sighandler_child_death ()
1085 int old_errno = errno; /* back-up errno */
1088 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
1089 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
1091 errno = old_errno; /* restore errno */
1096 * Setup our service record for the given section in the configuration file
1097 * (assuming the section is for a service).
1100 * @param section a section in the configuration file
1101 * @return GNUNET_OK (continue)
1104 setup_service (void *cls, const char *section)
1106 struct ServiceList *sl;
1110 struct sockaddr **addrs;
1111 socklen_t *addr_lens;
1115 if (strcasecmp (section, "arm") == 0)
1118 GNUNET_CONFIGURATION_get_value_string (cfg, section, "BINARY", &binary))
1120 /* not a service section */
1123 sl = find_service (section);
1126 /* got the same section twice!? */
1132 GNUNET_CONFIGURATION_get_value_filename (cfg, section, "CONFIG",
1135 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", "DEFAULTCONFIG",
1137 (0 != STAT (config, &sbuf)))
1141 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1144 GNUNET_free (config);
1148 sl = GNUNET_malloc (sizeof (struct ServiceList));
1149 sl->name = GNUNET_strdup (section);
1150 sl->binary = binary;
1151 sl->config = config;
1152 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1153 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1155 sl->pipe_control = GNUNET_YES;
1157 if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
1158 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
1160 GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
1162 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "AUTOSTART"))
1164 if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section, cfg,
1165 &addrs, &addr_lens)))
1167 /* this will free (or capture) addrs[i] */
1168 for (i = 0; i < ret; i++)
1169 create_listen_socket (addrs[i], addr_lens[i], sl);
1170 GNUNET_free (addrs);
1171 GNUNET_free (addr_lens);
1176 * Process arm requests.
1178 * @param cls closure
1179 * @param serv the initialized server
1180 * @param c configuration to use
1183 run (void *cls, struct GNUNET_SERVER_Handle *serv,
1184 const struct GNUNET_CONFIGURATION_Handle *c)
1186 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1187 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
1188 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
1189 {&handle_shutdown, NULL, GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN,
1190 sizeof (struct GNUNET_MessageHeader)},
1191 {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST,
1192 sizeof (struct GNUNET_MessageHeader)},
1195 char *defaultservices;
1197 struct ServiceList *sl;
1201 GNUNET_assert (serv != NULL);
1202 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1205 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1206 GNUNET_DISK_pipe_handle (sigpipe,
1207 GNUNET_DISK_PIPE_END_READ),
1208 &maint_child_death, NULL);
1211 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_PREFIX",
1213 prefix_command = GNUNET_strdup ("");
1215 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
1217 final_option = GNUNET_strdup ("");
1219 GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
1221 /* start default services... */
1223 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "DEFAULTSERVICES",
1226 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1227 _("Starting default services `%s'\n"), defaultservices);
1228 if (0 < strlen (defaultservices))
1230 for (pos = strtok (defaultservices, " "); NULL != pos;
1231 pos = strtok (NULL, " "))
1233 sl = find_service (pos);
1236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1238 ("Default service `%s' not configured correctly!\n"),
1242 sl->is_default = GNUNET_YES;
1246 GNUNET_free (defaultservices);
1250 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1252 ("No default services configured, GNUnet will not really start right now.\n"));
1255 /* process client requests */
1256 GNUNET_SERVER_add_handlers (server, handlers);
1261 * The main function for the arm service.
1263 * @param argc number of arguments from the command line
1264 * @param argv command line arguments
1265 * @return 0 ok, 1 on error
1268 main (int argc, char *const *argv)
1271 struct GNUNET_SIGNAL_Context *shc_chld;
1273 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
1274 GNUNET_assert (sigpipe != NULL);
1276 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
1279 GNUNET_SERVICE_run (argc, argv, "arm",
1280 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, &run, NULL)) ? 0 : 1;
1281 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1283 GNUNET_DISK_pipe_close (sigpipe);
1293 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1295 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1297 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1298 mallopt (M_TOP_PAD, 1 * 1024);
1304 /* end of gnunet-service-arm.c */