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_MINUTES, 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_MINUTES, 2)
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_SECONDS, 1)
52 * Timeout for listing all running services.
54 #define LIST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
57 * Set if we are to shutdown all services (including ARM).
62 * Set if we are to start default services (including ARM).
67 * Set if we are to stop/start default services (including ARM).
72 * Set if we should delete configuration and temp directory on exit.
77 * Set if we should not print status messages.
82 * Set if we should print a list of currently running services.
87 * Set to the name of a service to start.
92 * Set to the name of a service to kill.
97 * Set to the name of the config file used.
99 static const char *config_file;
102 * Set to the directory where runtime files are stored.
112 * Connection with ARM.
114 static struct GNUNET_ARM_Handle *h;
119 static const struct GNUNET_CONFIGURATION_Handle *cfg;
122 * Processing stage that we are in. Simple counter.
124 static unsigned int phase;
127 * User defined timestamp for completing operations.
129 static struct GNUNET_TIME_Relative timeout;
133 * Main continuation-passing-style loop. Runs the various
134 * jobs that we've been asked to do in order.
136 * @param cls closure, unused
137 * @param tc context, unused
140 cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
144 * Callback invoked with the status of the last operation. Reports to the
145 * user and then runs the next phase in the FSM.
147 * @param cls pointer to "const char*" identifying service that was manipulated
148 * @param result result of the operation
151 confirm_cb (void *cls,
152 enum GNUNET_ARM_ProcessStatus result)
154 const char *service = cls;
158 case GNUNET_ARM_PROCESS_UNKNOWN:
159 FPRINTF (stderr, _("Service `%s' is unknown to ARM.\n"), service);
162 case GNUNET_ARM_PROCESS_DOWN:
163 if (quiet != GNUNET_YES)
164 FPRINTF (stdout, _("Service `%s' has been stopped.\n"), service);
166 case GNUNET_ARM_PROCESS_ALREADY_RUNNING:
167 FPRINTF (stderr, _("Service `%s' was already running.\n"), service);
170 case GNUNET_ARM_PROCESS_STARTING:
171 if (quiet != GNUNET_YES)
172 FPRINTF (stdout, _("Service `%s' has been started.\n"), service);
174 case GNUNET_ARM_PROCESS_ALREADY_STOPPING:
175 FPRINTF (stderr, _("Service `%s' was already being stopped.\n"), service);
178 case GNUNET_ARM_PROCESS_ALREADY_DOWN:
179 FPRINTF (stderr, _("Service `%s' was already not running.\n"), service);
182 case GNUNET_ARM_PROCESS_SHUTDOWN:
183 FPRINTF (stderr, "%s", _("Request ignored as ARM is shutting down.\n"));
186 case GNUNET_ARM_PROCESS_COMMUNICATION_ERROR:
187 FPRINTF (stderr, "%s", _("Error communicating with ARM service.\n"));
190 case GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT:
191 FPRINTF (stderr, "%s", _("Timeout communicating with ARM service.\n"));
194 case GNUNET_ARM_PROCESS_FAILURE:
195 FPRINTF (stderr, "%s", _("Operation failed.\n"));
199 FPRINTF (stderr, "%s", _("Unknown response code from ARM.\n"));
202 GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
203 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
207 * Callback invoked with the list of running services.
208 * Reports to the user and then runs the next phase in the FSM.
210 * @param cls currently not used
211 * @param result result of the operation
212 * @param count number of running services
213 * @param list copy of the list of running services
216 list_cb (void *cls, int result, unsigned int count, const char *const*list)
220 if ( (result != GNUNET_YES) || (NULL == list) )
222 FPRINTF (stderr, "%s", _("Error communicating with ARM. ARM not running?\n"));
225 FPRINTF (stdout, "%s", _("Running services:\n"));
226 for (i=0; i<count; i++)
227 FPRINTF (stdout, "%s\n", list[i]);
231 * Main function that will be run by the scheduler.
234 * @param args remaining command-line arguments
235 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
236 * @param c configuration
239 run (void *cls, char *const *args, const char *cfgfile,
240 const struct GNUNET_CONFIGURATION_Handle *c)
243 config_file = cfgfile;
244 if (GNUNET_CONFIGURATION_get_value_string
245 (cfg, "PATHS", "SERVICEHOME", &dir) != GNUNET_OK)
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
249 ("Fatal configuration error: `%s' option in section `%s' missing.\n"),
250 "SERVICEHOME", "PATHS");
253 h = GNUNET_ARM_connect (cfg, NULL);
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
257 _("Fatal error initializing ARM API.\n"));
261 GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
262 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
266 * Attempts to delete configuration file and SERVICEHOME
267 * on arm shutdown provided the end and delete options
268 * were specified when gnunet-arm was run.
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "Will attempt to remove configuration file %s and service directory %s\n",
277 if (UNLINK (config_file) != 0)
279 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
280 _("Failed to remove configuration file %s\n"), config_file);
283 if (GNUNET_DISK_directory_remove (dir) != GNUNET_OK)
285 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
286 _("Failed to remove servicehome directory %s\n"), dir);
292 * Main continuation-passing-style loop. Runs the various
293 * jobs that we've been asked to do in order.
295 * @param cls closure, unused
296 * @param tc context, unused
299 cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
308 GNUNET_ARM_stop_service (h, term,
310 timeout.rel_value) ? STOP_TIMEOUT :
311 timeout, &confirm_cb, term);
316 if ((end) || (restart))
318 GNUNET_ARM_stop_service (h, "arm",
320 timeout.rel_value) ? STOP_TIMEOUT_ARM
321 : timeout, &confirm_cb, "arm");
328 GNUNET_ARM_start_service (h, "arm",
330 timeout.rel_value) ? START_TIMEOUT :
331 timeout, &confirm_cb, "arm");
338 GNUNET_ARM_start_service (h, init,
340 timeout.rel_value) ? START_TIMEOUT :
341 timeout, &confirm_cb, init);
348 GNUNET_ARM_disconnect (h);
353 h = GNUNET_ARM_connect (cfg, NULL);
356 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
357 _("Fatal error initializing ARM API.\n"));
361 GNUNET_SCHEDULER_add_now (&cps_loop, NULL);
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "Going to list all running services controlled by ARM.\n");
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372 _("Fatal error initializing ARM API.\n"));
376 GNUNET_ARM_list_running_services (h,
378 timeout.rel_value) ? LIST_TIMEOUT :
379 timeout, &list_cb, NULL);
383 default: /* last phase */
384 GNUNET_ARM_disconnect (h);
385 if ((end == GNUNET_YES) && (delete == GNUNET_YES))
394 * The main function to obtain arm from gnunetd.
396 * @param argc number of arguments from the command line
397 * @param argv command line arguments
398 * @return 0 ok, 1 on error
401 main (int argc, char *const *argv)
403 static unsigned long long temp_timeout_ms;
405 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
406 {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
407 GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
408 {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
409 GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
410 {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
411 GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
412 {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
413 GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
414 {'r', "restart", NULL,
415 gettext_noop ("stop and start all GNUnet default services"),
416 GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
417 {'d', "delete", NULL,
418 gettext_noop ("delete config file and directory on exit"),
419 GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
420 {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
421 GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
422 {'T', "timeout", NULL,
423 gettext_noop ("timeout for completing current operation"),
424 GNUNET_YES, &GNUNET_GETOPT_set_ulong, &temp_timeout_ms},
425 {'I', "info", NULL, gettext_noop ("List currently running services"),
426 GNUNET_NO, &GNUNET_GETOPT_set_one, &list},
427 GNUNET_GETOPT_OPTION_END
430 if (temp_timeout_ms > 0)
431 timeout.rel_value = temp_timeout_ms;
434 GNUNET_PROGRAM_run (argc, argv, "gnunet-arm",
436 ("Control services and the Automated Restart Manager (ARM)"),
437 options, &run, NULL))
445 /* end of gnunet-arm.c */