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 2, 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_getopt_lib.h"
30 #include "gnunet_program_lib.h"
31 #include "gnunet_time_lib.h"
32
33 /**
34  * Timeout for all operations.
35  */
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50)
37
38 /**
39  * Set if we are to shutdown all services (including ARM).
40  */
41 static int end;
42
43 /**
44  * Set if we are to start default services (including ARM).
45  */
46 static int start;
47
48 /**
49  * Set if we should delete configuration and temp directory on exit.
50  */
51 static int delete;
52
53 /**
54  * Set if we should not print status messages.
55  */
56 static int quiet;
57
58 /**
59  * Set to the name of a service to start.
60  */
61 static char *init;
62
63 /**
64  * Set to the name of a service to kill.
65  */
66 static char *term;
67
68 /**
69  * Set to the name of a service to test.
70  */
71 static char *test;
72
73 /**
74  * Set to the name of the config file used.
75  */
76 static const char *config_file;
77
78 /**
79  * Set to the directory where runtime files are stored.
80  */
81 static char *dir;
82
83 /**
84  * Final status code.
85  */
86 static int ret;
87
88 /**
89  * Connection with ARM.
90  */
91 static struct GNUNET_ARM_Handle *h;
92
93 /**
94  * Our scheduler.
95  */
96 static struct GNUNET_SCHEDULER_Handle *sched;
97
98 /**
99  * Our configuration.
100  */
101 const struct GNUNET_CONFIGURATION_Handle *cfg;
102
103 /**
104  * Processing stage that we are in.  Simple counter.
105  */
106 static unsigned int phase;
107
108
109 /**
110  * Main continuation-passing-style loop.  Runs the various
111  * jobs that we've been asked to do in order.
112  *
113  * @param cls closure, unused
114  * @param tc context, unused
115  */
116 static void
117 cps_loop (void *cls,
118           const struct GNUNET_SCHEDULER_TaskContext *tc);
119
120
121 /**
122  * Callback invoked with the status of the last operation.  Reports to the
123  * user and then runs the next phase in the FSM.
124  *
125  * @param cls pointer to "const char*" identifying service that was manipulated
126  * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error
127  */
128 static void
129 confirm_cb (void *cls, int success)
130 {
131   const char *service = cls;
132   switch (success)
133     {
134     case GNUNET_OK:
135       if (quiet != GNUNET_YES)
136         fprintf(stdout, _("Service `%s' is now running.\n"), service);
137       break;
138     case GNUNET_NO:
139       if (quiet != GNUNET_YES)
140         fprintf(stdout, _("Service `%s' is not running.\n"), service);
141       break;
142     case GNUNET_SYSERR:
143       if (quiet != GNUNET_YES)
144         fprintf(stdout,
145                 _("Error updating service `%s': ARM not running\n"), service);
146       break;
147     }
148   GNUNET_SCHEDULER_add_continuation (sched,
149                                      &cps_loop,
150                                      NULL,
151                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
152 }
153
154
155 /**
156  * Function called to confirm that a service is running (or that
157  * it is not running).
158  *
159  * @param cls pointer to "const char*" identifying service that was manipulated
160  * @param tc reason determines if service is now running
161  */
162 static void
163 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
164 {
165   const char *service = cls;
166
167   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
168     {
169       if (quiet != GNUNET_YES)
170         fprintf(stdout, _("Service `%s' is running.\n"), service);
171     }
172   else
173     {
174       if (quiet != GNUNET_YES)
175         fprintf(stdout, _("Service `%s' is not running.\n"), service);
176     }
177   GNUNET_SCHEDULER_add_continuation (sched,
178                                      &cps_loop,
179                                      NULL,
180                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
181 }
182
183
184 /**
185  * Main function that will be run by the scheduler.
186  *
187  * @param cls closure
188  * @param s the scheduler to use
189  * @param args remaining command-line arguments
190  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
191  * @param c configuration
192  */
193 static void
194 run (void *cls,
195      struct GNUNET_SCHEDULER_Handle *s,
196      char *const *args,
197      const char *cfgfile,
198      const struct GNUNET_CONFIGURATION_Handle *c)
199 {
200   sched = s;
201   cfg = c;
202   config_file = cfgfile;
203   if (GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "SERVICEHOME", &dir) != GNUNET_OK)
204   {
205     dir = NULL;
206   }
207
208   h = GNUNET_ARM_connect (cfg, sched, NULL);
209   if (h == NULL)
210     {
211       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212                _("Fatal error initializing ARM API.\n"));
213       ret = 1;
214       return;
215     }
216   GNUNET_SCHEDULER_add_continuation (sched,
217                                      &cps_loop,
218                                      NULL,
219                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
220 }
221
222 /**
223  * Attempts to delete configuration file and SERVICEHOME
224  * on arm shutdown provided the end and delete options
225  * were specified when gnunet-arm was run.
226  */
227 static void delete_files()
228 {
229   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will attempt to remove configuration file %s and service directory %s\n", config_file, dir);
230
231   if (UNLINK(config_file) != 0)
232   {
233     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
234            _("Failed to remove configuration file %s\n"), config_file);
235   }
236
237   if (GNUNET_DISK_directory_remove(dir) != GNUNET_OK)
238   {
239     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
240         _("Failed to remove servicehome directory %s\n"), dir);
241
242   }
243 }
244
245 /**
246  * Main continuation-passing-style loop.  Runs the various
247  * jobs that we've been asked to do in order.
248  *
249  * @param cls closure, unused
250  * @param tc context, unused
251  */
252 static void
253 cps_loop (void *cls,
254           const struct GNUNET_SCHEDULER_TaskContext *tc)
255 {
256   while (1)
257     {
258       switch (phase++)
259         {
260         case 0:
261           if (term != NULL)
262             {
263               GNUNET_ARM_stop_service (h, term, TIMEOUT, &confirm_cb, term);
264               return;
265             }
266           break;
267         case 1:
268           if (end)
269             {
270               GNUNET_ARM_stop_service (h, "arm", TIMEOUT, &confirm_cb, "arm");
271               return;
272             }
273           break;
274         case 2:
275           if (start)
276             {
277               GNUNET_ARM_start_service (h, "arm", TIMEOUT, &confirm_cb, "arm");
278               return;
279             }
280           break;
281         case 3:
282           if (init != NULL)
283             {
284               GNUNET_ARM_start_service (h, init, TIMEOUT, &confirm_cb, init);
285               return;
286             }
287           break;
288         case 4:
289           if (test != NULL)
290             {
291               GNUNET_CLIENT_service_test (sched, test, cfg, TIMEOUT, &confirm_task, test);
292               return;
293             }
294           break;
295         default: /* last phase */
296           GNUNET_ARM_disconnect (h);
297           if ((end == GNUNET_YES) && (delete == GNUNET_YES))
298             delete_files();
299           return;
300         }
301     }
302 }
303
304
305 /**
306  * gnunet-arm command line options
307  */
308 static struct GNUNET_GETOPT_CommandLineOption options[] = {
309   {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
310    GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
311   {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
312    GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
313   {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
314    GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
315   {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
316    GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
317   {'t', "test", "SERVICE",
318    gettext_noop ("test if a particular service is running"),
319    GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
320   {'d', "delete", NULL, gettext_noop ("delete config file and directory on exit"),
321    GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
322   {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
323    GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
324   GNUNET_GETOPT_OPTION_END
325 };
326
327
328 /**
329  * The main function to obtain arm from gnunetd.
330  *
331  * @param argc number of arguments from the command line
332  * @param argv command line arguments
333  * @return 0 ok, 1 on error
334  */
335 int
336 main (int argc, char *const *argv)
337 {
338   return (GNUNET_OK ==
339           GNUNET_PROGRAM_run (argc,
340                               argv,
341                               "gnunet-arm",
342                               gettext_noop
343                               ("Control services and the Automated Restart Manager (ARM)"),
344                               options, &run, NULL)) ? ret : 1;
345 }
346
347 /* end of gnunet-arm.c */