confparse.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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. char *s;
  537. tor_assert(options && key);
  538. CONFIG_CHECK(fmt, options);
  539. var = config_find_option(fmt, key);
  540. if (!var) {
  541. log_warn(LD_CONFIG, "Unknown option '%s'. Failing.", key);
  542. return NULL;
  543. }
  544. value = STRUCT_VAR_P(options, var->var_offset);
  545. result = tor_malloc_zero(sizeof(config_line_t));
  546. result->key = tor_strdup(var->name);
  547. switch (var->type)
  548. {
  549. case CONFIG_TYPE_STRING:
  550. case CONFIG_TYPE_FILENAME:
  551. if (*(char**)value) {
  552. result->value = tor_strdup(*(char**)value);
  553. } else {
  554. tor_free(result->key);
  555. tor_free(result);
  556. return NULL;
  557. }
  558. break;
  559. case CONFIG_TYPE_ISOTIME:
  560. if (*(time_t*)value) {
  561. result->value = tor_malloc(ISO_TIME_LEN+1);
  562. format_iso_time(result->value, *(time_t*)value);
  563. } else {
  564. tor_free(result->key);
  565. tor_free(result);
  566. }
  567. escape_val = 0; /* Can't need escape. */
  568. break;
  569. case CONFIG_TYPE_PORT:
  570. if (*(int*)value == CFG_AUTO_PORT) {
  571. result->value = tor_strdup("auto");
  572. escape_val = 0;
  573. break;
  574. }
  575. /* fall through */
  576. case CONFIG_TYPE_INTERVAL:
  577. case CONFIG_TYPE_MSEC_INTERVAL:
  578. case CONFIG_TYPE_UINT:
  579. case CONFIG_TYPE_INT:
  580. /* This means every or_options_t uint or bool element
  581. * needs to be an int. Not, say, a uint16_t or char. */
  582. tor_asprintf(&result->value, "%d", *(int*)value);
  583. escape_val = 0; /* Can't need escape. */
  584. break;
  585. case CONFIG_TYPE_MEMUNIT:
  586. tor_asprintf(&result->value, U64_FORMAT,
  587. U64_PRINTF_ARG(*(uint64_t*)value));
  588. escape_val = 0; /* Can't need escape. */
  589. break;
  590. case CONFIG_TYPE_DOUBLE:
  591. tor_asprintf(&result->value, "%f", *(double*)value);
  592. escape_val = 0; /* Can't need escape. */
  593. break;
  594. case CONFIG_TYPE_AUTOBOOL:
  595. if (*(int*)value == -1) {
  596. result->value = tor_strdup("auto");
  597. escape_val = 0;
  598. break;
  599. }
  600. /* fall through */
  601. case CONFIG_TYPE_BOOL:
  602. result->value = tor_strdup(*(int*)value ? "1" : "0");
  603. escape_val = 0; /* Can't need escape. */
  604. break;
  605. case CONFIG_TYPE_ROUTERSET:
  606. result->value = routerset_to_string(*(routerset_t**)value);
  607. break;
  608. case CONFIG_TYPE_CSV:
  609. if (*(smartlist_t**)value)
  610. result->value =
  611. smartlist_join_strings(*(smartlist_t**)value, ",", 0, NULL);
  612. else
  613. result->value = tor_strdup("");
  614. break;
  615. case CONFIG_TYPE_CSV_INTERVAL:
  616. if (*(smartlist_t**)value) {
  617. csv_str = smartlist_new();
  618. SMARTLIST_FOREACH_BEGIN(*(smartlist_t**)value, int *, i)
  619. {
  620. tor_asprintf(&s, "%d", *i);
  621. smartlist_add(csv_str, s);
  622. }
  623. SMARTLIST_FOREACH_END(i);
  624. result->value = smartlist_join_strings(csv_str, ",", 0, NULL);
  625. SMARTLIST_FOREACH(csv_str, char *, cp, tor_free(cp));
  626. smartlist_free(csv_str);
  627. } else
  628. result->value = tor_strdup("");
  629. break;
  630. case CONFIG_TYPE_OBSOLETE:
  631. log_fn(LOG_INFO, LD_CONFIG,
  632. "You asked me for the value of an obsolete config option '%s'.",
  633. key);
  634. tor_free(result->key);
  635. tor_free(result);
  636. return NULL;
  637. case CONFIG_TYPE_LINELIST_S:
  638. log_warn(LD_CONFIG,
  639. "Can't return context-sensitive '%s' on its own", key);
  640. tor_free(result->key);
  641. tor_free(result);
  642. return NULL;
  643. case CONFIG_TYPE_LINELIST:
  644. case CONFIG_TYPE_LINELIST_V:
  645. tor_free(result->key);
  646. tor_free(result);
  647. result = config_lines_dup(*(const config_line_t**)value);
  648. break;
  649. default:
  650. tor_free(result->key);
  651. tor_free(result);
  652. log_warn(LD_BUG,"Unknown type %d for known key '%s'",
  653. var->type, key);
  654. return NULL;
  655. }
  656. if (escape_val) {
  657. config_line_t *line;
  658. for (line = result; line; line = line->next) {
  659. if (line->value && config_value_needs_escape(line->value)) {
  660. char *newval = esc_for_log(line->value);
  661. tor_free(line->value);
  662. line->value = newval;
  663. }
  664. }
  665. }
  666. return result;
  667. }
  668. /** Iterate through the linked list of requested options <b>list</b>.
  669. * For each item, convert as appropriate and assign to <b>options</b>.
  670. * If an item is unrecognized, set *msg and return -1 immediately,
  671. * else return 0 for success.
  672. *
  673. * If <b>clear_first</b>, interpret config options as replacing (not
  674. * extending) their previous values. If <b>clear_first</b> is set,
  675. * then <b>use_defaults</b> to decide if you set to defaults after
  676. * clearing, or make the value 0 or NULL.
  677. *
  678. * Here are the use cases:
  679. * 1. A non-empty AllowInvalid line in your torrc. Appends to current
  680. * if linelist, replaces current if csv.
  681. * 2. An empty AllowInvalid line in your torrc. Should clear it.
  682. * 3. "RESETCONF AllowInvalid" sets it to default.
  683. * 4. "SETCONF AllowInvalid" makes it NULL.
  684. * 5. "SETCONF AllowInvalid=foo" clears it and sets it to "foo".
  685. *
  686. * Use_defaults Clear_first
  687. * 0 0 "append"
  688. * 1 0 undefined, don't use
  689. * 0 1 "set to null first"
  690. * 1 1 "set to defaults first"
  691. * Return 0 on success, -1 on bad key, -2 on bad value.
  692. *
  693. * As an additional special case, if a LINELIST config option has
  694. * no value and clear_first is 0, then warn and ignore it.
  695. */
  696. /*
  697. There are three call cases for config_assign() currently.
  698. Case one: Torrc entry
  699. options_init_from_torrc() calls config_assign(0, 0)
  700. calls config_assign_line(0, 0).
  701. if value is empty, calls config_reset(0) and returns.
  702. calls config_assign_value(), appends.
  703. Case two: setconf
  704. options_trial_assign() calls config_assign(0, 1)
  705. calls config_reset_line(0)
  706. calls config_reset(0)
  707. calls option_clear().
  708. calls config_assign_line(0, 1).
  709. if value is empty, returns.
  710. calls config_assign_value(), appends.
  711. Case three: resetconf
  712. options_trial_assign() calls config_assign(1, 1)
  713. calls config_reset_line(1)
  714. calls config_reset(1)
  715. calls option_clear().
  716. calls config_assign_value(default)
  717. calls config_assign_line(1, 1).
  718. returns.
  719. */
  720. int
  721. config_assign(const config_format_t *fmt, void *options, config_line_t *list,
  722. int use_defaults, int clear_first, char **msg)
  723. {
  724. config_line_t *p;
  725. bitarray_t *options_seen;
  726. const int n_options = config_count_options(fmt);
  727. CONFIG_CHECK(fmt, options);
  728. /* pass 1: normalize keys */
  729. for (p = list; p; p = p->next) {
  730. const char *full = config_expand_abbrev(fmt, p->key, 0, 1);
  731. if (strcmp(full,p->key)) {
  732. tor_free(p->key);
  733. p->key = tor_strdup(full);
  734. }
  735. }
  736. /* pass 2: if we're reading from a resetting source, clear all
  737. * mentioned config options, and maybe set to their defaults. */
  738. if (clear_first) {
  739. for (p = list; p; p = p->next)
  740. config_reset_line(fmt, options, p->key, use_defaults);
  741. }
  742. options_seen = bitarray_init_zero(n_options);
  743. /* pass 3: assign. */
  744. while (list) {
  745. int r;
  746. if ((r=config_assign_line(fmt, options, list, use_defaults,
  747. clear_first, options_seen, msg))) {
  748. bitarray_free(options_seen);
  749. return r;
  750. }
  751. list = list->next;
  752. }
  753. bitarray_free(options_seen);
  754. /** Now we're done assigning a group of options to the configuration.
  755. * Subsequent group assignments should _replace_ linelists, not extend
  756. * them. */
  757. config_mark_lists_fragile(fmt, options);
  758. return 0;
  759. }
  760. /** Reset config option <b>var</b> to 0, 0.0, NULL, or the equivalent.
  761. * Called from config_reset() and config_free(). */
  762. static void
  763. config_clear(const config_format_t *fmt, void *options,
  764. const config_var_t *var)
  765. {
  766. void *lvalue = STRUCT_VAR_P(options, var->var_offset);
  767. (void)fmt; /* unused */
  768. switch (var->type) {
  769. case CONFIG_TYPE_STRING:
  770. case CONFIG_TYPE_FILENAME:
  771. tor_free(*(char**)lvalue);
  772. break;
  773. case CONFIG_TYPE_DOUBLE:
  774. *(double*)lvalue = 0.0;
  775. break;
  776. case CONFIG_TYPE_ISOTIME:
  777. *(time_t*)lvalue = 0;
  778. break;
  779. case CONFIG_TYPE_INTERVAL:
  780. case CONFIG_TYPE_MSEC_INTERVAL:
  781. case CONFIG_TYPE_UINT:
  782. case CONFIG_TYPE_INT:
  783. case CONFIG_TYPE_PORT:
  784. case CONFIG_TYPE_BOOL:
  785. *(int*)lvalue = 0;
  786. break;
  787. case CONFIG_TYPE_AUTOBOOL:
  788. *(int*)lvalue = -1;
  789. break;
  790. case CONFIG_TYPE_MEMUNIT:
  791. *(uint64_t*)lvalue = 0;
  792. break;
  793. case CONFIG_TYPE_ROUTERSET:
  794. if (*(routerset_t**)lvalue) {
  795. routerset_free(*(routerset_t**)lvalue);
  796. *(routerset_t**)lvalue = NULL;
  797. }
  798. break;
  799. case CONFIG_TYPE_CSV:
  800. if (*(smartlist_t**)lvalue) {
  801. SMARTLIST_FOREACH(*(smartlist_t **)lvalue, char *, cp, tor_free(cp));
  802. smartlist_free(*(smartlist_t **)lvalue);
  803. *(smartlist_t **)lvalue = NULL;
  804. }
  805. break;
  806. case CONFIG_TYPE_CSV_INTERVAL:
  807. if (*(smartlist_t**)lvalue) {
  808. SMARTLIST_FOREACH(*(smartlist_t **)lvalue, int *, cp, tor_free(cp));
  809. smartlist_free(*(smartlist_t **)lvalue);
  810. *(smartlist_t **)lvalue = NULL;
  811. }
  812. break;
  813. case CONFIG_TYPE_LINELIST:
  814. case CONFIG_TYPE_LINELIST_S:
  815. config_free_lines(*(config_line_t **)lvalue);
  816. *(config_line_t **)lvalue = NULL;
  817. break;
  818. case CONFIG_TYPE_LINELIST_V:
  819. /* handled by linelist_s. */
  820. break;
  821. case CONFIG_TYPE_OBSOLETE:
  822. break;
  823. }
  824. }
  825. /** Clear the option indexed by <b>var</b> in <b>options</b>. Then if
  826. * <b>use_defaults</b>, set it to its default value.
  827. * Called by config_init() and option_reset_line() and option_assign_line(). */
  828. static void
  829. config_reset(const config_format_t *fmt, void *options,
  830. const config_var_t *var, int use_defaults)
  831. {
  832. config_line_t *c;
  833. char *msg = NULL;
  834. CONFIG_CHECK(fmt, options);
  835. config_clear(fmt, options, var); /* clear it first */
  836. if (!use_defaults)
  837. return; /* all done */
  838. if (var->initvalue) {
  839. c = tor_malloc_zero(sizeof(config_line_t));
  840. c->key = tor_strdup(var->name);
  841. c->value = tor_strdup(var->initvalue);
  842. if (config_assign_value(fmt, options, c, &msg) < 0) {
  843. log_warn(LD_BUG, "Failed to assign default: %s", msg);
  844. tor_free(msg); /* if this happens it's a bug */
  845. }
  846. config_free_lines(c);
  847. }
  848. }
  849. /** Release storage held by <b>options</b>. */
  850. void
  851. config_free(const config_format_t *fmt, void *options)
  852. {
  853. int i;
  854. if (!options)
  855. return;
  856. tor_assert(fmt);
  857. for (i=0; fmt->vars[i].name; ++i)
  858. config_clear(fmt, options, &(fmt->vars[i]));
  859. if (fmt->extra) {
  860. config_line_t **linep = STRUCT_VAR_P(options, fmt->extra->var_offset);
  861. config_free_lines(*linep);
  862. *linep = NULL;
  863. }
  864. tor_free(options);
  865. }
  866. /** Return true iff a and b contain identical keys and values in identical
  867. * order. */
  868. int
  869. config_lines_eq(config_line_t *a, config_line_t *b)
  870. {
  871. while (a && b) {
  872. if (strcasecmp(a->key, b->key) || strcmp(a->value, b->value))
  873. return 0;
  874. a = a->next;
  875. b = b->next;
  876. }
  877. if (a || b)
  878. return 0;
  879. return 1;
  880. }
  881. /** Return the number of lines in <b>a</b> whose key is <b>key</b>. */
  882. int
  883. config_count_key(const config_line_t *a, const char *key)
  884. {
  885. int n = 0;
  886. while (a) {
  887. if (!strcasecmp(a->key, key)) {
  888. ++n;
  889. }
  890. a = a->next;
  891. }
  892. return n;
  893. }
  894. /** Return true iff the option <b>name</b> has the same value in <b>o1</b>
  895. * and <b>o2</b>. Must not be called for LINELIST_S or OBSOLETE options.
  896. */
  897. int
  898. config_is_same(const config_format_t *fmt,
  899. const void *o1, const void *o2,
  900. const char *name)
  901. {
  902. config_line_t *c1, *c2;
  903. int r = 1;
  904. CONFIG_CHECK(fmt, o1);
  905. CONFIG_CHECK(fmt, o2);
  906. c1 = config_get_assigned_option(fmt, o1, name, 0);
  907. c2 = config_get_assigned_option(fmt, o2, name, 0);
  908. r = config_lines_eq(c1, c2);
  909. config_free_lines(c1);
  910. config_free_lines(c2);
  911. return r;
  912. }
  913. /** Copy storage held by <b>old</b> into a new or_options_t and return it. */
  914. void *
  915. config_dup(const config_format_t *fmt, const void *old)
  916. {
  917. void *newopts;
  918. int i;
  919. config_line_t *line;
  920. newopts = config_new(fmt);
  921. for (i=0; fmt->vars[i].name; ++i) {
  922. if (fmt->vars[i].type == CONFIG_TYPE_LINELIST_S)
  923. continue;
  924. if (fmt->vars[i].type == CONFIG_TYPE_OBSOLETE)
  925. continue;
  926. line = config_get_assigned_option(fmt, old, fmt->vars[i].name, 0);
  927. if (line) {
  928. char *msg = NULL;
  929. if (config_assign(fmt, newopts, line, 0, 0, &msg) < 0) {
  930. log_err(LD_BUG, "config_get_assigned_option() generated "
  931. "something we couldn't config_assign(): %s", msg);
  932. tor_free(msg);
  933. tor_assert(0);
  934. }
  935. }
  936. config_free_lines(line);
  937. }
  938. return newopts;
  939. }
  940. /** Set all vars in the configuration object <b>options</b> to their default
  941. * values. */
  942. void
  943. config_init(const config_format_t *fmt, void *options)
  944. {
  945. int i;
  946. const config_var_t *var;
  947. CONFIG_CHECK(fmt, options);
  948. for (i=0; fmt->vars[i].name; ++i) {
  949. var = &fmt->vars[i];
  950. if (!var->initvalue)
  951. continue; /* defaults to NULL or 0 */
  952. config_reset(fmt, options, var, 1);
  953. }
  954. }
  955. /** Allocate and return a new string holding the written-out values of the vars
  956. * in 'options'. If 'minimal', do not write out any default-valued vars.
  957. * Else, if comment_defaults, write default values as comments.
  958. */
  959. char *
  960. config_dump(const config_format_t *fmt, const void *default_options,
  961. const void *options, int minimal,
  962. int comment_defaults)
  963. {
  964. smartlist_t *elements;
  965. const void *defaults = default_options;
  966. void *defaults_tmp = NULL;
  967. config_line_t *line, *assigned;
  968. char *result;
  969. int i;
  970. char *msg = NULL;
  971. if (defaults == NULL) {
  972. defaults = defaults_tmp = config_new(fmt);
  973. config_init(fmt, defaults_tmp);
  974. }
  975. /* XXX use a 1 here so we don't add a new log line while dumping */
  976. if (default_options == NULL) {
  977. if (fmt->validate_fn(NULL, defaults_tmp, 1, &msg) < 0) {
  978. log_err(LD_BUG, "Failed to validate default config.");
  979. tor_free(msg);
  980. tor_assert(0);
  981. }
  982. }
  983. elements = smartlist_new();
  984. for (i=0; fmt->vars[i].name; ++i) {
  985. int comment_option = 0;
  986. if (fmt->vars[i].type == CONFIG_TYPE_OBSOLETE ||
  987. fmt->vars[i].type == CONFIG_TYPE_LINELIST_S)
  988. continue;
  989. /* Don't save 'hidden' control variables. */
  990. if (!strcmpstart(fmt->vars[i].name, "__"))
  991. continue;
  992. if (minimal && config_is_same(fmt, options, defaults, fmt->vars[i].name))
  993. continue;
  994. else if (comment_defaults &&
  995. config_is_same(fmt, options, defaults, fmt->vars[i].name))
  996. comment_option = 1;
  997. line = assigned =
  998. config_get_assigned_option(fmt, options, fmt->vars[i].name, 1);
  999. for (; line; line = line->next) {
  1000. smartlist_add_asprintf(elements, "%s%s %s\n",
  1001. comment_option ? "# " : "",
  1002. line->key, line->value);
  1003. }
  1004. config_free_lines(assigned);
  1005. }
  1006. if (fmt->extra) {
  1007. line = *(config_line_t**)STRUCT_VAR_P(options, fmt->extra->var_offset);
  1008. for (; line; line = line->next) {
  1009. smartlist_add_asprintf(elements, "%s %s\n", line->key, line->value);
  1010. }
  1011. }
  1012. result = smartlist_join_strings(elements, "", 0, NULL);
  1013. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  1014. smartlist_free(elements);
  1015. if (defaults_tmp)
  1016. config_free(fmt, defaults_tmp);
  1017. return result;
  1018. }
  1019. /** Mapping from a unit name to a multiplier for converting that unit into a
  1020. * base unit. Used by config_parse_unit. */
  1021. struct unit_table_t {
  1022. const char *unit; /**< The name of the unit */
  1023. uint64_t multiplier; /**< How many of the base unit appear in this unit */
  1024. };
  1025. /** Table to map the names of memory units to the number of bytes they
  1026. * contain. */
  1027. static struct unit_table_t memory_units[] = {
  1028. { "", 1 },
  1029. { "b", 1<< 0 },
  1030. { "byte", 1<< 0 },
  1031. { "bytes", 1<< 0 },
  1032. { "kb", 1<<10 },
  1033. { "kbyte", 1<<10 },
  1034. { "kbytes", 1<<10 },
  1035. { "kilobyte", 1<<10 },
  1036. { "kilobytes", 1<<10 },
  1037. { "m", 1<<20 },
  1038. { "mb", 1<<20 },
  1039. { "mbyte", 1<<20 },
  1040. { "mbytes", 1<<20 },
  1041. { "megabyte", 1<<20 },
  1042. { "megabytes", 1<<20 },
  1043. { "gb", 1<<30 },
  1044. { "gbyte", 1<<30 },
  1045. { "gbytes", 1<<30 },
  1046. { "gigabyte", 1<<30 },
  1047. { "gigabytes", 1<<30 },
  1048. { "tb", U64_LITERAL(1)<<40 },
  1049. { "terabyte", U64_LITERAL(1)<<40 },
  1050. { "terabytes", U64_LITERAL(1)<<40 },
  1051. { NULL, 0 },
  1052. };
  1053. /** Table to map the names of time units to the number of seconds they
  1054. * contain. */
  1055. static struct unit_table_t time_units[] = {
  1056. { "", 1 },
  1057. { "second", 1 },
  1058. { "seconds", 1 },
  1059. { "minute", 60 },
  1060. { "minutes", 60 },
  1061. { "hour", 60*60 },
  1062. { "hours", 60*60 },
  1063. { "day", 24*60*60 },
  1064. { "days", 24*60*60 },
  1065. { "week", 7*24*60*60 },
  1066. { "weeks", 7*24*60*60 },
  1067. { "month", 2629728, }, /* about 30.437 days */
  1068. { "months", 2629728, },
  1069. { NULL, 0 },
  1070. };
  1071. /** Table to map the names of time units to the number of milliseconds
  1072. * they contain. */
  1073. static struct unit_table_t time_msec_units[] = {
  1074. { "", 1 },
  1075. { "msec", 1 },
  1076. { "millisecond", 1 },
  1077. { "milliseconds", 1 },
  1078. { "second", 1000 },
  1079. { "seconds", 1000 },
  1080. { "minute", 60*1000 },
  1081. { "minutes", 60*1000 },
  1082. { "hour", 60*60*1000 },
  1083. { "hours", 60*60*1000 },
  1084. { "day", 24*60*60*1000 },
  1085. { "days", 24*60*60*1000 },
  1086. { "week", 7*24*60*60*1000 },
  1087. { "weeks", 7*24*60*60*1000 },
  1088. { NULL, 0 },
  1089. };
  1090. /** Parse a string <b>val</b> containing a number, zero or more
  1091. * spaces, and an optional unit string. If the unit appears in the
  1092. * table <b>u</b>, then multiply the number by the unit multiplier.
  1093. * On success, set *<b>ok</b> to 1 and return this product.
  1094. * Otherwise, set *<b>ok</b> to 0.
  1095. */
  1096. static uint64_t
  1097. config_parse_units(const char *val, struct unit_table_t *u, int *ok)
  1098. {
  1099. uint64_t v = 0;
  1100. double d = 0;
  1101. int use_float = 0;
  1102. char *cp;
  1103. tor_assert(ok);
  1104. v = tor_parse_uint64(val, 10, 0, UINT64_MAX, ok, &cp);
  1105. if (!*ok || (cp && *cp == '.')) {
  1106. d = tor_parse_double(val, 0, UINT64_MAX, ok, &cp);
  1107. if (!*ok)
  1108. goto done;
  1109. use_float = 1;
  1110. }
  1111. if (!cp) {
  1112. *ok = 1;
  1113. v = use_float ? DBL_TO_U64(d) : v;
  1114. goto done;
  1115. }
  1116. cp = (char*) eat_whitespace(cp);
  1117. for ( ;u->unit;++u) {
  1118. if (!strcasecmp(u->unit, cp)) {
  1119. if (use_float)
  1120. v = u->multiplier * d;
  1121. else
  1122. v *= u->multiplier;
  1123. *ok = 1;
  1124. goto done;
  1125. }
  1126. }
  1127. log_warn(LD_CONFIG, "Unknown unit '%s'.", cp);
  1128. *ok = 0;
  1129. done:
  1130. if (*ok)
  1131. return v;
  1132. else
  1133. return 0;
  1134. }
  1135. /** Parse a string in the format "number unit", where unit is a unit of
  1136. * information (byte, KB, M, etc). On success, set *<b>ok</b> to true
  1137. * and return the number of bytes specified. Otherwise, set
  1138. * *<b>ok</b> to false and return 0. */
  1139. static uint64_t
  1140. config_parse_memunit(const char *s, int *ok)
  1141. {
  1142. uint64_t u = config_parse_units(s, memory_units, ok);
  1143. return u;
  1144. }
  1145. /** Parse a string in the format "number unit", where unit is a unit of
  1146. * time in milliseconds. On success, set *<b>ok</b> to true and return
  1147. * the number of milliseconds in the provided interval. Otherwise, set
  1148. * *<b>ok</b> to 0 and return -1. */
  1149. static int
  1150. config_parse_msec_interval(const char *s, int *ok)
  1151. {
  1152. uint64_t r;
  1153. r = config_parse_units(s, time_msec_units, ok);
  1154. if (!ok)
  1155. return -1;
  1156. if (r > INT_MAX) {
  1157. log_warn(LD_CONFIG, "Msec interval '%s' is too long", s);
  1158. *ok = 0;
  1159. return -1;
  1160. }
  1161. return (int)r;
  1162. }
  1163. /** Parse a string in the format "number unit", where unit is a unit of time.
  1164. * On success, set *<b>ok</b> to true and return the number of seconds in
  1165. * the provided interval. Otherwise, set *<b>ok</b> to 0 and return -1.
  1166. */
  1167. static int
  1168. config_parse_interval(const char *s, int *ok)
  1169. {
  1170. uint64_t r;
  1171. r = config_parse_units(s, time_units, ok);
  1172. if (!ok)
  1173. return -1;
  1174. if (r > INT_MAX) {
  1175. log_warn(LD_CONFIG, "Interval '%s' is too long", s);
  1176. *ok = 0;
  1177. return -1;
  1178. }
  1179. return (int)r;
  1180. }