indentation
[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_SECONDS, 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_SECONDS, 3)
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 cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
142
143
144 /**
145  * Callback invoked with the status of the last operation.  Reports to the
146  * user and then runs the next phase in the FSM.
147  *
148  * @param cls pointer to "const char*" identifying service that was manipulated
149  * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error
150  */
151 static void
152 confirm_cb (void *cls, int success)
153 {
154   const char *service = cls;
155
156   switch (success)
157   {
158   case GNUNET_OK:
159     if (quiet != GNUNET_YES)
160       fprintf (stdout, _("Service `%s' has been started.\n"), service);
161     if ((phase - 1 != 2) && (phase - 1 != 3))
162     {
163       if (quiet != GNUNET_YES)
164         fprintf (stdout, _("Failed to stop service `%s'!\n"), service);
165       ret = 1;
166     }
167     break;
168   case GNUNET_NO:
169     if (quiet != GNUNET_YES)
170       fprintf (stdout, _("Service `%s' has been stopped.\n"), service);
171     if ((phase - 1 != 0) && (phase - 1 != 1))
172     {
173       if (quiet != GNUNET_YES)
174         fprintf (stdout, _("Failed to start service `%s'!\n"), service);
175       ret = 1;
176     }
177     break;
178   case GNUNET_SYSERR:
179     if (quiet != GNUNET_YES)
180       fprintf (stdout, _("Some error communicating with service `%s'.\n"),
181                service);
182     ret = 1;
183     break;
184   }
185
186   GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
187                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
188 }
189
190
191 /**
192  * Function called to confirm that a service is running (or that
193  * it is not running).
194  *
195  * @param cls pointer to "const char*" identifying service that was manipulated
196  * @param tc reason determines if service is now running
197  */
198 static void
199 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
200 {
201   const char *service = cls;
202
203   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
204   {
205     if (quiet != GNUNET_YES)
206       fprintf (stdout, _("Service `%s' is running.\n"), service);
207   }
208   else
209   {
210     if (quiet != GNUNET_YES)
211       fprintf (stdout, _("Service `%s' is not running.\n"), service);
212   }
213   GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
214                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
215 }
216
217
218 /**
219  * Main function that will be run by the scheduler.
220  *
221  * @param cls closure
222  * @param args remaining command-line arguments
223  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
224  * @param c configuration
225  */
226 static void
227 run (void *cls, char *const *args, const char *cfgfile,
228      const struct GNUNET_CONFIGURATION_Handle *c)
229 {
230   cfg = c;
231   config_file = cfgfile;
232   if (GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME", &dir)
233       != GNUNET_OK)
234   {
235     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236                 _
237                 ("Fatal configuration error: `%s' option in section `%s' missing.\n"),
238                 "SERVICEHOME", "PATHS");
239     return;
240   }
241   h = GNUNET_ARM_connect (cfg, NULL);
242   if (h == NULL)
243   {
244     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
245                 _("Fatal error initializing ARM API.\n"));
246     ret = 1;
247     return;
248   }
249   GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL,
250                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
251 }
252
253 /**
254  * Attempts to delete configuration file and SERVICEHOME
255  * on arm shutdown provided the end and delete options
256  * were specified when gnunet-arm was run.
257  */
258 static void
259 delete_files ()
260 {
261   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262               "Will attempt to remove configuration file %s and service directory %s\n",
263               config_file, dir);
264
265   if (UNLINK (config_file) != 0)
266   {
267     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
268                 _("Failed to remove configuration file %s\n"), config_file);
269   }
270
271   if (GNUNET_DISK_directory_remove (dir) != GNUNET_OK)
272   {
273     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
274                 _("Failed to remove servicehome directory %s\n"), dir);
275
276   }
277 }
278
279 /**
280  * Main continuation-passing-style loop.  Runs the various
281  * jobs that we've been asked to do in order.
282  *
283  * @param cls closure, unused
284  * @param tc context, unused
285  */
286 static void
287 cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
288 {
289   while (1)
290   {
291     switch (phase++)
292     {
293     case 0:
294       if (term != NULL)
295       {
296         GNUNET_ARM_stop_service (h, term,
297                                  (0 ==
298                                   timeout.rel_value) ? STOP_TIMEOUT : timeout,
299                                  &confirm_cb, term);
300         return;
301       }
302       break;
303     case 1:
304       if ((end) || (restart))
305       {
306         GNUNET_ARM_stop_service (h, "arm",
307                                  (0 ==
308                                   timeout.
309                                   rel_value) ? STOP_TIMEOUT_ARM : timeout,
310                                  &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.
340                                      rel_value) ? TEST_TIMEOUT : timeout,
341                                     &confirm_task, test);
342         return;
343       }
344       break;
345     case 5:
346       if (restart)
347       {
348         GNUNET_ARM_disconnect (h);
349         phase = 0;
350         end = 0;
351         start = 1;
352         restart = 0;
353         h = GNUNET_ARM_connect (cfg, NULL);
354         if (h == NULL)
355         {
356           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
357                       _("Fatal error initializing ARM API.\n"));
358           ret = 1;
359           return;
360         }
361         GNUNET_SCHEDULER_add_now (&cps_loop, NULL);
362         return;
363       }
364       /* Fall through */
365     default:                   /* last phase */
366       GNUNET_ARM_disconnect (h);
367       if ((end == GNUNET_YES) && (delete == GNUNET_YES))
368         delete_files ();
369       return;
370     }
371   }
372 }
373
374
375 /**
376  * The main function to obtain arm from gnunetd.
377  *
378  * @param argc number of arguments from the command line
379  * @param argv command line arguments
380  * @return 0 ok, 1 on error
381  */
382 int
383 main (int argc, char *const *argv)
384 {
385   static unsigned long long temp_timeout_ms;
386
387   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
388     {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
389      GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
390     {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
391      GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
392     {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
393      GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
394     {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
395      GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
396     {'r', "restart", NULL,
397      gettext_noop ("stop and start all GNUnet default services"),
398      GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
399     {'t', "test", "SERVICE",
400      gettext_noop ("test if a particular service is running"),
401      GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
402     {'d', "delete", NULL,
403      gettext_noop ("delete config file and directory on exit"),
404      GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
405     {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
406      GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
407     {'T', "timeout", NULL,
408      gettext_noop ("timeout for completing current operation"),
409      GNUNET_YES, &GNUNET_GETOPT_set_ulong, &temp_timeout_ms},
410     GNUNET_GETOPT_OPTION_END
411   };
412
413   if (temp_timeout_ms > 0)
414     timeout.rel_value = temp_timeout_ms;
415
416   if (GNUNET_OK ==
417       GNUNET_PROGRAM_run (argc, argv, "gnunet-arm",
418                           gettext_noop
419                           ("Control services and the Automated Restart Manager (ARM)"),
420                           options, &run, NULL))
421   {
422     return ret;
423   }
424
425   return 1;
426 }
427
428 /* end of gnunet-arm.c */