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_client_lib.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_time_lib.h"
35 * Timeout for stopping services. Long to give some services a real chance.
37 #define STOP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
40 * Timeout for stopping ARM. Extra-long since ARM needs to stop everyone else.
42 #define STOP_TIMEOUT_ARM GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
45 * Timeout for starting services, very short because of the strange way start works
46 * (by checking if running before starting, so really this time is always waited on
47 * startup (annoying)).
49 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
52 * Timeout for starting services, very short because of the strange way start works
53 * (by checking if running before starting, so really this time is always waited on
54 * startup (annoying)).
56 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
59 * Set if we are to shutdown all services (including ARM).
64 * Set if we are to start default services (including ARM).
69 * Set if we are to stop/start default services (including ARM).
74 * Set if we should delete configuration and temp directory on exit.
79 * Set if we should not print status messages.
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 a service to test.
99 * Set to the name of the config file used.
101 static const char *config_file;
104 * Set to the directory where runtime files are stored.
114 * Connection with ARM.
116 static struct GNUNET_ARM_Handle *h;
121 const 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;
135 * Main continuation-passing-style loop. Runs the various
136 * jobs that we've been asked to do in order.
138 * @param cls closure, unused
139 * @param tc context, unused
142 cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
146 * Callback invoked with the status of the last operation. Reports to the
147 * user and then runs the next phase in the FSM.
149 * @param cls pointer to "const char*" identifying service that was manipulated
150 * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error
153 confirm_cb (void *cls, int success)
155 const char *service = cls;
160 if (quiet != GNUNET_YES)
161 fprintf (stdout, _("Service `%s' has been started.\n"), service);
162 if ((phase - 1 != 2) && (phase - 1 != 3))
164 if (quiet != GNUNET_YES)
165 fprintf (stdout, _("Failed to stop service `%s'!\n"), service);
170 if (quiet != GNUNET_YES)
171 fprintf (stdout, _("Service `%s' has been stopped.\n"), service);
172 if ((phase - 1 != 0) && (phase - 1 != 1))
174 if (quiet != GNUNET_YES)
175 fprintf (stdout, _("Failed to start service `%s'!\n"), service);
180 if (quiet != GNUNET_YES)
181 fprintf (stdout, _("Some error communicating with service `%s'.\n"),
187 GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
188 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
193 * Function called to confirm that a service is running (or that
194 * it is not running).
196 * @param cls pointer to "const char*" identifying service that was manipulated
197 * @param tc reason determines if service is now running
200 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
202 const char *service = cls;
204 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
206 if (quiet != GNUNET_YES)
207 fprintf (stdout, _("Service `%s' is running.\n"), service);
211 if (quiet != GNUNET_YES)
212 fprintf (stdout, _("Service `%s' is not running.\n"), service);
214 GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
215 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
220 * Main function that will be run by the scheduler.
223 * @param args remaining command-line arguments
224 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
225 * @param c configuration
228 run (void *cls, char *const *args, const char *cfgfile,
229 const struct GNUNET_CONFIGURATION_Handle *c)
232 config_file = cfgfile;
233 if (GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME", &dir)
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238 ("Fatal configuration error: `%s' option in section `%s' missing.\n"),
239 "SERVICEHOME", "PATHS");
242 h = GNUNET_ARM_connect (cfg, NULL);
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246 _("Fatal error initializing ARM API.\n"));
250 GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
251 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
255 * Attempts to delete configuration file and SERVICEHOME
256 * on arm shutdown provided the end and delete options
257 * were specified when gnunet-arm was run.
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263 "Will attempt to remove configuration file %s and service directory %s\n",
266 if (UNLINK (config_file) != 0)
268 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
269 _("Failed to remove configuration file %s\n"), config_file);
272 if (GNUNET_DISK_directory_remove (dir) != GNUNET_OK)
274 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
275 _("Failed to remove servicehome directory %s\n"), dir);
281 * Main continuation-passing-style loop. Runs the various
282 * jobs that we've been asked to do in order.
284 * @param cls closure, unused
285 * @param tc context, unused
288 cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
297 GNUNET_ARM_stop_service (h, term,
299 timeout.rel_value) ? STOP_TIMEOUT : timeout,
305 if ((end) || (restart))
307 GNUNET_ARM_stop_service (h, "arm",
309 timeout.rel_value) ? STOP_TIMEOUT_ARM :
310 timeout, &confirm_cb, "arm");
317 GNUNET_ARM_start_service (h, "arm",
319 timeout.rel_value) ? START_TIMEOUT : timeout,
327 GNUNET_ARM_start_service (h, init,
329 timeout.rel_value) ? START_TIMEOUT : timeout,
337 GNUNET_CLIENT_service_test (test, cfg,
339 timeout.rel_value) ? TEST_TIMEOUT :
340 timeout, &confirm_task, test);
347 GNUNET_ARM_disconnect (h);
352 h = GNUNET_ARM_connect (cfg, NULL);
355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
356 _("Fatal error initializing ARM API.\n"));
360 GNUNET_SCHEDULER_add_now (&cps_loop, NULL);
364 default: /* last phase */
365 GNUNET_ARM_disconnect (h);
366 if ((end == GNUNET_YES) && (delete == GNUNET_YES))
375 * The main function to obtain arm from gnunetd.
377 * @param argc number of arguments from the command line
378 * @param argv command line arguments
379 * @return 0 ok, 1 on error
382 main (int argc, char *const *argv)
384 static unsigned long long temp_timeout_ms;
386 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
387 {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
388 GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
389 {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
390 GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
391 {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
392 GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
393 {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
394 GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
395 {'r', "restart", NULL,
396 gettext_noop ("stop and start all GNUnet default services"),
397 GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
398 {'t', "test", "SERVICE",
399 gettext_noop ("test if a particular service is running"),
400 GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
401 {'d', "delete", NULL,
402 gettext_noop ("delete config file and directory on exit"),
403 GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
404 {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
405 GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
406 {'T', "timeout", NULL,
407 gettext_noop ("timeout for completing current operation"),
408 GNUNET_YES, &GNUNET_GETOPT_set_ulong, &temp_timeout_ms},
409 GNUNET_GETOPT_OPTION_END
412 if (temp_timeout_ms > 0)
413 timeout.rel_value = temp_timeout_ms;
416 GNUNET_PROGRAM_run (argc, argv, "gnunet-arm",
418 ("Control services and the Automated Restart Manager (ARM)"),
419 options, &run, NULL))
427 /* end of gnunet-arm.c */