confparse.c 37 KB

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