2 This file is part of GNUnet.
3 (C) 2009, 2012, 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 * Timeout for stopping services. Long to give some services a real chance.
34 #define STOP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
37 * Timeout for stopping ARM. Extra-long since ARM needs to stop everyone else.
39 #define STOP_TIMEOUT_ARM GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
42 * Timeout for starting services, very short because of the strange way start works
43 * (by checking if running before starting, so really this time is always waited on
44 * startup (annoying)).
46 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
49 * Timeout for listing all running services.
51 #define LIST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
54 * Set if we are to shutdown all services (including ARM).
59 * Set if we are to start default services (including ARM).
64 * Set if we are to stop/start default services (including ARM).
69 * Set if we should delete configuration and temp directory on exit.
74 * Set if we should not print status messages.
79 * Set if we should print a list of currently running services.
84 * Set to the name of a service to start.
89 * Set to the name of a service to kill.
94 * Set to the name of the config file used.
96 static const char *config_file;
99 * Set to the directory where runtime files are stored.
109 * Connection with ARM.
111 static struct GNUNET_ARM_Handle *h;
114 * Monitor connection with ARM.
116 static struct GNUNET_ARM_MonitorHandle *m;
121 static struct GNUNET_CONFIGURATION_Handle *cfg;
124 * Processing stage that we are in. Simple counter.
126 static unsigned int phase;
129 * User defined timestamp for completing operations.
131 static struct GNUNET_TIME_Relative timeout;
134 * Do we want to give our stdout to gnunet-service-arm?
136 static unsigned int no_stdout;
139 * Do we want to give our stderr to gnunet-service-arm?
141 static unsigned int no_stderr;
145 * Attempts to delete configuration file and SERVICEHOME
146 * on arm shutdown provided the end and delete options
147 * were specified when gnunet-arm was run.
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "Will attempt to remove configuration file %s and service directory %s\n",
156 if (UNLINK (config_file) != 0)
158 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
159 _("Failed to remove configuration file %s\n"), config_file);
162 if (GNUNET_DISK_directory_remove (dir) != GNUNET_OK)
164 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
165 _("Failed to remove servicehome directory %s\n"), dir);
172 * Main continuation-passing-style loop. Runs the various
173 * jobs that we've been asked to do in order.
175 * @param cls closure, unused
176 * @param tc context, unused
179 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
181 GNUNET_ARM_disconnect_and_free (h);
182 GNUNET_ARM_monitor_disconnect_and_free (m);
185 if ((end == GNUNET_YES) && (delete == GNUNET_YES))
187 GNUNET_CONFIGURATION_destroy (cfg);
193 req_string (enum GNUNET_ARM_RequestStatus rs)
197 case GNUNET_ARM_REQUEST_SENT_OK:
198 return _("Message was sent successfully");
199 case GNUNET_ARM_REQUEST_CONFIGURATION_ERROR:
200 return _("Misconfiguration (can't connect to the ARM service)");
201 case GNUNET_ARM_REQUEST_DISCONNECTED:
202 return _("We disconnected from ARM before we could send a request");
203 case GNUNET_ARM_REQUEST_BUSY:
204 return _("ARM API is busy");
205 case GNUNET_ARM_REQUEST_TOO_LONG:
206 return _("Request doesn't fit into a message");
207 case GNUNET_ARM_REQUEST_TIMEOUT:
208 return _("Request timed out");
210 return _("Unknown request status");
215 ret_string (enum GNUNET_ARM_Result result)
219 case GNUNET_ARM_RESULT_STOPPED:
220 return _("%s is stopped");
221 case GNUNET_ARM_RESULT_STARTING:
222 return _("%s is starting");
223 case GNUNET_ARM_RESULT_IS_STARTING_ALREADY:
224 return _("%s is starting already");
225 case GNUNET_ARM_RESULT_IS_STOPPING_ALREADY:
226 return _("%s is stopping already");
227 case GNUNET_ARM_RESULT_IS_STARTED_ALREADY:
228 return _("%s is started already");
229 case GNUNET_ARM_RESULT_IS_STOPPED_ALREADY:
230 return _("%s is stopped already");
231 case GNUNET_ARM_RESULT_IS_NOT_KNOWN:
232 return _("%s service is not known to ARM");
233 case GNUNET_ARM_RESULT_START_FAILED:
234 return _("%s service failed to start");
235 case GNUNET_ARM_RESULT_IN_SHUTDOWN:
236 return _("%s service can't be started because ARM is shutting down");
238 return _("Unknown result code.");
242 static void action_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
245 * Function called whenever we connect to or disconnect from ARM.
248 * @param arm handle to the ARM connection
249 * @param connected GNUNET_YES if connected, GNUNET_NO if disconnected,
250 * GNUNET_SYSERR on error.
253 conn_status (void *cls, struct GNUNET_ARM_Handle *arm,
256 if (GNUNET_SYSERR == connected)
258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259 _("Fatal error initializing ARM API.\n"));
260 GNUNET_SCHEDULER_shutdown ();
265 GNUNET_SCHEDULER_add_now (action_loop, NULL);
271 term_callback (void *cls, struct GNUNET_ARM_Handle *arm,
272 enum GNUNET_ARM_RequestStatus rs, const char *service,
273 enum GNUNET_ARM_Result result)
275 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
278 GNUNET_asprintf (&msg, _("Failed to send a request to kill the `%s' service: %%s\n"), term);
279 FPRINTF (stdout, msg, req_string (rs));
281 GNUNET_SCHEDULER_shutdown ();
283 if ((GNUNET_ARM_RESULT_STOPPED == result) ||
284 (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY == result))
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service %s shutdown successful\n", term);
288 GNUNET_SCHEDULER_add_now (action_loop, NULL);
293 GNUNET_asprintf (&msg, _("Failed to kill the `%s' service: %%s\n"), term);
294 FPRINTF (stdout, msg, ret_string (result));
296 GNUNET_SCHEDULER_shutdown ();
301 end_callback (void *cls, struct GNUNET_ARM_Handle *arm,
302 enum GNUNET_ARM_RequestStatus rs, const char *service,
303 enum GNUNET_ARM_Result result)
305 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
308 GNUNET_asprintf (&msg, "%s", _("Failed to send a stop request to the ARM service: %s\n"));
309 FPRINTF (stdout, msg, req_string (rs));
311 GNUNET_SCHEDULER_shutdown ();
313 if ((GNUNET_ARM_RESULT_STOPPING == result) ||
314 (GNUNET_ARM_RESULT_STOPPED == result) ||
315 (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY == result))
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM service shutdown successful\n");
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initiating an ARM restart\n");
325 GNUNET_SCHEDULER_add_now (action_loop, NULL);
330 GNUNET_asprintf (&msg, "%s", _("Failed to stop the ARM service: %s\n"));
331 FPRINTF (stdout, msg, ret_string (result));
333 GNUNET_SCHEDULER_shutdown ();
338 start_callback (void *cls, struct GNUNET_ARM_Handle *arm,
339 enum GNUNET_ARM_RequestStatus rs, const char *service,
340 enum GNUNET_ARM_Result result)
342 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
345 GNUNET_asprintf (&msg, "%s", _("Failed to start the ARM service: %s\n"));
346 FPRINTF (stdout, msg, req_string (rs));
348 GNUNET_SCHEDULER_shutdown ();
350 if ((GNUNET_ARM_RESULT_STARTING == result) ||
351 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY == result))
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM service [re]start successful\n");
355 GNUNET_SCHEDULER_add_now (action_loop, NULL);
360 GNUNET_asprintf (&msg, "%s", _("Failed to start the ARM service: %s\n"));
361 FPRINTF (stdout, msg, ret_string (result));
363 GNUNET_SCHEDULER_shutdown ();
369 init_callback (void *cls, struct GNUNET_ARM_Handle *arm,
370 enum GNUNET_ARM_RequestStatus rs, const char *service,
371 enum GNUNET_ARM_Result result)
373 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
376 GNUNET_asprintf (&msg, _("Failed to send a request to start the `%s' service: %%s\n"), init);
377 FPRINTF (stdout, msg, req_string (rs));
379 GNUNET_SCHEDULER_shutdown ();
381 if ((GNUNET_ARM_RESULT_STARTING == result) ||
382 (GNUNET_ARM_RESULT_IS_STARTED_ALREADY == result))
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service %s [re]start successful\n", init);
386 GNUNET_SCHEDULER_add_now (action_loop, NULL);
391 GNUNET_asprintf (&msg, _("Failed to start the `%s' service: %%s\n"), init);
392 FPRINTF (stdout, msg, ret_string (result));
394 GNUNET_SCHEDULER_shutdown ();
400 list_callback (void *cls, struct GNUNET_ARM_Handle *arm,
401 enum GNUNET_ARM_RequestStatus rs, unsigned int count,
402 const char *const*list)
405 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
408 GNUNET_asprintf (&msg, "%s", _("Failed to request a list of services: %s\n"));
409 FPRINTF (stdout, msg, req_string (rs));
411 GNUNET_SCHEDULER_shutdown ();
415 FPRINTF (stderr, "%s", _("Error communicating with ARM. ARM not running?\n"));
418 FPRINTF (stdout, "%s", _("Running services:\n"));
419 for (i = 0; i < count; i++)
420 FPRINTF (stdout, "%s\n", list[i]);
421 GNUNET_SCHEDULER_add_now (action_loop, NULL);
426 * Main action loop. Runs the various
427 * jobs that we've been asked to do in order.
429 * @param cls closure, unused
430 * @param tc context, unused
433 action_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running requested actions\n");
436 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Termination action\n");
446 GNUNET_ARM_request_service_stop (h, term, (0 ==
447 timeout.rel_value) ? STOP_TIMEOUT : timeout,
448 term_callback, NULL);
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "End action\n");
456 GNUNET_ARM_request_service_stop (h, "arm", (0 ==
457 timeout.rel_value) ? STOP_TIMEOUT_ARM : timeout,
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start action\n");
466 GNUNET_ARM_request_service_start (h, "arm",
467 (no_stdout ? 0 : GNUNET_OS_INHERIT_STD_OUT) |
468 (no_stderr ? 0 : GNUNET_OS_INHERIT_STD_ERR),
469 (0 == timeout.rel_value) ? START_TIMEOUT: timeout,
470 start_callback, NULL);
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initialization action\n");
478 GNUNET_ARM_request_service_start (h, init, GNUNET_OS_INHERIT_STD_NONE,
479 (0 == timeout.rel_value) ? STOP_TIMEOUT : timeout,
480 init_callback, NULL);
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488 "Going to list all running services controlled by ARM.\n");
490 GNUNET_ARM_request_service_list (h,
491 (0 == timeout.rel_value) ? LIST_TIMEOUT : timeout,
492 list_callback, &list);
496 default: /* last phase */
497 GNUNET_SCHEDULER_shutdown ();
505 * Function called when a service starts or stops.
508 * @param arm handle to the ARM connection
509 * @param service service name
510 * @param status status of the service
513 srv_status (void *cls, struct GNUNET_ARM_MonitorHandle *arm,
514 const char *service, enum GNUNET_ARM_ServiceStatus status)
519 case GNUNET_ARM_SERVICE_MONITORING_STARTED:
520 msg = _("Began monitoring ARM for service status changes\n");
522 case GNUNET_ARM_SERVICE_STOPPED:
523 msg = _("Stopped %s.\n");
525 case GNUNET_ARM_SERVICE_STARTING:
526 msg = _("Starting %s...\n");
528 case GNUNET_ARM_SERVICE_STOPPING:
529 msg = _("Stopping %s...\n");
536 FPRINTF (stderr, msg, service);
538 FPRINTF (stderr, _("Unknown status %u for service %s.\n"), status, service);
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got service %s status %u\n", service, status);
544 * Main function that will be run by the scheduler.
547 * @param args remaining command-line arguments
548 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
549 * @param c configuration
552 run (void *cls, char *const *args, const char *cfgfile,
553 const struct GNUNET_CONFIGURATION_Handle *c)
557 cfg = GNUNET_CONFIGURATION_dup (c);
558 config_file = cfgfile;
559 if (GNUNET_CONFIGURATION_get_value_string
560 (cfg, "PATHS", "SERVICEHOME", &dir) != GNUNET_OK)
562 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
563 "PATHS", "SERVICEHOME");
569 GNUNET_CONFIGURATION_get_value_filename (cfg, "arm", "CONFIG",
572 GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG",
576 GNUNET_free (armconfig);
578 h = GNUNET_ARM_connect (cfg, &conn_status, NULL);
581 m = GNUNET_ARM_monitor (cfg, &srv_status, NULL);
584 GNUNET_SCHEDULER_add_now (&action_loop, NULL);
585 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
586 shutdown_task, NULL);
590 GNUNET_ARM_disconnect_and_free (h);
598 * The main function to obtain arm from gnunetd.
600 * @param argc number of arguments from the command line
601 * @param argv command line arguments
602 * @return 0 ok, 1 on error
605 main (int argc, char *const *argv)
607 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
608 {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
609 GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
610 {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
611 GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
612 {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
613 GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
614 {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
615 GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
616 {'r', "restart", NULL,
617 gettext_noop ("stop and start all GNUnet default services"),
618 GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
619 {'d', "delete", NULL,
620 gettext_noop ("delete config file and directory on exit"),
621 GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
622 {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
623 GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
624 {'T', "timeout", "MSECS",
625 gettext_noop ("timeout in MSECS milliseconds for completing current operation"),
626 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &timeout},
627 {'I', "info", NULL, gettext_noop ("list currently running services"),
628 GNUNET_NO, &GNUNET_GETOPT_set_one, &list},
629 {'O', "no-stdout", NULL, gettext_noop ("don't let gnunet-service-arm inherit standard output"),
630 GNUNET_NO, &GNUNET_GETOPT_set_one, &no_stdout},
631 {'E', "no-stderr", NULL, gettext_noop ("don't let gnunet-service-arm inherit standard error"),
632 GNUNET_NO, &GNUNET_GETOPT_set_one, &no_stderr},
633 GNUNET_GETOPT_OPTION_END
636 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
640 GNUNET_PROGRAM_run (argc, argv, "gnunet-arm",
642 ("Control services and the Automated Restart Manager (ARM)"),
643 options, &run, NULL))
645 GNUNET_free ((void *) argv);
648 GNUNET_free ((void*) argv);
652 /* end of gnunet-arm.c */