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 * Set if we should print all services, including stopped ones.
62 * Monitor ARM activity.
67 * Set if we should print a list of currently running services.
72 * Set to the name of a service to start.
77 * Set to the name of a service to kill.
82 * Set to the name of the config file used.
84 static char *config_file;
87 * Set to the directory where runtime files are stored.
97 * Connection with ARM.
99 static struct GNUNET_ARM_Handle *h;
102 * Monitor connection with ARM.
104 static struct GNUNET_ARM_MonitorHandle *m;
109 static struct GNUNET_CONFIGURATION_Handle *cfg;
112 * Processing stage that we are in. Simple counter.
114 static unsigned int phase;
117 * User defined timestamp for completing operations.
119 static struct GNUNET_TIME_Relative timeout;
122 * Task to be run on timeout.
124 static struct GNUNET_SCHEDULER_Task *timeout_task;
127 * Do we want to give our stdout to gnunet-service-arm?
129 static int no_stdout;
132 * Do we want to give our stderr to gnunet-service-arm?
134 static int no_stderr;
137 * Handle for the task running the #action_loop().
139 static struct GNUNET_SCHEDULER_Task *al_task;
144 static struct GNUNET_ARM_Operation *op;
147 * Attempts to delete configuration file and GNUNET_HOME
148 * on ARM shutdown provided the end and delete options
149 * were specified when gnunet-arm was run.
155 GNUNET_ERROR_TYPE_DEBUG,
156 "Will attempt to remove configuration file %s and service directory %s\n",
159 if (0 != unlink (config_file))
161 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
162 _ ("Failed to remove configuration file %s\n"),
165 if (GNUNET_OK != GNUNET_DISK_directory_remove (dir))
167 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
168 _ ("Failed to remove servicehome directory %s\n"),
175 * Main continuation-passing-style loop. Runs the various
176 * jobs that we've been asked to do in order.
178 * @param cls closure, unused
181 shutdown_task (void *cls)
186 GNUNET_SCHEDULER_cancel (al_task);
191 GNUNET_ARM_operation_cancel (op);
196 GNUNET_ARM_disconnect (h);
201 GNUNET_ARM_monitor_stop (m);
204 if (NULL != timeout_task)
206 GNUNET_SCHEDULER_cancel (timeout_task);
209 if ( (GNUNET_YES == end) &&
210 (GNUNET_YES == delete) )
212 GNUNET_CONFIGURATION_destroy (cfg);
218 * Returns a string interpretation of @a rs
220 * @param rs the request status from ARM
221 * @return a string interpretation of the request status
224 req_string (enum GNUNET_ARM_RequestStatus rs)
228 case GNUNET_ARM_REQUEST_SENT_OK:
229 return _ ("Message was sent successfully");
231 case GNUNET_ARM_REQUEST_DISCONNECTED:
232 return _ ("We disconnected from ARM before we could send a request");
234 return _ ("Unknown request status");
239 * Returns a string interpretation of the @a result
241 * @param result the arm result
242 * @return a string interpretation
245 ret_string (enum GNUNET_ARM_Result result)
249 case GNUNET_ARM_RESULT_STOPPED:
250 return _ ("is stopped");
252 case GNUNET_ARM_RESULT_STARTING:
253 return _ ("is starting");
255 case GNUNET_ARM_RESULT_STOPPING:
256 return _ ("is stopping");
258 case GNUNET_ARM_RESULT_IS_STARTING_ALREADY:
259 return _ ("is starting already");
261 case GNUNET_ARM_RESULT_IS_STOPPING_ALREADY:
262 return _ ("is stopping already");
264 case GNUNET_ARM_RESULT_IS_STARTED_ALREADY:
265 return _ ("is started already");
267 case GNUNET_ARM_RESULT_IS_STOPPED_ALREADY:
268 return _ ("is stopped already");
270 case GNUNET_ARM_RESULT_IS_NOT_KNOWN:
271 return _ ("service is not known to ARM");
273 case GNUNET_ARM_RESULT_START_FAILED:
274 return _ ("service failed to start");
276 case GNUNET_ARM_RESULT_IN_SHUTDOWN:
277 return _ ("service cannot be manipulated because ARM is shutting down");
279 return _ ("Unknown result code.");
284 * Main task that runs our various operations in order.
289 action_loop (void *cls);
293 * Function called whenever we connect to or disconnect from ARM.
294 * Termiantes the process if we fail to connect to the service on
298 * @param connected #GNUNET_YES if connected, #GNUNET_NO if disconnected,
299 * #GNUNET_SYSERR on error.
302 conn_status (void *cls,
308 if ( (GNUNET_SYSERR == connected) &&
311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
312 _ ("Fatal error initializing ARM API.\n"));
313 GNUNET_SCHEDULER_shutdown ();
321 * We have requested ARM to be started, this function
322 * is called with the result of the operation. Informs the
323 * use of the result; on success, we continue with the event
324 * loop, on failure we terminate the process.
326 * @param cls closure unused
327 * @param rs what happened to our request
328 * @param result if the request was processed, this is the result
332 start_callback (void *cls,
333 enum GNUNET_ARM_RequestStatus rs,
334 enum GNUNET_ARM_Result result)
338 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
341 _ ("Failed to start the ARM service: %s\n"),
343 GNUNET_SCHEDULER_shutdown ();
346 if ((GNUNET_ARM_RESULT_STARTING != result) &&
347 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY != result))
350 _ ("Failed to start the ARM service: %s\n"),
351 ret_string (result));
352 GNUNET_SCHEDULER_shutdown ();
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "ARM service [re]start successful\n");
358 al_task = GNUNET_SCHEDULER_add_now (&action_loop,
364 * We have requested ARM to be stopped, this function
365 * is called with the result of the operation. Informs the
366 * use of the result; on success, we continue with the event
367 * loop, on failure we terminate the process.
369 * @param cls closure unused
370 * @param rs what happened to our request
371 * @param result if the request was processed, this is the result
375 stop_callback (void *cls,
376 enum GNUNET_ARM_RequestStatus rs,
377 enum GNUNET_ARM_Result result)
383 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
385 GNUNET_asprintf (&msg,
388 "Failed to send a stop request to the ARM service: %s\n"));
389 fprintf (stdout, msg, req_string (rs));
391 GNUNET_SCHEDULER_shutdown ();
394 if ( (GNUNET_ARM_RESULT_STOPPING != result) &&
395 (GNUNET_ARM_RESULT_STOPPED != result) &&
396 (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY != result) )
399 _ ("Failed to stop the ARM service: %s\n"),
400 ret_string (result));
401 GNUNET_SCHEDULER_shutdown ();
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 "ARM service shutdown successful\n");
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Initiating an ARM restart\n");
414 al_task = GNUNET_SCHEDULER_add_now (&action_loop,
420 * We have requested a service to be started, this function
421 * is called with the result of the operation. Informs the
422 * use of the result; on success, we continue with the event
423 * loop, on failure we terminate the process.
425 * @param cls closure unused
426 * @param rs what happened to our request
427 * @param result if the request was processed, this is the result
431 init_callback (void *cls,
432 enum GNUNET_ARM_RequestStatus rs,
433 enum GNUNET_ARM_Result result)
437 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
440 _ ("Failed to send a request to start the `%s' service: %s\n"),
443 GNUNET_SCHEDULER_shutdown ();
446 if ((GNUNET_ARM_RESULT_STARTING != result) &&
447 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY != result))
450 _ ("Failed to start the `%s' service: %s\n"),
452 ret_string (result));
453 GNUNET_SCHEDULER_shutdown ();
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Service %s [re]started successfully\n",
461 al_task = GNUNET_SCHEDULER_add_now (&action_loop,
467 * We have requested a service to be stopped, this function
468 * is called with the result of the operation. Informs the
469 * use of the result; on success, we continue with the event
470 * loop, on failure we terminate the process.
472 * @param cls closure unused
473 * @param rs what happened to our request
474 * @param result if the request was processed, this is the result
478 term_callback (void *cls,
479 enum GNUNET_ARM_RequestStatus rs,
480 enum GNUNET_ARM_Result result)
486 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
488 GNUNET_asprintf (&msg,
490 "Failed to send a request to kill the `%s' service: %%s\n"),
496 GNUNET_SCHEDULER_shutdown ();
499 if ( (GNUNET_ARM_RESULT_STOPPED != result) &&
500 (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY != result) )
503 _ ("Failed to kill the `%s' service: %s\n"),
505 ret_string (result));
506 GNUNET_SCHEDULER_shutdown ();
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "Service %s stopped successfully\n",
515 al_task = GNUNET_SCHEDULER_add_now (&action_loop,
521 * Function called with the list of running services. Prints
522 * the list to stdout, then starts the event loop again.
523 * Prints an error message and terminates the process on errors.
525 * @param cls closure (unused)
526 * @param rs request status (success, failure, etc.)
527 * @param count number of services in the list
528 * @param list list of services managed by arm
531 list_callback (void *cls,
532 enum GNUNET_ARM_RequestStatus rs,
534 const struct GNUNET_ARM_ServiceInfo *list)
536 unsigned int num_stopped = 0;
537 unsigned int num_started = 0;
538 unsigned int num_stopping = 0;
539 unsigned int num_failed = 0;
540 unsigned int num_finished = 0;
543 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
547 GNUNET_asprintf (&msg,
549 _ ("Failed to request a list of services: %s\n"));
555 GNUNET_SCHEDULER_shutdown ();
561 _ ("Error communicating with ARM. ARM not running?\n"));
562 GNUNET_SCHEDULER_shutdown ();
566 for (unsigned int i = 0; i < count; i++)
568 switch (list[i].status)
570 case GNUNET_ARM_SERVICE_STATUS_STOPPED:
573 case GNUNET_ARM_SERVICE_STATUS_FAILED:
576 case GNUNET_ARM_SERVICE_STATUS_FINISHED:
579 case GNUNET_ARM_SERVICE_STATUS_STARTED:
582 case GNUNET_ARM_SERVICE_STATUS_STOPPING:
585 "%s (binary='%s', status=stopping)\n",
592 "%s (binary='%s', status=unknown)\n",
603 _ ("All services:\n"));
607 _ ("Services (excluding stopped services):\n"));
608 if (num_stopped || num_failed || num_finished || num_stopping ||
612 fprintf (stdout, "(");
613 if (0 != num_started)
616 fprintf (stdout, " / ");
625 fprintf (stdout, " / ");
631 if (0 != num_stopping)
634 fprintf (stdout, " / ");
640 if (0 != num_stopped)
643 fprintf (stdout, " / ");
649 if (0 != num_finished)
652 fprintf (stdout, " / ");
658 fprintf (stdout, ")\n");
664 _ ("(No services configured.)\n"));
667 for (unsigned int i = 0; i < count; i++)
669 struct GNUNET_TIME_Relative restart_in;
670 switch (list[i].status)
672 case GNUNET_ARM_SERVICE_STATUS_STOPPED:
675 "%s (binary='%s', status=stopped)\n",
679 case GNUNET_ARM_SERVICE_STATUS_FAILED:
680 restart_in = GNUNET_TIME_absolute_get_remaining (list[i].restart_at);
682 "%s (binary='%s', status=failed, exit_status=%d, restart_delay='%s')\n",
685 list[i].last_exit_status,
686 GNUNET_STRINGS_relative_time_to_string (restart_in,
689 case GNUNET_ARM_SERVICE_STATUS_FINISHED:
691 "%s (binary='%s', status=finished)\n",
695 case GNUNET_ARM_SERVICE_STATUS_STARTED:
697 "%s (binary='%s', status=started)\n",
701 case GNUNET_ARM_SERVICE_STATUS_STOPPING:
703 "%s (binary='%s', status=stopping)\n",
710 "%s (binary='%s', status=unknown)\n",
716 al_task = GNUNET_SCHEDULER_add_now (&action_loop,
722 * Main action loop. Runs the various jobs that we've been asked to
725 * @param cls closure, unused
728 action_loop (void *cls)
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 "Running requested actions\n");
741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
742 "Termination action\n");
743 op = GNUNET_ARM_request_service_stop (h,
755 GNUNET_CLIENT_test (cfg,
758 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
759 "GNUnet not running, cannot stop the peer\n");
763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 op = GNUNET_ARM_request_service_stop (h,
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 GNUNET_ARM_request_service_start (h,
784 : GNUNET_OS_INHERIT_STD_OUT)
787 : GNUNET_OS_INHERIT_STD_ERR),
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798 "Initialization action\n");
799 op = GNUNET_ARM_request_service_start (h,
801 GNUNET_OS_INHERIT_STD_NONE,
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "Going to list all running services controlled by ARM.\n");
813 op = GNUNET_ARM_request_service_list (h,
825 _ ("Now only monitoring, press CTRL-C to stop.\n"));
827 0; /* does not make sense to stay quiet in monitor mode at this time */
828 return; /* done with tasks, just monitor */
832 default: /* last phase */
833 GNUNET_SCHEDULER_shutdown ();
841 * Function called when a service starts or stops.
844 * @param service service name
845 * @param status status of the service
848 srv_status (void *cls,
850 enum GNUNET_ARM_ServiceMonitorStatus status)
857 case GNUNET_ARM_SERVICE_MONITORING_STARTED:
858 return; /* this should be done silently */
860 case GNUNET_ARM_SERVICE_STOPPED:
861 msg = _ ("Stopped %s.\n");
864 case GNUNET_ARM_SERVICE_STARTING:
865 msg = _ ("Starting %s...\n");
868 case GNUNET_ARM_SERVICE_STOPPING:
869 msg = _ ("Stopping %s...\n");
884 _ ("Unknown status %u for service %s.\n"),
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "Got service %s status %d\n",
896 * Task run on timeout (if -T is given).
899 timeout_task_cb (void *cls)
904 GNUNET_SCHEDULER_shutdown ();
909 * Main function that will be run by the scheduler.
912 * @param args remaining command-line arguments
913 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
914 * @param c configuration
920 const struct GNUNET_CONFIGURATION_Handle *c)
925 cfg = GNUNET_CONFIGURATION_dup (c);
927 GNUNET_CONFIGURATION_get_value_string (cfg,
932 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
937 (void) GNUNET_CONFIGURATION_get_value_filename (cfg,
941 if (NULL == (h = GNUNET_ARM_connect (cfg,
946 m = GNUNET_ARM_monitor_start (cfg,
949 al_task = GNUNET_SCHEDULER_add_now (&action_loop,
951 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
953 if (0 != timeout.rel_value_us)
955 GNUNET_SCHEDULER_add_delayed (timeout,
962 * The main function to obtain arm from gnunetd.
964 * @param argc number of arguments from the command line
965 * @param argv command line arguments
966 * @return 0 ok, 1 on error, 2 on timeout
969 main (int argc, char *const *argv)
971 struct GNUNET_GETOPT_CommandLineOption options[] = {
972 GNUNET_GETOPT_option_flag ('e',
974 gettext_noop ("stop all GNUnet services"),
976 GNUNET_GETOPT_option_string ('i',
979 gettext_noop ("start a particular service"),
981 GNUNET_GETOPT_option_string ('k',
984 gettext_noop ("stop a particular service"),
986 GNUNET_GETOPT_option_flag ('a',
989 "also show stopped services (used with -I)"),
991 GNUNET_GETOPT_option_flag ('s',
994 "start all GNUnet default services"),
996 GNUNET_GETOPT_option_flag ('r',
999 "stop and start all GNUnet default services"),
1001 GNUNET_GETOPT_option_flag ('d',
1004 "delete config file and directory on exit"),
1006 GNUNET_GETOPT_option_flag ('m',
1008 gettext_noop ("monitor ARM activities"),
1010 GNUNET_GETOPT_option_flag ('q',
1012 gettext_noop ("don't print status messages"),
1014 GNUNET_GETOPT_option_relative_time (
1019 "exit with error status if operation does not finish after DELAY"),
1021 GNUNET_GETOPT_option_flag ('I',
1024 "list currently running services"),
1026 GNUNET_GETOPT_option_flag (
1029 gettext_noop ("don't let gnunet-service-arm inherit standard output"),
1031 GNUNET_GETOPT_option_flag (
1034 gettext_noop ("don't let gnunet-service-arm inherit standard error"),
1036 GNUNET_GETOPT_OPTION_END
1041 GNUNET_STRINGS_get_utf8_args (argc,
1047 (lret = GNUNET_PROGRAM_run (
1052 "Control services and the Automated Restart Manager (ARM)"),
1057 GNUNET_free ((void *) argv);
1060 GNUNET_free ((void *) argv);
1065 /* end of gnunet-arm.c */