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;
663 struct ServiceList *nxt;
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Stopping all services\n"));
668 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
670 GNUNET_SCHEDULER_cancel (child_restart_task);
671 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
673 in_shutdown = GNUNET_YES;
674 stop_listening (NULL);
679 if (pos->proc != NULL)
681 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping service `%s'\n", pos->name);
682 if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
683 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
691 if (running_head == NULL)
697 * Task run whenever it is time to restart a child that died.
699 * @param cls closure, always NULL
703 delayed_restart_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
705 struct ServiceList *pos;
706 struct GNUNET_TIME_Relative lowestRestartDelay;
708 child_restart_task = GNUNET_SCHEDULER_NO_TASK;
709 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
711 GNUNET_assert (GNUNET_NO == in_shutdown);
712 lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
714 /* check for services that need to be restarted due to
715 * configuration changes or because the last restart failed */
719 if (pos->proc == NULL)
721 if (GNUNET_TIME_absolute_get_remaining (pos->restartAt).rel_value == 0)
723 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Restarting service `%s'.\n"),
725 start_process (pos, NULL);
730 GNUNET_TIME_relative_min (lowestRestartDelay,
731 GNUNET_TIME_absolute_get_remaining
737 if (lowestRestartDelay.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will restart process in %llums\n",
741 (unsigned long long) lowestRestartDelay.rel_value);
744 GNUNET_SCHEDULER_add_delayed (lowestRestartDelay, &delayed_restart_task,
751 * Task triggered whenever we receive a SIGCHLD (child
754 * @param cls closure, NULL if we need to self-restart
758 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
760 struct ServiceList *pos;
761 struct ServiceList *next;
766 enum GNUNET_OS_ProcessStatusType statusType;
767 unsigned long statusCode;
769 child_death_task = GNUNET_SCHEDULER_NO_TASK;
770 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
772 /* shutdown scheduled us, ignore! */
774 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, pr,
775 &maint_child_death, NULL);
778 /* consume the signal */
779 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
781 /* check for services that died (WAITPID) */
783 while (NULL != (pos = next))
786 if (pos->proc == NULL)
788 if ((GNUNET_SYSERR ==
789 (ret = GNUNET_OS_process_status (pos->proc, &statusType, &statusCode)))
790 || ((ret == GNUNET_NO) || (statusType == GNUNET_OS_PROCESS_STOPPED) ||
791 (statusType == GNUNET_OS_PROCESS_RUNNING)))
794 if (statusType == GNUNET_OS_PROCESS_EXITED)
796 statstr = _( /* process termination method */ "exit");
797 statcode = statusCode;
799 else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
801 statstr = _( /* process termination method */ "signal");
802 statcode = statusCode;
806 statstr = _( /* process termination method */ "unknown");
809 GNUNET_OS_process_close (pos->proc);
811 if (NULL != pos->killing_client)
813 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Service `%s' stopped\n"),
815 signal_result (pos->killing_client, pos->name,
816 GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
817 GNUNET_SERVER_receive_done (pos->killing_client, GNUNET_OK);
818 GNUNET_SERVER_client_drop (pos->killing_client);
822 if (GNUNET_YES != in_shutdown)
824 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
825 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
827 ("Service `%s' terminated with status %s/%d, will try to restart it!\n"),
828 pos->name, statstr, statcode);
829 /* schedule restart */
830 pos->restartAt = GNUNET_TIME_relative_to_absolute (pos->backoff);
831 if (pos->backoff.rel_value < EXPONENTIAL_BACKOFF_THRESHOLD)
832 pos->backoff = GNUNET_TIME_relative_multiply (pos->backoff, 2);
833 if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
834 GNUNET_SCHEDULER_cancel (child_restart_task);
836 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
837 &delayed_restart_task, NULL);
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 "Service `%s' terminated with status %s/%d\n", pos->name,
847 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, pr,
848 &maint_child_death, NULL);
849 if (GNUNET_YES == in_shutdown)
851 if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
857 transmit_shutdown_ack (void *cls, size_t size, void *buf)
859 struct GNUNET_SERVER_Client *client = cls;
860 struct GNUNET_MessageHeader *msg;
862 if (size < sizeof (struct GNUNET_MessageHeader))
864 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
865 _("Failed to transmit shutdown ACK.\n"));
866 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
867 return 0; /* client disconnected */
870 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transmitting shutdown ACK.\n"));
872 /* Make the connection flushing for the purpose of ACK transmitting,
873 * needed on W32 to ensure that the message is even received, harmless
874 * on other platforms... */
875 GNUNET_break (GNUNET_OK == GNUNET_SERVER_client_disable_corking (client));
876 msg = (struct GNUNET_MessageHeader *) buf;
877 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK);
878 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
879 GNUNET_SERVER_receive_done (client, GNUNET_OK);
880 GNUNET_SERVER_client_drop (client);
881 return sizeof (struct GNUNET_MessageHeader);
886 * Handler for SHUTDOWN message.
888 * @param cls closure (refers to service)
889 * @param client identification of the client
890 * @param message the actual message
893 handle_shutdown (void *cls, struct GNUNET_SERVER_Client *client,
894 const struct GNUNET_MessageHeader *message)
896 GNUNET_SERVER_client_keep (client);
897 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
898 _("Initiating shutdown as requested by client.\n"));
899 GNUNET_SERVER_notify_transmit_ready (client,
900 sizeof (struct GNUNET_MessageHeader),
901 GNUNET_TIME_UNIT_FOREVER_REL,
902 &transmit_shutdown_ack, client);
903 GNUNET_SERVER_client_persist_ (client);
904 GNUNET_SCHEDULER_shutdown ();
909 * Signal handler called for SIGCHLD. Triggers the
910 * respective handler by writing to the trigger pipe.
913 sighandler_child_death ()
916 int old_errno = errno; /* back-up errno */
919 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
920 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
922 errno = old_errno; /* restore errno */
927 * Process arm requests.
930 * @param serv the initialized server
931 * @param c configuration to use
934 run (void *cls, struct GNUNET_SERVER_Handle *serv,
935 const struct GNUNET_CONFIGURATION_Handle *c)
937 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
938 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
939 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
940 {&handle_shutdown, NULL, GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN,
941 sizeof (struct GNUNET_MessageHeader)},
944 char *defaultservices;
949 GNUNET_assert (serv != NULL);
950 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
951 GNUNET_assert (pr != NULL);
952 GNUNET_SERVER_ignore_shutdown (serv, GNUNET_YES);
953 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
956 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, pr,
957 &maint_child_death, NULL);
960 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_PREFIX",
962 prefix_command = GNUNET_strdup ("");
964 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
966 final_option = GNUNET_strdup ("");
967 /* start default services... */
969 GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "DEFAULTSERVICES",
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting default services `%s'\n",
976 if (0 < strlen (defaultservices))
978 pos = strtok (defaultservices, " ");
981 start_service (NULL, pos, NULL);
982 pos = strtok (NULL, " ");
985 GNUNET_free (defaultservices);
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No default services configured.\n");
994 /* create listening sockets for future services */
995 prepareServices (cfg);
997 /* process client requests */
998 GNUNET_SERVER_add_handlers (server, handlers);
1000 /* manage services */
1001 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1002 &config_change_task, NULL);
1007 * The main function for the arm service.
1009 * @param argc number of arguments from the command line
1010 * @param argv command line arguments
1011 * @return 0 ok, 1 on error
1014 main (int argc, char *const *argv)
1017 struct GNUNET_SIGNAL_Context *shc_chld;
1019 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
1020 GNUNET_assert (sigpipe != NULL);
1022 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
1025 GNUNET_SERVICE_run (argc, argv, "arm", GNUNET_YES, &run, NULL)) ? 0 : 1;
1026 GNUNET_SIGNAL_handler_uninstall (shc_chld);
1028 GNUNET_DISK_pipe_close (sigpipe);
1037 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1039 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1041 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1042 mallopt (M_TOP_PAD, 1 * 1024);
1048 /* end of gnunet-service-arm.c */