2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/program.c
23 * @brief standard code for GNUnet startup and shutdown
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_resolver_service.h"
30 #include "gnunet_constants.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
36 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
39 * Context for the command.
49 * Name of the configuration file used, can be NULL!
54 * Main function to run.
56 GNUNET_PROGRAM_Main task;
59 * Closure for @e task.
64 * Configuration to use.
66 const struct GNUNET_CONFIGURATION_Handle *cfg;
72 * Initial task called by the scheduler for each
73 * program. Runs the program-specific main task.
76 program_main (void *cls)
78 struct CommandContext *cc = cls;
79 const struct GNUNET_SCHEDULER_TaskContext *tc;
81 tc = GNUNET_SCHEDULER_get_task_context ();
82 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
84 GNUNET_SPEEDUP_start_(cc->cfg);
85 GNUNET_RESOLVER_connect (cc->cfg);
86 cc->task (cc->task_cls, cc->args, cc->cfgfile, cc->cfg);
91 * Compare function for 'qsort' to sort command-line arguments by the
94 * @param a1 first command line option
95 * @param a2 second command line option
98 cmd_sorter (const void *a1, const void *a2)
100 const struct GNUNET_GETOPT_CommandLineOption *c1 = a1;
101 const struct GNUNET_GETOPT_CommandLineOption *c2 = a2;
103 if (toupper ((unsigned char) c1->shortName) >
104 toupper ((unsigned char) c2->shortName))
106 if (toupper ((unsigned char) c1->shortName) <
107 toupper ((unsigned char) c2->shortName))
109 if (c1->shortName > c2->shortName)
111 if (c1->shortName < c2->shortName)
118 * Run a standard GNUnet command startup sequence (initialize loggers
119 * and configuration, parse options).
121 * @param argc number of command line arguments in @a argv
122 * @param argv command line arguments
123 * @param binaryName our expected name
124 * @param binaryHelp help text for the program
125 * @param options command line options
126 * @param task main function to run
127 * @param task_cls closure for @a task
128 * @param run_without_scheduler #GNUNET_NO start the scheduler, #GNUNET_YES do not
129 * start the scheduler just run the main task
130 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
133 GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
134 const char *binaryHelp,
135 const struct GNUNET_GETOPT_CommandLineOption *options,
136 GNUNET_PROGRAM_Main task, void *task_cls,
137 int run_without_scheduler)
139 struct CommandContext cc;
149 unsigned long long skew_offset;
150 unsigned long long skew_variance;
151 long long clock_offset;
152 struct GNUNET_CONFIGURATION_Handle *cfg;
154 struct GNUNET_GETOPT_CommandLineOption defoptions[] = {
155 GNUNET_GETOPT_OPTION_CFG_FILE (&cc.cfgfile),
156 GNUNET_GETOPT_OPTION_HELP (binaryHelp),
157 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
158 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
159 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION)
161 struct GNUNET_GETOPT_CommandLineOption *allopts;
167 gargs = getenv ("GNUNET_ARGS");
178 for (i = 0; i < argc; i++)
179 GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i]));
180 cargs = GNUNET_strdup (gargs);
181 for (tok = strtok (cargs, " "); NULL != tok; tok = strtok (NULL, " "))
182 GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok));
184 GNUNET_array_append (gargv, gargc, NULL);
185 argv = (char *const *) gargv;
188 memset (&cc, 0, sizeof (cc));
191 cc.task_cls = task_cls;
192 cc.cfg = cfg = GNUNET_CONFIGURATION_create ();
195 setlocale (LC_ALL, "");
196 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
199 BINDTEXTDOMAIN ("GNUnet", path);
202 textdomain ("GNUnet");
205 while (NULL != options[cnt].name)
208 GNUNET_malloc ((cnt +
209 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) +
210 sizeof (defoptions));
211 memcpy (allopts, defoptions, sizeof (defoptions));
213 [sizeof (defoptions) /
214 sizeof (struct GNUNET_GETOPT_CommandLineOption)], options,
215 (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption));
216 cnt += sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption);
217 qsort (allopts, cnt, sizeof (struct GNUNET_GETOPT_CommandLineOption),
220 xdg = getenv ("XDG_CONFIG_HOME");
222 GNUNET_asprintf (&cfg_fn,
228 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
229 lpfx = GNUNET_strdup (binaryName);
230 if (NULL != (spc = strstr (lpfx, " ")))
232 ret = GNUNET_GETOPT_run (binaryName, allopts, (unsigned int) argc, argv);
233 if ((GNUNET_OK > ret) ||
234 (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile)))
236 GNUNET_free (allopts);
240 if (NULL == cc.cfgfile)
241 cc.cfgfile = GNUNET_strdup (cfg_fn);
243 GNUNET_DISK_file_test (cc.cfgfile))
245 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, cc.cfgfile))
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
248 _("Malformed configuration file `%s', exit ...\n"),
251 GNUNET_free (allopts);
258 if (0 != strcmp (cc.cfgfile, cfg_fn))
259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260 _("Could not access configuration file `%s'\n"),
262 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, NULL))
264 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
265 _("Malformed configuration, exit ...\n"));
267 GNUNET_free (allopts);
272 GNUNET_free (allopts);
275 GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing", "skew_offset",
278 GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing",
279 "skew_variance", &skew_variance)))
281 clock_offset = skew_offset - skew_variance;
282 GNUNET_TIME_set_offset (clock_offset);
284 /* ARM needs to know which configuration file to use when starting
285 services. If we got a command-line option *and* if nothing is
286 specified in the configuration, remember the command-line option
287 in "cfg". This is typically really only having an effect if we
288 are running code in src/arm/, as obviously the rest of the code
289 has little business with ARM-specific options. */
291 GNUNET_CONFIGURATION_have_value (cfg,
295 GNUNET_CONFIGURATION_set_value_string (cfg,
301 cc.args = &argv[ret];
302 if (GNUNET_NO == run_without_scheduler)
304 GNUNET_SCHEDULER_run (&program_main, &cc);
308 GNUNET_RESOLVER_connect (cc.cfg);
309 cc.task (cc.task_cls, cc.args, cc.cfgfile, cc.cfg);
313 GNUNET_SPEEDUP_stop_ ();
314 GNUNET_CONFIGURATION_destroy (cfg);
315 GNUNET_free_non_null (cc.cfgfile);
316 GNUNET_free (cfg_fn);
317 GNUNET_free_non_null (loglev);
318 GNUNET_free_non_null (logfile);
323 * Run a standard GNUnet command startup sequence (initialize loggers
324 * and configuration, parse options).
326 * @param argc number of command line arguments
327 * @param argv command line arguments
328 * @param binaryName our expected name
329 * @param binaryHelp help text for the program
330 * @param options command line options
331 * @param task main function to run
332 * @param task_cls closure for @a task
333 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
336 GNUNET_PROGRAM_run (int argc, char *const *argv,
337 const char *binaryName,
338 const char *binaryHelp,
339 const struct GNUNET_GETOPT_CommandLineOption *options,
340 GNUNET_PROGRAM_Main task,
343 return GNUNET_PROGRAM_run2 (argc, argv,
344 binaryName, binaryHelp,
351 /* end of program.c */