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 if we should not print status messages.
59 * Set to the name of a service to start.
64 * Set to the name of a service to kill.
69 * Set to the name of a service to test.
74 * Set to the name of the config file used.
76 static const char *config_file;
79 * Set to the directory where runtime files are stored.
89 * Connection with ARM.
91 static struct GNUNET_ARM_Handle *h;
96 static struct GNUNET_SCHEDULER_Handle *sched;
101 const struct GNUNET_CONFIGURATION_Handle *cfg;
104 * Processing stage that we are in. Simple counter.
106 static unsigned int phase;
110 * Main continuation-passing-style loop. Runs the various
111 * jobs that we've been asked to do in order.
113 * @param cls closure, unused
114 * @param tc context, unused
118 const struct GNUNET_SCHEDULER_TaskContext *tc);
122 * Callback invoked with the status of the last operation. Reports to the
123 * user and then runs the next phase in the FSM.
125 * @param cls pointer to "const char*" identifying service that was manipulated
126 * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error
129 confirm_cb (void *cls, int success)
131 const char *service = cls;
135 if (quiet != GNUNET_YES)
136 fprintf(stdout, _("Service `%s' is now running.\n"), service);
139 if (quiet != GNUNET_YES)
140 fprintf(stdout, _("Service `%s' is not running.\n"), service);
143 if (quiet != GNUNET_YES)
145 _("Error updating service `%s': ARM not running\n"), service);
148 GNUNET_SCHEDULER_add_continuation (sched,
151 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
156 * Function called to confirm that a service is running (or that
157 * it is not running).
159 * @param cls pointer to "const char*" identifying service that was manipulated
160 * @param tc reason determines if service is now running
163 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
165 const char *service = cls;
167 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
169 if (quiet != GNUNET_YES)
170 fprintf(stdout, _("Service `%s' is running.\n"), service);
174 if (quiet != GNUNET_YES)
175 fprintf(stdout, _("Service `%s' is not running.\n"), service);
177 GNUNET_SCHEDULER_add_continuation (sched,
180 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
185 * Main function that will be run by the scheduler.
188 * @param s the scheduler to use
189 * @param args remaining command-line arguments
190 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
191 * @param c configuration
195 struct GNUNET_SCHEDULER_Handle *s,
198 const struct GNUNET_CONFIGURATION_Handle *c)
202 config_file = cfgfile;
203 if (GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "SERVICEHOME", &dir) != GNUNET_OK)
208 h = GNUNET_ARM_connect (cfg, sched, NULL);
211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212 _("Fatal error initializing ARM API.\n"));
216 GNUNET_SCHEDULER_add_continuation (sched,
219 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
223 * Attempts to delete configuration file and SERVICEHOME
224 * on arm shutdown provided the end and delete options
225 * were specified when gnunet-arm was run.
227 static void delete_files()
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will attempt to remove configuration file %s and service directory %s\n", config_file, dir);
231 if (UNLINK(config_file) != 0)
233 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
234 _("Failed to remove configuration file %s\n"), config_file);
237 if (GNUNET_DISK_directory_remove(dir) != GNUNET_OK)
239 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
240 _("Failed to remove servicehome directory %s\n"), dir);
246 * Main continuation-passing-style loop. Runs the various
247 * jobs that we've been asked to do in order.
249 * @param cls closure, unused
250 * @param tc context, unused
254 const struct GNUNET_SCHEDULER_TaskContext *tc)
263 GNUNET_ARM_stop_service (h, term, TIMEOUT, &confirm_cb, term);
270 GNUNET_ARM_stop_service (h, "arm", TIMEOUT, &confirm_cb, "arm");
277 GNUNET_ARM_start_service (h, "arm", TIMEOUT, &confirm_cb, "arm");
284 GNUNET_ARM_start_service (h, init, TIMEOUT, &confirm_cb, init);
291 GNUNET_CLIENT_service_test (sched, test, cfg, TIMEOUT, &confirm_task, test);
295 default: /* last phase */
296 GNUNET_ARM_disconnect (h);
297 if ((end == GNUNET_YES) && (delete == GNUNET_YES))
306 * gnunet-arm command line options
308 static struct GNUNET_GETOPT_CommandLineOption options[] = {
309 {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
310 GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
311 {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
312 GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
313 {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
314 GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
315 {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
316 GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
317 {'t', "test", "SERVICE",
318 gettext_noop ("test if a particular service is running"),
319 GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
320 {'d', "delete", NULL, gettext_noop ("delete config file and directory on exit"),
321 GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
322 {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
323 GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
324 GNUNET_GETOPT_OPTION_END
329 * The main function to obtain arm from gnunetd.
331 * @param argc number of arguments from the command line
332 * @param argv command line arguments
333 * @return 0 ok, 1 on error
336 main (int argc, char *const *argv)
339 GNUNET_PROGRAM_run (argc,
343 ("Control services and the Automated Restart Manager (ARM)"),
344 options, &run, NULL)) ? ret : 1;
347 /* end of gnunet-arm.c */