confparse.c 37 KB

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