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 int no_stdout;
127 * Do we want to give our stderr to gnunet-service-arm?
129 static 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_DISCONNECTED:
224 return _("We disconnected from ARM before we could send a request");
226 return _("Unknown request status");
231 * Returns a string interpretation of the 'result'
233 * @param result the arm result
234 * @return a string interpretation
237 ret_string (enum GNUNET_ARM_Result result)
241 case GNUNET_ARM_RESULT_STOPPED:
242 return _("is stopped");
243 case GNUNET_ARM_RESULT_STARTING:
244 return _("is starting");
245 case GNUNET_ARM_RESULT_STOPPING:
246 return _("is stopping");
247 case GNUNET_ARM_RESULT_IS_STARTING_ALREADY:
248 return _("is starting already");
249 case GNUNET_ARM_RESULT_IS_STOPPING_ALREADY:
250 return _("is stopping already");
251 case GNUNET_ARM_RESULT_IS_STARTED_ALREADY:
252 return _("is started already");
253 case GNUNET_ARM_RESULT_IS_STOPPED_ALREADY:
254 return _("is stopped already");
255 case GNUNET_ARM_RESULT_IS_NOT_KNOWN:
256 return _("service is not known to ARM");
257 case GNUNET_ARM_RESULT_START_FAILED:
258 return _("service failed to start");
259 case GNUNET_ARM_RESULT_IN_SHUTDOWN:
260 return _("service cannot be manipulated because ARM is shutting down");
262 return _("Unknown result code.");
267 * Main task that runs our various operations in order.
272 action_loop (void *cls);
276 * Function called whenever we connect to or disconnect from ARM.
277 * Termiantes the process if we fail to connect to the service on
281 * @param connected #GNUNET_YES if connected, #GNUNET_NO if disconnected,
282 * #GNUNET_SYSERR on error.
285 conn_status (void *cls,
290 if ( (GNUNET_SYSERR == connected) &&
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
294 _("Fatal error initializing ARM API.\n"));
295 GNUNET_SCHEDULER_shutdown ();
303 * We have requested ARM to be started, this function
304 * is called with the result of the operation. Informs the
305 * use of the result; on success, we continue with the event
306 * loop, on failure we terminate the process.
308 * @param cls closure unused
309 * @param rs what happened to our request
310 * @param result if the request was processed, this is the result
314 start_callback (void *cls,
315 enum GNUNET_ARM_RequestStatus rs,
316 enum GNUNET_ARM_Result result)
319 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
322 _("Failed to start the ARM service: %s\n"),
324 GNUNET_SCHEDULER_shutdown ();
327 if ( (GNUNET_ARM_RESULT_STARTING != result) &&
328 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY != result) )
331 _("Failed to start the ARM service: %s\n"),
332 ret_string (result));
333 GNUNET_SCHEDULER_shutdown ();
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "ARM service [re]start successful\n");
339 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
344 * We have requested ARM to be stopped, this function
345 * is called with the result of the operation. Informs the
346 * use of the result; on success, we continue with the event
347 * loop, on failure we terminate the process.
349 * @param cls closure unused
350 * @param rs what happened to our request
351 * @param result if the request was processed, this is the result
355 stop_callback (void *cls,
356 enum GNUNET_ARM_RequestStatus rs,
357 enum GNUNET_ARM_Result result)
362 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
364 GNUNET_asprintf (&msg, "%s",
365 _("Failed to send a stop request to the ARM service: %s\n"));
366 FPRINTF (stdout, msg, req_string (rs));
368 GNUNET_SCHEDULER_shutdown ();
371 if ((GNUNET_ARM_RESULT_STOPPING != result) &&
372 (GNUNET_ARM_RESULT_STOPPED != result) &&
373 (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY != result))
376 _("Failed to stop the ARM service: %s\n"),
377 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)
412 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
415 _("Failed to send a request to start the `%s' service: %s\n"),
418 GNUNET_SCHEDULER_shutdown ();
421 if ((GNUNET_ARM_RESULT_STARTING != result) &&
422 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY != result))
425 _("Failed to start the `%s' service: %s\n"),
427 ret_string (result));
428 GNUNET_SCHEDULER_shutdown ();
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Service %s [re]started successfully\n",
436 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
441 * We have requested a service to be stopped, this function
442 * is called with the result of the operation. Informs the
443 * use of the result; on success, we continue with the event
444 * loop, on failure we terminate the process.
446 * @param cls closure unused
447 * @param rs what happened to our request
448 * @param result if the request was processed, this is the result
452 term_callback (void *cls,
453 enum GNUNET_ARM_RequestStatus rs,
454 enum GNUNET_ARM_Result result)
459 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
461 GNUNET_asprintf (&msg,
462 _("Failed to send a request to kill the `%s' service: %%s\n"),
464 FPRINTF (stdout, msg, req_string (rs));
466 GNUNET_SCHEDULER_shutdown ();
469 if ((GNUNET_ARM_RESULT_STOPPED != result) &&
470 (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY != result))
473 _("Failed to kill the `%s' service: %s\n"),
475 ret_string (result));
476 GNUNET_SCHEDULER_shutdown ();
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481 "Service %s stopped successfully\n",
485 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
490 * Function called with the list of running services. Prints
491 * the list to stdout, then starts the event loop again.
492 * Prints an error message and terminates the process on errors.
494 * @param cls closure (unused)
495 * @param rs request status (success, failure, etc.)
496 * @param count number of services in the list
497 * @param list list of services that are running
500 list_callback (void *cls,
501 enum GNUNET_ARM_RequestStatus rs,
503 const char *const*list)
508 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
512 GNUNET_asprintf (&msg, "%s",
513 _("Failed to request a list of services: %s\n"));
514 FPRINTF (stdout, msg, req_string (rs));
517 GNUNET_SCHEDULER_shutdown ();
521 FPRINTF (stderr, "%s",
522 _("Error communicating with ARM. ARM not running?\n"));
523 GNUNET_SCHEDULER_shutdown ();
528 FPRINTF (stdout, "%s", _("Running services:\n"));
529 for (i = 0; i < count; i++)
530 FPRINTF (stdout, "%s\n", list[i]);
531 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
536 * Main action loop. Runs the various jobs that we've been asked to
539 * @param cls closure, unused
542 action_loop (void *cls)
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "Running requested actions\n");
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "Termination action\n");
556 op = GNUNET_ARM_request_service_stop (h,
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568 op = GNUNET_ARM_request_service_stop (h,
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
580 op = GNUNET_ARM_request_service_start (h, "arm",
581 (no_stdout ? 0 : GNUNET_OS_INHERIT_STD_OUT) |
582 (no_stderr ? 0 : GNUNET_OS_INHERIT_STD_ERR),
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592 "Initialization action\n");
593 op = GNUNET_ARM_request_service_start (h, init,
594 GNUNET_OS_INHERIT_STD_NONE,
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "Going to list all running services controlled by ARM.\n");
605 op = GNUNET_ARM_request_service_list (h,
616 _("Now only monitoring, press CTRL-C to stop.\n"));
617 quiet = 0; /* does not make sense to stay quiet in monitor mode at this time */
618 return; /* done with tasks, just monitor */
621 default: /* last phase */
622 GNUNET_SCHEDULER_shutdown ();
630 * Function called when a service starts or stops.
633 * @param service service name
634 * @param status status of the service
637 srv_status (void *cls,
639 enum GNUNET_ARM_ServiceStatus status)
645 case GNUNET_ARM_SERVICE_MONITORING_STARTED:
646 return; /* this should be done silently */
647 case GNUNET_ARM_SERVICE_STOPPED:
648 msg = _("Stopped %s.\n");
650 case GNUNET_ARM_SERVICE_STARTING:
651 msg = _("Starting %s...\n");
653 case GNUNET_ARM_SERVICE_STOPPING:
654 msg = _("Stopping %s...\n");
668 _("Unknown status %u for service %s.\n"),
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
673 "Got service %s status %d\n",
680 * Task run on timeout (if -T is given).
683 timeout_task_cb (void *cls)
687 GNUNET_SCHEDULER_shutdown ();
692 * Main function that will be run by the scheduler.
695 * @param args remaining command-line arguments
696 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
697 * @param c configuration
703 const struct GNUNET_CONFIGURATION_Handle *c)
707 cfg = GNUNET_CONFIGURATION_dup (c);
708 config_file = cfgfile;
710 GNUNET_CONFIGURATION_get_value_string (cfg,
715 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
723 GNUNET_CONFIGURATION_get_value_filename (cfg,
728 GNUNET_CONFIGURATION_set_value_string (cfg,
734 GNUNET_free (armconfig);
736 if (NULL == (h = GNUNET_ARM_connect (cfg,
741 m = GNUNET_ARM_monitor_start (cfg,
744 al_task = GNUNET_SCHEDULER_add_now (&action_loop,
746 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
748 if (0 != timeout.rel_value_us)
749 timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
756 * The main function to obtain arm from gnunetd.
758 * @param argc number of arguments from the command line
759 * @param argv command line arguments
760 * @return 0 ok, 1 on error, 2 on timeout
763 main (int argc, char *const *argv)
765 struct GNUNET_GETOPT_CommandLineOption options[] = {
767 GNUNET_GETOPT_option_flag ('e',
769 gettext_noop ("stop all GNUnet services"),
772 GNUNET_GETOPT_option_string ('i',
775 gettext_noop ("start a particular service"),
778 GNUNET_GETOPT_option_string ('k',
781 gettext_noop ("stop a particular service"),
784 GNUNET_GETOPT_option_flag ('s',
786 gettext_noop ("start all GNUnet default services"),
789 GNUNET_GETOPT_option_flag ('r',
791 gettext_noop ("stop and start all GNUnet default services"),
793 GNUNET_GETOPT_option_flag ('d',
795 gettext_noop ("delete config file and directory on exit"),
798 GNUNET_GETOPT_option_flag ('m',
800 gettext_noop ("monitor ARM activities"),
803 GNUNET_GETOPT_option_flag ('q',
805 gettext_noop ("don't print status messages"),
808 GNUNET_GETOPT_option_relative_time ('T',
811 gettext_noop ("exit with error status if operation does not finish after DELAY"),
814 GNUNET_GETOPT_option_flag ('I',
816 gettext_noop ("list currently running services"),
819 GNUNET_GETOPT_option_flag ('O',
821 gettext_noop ("don't let gnunet-service-arm inherit standard output"),
824 GNUNET_GETOPT_option_flag ('E',
826 gettext_noop ("don't let gnunet-service-arm inherit standard error"),
829 GNUNET_GETOPT_OPTION_END
832 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
837 GNUNET_PROGRAM_run (argc, argv, "gnunet-arm",
839 ("Control services and the Automated Restart Manager (ARM)"),
840 options, &run, NULL))
842 GNUNET_free ((void *) argv);
845 GNUNET_free ((void*) argv);
849 /* end of gnunet-arm.c */