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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 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"),
169 * Main continuation-passing-style loop. Runs the various
170 * jobs that we've been asked to do in order.
172 * @param cls closure, unused
175 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,
291 if ( (GNUNET_SYSERR == connected) &&
294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
295 _("Fatal error initializing ARM API.\n"));
296 GNUNET_SCHEDULER_shutdown ();
304 * We have requested ARM to be started, this function
305 * is called with the result of the operation. Informs the
306 * use of the result; on success, we continue with the event
307 * loop, on failure we terminate the process.
309 * @param cls closure unused
310 * @param rs what happened to our request
311 * @param result if the request was processed, this is the result
315 start_callback (void *cls,
316 enum GNUNET_ARM_RequestStatus rs,
317 enum GNUNET_ARM_Result result)
321 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
324 _("Failed to start the ARM service: %s\n"),
326 GNUNET_SCHEDULER_shutdown ();
329 if ( (GNUNET_ARM_RESULT_STARTING != result) &&
330 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY != result) )
333 _("Failed to start the ARM service: %s\n"),
334 ret_string (result));
335 GNUNET_SCHEDULER_shutdown ();
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "ARM service [re]start successful\n");
341 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
346 * We have requested ARM to be stopped, this function
347 * is called with the result of the operation. Informs the
348 * use of the result; on success, we continue with the event
349 * loop, on failure we terminate the process.
351 * @param cls closure unused
352 * @param rs what happened to our request
353 * @param result if the request was processed, this is the result
357 stop_callback (void *cls,
358 enum GNUNET_ARM_RequestStatus rs,
359 enum GNUNET_ARM_Result result)
365 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
367 GNUNET_asprintf (&msg, "%s",
368 _("Failed to send a stop request to the ARM service: %s\n"));
369 FPRINTF (stdout, msg, req_string (rs));
371 GNUNET_SCHEDULER_shutdown ();
374 if ((GNUNET_ARM_RESULT_STOPPING != result) &&
375 (GNUNET_ARM_RESULT_STOPPED != result) &&
376 (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY != result))
379 _("Failed to stop the ARM service: %s\n"),
380 ret_string (result));
381 GNUNET_SCHEDULER_shutdown ();
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "ARM service shutdown successful\n");
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "Initiating an ARM restart\n");
394 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
399 * We have requested a service to be started, this function
400 * is called with the result of the operation. Informs the
401 * use of the result; on success, we continue with the event
402 * loop, on failure we terminate the process.
404 * @param cls closure unused
405 * @param rs what happened to our request
406 * @param result if the request was processed, this is the result
410 init_callback (void *cls,
411 enum GNUNET_ARM_RequestStatus rs,
412 enum GNUNET_ARM_Result result)
416 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
419 _("Failed to send a request to start the `%s' service: %s\n"),
422 GNUNET_SCHEDULER_shutdown ();
425 if ((GNUNET_ARM_RESULT_STARTING != result) &&
426 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY != result))
429 _("Failed to start the `%s' service: %s\n"),
431 ret_string (result));
432 GNUNET_SCHEDULER_shutdown ();
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "Service %s [re]started successfully\n",
440 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
445 * We have requested a service to be stopped, this function
446 * is called with the result of the operation. Informs the
447 * use of the result; on success, we continue with the event
448 * loop, on failure we terminate the process.
450 * @param cls closure unused
451 * @param rs what happened to our request
452 * @param result if the request was processed, this is the result
456 term_callback (void *cls,
457 enum GNUNET_ARM_RequestStatus rs,
458 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))
478 _("Failed to kill the `%s' service: %s\n"),
480 ret_string (result));
481 GNUNET_SCHEDULER_shutdown ();
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486 "Service %s stopped successfully\n",
490 al_task = GNUNET_SCHEDULER_add_now (&action_loop, NULL);
495 * Function called with the list of running services. Prints
496 * the list to stdout, then starts the event loop again.
497 * Prints an error message and terminates the process on errors.
499 * @param cls closure (unused)
500 * @param rs request status (success, failure, etc.)
501 * @param count number of services in the list
502 * @param list list of services that are running
505 list_callback (void *cls,
506 enum GNUNET_ARM_RequestStatus rs,
508 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)
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 "Running requested actions\n");
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 "Termination action\n");
563 op = GNUNET_ARM_request_service_stop (h,
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 op = GNUNET_ARM_request_service_stop (h,
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 op = GNUNET_ARM_request_service_start (h, "arm",
588 (no_stdout ? 0 : GNUNET_OS_INHERIT_STD_OUT) |
589 (no_stderr ? 0 : GNUNET_OS_INHERIT_STD_ERR),
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599 "Initialization action\n");
600 op = GNUNET_ARM_request_service_start (h, init,
601 GNUNET_OS_INHERIT_STD_NONE,
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
611 "Going to list all running services controlled by ARM.\n");
612 op = GNUNET_ARM_request_service_list (h,
623 _("Now only monitoring, press CTRL-C to stop.\n"));
624 quiet = 0; /* does not make sense to stay quiet in monitor mode at this time */
625 return; /* done with tasks, just monitor */
628 default: /* last phase */
629 GNUNET_SCHEDULER_shutdown ();
637 * Function called when a service starts or stops.
640 * @param service service name
641 * @param status status of the service
644 srv_status (void *cls,
646 enum GNUNET_ARM_ServiceStatus status)
653 case GNUNET_ARM_SERVICE_MONITORING_STARTED:
654 return; /* this should be done silently */
655 case GNUNET_ARM_SERVICE_STOPPED:
656 msg = _("Stopped %s.\n");
658 case GNUNET_ARM_SERVICE_STARTING:
659 msg = _("Starting %s...\n");
661 case GNUNET_ARM_SERVICE_STOPPING:
662 msg = _("Stopping %s...\n");
676 _("Unknown status %u for service %s.\n"),
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
681 "Got service %s status %d\n",
688 * Task run on timeout (if -T is given).
691 timeout_task_cb (void *cls)
696 GNUNET_SCHEDULER_shutdown ();
701 * Main function that will be run by the scheduler.
704 * @param args remaining command-line arguments
705 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
706 * @param c configuration
712 const struct GNUNET_CONFIGURATION_Handle *c)
717 cfg = GNUNET_CONFIGURATION_dup (c);
719 GNUNET_CONFIGURATION_get_value_string (cfg,
724 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
729 (void) GNUNET_CONFIGURATION_get_value_filename (cfg,
733 if (NULL == (h = GNUNET_ARM_connect (cfg,
738 m = GNUNET_ARM_monitor_start (cfg,
741 al_task = GNUNET_SCHEDULER_add_now (&action_loop,
743 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
745 if (0 != timeout.rel_value_us)
746 timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
753 * The main function to obtain arm from gnunetd.
755 * @param argc number of arguments from the command line
756 * @param argv command line arguments
757 * @return 0 ok, 1 on error, 2 on timeout
763 struct GNUNET_GETOPT_CommandLineOption options[] = {
764 GNUNET_GETOPT_option_flag ('e',
766 gettext_noop ("stop all GNUnet services"),
768 GNUNET_GETOPT_option_string ('i',
771 gettext_noop ("start a particular service"),
773 GNUNET_GETOPT_option_string ('k',
776 gettext_noop ("stop a particular service"),
778 GNUNET_GETOPT_option_flag ('s',
780 gettext_noop ("start all GNUnet default services"),
782 GNUNET_GETOPT_option_flag ('r',
784 gettext_noop ("stop and start all GNUnet default services"),
786 GNUNET_GETOPT_option_flag ('d',
788 gettext_noop ("delete config file and directory on exit"),
790 GNUNET_GETOPT_option_flag ('m',
792 gettext_noop ("monitor ARM activities"),
794 GNUNET_GETOPT_option_flag ('q',
796 gettext_noop ("don't print status messages"),
798 GNUNET_GETOPT_option_relative_time ('T',
801 gettext_noop ("exit with error status if operation does not finish after DELAY"),
803 GNUNET_GETOPT_option_flag ('I',
805 gettext_noop ("list currently running services"),
807 GNUNET_GETOPT_option_flag ('O',
809 gettext_noop ("don't let gnunet-service-arm inherit standard output"),
811 GNUNET_GETOPT_option_flag ('E',
813 gettext_noop ("don't let gnunet-service-arm inherit standard error"),
815 GNUNET_GETOPT_OPTION_END
819 GNUNET_STRINGS_get_utf8_args (argc, argv,
824 GNUNET_PROGRAM_run (argc,
828 ("Control services and the Automated Restart Manager (ARM)"),
832 GNUNET_free ((void *) argv);
835 GNUNET_free ((void*) argv);
839 /* end of gnunet-arm.c */