2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012, 2013 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-arm.c
23 * @brief arm for writing a tool
24 * @author Christian Grothoff
27 #include "gnunet_arm_service.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_util_lib.h"
32 * Timeout for stopping services. Long to give some services a real chance.
34 #define STOP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
37 * Timeout for stopping ARM. Extra-long since ARM needs to stop everyone else.
39 #define STOP_TIMEOUT_ARM GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
42 * Timeout for starting services, very short because of the strange way start works
43 * (by checking if running before starting, so really this time is always waited on
44 * startup (annoying)).
46 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
49 * Timeout for listing all running services.
51 #define LIST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
54 * Set if we are to shutdown all services (including ARM).
59 * Set if we are to start default services (including ARM).
64 * Set if we are to stop/start default services (including ARM).
69 * Set if we should delete configuration and temp directory on exit.
74 * Set if we should not print status messages.
79 * Monitor ARM activity.
84 * Set if we should print a list of currently running services.
89 * Set to the name of a service to start.
94 * Set to the name of a service to kill.
99 * Set to the name of the config file used.
101 static const char *config_file;
104 * Set to the directory where runtime files are stored.
114 * Connection with ARM.
116 static struct GNUNET_ARM_Handle *h;
119 * Monitor connection with ARM.
121 static struct GNUNET_ARM_MonitorHandle *m;
126 static struct GNUNET_CONFIGURATION_Handle *cfg;
129 * Processing stage that we are in. Simple counter.
131 static unsigned int phase;
134 * User defined timestamp for completing operations.
136 static struct GNUNET_TIME_Relative timeout;
139 * Do we want to give our stdout to gnunet-service-arm?
141 static unsigned int no_stdout;
144 * Do we want to give our stderr to gnunet-service-arm?
146 static unsigned int no_stderr;
150 * Attempts to delete configuration file and GNUNET_HOME
151 * on ARM shutdown provided the end and delete options
152 * were specified when gnunet-arm was run.
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Will attempt to remove configuration file %s and service directory %s\n",
161 if (0 != UNLINK (config_file))
163 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
164 _("Failed to remove configuration file %s\n"),
167 if (GNUNET_OK != GNUNET_DISK_directory_remove (dir))
169 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
170 _("Failed to remove servicehome directory %s\n"),
178 * Main continuation-passing-style loop. Runs the various
179 * jobs that we've been asked to do in order.
181 * @param cls closure, unused
182 * @param tc context, unused
185 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
189 GNUNET_ARM_disconnect_and_free (h);
194 GNUNET_ARM_monitor_disconnect_and_free (m);
197 if ((GNUNET_YES == end) && (GNUNET_YES == delete))
199 GNUNET_CONFIGURATION_destroy (cfg);
205 * Returns a string interpretation of 'rs'
207 * @param rs the request status from ARM
208 * @return a string interpretation of the request status
211 req_string (enum GNUNET_ARM_RequestStatus rs)
215 case GNUNET_ARM_REQUEST_SENT_OK:
216 return _("Message was sent successfully");
217 case GNUNET_ARM_REQUEST_CONFIGURATION_ERROR:
218 return _("Misconfiguration (can not connect to the ARM service)");
219 case GNUNET_ARM_REQUEST_DISCONNECTED:
220 return _("We disconnected from ARM before we could send a request");
221 case GNUNET_ARM_REQUEST_BUSY:
222 return _("ARM API is busy");
223 case GNUNET_ARM_REQUEST_TOO_LONG:
224 return _("Request does not fit into a message");
225 case GNUNET_ARM_REQUEST_TIMEOUT:
226 return _("Request timed out");
228 return _("Unknown request status");
233 * Returns a string interpretation of the 'result'
235 * @param result the arm result
236 * @return a string interpretation
239 ret_string (enum GNUNET_ARM_Result result)
243 case GNUNET_ARM_RESULT_STOPPED:
244 return _("%s is stopped");
245 case GNUNET_ARM_RESULT_STARTING:
246 return _("%s is starting");
247 case GNUNET_ARM_RESULT_STOPPING:
248 return _("%s is stopping");
249 case GNUNET_ARM_RESULT_IS_STARTING_ALREADY:
250 return _("%s is starting already");
251 case GNUNET_ARM_RESULT_IS_STOPPING_ALREADY:
252 return _("%s is stopping already");
253 case GNUNET_ARM_RESULT_IS_STARTED_ALREADY:
254 return _("%s is started already");
255 case GNUNET_ARM_RESULT_IS_STOPPED_ALREADY:
256 return _("%s is stopped already");
257 case GNUNET_ARM_RESULT_IS_NOT_KNOWN:
258 return _("%s service is not known to ARM");
259 case GNUNET_ARM_RESULT_START_FAILED:
260 return _("%s service failed to start");
261 case GNUNET_ARM_RESULT_IN_SHUTDOWN:
262 return _("%s service cannot be started because ARM is shutting down");
264 return _("%.s Unknown result code.");
269 * Main task that runs our various operations in order.
272 * @param tc scheudler context
275 action_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
279 * Function called whenever we connect to or disconnect from ARM.
280 * Termiantes the process if we fail to connect to the service on
284 * @param connected GNUNET_YES if connected, GNUNET_NO if disconnected,
285 * GNUNET_SYSERR on error.
288 conn_status (void *cls,
293 if ( (GNUNET_SYSERR == connected) &&
296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
297 _("Fatal error initializing ARM API.\n"));
298 GNUNET_SCHEDULER_shutdown ();
306 * We have requested ARM to be started, this function
307 * is called with the result of the operation. Informs the
308 * use of the result; on success, we continue with the event
309 * loop, on failure we terminate the process.
311 * @param cls closure unused
312 * @param rs what happened to our request
313 * @param service name of the service we tried to start ("arm")
314 * @param result if the request was processed, this is the result
318 start_callback (void *cls,
319 enum GNUNET_ARM_RequestStatus rs, const char *service,
320 enum GNUNET_ARM_Result result)
324 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
326 GNUNET_asprintf (&msg, "%s", _("Failed to start the ARM service: %s\n"));
327 FPRINTF (stdout, msg, req_string (rs));
329 GNUNET_SCHEDULER_shutdown ();
332 if ( (GNUNET_ARM_RESULT_STARTING != result) &&
333 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY != result) )
335 GNUNET_asprintf (&msg, "%s", _("Failed to start the ARM service: %s\n"));
336 FPRINTF (stdout, msg, ret_string (result));
338 GNUNET_SCHEDULER_shutdown ();
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM service [re]start successful\n");
343 GNUNET_SCHEDULER_add_now (action_loop, NULL);
348 * We have requested ARM to be stopped, this function
349 * is called with the result of the operation. Informs the
350 * use of the result; on success, we continue with the event
351 * loop, on failure we terminate the process.
353 * @param cls closure unused
354 * @param rs what happened to our request
355 * @param service name of the service we tried to start ("arm")
356 * @param result if the request was processed, this is the result
360 stop_callback (void *cls,
361 enum GNUNET_ARM_RequestStatus rs, const char *service,
362 enum GNUNET_ARM_Result result)
366 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
368 GNUNET_asprintf (&msg, "%s",
369 _("Failed to send a stop request to the ARM service: %s\n"));
370 FPRINTF (stdout, msg, req_string (rs));
372 GNUNET_SCHEDULER_shutdown ();
375 if ((GNUNET_ARM_RESULT_STOPPING != result) &&
376 (GNUNET_ARM_RESULT_STOPPED != result) &&
377 (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY != result))
379 GNUNET_asprintf (&msg, "%s", _("Failed to stop the ARM service: %s\n"));
380 FPRINTF (stdout, msg, ret_string (result));
382 GNUNET_SCHEDULER_shutdown ();
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386 "ARM service shutdown successful\n");
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393 "Initiating an ARM restart\n");
395 GNUNET_SCHEDULER_add_now (&action_loop, NULL);
400 * We have requested a service to be started, this function
401 * is called with the result of the operation. Informs the
402 * use of the result; on success, we continue with the event
403 * loop, on failure we terminate the process.
405 * @param cls closure unused
406 * @param rs what happened to our request
407 * @param service name of the service we tried to start
408 * @param result if the request was processed, this is the result
412 init_callback (void *cls,
413 enum GNUNET_ARM_RequestStatus rs, const char *service,
414 enum GNUNET_ARM_Result result)
418 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
420 GNUNET_asprintf (&msg,
421 _("Failed to send a request to start the `%s' service: %%s\n"),
423 FPRINTF (stdout, msg, req_string (rs));
425 GNUNET_SCHEDULER_shutdown ();
428 if ((GNUNET_ARM_RESULT_STARTING != result) &&
429 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY != result))
431 GNUNET_asprintf (&msg, _("Failed to start the `%s' service: %s\n"),
432 init, ret_string (result));
433 FPRINTF (stdout, msg, service);
435 GNUNET_SCHEDULER_shutdown ();
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "Service %s [re]started successfully\n",
443 GNUNET_SCHEDULER_add_now (&action_loop, NULL);
448 * We have requested a service to be stopped, this function
449 * is called with the result of the operation. Informs the
450 * use of the result; on success, we continue with the event
451 * loop, on failure we terminate the process.
453 * @param cls closure unused
454 * @param rs what happened to our request
455 * @param service name of the service we tried to start
456 * @param result if the request was processed, this is the result
460 term_callback (void *cls,
461 enum GNUNET_ARM_RequestStatus rs, const char *service,
462 enum GNUNET_ARM_Result result)
465 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
467 GNUNET_asprintf (&msg,
468 _("Failed to send a request to kill the `%s' service: %%s\n"),
470 FPRINTF (stdout, msg, req_string (rs));
472 GNUNET_SCHEDULER_shutdown ();
475 if ((GNUNET_ARM_RESULT_STOPPED != result) &&
476 (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY != result))
478 GNUNET_asprintf (&msg,
479 _("Failed to kill the `%s' service: %s\n"),
480 term, ret_string (result));
481 FPRINTF (stdout, msg, service);
483 GNUNET_SCHEDULER_shutdown ();
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488 "Service %s stopped successfully\n",
492 GNUNET_SCHEDULER_add_now (&action_loop, NULL);
497 * Function called with the list of running services. Prints
498 * the list to stdout, then starts the event loop again.
499 * Prints an error message and terminates the process on errors.
501 * @param cls closure (unused)
502 * @param rs request status (success, failure, etc.)
503 * @param count number of services in the list
504 * @param list list of services that are running
507 list_callback (void *cls,
508 enum GNUNET_ARM_RequestStatus rs, unsigned int count,
509 const char *const*list)
513 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
517 GNUNET_asprintf (&msg, "%s", _("Failed to request a list of services: %s\n"));
518 FPRINTF (stdout, msg, req_string (rs));
521 GNUNET_SCHEDULER_shutdown ();
525 FPRINTF (stderr, "%s", _("Error communicating with ARM. ARM not running?\n"));
526 GNUNET_SCHEDULER_shutdown ();
530 FPRINTF (stdout, "%s", _("Running services:\n"));
531 for (i = 0; i < count; i++)
532 FPRINTF (stdout, "%s\n", list[i]);
533 GNUNET_SCHEDULER_add_now (&action_loop, NULL);
538 * Main action loop. Runs the various jobs that we've been asked to
541 * @param cls closure, unused
542 * @param tc context, unused
545 action_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
547 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running requested actions\n");
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Termination action\n");
558 GNUNET_ARM_request_service_stop (h, term,
559 (0 == timeout.rel_value_us) ? STOP_TIMEOUT : timeout,
560 &term_callback, NULL);
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "End action\n");
568 GNUNET_ARM_request_service_stop (h, "arm",
569 (0 == timeout.rel_value_us) ? STOP_TIMEOUT_ARM : timeout,
570 &stop_callback, NULL);
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start action\n");
578 GNUNET_ARM_request_service_start (h, "arm",
579 (no_stdout ? 0 : GNUNET_OS_INHERIT_STD_OUT) |
580 (no_stderr ? 0 : GNUNET_OS_INHERIT_STD_ERR),
581 (0 == timeout.rel_value_us) ? START_TIMEOUT: timeout,
582 start_callback, NULL);
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initialization action\n");
590 GNUNET_ARM_request_service_start (h, init, GNUNET_OS_INHERIT_STD_NONE,
591 (0 == timeout.rel_value_us) ? STOP_TIMEOUT : timeout,
592 &init_callback, NULL);
599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
600 "Going to list all running services controlled by ARM.\n");
601 GNUNET_ARM_request_service_list (h,
602 (0 == timeout.rel_value_us) ? LIST_TIMEOUT : timeout,
603 &list_callback, &list);
612 _("Now only monitoring, press CTRL-C to stop.\n"));
613 quiet = 0; /* does not make sense to stay quiet in monitor mode at this time */
614 return; /* done with tasks, just monitor */
617 default: /* last phase */
618 GNUNET_SCHEDULER_shutdown ();
626 * Function called when a service starts or stops.
629 * @param service service name
630 * @param status status of the service
633 srv_status (void *cls,
634 const char *service, enum GNUNET_ARM_ServiceStatus status)
640 case GNUNET_ARM_SERVICE_MONITORING_STARTED:
641 return; /* this should be done silently */
642 case GNUNET_ARM_SERVICE_STOPPED:
643 msg = _("Stopped %s.\n");
645 case GNUNET_ARM_SERVICE_STARTING:
646 msg = _("Starting %s...\n");
648 case GNUNET_ARM_SERVICE_STOPPING:
649 msg = _("Stopping %s...\n");
658 FPRINTF (stderr, msg, service);
660 FPRINTF (stderr, _("Unknown status %u for service %s.\n"), status, service);
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got service %s status %d\n", service, (int) status);
667 * Main function that will be run by the scheduler.
670 * @param args remaining command-line arguments
671 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
672 * @param c configuration
678 const struct GNUNET_CONFIGURATION_Handle *c)
682 cfg = GNUNET_CONFIGURATION_dup (c);
683 config_file = cfgfile;
685 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "GNUNET_HOME", &dir))
687 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
688 "PATHS", "GNUNET_HOME");
694 GNUNET_CONFIGURATION_get_value_filename (cfg, "arm", "CONFIG",
697 GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG",
701 GNUNET_free (armconfig);
703 if (NULL == (h = GNUNET_ARM_connect (cfg, &conn_status, NULL)))
706 m = GNUNET_ARM_monitor (cfg, &srv_status, NULL);
707 GNUNET_SCHEDULER_add_now (&action_loop, NULL);
708 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
709 &shutdown_task, NULL);
714 * The main function to obtain arm from gnunetd.
716 * @param argc number of arguments from the command line
717 * @param argv command line arguments
718 * @return 0 ok, 1 on error
721 main (int argc, char *const *argv)
723 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
724 {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
725 GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
726 {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
727 GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
728 {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
729 GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
730 {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
731 GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
732 {'r', "restart", NULL,
733 gettext_noop ("stop and start all GNUnet default services"),
734 GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
735 {'d', "delete", NULL,
736 gettext_noop ("delete config file and directory on exit"),
737 GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
738 {'m', "monitor", NULL,
739 gettext_noop ("monitor ARM activities"),
740 GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor},
741 {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
742 GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
743 {'T', "timeout", "MSECS",
744 gettext_noop ("timeout in MSECS milliseconds for completing current operation"),
745 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &timeout},
746 {'I', "info", NULL, gettext_noop ("list currently running services"),
747 GNUNET_NO, &GNUNET_GETOPT_set_one, &list},
748 {'O', "no-stdout", NULL, gettext_noop ("don't let gnunet-service-arm inherit standard output"),
749 GNUNET_NO, &GNUNET_GETOPT_set_one, &no_stdout},
750 {'E', "no-stderr", NULL, gettext_noop ("don't let gnunet-service-arm inherit standard error"),
751 GNUNET_NO, &GNUNET_GETOPT_set_one, &no_stderr},
752 GNUNET_GETOPT_OPTION_END
755 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
759 GNUNET_PROGRAM_run (argc, argv, "gnunet-arm",
761 ("Control services and the Automated Restart Manager (ARM)"),
762 options, &run, NULL))
764 GNUNET_free ((void *) argv);
767 GNUNET_free ((void*) argv);
771 /* end of gnunet-arm.c */