confparse.c 37 KB

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