2 This file is part of GNUnet.
3 (C) 2009 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 2, 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 * - multiple start-stop requests with RC>1 can result
28 * in UP/DOWN signals based on "pending" that are inaccurate...
29 * => have list of clients waiting for a resolution instead of
30 * giving instant (but incorrect) replies
31 * - code could go into restart-loop for a service
32 * if service crashes instantly -- need exponential back-off
33 * - need to test auto-restart code on configuration changes;
34 * - should refine restart code to check if *relevant* parts of the
35 * configuration were changed (anything in the section for the service)
36 * - should have a way to specify dependencies between services and
37 * manage restarts of groups of services
40 #include "gnunet_client_lib.h"
41 #include "gnunet_getopt_lib.h"
42 #include "gnunet_os_lib.h"
43 #include "gnunet_protocols.h"
44 #include "gnunet_service_lib.h"
49 * Run maintenance every second.
51 #define MAINT_FREQUENCY GNUNET_TIME_UNIT_SECONDS
54 * How long do we wait until we decide that a service
57 #define CHECK_TIMEOUT GNUNET_TIME_UNIT_MINUTES
61 typedef void (*CleanCallback) (void *cls, struct ServiceList * pos);
64 * List of our services.
69 * This is a linked list.
71 struct ServiceList *next;
74 * Name of the service.
79 * Name of the binary used.
84 * Name of the configuration file used.
89 * Function to call upon kill completion (waitpid), NULL
90 * if we should simply restart the process.
92 CleanCallback kill_continuation;
95 * Closure for kill_continuation.
97 void *kill_continuation_cls;
100 * Process ID of the child.
105 * Last time the config of this service was
111 * Reference counter (counts how many times we've been
112 * asked to start the service). We only actually stop
113 * it once rc hits zero.
120 * List of running services.
122 static struct ServiceList *running;
127 static struct GNUNET_CONFIGURATION_Handle *cfg;
132 static struct GNUNET_SCHEDULER_Handle *sched;
135 * Command to prepend to each actual command.
137 static char *prefix_command;
141 write_result (void *cls, size_t size, void *buf)
144 struct GNUNET_MessageHeader *msg;
147 return 0; /* error, not much we can do */
148 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
150 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
151 msg->type = htons (*res);
153 return sizeof (struct GNUNET_MessageHeader);
159 * Signal our client that we will start or stop the
162 * @return NULL if it was not found
165 signal_result (struct GNUNET_SERVER_Client *client,
166 const char *name, uint16_t result)
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172 "Telling client that service `%s' is now %s\n",
174 result == GNUNET_MESSAGE_TYPE_ARM_IS_DOWN ? "down" : "up");
176 res = GNUNET_malloc (sizeof (uint16_t));
178 GNUNET_SERVER_notify_transmit_ready (client,
179 sizeof (struct GNUNET_MessageHeader),
180 GNUNET_TIME_UNIT_FOREVER_REL,
186 * Find the process with the given PID in the
189 * @return NULL if it was not found
191 static struct ServiceList *
194 struct ServiceList *pos;
208 * Find the process with the given service
209 * name in the given list, remove it and return it.
211 * @return NULL if it was not found
213 static struct ServiceList *
214 find_name (const char *name)
216 struct ServiceList *pos;
217 struct ServiceList *prev;
223 if (0 == strcmp (pos->name, name))
228 prev->next = pos->next;
240 free_entry (struct ServiceList *pos)
242 GNUNET_free_non_null (pos->config);
243 GNUNET_free_non_null (pos->binary);
244 GNUNET_free (pos->name);
252 * Actually start the process for the given service.
254 * @param sl identifies service to start
257 start_process (struct ServiceList *sl)
261 unsigned int argv_size;
263 const char *firstarg;
268 GNUNET_CONFIGURATION_get_value_string (cfg,
269 sl->name, "PREFIX", &loprefix))
270 loprefix = GNUNET_strdup (prefix_command);
271 use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
273 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting service `%s'\n"), sl->name);
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276 "Starting service `%s' using binary `%s' and configuration `%s'\n",
277 sl->name, sl->binary, sl->config);
283 while ('\0' != *lopos)
290 argv = GNUNET_malloc (argv_size * sizeof (char *));
293 while ('\0' != *lopos)
295 while (*lopos == ' ')
301 argv[argv_size++] = lopos;
302 while (('\0' != *lopos) && (' ' != *lopos))
310 firstarg = sl->binary;
311 argv[argv_size++] = sl->binary;
312 argv[argv_size++] = "-c";
313 argv[argv_size++] = sl->config;
314 if (GNUNET_YES == use_debug)
316 argv[argv_size++] = "-L";
317 argv[argv_size++] = "DEBUG";
319 argv[argv_size++] = NULL;
320 sl->pid = GNUNET_OS_start_process_v (firstarg, argv);
322 GNUNET_free (loprefix);
327 * Start the specified service.
330 start_service (struct GNUNET_SERVER_Handle *server,
331 struct GNUNET_SERVER_Client *client, const char *servicename)
333 struct ServiceList *sl;
337 sl = find_name (servicename);
340 /* already running, just increment RC */
341 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
342 _("Service `%s' already running.\n"), servicename);
346 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_UP);
350 GNUNET_CONFIGURATION_get_value_string (cfg,
351 servicename, "BINARY", &binary))
353 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
354 _("Binary implementing service `%s' not known!\n"),
356 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
360 GNUNET_CONFIGURATION_get_value_filename (cfg,
364 (0 != STAT (config, &sbuf)))
366 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
367 _("Configuration file `%s' for service `%s' not known!\n"),
368 config, servicename);
369 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
370 GNUNET_free (binary);
373 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
374 _("Preparing to start `%s'\n"), servicename);
375 sl = GNUNET_malloc (sizeof (struct ServiceList));
376 sl->name = GNUNET_strdup (servicename);
381 sl->mtime = sbuf.st_mtime;
384 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_UP);
389 free_and_signal (void *cls, struct ServiceList *pos)
391 struct GNUNET_SERVER_Client *client = cls;
392 /* find_name will remove "pos" from the list! */
393 GNUNET_assert (pos == find_name (pos->name));
394 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Service `%s' stopped\n", pos->name);
395 signal_result (client, pos->name, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
396 GNUNET_SERVER_receive_done (client, GNUNET_OK);
397 GNUNET_SERVER_client_drop (client);
403 * Stop the specified service.
406 stop_service (struct GNUNET_SERVER_Handle *server,
407 struct GNUNET_SERVER_Client *client, const char *servicename)
409 struct ServiceList *pos;
410 struct GNUNET_CLIENT_Connection *sc;
412 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
413 "Preparing to stop `%s'\n", servicename);
414 pos = find_name (servicename);
415 if (pos->kill_continuation != NULL)
417 /* killing already in progress */
418 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
421 if ((pos != NULL) && (pos->rc > 1))
423 /* RC>1, just decrement RC */
427 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_UP);
428 GNUNET_SERVER_receive_done (client, GNUNET_OK);
433 if (0 != PLIBC_KILL (pos->pid, SIGTERM))
434 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
437 pos->kill_continuation = &free_and_signal;
438 pos->kill_continuation_cls = client;
439 GNUNET_SERVER_client_keep (client);
443 sc = GNUNET_CLIENT_connect (sched, servicename, cfg);
444 GNUNET_CLIENT_service_shutdown (sc);
445 GNUNET_CLIENT_disconnect (sc);
446 signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN);
447 GNUNET_SERVER_receive_done (client, GNUNET_OK);
453 * Handle START-message.
455 * @param cls closure (always NULL)
456 * @param server the server handling the message
457 * @param client identification of the client
458 * @param message the actual message
459 * @return GNUNET_OK to keep the connection open,
460 * GNUNET_SYSERR to close it (signal serious error)
463 handle_start (void *cls,
464 struct GNUNET_SERVER_Handle *server,
465 struct GNUNET_SERVER_Client *client,
466 const struct GNUNET_MessageHeader *message)
468 const char *servicename;
471 size = ntohs (message->size);
472 size -= sizeof (struct GNUNET_MessageHeader);
473 servicename = (const char *) &message[1];
474 if ((size == 0) || (servicename[size - 1] != '\0'))
477 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
480 start_service (server, client, servicename);
481 GNUNET_SERVER_receive_done (client, GNUNET_OK);
486 * Handle STOP-message.
488 * @param cls closure (always NULL)
489 * @param server the server handling the message
490 * @param client identification of the client
491 * @param message the actual message
492 * @return GNUNET_OK to keep the connection open,
493 * GNUNET_SYSERR to close it (signal serious error)
496 handle_stop (void *cls,
497 struct GNUNET_SERVER_Handle *server,
498 struct GNUNET_SERVER_Client *client,
499 const struct GNUNET_MessageHeader *message)
501 const char *servicename;
504 size = ntohs (message->size);
505 size -= sizeof (struct GNUNET_MessageHeader);
506 servicename = (const char *) &message[1];
507 if ((size == 0) || (servicename[size - 1] != '\0'))
510 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
513 stop_service (server, client, servicename);
519 * Background task doing maintenance.
525 maint (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
527 struct ServiceList *pos;
534 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
536 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Stopping all services\n"));
537 while (NULL != (pos = running))
540 if (0 != PLIBC_KILL (pos->pid, SIGTERM))
541 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
542 if (pos->pid != waitpid (pos->pid, NULL, 0))
543 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
548 GNUNET_SCHEDULER_add_delayed (tc->sched,
550 GNUNET_SCHEDULER_PRIORITY_IDLE,
551 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
552 MAINT_FREQUENCY, &maint, cfg);
554 /* check for services that died (WAITPID) */
555 while (0 < (pid = waitpid (0, &status, WNOHANG)))
557 if (WIFSTOPPED (status) || WIFCONTINUED (status))
559 pos = find_pid (pid);
562 /* we killed the service */
565 if (WIFEXITED (status))
567 statstr = _( /* process termination method */ "exit");
568 statcode = WEXITSTATUS (status);
570 else if (WTERMSIG (status))
572 statstr = _( /* process termination method */ "signal");
573 statcode = WTERMSIG (status);
577 statstr = _( /* process termination method */ "unknown");
580 if (NULL != pos->kill_continuation)
582 pos->kill_continuation (pos->kill_continuation_cls, pos);
586 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
588 ("Service `%s' terminated with status %s/%d, will try to restart it!\n"),
589 pos->name, statstr, statcode);
590 /* schedule restart */
595 /* check for services that need to be restarted due to
596 configuration changes or because the last restart failed */
600 if ((0 == STAT (pos->config, &sbuf)) && (pos->mtime < sbuf.st_mtime))
602 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
604 ("Restarting service `%s' due to configuration file change.\n"));
605 if (0 != PLIBC_KILL (pos->pid, SIGTERM))
606 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
610 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
611 _("Restarting service `%s'.\n"), pos->name);
612 /* FIXME: should have some exponentially
613 increasing timer to avoid tight restart loops */
622 * List of handlers for the messages understood by this
625 static struct GNUNET_SERVER_MessageHandler handlers[] = {
626 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
627 {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
633 * Process arm requests.
636 * @param s scheduler to use
637 * @param server the initialized server
638 * @param c configuration to use
642 struct GNUNET_SCHEDULER_Handle *s,
643 struct GNUNET_SERVER_Handle *server,
644 struct GNUNET_CONFIGURATION_Handle *c)
646 char *defaultservices;
649 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting...\n");
653 GNUNET_CONFIGURATION_get_value_string (cfg,
657 prefix_command = GNUNET_strdup ("");
658 /* start default services... */
660 GNUNET_CONFIGURATION_get_value_string (cfg,
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "Starting default services `%s'\n", defaultservices);
669 pos = strtok (defaultservices, " ");
672 start_service (server, NULL, pos);
673 pos = strtok (NULL, " ");
675 GNUNET_free (defaultservices);
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
681 "No default services configured.\n");
685 /* process client requests */
686 GNUNET_SERVER_add_handlers (server, handlers);
688 /* manage services */
689 GNUNET_SCHEDULER_add_delayed (sched,
691 GNUNET_SCHEDULER_PRIORITY_IDLE,
692 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
693 MAINT_FREQUENCY, &maint, NULL);
698 * The main function for the arm service.
700 * @param argc number of arguments from the command line
701 * @param argv command line arguments
702 * @return 0 ok, 1 on error
705 main (int argc, char *const *argv)
708 GNUNET_SERVICE_run (argc,
709 argv, "arm", &run, NULL, NULL, NULL)) ? 0 : 1;
712 /* end of gnunet-service-arm.c */