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