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 2, 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_getopt_lib.h"
30 #include "gnunet_program_lib.h"
31 #include "gnunet_time_lib.h"
34 * Timeout for all operations.
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50)
39 * Set if we are to shutdown all services (including ARM).
44 * Set if we are to start default services (including ARM).
49 * Set if we should delete configuration and temp directory on exit.
54 * Set to the name of a service to start.
59 * Set to the name of a service to kill.
64 * Set to the name of a service to test.
69 * Set to the name of the config file used.
71 static const char *config_file;
74 * Set to the directory where runtime files are stored.
84 * Connection with ARM.
86 static struct GNUNET_ARM_Handle *h;
91 static struct GNUNET_SCHEDULER_Handle *sched;
96 const struct GNUNET_CONFIGURATION_Handle *cfg;
99 * Processing stage that we are in. Simple counter.
101 static unsigned int phase;
105 * Main continuation-passing-style loop. Runs the various
106 * jobs that we've been asked to do in order.
108 * @param cls closure, unused
109 * @param tc context, unused
113 const struct GNUNET_SCHEDULER_TaskContext *tc);
117 * Callback invoked with the status of the last operation. Reports to the
118 * user and then runs the next phase in the FSM.
120 * @param cls pointer to "const char*" identifying service that was manipulated
121 * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error
124 confirm_cb (void *cls, int success)
126 const char *service = cls;
130 fprintf (stdout, _("Service `%s' is now running.\n"), service);
133 fprintf (stdout, _("Service `%s' is not running.\n"), service);
137 _("Error updating service `%s': ARM not running\n"), service);
140 GNUNET_SCHEDULER_add_continuation (sched,
143 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
148 * Function called to confirm that a service is running (or that
149 * it is not running).
151 * @param cls pointer to "const char*" identifying service that was manipulated
152 * @param tc reason determines if service is now running
155 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 const char *service = cls;
159 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
160 fprintf (stdout, _("Service `%s' is running.\n"), service);
162 fprintf (stdout, _("Service `%s' is not running.\n"), service);
163 GNUNET_SCHEDULER_add_continuation (sched,
166 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
171 * Main function that will be run by the scheduler.
174 * @param s the scheduler to use
175 * @param args remaining command-line arguments
176 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
177 * @param c configuration
181 struct GNUNET_SCHEDULER_Handle *s,
184 const struct GNUNET_CONFIGURATION_Handle *c)
188 config_file = cfgfile;
189 if (GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "SERVICEHOME", &dir) != GNUNET_OK)
194 h = GNUNET_ARM_connect (cfg, sched, NULL);
198 _("Fatal error initializing ARM API.\n"));
202 GNUNET_SCHEDULER_add_continuation (sched,
205 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
209 * Attempts to delete configuration file and SERVICEHOME
210 * on arm shutdown provided the end and delete options
211 * were specified when gnunet-arm was run.
213 static void delete_files()
215 fprintf(stderr, "Will attempt to remove configuration file %s and service directory %s\n", config_file, dir);
217 if (UNLINK(config_file) != 0)
220 _("Failed to remove configuration file %s\n"), config_file);
223 if (GNUNET_DISK_directory_remove(dir) != GNUNET_OK)
226 _("Failed to remove servicehome directory %s\n"), dir);
232 * Main continuation-passing-style loop. Runs the various
233 * jobs that we've been asked to do in order.
235 * @param cls closure, unused
236 * @param tc context, unused
240 const struct GNUNET_SCHEDULER_TaskContext *tc)
249 GNUNET_ARM_stop_service (h, term, TIMEOUT, &confirm_cb, term);
256 GNUNET_ARM_stop_service (h, "arm", TIMEOUT, &confirm_cb, "arm");
263 GNUNET_ARM_start_service (h, "arm", TIMEOUT, &confirm_cb, "arm");
270 GNUNET_ARM_start_service (h, init, TIMEOUT, &confirm_cb, init);
277 GNUNET_CLIENT_service_test (sched, test, cfg, TIMEOUT, &confirm_task, test);
281 default: /* last phase */
282 GNUNET_ARM_disconnect (h);
283 if ((end == GNUNET_YES) && (delete == GNUNET_YES))
292 * gnunet-arm command line options
294 static struct GNUNET_GETOPT_CommandLineOption options[] = {
295 {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
296 GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
297 {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
298 GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
299 {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
300 GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
301 {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
302 GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
303 {'t', "test", "SERVICE",
304 gettext_noop ("test if a particular service is running"),
305 GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
306 {'d', "delete", NULL, gettext_noop ("delete config file and directory on exit"),
307 GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
308 GNUNET_GETOPT_OPTION_END
313 * The main function to obtain arm from gnunetd.
315 * @param argc number of arguments from the command line
316 * @param argv command line arguments
317 * @return 0 ok, 1 on error
320 main (int argc, char *const *argv)
323 GNUNET_PROGRAM_run (argc,
327 ("Control services and the Automated Restart Manager (ARM)"),
328 options, &run, NULL)) ? ret : 1;
331 /* end of gnunet-arm.c */