confparse.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "or.h"
  7. #include "confparse.h"
  8. #include "routerset.h"
  9. static uint64_t config_parse_memunit(const char *s, int *ok);
  10. static int config_parse_msec_interval(const char *s, int *ok);
  11. static int config_parse_interval(const char *s, int *ok);
  12. static void config_reset(const config_format_t *fmt, void *options,
  13. const config_var_t *var, int use_defaults);
  14. /** Allocate an empty configuration object of a given format type. */
  15. void *
  16. config_new(const config_format_t *fmt)
  17. {
  18. void *opts = tor_malloc_zero(fmt->size);
  19. *(uint32_t*)STRUCT_VAR_P(opts, fmt->magic_offset) = fmt->magic;
  20. CONFIG_CHECK(fmt, opts);
  21. return opts;
  22. }
  23. /*
  24. * Functions to parse config options
  25. */
  26. /** If <b>option</b> is an official abbreviation for a longer option,
  27. * return the longer option. Otherwise return <b>option</b>.
  28. * If <b>command_line</b> is set, apply all abbreviations. Otherwise, only
  29. * apply abbreviations that work for the config file and the command line.
  30. * If <b>warn_obsolete</b> is set, warn about deprecated names. */
  31. const char *
  32. config_expand_abbrev(const config_format_t *fmt, const char *option,
  33. int command_line, int warn_obsolete)
  34. {
  35. int i;
  36. if (! fmt->abbrevs)
  37. return option;
  38. for (i=0; fmt->abbrevs[i].abbreviated; ++i) {
  39. /* Abbreviations are case insensitive. */
  40. if (!strcasecmp(option,fmt->abbrevs[i].abbreviated) &&
  41. (command_line || !fmt->abbrevs[i].commandline_only)) {
  42. if (warn_obsolete && fmt->abbrevs[i].warn) {
  43. log_warn(LD_CONFIG,
  44. "The configuration option '%s' is deprecated; "
  45. "use '%s' instead.",
  46. fmt->abbrevs[i].abbreviated,
  47. fmt->abbrevs[i].full);
  48. }
  49. /* Keep going through the list in case we want to rewrite it more.
  50. * (We could imagine recursing here, but I don't want to get the
  51. * user into an infinite loop if we craft our list wrong.) */
  52. option = fmt->abbrevs[i].full;
  53. }
  54. }
  55. return option;
  56. }
  57. /** Helper: allocate a new configuration option mapping 'key' to 'val',
  58. * append it to *<b>lst</b>. */
  59. void
  60. config_line_append(config_line_t **lst,
  61. const char *key,
  62. const char *val)
  63. {
  64. config_line_t *newline;
  65. newline = tor_malloc_zero(sizeof(config_line_t));
  66. newline->key = tor_strdup(key);
  67. newline->value = tor_strdup(val);
  68. newline->next = NULL;
  69. while (*lst)
  70. lst = &((*lst)->next);
  71. (*lst) = newline;
  72. }
  73. /** Helper: parse the config string and strdup into key/value
  74. * strings. Set *result to the list, or NULL if parsing the string
  75. * failed. Return 0 on success, -1 on failure. Warn and ignore any
  76. * misformatted lines.
  77. *
  78. * If <b>extended</b> is set, then treat keys beginning with / and with + as
  79. * indicating "clear" and "append" respectively. */
  80. int
  81. config_get_lines(const char *string, config_line_t **result, int extended)
  82. {
  83. config_line_t *list = NULL, **next;
  84. char *k, *v;
  85. const char *parse_err;
  86. next = &list;
  87. do {
  88. k = v = NULL;
  89. string = parse_config_line_from_str_verbose(string, &k, &v, &parse_err);
  90. if (!string) {
  91. log_warn(LD_CONFIG, "Error while parsing configuration: %s",
  92. parse_err?parse_err:"<unknown>");
  93. config_free_lines(list);
  94. tor_free(k);
  95. tor_free(v);
  96. return -1;
  97. }
  98. if (k && v) {
  99. unsigned command = CONFIG_LINE_NORMAL;
  100. if (extended) {
  101. if (k[0] == '+') {
  102. char *k_new = tor_strdup(k+1);
  103. tor_free(k);
  104. k = k_new;
  105. command = CONFIG_LINE_APPEND;
  106. } else if (k[0] == '/') {
  107. char *k_new = tor_strdup(k+1);
  108. tor_free(k);
  109. k = k_new;
  110. tor_free(v);
  111. v = tor_strdup("");
  112. command = CONFIG_LINE_CLEAR;
  113. }
  114. }
  115. /* This list can get long, so we keep a pointer to the end of it
  116. * rather than using config_line_append over and over and getting
  117. * n^2 performance. */
  118. *next = tor_malloc_zero(sizeof(config_line_t));
  119. (*next)->key = k;
  120. (*next)->value = v;
  121. (*next)->next = NULL;
  122. (*next)->command = command;
  123. next = &((*next)->next);
  124. } else {
  125. tor_free(k);
  126. tor_free(v);
  127. }
  128. } while (*string);
  129. *result = list;
  130. return 0;
  131. }
  132. /**
  133. * Free all the configuration lines on the linked list <b>front</b>.
  134. */
  135. void
  136. config_free_lines(config_line_t *front)
  137. {
  138. config_line_t *tmp;
  139. while (front) {
  140. tmp = front;
  141. front = tmp->next;
  142. tor_free(tmp->key);
  143. tor_free(tmp->value);
  144. tor_free(tmp);
  145. }
  146. }
  147. /** As config_find_option, but return a non-const pointer. */
  148. config_var_t *
  149. config_find_option_mutable(config_format_t *fmt, const char *key)
  150. {
  151. int i;
  152. size_t keylen = strlen(key);
  153. if (!keylen)
  154. return NULL; /* if they say "--" on the command line, it's not an option */
  155. /* First, check for an exact (case-insensitive) match */
  156. for (i=0; fmt->vars[i].name; ++i) {
  157. if (!strcasecmp(key, fmt->vars[i].name)) {
  158. return &fmt->vars[i];
  159. }
  160. }
  161. /* If none, check for an abbreviated match */
  162. for (i=0; fmt->vars[i].name; ++i) {
  163. if (!strncasecmp(key, fmt->vars[i].name, keylen)) {
  164. log_warn(LD_CONFIG, "The abbreviation '%s' is deprecated. "
  165. "Please use '%s' instead",
  166. key, fmt->vars[i].name);
  167. return &fmt->vars[i];
  168. }
  169. }
  170. /* Okay, unrecognized option */
  171. return NULL;
  172. }
  173. /** If <b>key</b> is a configuration option, return the corresponding const
  174. * config_var_t. Otherwise, if <b>key</b> is a non-standard abbreviation,
  175. * warn, and return the corresponding const config_var_t. Otherwise return
  176. * NULL.
  177. */
  178. const config_var_t *
  179. config_find_option(const config_format_t *fmt, const char *key)
  180. {
  181. return config_find_option_mutable((config_format_t*)fmt, key);
  182. }
  183. /** Return the number of option entries in <b>fmt</b>. */
  184. static int
  185. config_count_options(const config_format_t *fmt)
  186. {
  187. int i;
  188. for (i=0; fmt->vars[i].name; ++i)
  189. ;
  190. return i;
  191. }
  192. /*
  193. * Functions to assign config options.
  194. */
  195. /** <b>c</b>-\>key is known to be a real key. Update <b>options</b>
  196. * with <b>c</b>-\>value and return 0, or return -1 if bad value.
  197. *
  198. * Called from config_assign_line() and option_reset().
  199. */
  200. static int
  201. config_assign_value(const config_format_t *fmt, void *options,
  202. config_line_t *c, char **msg)
  203. {
  204. int i, ok;
  205. const config_var_t *var;
  206. void *lvalue;
  207. int *csv_int;
  208. smartlist_t *csv_str;
  209. CONFIG_CHECK(fmt, options);
  210. var = config_find_option(fmt, c->key);
  211. tor_assert(var);
  212. lvalue = STRUCT_VAR_P(options, var->var_offset);
  213. switch (var->type) {
  214. case CONFIG_TYPE_PORT:
  215. if (!strcasecmp(c->value, "auto")) {
  216. *(int *)lvalue = CFG_AUTO_PORT;
  217. break;
  218. }
  219. /* fall through */
  220. case CONFIG_TYPE_INT:
  221. case CONFIG_TYPE_UINT:
  222. i = (int)tor_parse_long(c->value, 10,
  223. var->type==CONFIG_TYPE_INT ? INT_MIN : 0,
  224. var->type==CONFIG_TYPE_PORT ? 65535 : INT_MAX,
  225. &ok, NULL);
  226. if (!ok) {
  227. tor_asprintf(msg,
  228. "Int keyword '%s %s' is malformed or out of bounds.",
  229. c->key, c->value);
  230. return -1;
  231. }
  232. *(int *)lvalue = i;
  233. break;
  234. case CONFIG_TYPE_INTERVAL: {
  235. i = config_parse_interval(c->value, &ok);
  236. if (!ok) {
  237. tor_asprintf(msg,
  238. "Interval '%s %s' is malformed or out of bounds.",
  239. c->key, c->value);
  240. return -1;
  241. }
  242. *(int *)lvalue = i;
  243. break;
  244. }
  245. case CONFIG_TYPE_MSEC_INTERVAL: {
  246. i = config_parse_msec_interval(c->value, &ok);
  247. if (!ok) {
  248. tor_asprintf(msg,
  249. "Msec interval '%s %s' is malformed or out of bounds.",
  250. c->key, c->value);
  251. return -1;
  252. }
  253. *(int *)lvalue = i;
  254. break;
  255. }
  256. case CONFIG_TYPE_MEMUNIT: {
  257. uint64_t u64 = config_parse_memunit(c->value, &ok);
  258. if (!ok) {
  259. tor_asprintf(msg,
  260. "Value '%s %s' is malformed or out of bounds.",
  261. c->key, c->value);
  262. return -1;
  263. }
  264. *(uint64_t *)lvalue = u64;
  265. break;
  266. }
  267. case CONFIG_TYPE_BOOL:
  268. i = (int)tor_parse_long(c->value, 10, 0, 1, &ok, NULL);
  269. if (!ok) {
  270. tor_asprintf(msg,
  271. "Boolean '%s %s' expects 0 or 1.",
  272. c->key, c->value);
  273. return -1;
  274. }
  275. *(int *)lvalue = i;
  276. break;
  277. case CONFIG_TYPE_AUTOBOOL:
  278. if (!strcmp(c->value, "auto"))
  279. *(int *)lvalue = -1;
  280. else if (!strcmp(c->value, "0"))
  281. *(int *)lvalue = 0;
  282. else if (!strcmp(c->value, "1"))
  283. *(int *)lvalue = 1;
  284. else {
  285. tor_asprintf(msg, "Boolean '%s %s' expects 0, 1, or 'auto'.",
  286. c->key, c->value);
  287. return -1;
  288. }
  289. break;
  290. case CONFIG_TYPE_STRING:
  291. case CONFIG_TYPE_FILENAME:
  292. tor_free(*(char **)lvalue);
  293. *(char **)lvalue = tor_strdup(c->value);
  294. break;
  295. case CONFIG_TYPE_DOUBLE:
  296. *(double *)lvalue = atof(c->value);
  297. break;
  298. case CONFIG_TYPE_ISOTIME:
  299. if (parse_iso_time(c->value, (time_t *)lvalue)) {
  300. tor_asprintf(msg,
  301. "Invalid time '%s' for keyword '%s'", c->value, c->key);
  302. return -1;
  303. }
  304. break;
  305. case CONFIG_TYPE_ROUTERSET:
  306. if (*(routerset_t**)lvalue) {
  307. routerset_free(*(routerset_t**)lvalue);
  308. }
  309. *(routerset_t**)lvalue = routerset_new();
  310. if (routerset_parse(*(routerset_t**)lvalue, c->value, c->key)<0) {
  311. tor_asprintf(msg, "Invalid exit list '%s' for option '%s'",
  312. c->value, c->key);
  313. return -1;
  314. }
  315. break;
  316. case CONFIG_TYPE_CSV:
  317. if (*(smartlist_t**)lvalue) {
  318. SMARTLIST_FOREACH(*(smartlist_t**)lvalue, char *, cp, tor_free(cp));
  319. smartlist_clear(*(smartlist_t**)lvalue);
  320. } else {
  321. *(smartlist_t**)lvalue = smartlist_new();
  322. }
  323. smartlist_split_string(*(smartlist_t**)lvalue, c->value, ",",
  324. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  325. break;
  326. case CONFIG_TYPE_CSV_INTERVAL:
  327. if (*(smartlist_t**)lvalue) {
  328. SMARTLIST_FOREACH(*(smartlist_t**)lvalue, int *, cp, tor_free(cp));
  329. smartlist_clear(*(smartlist_t**)lvalue);
  330. } else {
  331. *(smartlist_t**)lvalue = smartlist_new();
  332. }
  333. csv_str = smartlist_new();
  334. smartlist_split_string(csv_str, c->value, ",",
  335. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  336. SMARTLIST_FOREACH_BEGIN(csv_str, char *, str)
  337. {
  338. i = config_parse_interval(str, &ok);
  339. if (!ok) {
  340. tor_asprintf(msg,
  341. "Interval in '%s %s' is malformed or out of bounds.",
  342. c->key, c->value);
  343. SMARTLIST_FOREACH(csv_str, char *, cp, tor_free(cp));
  344. smartlist_clear(csv_str);
  345. return -1;
  346. }
  347. csv_int = tor_malloc_zero(sizeof(int));
  348. *csv_int = i;
  349. smartlist_add(*(smartlist_t**)lvalue, csv_int);
  350. }
  351. SMARTLIST_FOREACH_END(str);
  352. SMARTLIST_FOREACH(csv_str, char *, cp, tor_free(cp));
  353. smartlist_clear(csv_str);
  354. break;
  355. case CONFIG_TYPE_LINELIST:
  356. case CONFIG_TYPE_LINELIST_S:
  357. {
  358. config_line_t *lastval = *(config_line_t**)lvalue;
  359. if (lastval && lastval->fragile) {
  360. if (c->command != CONFIG_LINE_APPEND) {
  361. config_free_lines(lastval);
  362. *(config_line_t**)lvalue = NULL;
  363. } else {
  364. lastval->fragile = 0;
  365. }
  366. }
  367. config_line_append((config_line_t**)lvalue, c->key, c->value);
  368. }
  369. break;
  370. case CONFIG_TYPE_OBSOLETE:
  371. log_warn(LD_CONFIG, "Skipping obsolete configuration option '%s'", c->key);
  372. break;
  373. case CONFIG_TYPE_LINELIST_V:
  374. tor_asprintf(msg,
  375. "You may not provide a value for virtual option '%s'", c->key);
  376. return -1;
  377. default:
  378. tor_assert(0);
  379. break;
  380. }
  381. return 0;
  382. }
  383. /** Mark every linelist in <b>options</b> "fragile", so that fresh assignments
  384. * to it will replace old ones. */
  385. static void
  386. config_mark_lists_fragile(const config_format_t *fmt, void *options)
  387. {
  388. int i;
  389. tor_assert(fmt);
  390. tor_assert(options);
  391. for (i = 0; fmt->vars[i].name; ++i) {
  392. const config_var_t *var = &fmt->vars[i];
  393. config_line_t *list;
  394. if (var->type != CONFIG_TYPE_LINELIST &&
  395. var->type != CONFIG_TYPE_LINELIST_V)
  396. continue;
  397. list = *(config_line_t **)STRUCT_VAR_P(options, var->var_offset);
  398. if (list)
  399. list->fragile = 1;
  400. }
  401. }
  402. /** If <b>c</b> is a syntactically valid configuration line, update
  403. * <b>options</b> with its value and return 0. Otherwise return -1 for bad
  404. * key, -2 for bad value.
  405. *
  406. * If <b>clear_first</b> is set, clear the value first. Then if
  407. * <b>use_defaults</b> is set, set the value to the default.
  408. *
  409. * Called from config_assign().
  410. */
  411. static int
  412. config_assign_line(const config_format_t *fmt, void *options,
  413. config_line_t *c, int use_defaults,
  414. int clear_first, bitarray_t *options_seen, char **msg)
  415. {
  416. const config_var_t *var;
  417. CONFIG_CHECK(fmt, options);
  418. var = config_find_option(fmt, c->key);
  419. if (!var) {
  420. if (fmt->extra) {
  421. void *lvalue = STRUCT_VAR_P(options, fmt->extra->var_offset);
  422. log_info(LD_CONFIG,
  423. "Found unrecognized option '%s'; saving it.", c->key);
  424. config_line_append((config_line_t**)lvalue, c->key, c->value);
  425. return 0;
  426. } else {
  427. tor_asprintf(msg,
  428. "Unknown option '%s'. Failing.", c->key);
  429. return -1;
  430. }
  431. }
  432. /* Put keyword into canonical case. */
  433. if (strcmp(var->name, c->key)) {
  434. tor_free(c->key);
  435. c->key = tor_strdup(var->name);
  436. }
  437. if (!strlen(c->value)) {
  438. /* reset or clear it, then return */
  439. if (!clear_first) {
  440. if ((var->type == CONFIG_TYPE_LINELIST ||
  441. var->type == CONFIG_TYPE_LINELIST_S) &&
  442. c->command != CONFIG_LINE_CLEAR) {
  443. /* We got an empty linelist from the torrc or command line.
  444. As a special case, call this an error. Warn and ignore. */
  445. log_warn(LD_CONFIG,
  446. "Linelist option '%s' has no value. Skipping.", c->key);
  447. } else { /* not already cleared */
  448. config_reset(fmt, options, var, use_defaults);
  449. }
  450. }
  451. return 0;
  452. } else if (c->command == CONFIG_LINE_CLEAR && !clear_first) {
  453. config_reset(fmt, options, var, use_defaults);
  454. }
  455. if (options_seen && (var->type != CONFIG_TYPE_LINELIST &&
  456. var->type != CONFIG_TYPE_LINELIST_S)) {
  457. /* We're tracking which options we've seen, and this option is not
  458. * supposed to occur more than once. */
  459. int var_index = (int)(var - fmt->vars);
  460. if (bitarray_is_set(options_seen, var_index)) {
  461. log_warn(LD_CONFIG, "Option '%s' used more than once; all but the last "
  462. "value will be ignored.", var->name);
  463. }
  464. bitarray_set(options_seen, var_index);
  465. }
  466. if (config_assign_value(fmt, options, c, msg) < 0)
  467. return -2;
  468. return 0;
  469. }
  470. /** Restore the option named <b>key</b> in options to its default value.
  471. * Called from config_assign(). */
  472. static void
  473. config_reset_line(const config_format_t *fmt, void *options,
  474. const char *key, int use_defaults)
  475. {
  476. const config_var_t *var;
  477. CONFIG_CHECK(fmt, options);
  478. var = config_find_option(fmt, key);
  479. if (!var)
  480. return; /* give error on next pass. */
  481. config_reset(fmt, options, var, use_defaults);
  482. }
  483. /** Return true iff value needs to be quoted and escaped to be used in
  484. * a configuration file. */
  485. static int
  486. config_value_needs_escape(const char *value)
  487. {
  488. if (*value == '\"')
  489. return 1;
  490. while (*value) {
  491. switch (*value)
  492. {
  493. case '\r':
  494. case '\n':
  495. case '#':
  496. /* Note: quotes and backspaces need special handling when we are using
  497. * quotes, not otherwise, so they don't trigger escaping on their
  498. * own. */
  499. return 1;
  500. default:
  501. if (!TOR_ISPRINT(*value))
  502. return 1;
  503. }
  504. ++value;
  505. }
  506. return 0;
  507. }
  508. /** Return a newly allocated deep copy of the lines in <b>inp</b>. */
  509. config_line_t *
  510. config_lines_dup(const config_line_t *inp)
  511. {
  512. config_line_t *result = NULL;
  513. config_line_t **next_out = &result;
  514. while (inp) {
  515. *next_out = tor_malloc_zero(sizeof(config_line_t));
  516. (*next_out)->key = tor_strdup(inp->key);
  517. (*next_out)->value = tor_strdup(inp->value);
  518. inp = inp->next;
  519. next_out = &((*next_out)->next);
  520. }
  521. (*next_out) = NULL;
  522. return result;
  523. }
  524. /** Return newly allocated line or lines corresponding to <b>key</b> in the
  525. * configuration <b>options</b>. If <b>escape_val</b> is true and a
  526. * value needs to be quoted before it's put in a config file, quote and
  527. * escape that value. Return NULL if no such key exists. */
  528. config_line_t *
  529. config_get_assigned_option(const config_format_t *fmt, const void *options,
  530. const char *key, int escape_val)
  531. {
  532. const config_var_t *var;
  533. const void *value;
  534. config_line_t *result;
  535. smartlist_t *csv_str;
  536. tor_assert(options && key);
  537. CONFIG_CHECK(fmt, options);
  538. var = config_find_option(fmt, key);
  539. if (!var) {
  540. log_warn(LD_CONFIG, "Unknown option '%s'. Failing.", key);
  541. return NULL;
  542. }
  543. value = STRUCT_VAR_P(options, var->var_offset);
  544. result = tor_malloc_zero(sizeof(config_line_t));
  545. result->key = tor_strdup(var->name);
  546. switch (var->type)
  547. {
  548. case CONFIG_TYPE_STRING:
  549. case CONFIG_TYPE_FILENAME:
  550. if (*(char**)value) {
  551. result->value = tor_strdup(*(char**)value);
  552. } else {
  553. tor_free(result->key);
  554. tor_free(result);
  555. return NULL;
  556. }
  557. break;
  558. case CONFIG_TYPE_ISOTIME:
  559. if (*(time_t*)value) {
  560. result->value = tor_malloc(ISO_TIME_LEN+1);
  561. format_iso_time(result->value, *(time_t*)value);
  562. } else {
  563. tor_free(result->key);
  564. tor_free(result);
  565. }
  566. escape_val = 0; /* Can't need escape. */
  567. break;
  568. case CONFIG_TYPE_PORT:
  569. if (*(int*)value == CFG_AUTO_PORT) {
  570. result->value = tor_strdup("auto");
  571. escape_val = 0;
  572. break;
  573. }
  574. /* fall through */
  575. case CONFIG_TYPE_INTERVAL:
  576. case CONFIG_TYPE_MSEC_INTERVAL:
  577. case CONFIG_TYPE_UINT:
  578. case CONFIG_TYPE_INT:
  579. /* This means every or_options_t uint or bool element
  580. * needs to be an int. Not, say, a uint16_t or char. */
  581. tor_asprintf(&result->value, "%d", *(int*)value);
  582. escape_val = 0; /* Can't need escape. */
  583. break;
  584. case CONFIG_TYPE_MEMUNIT:
  585. tor_asprintf(&result->value, U64_FORMAT,
  586. U64_PRINTF_ARG(*(uint64_t*)value));
  587. escape_val = 0; /* Can't need escape. */
  588. break;
  589. case CONFIG_TYPE_DOUBLE:
  590. tor_asprintf(&result->value, "%f", *(double*)value);
  591. escape_val = 0; /* Can't need escape. */
  592. break;
  593. case CONFIG_TYPE_AUTOBOOL:
  594. if (*(int*)value == -1) {
  595. result->value = tor_strdup("auto");
  596. escape_val = 0;
  597. break;
  598. }
  599. /* fall through */
  600. case CONFIG_TYPE_BOOL:
  601. result->value = tor_strdup(*(int*)value ? "1" : "0");
  602. escape_val = 0; /* Can't need escape. */
  603. break;
  604. case CONFIG_TYPE_ROUTERSET:
  605. result->value = routerset_to_string(*(routerset_t**)value);
  606. break;
  607. case CONFIG_TYPE_CSV:
  608. if (*(smartlist_t**)value)
  609. result->value =
  610. smartlist_join_strings(*(smartlist_t**)value, ",", 0, NULL);
  611. else
  612. result->value = tor_strdup("");
  613. break;
  614. case CONFIG_TYPE_CSV_INTERVAL:
  615. if (*(smartlist_t**)value) {
  616. csv_str = smartlist_new();
  617. SMARTLIST_FOREACH_BEGIN(*(smartlist_t**)value, int *, i)
  618. {
  619. smartlist_add_asprintf(csv_str, "%d", *i);
  620. }
  621. SMARTLIST_FOREACH_END(i);
  622. result->value = smartlist_join_strings(csv_str, ",", 0, NULL);
  623. SMARTLIST_FOREACH(csv_str, char *, cp, tor_free(cp));
  624. smartlist_free(csv_str);
  625. } else
  626. result->value = tor_strdup("");
  627. break;
  628. case CONFIG_TYPE_OBSOLETE:
  629. log_fn(LOG_INFO, LD_CONFIG,
  630. "You asked me for the value of an obsolete config option '%s'.",
  631. key);
  632. tor_free(result->key);
  633. tor_free(result);
  634. return NULL;
  635. case CONFIG_TYPE_LINELIST_S:
  636. log_warn(LD_CONFIG,
  637. "Can't return context-sensitive '%s' on its own", key);
  638. tor_free(result->key);
  639. tor_free(result);
  640. return NULL;
  641. case CONFIG_TYPE_LINELIST:
  642. case CONFIG_TYPE_LINELIST_V:
  643. tor_free(result->key);
  644. tor_free(result);
  645. result = config_lines_dup(*(const config_line_t**)value);
  646. break;
  647. default:
  648. tor_free(result->key);
  649. tor_free(result);
  650. log_warn(LD_BUG,"Unknown type %d for known key '%s'",
  651. var->type, key);
  652. return NULL;
  653. }
  654. if (escape_val) {
  655. config_line_t *line;
  656. for (line = result; line; line = line->next) {
  657. if (line->value && config_value_needs_escape(line->value)) {
  658. char *newval = esc_for_log(line->value);
  659. tor_free(line->value);
  660. line->value = newval;
  661. }
  662. }
  663. }
  664. return result;
  665. }
  666. /** Iterate through the linked list of requested options <b>list</b>.
  667. * For each item, convert as appropriate and assign to <b>options</b>.
  668. * If an item is unrecognized, set *msg and return -1 immediately,
  669. * else return 0 for success.
  670. *
  671. * If <b>clear_first</b>, interpret config options as replacing (not
  672. * extending) their previous values. If <b>clear_first</b> is set,
  673. * then <b>use_defaults</b> to decide if you set to defaults after
  674. * clearing, or make the value 0 or NULL.
  675. *
  676. * Here are the use cases:
  677. * 1. A non-empty AllowInvalid line in your torrc. Appends to current
  678. * if linelist, replaces current if csv.
  679. * 2. An empty AllowInvalid line in your torrc. Should clear it.
  680. * 3. "RESETCONF AllowInvalid" sets it to default.
  681. * 4. "SETCONF AllowInvalid" makes it NULL.
  682. * 5. "SETCONF AllowInvalid=foo" clears it and sets it to "foo".
  683. *
  684. * Use_defaults Clear_first
  685. * 0 0 "append"
  686. * 1 0 undefined, don't use
  687. * 0 1 "set to null first"
  688. * 1 1 "set to defaults first"
  689. * Return 0 on success, -1 on bad key, -2 on bad value.
  690. *
  691. * As an additional special case, if a LINELIST config option has
  692. * no value and clear_first is 0, then warn and ignore it.
  693. */
  694. /*
  695. There are three call cases for config_assign() currently.
  696. Case one: Torrc entry
  697. options_init_from_torrc() calls config_assign(0, 0)
  698. calls config_assign_line(0, 0).
  699. if value is empty, calls config_reset(0) and returns.
  700. calls config_assign_value(), appends.
  701. Case two: setconf
  702. options_trial_assign() calls config_assign(0, 1)
  703. calls config_reset_line(0)
  704. calls config_reset(0)
  705. calls option_clear().
  706. calls config_assign_line(0, 1).
  707. if value is empty, returns.
  708. calls config_assign_value(), appends.
  709. Case three: resetconf
  710. options_trial_assign() calls config_assign(1, 1)
  711. calls config_reset_line(1)
  712. calls config_reset(1)
  713. calls option_clear().
  714. calls config_assign_value(default)
  715. calls config_assign_line(1, 1).
  716. returns.
  717. */
  718. int
  719. config_assign(const config_format_t *fmt, void *options, config_line_t *list,
  720. int use_defaults, int clear_first, char **msg)
  721. {
  722. config_line_t *p;
  723. bitarray_t *options_seen;
  724. const int n_options = config_count_options(fmt);
  725. CONFIG_CHECK(fmt, options);
  726. /* pass 1: normalize keys */
  727. for (p = list; p; p = p->next) {
  728. const char *full = config_expand_abbrev(fmt, p->key, 0, 1);
  729. if (strcmp(full,p->key)) {
  730. tor_free(p->key);
  731. p->key = tor_strdup(full);
  732. }
  733. }
  734. /* pass 2: if we're reading from a resetting source, clear all
  735. * mentioned config options, and maybe set to their defaults. */
  736. if (clear_first) {
  737. for (p = list; p; p = p->next)
  738. config_reset_line(fmt, options, p->key, use_defaults);
  739. }
  740. options_seen = bitarray_init_zero(n_options);
  741. /* pass 3: assign. */
  742. while (list) {
  743. int r;
  744. if ((r=config_assign_line(fmt, options, list, use_defaults,
  745. clear_first, options_seen, msg))) {
  746. bitarray_free(options_seen);
  747. return r;
  748. }
  749. list = list->next;
  750. }
  751. bitarray_free(options_seen);
  752. /** Now we're done assigning a group of options to the configuration.
  753. * Subsequent group assignments should _replace_ linelists, not extend
  754. * them. */
  755. config_mark_lists_fragile(fmt, options);
  756. return 0;
  757. }
  758. /** Reset config option <b>var</b> to 0, 0.0, NULL, or the equivalent.
  759. * Called from config_reset() and config_free(). */
  760. static void
  761. config_clear(const config_format_t *fmt, void *options,
  762. const config_var_t *var)
  763. {
  764. void *lvalue = STRUCT_VAR_P(options, var->var_offset);
  765. (void)fmt; /* unused */
  766. switch (var->type) {
  767. case CONFIG_TYPE_STRING:
  768. case CONFIG_TYPE_FILENAME:
  769. tor_free(*(char**)lvalue);
  770. break;
  771. case CONFIG_TYPE_DOUBLE:
  772. *(double*)lvalue = 0.0;
  773. break;
  774. case CONFIG_TYPE_ISOTIME:
  775. *(time_t*)lvalue = 0;
  776. break;
  777. case CONFIG_TYPE_INTERVAL:
  778. case CONFIG_TYPE_MSEC_INTERVAL:
  779. case CONFIG_TYPE_UINT:
  780. case CONFIG_TYPE_INT:
  781. case CONFIG_TYPE_PORT:
  782. case CONFIG_TYPE_BOOL:
  783. *(int*)lvalue = 0;
  784. break;
  785. case CONFIG_TYPE_AUTOBOOL:
  786. *(int*)lvalue = -1;
  787. break;
  788. case CONFIG_TYPE_MEMUNIT:
  789. *(uint64_t*)lvalue = 0;
  790. break;
  791. case CONFIG_TYPE_ROUTERSET:
  792. if (*(routerset_t**)lvalue) {
  793. routerset_free(*(routerset_t**)lvalue);
  794. *(routerset_t**)lvalue = NULL;
  795. }
  796. break;
  797. case CONFIG_TYPE_CSV:
  798. if (*(smartlist_t**)lvalue) {
  799. SMARTLIST_FOREACH(*(smartlist_t **)lvalue, char *, cp, tor_free(cp));
  800. smartlist_free(*(smartlist_t **)lvalue);
  801. *(smartlist_t **)lvalue = NULL;
  802. }
  803. break;
  804. case CONFIG_TYPE_CSV_INTERVAL:
  805. if (*(smartlist_t**)lvalue) {
  806. SMARTLIST_FOREACH(*(smartlist_t **)lvalue, int *, cp, tor_free(cp));
  807. smartlist_free(*(smartlist_t **)lvalue);
  808. *(smartlist_t **)lvalue = NULL;
  809. }
  810. break;
  811. case CONFIG_TYPE_LINELIST:
  812. case CONFIG_TYPE_LINELIST_S:
  813. config_free_lines(*(config_line_t **)lvalue);
  814. *(config_line_t **)lvalue = NULL;
  815. break;
  816. case CONFIG_TYPE_LINELIST_V:
  817. /* handled by linelist_s. */
  818. break;
  819. case CONFIG_TYPE_OBSOLETE:
  820. break;
  821. }
  822. }
  823. /** Clear the option indexed by <b>var</b> in <b>options</b>. Then if
  824. * <b>use_defaults</b>, set it to its default value.
  825. * Called by config_init() and option_reset_line() and option_assign_line(). */
  826. static void
  827. config_reset(const config_format_t *fmt, void *options,
  828. const config_var_t *var, int use_defaults)
  829. {
  830. config_line_t *c;
  831. char *msg = NULL;
  832. CONFIG_CHECK(fmt, options);
  833. config_clear(fmt, options, var); /* clear it first */
  834. if (!use_defaults)
  835. return; /* all done */
  836. if (var->initvalue) {
  837. c = tor_malloc_zero(sizeof(config_line_t));
  838. c->key = tor_strdup(var->name);
  839. c->value = tor_strdup(var->initvalue);
  840. if (config_assign_value(fmt, options, c, &msg) < 0) {
  841. log_warn(LD_BUG, "Failed to assign default: %s", msg);
  842. tor_free(msg); /* if this happens it's a bug */
  843. }
  844. config_free_lines(c);
  845. }
  846. }
  847. /** Release storage held by <b>options</b>. */
  848. void
  849. config_free(const config_format_t *fmt, void *options)
  850. {
  851. int i;
  852. if (!options)
  853. return;
  854. tor_assert(fmt);
  855. for (i=0; fmt->vars[i].name; ++i)
  856. config_clear(fmt, options, &(fmt->vars[i]));
  857. if (fmt->extra) {
  858. config_line_t **linep = STRUCT_VAR_P(options, fmt->extra->var_offset);
  859. config_free_lines(*linep);
  860. *linep = NULL;
  861. }
  862. tor_free(options);
  863. }
  864. /** Return true iff a and b contain identical keys and values in identical
  865. * order. */
  866. int
  867. config_lines_eq(config_line_t *a, config_line_t *b)
  868. {
  869. while (a && b) {
  870. if (strcasecmp(a->key, b->key) || strcmp(a->value, b->value))
  871. return 0;
  872. a = a->next;
  873. b = b->next;
  874. }
  875. if (a || b)
  876. return 0;
  877. return 1;
  878. }
  879. /** Return the number of lines in <b>a</b> whose key is <b>key</b>. */
  880. int
  881. config_count_key(const config_line_t *a, const char *key)
  882. {
  883. int n = 0;
  884. while (a) {
  885. if (!strcasecmp(a->key, key)) {
  886. ++n;
  887. }
  888. a = a->next;
  889. }
  890. return n;
  891. }
  892. /** Return true iff the option <b>name</b> has the same value in <b>o1</b>
  893. * and <b>o2</b>. Must not be called for LINELIST_S or OBSOLETE options.
  894. */
  895. int
  896. config_is_same(const config_format_t *fmt,
  897. const void *o1, const void *o2,
  898. const char *name)
  899. {
  900. config_line_t *c1, *c2;
  901. int r = 1;
  902. CONFIG_CHECK(fmt, o1);
  903. CONFIG_CHECK(fmt, o2);
  904. c1 = config_get_assigned_option(fmt, o1, name, 0);
  905. c2 = config_get_assigned_option(fmt, o2, name, 0);
  906. r = config_lines_eq(c1, c2);
  907. config_free_lines(c1);
  908. config_free_lines(c2);
  909. return r;
  910. }
  911. /** Copy storage held by <b>old</b> into a new or_options_t and return it. */
  912. void *
  913. config_dup(const config_format_t *fmt, const void *old)
  914. {
  915. void *newopts;
  916. int i;
  917. config_line_t *line;
  918. newopts = config_new(fmt);
  919. for (i=0; fmt->vars[i].name; ++i) {
  920. if (fmt->vars[i].type == CONFIG_TYPE_LINELIST_S)
  921. continue;
  922. if (fmt->vars[i].type == CONFIG_TYPE_OBSOLETE)
  923. continue;
  924. line = config_get_assigned_option(fmt, old, fmt->vars[i].name, 0);
  925. if (line) {
  926. char *msg = NULL;
  927. if (config_assign(fmt, newopts, line, 0, 0, &msg) < 0) {
  928. log_err(LD_BUG, "config_get_assigned_option() generated "
  929. "something we couldn't config_assign(): %s", msg);
  930. tor_free(msg);
  931. tor_assert(0);
  932. }
  933. }
  934. config_free_lines(line);
  935. }
  936. return newopts;
  937. }
  938. /** Set all vars in the configuration object <b>options</b> to their default
  939. * values. */
  940. void
  941. config_init(const config_format_t *fmt, void *options)
  942. {
  943. int i;
  944. const config_var_t *var;
  945. CONFIG_CHECK(fmt, options);
  946. for (i=0; fmt->vars[i].name; ++i) {
  947. var = &fmt->vars[i];
  948. if (!var->initvalue)
  949. continue; /* defaults to NULL or 0 */
  950. config_reset(fmt, options, var, 1);
  951. }
  952. }
  953. /** Allocate and return a new string holding the written-out values of the vars
  954. * in 'options'. If 'minimal', do not write out any default-valued vars.
  955. * Else, if comment_defaults, write default values as comments.
  956. */
  957. char *
  958. config_dump(const config_format_t *fmt, const void *default_options,
  959. const void *options, int minimal,
  960. int comment_defaults)
  961. {
  962. smartlist_t *elements;
  963. const void *defaults = default_options;
  964. void *defaults_tmp = NULL;
  965. config_line_t *line, *assigned;
  966. char *result;
  967. int i;
  968. char *msg = NULL;
  969. if (defaults == NULL) {
  970. defaults = defaults_tmp = config_new(fmt);
  971. config_init(fmt, defaults_tmp);
  972. }
  973. /* XXX use a 1 here so we don't add a new log line while dumping */
  974. if (default_options == NULL) {
  975. if (fmt->validate_fn(NULL, defaults_tmp, 1, &msg) < 0) {
  976. log_err(LD_BUG, "Failed to validate default config.");
  977. tor_free(msg);
  978. tor_assert(0);
  979. }
  980. }
  981. elements = smartlist_new();
  982. for (i=0; fmt->vars[i].name; ++i) {
  983. int comment_option = 0;
  984. if (fmt->vars[i].type == CONFIG_TYPE_OBSOLETE ||
  985. fmt->vars[i].type == CONFIG_TYPE_LINELIST_S)
  986. continue;
  987. /* Don't save 'hidden' control variables. */
  988. if (!strcmpstart(fmt->vars[i].name, "__"))
  989. continue;
  990. if (minimal && config_is_same(fmt, options, defaults, fmt->vars[i].name))
  991. continue;
  992. else if (comment_defaults &&
  993. config_is_same(fmt, options, defaults, fmt->vars[i].name))
  994. comment_option = 1;
  995. line = assigned =
  996. config_get_assigned_option(fmt, options, fmt->vars[i].name, 1);
  997. for (; line; line = line->next) {
  998. smartlist_add_asprintf(elements, "%s%s %s\n",
  999. comment_option ? "# " : "",
  1000. line->key, line->value);
  1001. }
  1002. config_free_lines(assigned);
  1003. }
  1004. if (fmt->extra) {
  1005. line = *(config_line_t**)STRUCT_VAR_P(options, fmt->extra->var_offset);
  1006. for (; line; line = line->next) {
  1007. smartlist_add_asprintf(elements, "%s %s\n", line->key, line->value);
  1008. }
  1009. }
  1010. result = smartlist_join_strings(elements, "", 0, NULL);
  1011. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  1012. smartlist_free(elements);
  1013. if (defaults_tmp)
  1014. config_free(fmt, defaults_tmp);
  1015. return result;
  1016. }
  1017. /** Mapping from a unit name to a multiplier for converting that unit into a
  1018. * base unit. Used by config_parse_unit. */
  1019. struct unit_table_t {
  1020. const char *unit; /**< The name of the unit */
  1021. uint64_t multiplier; /**< How many of the base unit appear in this unit */
  1022. };
  1023. /** Table to map the names of memory units to the number of bytes they
  1024. * contain. */
  1025. static struct unit_table_t memory_units[] = {
  1026. { "", 1 },
  1027. { "b", 1<< 0 },
  1028. { "byte", 1<< 0 },
  1029. { "bytes", 1<< 0 },
  1030. { "kb", 1<<10 },
  1031. { "kbyte", 1<<10 },
  1032. { "kbytes", 1<<10 },
  1033. { "kilobyte", 1<<10 },
  1034. { "kilobytes", 1<<10 },
  1035. { "m", 1<<20 },
  1036. { "mb", 1<<20 },
  1037. { "mbyte", 1<<20 },
  1038. { "mbytes", 1<<20 },
  1039. { "megabyte", 1<<20 },
  1040. { "megabytes", 1<<20 },
  1041. { "gb", 1<<30 },
  1042. { "gbyte", 1<<30 },
  1043. { "gbytes", 1<<30 },
  1044. { "gigabyte", 1<<30 },
  1045. { "gigabytes", 1<<30 },
  1046. { "tb", U64_LITERAL(1)<<40 },
  1047. { "terabyte", U64_LITERAL(1)<<40 },
  1048. { "terabytes", U64_LITERAL(1)<<40 },
  1049. { NULL, 0 },
  1050. };
  1051. /** Table to map the names of time units to the number of seconds they
  1052. * contain. */
  1053. static struct unit_table_t time_units[] = {
  1054. { "", 1 },
  1055. { "second", 1 },
  1056. { "seconds", 1 },
  1057. { "minute", 60 },
  1058. { "minutes", 60 },
  1059. { "hour", 60*60 },
  1060. { "hours", 60*60 },
  1061. { "day", 24*60*60 },
  1062. { "days", 24*60*60 },
  1063. { "week", 7*24*60*60 },
  1064. { "weeks", 7*24*60*60 },
  1065. { "month", 2629728, }, /* about 30.437 days */
  1066. { "months", 2629728, },
  1067. { NULL, 0 },
  1068. };
  1069. /** Table to map the names of time units to the number of milliseconds
  1070. * they contain. */
  1071. static struct unit_table_t time_msec_units[] = {
  1072. { "", 1 },
  1073. { "msec", 1 },
  1074. { "millisecond", 1 },
  1075. { "milliseconds", 1 },
  1076. { "second", 1000 },
  1077. { "seconds", 1000 },
  1078. { "minute", 60*1000 },
  1079. { "minutes", 60*1000 },
  1080. { "hour", 60*60*1000 },
  1081. { "hours", 60*60*1000 },
  1082. { "day", 24*60*60*1000 },
  1083. { "days", 24*60*60*1000 },
  1084. { "week", 7*24*60*60*1000 },
  1085. { "weeks", 7*24*60*60*1000 },
  1086. { NULL, 0 },
  1087. };
  1088. /** Parse a string <b>val</b> containing a number, zero or more
  1089. * spaces, and an optional unit string. If the unit appears in the
  1090. * table <b>u</b>, then multiply the number by the unit multiplier.
  1091. * On success, set *<b>ok</b> to 1 and return this product.
  1092. * Otherwise, set *<b>ok</b> to 0.
  1093. */
  1094. static uint64_t
  1095. config_parse_units(const char *val, struct unit_table_t *u, int *ok)
  1096. {
  1097. uint64_t v = 0;
  1098. double d = 0;
  1099. int use_float = 0;
  1100. char *cp;
  1101. tor_assert(ok);
  1102. v = tor_parse_uint64(val, 10, 0, UINT64_MAX, ok, &cp);
  1103. if (!*ok || (cp && *cp == '.')) {
  1104. d = tor_parse_double(val, 0, UINT64_MAX, ok, &cp);
  1105. if (!*ok)
  1106. goto done;
  1107. use_float = 1;
  1108. }
  1109. if (!cp) {
  1110. *ok = 1;
  1111. v = use_float ? DBL_TO_U64(d) : v;
  1112. goto done;
  1113. }
  1114. cp = (char*) eat_whitespace(cp);
  1115. for ( ;u->unit;++u) {
  1116. if (!strcasecmp(u->unit, cp)) {
  1117. if (use_float)
  1118. v = u->multiplier * d;
  1119. else
  1120. v *= u->multiplier;
  1121. *ok = 1;
  1122. goto done;
  1123. }
  1124. }
  1125. log_warn(LD_CONFIG, "Unknown unit '%s'.", cp);
  1126. *ok = 0;
  1127. done:
  1128. if (*ok)
  1129. return v;
  1130. else
  1131. return 0;
  1132. }
  1133. /** Parse a string in the format "number unit", where unit is a unit of
  1134. * information (byte, KB, M, etc). On success, set *<b>ok</b> to true
  1135. * and return the number of bytes specified. Otherwise, set
  1136. * *<b>ok</b> to false and return 0. */
  1137. static uint64_t
  1138. config_parse_memunit(const char *s, int *ok)
  1139. {
  1140. uint64_t u = config_parse_units(s, memory_units, ok);
  1141. return u;
  1142. }
  1143. /** Parse a string in the format "number unit", where unit is a unit of
  1144. * time in milliseconds. On success, set *<b>ok</b> to true and return
  1145. * the number of milliseconds in the provided interval. Otherwise, set
  1146. * *<b>ok</b> to 0 and return -1. */
  1147. static int
  1148. config_parse_msec_interval(const char *s, int *ok)
  1149. {
  1150. uint64_t r;
  1151. r = config_parse_units(s, time_msec_units, ok);
  1152. if (!ok)
  1153. return -1;
  1154. if (r > INT_MAX) {
  1155. log_warn(LD_CONFIG, "Msec interval '%s' is too long", s);
  1156. *ok = 0;
  1157. return -1;
  1158. }
  1159. return (int)r;
  1160. }
  1161. /** Parse a string in the format "number unit", where unit is a unit of time.
  1162. * On success, set *<b>ok</b> to true and return the number of seconds in
  1163. * the provided interval. Otherwise, set *<b>ok</b> to 0 and return -1.
  1164. */
  1165. static int
  1166. config_parse_interval(const char *s, int *ok)
  1167. {
  1168. uint64_t r;
  1169. r = config_parse_units(s, time_units, ok);
  1170. if (!ok)
  1171. return -1;
  1172. if (r > INT_MAX) {
  1173. log_warn(LD_CONFIG, "Interval '%s' is too long", s);
  1174. *ok = 0;
  1175. return -1;
  1176. }
  1177. return (int)r;
  1178. }