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_MILLISECONDS, 1000)
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
143 const struct GNUNET_SCHEDULER_TaskContext *tc);
147 * Callback invoked with the status of the last operation. Reports to the
148 * user and then runs the next phase in the FSM.
150 * @param cls pointer to "const char*" identifying service that was manipulated
151 * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error
154 confirm_cb (void *cls, int success)
156 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)
182 _("Some error communicating with service `%s'.\n"), service);
187 GNUNET_SCHEDULER_add_continuation (&cps_loop,
189 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
194 * Function called to confirm that a service is running (or that
195 * it is not running).
197 * @param cls pointer to "const char*" identifying service that was manipulated
198 * @param tc reason determines if service is now running
201 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
203 const char *service = cls;
205 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
207 if (quiet != GNUNET_YES)
208 fprintf(stdout, _("Service `%s' is running.\n"), service);
212 if (quiet != GNUNET_YES)
213 fprintf(stdout, _("Service `%s' is not running.\n"), service);
215 GNUNET_SCHEDULER_add_continuation (&cps_loop,
217 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
222 * Main function that will be run by the scheduler.
225 * @param args remaining command-line arguments
226 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
227 * @param c configuration
233 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, NULL);
248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
249 _("Fatal error initializing ARM API.\n"));
253 GNUNET_SCHEDULER_add_continuation (&cps_loop,
255 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
259 * Attempts to delete configuration file and SERVICEHOME
260 * on arm shutdown provided the end and delete options
261 * were specified when gnunet-arm was run.
263 static void delete_files()
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will attempt to remove configuration file %s and service directory %s\n", config_file, dir);
267 if (UNLINK(config_file) != 0)
269 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
270 _("Failed to remove configuration file %s\n"), config_file);
273 if (GNUNET_DISK_directory_remove(dir) != GNUNET_OK)
275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
276 _("Failed to remove servicehome directory %s\n"), dir);
282 * Main continuation-passing-style loop. Runs the various
283 * jobs that we've been asked to do in order.
285 * @param cls closure, unused
286 * @param tc context, unused
290 const struct GNUNET_SCHEDULER_TaskContext *tc)
299 GNUNET_ARM_stop_service (h, term, (0 == timeout.rel_value) ? STOP_TIMEOUT : timeout, &confirm_cb, term);
304 if ((end) || (restart))
306 GNUNET_ARM_stop_service (h, "arm", (0 == timeout.rel_value) ? STOP_TIMEOUT_ARM : timeout, &confirm_cb, "arm");
313 GNUNET_ARM_start_service (h, "arm", (0 == timeout.rel_value) ? START_TIMEOUT : timeout, &confirm_cb, "arm");
320 GNUNET_ARM_start_service (h, init, (0 == timeout.rel_value) ? START_TIMEOUT : timeout, &confirm_cb, init);
327 GNUNET_CLIENT_service_test (test, cfg, (0 == timeout.rel_value) ? TEST_TIMEOUT : timeout, &confirm_task, test);
334 GNUNET_ARM_disconnect (h);
339 h = GNUNET_ARM_connect (cfg, NULL);
342 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
343 _("Fatal error initializing ARM API.\n"));
347 GNUNET_SCHEDULER_add_now(&cps_loop, NULL);
351 default: /* last phase */
352 GNUNET_ARM_disconnect (h);
353 if ((end == GNUNET_YES) && (delete == GNUNET_YES))
362 * The main function to obtain arm from gnunetd.
364 * @param argc number of arguments from the command line
365 * @param argv command line arguments
366 * @return 0 ok, 1 on error
369 main (int argc, char *const *argv)
371 static unsigned long long temp_timeout_ms;
373 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
374 {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
375 GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
376 {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
377 GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
378 {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
379 GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
380 {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
381 GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
382 {'r', "restart", NULL, gettext_noop ("stop and start all GNUnet default services"),
383 GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
384 {'t', "test", "SERVICE",
385 gettext_noop ("test if a particular service is running"),
386 GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
387 {'d', "delete", NULL, gettext_noop ("delete config file and directory on exit"),
388 GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
389 {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
390 GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
391 {'T', "timeout", NULL, gettext_noop ("timeout for completing current operation"),
392 GNUNET_YES, &GNUNET_GETOPT_set_ulong, &temp_timeout_ms},
393 GNUNET_GETOPT_OPTION_END
396 if (temp_timeout_ms > 0)
397 timeout.rel_value = temp_timeout_ms;
399 if (GNUNET_OK == GNUNET_PROGRAM_run (argc,
403 ("Control services and the Automated Restart Manager (ARM)"),
404 options, &run, NULL))
412 /* end of gnunet-arm.c */