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