2
0

opts-visitor.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * Options Visitor
  3. *
  4. * Copyright Red Hat, Inc. 2012-2016
  5. *
  6. * Author: Laszlo Ersek <lersek@redhat.com>
  7. *
  8. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  9. * See the COPYING.LIB file in the top-level directory.
  10. *
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qapi/error.h"
  14. #include "qemu/cutils.h"
  15. #include "qapi/qmp/qerror.h"
  16. #include "qapi/opts-visitor.h"
  17. #include "qemu/queue.h"
  18. #include "qemu/option_int.h"
  19. #include "qapi/visitor-impl.h"
  20. enum ListMode
  21. {
  22. LM_NONE, /* not traversing a list of repeated options */
  23. LM_IN_PROGRESS, /*
  24. * opts_next_list() ready to be called.
  25. *
  26. * Generating the next list link will consume the most
  27. * recently parsed QemuOpt instance of the repeated
  28. * option.
  29. *
  30. * Parsing a value into the list link will examine the
  31. * next QemuOpt instance of the repeated option, and
  32. * possibly enter LM_SIGNED_INTERVAL or
  33. * LM_UNSIGNED_INTERVAL.
  34. */
  35. LM_SIGNED_INTERVAL, /*
  36. * opts_next_list() has been called.
  37. *
  38. * Generating the next list link will consume the most
  39. * recently stored element from the signed interval,
  40. * parsed from the most recent QemuOpt instance of the
  41. * repeated option. This may consume QemuOpt itself
  42. * and return to LM_IN_PROGRESS.
  43. *
  44. * Parsing a value into the list link will store the
  45. * next element of the signed interval.
  46. */
  47. LM_UNSIGNED_INTERVAL, /* Same as above, only for an unsigned interval. */
  48. LM_TRAVERSED /*
  49. * opts_next_list() has been called.
  50. *
  51. * No more QemuOpt instance in the list.
  52. * The traversal has been completed.
  53. */
  54. };
  55. typedef enum ListMode ListMode;
  56. struct OptsVisitor
  57. {
  58. Visitor visitor;
  59. /* Ownership remains with opts_visitor_new()'s caller. */
  60. const QemuOpts *opts_root;
  61. unsigned depth;
  62. /* Non-null iff depth is positive. Each key is a QemuOpt name. Each value
  63. * is a non-empty GQueue, enumerating all QemuOpt occurrences with that
  64. * name. */
  65. GHashTable *unprocessed_opts;
  66. /* The list currently being traversed with opts_start_list() /
  67. * opts_next_list(). The list must have a struct element type in the
  68. * schema, with a single mandatory scalar member. */
  69. ListMode list_mode;
  70. GQueue *repeated_opts;
  71. /* When parsing a list of repeating options as integers, values of the form
  72. * "a-b", representing a closed interval, are allowed. Elements in the
  73. * range are generated individually.
  74. */
  75. union {
  76. int64_t s;
  77. uint64_t u;
  78. } range_next, range_limit;
  79. /* If "opts_root->id" is set, reinstantiate it as a fake QemuOpt for
  80. * uniformity. Only its "name" and "str" fields are set. "fake_id_opt" does
  81. * not survive or escape the OptsVisitor object.
  82. */
  83. QemuOpt *fake_id_opt;
  84. };
  85. static OptsVisitor *to_ov(Visitor *v)
  86. {
  87. return container_of(v, OptsVisitor, visitor);
  88. }
  89. static void
  90. destroy_list(gpointer list)
  91. {
  92. g_queue_free(list);
  93. }
  94. static void
  95. opts_visitor_insert(GHashTable *unprocessed_opts, const QemuOpt *opt)
  96. {
  97. GQueue *list;
  98. list = g_hash_table_lookup(unprocessed_opts, opt->name);
  99. if (list == NULL) {
  100. list = g_queue_new();
  101. /* GHashTable will never try to free the keys -- we supply NULL as
  102. * "key_destroy_func" in opts_start_struct(). Thus cast away key
  103. * const-ness in order to suppress gcc's warning.
  104. */
  105. g_hash_table_insert(unprocessed_opts, (gpointer)opt->name, list);
  106. }
  107. /* Similarly, destroy_list() doesn't call g_queue_free_full(). */
  108. g_queue_push_tail(list, (gpointer)opt);
  109. }
  110. static void
  111. opts_start_struct(Visitor *v, const char *name, void **obj,
  112. size_t size, Error **errp)
  113. {
  114. OptsVisitor *ov = to_ov(v);
  115. const QemuOpt *opt;
  116. if (obj) {
  117. *obj = g_malloc0(size);
  118. }
  119. if (ov->depth++ > 0) {
  120. return;
  121. }
  122. ov->unprocessed_opts = g_hash_table_new_full(&g_str_hash, &g_str_equal,
  123. NULL, &destroy_list);
  124. QTAILQ_FOREACH(opt, &ov->opts_root->head, next) {
  125. /* ensured by qemu-option.c::opts_do_parse() */
  126. assert(strcmp(opt->name, "id") != 0);
  127. opts_visitor_insert(ov->unprocessed_opts, opt);
  128. }
  129. if (ov->opts_root->id != NULL) {
  130. ov->fake_id_opt = g_malloc0(sizeof *ov->fake_id_opt);
  131. ov->fake_id_opt->name = g_strdup("id");
  132. ov->fake_id_opt->str = g_strdup(ov->opts_root->id);
  133. opts_visitor_insert(ov->unprocessed_opts, ov->fake_id_opt);
  134. }
  135. }
  136. static void
  137. opts_check_struct(Visitor *v, Error **errp)
  138. {
  139. OptsVisitor *ov = to_ov(v);
  140. GHashTableIter iter;
  141. GQueue *any;
  142. if (ov->depth > 1) {
  143. return;
  144. }
  145. /* we should have processed all (distinct) QemuOpt instances */
  146. g_hash_table_iter_init(&iter, ov->unprocessed_opts);
  147. if (g_hash_table_iter_next(&iter, NULL, (void **)&any)) {
  148. const QemuOpt *first;
  149. first = g_queue_peek_head(any);
  150. error_setg(errp, QERR_INVALID_PARAMETER, first->name);
  151. }
  152. }
  153. static void
  154. opts_end_struct(Visitor *v, void **obj)
  155. {
  156. OptsVisitor *ov = to_ov(v);
  157. if (--ov->depth > 0) {
  158. return;
  159. }
  160. g_hash_table_destroy(ov->unprocessed_opts);
  161. ov->unprocessed_opts = NULL;
  162. if (ov->fake_id_opt) {
  163. g_free(ov->fake_id_opt->name);
  164. g_free(ov->fake_id_opt->str);
  165. g_free(ov->fake_id_opt);
  166. }
  167. ov->fake_id_opt = NULL;
  168. }
  169. static GQueue *
  170. lookup_distinct(const OptsVisitor *ov, const char *name, Error **errp)
  171. {
  172. GQueue *list;
  173. list = g_hash_table_lookup(ov->unprocessed_opts, name);
  174. if (!list) {
  175. error_setg(errp, QERR_MISSING_PARAMETER, name);
  176. }
  177. return list;
  178. }
  179. static void
  180. opts_start_list(Visitor *v, const char *name, GenericList **list, size_t size,
  181. Error **errp)
  182. {
  183. OptsVisitor *ov = to_ov(v);
  184. /* we can't traverse a list in a list */
  185. assert(ov->list_mode == LM_NONE);
  186. /* we don't support visits without a list */
  187. assert(list);
  188. ov->repeated_opts = lookup_distinct(ov, name, errp);
  189. if (ov->repeated_opts) {
  190. ov->list_mode = LM_IN_PROGRESS;
  191. *list = g_malloc0(size);
  192. } else {
  193. *list = NULL;
  194. }
  195. }
  196. static GenericList *
  197. opts_next_list(Visitor *v, GenericList *tail, size_t size)
  198. {
  199. OptsVisitor *ov = to_ov(v);
  200. switch (ov->list_mode) {
  201. case LM_TRAVERSED:
  202. return NULL;
  203. case LM_SIGNED_INTERVAL:
  204. case LM_UNSIGNED_INTERVAL:
  205. if (ov->list_mode == LM_SIGNED_INTERVAL) {
  206. if (ov->range_next.s < ov->range_limit.s) {
  207. ++ov->range_next.s;
  208. break;
  209. }
  210. } else if (ov->range_next.u < ov->range_limit.u) {
  211. ++ov->range_next.u;
  212. break;
  213. }
  214. ov->list_mode = LM_IN_PROGRESS;
  215. /* range has been completed, fall through in order to pop option */
  216. case LM_IN_PROGRESS: {
  217. const QemuOpt *opt;
  218. opt = g_queue_pop_head(ov->repeated_opts);
  219. if (g_queue_is_empty(ov->repeated_opts)) {
  220. g_hash_table_remove(ov->unprocessed_opts, opt->name);
  221. ov->repeated_opts = NULL;
  222. ov->list_mode = LM_TRAVERSED;
  223. return NULL;
  224. }
  225. break;
  226. }
  227. default:
  228. abort();
  229. }
  230. tail->next = g_malloc0(size);
  231. return tail->next;
  232. }
  233. static void
  234. opts_check_list(Visitor *v, Error **errp)
  235. {
  236. /*
  237. * Unvisited list elements will be reported later when checking
  238. * whether unvisited struct members remain.
  239. */
  240. }
  241. static void
  242. opts_end_list(Visitor *v, void **obj)
  243. {
  244. OptsVisitor *ov = to_ov(v);
  245. assert(ov->list_mode == LM_IN_PROGRESS ||
  246. ov->list_mode == LM_SIGNED_INTERVAL ||
  247. ov->list_mode == LM_UNSIGNED_INTERVAL ||
  248. ov->list_mode == LM_TRAVERSED);
  249. ov->repeated_opts = NULL;
  250. ov->list_mode = LM_NONE;
  251. }
  252. static const QemuOpt *
  253. lookup_scalar(const OptsVisitor *ov, const char *name, Error **errp)
  254. {
  255. if (ov->list_mode == LM_NONE) {
  256. GQueue *list;
  257. /* the last occurrence of any QemuOpt takes effect when queried by name
  258. */
  259. list = lookup_distinct(ov, name, errp);
  260. return list ? g_queue_peek_tail(list) : NULL;
  261. }
  262. if (ov->list_mode == LM_TRAVERSED) {
  263. error_setg(errp, "Fewer list elements than expected");
  264. return NULL;
  265. }
  266. assert(ov->list_mode == LM_IN_PROGRESS);
  267. return g_queue_peek_head(ov->repeated_opts);
  268. }
  269. static void
  270. processed(OptsVisitor *ov, const char *name)
  271. {
  272. if (ov->list_mode == LM_NONE) {
  273. g_hash_table_remove(ov->unprocessed_opts, name);
  274. return;
  275. }
  276. assert(ov->list_mode == LM_IN_PROGRESS);
  277. /* do nothing */
  278. }
  279. static void
  280. opts_type_str(Visitor *v, const char *name, char **obj, Error **errp)
  281. {
  282. OptsVisitor *ov = to_ov(v);
  283. const QemuOpt *opt;
  284. opt = lookup_scalar(ov, name, errp);
  285. if (!opt) {
  286. *obj = NULL;
  287. return;
  288. }
  289. *obj = g_strdup(opt->str ? opt->str : "");
  290. /* Note that we consume a string even if this is called as part of
  291. * an enum visit that later fails because the string is not a
  292. * valid enum value; this is harmless because tracking what gets
  293. * consumed only matters to visit_end_struct() as the final error
  294. * check if there were no other failures during the visit. */
  295. processed(ov, name);
  296. }
  297. /* mimics qemu-option.c::parse_option_bool() */
  298. static void
  299. opts_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
  300. {
  301. OptsVisitor *ov = to_ov(v);
  302. const QemuOpt *opt;
  303. opt = lookup_scalar(ov, name, errp);
  304. if (!opt) {
  305. return;
  306. }
  307. if (opt->str) {
  308. if (strcmp(opt->str, "on") == 0 ||
  309. strcmp(opt->str, "yes") == 0 ||
  310. strcmp(opt->str, "y") == 0) {
  311. *obj = true;
  312. } else if (strcmp(opt->str, "off") == 0 ||
  313. strcmp(opt->str, "no") == 0 ||
  314. strcmp(opt->str, "n") == 0) {
  315. *obj = false;
  316. } else {
  317. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
  318. "on|yes|y|off|no|n");
  319. return;
  320. }
  321. } else {
  322. *obj = true;
  323. }
  324. processed(ov, name);
  325. }
  326. static void
  327. opts_type_int64(Visitor *v, const char *name, int64_t *obj, Error **errp)
  328. {
  329. OptsVisitor *ov = to_ov(v);
  330. const QemuOpt *opt;
  331. const char *str;
  332. long long val;
  333. char *endptr;
  334. if (ov->list_mode == LM_SIGNED_INTERVAL) {
  335. *obj = ov->range_next.s;
  336. return;
  337. }
  338. opt = lookup_scalar(ov, name, errp);
  339. if (!opt) {
  340. return;
  341. }
  342. str = opt->str ? opt->str : "";
  343. /* we've gotten past lookup_scalar() */
  344. assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS);
  345. errno = 0;
  346. val = strtoll(str, &endptr, 0);
  347. if (errno == 0 && endptr > str && INT64_MIN <= val && val <= INT64_MAX) {
  348. if (*endptr == '\0') {
  349. *obj = val;
  350. processed(ov, name);
  351. return;
  352. }
  353. if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) {
  354. long long val2;
  355. str = endptr + 1;
  356. val2 = strtoll(str, &endptr, 0);
  357. if (errno == 0 && endptr > str && *endptr == '\0' &&
  358. INT64_MIN <= val2 && val2 <= INT64_MAX && val <= val2 &&
  359. (val > INT64_MAX - OPTS_VISITOR_RANGE_MAX ||
  360. val2 < val + OPTS_VISITOR_RANGE_MAX)) {
  361. ov->range_next.s = val;
  362. ov->range_limit.s = val2;
  363. ov->list_mode = LM_SIGNED_INTERVAL;
  364. /* as if entering on the top */
  365. *obj = ov->range_next.s;
  366. return;
  367. }
  368. }
  369. }
  370. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
  371. (ov->list_mode == LM_NONE) ? "an int64 value" :
  372. "an int64 value or range");
  373. }
  374. static void
  375. opts_type_uint64(Visitor *v, const char *name, uint64_t *obj, Error **errp)
  376. {
  377. OptsVisitor *ov = to_ov(v);
  378. const QemuOpt *opt;
  379. const char *str;
  380. unsigned long long val;
  381. char *endptr;
  382. if (ov->list_mode == LM_UNSIGNED_INTERVAL) {
  383. *obj = ov->range_next.u;
  384. return;
  385. }
  386. opt = lookup_scalar(ov, name, errp);
  387. if (!opt) {
  388. return;
  389. }
  390. str = opt->str;
  391. /* we've gotten past lookup_scalar() */
  392. assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS);
  393. if (parse_uint(str, &val, &endptr, 0) == 0 && val <= UINT64_MAX) {
  394. if (*endptr == '\0') {
  395. *obj = val;
  396. processed(ov, name);
  397. return;
  398. }
  399. if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) {
  400. unsigned long long val2;
  401. str = endptr + 1;
  402. if (parse_uint_full(str, &val2, 0) == 0 &&
  403. val2 <= UINT64_MAX && val <= val2 &&
  404. val2 - val < OPTS_VISITOR_RANGE_MAX) {
  405. ov->range_next.u = val;
  406. ov->range_limit.u = val2;
  407. ov->list_mode = LM_UNSIGNED_INTERVAL;
  408. /* as if entering on the top */
  409. *obj = ov->range_next.u;
  410. return;
  411. }
  412. }
  413. }
  414. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
  415. (ov->list_mode == LM_NONE) ? "a uint64 value" :
  416. "a uint64 value or range");
  417. }
  418. static void
  419. opts_type_size(Visitor *v, const char *name, uint64_t *obj, Error **errp)
  420. {
  421. OptsVisitor *ov = to_ov(v);
  422. const QemuOpt *opt;
  423. int err;
  424. opt = lookup_scalar(ov, name, errp);
  425. if (!opt) {
  426. return;
  427. }
  428. err = qemu_strtosz(opt->str ? opt->str : "", NULL, obj);
  429. if (err < 0) {
  430. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
  431. "a size value");
  432. return;
  433. }
  434. processed(ov, name);
  435. }
  436. static void
  437. opts_optional(Visitor *v, const char *name, bool *present)
  438. {
  439. OptsVisitor *ov = to_ov(v);
  440. /* we only support a single mandatory scalar field in a list node */
  441. assert(ov->list_mode == LM_NONE);
  442. *present = (lookup_distinct(ov, name, NULL) != NULL);
  443. }
  444. static void
  445. opts_free(Visitor *v)
  446. {
  447. OptsVisitor *ov = to_ov(v);
  448. if (ov->unprocessed_opts != NULL) {
  449. g_hash_table_destroy(ov->unprocessed_opts);
  450. }
  451. g_free(ov->fake_id_opt);
  452. g_free(ov);
  453. }
  454. Visitor *
  455. opts_visitor_new(const QemuOpts *opts)
  456. {
  457. OptsVisitor *ov;
  458. assert(opts);
  459. ov = g_malloc0(sizeof *ov);
  460. ov->visitor.type = VISITOR_INPUT;
  461. ov->visitor.start_struct = &opts_start_struct;
  462. ov->visitor.check_struct = &opts_check_struct;
  463. ov->visitor.end_struct = &opts_end_struct;
  464. ov->visitor.start_list = &opts_start_list;
  465. ov->visitor.next_list = &opts_next_list;
  466. ov->visitor.check_list = &opts_check_list;
  467. ov->visitor.end_list = &opts_end_list;
  468. ov->visitor.type_int64 = &opts_type_int64;
  469. ov->visitor.type_uint64 = &opts_type_uint64;
  470. ov->visitor.type_size = &opts_type_size;
  471. ov->visitor.type_bool = &opts_type_bool;
  472. ov->visitor.type_str = &opts_type_str;
  473. /* type_number() is not filled in, but this is not the first visitor to
  474. * skip some mandatory methods... */
  475. ov->visitor.optional = &opts_optional;
  476. ov->visitor.free = opts_free;
  477. ov->opts_root = opts;
  478. return &ov->visitor;
  479. }