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