program.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPROSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/program.c
  18. * @brief standard code for GNUnet startup and shutdown
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_resolver_service.h"
  24. #include "gnunet_constants.h"
  25. #include "speedup.h"
  26. #include <gcrypt.h>
  27. #define LOG(kind, ...) GNUNET_log_from (kind, "util-program", __VA_ARGS__)
  28. #define LOG_STRERROR_FILE(kind, syscall, filename) \
  29. GNUNET_log_from_strerror_file (kind, "util-program", syscall, filename)
  30. /**
  31. * Context for the command.
  32. */
  33. struct CommandContext
  34. {
  35. /**
  36. * Argv argument.
  37. */
  38. char *const *args;
  39. /**
  40. * Name of the configuration file used, can be NULL!
  41. */
  42. char *cfgfile;
  43. /**
  44. * Main function to run.
  45. */
  46. GNUNET_PROGRAM_Main task;
  47. /**
  48. * Closure for @e task.
  49. */
  50. void *task_cls;
  51. /**
  52. * Configuration to use.
  53. */
  54. const struct GNUNET_CONFIGURATION_Handle *cfg;
  55. };
  56. /**
  57. * task run when the scheduler shuts down
  58. */
  59. static void
  60. shutdown_task (void *cls)
  61. {
  62. (void) cls;
  63. GNUNET_SPEEDUP_stop_ ();
  64. }
  65. /**
  66. * Initial task called by the scheduler for each
  67. * program. Runs the program-specific main task.
  68. */
  69. static void
  70. program_main (void *cls)
  71. {
  72. struct CommandContext *cc = cls;
  73. GNUNET_SPEEDUP_start_ (cc->cfg);
  74. GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
  75. GNUNET_RESOLVER_connect (cc->cfg);
  76. cc->task (cc->task_cls, cc->args, cc->cfgfile, cc->cfg);
  77. }
  78. /**
  79. * Compare function for 'qsort' to sort command-line arguments by the
  80. * short option.
  81. *
  82. * @param a1 first command line option
  83. * @param a2 second command line option
  84. */
  85. static int
  86. cmd_sorter (const void *a1, const void *a2)
  87. {
  88. const struct GNUNET_GETOPT_CommandLineOption *c1 = a1;
  89. const struct GNUNET_GETOPT_CommandLineOption *c2 = a2;
  90. if (toupper ((unsigned char) c1->shortName) >
  91. toupper ((unsigned char) c2->shortName))
  92. return 1;
  93. if (toupper ((unsigned char) c1->shortName) <
  94. toupper ((unsigned char) c2->shortName))
  95. return -1;
  96. if (c1->shortName > c2->shortName)
  97. return 1;
  98. if (c1->shortName < c2->shortName)
  99. return -1;
  100. return 0;
  101. }
  102. /**
  103. * Run a standard GNUnet command startup sequence (initialize loggers
  104. * and configuration, parse options).
  105. *
  106. * @param argc number of command line arguments in @a argv
  107. * @param argv command line arguments
  108. * @param binaryName our expected name
  109. * @param binaryHelp help text for the program
  110. * @param options command line options
  111. * @param task main function to run
  112. * @param task_cls closure for @a task
  113. * @param run_without_scheduler #GNUNET_NO start the scheduler, #GNUNET_YES do not
  114. * start the scheduler just run the main task
  115. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  116. */
  117. int
  118. GNUNET_PROGRAM_run2 (int argc,
  119. char *const *argv,
  120. const char *binaryName,
  121. const char *binaryHelp,
  122. const struct GNUNET_GETOPT_CommandLineOption *options,
  123. GNUNET_PROGRAM_Main task,
  124. void *task_cls,
  125. int run_without_scheduler)
  126. {
  127. struct CommandContext cc;
  128. #if ENABLE_NLS
  129. char *path;
  130. #endif
  131. char *loglev;
  132. char *logfile;
  133. char *cfg_fn;
  134. const char *xdg;
  135. int ret;
  136. unsigned int cnt;
  137. unsigned long long skew_offset;
  138. unsigned long long skew_variance;
  139. long long clock_offset;
  140. struct GNUNET_CONFIGURATION_Handle *cfg;
  141. const struct GNUNET_OS_ProjectData *pd = GNUNET_OS_project_data_get ();
  142. struct GNUNET_GETOPT_CommandLineOption defoptions[] =
  143. { GNUNET_GETOPT_option_cfgfile (&cc.cfgfile),
  144. GNUNET_GETOPT_option_help (binaryHelp),
  145. GNUNET_GETOPT_option_loglevel (&loglev),
  146. GNUNET_GETOPT_option_logfile (&logfile),
  147. GNUNET_GETOPT_option_version (pd->version) };
  148. struct GNUNET_GETOPT_CommandLineOption *allopts;
  149. const char *gargs;
  150. char *lpfx;
  151. char *spc;
  152. logfile = NULL;
  153. gargs = getenv ("GNUNET_ARGS");
  154. if (NULL != gargs)
  155. {
  156. char **gargv;
  157. unsigned int gargc;
  158. char *cargs;
  159. gargv = NULL;
  160. gargc = 0;
  161. for (int i = 0; i < argc; i++)
  162. GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i]));
  163. cargs = GNUNET_strdup (gargs);
  164. for (char *tok = strtok (cargs, " "); NULL != tok; tok = strtok (NULL, " "))
  165. GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok));
  166. GNUNET_free (cargs);
  167. GNUNET_array_append (gargv, gargc, NULL);
  168. argv = (char *const *) gargv;
  169. argc = gargc - 1;
  170. }
  171. memset (&cc, 0, sizeof(cc));
  172. loglev = NULL;
  173. cc.task = task;
  174. cc.task_cls = task_cls;
  175. cc.cfg = cfg = GNUNET_CONFIGURATION_create ();
  176. /* prepare */
  177. #if ENABLE_NLS
  178. if (NULL != pd->gettext_domain)
  179. {
  180. setlocale (LC_ALL, "");
  181. path = (NULL == pd->gettext_path)
  182. ? GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR)
  183. : GNUNET_strdup (pd->gettext_path);
  184. if (NULL != path)
  185. {
  186. bindtextdomain (pd->gettext_domain, path);
  187. GNUNET_free (path);
  188. }
  189. textdomain (pd->gettext_domain);
  190. }
  191. #endif
  192. cnt = 0;
  193. while (NULL != options[cnt].name)
  194. cnt++;
  195. allopts =
  196. GNUNET_malloc ((cnt + 1) * sizeof(struct GNUNET_GETOPT_CommandLineOption)
  197. + sizeof(defoptions));
  198. GNUNET_memcpy (allopts, defoptions, sizeof(defoptions));
  199. GNUNET_memcpy (&allopts[sizeof(defoptions)
  200. / sizeof(struct GNUNET_GETOPT_CommandLineOption)],
  201. options,
  202. (cnt + 1) * sizeof(struct GNUNET_GETOPT_CommandLineOption));
  203. cnt += sizeof(defoptions) / sizeof(struct GNUNET_GETOPT_CommandLineOption);
  204. qsort (allopts,
  205. cnt,
  206. sizeof(struct GNUNET_GETOPT_CommandLineOption),
  207. &cmd_sorter);
  208. loglev = NULL;
  209. xdg = getenv ("XDG_CONFIG_HOME");
  210. if (NULL != xdg)
  211. GNUNET_asprintf (&cfg_fn,
  212. "%s%s%s",
  213. xdg,
  214. DIR_SEPARATOR_STR,
  215. pd->config_file);
  216. else
  217. cfg_fn = GNUNET_strdup (pd->user_config_file);
  218. lpfx = GNUNET_strdup (binaryName);
  219. if (NULL != (spc = strstr (lpfx, " ")))
  220. *spc = '\0';
  221. ret = GNUNET_GETOPT_run (binaryName, allopts, (unsigned int) argc, argv);
  222. if ((GNUNET_OK > ret) ||
  223. (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile)))
  224. {
  225. GNUNET_free (allopts);
  226. GNUNET_free (lpfx);
  227. goto cleanup;
  228. }
  229. if (NULL != cc.cfgfile)
  230. {
  231. if ((GNUNET_YES != GNUNET_DISK_file_test (cc.cfgfile)) ||
  232. (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, cc.cfgfile)))
  233. {
  234. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  235. _ (
  236. "Unreadable or malformed configuration file `%s', exit ...\n"),
  237. cc.cfgfile);
  238. ret = GNUNET_SYSERR;
  239. GNUNET_free (allopts);
  240. GNUNET_free (lpfx);
  241. goto cleanup;
  242. }
  243. }
  244. else
  245. {
  246. if (GNUNET_YES == GNUNET_DISK_file_test (cfg_fn))
  247. {
  248. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, cfg_fn))
  249. {
  250. GNUNET_log (
  251. GNUNET_ERROR_TYPE_ERROR,
  252. _ (
  253. "Unreadable or malformed default configuration file `%s', exit ...\n"),
  254. cfg_fn);
  255. ret = GNUNET_SYSERR;
  256. GNUNET_free (allopts);
  257. GNUNET_free (lpfx);
  258. goto cleanup;
  259. }
  260. }
  261. else
  262. {
  263. GNUNET_free (cfg_fn);
  264. cfg_fn = NULL;
  265. if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, NULL))
  266. {
  267. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  268. _ ("Unreadable or malformed configuration, exit ...\n"));
  269. ret = GNUNET_SYSERR;
  270. GNUNET_free (allopts);
  271. GNUNET_free (lpfx);
  272. goto cleanup;
  273. }
  274. }
  275. }
  276. GNUNET_free (allopts);
  277. GNUNET_free (lpfx);
  278. if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cc.cfg,
  279. "testing",
  280. "skew_offset",
  281. &skew_offset)) &&
  282. (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cc.cfg,
  283. "testing",
  284. "skew_variance",
  285. &skew_variance)))
  286. {
  287. clock_offset = skew_offset - skew_variance;
  288. GNUNET_TIME_set_offset (clock_offset);
  289. }
  290. /* ARM needs to know which configuration file to use when starting
  291. services. If we got a command-line option *and* if nothing is
  292. specified in the configuration, remember the command-line option
  293. in "cfg". This is typically really only having an effect if we
  294. are running code in src/arm/, as obviously the rest of the code
  295. has little business with ARM-specific options. */if (GNUNET_YES != GNUNET_CONFIGURATION_have_value (cfg, "arm", "CONFIG"))
  296. {
  297. if (NULL != cc.cfgfile)
  298. GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG", cc.cfgfile);
  299. else if (NULL != cfg_fn)
  300. GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG", cfg_fn);
  301. }
  302. /* run */
  303. cc.args = &argv[ret];
  304. if ((NULL == cc.cfgfile) && (NULL != cfg_fn))
  305. cc.cfgfile = GNUNET_strdup (cfg_fn);
  306. if (GNUNET_NO == run_without_scheduler)
  307. {
  308. GNUNET_SCHEDULER_run (&program_main, &cc);
  309. }
  310. else
  311. {
  312. GNUNET_RESOLVER_connect (cc.cfg);
  313. cc.task (cc.task_cls, cc.args, cc.cfgfile, cc.cfg);
  314. }
  315. ret = GNUNET_OK;
  316. cleanup:
  317. GNUNET_CONFIGURATION_destroy (cfg);
  318. GNUNET_free_non_null (cc.cfgfile);
  319. GNUNET_free_non_null (cfg_fn);
  320. GNUNET_free_non_null (loglev);
  321. GNUNET_free_non_null (logfile);
  322. return ret;
  323. }
  324. /**
  325. * Run a standard GNUnet command startup sequence (initialize loggers
  326. * and configuration, parse options).
  327. *
  328. * @param argc number of command line arguments
  329. * @param argv command line arguments
  330. * @param binaryName our expected name
  331. * @param binaryHelp help text for the program
  332. * @param options command line options
  333. * @param task main function to run
  334. * @param task_cls closure for @a task
  335. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  336. */
  337. int
  338. GNUNET_PROGRAM_run (int argc,
  339. char *const *argv,
  340. const char *binaryName,
  341. const char *binaryHelp,
  342. const struct GNUNET_GETOPT_CommandLineOption *options,
  343. GNUNET_PROGRAM_Main task,
  344. void *task_cls)
  345. {
  346. return GNUNET_PROGRAM_run2 (argc,
  347. argv,
  348. binaryName,
  349. binaryHelp,
  350. options,
  351. task,
  352. task_cls,
  353. GNUNET_NO);
  354. }
  355. /* end of program.c */