fixing common off-by-one error with respect to maximum message size
[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_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_SECONDS, 360)
38
39 /**
40  * Timeout for starting services, very short because of the strange way start works
41  * (by checking if running before starting, so really this time is always waited on
42  * startup (annoying)).
43  */
44 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50)
45
46 /**
47  * Timeout for starting services, very short because of the strange way start works
48  * (by checking if running before starting, so really this time is always waited on
49  * startup (annoying)).
50  */
51 #define TEST_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 to the name of a service to start.
80  */
81 static char *init;
82
83 /**
84  * Set to the name of a service to kill.
85  */
86 static char *term;
87
88 /**
89  * Set to the name of a service to test.
90  */
91 static char *test;
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 scheduler.
115  */
116 static struct GNUNET_SCHEDULER_Handle *sched;
117
118 /**
119  * Our configuration.
120  */
121 const struct GNUNET_CONFIGURATION_Handle *cfg;
122
123 /**
124  * Processing stage that we are in.  Simple counter.
125  */
126 static unsigned int phase;
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,
138           const struct GNUNET_SCHEDULER_TaskContext *tc);
139
140
141 /**
142  * Callback invoked with the status of the last operation.  Reports to the
143  * user and then runs the next phase in the FSM.
144  *
145  * @param cls pointer to "const char*" identifying service that was manipulated
146  * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error
147  */
148 static void
149 confirm_cb (void *cls, int success)
150 {
151   const char *service = cls;
152   switch (success)
153     {
154     case GNUNET_OK:
155       if (quiet != GNUNET_YES)
156         fprintf(stdout, _("Service `%s' is now running.\n"), service);
157       if ((phase - 1 != 2) && (phase - 1 != 3))
158         {
159           if (quiet != GNUNET_YES)
160             fprintf(stdout, _("Failed to stop service `%s'!\n"), service);
161           ret = 1;
162         }
163       break;
164     case GNUNET_NO:
165       if (quiet != GNUNET_YES)
166         fprintf(stdout, _("Service `%s' is not running.\n"), service);
167       if ((phase - 1 != 0) && (phase - 1 != 1))
168         {
169           if (quiet != GNUNET_YES)
170             fprintf(stdout, _("Failed to start service `%s'!\n"), service);
171           ret = 1;
172         }
173       break;
174     case GNUNET_SYSERR:
175       if (quiet != GNUNET_YES)
176         fprintf(stdout,
177                 _("Some error communicating with service `%s'.\n"), service);
178       ret = 1;
179       break;
180     }
181
182   GNUNET_SCHEDULER_add_continuation (sched,
183                                      &cps_loop,
184                                      NULL,
185                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
186 }
187
188
189 /**
190  * Function called to confirm that a service is running (or that
191  * it is not running).
192  *
193  * @param cls pointer to "const char*" identifying service that was manipulated
194  * @param tc reason determines if service is now running
195  */
196 static void
197 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
198 {
199   const char *service = cls;
200
201   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
202     {
203       if (quiet != GNUNET_YES)
204         fprintf(stdout, _("Service `%s' is running.\n"), service);
205     }
206   else
207     {
208       if (quiet != GNUNET_YES)
209         fprintf(stdout, _("Service `%s' is not running.\n"), service);
210     }
211   GNUNET_SCHEDULER_add_continuation (sched,
212                                      &cps_loop,
213                                      NULL,
214                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
215 }
216
217
218 /**
219  * Main function that will be run by the scheduler.
220  *
221  * @param cls closure
222  * @param s the scheduler to use
223  * @param args remaining command-line arguments
224  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
225  * @param c configuration
226  */
227 static void
228 run (void *cls,
229      struct GNUNET_SCHEDULER_Handle *s,
230      char *const *args,
231      const char *cfgfile,
232      const struct GNUNET_CONFIGURATION_Handle *c)
233 {
234   sched = s;
235   cfg = c;
236   config_file = cfgfile;
237   if (GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "SERVICEHOME", &dir) != GNUNET_OK)
238     {
239       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
240                   _("Fatal configuration error: `%s' option in section `%s' missing.\n"),
241                   "SERVICEHOME",
242                   "PATHS");
243       return;
244     }
245   h = GNUNET_ARM_connect (cfg, sched, NULL);
246   if (h == NULL)
247     {
248       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
249                _("Fatal error initializing ARM API.\n"));
250       ret = 1;
251       return;
252     }
253   GNUNET_SCHEDULER_add_continuation (sched,
254                                      &cps_loop,
255                                      NULL,
256                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
257 }
258
259 /**
260  * Attempts to delete configuration file and SERVICEHOME
261  * on arm shutdown provided the end and delete options
262  * were specified when gnunet-arm was run.
263  */
264 static void delete_files()
265 {
266   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will attempt to remove configuration file %s and service directory %s\n", config_file, dir);
267
268   if (UNLINK(config_file) != 0)
269   {
270     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
271            _("Failed to remove configuration file %s\n"), config_file);
272   }
273
274   if (GNUNET_DISK_directory_remove(dir) != GNUNET_OK)
275   {
276     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
277         _("Failed to remove servicehome directory %s\n"), dir);
278
279   }
280 }
281
282 /**
283  * Main continuation-passing-style loop.  Runs the various
284  * jobs that we've been asked to do in order.
285  *
286  * @param cls closure, unused
287  * @param tc context, unused
288  */
289 static void
290 cps_loop (void *cls,
291           const struct GNUNET_SCHEDULER_TaskContext *tc)
292 {
293   while (1)
294     {
295       switch (phase++)
296         {
297         case 0:
298           if (term != NULL)
299             {
300               GNUNET_ARM_stop_service (h, term, STOP_TIMEOUT, &confirm_cb, term);
301               return;
302             }
303           break;
304         case 1:
305           if ((end) || (restart))
306             {
307               GNUNET_ARM_stop_service (h, "arm", STOP_TIMEOUT, &confirm_cb, "arm");
308               return;
309             }
310           break;
311         case 2:
312           if (start)
313             {
314               GNUNET_ARM_start_service (h, "arm", START_TIMEOUT, &confirm_cb, "arm");
315               return;
316             }
317           break;
318         case 3:
319           if (init != NULL)
320             {
321               GNUNET_ARM_start_service (h, init, START_TIMEOUT, &confirm_cb, init);
322               return;
323             }
324           break;
325         case 4:
326           if (test != NULL)
327             {
328               GNUNET_CLIENT_service_test (sched, test, cfg, TEST_TIMEOUT, &confirm_task, test);
329               return;
330             }
331           break;
332         case 5:
333           if (restart)
334             {
335               GNUNET_ARM_disconnect (h);
336               phase = 0;
337               end = 0;
338               start = 1;
339               restart = 0;
340               h = GNUNET_ARM_connect (cfg, sched, NULL);
341               if (h == NULL)
342                 {
343                   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
344                            _("Fatal error initializing ARM API.\n"));
345                   ret = 1;
346                   return;
347                 }
348               GNUNET_SCHEDULER_add_now(sched, &cps_loop, NULL);
349               return;
350             }
351           /* Fall through */
352         default: /* last phase */
353           GNUNET_ARM_disconnect (h);
354           if ((end == GNUNET_YES) && (delete == GNUNET_YES))
355             delete_files();
356           return;
357         }
358     }
359 }
360
361
362 /**
363  * The main function to obtain arm from gnunetd.
364  *
365  * @param argc number of arguments from the command line
366  * @param argv command line arguments
367  * @return 0 ok, 1 on error
368  */
369 int
370 main (int argc, char *const *argv)
371 {
372   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
373     {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
374      GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
375     {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
376      GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
377     {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
378      GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
379     {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
380      GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
381     {'r', "restart", NULL, gettext_noop ("stop and start all GNUnet default services"),
382      GNUNET_NO, &GNUNET_GETOPT_set_one, &restart},
383     {'t', "test", "SERVICE",
384      gettext_noop ("test if a particular service is running"),
385      GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
386     {'d', "delete", NULL, gettext_noop ("delete config file and directory on exit"),
387      GNUNET_NO, &GNUNET_GETOPT_set_one, &delete},
388     {'q', "quiet", NULL, gettext_noop ("don't print status messages"),
389      GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet},
390     GNUNET_GETOPT_OPTION_END
391   };
392   return (GNUNET_OK ==
393           GNUNET_PROGRAM_run (argc,
394                               argv,
395                               "gnunet-arm",
396                               gettext_noop
397                               ("Control services and the Automated Restart Manager (ARM)"),
398                               options, &run, NULL)) ? ret : 1;
399 }
400
401 /* end of gnunet-arm.c */