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