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 * Threshold after which exponential backoff shouldn't increase (in ms); 30m
35 #define EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
39 * List of our services.
45 * Record with information about a listen socket we have open.
47 struct ServiceListeningInfo
50 * This is a linked list.
52 struct ServiceListeningInfo *next;
55 * This is a linked list.
57 struct ServiceListeningInfo *prev;
60 * Address this socket is listening on.
62 struct sockaddr *service_addr;
65 * Service this listen socket is for.
67 struct ServiceList *sl;
70 * Number of bytes in 'service_addr'
72 socklen_t service_addr_len;
75 * Our listening socket.
77 struct GNUNET_NETWORK_Handle *listen_socket;
80 * Task doing the accepting.
82 GNUNET_SCHEDULER_TaskIdentifier accept_task;
88 * List of our services.
93 * This is a doubly-linked list.
95 struct ServiceList *next;
98 * This is a doubly-linked list.
100 struct ServiceList *prev;
103 * Linked list of listen sockets associated with this service.
105 struct ServiceListeningInfo *listen_head;
108 * Linked list of listen sockets associated with this service.
110 struct ServiceListeningInfo *listen_tail;
113 * Name of the service.
118 * Name of the binary used.
123 * Name of the configuration file used.
128 * Client to notify upon kill completion (waitpid), NULL
129 * if we should simply restart the process.
131 struct GNUNET_SERVER_Client *killing_client;
134 * Process structure pointer of the child.
136 struct GNUNET_OS_Process *proc;
139 * Process exponential backoff time
141 struct GNUNET_TIME_Relative backoff;
144 * Absolute time at which the process is scheduled to restart in case of death
146 struct GNUNET_TIME_Absolute restart_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;
244 /* calculate listen socket list */
247 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
249 GNUNET_array_append (lsocks, ls,
250 GNUNET_NETWORK_get_fd (sli->listen_socket));
251 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
253 GNUNET_SCHEDULER_cancel (sli->accept_task);
254 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
258 GNUNET_array_append (lsocks, ls, INVALID_SOCKET);
260 GNUNET_array_append (lsocks, ls, -1);
263 /* obtain configuration */
265 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "PREFIX",
267 loprefix = GNUNET_strdup (prefix_command);
269 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "OPTIONS",
272 options = GNUNET_strdup (final_option);
273 if (NULL == strstr (options, "%"))
275 /* replace '{}' with service name */
276 while (NULL != (optpos = strstr (options, "{}")))
280 GNUNET_asprintf (&optpos, options, sl->name);
281 GNUNET_free (options);
284 /* replace '$PATH' with value associated with "PATH" */
285 while (NULL != (optpos = strstr (options, "$")))
288 while (isupper ((unsigned char) *optend))
297 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
299 val = GNUNET_strdup ("");
301 GNUNET_asprintf (&optpos, "%s%s%c%s", options, val, b, next);
302 GNUNET_free (options);
308 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
310 /* 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);
316 GNUNET_assert (NULL == sl->proc);
317 if (GNUNET_YES == use_debug)
319 do_start_process (sl->pipe_control,
320 lsocks, loprefix, sl->binary, "-c", sl->config, "-L",
321 "DEBUG", options, NULL);
324 do_start_process (sl->pipe_control,
325 lsocks, loprefix, sl->binary, "-c", sl->config,
327 if (sl->proc == NULL)
328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to start service `%s'\n"),
331 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting service `%s'\n"),
334 GNUNET_free (loprefix);
335 GNUNET_free (options);
336 GNUNET_array_grow (lsocks, ls, 0);
341 * Transmit a status result message.
343 * @param cls pointer to "unit16_t*" with message type
344 * @param size number of bytes available in buf
345 * @param buf where to copy the message, NULL on error
346 * @return number of bytes copied to buf
349 write_result (void *cls, size_t size, void *buf)
351 enum GNUNET_ARM_ProcessStatus *res = cls;
352 struct GNUNET_ARM_ResultMessage *msg;
356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
357 _("Could not send status result to client\n"));
358 return 0; /* error, not much we can do */
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Sending status response %u to client\n", (unsigned int) *res);
364 GNUNET_assert (size >= sizeof (struct GNUNET_ARM_ResultMessage));
366 msg->header.size = htons (sizeof (struct GNUNET_ARM_ResultMessage));
367 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_RESULT);
368 msg->status = htonl ((uint32_t) (*res));
370 return sizeof (struct GNUNET_ARM_ResultMessage);
374 * Transmit the list of running services.
376 * @param cls pointer to struct GNUNET_ARM_ListResultMessage with the message
377 * @param size number of bytes available in buf
378 * @param buf where to copy the message, NULL on error
379 * @return number of bytes copied to buf
382 write_list_result (void *cls, size_t size, void *buf)
384 struct GNUNET_ARM_ListResultMessage *msg = cls;
385 struct GNUNET_ARM_ListResultMessage *rslt;
390 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
391 _("Could not send list result to client\n"));
392 return 0; /* error, not much we can do */
395 GNUNET_assert (size >= msg->header.size);
397 rslt->header.size = htons (msg->header.size);
398 rslt->header.type = htons (msg->header.type);
399 rslt->count = htons (msg->count);
401 size_t list_size = msg->header.size
402 - sizeof (struct GNUNET_ARM_ListResultMessage);
403 memcpy (&rslt[1], &msg[1], list_size);
405 rslt_size = msg->header.size;
412 * Signal our client that we will start or stop the
415 * @param client who is being signalled
416 * @param name name of the service
417 * @param result message type to send
418 * @return NULL if it was not found
421 signal_result (struct GNUNET_SERVER_Client *client, const char *name,
422 enum GNUNET_ARM_ProcessStatus result)
424 enum GNUNET_ARM_ProcessStatus *res;
428 /* FIXME: this is not super-clean yet... */
429 res = GNUNET_malloc (sizeof (enum GNUNET_ARM_ProcessStatus));
431 GNUNET_SERVER_notify_transmit_ready (client,
433 GNUNET_ARM_ResultMessage),
434 GNUNET_TIME_UNIT_FOREVER_REL,
436 GNUNET_SERVER_receive_done (client, GNUNET_OK);
441 * Find the process with the given service
442 * name in the given list and return it.
444 * @param name which service entry to look up
445 * @return NULL if it was not found
447 static struct ServiceList *
448 find_service (const char *name)
450 struct ServiceList *sl;
455 if (0 == strcasecmp (sl->name, name))
464 * First connection has come to the listening socket associated with the service,
465 * create the service in order to relay the incoming connection to it
467 * @param cls callback data, struct ServiceListeningInfo describing a listen socket
471 accept_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
473 struct ServiceListeningInfo *sli = cls;
474 struct ServiceList *sl = sli->sl;
476 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
477 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
484 * Creating a listening socket for each of the service's addresses and
485 * wait for the first incoming connection to it
487 * @param sa address associated with the service
488 * @param addr_len length of sa
489 * @param sl service entry for the service in question
492 create_listen_socket (struct sockaddr *sa, socklen_t addr_len,
493 struct ServiceList *sl)
495 const static int on = 1;
496 struct GNUNET_NETWORK_Handle *sock;
497 struct ServiceListeningInfo *sli;
499 switch (sa->sa_family)
502 sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
505 sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
508 if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */
510 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
515 errno = EAFNOSUPPORT;
520 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
521 _("Unable to create socket for service `%s': %s\n"),
522 sl->name, STRERROR (errno));
526 if (GNUNET_NETWORK_socket_setsockopt
527 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
528 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
531 if ((sa->sa_family == AF_INET6) &&
532 (GNUNET_NETWORK_socket_setsockopt
533 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
534 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
538 if (GNUNET_NETWORK_socket_bind
539 (sock, (const struct sockaddr *) sa, addr_len) != GNUNET_OK)
541 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
543 ("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
544 sl->name, GNUNET_a2s (sa, addr_len), STRERROR (errno));
545 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
549 if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
551 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
552 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
556 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
557 _("ARM now monitors connections to service `%s' at `%s'\n"),
558 sl->name, GNUNET_a2s (sa, addr_len));
559 sli = GNUNET_malloc (sizeof (struct ServiceListeningInfo));
560 sli->service_addr = sa;
561 sli->service_addr_len = addr_len;
562 sli->listen_socket = sock;
565 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock,
566 &accept_connection, sli);
567 GNUNET_CONTAINER_DLL_insert (sl->listen_head, sl->listen_tail, sli);
572 * Remove and free an entry in the service list. Listen sockets
573 * must have already been cleaned up. Only to be called during shutdown.
575 * @param sl entry to free
578 free_service (struct ServiceList *sl)
580 GNUNET_assert (GNUNET_YES == in_shutdown);
581 GNUNET_CONTAINER_DLL_remove (running_head, running_tail, sl);
582 GNUNET_assert (NULL == sl->listen_head);
583 GNUNET_free_non_null (sl->config);
584 GNUNET_free_non_null (sl->binary);
585 GNUNET_free (sl->name);
591 * Handle START-message.
593 * @param cls closure (always NULL)
594 * @param client identification of the client
595 * @param message the actual message
596 * @return GNUNET_OK to keep the connection open,
597 * GNUNET_SYSERR to close it (signal serious error)
600 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
601 const struct GNUNET_MessageHeader *message)
603 const char *servicename;
604 struct ServiceList *sl;
607 size = ntohs (message->size);
608 size -= sizeof (struct GNUNET_MessageHeader);
609 servicename = (const char *) &message[1];
610 if ((size == 0) || (servicename[size - 1] != '\0'))
613 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
616 if (GNUNET_YES == in_shutdown)
618 signal_result (client, servicename, GNUNET_ARM_PROCESS_SHUTDOWN);
621 sl = find_service (servicename);
624 signal_result (client, servicename, GNUNET_ARM_PROCESS_UNKNOWN);
627 sl->is_default = GNUNET_YES;
628 if (sl->proc != NULL)
630 signal_result (client, servicename, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
634 signal_result (client, servicename, GNUNET_ARM_PROCESS_STARTING);
639 * Handle STOP-message.
641 * @param cls closure (always NULL)
642 * @param client identification of the client
643 * @param message the actual message
644 * @return GNUNET_OK to keep the connection open,
645 * GNUNET_SYSERR to close it (signal serious error)
648 handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
649 const struct GNUNET_MessageHeader *message)
651 struct ServiceList *sl;
652 const char *servicename;
655 size = ntohs (message->size);
656 size -= sizeof (struct GNUNET_MessageHeader);
657 servicename = (const char *) &message[1];
658 if ((size == 0) || (servicename[size - 1] != '\0'))
661 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
664 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
665 _("Preparing to stop `%s'\n"), servicename);
666 sl = find_service (servicename);
669 signal_result (client, servicename, GNUNET_ARM_PROCESS_UNKNOWN);
672 sl->is_default = GNUNET_NO;
673 if (GNUNET_YES == in_shutdown)
675 /* shutdown in progress */
676 signal_result (client, servicename, GNUNET_ARM_PROCESS_SHUTDOWN);
679 if (sl->killing_client != NULL)
681 /* killing already in progress */
682 signal_result (client, servicename,
683 GNUNET_ARM_PROCESS_ALREADY_STOPPING);
686 if (sl->proc == NULL)
688 /* process is down */
689 signal_result (client, servicename, GNUNET_ARM_PROCESS_ALREADY_DOWN);
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694 "Sending kill signal to service `%s', waiting for process to die.\n",
697 if (0 != GNUNET_OS_process_kill (sl->proc, SIGTERM))
698 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
699 sl->killing_client = client;
700 GNUNET_SERVER_client_keep (client);
704 * Handle LIST-message.
706 * @param cls closure (always NULL)
707 * @param client identification of the client
708 * @param message the actual message
711 handle_list (void *cls, struct GNUNET_SERVER_Client *client,
712 const struct GNUNET_MessageHeader *message)
714 struct GNUNET_ARM_ListResultMessage *msg;
715 size_t string_list_size;
717 struct ServiceList *sl;
724 string_list_size = 0;
725 /* first count the running processes get their name's size */
726 for (sl = running_head; sl != NULL; sl = sl->next)
728 if (sl->proc != NULL)
730 string_list_size += strlen (sl->name);
731 string_list_size += strlen (sl->binary);
732 string_list_size += 4;
736 total_size = sizeof (struct GNUNET_ARM_ListResultMessage)
738 msg = GNUNET_malloc (total_size);
739 msg->header.size = total_size;
740 msg->header.type = GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT;
743 char *pos = (char *)&msg[1];
744 for (sl = running_head; sl != NULL; sl = sl->next)
746 if (sl->proc != NULL)
748 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
749 GNUNET_snprintf(pos, s, "%s (%s)", sl->name, sl->binary);
754 GNUNET_SERVER_notify_transmit_ready (client,
756 GNUNET_TIME_UNIT_FOREVER_REL,
757 &write_list_result, msg);
758 GNUNET_SERVER_receive_done (client, GNUNET_OK);
762 * We are done with everything. Stop remaining
763 * tasks, signal handler and the server.
770 GNUNET_SERVER_destroy (server);
773 if (GNUNET_SCHEDULER_NO_TASK != child_death_task)
775 GNUNET_SCHEDULER_cancel (child_death_task);
776 child_death_task = GNUNET_SCHEDULER_NO_TASK;
782 * Task run for shutdown.
784 * @param cls closure, NULL if we need to self-restart
788 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
790 struct ServiceList *pos;
791 struct ServiceList *nxt;
792 struct ServiceListeningInfo *sli;
794 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
796 GNUNET_SCHEDULER_cancel (child_restart_task);
797 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
799 in_shutdown = GNUNET_YES;
800 /* first, stop listening */
801 for (pos = running_head; NULL != pos; pos = pos->next)
803 while (NULL != (sli = pos->listen_head))
805 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
806 pos->listen_tail, sli);
807 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
809 GNUNET_SCHEDULER_cancel (sli->accept_task);
810 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
812 GNUNET_break (GNUNET_OK ==
813 GNUNET_NETWORK_socket_close (sli->listen_socket));
814 GNUNET_free (sli->service_addr);
818 /* then, shutdown all existing service processes */
820 while (NULL != (pos = nxt))
823 if (pos->proc != NULL)
825 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping service `%s'\n",
827 if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
828 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
835 /* finally, should all service processes be already gone, terminate for real */
836 if (running_head == NULL)
842 * Task run whenever it is time to restart a child that died.
844 * @param cls closure, always NULL
848 delayed_restart_task (void *cls,
849 const struct GNUNET_SCHEDULER_TaskContext *tc)
851 struct ServiceList *sl;
852 struct GNUNET_TIME_Relative lowestRestartDelay;
853 struct ServiceListeningInfo *sli;
855 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
856 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
858 GNUNET_assert (GNUNET_NO == in_shutdown);
859 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
861 /* check for services that need to be restarted due to
862 * configuration changes or because the last restart failed */
863 for (sl = running_head; NULL != sl; sl = sl->next)
865 if (sl->proc == NULL)
867 /* service is currently not running */
868 if (GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value ==
871 /* restart is now allowed */
874 /* process should run by default, start immediately */
875 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
876 _("Restarting service `%s'.\n"), sl->name);
881 /* process is run on-demand, ensure it is re-started if there is demand */
882 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
883 if (GNUNET_SCHEDULER_NO_TASK == sli->accept_task)
885 /* accept was actually paused, so start it again */
887 GNUNET_SCHEDULER_add_read_net
888 (GNUNET_TIME_UNIT_FOREVER_REL, sli->listen_socket,
889 &accept_connection, sli);
895 /* update calculation for earliest time to reactivate a service */
897 GNUNET_TIME_relative_min (lowestRestartDelay,
898 GNUNET_TIME_absolute_get_remaining
903 if (lowestRestartDelay.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will restart process in %llums\n",
907 (unsigned long long) lowestRestartDelay.rel_value);
910 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
911 GNUNET_SCHEDULER_PRIORITY_IDLE,
912 &delayed_restart_task, NULL);
918 * Task triggered whenever we receive a SIGCHLD (child
921 * @param cls closure, NULL if we need to self-restart
925 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
927 struct ServiceList *pos;
928 struct ServiceList *next;
929 struct ServiceListeningInfo *sli;
934 enum GNUNET_OS_ProcessStatusType statusType;
935 unsigned long statusCode;
936 const struct GNUNET_DISK_FileHandle *pr;
938 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
939 child_death_task = GNUNET_SCHEDULER_NO_TASK;
940 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
942 /* shutdown scheduled us, ignore! */
944 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
945 pr, &maint_child_death, NULL);
948 /* consume the signal */
949 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
951 /* check for services that died (WAITPID) */
953 while (NULL != (pos = next))
957 if (pos->proc == NULL)
959 if (GNUNET_YES == in_shutdown)
963 if ((GNUNET_SYSERR ==
965 GNUNET_OS_process_status (pos->proc, &statusType, &statusCode)))
966 || ((ret == GNUNET_NO) || (statusType == GNUNET_OS_PROCESS_STOPPED)
967 || (statusType == GNUNET_OS_PROCESS_RUNNING)))
969 if (statusType == GNUNET_OS_PROCESS_EXITED)
971 statstr = _( /* process termination method */ "exit");
972 statcode = statusCode;
974 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
976 statstr = _( /* process termination method */ "signal");
977 statcode = statusCode;
981 statstr = _( /* process termination method */ "unknown");
984 GNUNET_OS_process_close (pos->proc);
986 if (NULL != pos->killing_client)
988 signal_result (pos->killing_client, pos->name,
989 GNUNET_ARM_PROCESS_DOWN);
990 GNUNET_SERVER_client_drop (pos->killing_client);
991 pos->killing_client = NULL;
992 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
993 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
995 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sli->accept_task);
997 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
999 &accept_connection, sli);
1003 if (GNUNET_YES != in_shutdown)
1005 if ((statusType == GNUNET_OS_PROCESS_EXITED) && (statcode == 0))
1007 /* process terminated normally, allow restart at any time */
1008 pos->restart_at.abs_value = 0;
1012 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1013 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1015 ("Service `%s' terminated with status %s/%d, will restart in %llu ms\n"),
1016 pos->name, statstr, statcode, pos->backoff.rel_value);
1017 /* schedule restart */
1018 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1020 GNUNET_TIME_relative_min (EXPONENTIAL_BACKOFF_THRESHOLD,
1021 GNUNET_TIME_relative_multiply
1024 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
1025 GNUNET_SCHEDULER_cancel (child_restart_task);
1026 child_restart_task =
1027 GNUNET_SCHEDULER_add_with_priority
1028 (GNUNET_SCHEDULER_PRIORITY_IDLE,
1029 &delayed_restart_task, NULL);
1037 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1038 pr, &maint_child_death, NULL);
1039 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1045 * Transmit our shutdown acknowledgement to the client.
1047 * @param cls the 'struct GNUNET_SERVER_Client'
1048 * @param size number of bytes available in buf
1049 * @param buf where to write the message
1050 * @return number of bytes written
1053 transmit_shutdown_ack (void *cls, size_t size, void *buf)
1055 struct GNUNET_SERVER_Client *client = cls;
1056 struct GNUNET_ARM_ResultMessage *msg;
1058 if (size < sizeof (struct GNUNET_ARM_ResultMessage))
1060 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1061 _("Failed to transmit shutdown ACK.\n"));
1062 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1063 return 0; /* client disconnected */
1065 /* Make the connection flushing for the purpose of ACK transmitting,
1066 * needed on W32 to ensure that the message is even received, harmless
1067 * on other platforms... */
1068 GNUNET_break (GNUNET_OK == GNUNET_SERVER_client_disable_corking (client));
1069 msg = (struct GNUNET_ARM_ResultMessage *) buf;
1070 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_RESULT);
1071 msg->header.size = htons (sizeof (struct GNUNET_ARM_ResultMessage));
1072 msg->status = htonl ((uint32_t) GNUNET_ARM_PROCESS_SHUTDOWN);
1073 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1074 GNUNET_SERVER_client_drop (client);
1075 return sizeof (struct GNUNET_ARM_ResultMessage);
1080 * Handler for SHUTDOWN message.
1082 * @param cls closure (refers to service)
1083 * @param client identification of the client
1084 * @param message the actual message
1087 handle_shutdown (void *cls, struct GNUNET_SERVER_Client *client,
1088 const struct GNUNET_MessageHeader *message)
1090 GNUNET_SCHEDULER_shutdown ();
1091 GNUNET_SERVER_client_keep (client);
1092 GNUNET_SERVER_notify_transmit_ready (client,
1093 sizeof (struct GNUNET_ARM_ResultMessage),
1094 GNUNET_TIME_UNIT_FOREVER_REL,
1095 &transmit_shutdown_ack, client);
1096 GNUNET_SERVER_client_persist_ (client);
1100 * Signal handler called for SIGCHLD. Triggers the
1101 * respective handler by writing to the trigger pipe.
1104 sighandler_child_death ()
1107 int old_errno = errno; /* back-up errno */
1110 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
1111 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
1113 errno = old_errno; /* restore errno */
1118 * Setup our service record for the given section in the configuration file
1119 * (assuming the section is for a service).
1122 * @param section a section in the configuration file
1123 * @return GNUNET_OK (continue)
1126 setup_service (void *cls, const char *section)
1128 struct ServiceList *sl;
1132 struct sockaddr **addrs;
1133 socklen_t *addr_lens;
1137 if (strcasecmp (section, "arm") == 0)
1140 GNUNET_CONFIGURATION_get_value_string (cfg, section, "BINARY", &binary))
1142 /* not a service section */
1145 sl = find_service (section);
1148 /* got the same section twice!? */
1154 GNUNET_CONFIGURATION_get_value_filename (cfg, section, "CONFIG",
1156 (0 != STAT (config, &sbuf)))
1158 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1160 ("Configuration file `%s' for service `%s' not valid: %s\n"),
1162 (config == NULL) ? _("option missing") : STRERROR (errno));
1163 GNUNET_free (binary);
1164 GNUNET_free_non_null (config);
1167 sl = GNUNET_malloc (sizeof (struct ServiceList));
1168 sl->name = GNUNET_strdup (section);
1169 sl->binary = binary;
1170 sl->config = config;
1171 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1172 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1174 sl->pipe_control = GNUNET_YES;
1176 if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
1177 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
1179 GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
1181 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "AUTOSTART"))
1183 if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section, cfg,
1184 &addrs, &addr_lens)))
1186 /* this will free (or capture) addrs[i] */
1187 for (i = 0; i < ret; i++)
1188 create_listen_socket (addrs[i], addr_lens[i], sl);
1189 GNUNET_free (addrs);
1190 GNUNET_free (addr_lens);
1195 * Process arm requests.
1197 * @param cls closure
1198 * @param serv the initialized server
1199 * @param c configuration to use
1202 run (void *cls, struct GNUNET_SERVER_Handle *serv,
1203 const struct GNUNET_CONFIGURATION_Handle *c)
1205 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1206 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
1207 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
1208 {&handle_shutdown, NULL, GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN,
1209 sizeof (struct GNUNET_MessageHeader)},
1210 {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST,
1211 sizeof (struct GNUNET_MessageHeader)},
1214 char *defaultservices;
1216 struct ServiceList *sl;
1220 GNUNET_assert (serv != NULL);
1221 GNUNET_SERVER_ignore_shutdown (serv, GNUNET_YES);
1222 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1225 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1226 GNUNET_DISK_pipe_handle (sigpipe,
1227 GNUNET_DISK_PIPE_END_READ),
1228 &maint_child_death, NULL);
1231 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_PREFIX",
1233 prefix_command = GNUNET_strdup ("");
1235 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
1237 final_option = GNUNET_strdup ("");
1239 GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
1241 /* start default services... */
1243 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "DEFAULTSERVICES",
1246 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1247 _("Starting default services `%s'\n"), defaultservices);
1248 if (0 < strlen (defaultservices))
1250 for (pos = strtok (defaultservices, " "); NULL != pos;
1251 pos = strtok (NULL, " "))
1253 sl = find_service (pos);
1256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1258 ("Default service `%s' not configured correctly!\n"),
1262 sl->is_default = GNUNET_YES;
1266 GNUNET_free (defaultservices);
1270 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1272 ("No default services configured, GNUnet will not really start right now.\n"));
1275 /* process client requests */
1276 GNUNET_SERVER_add_handlers (server, handlers);
1281 * The main function for the arm service.
1283 * @param argc number of arguments from the command line
1284 * @param argv command line arguments
1285 * @return 0 ok, 1 on error
1288 main (int argc, char *const *argv)
1291 struct GNUNET_SIGNAL_Context *shc_chld;
1293 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
1294 GNUNET_assert (sigpipe != NULL);
1296 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
1299 GNUNET_SERVICE_run (argc, argv, "arm", GNUNET_YES, &run, NULL)) ? 0 : 1;
1300 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1302 GNUNET_DISK_pipe_close (sigpipe);
1312 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1314 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1316 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1317 mallopt (M_TOP_PAD, 1 * 1024);
1323 /* end of gnunet-service-arm.c */