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 to the name of a service to start.
54 * Set to the name of a service to kill.
59 * Set to the name of a service to test.
69 * Connection with ARM.
71 static struct GNUNET_ARM_Handle *h;
76 static struct GNUNET_SCHEDULER_Handle *sched;
81 const struct GNUNET_CONFIGURATION_Handle *cfg;
84 * Processing stage that we are in. Simple counter.
86 static unsigned int phase;
90 * Main continuation-passing-style loop. Runs the various
91 * jobs that we've been asked to do in order.
93 * @param cls closure, unused
94 * @param tc context, unused
98 const struct GNUNET_SCHEDULER_TaskContext *tc);
102 * Callback invoked with the status of the last operation. Reports to the
103 * user and then runs the next phase in the FSM.
105 * @param cls pointer to "const char*" identifying service that was manipulated
106 * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error
109 confirm_cb (void *cls, int success)
111 const char *service = cls;
115 fprintf (stdout, _("Service `%s' is now running.\n"), service);
118 fprintf (stdout, _("Service `%s' is not running.\n"), service);
122 _("Error updating service `%s': ARM not running\n"), service);
125 GNUNET_SCHEDULER_add_continuation (sched,
128 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
133 * Function called to confirm that a service is running (or that
134 * it is not running).
136 * @param cls pointer to "const char*" identifying service that was manipulated
137 * @param tc reason determines if service is now running
140 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
142 const char *service = cls;
144 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
145 fprintf (stdout, _("Service `%s' is running.\n"), service);
147 fprintf (stdout, _("Service `%s' is not running.\n"), service);
148 GNUNET_SCHEDULER_add_continuation (sched,
151 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
156 * Main function that will be run by the scheduler.
159 * @param s the scheduler to use
160 * @param args remaining command-line arguments
161 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
162 * @param c configuration
166 struct GNUNET_SCHEDULER_Handle *s,
169 const struct GNUNET_CONFIGURATION_Handle *c)
173 h = GNUNET_ARM_connect (cfg, sched, NULL);
177 _("Fatal error initializing ARM API.\n"));
181 GNUNET_SCHEDULER_add_continuation (sched,
184 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
189 * Main continuation-passing-style loop. Runs the various
190 * jobs that we've been asked to do in order.
192 * @param cls closure, unused
193 * @param tc context, unused
197 const struct GNUNET_SCHEDULER_TaskContext *tc)
206 GNUNET_ARM_stop_service (h, term, TIMEOUT, &confirm_cb, term);
213 GNUNET_ARM_stop_service (h, "arm", TIMEOUT, &confirm_cb, "arm");
220 GNUNET_ARM_start_service (h, "arm", TIMEOUT, &confirm_cb, "arm");
227 GNUNET_ARM_start_service (h, init, TIMEOUT, &confirm_cb, init);
234 GNUNET_CLIENT_service_test (sched, test, cfg, TIMEOUT, &confirm_task, test);
238 default: /* last phase */
239 GNUNET_ARM_disconnect (h);
247 * gnunet-arm command line options
249 static struct GNUNET_GETOPT_CommandLineOption options[] = {
250 {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
251 GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
252 {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
253 GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
254 {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
255 GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
256 {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
257 GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
258 {'t', "test", "SERVICE",
259 gettext_noop ("test if a particular service is running"),
260 GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
261 GNUNET_GETOPT_OPTION_END
266 * The main function to obtain arm from gnunetd.
268 * @param argc number of arguments from the command line
269 * @param argv command line arguments
270 * @return 0 ok, 1 on error
273 main (int argc, char *const *argv)
276 GNUNET_PROGRAM_run (argc,
280 ("Control services and the Automated Restart Manager (ARM)"),
281 options, &run, NULL)) ? ret : 1;
284 /* end of gnunet-arm.c */