2
0

opts-visitor.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 bool
  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 true;
  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. return true;
  136. }
  137. static bool
  138. opts_check_struct(Visitor *v, Error **errp)
  139. {
  140. OptsVisitor *ov = to_ov(v);
  141. GHashTableIter iter;
  142. GQueue *any;
  143. if (ov->depth > 1) {
  144. return true;
  145. }
  146. /* we should have processed all (distinct) QemuOpt instances */
  147. g_hash_table_iter_init(&iter, ov->unprocessed_opts);
  148. if (g_hash_table_iter_next(&iter, NULL, (void **)&any)) {
  149. const QemuOpt *first;
  150. first = g_queue_peek_head(any);
  151. error_setg(errp, QERR_INVALID_PARAMETER, first->name);
  152. return false;
  153. }
  154. return true;
  155. }
  156. static void
  157. opts_end_struct(Visitor *v, void **obj)
  158. {
  159. OptsVisitor *ov = to_ov(v);
  160. if (--ov->depth > 0) {
  161. return;
  162. }
  163. g_hash_table_destroy(ov->unprocessed_opts);
  164. ov->unprocessed_opts = NULL;
  165. if (ov->fake_id_opt) {
  166. g_free(ov->fake_id_opt->name);
  167. g_free(ov->fake_id_opt->str);
  168. g_free(ov->fake_id_opt);
  169. }
  170. ov->fake_id_opt = NULL;
  171. }
  172. static GQueue *
  173. lookup_distinct(const OptsVisitor *ov, const char *name, Error **errp)
  174. {
  175. GQueue *list;
  176. list = g_hash_table_lookup(ov->unprocessed_opts, name);
  177. if (!list) {
  178. error_setg(errp, QERR_MISSING_PARAMETER, name);
  179. }
  180. return list;
  181. }
  182. static bool
  183. opts_start_list(Visitor *v, const char *name, GenericList **list, size_t size,
  184. Error **errp)
  185. {
  186. OptsVisitor *ov = to_ov(v);
  187. /* we can't traverse a list in a list */
  188. assert(ov->list_mode == LM_NONE);
  189. /* we don't support visits without a list */
  190. assert(list);
  191. ov->repeated_opts = lookup_distinct(ov, name, errp);
  192. if (!ov->repeated_opts) {
  193. *list = NULL;
  194. return false;
  195. }
  196. ov->list_mode = LM_IN_PROGRESS;
  197. *list = g_malloc0(size);
  198. return true;
  199. }
  200. static GenericList *
  201. opts_next_list(Visitor *v, GenericList *tail, size_t size)
  202. {
  203. OptsVisitor *ov = to_ov(v);
  204. switch (ov->list_mode) {
  205. case LM_TRAVERSED:
  206. return NULL;
  207. case LM_SIGNED_INTERVAL:
  208. case LM_UNSIGNED_INTERVAL:
  209. if (ov->list_mode == LM_SIGNED_INTERVAL) {
  210. if (ov->range_next.s < ov->range_limit.s) {
  211. ++ov->range_next.s;
  212. break;
  213. }
  214. } else if (ov->range_next.u < ov->range_limit.u) {
  215. ++ov->range_next.u;
  216. break;
  217. }
  218. ov->list_mode = LM_IN_PROGRESS;
  219. /* range has been completed, fall through in order to pop option */
  220. case LM_IN_PROGRESS: {
  221. const QemuOpt *opt;
  222. opt = g_queue_pop_head(ov->repeated_opts);
  223. if (g_queue_is_empty(ov->repeated_opts)) {
  224. g_hash_table_remove(ov->unprocessed_opts, opt->name);
  225. ov->repeated_opts = NULL;
  226. ov->list_mode = LM_TRAVERSED;
  227. return NULL;
  228. }
  229. break;
  230. }
  231. default:
  232. abort();
  233. }
  234. tail->next = g_malloc0(size);
  235. return tail->next;
  236. }
  237. static bool
  238. opts_check_list(Visitor *v, Error **errp)
  239. {
  240. /*
  241. * Unvisited list elements will be reported later when checking
  242. * whether unvisited struct members remain.
  243. */
  244. return true;
  245. }
  246. static void
  247. opts_end_list(Visitor *v, void **obj)
  248. {
  249. OptsVisitor *ov = to_ov(v);
  250. assert(ov->list_mode == LM_IN_PROGRESS ||
  251. ov->list_mode == LM_SIGNED_INTERVAL ||
  252. ov->list_mode == LM_UNSIGNED_INTERVAL ||
  253. ov->list_mode == LM_TRAVERSED);
  254. ov->repeated_opts = NULL;
  255. ov->list_mode = LM_NONE;
  256. }
  257. static const QemuOpt *
  258. lookup_scalar(const OptsVisitor *ov, const char *name, Error **errp)
  259. {
  260. if (ov->list_mode == LM_NONE) {
  261. GQueue *list;
  262. /* the last occurrence of any QemuOpt takes effect when queried by name
  263. */
  264. list = lookup_distinct(ov, name, errp);
  265. return list ? g_queue_peek_tail(list) : NULL;
  266. }
  267. if (ov->list_mode == LM_TRAVERSED) {
  268. error_setg(errp, "Fewer list elements than expected");
  269. return NULL;
  270. }
  271. assert(ov->list_mode == LM_IN_PROGRESS);
  272. return g_queue_peek_head(ov->repeated_opts);
  273. }
  274. static void
  275. processed(OptsVisitor *ov, const char *name)
  276. {
  277. if (ov->list_mode == LM_NONE) {
  278. g_hash_table_remove(ov->unprocessed_opts, name);
  279. return;
  280. }
  281. assert(ov->list_mode == LM_IN_PROGRESS);
  282. /* do nothing */
  283. }
  284. static bool
  285. opts_type_str(Visitor *v, const char *name, char **obj, Error **errp)
  286. {
  287. OptsVisitor *ov = to_ov(v);
  288. const QemuOpt *opt;
  289. opt = lookup_scalar(ov, name, errp);
  290. if (!opt) {
  291. *obj = NULL;
  292. return false;
  293. }
  294. *obj = g_strdup(opt->str ? opt->str : "");
  295. /* Note that we consume a string even if this is called as part of
  296. * an enum visit that later fails because the string is not a
  297. * valid enum value; this is harmless because tracking what gets
  298. * consumed only matters to visit_end_struct() as the final error
  299. * check if there were no other failures during the visit. */
  300. processed(ov, name);
  301. return true;
  302. }
  303. static bool
  304. opts_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
  305. {
  306. OptsVisitor *ov = to_ov(v);
  307. const QemuOpt *opt;
  308. opt = lookup_scalar(ov, name, errp);
  309. if (!opt) {
  310. return false;
  311. }
  312. if (opt->str) {
  313. if (!qapi_bool_parse(opt->name, opt->str, obj, errp)) {
  314. return false;
  315. }
  316. } else {
  317. *obj = true;
  318. }
  319. processed(ov, name);
  320. return true;
  321. }
  322. static bool
  323. opts_type_int64(Visitor *v, const char *name, int64_t *obj, Error **errp)
  324. {
  325. OptsVisitor *ov = to_ov(v);
  326. const QemuOpt *opt;
  327. const char *str;
  328. long long val;
  329. char *endptr;
  330. if (ov->list_mode == LM_SIGNED_INTERVAL) {
  331. *obj = ov->range_next.s;
  332. return true;
  333. }
  334. opt = lookup_scalar(ov, name, errp);
  335. if (!opt) {
  336. return false;
  337. }
  338. str = opt->str ? opt->str : "";
  339. /* we've gotten past lookup_scalar() */
  340. assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS);
  341. errno = 0;
  342. val = strtoll(str, &endptr, 0);
  343. if (errno == 0 && endptr > str && INT64_MIN <= val && val <= INT64_MAX) {
  344. if (*endptr == '\0') {
  345. *obj = val;
  346. processed(ov, name);
  347. return true;
  348. }
  349. if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) {
  350. long long val2;
  351. str = endptr + 1;
  352. val2 = strtoll(str, &endptr, 0);
  353. if (errno == 0 && endptr > str && *endptr == '\0' &&
  354. INT64_MIN <= val2 && val2 <= INT64_MAX && val <= val2 &&
  355. (val > INT64_MAX - OPTS_VISITOR_RANGE_MAX ||
  356. val2 < val + OPTS_VISITOR_RANGE_MAX)) {
  357. ov->range_next.s = val;
  358. ov->range_limit.s = val2;
  359. ov->list_mode = LM_SIGNED_INTERVAL;
  360. /* as if entering on the top */
  361. *obj = ov->range_next.s;
  362. return true;
  363. }
  364. }
  365. }
  366. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
  367. (ov->list_mode == LM_NONE) ? "an int64 value" :
  368. "an int64 value or range");
  369. return false;
  370. }
  371. static bool
  372. opts_type_uint64(Visitor *v, const char *name, uint64_t *obj, Error **errp)
  373. {
  374. OptsVisitor *ov = to_ov(v);
  375. const QemuOpt *opt;
  376. const char *str;
  377. unsigned long long val;
  378. char *endptr;
  379. if (ov->list_mode == LM_UNSIGNED_INTERVAL) {
  380. *obj = ov->range_next.u;
  381. return true;
  382. }
  383. opt = lookup_scalar(ov, name, errp);
  384. if (!opt) {
  385. return false;
  386. }
  387. str = opt->str;
  388. /* we've gotten past lookup_scalar() */
  389. assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS);
  390. if (parse_uint(str, &val, &endptr, 0) == 0 && val <= UINT64_MAX) {
  391. if (*endptr == '\0') {
  392. *obj = val;
  393. processed(ov, name);
  394. return true;
  395. }
  396. if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) {
  397. unsigned long long val2;
  398. str = endptr + 1;
  399. if (parse_uint_full(str, &val2, 0) == 0 &&
  400. val2 <= UINT64_MAX && val <= val2 &&
  401. val2 - val < OPTS_VISITOR_RANGE_MAX) {
  402. ov->range_next.u = val;
  403. ov->range_limit.u = val2;
  404. ov->list_mode = LM_UNSIGNED_INTERVAL;
  405. /* as if entering on the top */
  406. *obj = ov->range_next.u;
  407. return true;
  408. }
  409. }
  410. }
  411. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
  412. (ov->list_mode == LM_NONE) ? "a uint64 value" :
  413. "a uint64 value or range");
  414. return false;
  415. }
  416. static bool
  417. opts_type_size(Visitor *v, const char *name, uint64_t *obj, Error **errp)
  418. {
  419. OptsVisitor *ov = to_ov(v);
  420. const QemuOpt *opt;
  421. int err;
  422. opt = lookup_scalar(ov, name, errp);
  423. if (!opt) {
  424. return false;
  425. }
  426. err = qemu_strtosz(opt->str ? opt->str : "", NULL, obj);
  427. if (err < 0) {
  428. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
  429. "a size value");
  430. return false;
  431. }
  432. processed(ov, name);
  433. return true;
  434. }
  435. static void
  436. opts_optional(Visitor *v, const char *name, bool *present)
  437. {
  438. OptsVisitor *ov = to_ov(v);
  439. /* we only support a single mandatory scalar field in a list node */
  440. assert(ov->list_mode == LM_NONE);
  441. *present = (lookup_distinct(ov, name, NULL) != NULL);
  442. }
  443. static void
  444. opts_free(Visitor *v)
  445. {
  446. OptsVisitor *ov = to_ov(v);
  447. if (ov->unprocessed_opts != NULL) {
  448. g_hash_table_destroy(ov->unprocessed_opts);
  449. }
  450. g_free(ov->fake_id_opt);
  451. g_free(ov);
  452. }
  453. Visitor *
  454. opts_visitor_new(const QemuOpts *opts)
  455. {
  456. OptsVisitor *ov;
  457. assert(opts);
  458. ov = g_malloc0(sizeof *ov);
  459. ov->visitor.type = VISITOR_INPUT;
  460. ov->visitor.start_struct = &opts_start_struct;
  461. ov->visitor.check_struct = &opts_check_struct;
  462. ov->visitor.end_struct = &opts_end_struct;
  463. ov->visitor.start_list = &opts_start_list;
  464. ov->visitor.next_list = &opts_next_list;
  465. ov->visitor.check_list = &opts_check_list;
  466. ov->visitor.end_list = &opts_end_list;
  467. ov->visitor.type_int64 = &opts_type_int64;
  468. ov->visitor.type_uint64 = &opts_type_uint64;
  469. ov->visitor.type_size = &opts_type_size;
  470. ov->visitor.type_bool = &opts_type_bool;
  471. ov->visitor.type_str = &opts_type_str;
  472. /* type_number() is not filled in, but this is not the first visitor to
  473. * skip some mandatory methods... */
  474. ov->visitor.optional = &opts_optional;
  475. ov->visitor.free = opts_free;
  476. ov->opts_root = opts;
  477. return &ov->visitor;
  478. }