2
0

qerror.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * QError Module
  3. *
  4. * Copyright (C) 2009 Red Hat Inc.
  5. *
  6. * Authors:
  7. * Luiz Capitulino <lcapitulino@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  10. * See the COPYING.LIB file in the top-level directory.
  11. */
  12. #include "monitor.h"
  13. #include "qjson.h"
  14. #include "qerror.h"
  15. #include "qemu-common.h"
  16. static void qerror_destroy_obj(QObject *obj);
  17. static const QType qerror_type = {
  18. .code = QTYPE_QERROR,
  19. .destroy = qerror_destroy_obj,
  20. };
  21. /**
  22. * The 'desc' parameter is a printf-like string, the format of the format
  23. * string is:
  24. *
  25. * %(KEY)
  26. *
  27. * Where KEY is a QDict key, which has to be passed to qerror_from_info().
  28. *
  29. * Example:
  30. *
  31. * "foo error on device: %(device) slot: %(slot_nr)"
  32. *
  33. * A single percent sign can be printed if followed by a second one,
  34. * for example:
  35. *
  36. * "running out of foo: %(foo)%%"
  37. *
  38. * Please keep the entries in alphabetical order.
  39. * Use scripts/check-qerror.sh to check.
  40. */
  41. static const QErrorStringTable qerror_table[] = {
  42. {
  43. .error_fmt = QERR_ADD_CLIENT_FAILED,
  44. .desc = "Could not add client",
  45. },
  46. {
  47. .error_fmt = QERR_AMBIGUOUS_PATH,
  48. .desc = "Path '%(path)' does not uniquely identify a %(object)"
  49. },
  50. {
  51. .error_fmt = QERR_BAD_BUS_FOR_DEVICE,
  52. .desc = "Device '%(device)' can't go on a %(bad_bus_type) bus",
  53. },
  54. {
  55. .error_fmt = QERR_BASE_NOT_FOUND,
  56. .desc = "Base '%(base)' not found",
  57. },
  58. {
  59. .error_fmt = QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
  60. .desc = "Block format '%(format)' used by device '%(name)' does not support feature '%(feature)'",
  61. },
  62. {
  63. .error_fmt = QERR_BUS_NO_HOTPLUG,
  64. .desc = "Bus '%(bus)' does not support hotplugging",
  65. },
  66. {
  67. .error_fmt = QERR_BUS_NOT_FOUND,
  68. .desc = "Bus '%(bus)' not found",
  69. },
  70. {
  71. .error_fmt = QERR_COMMAND_DISABLED,
  72. .desc = "The command %(name) has been disabled for this instance",
  73. },
  74. {
  75. .error_fmt = QERR_COMMAND_NOT_FOUND,
  76. .desc = "The command %(name) has not been found",
  77. },
  78. {
  79. .error_fmt = QERR_DEVICE_ENCRYPTED,
  80. .desc = "Device '%(device)' is encrypted",
  81. },
  82. {
  83. .error_fmt = QERR_DEVICE_FEATURE_BLOCKS_MIGRATION,
  84. .desc = "Migration is disabled when using feature '%(feature)' in device '%(device)'",
  85. },
  86. {
  87. .error_fmt = QERR_DEVICE_HAS_NO_MEDIUM,
  88. .desc = "Device '%(device)' has no medium",
  89. },
  90. {
  91. .error_fmt = QERR_DEVICE_INIT_FAILED,
  92. .desc = "Device '%(device)' could not be initialized",
  93. },
  94. {
  95. .error_fmt = QERR_DEVICE_IN_USE,
  96. .desc = "Device '%(device)' is in use",
  97. },
  98. {
  99. .error_fmt = QERR_DEVICE_IS_READ_ONLY,
  100. .desc = "Device '%(device)' is read only",
  101. },
  102. {
  103. .error_fmt = QERR_DEVICE_LOCKED,
  104. .desc = "Device '%(device)' is locked",
  105. },
  106. {
  107. .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES,
  108. .desc = "Device '%(device)' has multiple child busses",
  109. },
  110. {
  111. .error_fmt = QERR_DEVICE_NO_BUS,
  112. .desc = "Device '%(device)' has no child bus",
  113. },
  114. {
  115. .error_fmt = QERR_DEVICE_NO_HOTPLUG,
  116. .desc = "Device '%(device)' does not support hotplugging",
  117. },
  118. {
  119. .error_fmt = QERR_DEVICE_NOT_ACTIVE,
  120. .desc = "Device '%(device)' has not been activated",
  121. },
  122. {
  123. .error_fmt = QERR_DEVICE_NOT_ENCRYPTED,
  124. .desc = "Device '%(device)' is not encrypted",
  125. },
  126. {
  127. .error_fmt = QERR_DEVICE_NOT_FOUND,
  128. .desc = "Device '%(device)' not found",
  129. },
  130. {
  131. .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
  132. .desc = "Device '%(device)' is not removable",
  133. },
  134. {
  135. .error_fmt = QERR_DUPLICATE_ID,
  136. .desc = "Duplicate ID '%(id)' for %(object)",
  137. },
  138. {
  139. .error_fmt = QERR_FD_NOT_FOUND,
  140. .desc = "File descriptor named '%(name)' not found",
  141. },
  142. {
  143. .error_fmt = QERR_FD_NOT_SUPPLIED,
  144. .desc = "No file descriptor supplied via SCM_RIGHTS",
  145. },
  146. {
  147. .error_fmt = QERR_FEATURE_DISABLED,
  148. .desc = "The feature '%(name)' is not enabled",
  149. },
  150. {
  151. .error_fmt = QERR_INVALID_BLOCK_FORMAT,
  152. .desc = "Invalid block format '%(name)'",
  153. },
  154. {
  155. .error_fmt = QERR_INVALID_PARAMETER,
  156. .desc = "Invalid parameter '%(name)'",
  157. },
  158. {
  159. .error_fmt = QERR_INVALID_PARAMETER_COMBINATION,
  160. .desc = "Invalid parameter combination",
  161. },
  162. {
  163. .error_fmt = QERR_INVALID_PARAMETER_TYPE,
  164. .desc = "Invalid parameter type for '%(name)', expected: %(expected)",
  165. },
  166. {
  167. .error_fmt = QERR_INVALID_PARAMETER_VALUE,
  168. .desc = "Parameter '%(name)' expects %(expected)",
  169. },
  170. {
  171. .error_fmt = QERR_INVALID_PASSWORD,
  172. .desc = "Password incorrect",
  173. },
  174. {
  175. .error_fmt = QERR_IO_ERROR,
  176. .desc = "An IO error has occurred",
  177. },
  178. {
  179. .error_fmt = QERR_JSON_PARSE_ERROR,
  180. .desc = "JSON parse error, %(message)",
  181. },
  182. {
  183. .error_fmt = QERR_JSON_PARSING,
  184. .desc = "Invalid JSON syntax",
  185. },
  186. {
  187. .error_fmt = QERR_KVM_MISSING_CAP,
  188. .desc = "Using KVM without %(capability), %(feature) unavailable",
  189. },
  190. {
  191. .error_fmt = QERR_MIGRATION_ACTIVE,
  192. .desc = "There's a migration process in progress",
  193. },
  194. {
  195. .error_fmt = QERR_MIGRATION_NOT_SUPPORTED,
  196. .desc = "State blocked by non-migratable device '%(device)'",
  197. },
  198. {
  199. .error_fmt = QERR_MIGRATION_EXPECTED,
  200. .desc = "An incoming migration is expected before this command can be executed",
  201. },
  202. {
  203. .error_fmt = QERR_MISSING_PARAMETER,
  204. .desc = "Parameter '%(name)' is missing",
  205. },
  206. {
  207. .error_fmt = QERR_NO_BUS_FOR_DEVICE,
  208. .desc = "No '%(bus)' bus found for device '%(device)'",
  209. },
  210. {
  211. .error_fmt = QERR_NOT_SUPPORTED,
  212. .desc = "Not supported",
  213. },
  214. {
  215. .error_fmt = QERR_OPEN_FILE_FAILED,
  216. .desc = "Could not open '%(filename)'",
  217. },
  218. {
  219. .error_fmt = QERR_PERMISSION_DENIED,
  220. .desc = "Insufficient permission to perform this operation",
  221. },
  222. {
  223. .error_fmt = QERR_PROPERTY_NOT_FOUND,
  224. .desc = "Property '%(device).%(property)' not found",
  225. },
  226. {
  227. .error_fmt = QERR_PROPERTY_VALUE_BAD,
  228. .desc = "Property '%(device).%(property)' doesn't take value '%(value)'",
  229. },
  230. {
  231. .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
  232. .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
  233. },
  234. {
  235. .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
  236. .desc = "Property '%(device).%(property)' can't find value '%(value)'",
  237. },
  238. {
  239. .error_fmt = QERR_PROPERTY_VALUE_NOT_POWER_OF_2,
  240. .desc = "Property '%(device).%(property)' doesn't take "
  241. "value '%(value)', it's not a power of 2",
  242. },
  243. {
  244. .error_fmt = QERR_PROPERTY_VALUE_OUT_OF_RANGE,
  245. .desc = "Property '%(device).%(property)' doesn't take "
  246. "value %(value) (minimum: %(min), maximum: %(max))",
  247. },
  248. {
  249. .error_fmt = QERR_QGA_COMMAND_FAILED,
  250. .desc = "Guest agent command failed, error was '%(message)'",
  251. },
  252. {
  253. .error_fmt = QERR_QGA_LOGGING_FAILED,
  254. .desc = "Guest agent failed to log non-optional log statement",
  255. },
  256. {
  257. .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
  258. .desc = "Expected '%(expected)' in QMP input",
  259. },
  260. {
  261. .error_fmt = QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
  262. .desc = "QMP input object member '%(member)' expects '%(expected)'",
  263. },
  264. {
  265. .error_fmt = QERR_QMP_EXTRA_MEMBER,
  266. .desc = "QMP input object member '%(member)' is unexpected",
  267. },
  268. {
  269. .error_fmt = QERR_RESET_REQUIRED,
  270. .desc = "Resetting the Virtual Machine is required",
  271. },
  272. {
  273. .error_fmt = QERR_SET_PASSWD_FAILED,
  274. .desc = "Could not set password",
  275. },
  276. {
  277. .error_fmt = QERR_TOO_MANY_FILES,
  278. .desc = "Too many open files",
  279. },
  280. {
  281. .error_fmt = QERR_UNDEFINED_ERROR,
  282. .desc = "An undefined error has occurred",
  283. },
  284. {
  285. .error_fmt = QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
  286. .desc = "'%(device)' uses a %(format) feature which is not "
  287. "supported by this qemu version: %(feature)",
  288. },
  289. {
  290. .error_fmt = QERR_UNSUPPORTED,
  291. .desc = "this feature or command is not currently supported",
  292. },
  293. {
  294. .error_fmt = QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION,
  295. .desc = "Migration is disabled when VirtFS export path '%(path)' "
  296. "is mounted in the guest using mount_tag '%(tag)'",
  297. },
  298. {
  299. .error_fmt = QERR_VNC_SERVER_FAILED,
  300. .desc = "Could not start VNC server on %(target)",
  301. },
  302. {
  303. .error_fmt = QERR_SOCKET_CONNECT_IN_PROGRESS,
  304. .desc = "Connection can not be completed immediately",
  305. },
  306. {
  307. .error_fmt = QERR_SOCKET_CONNECT_FAILED,
  308. .desc = "Failed to connect to socket",
  309. },
  310. {
  311. .error_fmt = QERR_SOCKET_LISTEN_FAILED,
  312. .desc = "Failed to set socket to listening mode",
  313. },
  314. {
  315. .error_fmt = QERR_SOCKET_BIND_FAILED,
  316. .desc = "Failed to bind socket",
  317. },
  318. {
  319. .error_fmt = QERR_SOCKET_CREATE_FAILED,
  320. .desc = "Failed to create socket",
  321. },
  322. {}
  323. };
  324. /**
  325. * qerror_new(): Create a new QError
  326. *
  327. * Return strong reference.
  328. */
  329. QError *qerror_new(void)
  330. {
  331. QError *qerr;
  332. qerr = g_malloc0(sizeof(*qerr));
  333. QOBJECT_INIT(qerr, &qerror_type);
  334. return qerr;
  335. }
  336. static void GCC_FMT_ATTR(2, 3) qerror_abort(const QError *qerr,
  337. const char *fmt, ...)
  338. {
  339. va_list ap;
  340. fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
  341. fprintf(stderr, "qerror: -> ");
  342. va_start(ap, fmt);
  343. vfprintf(stderr, fmt, ap);
  344. va_end(ap);
  345. fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
  346. abort();
  347. }
  348. static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
  349. const char *fmt, va_list *va)
  350. {
  351. QObject *obj;
  352. obj = qobject_from_jsonv(fmt, va);
  353. if (!obj) {
  354. qerror_abort(qerr, "invalid format '%s'", fmt);
  355. }
  356. if (qobject_type(obj) != QTYPE_QDICT) {
  357. qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
  358. }
  359. qerr->error = qobject_to_qdict(obj);
  360. obj = qdict_get(qerr->error, "class");
  361. if (!obj) {
  362. qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
  363. }
  364. if (qobject_type(obj) != QTYPE_QSTRING) {
  365. qerror_abort(qerr, "'class' key value should be a QString");
  366. }
  367. obj = qdict_get(qerr->error, "data");
  368. if (!obj) {
  369. qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
  370. }
  371. if (qobject_type(obj) != QTYPE_QDICT) {
  372. qerror_abort(qerr, "'data' key value should be a QDICT");
  373. }
  374. }
  375. static void qerror_set_desc(QError *qerr, const char *fmt)
  376. {
  377. int i;
  378. // FIXME: inefficient loop
  379. for (i = 0; qerror_table[i].error_fmt; i++) {
  380. if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
  381. qerr->entry = &qerror_table[i];
  382. return;
  383. }
  384. }
  385. qerror_abort(qerr, "error format '%s' not found", fmt);
  386. }
  387. /**
  388. * qerror_from_info(): Create a new QError from error information
  389. *
  390. * The information consists of:
  391. *
  392. * - file the file name of where the error occurred
  393. * - linenr the line number of where the error occurred
  394. * - func the function name of where the error occurred
  395. * - fmt JSON printf-like dictionary, there must exist keys 'class' and
  396. * 'data'
  397. * - va va_list of all arguments specified by fmt
  398. *
  399. * Return strong reference.
  400. */
  401. QError *qerror_from_info(const char *file, int linenr, const char *func,
  402. const char *fmt, va_list *va)
  403. {
  404. QError *qerr;
  405. qerr = qerror_new();
  406. loc_save(&qerr->loc);
  407. qerr->linenr = linenr;
  408. qerr->file = file;
  409. qerr->func = func;
  410. if (!fmt) {
  411. qerror_abort(qerr, "QDict not specified");
  412. }
  413. qerror_set_data(qerr, fmt, va);
  414. qerror_set_desc(qerr, fmt);
  415. return qerr;
  416. }
  417. static void parse_error(const QErrorStringTable *entry, int c)
  418. {
  419. fprintf(stderr, "expected '%c' in '%s'", c, entry->desc);
  420. abort();
  421. }
  422. static const char *append_field(QDict *error, QString *outstr,
  423. const QErrorStringTable *entry,
  424. const char *start)
  425. {
  426. QObject *obj;
  427. QDict *qdict;
  428. QString *key_qs;
  429. const char *end, *key;
  430. if (*start != '%')
  431. parse_error(entry, '%');
  432. start++;
  433. if (*start != '(')
  434. parse_error(entry, '(');
  435. start++;
  436. end = strchr(start, ')');
  437. if (!end)
  438. parse_error(entry, ')');
  439. key_qs = qstring_from_substr(start, 0, end - start - 1);
  440. key = qstring_get_str(key_qs);
  441. qdict = qobject_to_qdict(qdict_get(error, "data"));
  442. obj = qdict_get(qdict, key);
  443. if (!obj) {
  444. abort();
  445. }
  446. switch (qobject_type(obj)) {
  447. case QTYPE_QSTRING:
  448. qstring_append(outstr, qdict_get_str(qdict, key));
  449. break;
  450. case QTYPE_QINT:
  451. qstring_append_int(outstr, qdict_get_int(qdict, key));
  452. break;
  453. default:
  454. abort();
  455. }
  456. QDECREF(key_qs);
  457. return ++end;
  458. }
  459. static QString *qerror_format_desc(QDict *error,
  460. const QErrorStringTable *entry)
  461. {
  462. QString *qstring;
  463. const char *p;
  464. assert(entry != NULL);
  465. qstring = qstring_new();
  466. for (p = entry->desc; *p != '\0';) {
  467. if (*p != '%') {
  468. qstring_append_chr(qstring, *p++);
  469. } else if (*(p + 1) == '%') {
  470. qstring_append_chr(qstring, '%');
  471. p += 2;
  472. } else {
  473. p = append_field(error, qstring, entry, p);
  474. }
  475. }
  476. return qstring;
  477. }
  478. QString *qerror_format(const char *fmt, QDict *error)
  479. {
  480. const QErrorStringTable *entry = NULL;
  481. int i;
  482. for (i = 0; qerror_table[i].error_fmt; i++) {
  483. if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
  484. entry = &qerror_table[i];
  485. break;
  486. }
  487. }
  488. return qerror_format_desc(error, entry);
  489. }
  490. /**
  491. * qerror_human(): Format QError data into human-readable string.
  492. */
  493. QString *qerror_human(const QError *qerror)
  494. {
  495. return qerror_format_desc(qerror->error, qerror->entry);
  496. }
  497. /**
  498. * qerror_print(): Print QError data
  499. *
  500. * This function will print the member 'desc' of the specified QError object,
  501. * it uses error_report() for this, so that the output is routed to the right
  502. * place (ie. stderr or Monitor's device).
  503. */
  504. void qerror_print(QError *qerror)
  505. {
  506. QString *qstring = qerror_human(qerror);
  507. loc_push_restore(&qerror->loc);
  508. error_report("%s", qstring_get_str(qstring));
  509. loc_pop(&qerror->loc);
  510. QDECREF(qstring);
  511. }
  512. void qerror_report_internal(const char *file, int linenr, const char *func,
  513. const char *fmt, ...)
  514. {
  515. va_list va;
  516. QError *qerror;
  517. va_start(va, fmt);
  518. qerror = qerror_from_info(file, linenr, func, fmt, &va);
  519. va_end(va);
  520. if (monitor_cur_is_qmp()) {
  521. monitor_set_error(cur_mon, qerror);
  522. } else {
  523. qerror_print(qerror);
  524. QDECREF(qerror);
  525. }
  526. }
  527. /* Evil... */
  528. struct Error
  529. {
  530. QDict *obj;
  531. const char *fmt;
  532. char *msg;
  533. };
  534. void qerror_report_err(Error *err)
  535. {
  536. QError *qerr;
  537. int i;
  538. qerr = qerror_new();
  539. loc_save(&qerr->loc);
  540. QINCREF(err->obj);
  541. qerr->error = err->obj;
  542. for (i = 0; qerror_table[i].error_fmt; i++) {
  543. if (strcmp(qerror_table[i].error_fmt, err->fmt) == 0) {
  544. qerr->entry = &qerror_table[i];
  545. break;
  546. }
  547. }
  548. if (monitor_cur_is_qmp()) {
  549. monitor_set_error(cur_mon, qerr);
  550. } else {
  551. qerror_print(qerr);
  552. QDECREF(qerr);
  553. }
  554. }
  555. void assert_no_error(Error *err)
  556. {
  557. if (err) {
  558. qerror_report_err(err);
  559. abort();
  560. }
  561. }
  562. /**
  563. * qobject_to_qerror(): Convert a QObject into a QError
  564. */
  565. QError *qobject_to_qerror(const QObject *obj)
  566. {
  567. if (qobject_type(obj) != QTYPE_QERROR) {
  568. return NULL;
  569. }
  570. return container_of(obj, QError, base);
  571. }
  572. /**
  573. * qerror_destroy_obj(): Free all memory allocated by a QError
  574. */
  575. static void qerror_destroy_obj(QObject *obj)
  576. {
  577. QError *qerr;
  578. assert(obj != NULL);
  579. qerr = qobject_to_qerror(obj);
  580. QDECREF(qerr->error);
  581. g_free(qerr);
  582. }