Load the correct ATS plugin when MODE is unconfigured.
[oweals/gnunet.git] / src / util / program.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009-2013 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 PURPROSE.  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 util/program.c
23  * @brief standard code for GNUnet startup and shutdown
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_resolver_service.h"
30 #include "gnunet_constants.h"
31 #include <gcrypt.h>
32
33 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34
35 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
36
37 /**
38  * Context for the command.
39  */
40 struct CommandContext
41 {
42   /**
43    * Argv argument.
44    */
45   char *const *args;
46
47   /**
48    * Name of the configuration file used, can be NULL!
49    */
50   char *cfgfile;
51
52   /**
53    * Main function to run.
54    */
55   GNUNET_PROGRAM_Main task;
56
57   /**
58    * Closure for @e task.
59    */
60   void *task_cls;
61
62   /**
63    * Configuration to use.
64    */
65   const struct GNUNET_CONFIGURATION_Handle *cfg;
66
67 };
68
69
70 /**
71  * Start task that may speed up our system clock artificially
72  *
73  * @param cfg configuration to use
74  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the speedup was not configured
75  */
76 int
77 GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg);
78
79
80 /**
81  * Stop tasks that modify clock behavior.
82  */
83 int
84 GNUNET_SPEEDUP_stop_ (void);
85
86
87 /**
88  * Initial task called by the scheduler for each
89  * program.  Runs the program-specific main task.
90  */
91 static void
92 program_main (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 {
94   struct CommandContext *cc = cls;
95
96   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
97     return;
98   GNUNET_SPEEDUP_start_(cc->cfg);
99   GNUNET_RESOLVER_connect (cc->cfg);
100   cc->task (cc->task_cls, cc->args, cc->cfgfile, cc->cfg);
101 }
102
103
104 /**
105  * Compare function for 'qsort' to sort command-line arguments by the
106  * short option.
107  *
108  * @param a1 first command line option
109  * @param a2 second command line option
110  */
111 static int
112 cmd_sorter (const void *a1, const void *a2)
113 {
114   const struct GNUNET_GETOPT_CommandLineOption *c1 = a1;
115   const struct GNUNET_GETOPT_CommandLineOption *c2 = a2;
116
117   if (toupper ((unsigned char) c1->shortName) >
118       toupper ((unsigned char) c2->shortName))
119     return 1;
120   if (toupper ((unsigned char) c1->shortName) <
121       toupper ((unsigned char) c2->shortName))
122     return -1;
123   if (c1->shortName > c2->shortName)
124     return 1;
125   if (c1->shortName < c2->shortName)
126     return -1;
127   return 0;
128 }
129
130
131 /**
132  * Run a standard GNUnet command startup sequence (initialize loggers
133  * and configuration, parse options).
134  *
135  * @param argc number of command line arguments in @a argv
136  * @param argv command line arguments
137  * @param binaryName our expected name
138  * @param binaryHelp help text for the program
139  * @param options command line options
140  * @param task main function to run
141  * @param task_cls closure for @a task
142  * @param run_without_scheduler #GNUNET_NO start the scheduler, #GNUNET_YES do not
143  *        start the scheduler just run the main task
144  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
145  */
146 int
147 GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
148                     const char *binaryHelp,
149                     const struct GNUNET_GETOPT_CommandLineOption *options,
150                     GNUNET_PROGRAM_Main task, void *task_cls,
151                     int run_without_scheduler)
152 {
153   struct CommandContext cc;
154 #if ENABLE_NLS
155   char *path;
156 #endif
157   char *loglev;
158   char *logfile;
159   char *cfg_fn;
160   const char *xdg;
161   int ret;
162   unsigned int cnt;
163   unsigned long long skew_offset;
164   unsigned long long skew_variance;
165   long long clock_offset;
166   struct GNUNET_CONFIGURATION_Handle *cfg;
167
168   struct GNUNET_GETOPT_CommandLineOption defoptions[] = {
169     GNUNET_GETOPT_OPTION_CFG_FILE (&cc.cfgfile),
170     GNUNET_GETOPT_OPTION_HELP (binaryHelp),
171     GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
172     GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
173     GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION)
174   };
175   struct GNUNET_GETOPT_CommandLineOption *allopts;
176   const char *gargs;
177   char *lpfx;
178   char *spc;
179
180   logfile = NULL;
181   gargs = getenv ("GNUNET_ARGS");
182   if (NULL != gargs)
183   {
184     char **gargv;
185     unsigned int gargc;
186     int i;
187     char *tok;
188     char *cargs;
189
190     gargv = NULL;
191     gargc = 0;
192     for (i = 0; i < argc; i++)
193       GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i]));
194     cargs = GNUNET_strdup (gargs);
195     for (tok = strtok (cargs, " "); NULL != tok; tok = strtok (NULL, " "))
196       GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok));
197     GNUNET_free (cargs);
198     GNUNET_array_append (gargv, gargc, NULL);
199     argv = (char *const *) gargv;
200     argc = gargc - 1;
201   }
202   memset (&cc, 0, sizeof (cc));
203   loglev = NULL;
204   cc.task = task;
205   cc.task_cls = task_cls;
206   cc.cfg = cfg = GNUNET_CONFIGURATION_create ();
207   /* prepare */
208 #if ENABLE_NLS
209   setlocale (LC_ALL, "");
210   path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
211   if (NULL != path)
212   {
213     BINDTEXTDOMAIN ("GNUnet", path);
214     GNUNET_free (path);
215   }
216   textdomain ("GNUnet");
217 #endif
218   cnt = 0;
219   while (NULL != options[cnt].name)
220     cnt++;
221   allopts =
222       GNUNET_malloc ((cnt +
223                       1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) +
224                      sizeof (defoptions));
225   memcpy (allopts, defoptions, sizeof (defoptions));
226   memcpy (&allopts
227           [sizeof (defoptions) /
228            sizeof (struct GNUNET_GETOPT_CommandLineOption)], options,
229           (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption));
230   cnt += sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption);
231   qsort (allopts, cnt, sizeof (struct GNUNET_GETOPT_CommandLineOption),
232          &cmd_sorter);
233   loglev = NULL;
234   xdg = getenv ("XDG_CONFIG_HOME");
235   if (NULL != xdg)
236     GNUNET_asprintf (&cfg_fn,
237                      "%s%s%s",
238                      xdg,
239                      DIR_SEPARATOR_STR,
240                      "gnunet.conf");
241   else
242     cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
243   lpfx = GNUNET_strdup (binaryName);
244   if (NULL != (spc = strstr (lpfx, " ")))
245     *spc = '\0';
246   ret = GNUNET_GETOPT_run (binaryName, allopts, (unsigned int) argc, argv);
247   if ((GNUNET_OK > ret) ||
248       (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile)))
249   {
250     GNUNET_free (allopts);
251     GNUNET_free (lpfx);
252     goto cleanup;
253   }
254   if (NULL == cc.cfgfile)
255     cc.cfgfile = GNUNET_strdup (cfg_fn);
256   if (GNUNET_YES ==
257       GNUNET_DISK_file_test (cc.cfgfile))
258   {
259     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, cc.cfgfile))
260     {
261       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
262                   _("Malformed configuration file `%s', exit ...\n"),
263                   cc.cfgfile);
264       ret = GNUNET_SYSERR;
265       GNUNET_free (allopts);
266       GNUNET_free (lpfx);
267       goto cleanup;
268     }
269   }
270   else
271   {
272     if (0 != strcmp (cc.cfgfile, cfg_fn))
273       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
274                   _("Could not access configuration file `%s'\n"),
275                   cc.cfgfile);
276     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, NULL))
277     {
278       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
279                   _("Malformed configuration, exit ...\n"));
280       ret = GNUNET_SYSERR;
281       GNUNET_free (allopts);
282       GNUNET_free (lpfx);
283       goto cleanup;
284     }
285   }
286   GNUNET_free (allopts);
287   GNUNET_free (lpfx);
288   if (GNUNET_OK ==
289       GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing", "skew_offset",
290                                              &skew_offset) &&
291       (GNUNET_OK ==
292        GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing",
293                                               "skew_variance", &skew_variance)))
294   {
295     clock_offset = skew_offset - skew_variance;
296     GNUNET_TIME_set_offset (clock_offset);
297   }
298   /* run */
299   cc.args = &argv[ret];
300   if (GNUNET_NO == run_without_scheduler)
301   {
302     GNUNET_SCHEDULER_run (&program_main, &cc);
303   }
304   else
305   {
306     GNUNET_RESOLVER_connect (cc.cfg);
307     cc.task (cc.task_cls, cc.args, cc.cfgfile, cc.cfg);
308   }
309   ret = GNUNET_OK;
310  cleanup:
311   GNUNET_SPEEDUP_stop_ ();
312   GNUNET_CONFIGURATION_destroy (cfg);
313   GNUNET_free_non_null (cc.cfgfile);
314   GNUNET_free (cfg_fn);
315   GNUNET_free_non_null (loglev);
316   GNUNET_free_non_null (logfile);
317   return ret;
318 }
319
320 /**
321  * Run a standard GNUnet command startup sequence (initialize loggers
322  * and configuration, parse options).
323  *
324  * @param argc number of command line arguments
325  * @param argv command line arguments
326  * @param binaryName our expected name
327  * @param binaryHelp help text for the program
328  * @param options command line options
329  * @param task main function to run
330  * @param task_cls closure for @a task
331  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
332  */
333 int
334 GNUNET_PROGRAM_run (int argc, char *const *argv,
335                     const char *binaryName,
336                     const char *binaryHelp,
337                     const struct GNUNET_GETOPT_CommandLineOption *options,
338                     GNUNET_PROGRAM_Main task,
339                     void *task_cls)
340 {
341   return GNUNET_PROGRAM_run2 (argc, argv,
342                               binaryName, binaryHelp,
343                               options,
344                               task, task_cls,
345                               GNUNET_NO);
346 }
347
348
349 /* end of program.c */