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, 60)
40 * Timeout for starting services, very short because of the strange way start works
41 * (by checking if running before starting, so really this time is always waited on
42 * startup (annoying)).
44 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50)
47 * Timeout for starting services, very short because of the strange way start works
48 * (by checking if running before starting, so really this time is always waited on
49 * startup (annoying)).
51 #define TEST_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 to the name of a service to start.
84 * Set to the name of a service to kill.
89 * Set to the name of a service to test.
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 struct GNUNET_SCHEDULER_Handle *sched;
121 const struct GNUNET_CONFIGURATION_Handle *cfg;
124 * Processing stage that we are in. Simple counter.
126 static unsigned int phase;
130 * Main continuation-passing-style loop. Runs the various
131 * jobs that we've been asked to do in order.
133 * @param cls closure, unused
134 * @param tc context, unused
138 const struct GNUNET_SCHEDULER_TaskContext *tc);
142 * Callback invoked with the status of the last operation. Reports to the
143 * user and then runs the next phase in the FSM.
145 * @param cls pointer to "const char*" identifying service that was manipulated
146 * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error
149 confirm_cb (void *cls, int success)
151 const char *service = cls;
155 if (quiet != GNUNET_YES)
156 fprintf(stdout, _("Service `%s' is now running.\n"), service);
157 if ((phase - 1 != 2) && (phase - 1 != 3))
159 if (quiet != GNUNET_YES)
160 fprintf(stdout, _("Failed to stop service `%s'!\n"), service);
165 if (quiet != GNUNET_YES)
166 fprintf(stdout, _("Service `%s' is not running.\n"), service);
167 if ((phase - 1 != 0) && (phase - 1 != 1))
169 if (quiet != GNUNET_YES)
170 fprintf(stdout, _("Failed to start service `%s'!\n"), service);
175 if (quiet != GNUNET_YES)
177 _("Some error communicating with service `%s'.\n"), service);
182 GNUNET_SCHEDULER_add_continuation (sched,
185 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
190 * Function called to confirm that a service is running (or that
191 * it is not running).
193 * @param cls pointer to "const char*" identifying service that was manipulated
194 * @param tc reason determines if service is now running
197 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
199 const char *service = cls;
201 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
203 if (quiet != GNUNET_YES)
204 fprintf(stdout, _("Service `%s' is running.\n"), service);
208 if (quiet != GNUNET_YES)
209 fprintf(stdout, _("Service `%s' is not running.\n"), service);
211 GNUNET_SCHEDULER_add_continuation (sched,
214 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
219 * Main function that will be run by the scheduler.
222 * @param s the scheduler to use
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
229 struct GNUNET_SCHEDULER_Handle *s,
232 const struct GNUNET_CONFIGURATION_Handle *c)
236 config_file = cfgfile;
237 if (GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "SERVICEHOME", &dir) != GNUNET_OK)
239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
240 _("Fatal configuration error: `%s' option in section `%s' missing.\n"),
245 h = GNUNET_ARM_connect (cfg, sched, NULL);
248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
249 _("Fatal error initializing ARM API.\n"));
253 GNUNET_SCHEDULER_add_continuation (sched,
256 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
260 * Attempts to delete configuration file and SERVICEHOME
261 * on arm shutdown provided the end and delete options
262 * were specified when gnunet-arm was run.
264 static void delete_files()
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will attempt to remove configuration file %s and service directory %s\n", config_file, dir);
268 if (UNLINK(config_file) != 0)
270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
271 _("Failed to remove configuration file %s\n"), config_file);
274 if (GNUNET_DISK_directory_remove(dir) != GNUNET_OK)
276 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
277 _("Failed to remove servicehome directory %s\n"), dir);
283 * Main continuation-passing-style loop. Runs the various
284 * jobs that we've been asked to do in order.
286 * @param cls closure, unused
287 * @param tc context, unused
291 const struct GNUNET_SCHEDULER_TaskContext *tc)
300 GNUNET_ARM_stop_service (h, term, STOP_TIMEOUT, &confirm_cb, term);
305 if ((end) || (restart))
307 GNUNET_ARM_stop_service (h, "arm", STOP_TIMEOUT, &confirm_cb, "arm");
314 GNUNET_ARM_start_service (h, "arm", START_TIMEOUT, &confirm_cb, "arm");
321 GNUNET_ARM_start_service (h, init, START_TIMEOUT, &confirm_cb, init);
328 GNUNET_CLIENT_service_test (sched, test, cfg, TEST_TIMEOUT, &confirm_task, test);
335 GNUNET_ARM_disconnect (h);
340 h = GNUNET_ARM_connect (cfg, sched, NULL);
343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
344 _("Fatal error initializing ARM API.\n"));
348 GNUNET_SCHEDULER_add_now(sched, &cps_loop, NULL);
352 default: /* last phase */
353 GNUNET_ARM_disconnect (h);
354 if ((end == GNUNET_YES) && (delete == GNUNET_YES))
363 * The main function to obtain arm from gnunetd.
365 * @param argc number of arguments from the command line
366 * @param argv command line arguments
367 * @return 0 ok, 1 on error
370 main (int argc, char *const *argv)
372 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
373 {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
374 GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
375 {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
376 GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
377 {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
378 GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
379 {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
380 GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
381 {'r', "restart", NULL, gettext_noop ("stop and start all GNUnet default services"),
382 GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
383 {'t', "test", "SERVICE",
384 gettext_noop ("test if a particular service is running"),
385 GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
386 {'d', "delete", NULL, gettext_noop ("delete config file and directory on exit"),
387 GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
388 {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
389 GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
390 GNUNET_GETOPT_OPTION_END
393 if (GNUNET_OK == GNUNET_PROGRAM_run (argc,
397 ("Control services and the Automated Restart Manager (ARM)"),
398 options, &run, NULL))
406 /* end of gnunet-arm.c */