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 stopping services. Long to give some services a real chance.
36 #define STOP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 360)
39 * Timeout for starting services, very short because of the strange way start works
40 * (by checking if running before starting, so really this time is always waited on
41 * startup (annoying)).
43 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50)
46 * Set if we are to shutdown all services (including ARM).
51 * Set if we are to start default services (including ARM).
56 * Set if we are to stop/start default services (including ARM).
61 * Set if we should delete configuration and temp directory on exit.
66 * Set if we should not print status messages.
71 * Set to the name of a service to start.
76 * Set to the name of a service to kill.
81 * Set to the name of a service to test.
86 * Set to the name of the config file used.
88 static const char *config_file;
91 * Set to the directory where runtime files are stored.
101 * Connection with ARM.
103 static struct GNUNET_ARM_Handle *h;
108 static struct GNUNET_SCHEDULER_Handle *sched;
113 const struct GNUNET_CONFIGURATION_Handle *cfg;
116 * Processing stage that we are in. Simple counter.
118 static unsigned int phase;
122 * Main continuation-passing-style loop. Runs the various
123 * jobs that we've been asked to do in order.
125 * @param cls closure, unused
126 * @param tc context, unused
130 const struct GNUNET_SCHEDULER_TaskContext *tc);
134 * Callback invoked with the status of the last operation. Reports to the
135 * user and then runs the next phase in the FSM.
137 * @param cls pointer to "const char*" identifying service that was manipulated
138 * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error
141 confirm_cb (void *cls, int success)
143 const char *service = cls;
147 if (quiet != GNUNET_YES)
148 fprintf(stdout, _("Service `%s' is now running.\n"), service);
149 if ((phase - 1 != 2) && (phase - 1 != 3))
151 if (quiet != GNUNET_YES)
152 fprintf(stdout, _("Failed to stop service `%s'!\n"), service);
157 if (quiet != GNUNET_YES)
158 fprintf(stdout, _("Service `%s' is not running.\n"), service);
159 if ((phase - 1 != 0) && (phase - 1 != 1))
161 if (quiet != GNUNET_YES)
162 fprintf(stdout, _("Failed to start service `%s'!\n"), service);
167 if (quiet != GNUNET_YES)
169 _("Some error communicating with service `%s'.\n"), service);
174 GNUNET_SCHEDULER_add_continuation (sched,
177 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
182 * Function called to confirm that a service is running (or that
183 * it is not running).
185 * @param cls pointer to "const char*" identifying service that was manipulated
186 * @param tc reason determines if service is now running
189 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
191 const char *service = cls;
193 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
195 if (quiet != GNUNET_YES)
196 fprintf(stdout, _("Service `%s' is running.\n"), service);
200 if (quiet != GNUNET_YES)
201 fprintf(stdout, _("Service `%s' is not running.\n"), service);
203 GNUNET_SCHEDULER_add_continuation (sched,
206 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
211 * Main function that will be run by the scheduler.
214 * @param s the scheduler to use
215 * @param args remaining command-line arguments
216 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
217 * @param c configuration
221 struct GNUNET_SCHEDULER_Handle *s,
224 const struct GNUNET_CONFIGURATION_Handle *c)
228 config_file = cfgfile;
229 if (GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "SERVICEHOME", &dir) != GNUNET_OK)
234 h = GNUNET_ARM_connect (cfg, sched, NULL);
237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238 _("Fatal error initializing ARM API.\n"));
242 GNUNET_SCHEDULER_add_continuation (sched,
245 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
249 * Attempts to delete configuration file and SERVICEHOME
250 * on arm shutdown provided the end and delete options
251 * were specified when gnunet-arm was run.
253 static void delete_files()
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will attempt to remove configuration file %s and service directory %s\n", config_file, dir);
257 if (UNLINK(config_file) != 0)
259 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
260 _("Failed to remove configuration file %s\n"), config_file);
263 if (GNUNET_DISK_directory_remove(dir) != GNUNET_OK)
265 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
266 _("Failed to remove servicehome directory %s\n"), dir);
272 * Main continuation-passing-style loop. Runs the various
273 * jobs that we've been asked to do in order.
275 * @param cls closure, unused
276 * @param tc context, unused
280 const struct GNUNET_SCHEDULER_TaskContext *tc)
289 GNUNET_ARM_stop_service (h, term, STOP_TIMEOUT, &confirm_cb, term);
294 if ((end) || (restart))
296 GNUNET_ARM_stop_service (h, "arm", STOP_TIMEOUT, &confirm_cb, "arm");
303 GNUNET_ARM_start_service (h, "arm", START_TIMEOUT, &confirm_cb, "arm");
310 GNUNET_ARM_start_service (h, init, START_TIMEOUT, &confirm_cb, init);
317 GNUNET_CLIENT_service_test (sched, test, cfg, START_TIMEOUT, &confirm_task, test);
324 GNUNET_ARM_disconnect (h);
329 h = GNUNET_ARM_connect (cfg, sched, NULL);
332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
333 _("Fatal error initializing ARM API.\n"));
337 GNUNET_SCHEDULER_add_now(sched, &cps_loop, NULL);
341 default: /* last phase */
342 GNUNET_ARM_disconnect (h);
343 if ((end == GNUNET_YES) && (delete == GNUNET_YES))
352 * gnunet-arm command line options
354 static struct GNUNET_GETOPT_CommandLineOption options[] = {
355 {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
356 GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
357 {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
358 GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
359 {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
360 GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
361 {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
362 GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
363 {'r', "restart", NULL, gettext_noop ("stop and start all GNUnet default services"),
364 GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
365 {'t', "test", "SERVICE",
366 gettext_noop ("test if a particular service is running"),
367 GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
368 {'d', "delete", NULL, gettext_noop ("delete config file and directory on exit"),
369 GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
370 {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
371 GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
372 GNUNET_GETOPT_OPTION_END
377 * The main function to obtain arm from gnunetd.
379 * @param argc number of arguments from the command line
380 * @param argv command line arguments
381 * @return 0 ok, 1 on error
384 main (int argc, char *const *argv)
387 GNUNET_PROGRAM_run (argc,
391 ("Control services and the Automated Restart Manager (ARM)"),
392 options, &run, NULL)) ? ret : 1;
395 /* end of gnunet-arm.c */