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 * Time we asked the service to shut down (used to calculate time it took
150 * the service to terminate).
152 struct GNUNET_TIME_Absolute killed_at;
155 * Is this service to be started by default (or did a client tell us explicitly
156 * to start it)? GNUNET_NO if the service is started only upon 'accept' on a
157 * listen socket or possibly explicitly by a client changing the value.
162 * Should we use pipes to signal this process? (YES for Java binaries and if we
169 * List of running services.
171 static struct ServiceList *running_head;
174 * List of running services.
176 static struct ServiceList *running_tail;
181 static const struct GNUNET_CONFIGURATION_Handle *cfg;
184 * Command to prepend to each actual command.
186 static char *prefix_command;
189 * Option to append to each actual command.
191 static char *final_option;
194 * ID of task called whenever we get a SIGCHILD.
196 static GNUNET_SCHEDULER_TaskIdentifier child_death_task;
199 * ID of task called whenever the timeout for restarting a child
202 static GNUNET_SCHEDULER_TaskIdentifier child_restart_task;
205 * Pipe used to communicate shutdown via signal.
207 static struct GNUNET_DISK_PipeHandle *sigpipe;
210 * Are we in shutdown mode?
212 static int in_shutdown;
215 * Handle to our server instance. Our server is a bit special in that
216 * its service is not immediately stopped once we get a shutdown
217 * request (since we need to continue service until all of our child
218 * processes are dead). This handle is used to shut down the server
219 * (and thus trigger process termination) once all child processes are
220 * also dead. A special option in the ARM configuration modifies the
221 * behaviour of the service implementation to not do the shutdown
224 static struct GNUNET_SERVER_Handle *server;
227 #include "do_start_process.c"
231 * Actually start the process for the given service.
233 * @param sl identifies service to start
236 start_process (struct ServiceList *sl)
246 struct ServiceListeningInfo *sli;
250 /* calculate listen socket list */
253 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
255 GNUNET_array_append (lsocks, ls,
256 GNUNET_NETWORK_get_fd (sli->listen_socket));
257 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
259 GNUNET_SCHEDULER_cancel (sli->accept_task);
260 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
264 GNUNET_array_append (lsocks, ls, INVALID_SOCKET);
266 GNUNET_array_append (lsocks, ls, -1);
269 /* obtain configuration */
271 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "PREFIX",
273 loprefix = GNUNET_strdup (prefix_command);
275 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "OPTIONS",
278 options = GNUNET_strdup (final_option);
279 if (NULL == strstr (options, "%"))
281 /* replace '{}' with service name */
282 while (NULL != (optpos = strstr (options, "{}")))
286 GNUNET_asprintf (&optpos, options, sl->name);
287 GNUNET_free (options);
290 /* replace '$PATH' with value associated with "PATH" */
291 while (NULL != (optpos = strstr (options, "$")))
294 while (isupper ((unsigned char) *optend))
303 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
305 val = GNUNET_strdup ("");
307 GNUNET_asprintf (&optpos, "%s%s%c%s", options, val, b, next);
308 GNUNET_free (options);
314 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
316 /* actually start process */
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "Starting service `%s' using binary `%s' and configuration `%s'\n",
319 sl->name, sl->binary, sl->config);
320 GNUNET_assert (NULL == sl->proc);
321 if (GNUNET_YES == use_debug)
323 do_start_process (sl->pipe_control,
324 lsocks, loprefix, sl->binary, "-c", sl->config, "-L",
325 "DEBUG", options, NULL);
328 do_start_process (sl->pipe_control,
329 lsocks, loprefix, sl->binary, "-c", sl->config,
331 if (sl->proc == NULL)
332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to start service `%s'\n"),
335 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting service `%s'\n"),
338 GNUNET_free (loprefix);
339 GNUNET_free (options);
340 GNUNET_array_grow (lsocks, ls, 0);
345 * Transmit a status result message.
347 * @param cls pointer to "unit16_t*" with message type
348 * @param size number of bytes available in buf
349 * @param buf where to copy the message, NULL on error
350 * @return number of bytes copied to buf
353 write_result (void *cls, size_t size, void *buf)
355 enum GNUNET_ARM_ProcessStatus *res = cls;
356 struct GNUNET_ARM_ResultMessage *msg;
360 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
361 _("Could not send status result to client\n"));
362 return 0; /* error, not much we can do */
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "Sending status response %u to client\n", (unsigned int) *res);
366 GNUNET_assert (size >= sizeof (struct GNUNET_ARM_ResultMessage));
368 msg->header.size = htons (sizeof (struct GNUNET_ARM_ResultMessage));
369 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_RESULT);
370 msg->status = htonl ((uint32_t) (*res));
372 return sizeof (struct GNUNET_ARM_ResultMessage);
376 * Transmit the list of running services.
378 * @param cls pointer to struct GNUNET_ARM_ListResultMessage with the message
379 * @param size number of bytes available in buf
380 * @param buf where to copy the message, NULL on error
381 * @return number of bytes copied to buf
384 write_list_result (void *cls, size_t size, void *buf)
386 struct GNUNET_ARM_ListResultMessage *msg = cls;
387 struct GNUNET_ARM_ListResultMessage *rslt;
392 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
393 _("Could not send list result to client\n"));
394 return 0; /* error, not much we can do */
397 GNUNET_assert (size >= msg->header.size);
399 rslt->header.size = htons (msg->header.size);
400 rslt->header.type = htons (msg->header.type);
401 rslt->count = htons (msg->count);
403 size_t list_size = msg->header.size
404 - sizeof (struct GNUNET_ARM_ListResultMessage);
405 memcpy (&rslt[1], &msg[1], list_size);
407 rslt_size = msg->header.size;
414 * Signal our client that we will start or stop the
417 * @param client who is being signalled
418 * @param name name of the service
419 * @param result message type to send
420 * @return NULL if it was not found
423 signal_result (struct GNUNET_SERVER_Client *client, const char *name,
424 enum GNUNET_ARM_ProcessStatus result)
426 enum GNUNET_ARM_ProcessStatus *res;
430 /* FIXME: this is not super-clean yet... */
431 res = GNUNET_malloc (sizeof (enum GNUNET_ARM_ProcessStatus));
433 GNUNET_SERVER_notify_transmit_ready (client,
435 GNUNET_ARM_ResultMessage),
436 GNUNET_TIME_UNIT_FOREVER_REL,
438 GNUNET_SERVER_receive_done (client, GNUNET_OK);
443 * Find the process with the given service
444 * name in the given list and return it.
446 * @param name which service entry to look up
447 * @return NULL if it was not found
449 static struct ServiceList *
450 find_service (const char *name)
452 struct ServiceList *sl;
457 if (0 == strcasecmp (sl->name, name))
466 * First connection has come to the listening socket associated with the service,
467 * create the service in order to relay the incoming connection to it
469 * @param cls callback data, struct ServiceListeningInfo describing a listen socket
473 accept_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
475 struct ServiceListeningInfo *sli = cls;
476 struct ServiceList *sl = sli->sl;
478 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
479 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
486 * Creating a listening socket for each of the service's addresses and
487 * wait for the first incoming connection to it
489 * @param sa address associated with the service
490 * @param addr_len length of sa
491 * @param sl service entry for the service in question
494 create_listen_socket (struct sockaddr *sa, socklen_t addr_len,
495 struct ServiceList *sl)
497 const static int on = 1;
498 struct GNUNET_NETWORK_Handle *sock;
499 struct ServiceListeningInfo *sli;
501 switch (sa->sa_family)
504 sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
507 sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
510 if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */
512 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
517 errno = EAFNOSUPPORT;
522 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
523 _("Unable to create socket for service `%s': %s\n"),
524 sl->name, STRERROR (errno));
528 if (GNUNET_NETWORK_socket_setsockopt
529 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
530 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
533 if ((sa->sa_family == AF_INET6) &&
534 (GNUNET_NETWORK_socket_setsockopt
535 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
536 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
540 if (GNUNET_NETWORK_socket_bind
541 (sock, (const struct sockaddr *) sa, addr_len) != GNUNET_OK)
543 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
545 ("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
546 sl->name, GNUNET_a2s (sa, addr_len), STRERROR (errno));
547 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
551 if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
553 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
554 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
558 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
559 _("ARM now monitors connections to service `%s' at `%s'\n"),
560 sl->name, GNUNET_a2s (sa, addr_len));
561 sli = GNUNET_malloc (sizeof (struct ServiceListeningInfo));
562 sli->service_addr = sa;
563 sli->service_addr_len = addr_len;
564 sli->listen_socket = sock;
567 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock,
568 &accept_connection, sli);
569 GNUNET_CONTAINER_DLL_insert (sl->listen_head, sl->listen_tail, sli);
574 * Remove and free an entry in the service list. Listen sockets
575 * must have already been cleaned up. Only to be called during shutdown.
577 * @param sl entry to free
580 free_service (struct ServiceList *sl)
582 GNUNET_assert (GNUNET_YES == in_shutdown);
583 GNUNET_CONTAINER_DLL_remove (running_head, running_tail, sl);
584 GNUNET_assert (NULL == sl->listen_head);
585 GNUNET_free_non_null (sl->config);
586 GNUNET_free_non_null (sl->binary);
587 GNUNET_free (sl->name);
593 * Handle START-message.
595 * @param cls closure (always NULL)
596 * @param client identification of the client
597 * @param message the actual message
598 * @return GNUNET_OK to keep the connection open,
599 * GNUNET_SYSERR to close it (signal serious error)
602 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
603 const struct GNUNET_MessageHeader *message)
605 const char *servicename;
606 struct ServiceList *sl;
609 size = ntohs (message->size);
610 size -= sizeof (struct GNUNET_MessageHeader);
611 servicename = (const char *) &message[1];
612 if ((size == 0) || (servicename[size - 1] != '\0'))
615 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
618 if (GNUNET_YES == in_shutdown)
620 signal_result (client, servicename, GNUNET_ARM_PROCESS_SHUTDOWN);
623 sl = find_service (servicename);
626 signal_result (client, servicename, GNUNET_ARM_PROCESS_UNKNOWN);
629 sl->is_default = GNUNET_YES;
630 if (sl->proc != NULL)
632 signal_result (client, servicename, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
636 signal_result (client, servicename, GNUNET_ARM_PROCESS_STARTING);
641 * Handle STOP-message.
643 * @param cls closure (always NULL)
644 * @param client identification of the client
645 * @param message the actual message
646 * @return GNUNET_OK to keep the connection open,
647 * GNUNET_SYSERR to close it (signal serious error)
650 handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
651 const struct GNUNET_MessageHeader *message)
653 struct ServiceList *sl;
654 const char *servicename;
657 size = ntohs (message->size);
658 size -= sizeof (struct GNUNET_MessageHeader);
659 servicename = (const char *) &message[1];
660 if ((size == 0) || (servicename[size - 1] != '\0'))
663 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
666 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
667 _("Preparing to stop `%s'\n"), servicename);
668 sl = find_service (servicename);
671 signal_result (client, servicename, GNUNET_ARM_PROCESS_UNKNOWN);
674 sl->is_default = GNUNET_NO;
675 if (GNUNET_YES == in_shutdown)
677 /* shutdown in progress */
678 signal_result (client, servicename, GNUNET_ARM_PROCESS_SHUTDOWN);
681 if (sl->killing_client != NULL)
683 /* killing already in progress */
684 signal_result (client, servicename,
685 GNUNET_ARM_PROCESS_ALREADY_STOPPING);
688 if (sl->proc == NULL)
690 /* process is down */
691 signal_result (client, servicename, GNUNET_ARM_PROCESS_ALREADY_DOWN);
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 "Sending kill signal to service `%s', waiting for process to die.\n",
697 sl->killed_at = GNUNET_TIME_absolute_get ();
698 if (0 != GNUNET_OS_process_kill (sl->proc, SIGTERM))
699 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
700 sl->killing_client = client;
701 GNUNET_SERVER_client_keep (client);
705 * Handle LIST-message.
707 * @param cls closure (always NULL)
708 * @param client identification of the client
709 * @param message the actual message
712 handle_list (void *cls, struct GNUNET_SERVER_Client *client,
713 const struct GNUNET_MessageHeader *message)
715 struct GNUNET_ARM_ListResultMessage *msg;
716 size_t string_list_size;
718 struct ServiceList *sl;
725 string_list_size = 0;
726 /* first count the running processes get their name's size */
727 for (sl = running_head; sl != NULL; sl = sl->next)
729 if (sl->proc != NULL)
731 string_list_size += strlen (sl->name);
732 string_list_size += strlen (sl->binary);
733 string_list_size += 4;
737 total_size = sizeof (struct GNUNET_ARM_ListResultMessage)
739 msg = GNUNET_malloc (total_size);
740 msg->header.size = total_size;
741 msg->header.type = GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT;
744 char *pos = (char *)&msg[1];
745 for (sl = running_head; sl != NULL; sl = sl->next)
747 if (sl->proc != NULL)
749 size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
750 GNUNET_snprintf(pos, s, "%s (%s)", sl->name, sl->binary);
755 GNUNET_SERVER_notify_transmit_ready (client,
757 GNUNET_TIME_UNIT_FOREVER_REL,
758 &write_list_result, msg);
759 GNUNET_SERVER_receive_done (client, GNUNET_OK);
763 * We are done with everything. Stop remaining
764 * tasks, signal handler and the server.
771 GNUNET_SERVER_destroy (server);
774 if (GNUNET_SCHEDULER_NO_TASK != child_death_task)
776 GNUNET_SCHEDULER_cancel (child_death_task);
777 child_death_task = GNUNET_SCHEDULER_NO_TASK;
783 * Task run for shutdown.
785 * @param cls closure, NULL if we need to self-restart
789 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
791 struct ServiceList *pos;
792 struct ServiceList *nxt;
793 struct ServiceListeningInfo *sli;
795 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
797 GNUNET_SCHEDULER_cancel (child_restart_task);
798 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
800 in_shutdown = GNUNET_YES;
801 /* first, stop listening */
802 for (pos = running_head; NULL != pos; pos = pos->next)
804 while (NULL != (sli = pos->listen_head))
806 GNUNET_CONTAINER_DLL_remove (pos->listen_head,
807 pos->listen_tail, sli);
808 if (sli->accept_task != GNUNET_SCHEDULER_NO_TASK)
810 GNUNET_SCHEDULER_cancel (sli->accept_task);
811 sli->accept_task = GNUNET_SCHEDULER_NO_TASK;
813 GNUNET_break (GNUNET_OK ==
814 GNUNET_NETWORK_socket_close (sli->listen_socket));
815 GNUNET_free (sli->service_addr);
819 /* then, shutdown all existing service processes */
821 while (NULL != (pos = nxt))
824 if (pos->proc != NULL)
826 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping service `%s'\n",
828 pos->killed_at = GNUNET_TIME_absolute_get ();
829 if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
830 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
837 /* finally, should all service processes be already gone, terminate for real */
838 if (running_head == NULL)
844 * Task run whenever it is time to restart a child that died.
846 * @param cls closure, always NULL
850 delayed_restart_task (void *cls,
851 const struct GNUNET_SCHEDULER_TaskContext *tc)
853 struct ServiceList *sl;
854 struct GNUNET_TIME_Relative lowestRestartDelay;
855 struct ServiceListeningInfo *sli;
857 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
858 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
860 GNUNET_assert (GNUNET_NO == in_shutdown);
861 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
863 /* check for services that need to be restarted due to
864 * configuration changes or because the last restart failed */
865 for (sl = running_head; NULL != sl; sl = sl->next)
867 if (NULL != sl->proc)
869 /* service is currently not running */
870 if (GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value ==
873 /* restart is now allowed */
876 /* process should run by default, start immediately */
877 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
878 _("Restarting service `%s'.\n"), sl->name);
883 /* process is run on-demand, ensure it is re-started if there is demand */
884 for (sli = sl->listen_head; NULL != sli; sli = sli->next)
885 if (GNUNET_SCHEDULER_NO_TASK == sli->accept_task)
887 /* accept was actually paused, so start it again */
889 GNUNET_SCHEDULER_add_read_net
890 (GNUNET_TIME_UNIT_FOREVER_REL, sli->listen_socket,
891 &accept_connection, sli);
897 /* update calculation for earliest time to reactivate a service */
899 GNUNET_TIME_relative_min (lowestRestartDelay,
900 GNUNET_TIME_absolute_get_remaining
904 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);
909 GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
910 GNUNET_SCHEDULER_PRIORITY_IDLE,
911 &delayed_restart_task, NULL);
917 * Task triggered whenever we receive a SIGCHLD (child
920 * @param cls closure, NULL if we need to self-restart
924 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
926 struct ServiceList *pos;
927 struct ServiceList *next;
928 struct ServiceListeningInfo *sli;
933 enum GNUNET_OS_ProcessStatusType statusType;
934 unsigned long statusCode;
935 const struct GNUNET_DISK_FileHandle *pr;
937 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
938 child_death_task = GNUNET_SCHEDULER_NO_TASK;
939 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
941 /* shutdown scheduled us, ignore! */
943 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
944 pr, &maint_child_death, NULL);
947 /* consume the signal */
948 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
950 /* check for services that died (WAITPID) */
952 while (NULL != (pos = next))
956 if (pos->proc == NULL)
958 if (GNUNET_YES == in_shutdown)
962 if ((GNUNET_SYSERR ==
964 GNUNET_OS_process_status (pos->proc, &statusType, &statusCode)))
965 || ((ret == GNUNET_NO) || (statusType == GNUNET_OS_PROCESS_STOPPED)
966 || (statusType == GNUNET_OS_PROCESS_RUNNING)))
968 if (statusType == GNUNET_OS_PROCESS_EXITED)
970 statstr = _( /* process termination method */ "exit");
971 statcode = statusCode;
973 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
975 statstr = _( /* process termination method */ "signal");
976 statcode = statusCode;
980 statstr = _( /* process termination method */ "unknown");
983 if (0 != pos->killed_at.abs_value)
985 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
986 _("Service `%s' took %llu ms to terminate\n"),
988 GNUNET_TIME_absolute_get_duration (pos->killed_at).rel_value);
990 GNUNET_OS_process_close (pos->proc);
992 if (NULL != pos->killing_client)
994 signal_result (pos->killing_client, pos->name,
995 GNUNET_ARM_PROCESS_DOWN);
996 GNUNET_SERVER_client_drop (pos->killing_client);
997 pos->killing_client = NULL;
998 /* process can still be re-started on-demand, ensure it is re-started if there is demand */
999 for (sli = pos->listen_head; NULL != sli; sli = sli->next)
1001 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sli->accept_task);
1003 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1005 &accept_connection, sli);
1009 if (GNUNET_YES != in_shutdown)
1011 if ((statusType == GNUNET_OS_PROCESS_EXITED) && (statcode == 0))
1013 /* process terminated normally, allow restart at any time */
1014 pos->restart_at.abs_value = 0;
1018 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1019 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1021 ("Service `%s' terminated with status %s/%d, will restart in %llu ms\n"),
1022 pos->name, statstr, statcode, pos->backoff.rel_value);
1023 /* schedule restart */
1024 pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
1026 GNUNET_TIME_relative_min (EXPONENTIAL_BACKOFF_THRESHOLD,
1027 GNUNET_TIME_relative_multiply
1030 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
1031 GNUNET_SCHEDULER_cancel (child_restart_task);
1032 child_restart_task =
1033 GNUNET_SCHEDULER_add_with_priority
1034 (GNUNET_SCHEDULER_PRIORITY_IDLE,
1035 &delayed_restart_task, NULL);
1043 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1044 pr, &maint_child_death, NULL);
1045 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
1051 * Transmit our shutdown acknowledgement to the client.
1053 * @param cls the 'struct GNUNET_SERVER_Client'
1054 * @param size number of bytes available in buf
1055 * @param buf where to write the message
1056 * @return number of bytes written
1059 transmit_shutdown_ack (void *cls, size_t size, void *buf)
1061 struct GNUNET_SERVER_Client *client = cls;
1062 struct GNUNET_ARM_ResultMessage *msg;
1064 if (size < sizeof (struct GNUNET_ARM_ResultMessage))
1066 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1067 _("Failed to transmit shutdown ACK.\n"));
1068 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1069 return 0; /* client disconnected */
1071 /* Make the connection flushing for the purpose of ACK transmitting,
1072 * needed on W32 to ensure that the message is even received, harmless
1073 * on other platforms... */
1074 GNUNET_break (GNUNET_OK == GNUNET_SERVER_client_disable_corking (client));
1075 msg = (struct GNUNET_ARM_ResultMessage *) buf;
1076 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_RESULT);
1077 msg->header.size = htons (sizeof (struct GNUNET_ARM_ResultMessage));
1078 msg->status = htonl ((uint32_t) GNUNET_ARM_PROCESS_SHUTDOWN);
1079 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1080 GNUNET_SERVER_client_drop (client);
1081 return sizeof (struct GNUNET_ARM_ResultMessage);
1086 * Handler for SHUTDOWN message.
1088 * @param cls closure (refers to service)
1089 * @param client identification of the client
1090 * @param message the actual message
1093 handle_shutdown (void *cls, struct GNUNET_SERVER_Client *client,
1094 const struct GNUNET_MessageHeader *message)
1096 GNUNET_SCHEDULER_shutdown ();
1097 GNUNET_SERVER_client_keep (client);
1098 GNUNET_SERVER_notify_transmit_ready (client,
1099 sizeof (struct GNUNET_ARM_ResultMessage),
1100 GNUNET_TIME_UNIT_FOREVER_REL,
1101 &transmit_shutdown_ack, client);
1102 GNUNET_SERVER_client_persist_ (client);
1106 * Signal handler called for SIGCHLD. Triggers the
1107 * respective handler by writing to the trigger pipe.
1110 sighandler_child_death ()
1113 int old_errno = errno; /* back-up errno */
1116 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
1117 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
1119 errno = old_errno; /* restore errno */
1124 * Setup our service record for the given section in the configuration file
1125 * (assuming the section is for a service).
1128 * @param section a section in the configuration file
1129 * @return GNUNET_OK (continue)
1132 setup_service (void *cls, const char *section)
1134 struct ServiceList *sl;
1138 struct sockaddr **addrs;
1139 socklen_t *addr_lens;
1143 if (strcasecmp (section, "arm") == 0)
1146 GNUNET_CONFIGURATION_get_value_string (cfg, section, "BINARY", &binary))
1148 /* not a service section */
1151 sl = find_service (section);
1154 /* got the same section twice!? */
1160 GNUNET_CONFIGURATION_get_value_filename (cfg, section, "CONFIG",
1162 (0 != STAT (config, &sbuf)))
1164 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1166 ("Configuration file `%s' for service `%s' not valid: %s\n"),
1168 (config == NULL) ? _("option missing") : STRERROR (errno));
1169 GNUNET_free (binary);
1170 GNUNET_free_non_null (config);
1173 sl = GNUNET_malloc (sizeof (struct ServiceList));
1174 sl->name = GNUNET_strdup (section);
1175 sl->binary = binary;
1176 sl->config = config;
1177 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1178 sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
1180 sl->pipe_control = GNUNET_YES;
1182 if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
1183 sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
1185 GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
1187 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "AUTOSTART"))
1189 if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section, cfg,
1190 &addrs, &addr_lens)))
1192 /* this will free (or capture) addrs[i] */
1193 for (i = 0; i < ret; i++)
1194 create_listen_socket (addrs[i], addr_lens[i], sl);
1195 GNUNET_free (addrs);
1196 GNUNET_free (addr_lens);
1201 * Process arm requests.
1203 * @param cls closure
1204 * @param serv the initialized server
1205 * @param c configuration to use
1208 run (void *cls, struct GNUNET_SERVER_Handle *serv,
1209 const struct GNUNET_CONFIGURATION_Handle *c)
1211 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1212 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
1213 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
1214 {&handle_shutdown, NULL, GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN,
1215 sizeof (struct GNUNET_MessageHeader)},
1216 {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST,
1217 sizeof (struct GNUNET_MessageHeader)},
1220 char *defaultservices;
1222 struct ServiceList *sl;
1226 GNUNET_assert (serv != NULL);
1227 GNUNET_SERVER_ignore_shutdown (serv, GNUNET_YES);
1228 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1231 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1232 GNUNET_DISK_pipe_handle (sigpipe,
1233 GNUNET_DISK_PIPE_END_READ),
1234 &maint_child_death, NULL);
1237 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_PREFIX",
1239 prefix_command = GNUNET_strdup ("");
1241 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
1243 final_option = GNUNET_strdup ("");
1245 GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
1247 /* start default services... */
1249 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "DEFAULTSERVICES",
1252 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1253 _("Starting default services `%s'\n"), defaultservices);
1254 if (0 < strlen (defaultservices))
1256 for (pos = strtok (defaultservices, " "); NULL != pos;
1257 pos = strtok (NULL, " "))
1259 sl = find_service (pos);
1262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1264 ("Default service `%s' not configured correctly!\n"),
1268 sl->is_default = GNUNET_YES;
1272 GNUNET_free (defaultservices);
1276 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1278 ("No default services configured, GNUnet will not really start right now.\n"));
1281 /* process client requests */
1282 GNUNET_SERVER_add_handlers (server, handlers);
1287 * The main function for the arm service.
1289 * @param argc number of arguments from the command line
1290 * @param argv command line arguments
1291 * @return 0 ok, 1 on error
1294 main (int argc, char *const *argv)
1297 struct GNUNET_SIGNAL_Context *shc_chld;
1299 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
1300 GNUNET_assert (sigpipe != NULL);
1302 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
1305 GNUNET_SERVICE_run (argc, argv, "arm", GNUNET_YES, &run, NULL)) ? 0 : 1;
1306 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1308 GNUNET_DISK_pipe_close (sigpipe);
1318 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1320 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1322 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1323 mallopt (M_TOP_PAD, 1 * 1024);
1329 /* end of gnunet-service-arm.c */