code clean up
[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 2, 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, 360)
38
39 /**
40  * Timeout for starting services, very short because of the strange way start works
41  * (by checking if running before starting, so really this time is always waited on
42  * startup (annoying)).
43  */
44 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50)
45
46 /**
47  * Timeout for starting services, very short because of the strange way start works
48  * (by checking if running before starting, so really this time is always waited on
49  * startup (annoying)).
50  */
51 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
52
53 /**
54  * Set if we are to shutdown all services (including ARM).
55  */
56 static int end;
57
58 /**
59  * Set if we are to start default services (including ARM).
60  */
61 static int start;
62
63 /**
64  * Set if we are to stop/start default services (including ARM).
65  */
66 static int restart;
67
68 /**
69  * Set if we should delete configuration and temp directory on exit.
70  */
71 static int delete;
72
73 /**
74  * Set if we should not print status messages.
75  */
76 static int quiet;
77
78 /**
79  * Set to the name of a service to start.
80  */
81 static char *init;
82
83 /**
84  * Set to the name of a service to kill.
85  */
86 static char *term;
87
88 /**
89  * Set to the name of a service to test.
90  */
91 static char *test;
92
93 /**
94  * Set to the name of the config file used.
95  */
96 static const char *config_file;
97
98 /**
99  * Set to the directory where runtime files are stored.
100  */
101 static char *dir;
102
103 /**
104  * Final status code.
105  */
106 static int ret;
107
108 /**
109  * Connection with ARM.
110  */
111 static struct GNUNET_ARM_Handle *h;
112
113 /**
114  * Our scheduler.
115  */
116 static struct GNUNET_SCHEDULER_Handle *sched;
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 /**
130  * Main continuation-passing-style loop.  Runs the various
131  * jobs that we've been asked to do in order.
132  *
133  * @param cls closure, unused
134  * @param tc context, unused
135  */
136 static void
137 cps_loop (void *cls,
138           const struct GNUNET_SCHEDULER_TaskContext *tc);
139
140
141 /**
142  * Callback invoked with the status of the last operation.  Reports to the
143  * user and then runs the next phase in the FSM.
144  *
145  * @param cls pointer to "const char*" identifying service that was manipulated
146  * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error
147  */
148 static void
149 confirm_cb (void *cls, int success)
150 {
151   const char *service = cls;
152   switch (success)
153     {
154     case GNUNET_OK:
155       if (quiet != GNUNET_YES)
156         fprintf(stdout, _("Service `%s' is now running.\n"), service);
157       if ((phase - 1 != 2) && (phase - 1 != 3))
158         {
159           if (quiet != GNUNET_YES)
160             fprintf(stdout, _("Failed to stop service `%s'!\n"), service);
161           ret = 1;
162         }
163       break;
164     case GNUNET_NO:
165       if (quiet != GNUNET_YES)
166         fprintf(stdout, _("Service `%s' is not running.\n"), service);
167       if ((phase - 1 != 0) && (phase - 1 != 1))
168         {
169           if (quiet != GNUNET_YES)
170             fprintf(stdout, _("Failed to start service `%s'!\n"), service);
171           ret = 1;
172         }
173       break;
174     case GNUNET_SYSERR:
175       if (quiet != GNUNET_YES)
176         fprintf(stdout,
177                 _("Some error communicating with service `%s'.\n"), service);
178       ret = 1;
179       break;
180     }
181
182   GNUNET_SCHEDULER_add_continuation (sched,
183                                      &cps_loop,
184                                      NULL,
185                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
186 }
187
188
189 /**
190  * Function called to confirm that a service is running (or that
191  * it is not running).
192  *
193  * @param cls pointer to "const char*" identifying service that was manipulated
194  * @param tc reason determines if service is now running
195  */
196 static void
197 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
198 {
199   const char *service = cls;
200
201   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
202     {
203       if (quiet != GNUNET_YES)
204         fprintf(stdout, _("Service `%s' is running.\n"), service);
205     }
206   else
207     {
208       if (quiet != GNUNET_YES)
209         fprintf(stdout, _("Service `%s' is not running.\n"), service);
210     }
211   GNUNET_SCHEDULER_add_continuation (sched,
212                                      &cps_loop,
213                                      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 s the scheduler to use
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,
229      struct GNUNET_SCHEDULER_Handle *s,
230      char *const *args,
231      const char *cfgfile,
232      const struct GNUNET_CONFIGURATION_Handle *c)
233 {
234   sched = s;
235   cfg = c;
236   config_file = cfgfile;
237   if (GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "SERVICEHOME", &dir) != GNUNET_OK)
238   {
239     dir = NULL;
240   }
241
242   h = GNUNET_ARM_connect (cfg, sched, 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 (sched,
251                                      &cps_loop,
252                                      NULL,
253                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
254 }
255
256 /**
257  * Attempts to delete configuration file and SERVICEHOME
258  * on arm shutdown provided the end and delete options
259  * were specified when gnunet-arm was run.
260  */
261 static void delete_files()
262 {
263   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will attempt to remove configuration file %s and service directory %s\n", 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,
288           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, STOP_TIMEOUT, &confirm_cb, term);
298               return;
299             }
300           break;
301         case 1:
302           if ((end) || (restart))
303             {
304               GNUNET_ARM_stop_service (h, "arm", STOP_TIMEOUT, &confirm_cb, "arm");
305               return;
306             }
307           break;
308         case 2:
309           if (start)
310             {
311               GNUNET_ARM_start_service (h, "arm", START_TIMEOUT, &confirm_cb, "arm");
312               return;
313             }
314           break;
315         case 3:
316           if (init != NULL)
317             {
318               GNUNET_ARM_start_service (h, init, START_TIMEOUT, &confirm_cb, init);
319               return;
320             }
321           break;
322         case 4:
323           if (test != NULL)
324             {
325               GNUNET_CLIENT_service_test (sched, test, cfg, TEST_TIMEOUT, &confirm_task, test);
326               return;
327             }
328           break;
329         case 5:
330           if (restart)
331             {
332               GNUNET_ARM_disconnect (h);
333               phase = 0;
334               end = 0;
335               start = 1;
336               restart = 0;
337               h = GNUNET_ARM_connect (cfg, sched, NULL);
338               if (h == NULL)
339                 {
340                   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
341                            _("Fatal error initializing ARM API.\n"));
342                   ret = 1;
343                   return;
344                 }
345               GNUNET_SCHEDULER_add_now(sched, &cps_loop, NULL);
346               return;
347             }
348           /* Fall through */
349         default: /* last phase */
350           GNUNET_ARM_disconnect (h);
351           if ((end == GNUNET_YES) && (delete == GNUNET_YES))
352             delete_files();
353           return;
354         }
355     }
356 }
357
358
359 /**
360  * The main function to obtain arm from gnunetd.
361  *
362  * @param argc number of arguments from the command line
363  * @param argv command line arguments
364  * @return 0 ok, 1 on error
365  */
366 int
367 main (int argc, char *const *argv)
368 {
369   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
370     {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
371      GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
372     {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
373      GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
374     {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
375      GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
376     {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
377      GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
378     {'r', "restart", NULL, gettext_noop ("stop and start all GNUnet default services"),
379      GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
380     {'t', "test", "SERVICE",
381      gettext_noop ("test if a particular service is running"),
382      GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
383     {'d', "delete", NULL, gettext_noop ("delete config file and directory on exit"),
384      GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
385     {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
386      GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
387     GNUNET_GETOPT_OPTION_END
388   };
389   return (GNUNET_OK ==
390           GNUNET_PROGRAM_run (argc,
391                               argv,
392                               "gnunet-arm",
393                               gettext_noop
394                               ("Control services and the Automated Restart Manager (ARM)"),
395                               options, &run, NULL)) ? ret : 1;
396 }
397
398 /* end of gnunet-arm.c */