longer ARM stop timeouts, powerpc buildbot still has trouble
[oweals/gnunet.git] / src / arm / gnunet-arm.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file arm/gnunet-arm.c
23  * @brief arm for writing a tool
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
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"
33
34 /**
35  * Timeout for stopping services.  Long to give some services a real chance.
36  */
37 #define STOP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
38
39 /**
40  * Timeout for stopping ARM.  Extra-long since ARM needs to stop everyone else.
41  */
42 #define STOP_TIMEOUT_ARM GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
43
44 /**
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)).
48  */
49 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
50
51 /**
52  * Timeout for starting services, very short because of the strange way start works
53  * (by checking if running before starting, so really this time is always waited on
54  * startup (annoying)).
55  */
56 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
57
58 /**
59  * Set if we are to shutdown all services (including ARM).
60  */
61 static int end;
62
63 /**
64  * Set if we are to start default services (including ARM).
65  */
66 static int start;
67
68 /**
69  * Set if we are to stop/start default services (including ARM).
70  */
71 static int restart;
72
73 /**
74  * Set if we should delete configuration and temp directory on exit.
75  */
76 static int delete;
77
78 /**
79  * Set if we should not print status messages.
80  */
81 static int quiet;
82
83 /**
84  * Set to the name of a service to start.
85  */
86 static char *init;
87
88 /**
89  * Set to the name of a service to kill.
90  */
91 static char *term;
92
93 /**
94  * Set to the name of a service to test.
95  */
96 static char *test;
97
98 /**
99  * Set to the name of the config file used.
100  */
101 static const char *config_file;
102
103 /**
104  * Set to the directory where runtime files are stored.
105  */
106 static char *dir;
107
108 /**
109  * Final status code.
110  */
111 static int ret;
112
113 /**
114  * Connection with ARM.
115  */
116 static struct GNUNET_ARM_Handle *h;
117
118 /**
119  * Our configuration.
120  */
121 const struct GNUNET_CONFIGURATION_Handle *cfg;
122
123 /**
124  * Processing stage that we are in.  Simple counter.
125  */
126 static unsigned int phase;
127
128 /**
129  * User defined timestamp for completing operations.
130  */
131 static struct GNUNET_TIME_Relative timeout;
132
133
134 /**
135  * Main continuation-passing-style loop.  Runs the various
136  * jobs that we've been asked to do in order.
137  *
138  * @param cls closure, unused
139  * @param tc context, unused
140  */
141 static void
142 cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
143
144
145 /**
146  * Callback invoked with the status of the last operation.  Reports to the
147  * user and then runs the next phase in the FSM.
148  *
149  * @param cls pointer to "const char*" identifying service that was manipulated
150  * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error
151  */
152 static void
153 confirm_cb (void *cls, int success)
154 {
155   const char *service = cls;
156
157   switch (success)
158   {
159   case GNUNET_OK:
160     if (quiet != GNUNET_YES)
161       fprintf (stdout, _("Service `%s' has been started.\n"), service);
162     if ((phase - 1 != 2) && (phase - 1 != 3))
163     {
164       if (quiet != GNUNET_YES)
165         fprintf (stdout, _("Failed to stop service `%s'!\n"), service);
166       ret = 1;
167     }
168     break;
169   case GNUNET_NO:
170     if (quiet != GNUNET_YES)
171       fprintf (stdout, _("Service `%s' has been stopped.\n"), service);
172     if ((phase - 1 != 0) && (phase - 1 != 1))
173     {
174       if (quiet != GNUNET_YES)
175         fprintf (stdout, _("Failed to start service `%s'!\n"), service);
176       ret = 1;
177     }
178     break;
179   case GNUNET_SYSERR:
180     if (quiet != GNUNET_YES)
181       fprintf (stdout, _("Some error communicating with service `%s'.\n"),
182                service);
183     ret = 1;
184     break;
185   }
186
187   GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
188                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
189 }
190
191
192 /**
193  * Function called to confirm that a service is running (or that
194  * it is not running).
195  *
196  * @param cls pointer to "const char*" identifying service that was manipulated
197  * @param tc reason determines if service is now running
198  */
199 static void
200 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
201 {
202   const char *service = cls;
203
204   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
205   {
206     if (quiet != GNUNET_YES)
207       fprintf (stdout, _("Service `%s' is running.\n"), service);
208   }
209   else
210   {
211     if (quiet != GNUNET_YES)
212       fprintf (stdout, _("Service `%s' is not running.\n"), service);
213   }
214   GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
215                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
216 }
217
218
219 /**
220  * Main function that will be run by the scheduler.
221  *
222  * @param cls closure
223  * @param args remaining command-line arguments
224  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
225  * @param c configuration
226  */
227 static void
228 run (void *cls, char *const *args, const char *cfgfile,
229      const struct GNUNET_CONFIGURATION_Handle *c)
230 {
231   cfg = c;
232   config_file = cfgfile;
233   if (GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME", &dir)
234       != GNUNET_OK)
235   {
236     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237                 _
238                 ("Fatal configuration error: `%s' option in section `%s' missing.\n"),
239                 "SERVICEHOME", "PATHS");
240     return;
241   }
242   h = GNUNET_ARM_connect (cfg, NULL);
243   if (h == NULL)
244   {
245     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246                 _("Fatal error initializing ARM API.\n"));
247     ret = 1;
248     return;
249   }
250   GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
251                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
252 }
253
254 /**
255  * Attempts to delete configuration file and SERVICEHOME
256  * on arm shutdown provided the end and delete options
257  * were specified when gnunet-arm was run.
258  */
259 static void
260 delete_files ()
261 {
262   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263               "Will attempt to remove configuration file %s and service directory %s\n",
264               config_file, dir);
265
266   if (UNLINK (config_file) != 0)
267   {
268     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
269                 _("Failed to remove configuration file %s\n"), config_file);
270   }
271
272   if (GNUNET_DISK_directory_remove (dir) != GNUNET_OK)
273   {
274     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
275                 _("Failed to remove servicehome directory %s\n"), dir);
276
277   }
278 }
279
280 /**
281  * Main continuation-passing-style loop.  Runs the various
282  * jobs that we've been asked to do in order.
283  *
284  * @param cls closure, unused
285  * @param tc context, unused
286  */
287 static void
288 cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
289 {
290   while (1)
291   {
292     switch (phase++)
293     {
294     case 0:
295       if (term != NULL)
296       {
297         GNUNET_ARM_stop_service (h, term,
298                                  (0 ==
299                                   timeout.rel_value) ? STOP_TIMEOUT : timeout,
300                                  &confirm_cb, term);
301         return;
302       }
303       break;
304     case 1:
305       if ((end) || (restart))
306       {
307         GNUNET_ARM_stop_service (h, "arm",
308                                  (0 ==
309                                   timeout.rel_value) ? STOP_TIMEOUT_ARM :
310                                  timeout, &confirm_cb, "arm");
311         return;
312       }
313       break;
314     case 2:
315       if (start)
316       {
317         GNUNET_ARM_start_service (h, "arm",
318                                   (0 ==
319                                    timeout.rel_value) ? START_TIMEOUT : timeout,
320                                   &confirm_cb, "arm");
321         return;
322       }
323       break;
324     case 3:
325       if (init != NULL)
326       {
327         GNUNET_ARM_start_service (h, init,
328                                   (0 ==
329                                    timeout.rel_value) ? START_TIMEOUT : timeout,
330                                   &confirm_cb, init);
331         return;
332       }
333       break;
334     case 4:
335       if (test != NULL)
336       {
337         GNUNET_CLIENT_service_test (test, cfg,
338                                     (0 ==
339                                      timeout.rel_value) ? TEST_TIMEOUT :
340                                     timeout, &confirm_task, test);
341         return;
342       }
343       break;
344     case 5:
345       if (restart)
346       {
347         GNUNET_ARM_disconnect (h);
348         phase = 0;
349         end = 0;
350         start = 1;
351         restart = 0;
352         h = GNUNET_ARM_connect (cfg, NULL);
353         if (h == NULL)
354         {
355           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
356                       _("Fatal error initializing ARM API.\n"));
357           ret = 1;
358           return;
359         }
360         GNUNET_SCHEDULER_add_now (&cps_loop, NULL);
361         return;
362       }
363       /* Fall through */
364     default:                   /* last phase */
365       GNUNET_ARM_disconnect (h);
366       if ((end == GNUNET_YES) && (delete == GNUNET_YES))
367         delete_files ();
368       return;
369     }
370   }
371 }
372
373
374 /**
375  * The main function to obtain arm from gnunetd.
376  *
377  * @param argc number of arguments from the command line
378  * @param argv command line arguments
379  * @return 0 ok, 1 on error
380  */
381 int
382 main (int argc, char *const *argv)
383 {
384   static unsigned long long temp_timeout_ms;
385
386   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
387     {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
388      GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
389     {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
390      GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
391     {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
392      GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
393     {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
394      GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
395     {'r', "restart", NULL,
396      gettext_noop ("stop and start all GNUnet default services"),
397      GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
398     {'t', "test", "SERVICE",
399      gettext_noop ("test if a particular service is running"),
400      GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
401     {'d', "delete", NULL,
402      gettext_noop ("delete config file and directory on exit"),
403      GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
404     {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
405      GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
406     {'T', "timeout", NULL,
407      gettext_noop ("timeout for completing current operation"),
408      GNUNET_YES, &GNUNET_GETOPT_set_ulong, &temp_timeout_ms},
409     GNUNET_GETOPT_OPTION_END
410   };
411
412   if (temp_timeout_ms > 0)
413     timeout.rel_value = temp_timeout_ms;
414
415   if (GNUNET_OK ==
416       GNUNET_PROGRAM_run (argc, argv, "gnunet-arm",
417                           gettext_noop
418                           ("Control services and the Automated Restart Manager (ARM)"),
419                           options, &run, NULL))
420   {
421     return ret;
422   }
423
424   return 1;
425 }
426
427 /* end of gnunet-arm.c */