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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file util/program.c
18 * @brief standard code for GNUnet startup and shutdown
19 * @author Christian Grothoff
23 #include "gnunet_util_lib.h"
24 #include "gnunet_resolver_service.h"
25 #include "gnunet_constants.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-program", __VA_ARGS__)
31 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-program", syscall, filename)
34 * Context for the command.
44 * Name of the configuration file used, can be NULL!
49 * Main function to run.
51 GNUNET_PROGRAM_Main task;
54 * Closure for @e task.
59 * Configuration to use.
61 const struct GNUNET_CONFIGURATION_Handle *cfg;
67 * task run when the scheduler shuts down
70 shutdown_task (void *cls)
73 GNUNET_SPEEDUP_stop_ ();
78 * Initial task called by the scheduler for each
79 * program. Runs the program-specific main task.
82 program_main (void *cls)
84 struct CommandContext *cc = cls;
86 GNUNET_SPEEDUP_start_(cc->cfg);
87 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
88 GNUNET_RESOLVER_connect (cc->cfg);
89 cc->task (cc->task_cls, cc->args, cc->cfgfile, cc->cfg);
94 * Compare function for 'qsort' to sort command-line arguments by the
97 * @param a1 first command line option
98 * @param a2 second command line option
101 cmd_sorter (const void *a1, const void *a2)
103 const struct GNUNET_GETOPT_CommandLineOption *c1 = a1;
104 const struct GNUNET_GETOPT_CommandLineOption *c2 = a2;
106 if (toupper ((unsigned char) c1->shortName) >
107 toupper ((unsigned char) c2->shortName))
109 if (toupper ((unsigned char) c1->shortName) <
110 toupper ((unsigned char) c2->shortName))
112 if (c1->shortName > c2->shortName)
114 if (c1->shortName < c2->shortName)
121 * Run a standard GNUnet command startup sequence (initialize loggers
122 * and configuration, parse options).
124 * @param argc number of command line arguments in @a argv
125 * @param argv command line arguments
126 * @param binaryName our expected name
127 * @param binaryHelp help text for the program
128 * @param options command line options
129 * @param task main function to run
130 * @param task_cls closure for @a task
131 * @param run_without_scheduler #GNUNET_NO start the scheduler, #GNUNET_YES do not
132 * start the scheduler just run the main task
133 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
136 GNUNET_PROGRAM_run2 (int argc,
138 const char *binaryName,
139 const char *binaryHelp,
140 const struct GNUNET_GETOPT_CommandLineOption *options,
141 GNUNET_PROGRAM_Main task,
143 int run_without_scheduler)
145 struct CommandContext cc;
155 unsigned long long skew_offset;
156 unsigned long long skew_variance;
157 long long clock_offset;
158 struct GNUNET_CONFIGURATION_Handle *cfg;
159 struct GNUNET_GETOPT_CommandLineOption defoptions[] = {
160 GNUNET_GETOPT_option_cfgfile (&cc.cfgfile),
161 GNUNET_GETOPT_option_help (binaryHelp),
162 GNUNET_GETOPT_option_loglevel (&loglev),
163 GNUNET_GETOPT_option_logfile (&logfile),
164 GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION)
166 struct GNUNET_GETOPT_CommandLineOption *allopts;
172 gargs = getenv ("GNUNET_ARGS");
181 for (int i = 0; i < argc; i++)
182 GNUNET_array_append (gargv,
184 GNUNET_strdup (argv[i]));
185 cargs = GNUNET_strdup (gargs);
186 for (char *tok = strtok (cargs, " ");
188 tok = strtok (NULL, " "))
189 GNUNET_array_append (gargv,
191 GNUNET_strdup (tok));
193 GNUNET_array_append (gargv,
196 argv = (char *const *) gargv;
204 cc.task_cls = task_cls;
205 cc.cfg = cfg = GNUNET_CONFIGURATION_create ();
208 setlocale (LC_ALL, "");
209 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
212 BINDTEXTDOMAIN ("GNUnet",
216 textdomain ("GNUnet");
219 while (NULL != options[cnt].name)
222 GNUNET_malloc ((cnt +
223 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) +
224 sizeof (defoptions));
225 GNUNET_memcpy (allopts,
227 sizeof (defoptions));
228 GNUNET_memcpy (&allopts
229 [sizeof (defoptions) /
230 sizeof (struct GNUNET_GETOPT_CommandLineOption)], options,
231 (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption));
232 cnt += sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption);
235 sizeof (struct GNUNET_GETOPT_CommandLineOption),
238 xdg = getenv ("XDG_CONFIG_HOME");
240 GNUNET_asprintf (&cfg_fn,
244 GNUNET_OS_project_data_get ()->config_file);
246 cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
247 lpfx = GNUNET_strdup (binaryName);
248 if (NULL != (spc = strstr (lpfx, " ")))
250 ret = GNUNET_GETOPT_run (binaryName,
254 if ((GNUNET_OK > ret) ||
256 GNUNET_log_setup (lpfx,
260 GNUNET_free (allopts);
264 if (NULL == cc.cfgfile)
265 cc.cfgfile = GNUNET_strdup (cfg_fn);
267 GNUNET_DISK_file_test (cc.cfgfile))
270 GNUNET_CONFIGURATION_load (cfg,
273 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
274 _("Malformed configuration file `%s', exit ...\n"),
277 GNUNET_free (allopts);
284 if (0 != strcmp (cc.cfgfile,
286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
287 _("Could not access configuration file `%s'\n"),
290 GNUNET_CONFIGURATION_load (cfg,
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
294 _("Malformed configuration, exit ...\n"));
296 GNUNET_free (allopts);
301 GNUNET_free (allopts);
304 GNUNET_CONFIGURATION_get_value_number (cc.cfg,
309 GNUNET_CONFIGURATION_get_value_number (cc.cfg,
314 clock_offset = skew_offset - skew_variance;
315 GNUNET_TIME_set_offset (clock_offset);
317 /* ARM needs to know which configuration file to use when starting
318 services. If we got a command-line option *and* if nothing is
319 specified in the configuration, remember the command-line option
320 in "cfg". This is typically really only having an effect if we
321 are running code in src/arm/, as obviously the rest of the code
322 has little business with ARM-specific options. */
324 GNUNET_CONFIGURATION_have_value (cfg,
328 GNUNET_CONFIGURATION_set_value_string (cfg,
335 cc.args = &argv[ret];
336 if (GNUNET_NO == run_without_scheduler)
338 GNUNET_SCHEDULER_run (&program_main, &cc);
342 GNUNET_RESOLVER_connect (cc.cfg);
343 cc.task (cc.task_cls,
350 GNUNET_CONFIGURATION_destroy (cfg);
351 GNUNET_free_non_null (cc.cfgfile);
352 GNUNET_free (cfg_fn);
353 GNUNET_free_non_null (loglev);
354 GNUNET_free_non_null (logfile);
360 * Run a standard GNUnet command startup sequence (initialize loggers
361 * and configuration, parse options).
363 * @param argc number of command line arguments
364 * @param argv command line arguments
365 * @param binaryName our expected name
366 * @param binaryHelp help text for the program
367 * @param options command line options
368 * @param task main function to run
369 * @param task_cls closure for @a task
370 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
373 GNUNET_PROGRAM_run (int argc, char *const *argv,
374 const char *binaryName,
375 const char *binaryHelp,
376 const struct GNUNET_GETOPT_CommandLineOption *options,
377 GNUNET_PROGRAM_Main task,
380 return GNUNET_PROGRAM_run2 (argc, argv,
381 binaryName, binaryHelp,
388 /* end of program.c */