2 This file is part of GNUnet.
3 (C) 2009 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;
116 static const struct GNUNET_CONFIGURATION_Handle *cfg;
119 * Processing stage that we are in. Simple counter.
121 static unsigned int phase;
124 * User defined timestamp for completing operations.
126 static struct GNUNET_TIME_Relative timeout;
130 * Do we want to give our stdout to gnunet-service-arm?
132 static unsigned int no_stdout = 0;
135 * Do we want to give our stderr to gnunet-service-arm?
137 static unsigned int no_stderr = 0;
140 * Main continuation-passing-style loop. Runs the various
141 * jobs that we've been asked to do in order.
143 * @param cls closure, unused
144 * @param tc context, unused
147 cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
151 * Callback invoked with the status of the last operation. Reports to the
152 * user and then runs the next phase in the FSM.
154 * @param cls pointer to "const char*" identifying service that was manipulated
155 * @param result result of the operation
158 confirm_cb (void *cls,
159 enum GNUNET_ARM_ProcessStatus result)
161 const char *service = cls;
165 case GNUNET_ARM_PROCESS_UNKNOWN:
166 FPRINTF (stderr, _("Service `%s' is unknown to ARM.\n"), service);
169 case GNUNET_ARM_PROCESS_DOWN:
170 if (quiet != GNUNET_YES)
171 FPRINTF (stdout, _("Service `%s' has been stopped.\n"), service);
173 case GNUNET_ARM_PROCESS_ALREADY_RUNNING:
174 FPRINTF (stderr, _("Service `%s' was already running.\n"), service);
177 case GNUNET_ARM_PROCESS_STARTING:
178 if (quiet != GNUNET_YES)
179 FPRINTF (stdout, _("Service `%s' has been started.\n"), service);
181 case GNUNET_ARM_PROCESS_ALREADY_STOPPING:
182 FPRINTF (stderr, _("Service `%s' was already being stopped.\n"), service);
185 case GNUNET_ARM_PROCESS_ALREADY_DOWN:
186 FPRINTF (stderr, _("Service `%s' was already not running.\n"), service);
189 case GNUNET_ARM_PROCESS_SHUTDOWN:
190 FPRINTF (stderr, "%s", _("Request ignored as ARM is shutting down.\n"));
193 case GNUNET_ARM_PROCESS_COMMUNICATION_ERROR:
194 FPRINTF (stderr, "%s", _("Error communicating with ARM service.\n"));
197 case GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT:
198 FPRINTF (stderr, "%s", _("Timeout communicating with ARM service.\n"));
201 case GNUNET_ARM_PROCESS_FAILURE:
202 FPRINTF (stderr, "%s", _("Operation failed.\n"));
206 FPRINTF (stderr, "%s", _("Unknown response code from ARM.\n"));
209 GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
210 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
214 * Callback invoked with the list of running services.
215 * Reports to the user and then runs the next phase in the FSM.
217 * @param cls currently not used
218 * @param result result of the operation
219 * @param count number of running services
220 * @param list copy of the list of running services
223 list_cb (void *cls, int result, unsigned int count, const char *const*list)
227 if ( (result != GNUNET_YES) || (NULL == list) )
229 FPRINTF (stderr, "%s", _("Error communicating with ARM. ARM not running?\n"));
232 FPRINTF (stdout, "%s", _("Running services:\n"));
233 for (i=0; i<count; i++)
234 FPRINTF (stdout, "%s\n", list[i]);
238 * Main function that will be run by the scheduler.
241 * @param args remaining command-line arguments
242 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
243 * @param c configuration
246 run (void *cls, char *const *args, const char *cfgfile,
247 const struct GNUNET_CONFIGURATION_Handle *c)
250 config_file = cfgfile;
251 if (GNUNET_CONFIGURATION_get_value_string
252 (cfg, "PATHS", "SERVICEHOME", &dir) != GNUNET_OK)
254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256 ("Fatal configuration error: `%s' option in section `%s' missing.\n"),
257 "SERVICEHOME", "PATHS");
260 h = GNUNET_ARM_connect (cfg, NULL);
263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264 _("Fatal error initializing ARM API.\n"));
268 GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
269 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
273 * Attempts to delete configuration file and SERVICEHOME
274 * on arm shutdown provided the end and delete options
275 * were specified when gnunet-arm was run.
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Will attempt to remove configuration file %s and service directory %s\n",
284 if (UNLINK (config_file) != 0)
286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
287 _("Failed to remove configuration file %s\n"), config_file);
290 if (GNUNET_DISK_directory_remove (dir) != GNUNET_OK)
292 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
293 _("Failed to remove servicehome directory %s\n"), dir);
299 * Main continuation-passing-style loop. Runs the various
300 * jobs that we've been asked to do in order.
302 * @param cls closure, unused
303 * @param tc context, unused
306 cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
315 GNUNET_ARM_stop_service (h, term,
317 timeout.rel_value) ? STOP_TIMEOUT :
318 timeout, &confirm_cb, term);
323 if ((end) || (restart))
325 GNUNET_ARM_stop_service (h, "arm",
327 timeout.rel_value) ? STOP_TIMEOUT_ARM
328 : timeout, &confirm_cb, "arm");
335 GNUNET_ARM_start_service (h, "arm",
336 (no_stdout ? 0 : GNUNET_OS_INHERIT_STD_OUT) |
337 (no_stderr ? 0 : GNUNET_OS_INHERIT_STD_ERR),
339 timeout.rel_value) ? START_TIMEOUT :
340 timeout, &confirm_cb, "arm");
347 GNUNET_ARM_start_service (h, init,
348 (no_stdout ? 0 : GNUNET_OS_INHERIT_STD_OUT) |
349 (no_stderr ? 0 : GNUNET_OS_INHERIT_STD_ERR),
351 timeout.rel_value) ? START_TIMEOUT :
352 timeout, &confirm_cb, init);
359 GNUNET_ARM_disconnect (h);
364 h = GNUNET_ARM_connect (cfg, NULL);
367 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
368 _("Fatal error initializing ARM API.\n"));
372 GNUNET_SCHEDULER_add_now (&cps_loop, NULL);
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379 "Going to list all running services controlled by ARM.\n");
383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
384 _("Fatal error initializing ARM API.\n"));
388 GNUNET_ARM_list_running_services (h,
390 timeout.rel_value) ? LIST_TIMEOUT :
391 timeout, &list_cb, NULL);
395 default: /* last phase */
396 GNUNET_ARM_disconnect (h);
397 if ((end == GNUNET_YES) && (delete == GNUNET_YES))
406 * The main function to obtain arm from gnunetd.
408 * @param argc number of arguments from the command line
409 * @param argv command line arguments
410 * @return 0 ok, 1 on error
413 main (int argc, char *const *argv)
415 static unsigned long long temp_timeout_ms;
417 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
418 {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
419 GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
420 {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
421 GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
422 {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
423 GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
424 {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
425 GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
426 {'r', "restart", NULL,
427 gettext_noop ("stop and start all GNUnet default services"),
428 GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
429 {'d', "delete", NULL,
430 gettext_noop ("delete config file and directory on exit"),
431 GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
432 {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
433 GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
434 {'T', "timeout", NULL,
435 gettext_noop ("timeout for completing current operation"),
436 GNUNET_YES, &GNUNET_GETOPT_set_ulong, &temp_timeout_ms},
437 {'I', "info", NULL, gettext_noop ("List currently running services"),
438 GNUNET_NO, &GNUNET_GETOPT_set_one, &list},
439 {'O', "no-stdout", NULL, gettext_noop ("Don't let gnunet-service-arm inherit standard output"),
440 GNUNET_NO, &GNUNET_GETOPT_set_one, &no_stdout},
441 {'E', "no-stderr", NULL, gettext_noop ("Don't let gnunet-service-arm inherit standard error"),
442 GNUNET_NO, &GNUNET_GETOPT_set_one, &no_stderr},
443 GNUNET_GETOPT_OPTION_END
446 if (temp_timeout_ms > 0)
447 timeout.rel_value = temp_timeout_ms;
449 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
453 GNUNET_PROGRAM_run (argc, argv, "gnunet-arm",
455 ("Control services and the Automated Restart Manager (ARM)"),
456 options, &run, NULL))
464 /* end of gnunet-arm.c */