2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012, 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 * Set if we are to shutdown all services (including ARM).
37 * Set if we are to start default services (including ARM).
42 * Set if we are to stop/start default services (including ARM).
47 * Set if we should delete configuration and temp directory on exit.
52 * Set if we should not print status messages.
57 * Monitor ARM activity.
62 * Set if we should print a list of currently running services.
67 * Set to the name of a service to start.
72 * Set to the name of a service to kill.
77 * Set to the name of the config file used.
79 static const char *config_file;
82 * Set to the directory where runtime files are stored.
92 * Connection with ARM.
94 static struct GNUNET_ARM_Handle *h;
97 * Monitor connection with ARM.
99 static struct GNUNET_ARM_MonitorHandle *m;
104 static struct GNUNET_CONFIGURATION_Handle *cfg;
107 * Processing stage that we are in. Simple counter.
109 static unsigned int phase;
112 * User defined timestamp for completing operations.
114 static struct GNUNET_TIME_Relative timeout;
117 * Task to be run on timeout.
119 static struct GNUNET_SCHEDULER_Task *timeout_task;
122 * Do we want to give our stdout to gnunet-service-arm?
124 static unsigned int no_stdout;
127 * Do we want to give our stderr to gnunet-service-arm?
129 static unsigned int no_stderr;
132 * Handle for the task running the #action_loop().
134 static struct GNUNET_SCHEDULER_Task *al_task;
139 static struct GNUNET_ARM_Operation *op;
142 * Attempts to delete configuration file and GNUNET_HOME
143 * on ARM shutdown provided the end and delete options
144 * were specified when gnunet-arm was run.
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150 "Will attempt to remove configuration file %s and service directory %s\n",
153 if (0 != UNLINK (config_file))
155 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
156 _("Failed to remove configuration file %s\n"),
159 if (GNUNET_OK != GNUNET_DISK_directory_remove (dir))
161 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
162 _("Failed to remove servicehome directory %s\n"),
170 * Main continuation-passing-style loop. Runs the various
171 * jobs that we've been asked to do in order.
173 * @param cls closure, unused
176 shutdown_task (void *cls)
180 GNUNET_SCHEDULER_cancel (al_task);
185 GNUNET_ARM_operation_cancel (op);
190 GNUNET_ARM_disconnect (h);
195 GNUNET_ARM_monitor_stop (m);
198 if (NULL != timeout_task)
200 GNUNET_SCHEDULER_cancel (timeout_task);
203 if ((GNUNET_YES == end) && (GNUNET_YES == delete))
205 GNUNET_CONFIGURATION_destroy (cfg);
211 * Returns a string interpretation of 'rs'
213 * @param rs the request status from ARM
214 * @return a string interpretation of the request status
217 req_string (enum GNUNET_ARM_RequestStatus rs)
221 case GNUNET_ARM_REQUEST_SENT_OK:
222 return _("Message was sent successfully");
223 case GNUNET_ARM_REQUEST_CONFIGURATION_ERROR:
224 return _("Misconfiguration (can not connect to the ARM service)");
225 case GNUNET_ARM_REQUEST_DISCONNECTED:
226 return _("We disconnected from ARM before we could send a request");
227 case GNUNET_ARM_REQUEST_BUSY:
228 return _("ARM API is busy");
229 case GNUNET_ARM_REQUEST_TIMEOUT:
230 return _("Request timed out");
232 return _("Unknown request status");
237 * Returns a string interpretation of the 'result'
239 * @param result the arm result
240 * @return a string interpretation
243 ret_string (enum GNUNET_ARM_Result result)
247 case GNUNET_ARM_RESULT_STOPPED:
248 return _("%s is stopped");
249 case GNUNET_ARM_RESULT_STARTING:
250 return _("%s is starting");
251 case GNUNET_ARM_RESULT_STOPPING:
252 return _("%s is stopping");
253 case GNUNET_ARM_RESULT_IS_STARTING_ALREADY:
254 return _("%s is starting already");
255 case GNUNET_ARM_RESULT_IS_STOPPING_ALREADY:
256 return _("%s is stopping already");
257 case GNUNET_ARM_RESULT_IS_STARTED_ALREADY:
258 return _("%s is started already");
259 case GNUNET_ARM_RESULT_IS_STOPPED_ALREADY:
260 return _("%s is stopped already");
261 case GNUNET_ARM_RESULT_IS_NOT_KNOWN:
262 return _("%s service is not known to ARM");
263 case GNUNET_ARM_RESULT_START_FAILED:
264 return _("%s service failed to start");
265 case GNUNET_ARM_RESULT_IN_SHUTDOWN:
266 return _("%s service cannot be started because ARM is shutting down");
268 return _("%.s Unknown result code.");
273 * Main task that runs our various operations in order.
278 action_loop (void *cls);
282 * Function called whenever we connect to or disconnect from ARM.
283 * Termiantes the process if we fail to connect to the service on
287 * @param connected #GNUNET_YES if connected, #GNUNET_NO if disconnected,
288 * #GNUNET_SYSERR on error.
291 conn_status (void *cls,
296 if ( (GNUNET_SYSERR == connected) &&
299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
300 _("Fatal error initializing ARM API.\n"));
301 GNUNET_SCHEDULER_shutdown ();
309 * We have requested ARM to be started, this function
310 * is called with the result of the operation. Informs the
311 * use of the result; on success, we continue with the event
312 * loop, on failure we terminate the process.
314 * @param cls closure unused
315 * @param rs what happened to our request
316 * @param result if the request was processed, this is the result
320 start_callback (void *cls,
321 enum GNUNET_ARM_RequestStatus rs,
322 enum GNUNET_ARM_Result result)
327 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
329 GNUNET_asprintf (&msg, "%s", _("Failed to start the ARM service: %s\n"));
330 FPRINTF (stdout, msg, req_string (rs));
332 GNUNET_SCHEDULER_shutdown ();
335 if ( (GNUNET_ARM_RESULT_STARTING != result) &&
336 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY != result) )
338 GNUNET_asprintf (&msg, "%s", _("Failed to start the ARM service: %s\n"));
339 FPRINTF (stdout, msg, ret_string (result));
341 GNUNET_SCHEDULER_shutdown ();
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "ARM service [re]start successful\n");
347 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
352 * We have requested ARM to be stopped, this function
353 * is called with the result of the operation. Informs the
354 * use of the result; on success, we continue with the event
355 * loop, on failure we terminate the process.
357 * @param cls closure unused
358 * @param rs what happened to our request
359 * @param result if the request was processed, this is the result
363 stop_callback (void *cls,
364 enum GNUNET_ARM_RequestStatus rs,
365 enum GNUNET_ARM_Result result)
370 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
372 GNUNET_asprintf (&msg, "%s",
373 _("Failed to send a stop request to the ARM service: %s\n"));
374 FPRINTF (stdout, msg, req_string (rs));
376 GNUNET_SCHEDULER_shutdown ();
379 if ((GNUNET_ARM_RESULT_STOPPING != result) &&
380 (GNUNET_ARM_RESULT_STOPPED != result) &&
381 (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY != result))
383 GNUNET_asprintf (&msg, "%s",
384 _("Failed to stop the ARM service: %s\n"));
385 FPRINTF (stdout, msg, ret_string (result));
387 GNUNET_SCHEDULER_shutdown ();
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "ARM service shutdown successful\n");
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "Initiating an ARM restart\n");
400 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
405 * We have requested a service to be started, this function
406 * is called with the result of the operation. Informs the
407 * use of the result; on success, we continue with the event
408 * loop, on failure we terminate the process.
410 * @param cls closure unused
411 * @param rs what happened to our request
412 * @param result if the request was processed, this is the result
416 init_callback (void *cls,
417 enum GNUNET_ARM_RequestStatus rs,
418 enum GNUNET_ARM_Result result)
423 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
425 GNUNET_asprintf (&msg,
426 _("Failed to send a request to start the `%s' service: %%s\n"),
428 FPRINTF (stdout, msg, req_string (rs));
430 GNUNET_SCHEDULER_shutdown ();
433 if ((GNUNET_ARM_RESULT_STARTING != result) &&
434 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY != result))
436 GNUNET_asprintf (&msg,
437 _("Failed to start the `%s' service: %s\n"),
439 ret_string (result));
440 FPRINTF (stdout, "%s", msg);
442 GNUNET_SCHEDULER_shutdown ();
445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
446 "Service %s [re]started successfully\n",
450 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
455 * We have requested a service to be stopped, this function
456 * is called with the result of the operation. Informs the
457 * use of the result; on success, we continue with the event
458 * loop, on failure we terminate the process.
460 * @param cls closure unused
461 * @param rs what happened to our request
462 * @param result if the request was processed, this is the result
466 term_callback (void *cls,
467 enum GNUNET_ARM_RequestStatus rs,
468 enum GNUNET_ARM_Result result)
473 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
475 GNUNET_asprintf (&msg,
476 _("Failed to send a request to kill the `%s' service: %%s\n"),
478 FPRINTF (stdout, msg, req_string (rs));
480 GNUNET_SCHEDULER_shutdown ();
483 if ((GNUNET_ARM_RESULT_STOPPED != result) &&
484 (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY != result))
486 GNUNET_asprintf (&msg,
487 _("Failed to kill the `%s' service: %s\n"),
488 term, ret_string (result));
489 FPRINTF (stdout, "%s", msg);
491 GNUNET_SCHEDULER_shutdown ();
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Service %s stopped successfully\n",
500 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
505 * Function called with the list of running services. Prints
506 * the list to stdout, then starts the event loop again.
507 * Prints an error message and terminates the process on errors.
509 * @param cls closure (unused)
510 * @param rs request status (success, failure, etc.)
511 * @param count number of services in the list
512 * @param list list of services that are running
515 list_callback (void *cls,
516 enum GNUNET_ARM_RequestStatus rs,
518 const char *const*list)
523 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
527 GNUNET_asprintf (&msg, "%s",
528 _("Failed to request a list of services: %s\n"));
529 FPRINTF (stdout, msg, req_string (rs));
532 GNUNET_SCHEDULER_shutdown ();
536 FPRINTF (stderr, "%s",
537 _("Error communicating with ARM. ARM not running?\n"));
538 GNUNET_SCHEDULER_shutdown ();
543 FPRINTF (stdout, "%s", _("Running services:\n"));
544 for (i = 0; i < count; i++)
545 FPRINTF (stdout, "%s\n", list[i]);
546 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
551 * Main action loop. Runs the various jobs that we've been asked to
554 * @param cls closure, unused
557 action_loop (void *cls)
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561 "Running requested actions\n");
569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
570 "Termination action\n");
571 op = GNUNET_ARM_request_service_stop (h,
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
583 op = GNUNET_ARM_request_service_stop (h,
593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
595 op = GNUNET_ARM_request_service_start (h, "arm",
596 (no_stdout ? 0 : GNUNET_OS_INHERIT_STD_OUT) |
597 (no_stderr ? 0 : GNUNET_OS_INHERIT_STD_ERR),
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Initialization action\n");
608 op = GNUNET_ARM_request_service_start (h, init,
609 GNUNET_OS_INHERIT_STD_NONE,
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "Going to list all running services controlled by ARM.\n");
620 op = GNUNET_ARM_request_service_list (h,
631 _("Now only monitoring, press CTRL-C to stop.\n"));
632 quiet = 0; /* does not make sense to stay quiet in monitor mode at this time */
633 return; /* done with tasks, just monitor */
636 default: /* last phase */
637 GNUNET_SCHEDULER_shutdown ();
645 * Function called when a service starts or stops.
648 * @param service service name
649 * @param status status of the service
652 srv_status (void *cls,
654 enum GNUNET_ARM_ServiceStatus status)
660 case GNUNET_ARM_SERVICE_MONITORING_STARTED:
661 return; /* this should be done silently */
662 case GNUNET_ARM_SERVICE_STOPPED:
663 msg = _("Stopped %s.\n");
665 case GNUNET_ARM_SERVICE_STARTING:
666 msg = _("Starting %s...\n");
668 case GNUNET_ARM_SERVICE_STOPPING:
669 msg = _("Stopping %s...\n");
683 _("Unknown status %u for service %s.\n"),
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688 "Got service %s status %d\n",
695 * Task run on timeout (if -T is given).
698 timeout_task_cb (void *cls)
702 GNUNET_SCHEDULER_shutdown ();
707 * Main function that will be run by the scheduler.
710 * @param args remaining command-line arguments
711 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
712 * @param c configuration
718 const struct GNUNET_CONFIGURATION_Handle *c)
722 cfg = GNUNET_CONFIGURATION_dup (c);
723 config_file = cfgfile;
725 GNUNET_CONFIGURATION_get_value_string (cfg,
730 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
738 GNUNET_CONFIGURATION_get_value_filename (cfg,
743 GNUNET_CONFIGURATION_set_value_string (cfg,
749 GNUNET_free (armconfig);
751 if (NULL == (h = GNUNET_ARM_connect (cfg,
756 m = GNUNET_ARM_monitor_start (cfg,
759 al_task = GNUNET_SCHEDULER_add_now (&action_loop,
761 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
763 if (0 != timeout.rel_value_us)
764 timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
771 * The main function to obtain arm from gnunetd.
773 * @param argc number of arguments from the command line
774 * @param argv command line arguments
775 * @return 0 ok, 1 on error, 2 on timeout
778 main (int argc, char *const *argv)
780 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
781 {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
782 GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
783 {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
784 GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
785 {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
786 GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
787 {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
788 GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
789 {'r', "restart", NULL,
790 gettext_noop ("stop and start all GNUnet default services"),
791 GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
792 {'d', "delete", NULL,
793 gettext_noop ("delete config file and directory on exit"),
794 GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
795 {'m', "monitor", NULL,
796 gettext_noop ("monitor ARM activities"),
797 GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor},
798 {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
799 GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
800 {'T', "timeout", "DELAY",
801 gettext_noop ("exit with error status if operation does not finish after DELAY"),
802 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &timeout},
803 {'I', "info", NULL, gettext_noop ("list currently running services"),
804 GNUNET_NO, &GNUNET_GETOPT_set_one, &list},
805 {'O', "no-stdout", NULL, gettext_noop ("don't let gnunet-service-arm inherit standard output"),
806 GNUNET_NO, &GNUNET_GETOPT_set_one, &no_stdout},
807 {'E', "no-stderr", NULL, gettext_noop ("don't let gnunet-service-arm inherit standard error"),
808 GNUNET_NO, &GNUNET_GETOPT_set_one, &no_stderr},
809 GNUNET_GETOPT_OPTION_END
812 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
817 GNUNET_PROGRAM_run (argc, argv, "gnunet-arm",
819 ("Control services and the Automated Restart Manager (ARM)"),
820 options, &run, NULL))
822 GNUNET_free ((void *) argv);
825 GNUNET_free ((void*) argv);
829 /* end of gnunet-arm.c */