2 This file is part of GNUnet.
3 (C) 2009, 2010 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 * - need to test auto-restart code on configuration changes;
28 * - should refine restart code to check if *relevant* parts of the
29 * configuration were changed (anything in the section for the service)
30 * - should have a way to specify dependencies between services and
31 * manage restarts of groups of services
33 * + install handler for disconnecting clients!?
36 #include "gnunet_util_lib.h"
37 #include "gnunet_protocols.h"
38 #include "gnunet-service-arm.h"
43 * Check for configuration file changes every 5s.
45 #define MAINT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
48 * Threshold after which exponential backoff shouldn't increase (in ms); 30m
50 #define EXPONENTIAL_BACKOFF_THRESHOLD (1000 * 60 * 30)
53 * List of our services.
59 * List of our services.
64 * This is a doubly-linked list.
66 struct ServiceList *next;
69 * This is a doubly-linked list.
71 struct ServiceList *prev;
74 * Name of the service.
79 * Name of the binary used.
84 * Name of the configuration file used.
89 * Client to notify upon kill completion (waitpid), NULL
90 * if we should simply restart the process.
92 struct GNUNET_SERVER_Client *killing_client;
95 * Process structure pointer of the child.
97 struct GNUNET_OS_Process *proc;
100 * Last time the config of this service was
106 * Process exponential backoff time
108 struct GNUNET_TIME_Relative backoff;
111 * Absolute time at which the process is scheduled to restart in case of death
113 struct GNUNET_TIME_Absolute restartAt;
118 * List of running services.
120 static struct ServiceList *running_head;
123 * List of running services.
125 static struct ServiceList *running_tail;
130 static const struct GNUNET_CONFIGURATION_Handle *cfg;
133 * Command to prepend to each actual command.
135 static char *prefix_command;
138 * Option to append to each actual command.
140 static char *final_option;
143 * ID of task called whenever we get a SIGCHILD.
145 static GNUNET_SCHEDULER_TaskIdentifier child_death_task;
148 * ID of task called whenever the timeout for restarting a child
151 static GNUNET_SCHEDULER_TaskIdentifier child_restart_task;
156 * Pipe used to communicate shutdown via signal.
158 static struct GNUNET_DISK_PipeHandle *sigpipe;
161 * Reading end of the signal pipe.
163 static const struct GNUNET_DISK_FileHandle *pr;
166 * Are we in shutdown mode?
168 static int in_shutdown;
172 * Handle to our server instance. Our server is a bit special in that
173 * its service is not immediately stopped once we get a shutdown
174 * request (since we need to continue service until all of our child
175 * processes are dead). This handle is used to shut down the server
176 * (and thus trigger process termination) once all child processes are
177 * also dead. A special option in the ARM configuration modifies the
178 * behaviour of the service implementation to not do the shutdown
181 static struct GNUNET_SERVER_Handle *server;
185 * If the configuration file changes, restart tasks that depended on that
188 * @param cls closure, NULL if we need to self-restart
192 config_change_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
194 struct ServiceList *pos;
200 /* FIXME: this test for config change may be a bit too coarse grained */
201 if ((0 == STAT (pos->config, &sbuf)) && (pos->mtime < sbuf.st_mtime) &&
204 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
206 ("Restarting service `%s' due to configuration file change.\n"));
207 if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
208 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
210 pos->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
218 * Transmit a status result message.
220 * @param cls pointer to "unit16_t*" with message type
221 * @param size number of bytes available in buf
222 * @param buf where to copy the message, NULL on error
223 * @return number of bytes copied to buf
226 write_result (void *cls, size_t size, void *buf)
229 struct GNUNET_MessageHeader *msg;
233 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
234 _("Could not send status result to client\n"));
235 return 0; /* error, not much we can do */
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending status response %u to client\n",
239 (unsigned int) *res);
241 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
243 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
244 msg->type = htons (*res);
246 return sizeof (struct GNUNET_MessageHeader);
251 * Signal our client that we will start or stop the
254 * @param client who is being signalled
255 * @param name name of the service
256 * @param result message type to send
257 * @return NULL if it was not found
260 signal_result (struct GNUNET_SERVER_Client *client, const char *name,
267 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
268 _("Not sending status result to client: no client known\n"));
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "Telling client that service `%s' is now %s\n", name,
274 result == GNUNET_MESSAGE_TYPE_ARM_IS_DOWN ? "down" : "up");
276 res = GNUNET_malloc (sizeof (uint16_t));
278 GNUNET_SERVER_notify_transmit_ready (client,
279 sizeof (struct GNUNET_MessageHeader),
280 GNUNET_TIME_UNIT_FOREVER_REL,
286 * Find the process with the given service
287 * name in the given list and return it.
289 * @param name which service entry to look up
290 * @return NULL if it was not found
292 static struct ServiceList *
293 find_service (const char *name)
295 struct ServiceList *pos;
300 if (0 == strcmp (pos->name, name))
309 * Remove and free an entry in the service list.
311 * @param pos entry to free
314 free_service (struct ServiceList *pos)
316 GNUNET_CONTAINER_DLL_remove (running_head, running_tail, pos);
317 GNUNET_free_non_null (pos->config);
318 GNUNET_free_non_null (pos->binary);
319 GNUNET_free (pos->name);
324 #include "do_start_process.c"
328 * Actually start the process for the given service.
330 * @param sl identifies service to start
331 * @param lsocks -1 terminated list of listen sockets to pass (systemd style), or NULL
334 start_process (struct ServiceList *sl, const int *lsocks)
347 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "PREFIX",
349 loprefix = GNUNET_strdup (prefix_command);
351 GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "OPTIONS",
354 options = GNUNET_strdup (final_option);
355 if (NULL == strstr (options, "%"))
357 /* replace '{}' with service name */
358 while (NULL != (optpos = strstr (options, "{}")))
362 GNUNET_asprintf (&optpos, options, sl->name);
363 GNUNET_free (options);
366 /* replace '$PATH' with value associated with "PATH" */
367 while (NULL != (optpos = strstr (options, "$")))
370 while (isupper ((unsigned char) *optend))
379 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", optpos + 1,
381 val = GNUNET_strdup ("");
383 GNUNET_asprintf (&optpos, "%s%s%c%s", options, val, b, next);
384 GNUNET_free (options);
390 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Starting service `%s' using binary `%s' and configuration `%s'\n",
395 sl->name, sl->binary, sl->config);
397 if (GNUNET_YES == use_debug)
399 do_start_process (lsocks, loprefix, sl->binary, "-c", sl->config, "-L",
400 "DEBUG", options, NULL);
403 do_start_process (lsocks, loprefix, sl->binary, "-c", sl->config,
405 if (sl->proc == NULL)
406 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to start service `%s'\n"),
409 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting service `%s'\n"), sl->name);
410 GNUNET_free (loprefix);
411 GNUNET_free (options);
416 * Start the specified service.
418 * @param client who is asking for this
419 * @param servicename name of the service to start
420 * @param lsocks -1 terminated list of listen sockets to pass (systemd style), or NULL
421 * @return GNUNET_OK on success, GNUNET_SYSERR on error
424 start_service (struct GNUNET_SERVER_Client *client, const char *servicename,
427 struct ServiceList *sl;
432 if (GNUNET_YES == in_shutdown)
434 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
435 _("ARM is shutting down, service `%s' not started.\n"),
437 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
438 return GNUNET_SYSERR;
440 sl = find_service (servicename);
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Service `%s' already running.\n"),
445 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_UP);
446 return GNUNET_SYSERR;
449 GNUNET_CONFIGURATION_get_value_string (cfg, servicename, "BINARY",
452 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
453 _("Binary implementing service `%s' not known!\n"),
455 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
456 return GNUNET_SYSERR;
459 GNUNET_CONFIGURATION_get_value_filename (cfg, servicename, "CONFIG",
461 (0 != STAT (config, &sbuf)))
463 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
464 _("Configuration file `%s' for service `%s' not known!\n"),
465 config, servicename);
466 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
467 GNUNET_free (binary);
468 GNUNET_free_non_null (config);
469 return GNUNET_SYSERR;
471 (void) stop_listening (servicename);
472 sl = GNUNET_malloc (sizeof (struct ServiceList));
473 sl->name = GNUNET_strdup (servicename);
476 sl->mtime = sbuf.st_mtime;
477 sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
478 sl->restartAt = GNUNET_TIME_UNIT_FOREVER_ABS;
479 GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
480 start_process (sl, lsocks);
482 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_UP);
488 * Stop the specified service.
490 * @param client who is asking for this
491 * @param servicename name of the service to stop
494 stop_service (struct GNUNET_SERVER_Client *client, const char *servicename)
496 struct ServiceList *pos;
498 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Preparing to stop `%s'\n"),
500 pos = find_service (servicename);
503 if (GNUNET_OK == stop_listening (servicename))
504 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
506 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN);
507 GNUNET_SERVER_receive_done (client, GNUNET_OK);
510 if (pos->killing_client != NULL)
512 /* killing already in progress */
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' is already down\n",
517 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
518 GNUNET_SERVER_receive_done (client, GNUNET_OK);
522 if (GNUNET_YES == in_shutdown)
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "Termination request already sent to `%s' (since ARM is in shutdown).\n",
529 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
530 GNUNET_SERVER_receive_done (client, GNUNET_OK);
533 if (pos->proc == NULL)
535 /* process is in delayed restart, simply remove it! */
537 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
538 GNUNET_SERVER_receive_done (client, GNUNET_OK);
542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543 "Sending kill signal to service `%s', waiting for process to die.\n",
546 if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
547 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
548 pos->killing_client = client;
549 GNUNET_SERVER_client_keep (client);
554 * Handle START-message.
556 * @param cls closure (always NULL)
557 * @param client identification of the client
558 * @param message the actual message
559 * @return GNUNET_OK to keep the connection open,
560 * GNUNET_SYSERR to close it (signal serious error)
563 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
564 const struct GNUNET_MessageHeader *message)
566 const char *servicename;
569 size = ntohs (message->size);
570 size -= sizeof (struct GNUNET_MessageHeader);
571 servicename = (const char *) &message[1];
572 if ((size == 0) || (servicename[size - 1] != '\0'))
575 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
578 start_service (client, servicename, NULL);
579 GNUNET_SERVER_receive_done (client, GNUNET_OK);
584 * Handle STOP-message.
586 * @param cls closure (always NULL)
587 * @param client identification of the client
588 * @param message the actual message
589 * @return GNUNET_OK to keep the connection open,
590 * GNUNET_SYSERR to close it (signal serious error)
593 handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
594 const struct GNUNET_MessageHeader *message)
596 const char *servicename;
599 size = ntohs (message->size);
600 size -= sizeof (struct GNUNET_MessageHeader);
601 servicename = (const char *) &message[1];
602 if ((size == 0) || (servicename[size - 1] != '\0'))
605 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
608 stop_service (client, servicename);
613 * Remove all entries for tasks that are not running
614 * (proc = NULL) from the running list (they will no longer
615 * be restarted since we are shutting down).
620 struct ServiceList *pos;
621 struct ServiceList *next;
624 while (NULL != (pos = next))
627 if (pos->proc == NULL)
634 * We are done with everything. Stop remaining
635 * tasks, signal handler and the server.
642 GNUNET_SERVER_destroy (server);
645 if (GNUNET_SCHEDULER_NO_TASK != child_death_task)
647 GNUNET_SCHEDULER_cancel (child_death_task);
648 child_death_task = GNUNET_SCHEDULER_NO_TASK;
654 * Task run for shutdown.
656 * @param cls closure, NULL if we need to self-restart
660 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
662 struct ServiceList *pos;
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Stopping all services\n"));
667 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
669 GNUNET_SCHEDULER_cancel (child_restart_task);
670 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
672 in_shutdown = GNUNET_YES;
673 stop_listening (NULL);
677 if (pos->proc != NULL)
679 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping service `%s'\n", pos->name);
680 if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
681 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
685 if (running_head == NULL)
691 * Task run whenever it is time to restart a child that died.
693 * @param cls closure, always NULL
697 delayed_restart_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
699 struct ServiceList *pos;
700 struct GNUNET_TIME_Relative lowestRestartDelay;
702 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
703 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
705 GNUNET_assert (GNUNET_NO == in_shutdown);
706 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
708 /* check for services that need to be restarted due to
709 * configuration changes or because the last restart failed */
713 if (pos->proc == NULL)
715 if (GNUNET_TIME_absolute_get_remaining (pos->restartAt).rel_value == 0)
717 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Restarting service `%s'.\n"),
719 start_process (pos, NULL);
724 GNUNET_TIME_relative_min (lowestRestartDelay,
725 GNUNET_TIME_absolute_get_remaining
731 if (lowestRestartDelay.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will restart process in %llums\n",
735 (unsigned long long) lowestRestartDelay.rel_value);
738 GNUNET_SCHEDULER_add_delayed (lowestRestartDelay, &delayed_restart_task,
745 * Task triggered whenever we receive a SIGCHLD (child
748 * @param cls closure, NULL if we need to self-restart
752 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
754 struct ServiceList *pos;
755 struct ServiceList *next;
760 enum GNUNET_OS_ProcessStatusType statusType;
761 unsigned long statusCode;
763 child_death_task = GNUNET_SCHEDULER_NO_TASK;
764 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
766 /* shutdown scheduled us, ignore! */
768 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, pr,
769 &maint_child_death, NULL);
772 /* consume the signal */
773 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
775 /* check for services that died (WAITPID) */
777 while (NULL != (pos = next))
780 if (pos->proc == NULL)
782 if ((GNUNET_SYSERR ==
783 (ret = GNUNET_OS_process_status (pos->proc, &statusType, &statusCode)))
784 || ((ret == GNUNET_NO) || (statusType == GNUNET_OS_PROCESS_STOPPED) ||
785 (statusType == GNUNET_OS_PROCESS_RUNNING)))
788 if (statusType == GNUNET_OS_PROCESS_EXITED)
790 statstr = _( /* process termination method */ "exit");
791 statcode = statusCode;
793 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
795 statstr = _( /* process termination method */ "signal");
796 statcode = statusCode;
800 statstr = _( /* process termination method */ "unknown");
803 GNUNET_OS_process_close (pos->proc);
805 if (NULL != pos->killing_client)
807 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Service `%s' stopped\n"),
809 signal_result (pos->killing_client, pos->name,
810 GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
811 GNUNET_SERVER_receive_done (pos->killing_client, GNUNET_OK);
812 GNUNET_SERVER_client_drop (pos->killing_client);
816 if (GNUNET_YES != in_shutdown)
818 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
819 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
821 ("Service `%s' terminated with status %s/%d, will try to restart it!\n"),
822 pos->name, statstr, statcode);
823 /* schedule restart */
824 pos->restartAt = GNUNET_TIME_relative_to_absolute (pos->backoff);
825 if (pos->backoff.rel_value < EXPONENTIAL_BACKOFF_THRESHOLD)
826 pos->backoff = GNUNET_TIME_relative_multiply (pos->backoff, 2);
827 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
828 GNUNET_SCHEDULER_cancel (child_restart_task);
830 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
831 &delayed_restart_task, NULL);
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
836 "Service `%s' terminated with status %s/%d\n", pos->name,
841 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, pr,
842 &maint_child_death, NULL);
843 if (GNUNET_YES == in_shutdown)
845 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
851 transmit_shutdown_ack (void *cls, size_t size, void *buf)
853 struct GNUNET_SERVER_Client *client = cls;
854 struct GNUNET_MessageHeader *msg;
856 if (size < sizeof (struct GNUNET_MessageHeader))
858 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
859 _("Failed to transmit shutdown ACK.\n"));
860 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
861 return 0; /* client disconnected */
864 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transmitting shutdown ACK.\n"));
866 /* Make the connection flushing for the purpose of ACK transmitting,
867 * needed on W32 to ensure that the message is even received, harmless
868 * on other platforms... */
869 GNUNET_break (GNUNET_OK == GNUNET_SERVER_client_disable_corking (client));
870 msg = (struct GNUNET_MessageHeader *) buf;
871 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK);
872 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
873 GNUNET_SERVER_receive_done (client, GNUNET_OK);
874 GNUNET_SERVER_client_drop (client);
875 return sizeof (struct GNUNET_MessageHeader);
880 * Handler for SHUTDOWN message.
882 * @param cls closure (refers to service)
883 * @param client identification of the client
884 * @param message the actual message
887 handle_shutdown (void *cls, struct GNUNET_SERVER_Client *client,
888 const struct GNUNET_MessageHeader *message)
890 GNUNET_SERVER_client_keep (client);
891 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
892 _("Initiating shutdown as requested by client.\n"));
893 GNUNET_SERVER_notify_transmit_ready (client,
894 sizeof (struct GNUNET_MessageHeader),
895 GNUNET_TIME_UNIT_FOREVER_REL,
896 &transmit_shutdown_ack, client);
897 GNUNET_SERVER_client_persist_ (client);
898 GNUNET_SCHEDULER_shutdown ();
903 * Signal handler called for SIGCHLD. Triggers the
904 * respective handler by writing to the trigger pipe.
907 sighandler_child_death ()
910 int old_errno = errno; /* back-up errno */
913 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
914 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
916 errno = old_errno; /* restore errno */
921 * Process arm requests.
924 * @param serv the initialized server
925 * @param c configuration to use
928 run (void *cls, struct GNUNET_SERVER_Handle *serv,
929 const struct GNUNET_CONFIGURATION_Handle *c)
931 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
932 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
933 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
934 {&handle_shutdown, NULL, GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN,
935 sizeof (struct GNUNET_MessageHeader)},
938 char *defaultservices;
943 GNUNET_assert (serv != NULL);
944 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
945 GNUNET_assert (pr != NULL);
946 GNUNET_SERVER_ignore_shutdown (serv, GNUNET_YES);
947 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
950 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, pr,
951 &maint_child_death, NULL);
954 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_PREFIX",
956 prefix_command = GNUNET_strdup ("");
958 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
960 final_option = GNUNET_strdup ("");
961 /* start default services... */
963 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "DEFAULTSERVICES",
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting default services `%s'\n",
970 if (0 < strlen (defaultservices))
972 pos = strtok (defaultservices, " ");
975 start_service (NULL, pos, NULL);
976 pos = strtok (NULL, " ");
979 GNUNET_free (defaultservices);
984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No default services configured.\n");
988 /* create listening sockets for future services */
989 prepareServices (cfg);
991 /* process client requests */
992 GNUNET_SERVER_add_handlers (server, handlers);
994 /* manage services */
995 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
996 &config_change_task, NULL);
1001 * The main function for the arm service.
1003 * @param argc number of arguments from the command line
1004 * @param argv command line arguments
1005 * @return 0 ok, 1 on error
1008 main (int argc, char *const *argv)
1011 struct GNUNET_SIGNAL_Context *shc_chld;
1013 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
1014 GNUNET_assert (sigpipe != NULL);
1016 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
1019 GNUNET_SERVICE_run (argc, argv, "arm", GNUNET_YES, &run, NULL)) ? 0 : 1;
1020 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1022 GNUNET_DISK_pipe_close (sigpipe);
1031 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1033 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1035 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1036 mallopt (M_TOP_PAD, 1 * 1024);
1042 /* end of gnunet-service-arm.c */