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.
113 * FIXME: to be implemented!
115 static struct GNUNET_TIME_Relative timeout;
118 * Do we want to give our stdout to gnunet-service-arm?
120 static unsigned int no_stdout;
123 * Do we want to give our stderr to gnunet-service-arm?
125 static unsigned int no_stderr;
128 * Handle for the task running the #action_loop().
130 static struct GNUNET_SCHEDULER_Task *al_task;
135 static struct GNUNET_ARM_Operation *op;
138 * Attempts to delete configuration file and GNUNET_HOME
139 * on ARM shutdown provided the end and delete options
140 * were specified when gnunet-arm was run.
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "Will attempt to remove configuration file %s and service directory %s\n",
149 if (0 != UNLINK (config_file))
151 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
152 _("Failed to remove configuration file %s\n"),
155 if (GNUNET_OK != GNUNET_DISK_directory_remove (dir))
157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
158 _("Failed to remove servicehome directory %s\n"),
166 * Main continuation-passing-style loop. Runs the various
167 * jobs that we've been asked to do in order.
169 * @param cls closure, unused
172 shutdown_task (void *cls)
176 GNUNET_SCHEDULER_cancel (al_task);
181 GNUNET_ARM_operation_cancel (op);
186 GNUNET_ARM_disconnect (h);
191 GNUNET_ARM_monitor_stop (m);
194 if ((GNUNET_YES == end) && (GNUNET_YES == delete))
196 GNUNET_CONFIGURATION_destroy (cfg);
202 * Returns a string interpretation of 'rs'
204 * @param rs the request status from ARM
205 * @return a string interpretation of the request status
208 req_string (enum GNUNET_ARM_RequestStatus rs)
212 case GNUNET_ARM_REQUEST_SENT_OK:
213 return _("Message was sent successfully");
214 case GNUNET_ARM_REQUEST_CONFIGURATION_ERROR:
215 return _("Misconfiguration (can not connect to the ARM service)");
216 case GNUNET_ARM_REQUEST_DISCONNECTED:
217 return _("We disconnected from ARM before we could send a request");
218 case GNUNET_ARM_REQUEST_BUSY:
219 return _("ARM API is busy");
220 case GNUNET_ARM_REQUEST_TIMEOUT:
221 return _("Request timed out");
223 return _("Unknown request status");
228 * Returns a string interpretation of the 'result'
230 * @param result the arm result
231 * @return a string interpretation
234 ret_string (enum GNUNET_ARM_Result result)
238 case GNUNET_ARM_RESULT_STOPPED:
239 return _("%s is stopped");
240 case GNUNET_ARM_RESULT_STARTING:
241 return _("%s is starting");
242 case GNUNET_ARM_RESULT_STOPPING:
243 return _("%s is stopping");
244 case GNUNET_ARM_RESULT_IS_STARTING_ALREADY:
245 return _("%s is starting already");
246 case GNUNET_ARM_RESULT_IS_STOPPING_ALREADY:
247 return _("%s is stopping already");
248 case GNUNET_ARM_RESULT_IS_STARTED_ALREADY:
249 return _("%s is started already");
250 case GNUNET_ARM_RESULT_IS_STOPPED_ALREADY:
251 return _("%s is stopped already");
252 case GNUNET_ARM_RESULT_IS_NOT_KNOWN:
253 return _("%s service is not known to ARM");
254 case GNUNET_ARM_RESULT_START_FAILED:
255 return _("%s service failed to start");
256 case GNUNET_ARM_RESULT_IN_SHUTDOWN:
257 return _("%s service cannot be started because ARM is shutting down");
259 return _("%.s Unknown result code.");
264 * Main task that runs our various operations in order.
269 action_loop (void *cls);
273 * Function called whenever we connect to or disconnect from ARM.
274 * Termiantes the process if we fail to connect to the service on
278 * @param connected #GNUNET_YES if connected, #GNUNET_NO if disconnected,
279 * #GNUNET_SYSERR on error.
282 conn_status (void *cls,
287 if ( (GNUNET_SYSERR == connected) &&
290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
291 _("Fatal error initializing ARM API.\n"));
292 GNUNET_SCHEDULER_shutdown ();
300 * We have requested ARM to be started, this function
301 * is called with the result of the operation. Informs the
302 * use of the result; on success, we continue with the event
303 * loop, on failure we terminate the process.
305 * @param cls closure unused
306 * @param rs what happened to our request
307 * @param result if the request was processed, this is the result
311 start_callback (void *cls,
312 enum GNUNET_ARM_RequestStatus rs,
313 enum GNUNET_ARM_Result result)
318 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
320 GNUNET_asprintf (&msg, "%s", _("Failed to start the ARM service: %s\n"));
321 FPRINTF (stdout, msg, req_string (rs));
323 GNUNET_SCHEDULER_shutdown ();
326 if ( (GNUNET_ARM_RESULT_STARTING != result) &&
327 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY != result) )
329 GNUNET_asprintf (&msg, "%s", _("Failed to start the ARM service: %s\n"));
330 FPRINTF (stdout, msg, ret_string (result));
332 GNUNET_SCHEDULER_shutdown ();
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336 "ARM service [re]start successful\n");
338 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
343 * We have requested ARM to be stopped, this function
344 * is called with the result of the operation. Informs the
345 * use of the result; on success, we continue with the event
346 * loop, on failure we terminate the process.
348 * @param cls closure unused
349 * @param rs what happened to our request
350 * @param result if the request was processed, this is the result
354 stop_callback (void *cls,
355 enum GNUNET_ARM_RequestStatus rs,
356 enum GNUNET_ARM_Result result)
361 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
363 GNUNET_asprintf (&msg, "%s",
364 _("Failed to send a stop request to the ARM service: %s\n"));
365 FPRINTF (stdout, msg, req_string (rs));
367 GNUNET_SCHEDULER_shutdown ();
370 if ((GNUNET_ARM_RESULT_STOPPING != result) &&
371 (GNUNET_ARM_RESULT_STOPPED != result) &&
372 (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY != result))
374 GNUNET_asprintf (&msg, "%s",
375 _("Failed to stop the ARM service: %s\n"));
376 FPRINTF (stdout, msg, ret_string (result));
378 GNUNET_SCHEDULER_shutdown ();
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "ARM service shutdown successful\n");
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Initiating an ARM restart\n");
391 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
396 * We have requested a service to be started, this function
397 * is called with the result of the operation. Informs the
398 * use of the result; on success, we continue with the event
399 * loop, on failure we terminate the process.
401 * @param cls closure unused
402 * @param rs what happened to our request
403 * @param result if the request was processed, this is the result
407 init_callback (void *cls,
408 enum GNUNET_ARM_RequestStatus rs,
409 enum GNUNET_ARM_Result result)
414 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
416 GNUNET_asprintf (&msg,
417 _("Failed to send a request to start the `%s' service: %%s\n"),
419 FPRINTF (stdout, msg, req_string (rs));
421 GNUNET_SCHEDULER_shutdown ();
424 if ((GNUNET_ARM_RESULT_STARTING != result) &&
425 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY != result))
427 GNUNET_asprintf (&msg,
428 _("Failed to start the `%s' service: %s\n"),
430 ret_string (result));
431 FPRINTF (stdout, "%s", msg);
433 GNUNET_SCHEDULER_shutdown ();
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437 "Service %s [re]started successfully\n",
441 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
446 * We have requested a service to be stopped, this function
447 * is called with the result of the operation. Informs the
448 * use of the result; on success, we continue with the event
449 * loop, on failure we terminate the process.
451 * @param cls closure unused
452 * @param rs what happened to our request
453 * @param result if the request was processed, this is the result
457 term_callback (void *cls,
458 enum GNUNET_ARM_RequestStatus rs,
459 enum GNUNET_ARM_Result result)
464 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
466 GNUNET_asprintf (&msg,
467 _("Failed to send a request to kill the `%s' service: %%s\n"),
469 FPRINTF (stdout, msg, req_string (rs));
471 GNUNET_SCHEDULER_shutdown ();
474 if ((GNUNET_ARM_RESULT_STOPPED != result) &&
475 (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY != result))
477 GNUNET_asprintf (&msg,
478 _("Failed to kill the `%s' service: %s\n"),
479 term, ret_string (result));
480 FPRINTF (stdout, "%s", msg);
482 GNUNET_SCHEDULER_shutdown ();
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Service %s stopped successfully\n",
491 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
496 * Function called with the list of running services. Prints
497 * the list to stdout, then starts the event loop again.
498 * Prints an error message and terminates the process on errors.
500 * @param cls closure (unused)
501 * @param rs request status (success, failure, etc.)
502 * @param count number of services in the list
503 * @param list list of services that are running
506 list_callback (void *cls,
507 enum GNUNET_ARM_RequestStatus rs,
509 const char *const*list)
514 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
518 GNUNET_asprintf (&msg, "%s",
519 _("Failed to request a list of services: %s\n"));
520 FPRINTF (stdout, msg, req_string (rs));
523 GNUNET_SCHEDULER_shutdown ();
527 FPRINTF (stderr, "%s",
528 _("Error communicating with ARM. ARM not running?\n"));
529 GNUNET_SCHEDULER_shutdown ();
534 FPRINTF (stdout, "%s", _("Running services:\n"));
535 for (i = 0; i < count; i++)
536 FPRINTF (stdout, "%s\n", list[i]);
537 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
542 * Main action loop. Runs the various jobs that we've been asked to
545 * @param cls closure, unused
548 action_loop (void *cls)
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "Running requested actions\n");
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561 "Termination action\n");
562 op = GNUNET_ARM_request_service_stop (h,
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 op = GNUNET_ARM_request_service_stop (h,
584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 op = GNUNET_ARM_request_service_start (h, "arm",
587 (no_stdout ? 0 : GNUNET_OS_INHERIT_STD_OUT) |
588 (no_stderr ? 0 : GNUNET_OS_INHERIT_STD_ERR),
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Initialization action\n");
599 op = GNUNET_ARM_request_service_start (h, init,
600 GNUNET_OS_INHERIT_STD_NONE,
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
610 "Going to list all running services controlled by ARM.\n");
611 op = GNUNET_ARM_request_service_list (h,
622 _("Now only monitoring, press CTRL-C to stop.\n"));
623 quiet = 0; /* does not make sense to stay quiet in monitor mode at this time */
624 return; /* done with tasks, just monitor */
627 default: /* last phase */
628 GNUNET_SCHEDULER_shutdown ();
636 * Function called when a service starts or stops.
639 * @param service service name
640 * @param status status of the service
643 srv_status (void *cls,
645 enum GNUNET_ARM_ServiceStatus status)
651 case GNUNET_ARM_SERVICE_MONITORING_STARTED:
652 return; /* this should be done silently */
653 case GNUNET_ARM_SERVICE_STOPPED:
654 msg = _("Stopped %s.\n");
656 case GNUNET_ARM_SERVICE_STARTING:
657 msg = _("Starting %s...\n");
659 case GNUNET_ARM_SERVICE_STOPPING:
660 msg = _("Stopping %s...\n");
674 _("Unknown status %u for service %s.\n"),
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
679 "Got service %s status %d\n",
686 * Main function that will be run by the scheduler.
689 * @param args remaining command-line arguments
690 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
691 * @param c configuration
697 const struct GNUNET_CONFIGURATION_Handle *c)
701 cfg = GNUNET_CONFIGURATION_dup (c);
702 config_file = cfgfile;
704 GNUNET_CONFIGURATION_get_value_string (cfg,
709 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
717 GNUNET_CONFIGURATION_get_value_filename (cfg,
722 GNUNET_CONFIGURATION_set_value_string (cfg,
728 GNUNET_free (armconfig);
730 if (NULL == (h = GNUNET_ARM_connect (cfg,
735 m = GNUNET_ARM_monitor_start (cfg,
738 al_task = GNUNET_SCHEDULER_add_now (&action_loop,
740 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
746 * The main function to obtain arm from gnunetd.
748 * @param argc number of arguments from the command line
749 * @param argv command line arguments
750 * @return 0 ok, 1 on error
753 main (int argc, char *const *argv)
755 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
756 {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
757 GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
758 {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
759 GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
760 {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
761 GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
762 {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
763 GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
764 {'r', "restart", NULL,
765 gettext_noop ("stop and start all GNUnet default services"),
766 GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
767 {'d', "delete", NULL,
768 gettext_noop ("delete config file and directory on exit"),
769 GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
770 {'m', "monitor", NULL,
771 gettext_noop ("monitor ARM activities"),
772 GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor},
773 {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
774 GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
775 {'T', "timeout", "DELAY",
776 gettext_noop ("exit with error status if operation does not finish after DELAY"),
777 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &timeout},
778 {'I', "info", NULL, gettext_noop ("list currently running services"),
779 GNUNET_NO, &GNUNET_GETOPT_set_one, &list},
780 {'O', "no-stdout", NULL, gettext_noop ("don't let gnunet-service-arm inherit standard output"),
781 GNUNET_NO, &GNUNET_GETOPT_set_one, &no_stdout},
782 {'E', "no-stderr", NULL, gettext_noop ("don't let gnunet-service-arm inherit standard error"),
783 GNUNET_NO, &GNUNET_GETOPT_set_one, &no_stderr},
784 GNUNET_GETOPT_OPTION_END
787 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
792 GNUNET_PROGRAM_run (argc, argv, "gnunet-arm",
794 ("Control services and the Automated Restart Manager (ARM)"),
795 options, &run, NULL))
797 GNUNET_free ((void *) argv);
800 GNUNET_free ((void*) argv);
804 /* end of gnunet-arm.c */