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