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