qerror.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. QERR_ADD_CLIENT_FAILED,
  44. "Could not add client",
  45. },
  46. {
  47. QERR_AMBIGUOUS_PATH,
  48. "Path '%(path)' does not uniquely identify an object"
  49. },
  50. {
  51. QERR_BAD_BUS_FOR_DEVICE,
  52. "Device '%(device)' can't go on a %(bad_bus_type) bus",
  53. },
  54. {
  55. QERR_BASE_NOT_FOUND,
  56. "Base '%(base)' not found",
  57. },
  58. {
  59. QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
  60. "Block format '%(format)' used by device '%(name)' does not support feature '%(feature)'",
  61. },
  62. {
  63. QERR_BUS_NO_HOTPLUG,
  64. "Bus '%(bus)' does not support hotplugging",
  65. },
  66. {
  67. QERR_BUS_NOT_FOUND,
  68. "Bus '%(bus)' not found",
  69. },
  70. {
  71. QERR_COMMAND_DISABLED,
  72. "The command %(name) has been disabled for this instance",
  73. },
  74. {
  75. QERR_COMMAND_NOT_FOUND,
  76. "The command %(name) has not been found",
  77. },
  78. {
  79. QERR_DEVICE_ENCRYPTED,
  80. "'%(device)' (%(filename)) is encrypted",
  81. },
  82. {
  83. QERR_DEVICE_FEATURE_BLOCKS_MIGRATION,
  84. "Migration is disabled when using feature '%(feature)' in device '%(device)'",
  85. },
  86. {
  87. QERR_DEVICE_HAS_NO_MEDIUM,
  88. "Device '%(device)' has no medium",
  89. },
  90. {
  91. QERR_DEVICE_INIT_FAILED,
  92. "Device '%(device)' could not be initialized",
  93. },
  94. {
  95. QERR_DEVICE_IN_USE,
  96. "Device '%(device)' is in use",
  97. },
  98. {
  99. QERR_DEVICE_IS_READ_ONLY,
  100. "Device '%(device)' is read only",
  101. },
  102. {
  103. QERR_DEVICE_LOCKED,
  104. "Device '%(device)' is locked",
  105. },
  106. {
  107. QERR_DEVICE_MULTIPLE_BUSSES,
  108. "Device '%(device)' has multiple child busses",
  109. },
  110. {
  111. QERR_DEVICE_NO_BUS,
  112. "Device '%(device)' has no child bus",
  113. },
  114. {
  115. QERR_DEVICE_NO_HOTPLUG,
  116. "Device '%(device)' does not support hotplugging",
  117. },
  118. {
  119. QERR_DEVICE_NOT_ACTIVE,
  120. "Device '%(device)' has not been activated",
  121. },
  122. {
  123. QERR_DEVICE_NOT_ENCRYPTED,
  124. "Device '%(device)' is not encrypted",
  125. },
  126. {
  127. QERR_DEVICE_NOT_FOUND,
  128. "Device '%(device)' not found",
  129. },
  130. {
  131. QERR_DEVICE_NOT_REMOVABLE,
  132. "Device '%(device)' is not removable",
  133. },
  134. {
  135. QERR_DUPLICATE_ID,
  136. "Duplicate ID '%(id)' for %(object)",
  137. },
  138. {
  139. QERR_FD_NOT_FOUND,
  140. "File descriptor named '%(name)' not found",
  141. },
  142. {
  143. QERR_FD_NOT_SUPPLIED,
  144. "No file descriptor supplied via SCM_RIGHTS",
  145. },
  146. {
  147. QERR_FEATURE_DISABLED,
  148. "The feature '%(name)' is not enabled",
  149. },
  150. {
  151. QERR_INVALID_BLOCK_FORMAT,
  152. "Invalid block format '%(name)'",
  153. },
  154. {
  155. QERR_INVALID_OPTION_GROUP,
  156. "There is no option group '%(group)'",
  157. },
  158. {
  159. QERR_INVALID_PARAMETER,
  160. "Invalid parameter '%(name)'",
  161. },
  162. {
  163. QERR_INVALID_PARAMETER_COMBINATION,
  164. "Invalid parameter combination",
  165. },
  166. {
  167. QERR_INVALID_PARAMETER_TYPE,
  168. "Invalid parameter type for '%(name)', expected: %(expected)",
  169. },
  170. {
  171. QERR_INVALID_PARAMETER_VALUE,
  172. "Parameter '%(name)' expects %(expected)",
  173. },
  174. {
  175. QERR_INVALID_PASSWORD,
  176. "Password incorrect",
  177. },
  178. {
  179. QERR_IO_ERROR,
  180. "An IO error has occurred",
  181. },
  182. {
  183. QERR_JSON_PARSE_ERROR,
  184. "JSON parse error, %(message)",
  185. },
  186. {
  187. QERR_JSON_PARSING,
  188. "Invalid JSON syntax",
  189. },
  190. {
  191. QERR_KVM_MISSING_CAP,
  192. "Using KVM without %(capability), %(feature) unavailable",
  193. },
  194. {
  195. QERR_MIGRATION_ACTIVE,
  196. "There's a migration process in progress",
  197. },
  198. {
  199. QERR_MIGRATION_NOT_SUPPORTED,
  200. "State blocked by non-migratable device '%(device)'",
  201. },
  202. {
  203. QERR_MIGRATION_EXPECTED,
  204. "An incoming migration is expected before this command can be executed",
  205. },
  206. {
  207. QERR_MISSING_PARAMETER,
  208. "Parameter '%(name)' is missing",
  209. },
  210. {
  211. QERR_NO_BUS_FOR_DEVICE,
  212. "No '%(bus)' bus found for device '%(device)'",
  213. },
  214. {
  215. QERR_NOT_SUPPORTED,
  216. "Not supported",
  217. },
  218. {
  219. QERR_OPEN_FILE_FAILED,
  220. "Could not open '%(filename)'",
  221. },
  222. {
  223. QERR_PERMISSION_DENIED,
  224. "Insufficient permission to perform this operation",
  225. },
  226. {
  227. QERR_PROPERTY_NOT_FOUND,
  228. "Property '%(device).%(property)' not found",
  229. },
  230. {
  231. QERR_PROPERTY_VALUE_BAD,
  232. "Property '%(device).%(property)' doesn't take value '%(value)'",
  233. },
  234. {
  235. QERR_PROPERTY_VALUE_IN_USE,
  236. "Property '%(device).%(property)' can't take value '%(value)', it's in use",
  237. },
  238. {
  239. QERR_PROPERTY_VALUE_NOT_FOUND,
  240. "Property '%(device).%(property)' can't find value '%(value)'",
  241. },
  242. {
  243. QERR_PROPERTY_VALUE_NOT_POWER_OF_2,
  244. "Property '%(device).%(property)' doesn't take "
  245. "value '%(value)', it's not a power of 2",
  246. },
  247. {
  248. QERR_PROPERTY_VALUE_OUT_OF_RANGE,
  249. "Property '%(device).%(property)' doesn't take "
  250. "value %(value) (minimum: %(min), maximum: %(max))",
  251. },
  252. {
  253. QERR_QGA_COMMAND_FAILED,
  254. "Guest agent command failed, error was '%(message)'",
  255. },
  256. {
  257. QERR_QGA_LOGGING_FAILED,
  258. "Guest agent failed to log non-optional log statement",
  259. },
  260. {
  261. QERR_QMP_BAD_INPUT_OBJECT,
  262. "Expected '%(expected)' in QMP input",
  263. },
  264. {
  265. QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
  266. "QMP input object member '%(member)' expects '%(expected)'",
  267. },
  268. {
  269. QERR_QMP_EXTRA_MEMBER,
  270. "QMP input object member '%(member)' is unexpected",
  271. },
  272. {
  273. QERR_RESET_REQUIRED,
  274. "Resetting the Virtual Machine is required",
  275. },
  276. {
  277. QERR_SET_PASSWD_FAILED,
  278. "Could not set password",
  279. },
  280. {
  281. QERR_TOO_MANY_FILES,
  282. "Too many open files",
  283. },
  284. {
  285. QERR_UNDEFINED_ERROR,
  286. "An undefined error has occurred",
  287. },
  288. {
  289. QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
  290. "'%(device)' uses a %(format) feature which is not "
  291. "supported by this qemu version: %(feature)",
  292. },
  293. {
  294. QERR_UNSUPPORTED,
  295. "this feature or command is not currently supported",
  296. },
  297. {
  298. QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION,
  299. "Migration is disabled when VirtFS export path '%(path)' "
  300. "is mounted in the guest using mount_tag '%(tag)'",
  301. },
  302. {
  303. QERR_VNC_SERVER_FAILED,
  304. "Could not start VNC server on %(target)",
  305. },
  306. {
  307. QERR_SOCKET_CONNECT_FAILED,
  308. "Failed to connect to socket",
  309. },
  310. {
  311. QERR_SOCKET_LISTEN_FAILED,
  312. "Failed to set socket to listening mode",
  313. },
  314. {
  315. QERR_SOCKET_BIND_FAILED,
  316. "Failed to bind socket",
  317. },
  318. {
  319. QERR_SOCKET_CREATE_FAILED,
  320. "Failed to create socket",
  321. },
  322. {}
  323. };
  324. /**
  325. * qerror_new(): Create a new QError
  326. *
  327. * Return strong reference.
  328. */
  329. static 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. /**
  337. * qerror_from_info(): Create a new QError from error information
  338. *
  339. * Return strong reference.
  340. */
  341. static QError *qerror_from_info(ErrorClass err_class, const char *fmt,
  342. va_list *va)
  343. {
  344. QError *qerr;
  345. qerr = qerror_new();
  346. loc_save(&qerr->loc);
  347. qerr->err_msg = g_strdup_vprintf(fmt, *va);
  348. qerr->err_class = err_class;
  349. return qerr;
  350. }
  351. static void parse_error(const QErrorStringTable *entry, int c)
  352. {
  353. fprintf(stderr, "expected '%c' in '%s'", c, entry->desc);
  354. abort();
  355. }
  356. static const char *append_field(QDict *error, QString *outstr,
  357. const QErrorStringTable *entry,
  358. const char *start)
  359. {
  360. QObject *obj;
  361. QDict *qdict;
  362. QString *key_qs;
  363. const char *end, *key;
  364. if (*start != '%')
  365. parse_error(entry, '%');
  366. start++;
  367. if (*start != '(')
  368. parse_error(entry, '(');
  369. start++;
  370. end = strchr(start, ')');
  371. if (!end)
  372. parse_error(entry, ')');
  373. key_qs = qstring_from_substr(start, 0, end - start - 1);
  374. key = qstring_get_str(key_qs);
  375. qdict = qobject_to_qdict(qdict_get(error, "data"));
  376. obj = qdict_get(qdict, key);
  377. if (!obj) {
  378. abort();
  379. }
  380. switch (qobject_type(obj)) {
  381. case QTYPE_QSTRING:
  382. qstring_append(outstr, qdict_get_str(qdict, key));
  383. break;
  384. case QTYPE_QINT:
  385. qstring_append_int(outstr, qdict_get_int(qdict, key));
  386. break;
  387. default:
  388. abort();
  389. }
  390. QDECREF(key_qs);
  391. return ++end;
  392. }
  393. static QString *qerror_format_desc(QDict *error,
  394. const QErrorStringTable *entry)
  395. {
  396. QString *qstring;
  397. const char *p;
  398. assert(entry != NULL);
  399. qstring = qstring_new();
  400. for (p = entry->desc; *p != '\0';) {
  401. if (*p != '%') {
  402. qstring_append_chr(qstring, *p++);
  403. } else if (*(p + 1) == '%') {
  404. qstring_append_chr(qstring, '%');
  405. p += 2;
  406. } else {
  407. p = append_field(error, qstring, entry, p);
  408. }
  409. }
  410. return qstring;
  411. }
  412. char *qerror_format(const char *fmt, QDict *error)
  413. {
  414. const QErrorStringTable *entry = NULL;
  415. QString *qstr;
  416. char *ret;
  417. int i;
  418. for (i = 0; qerror_table[i].error_fmt; i++) {
  419. if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
  420. entry = &qerror_table[i];
  421. break;
  422. }
  423. }
  424. qstr = qerror_format_desc(error, entry);
  425. ret = g_strdup(qstring_get_str(qstr));
  426. QDECREF(qstr);
  427. return ret;
  428. }
  429. /**
  430. * qerror_human(): Format QError data into human-readable string.
  431. */
  432. QString *qerror_human(const QError *qerror)
  433. {
  434. return qstring_from_str(qerror->err_msg);
  435. }
  436. /**
  437. * qerror_print(): Print QError data
  438. *
  439. * This function will print the member 'desc' of the specified QError object,
  440. * it uses error_report() for this, so that the output is routed to the right
  441. * place (ie. stderr or Monitor's device).
  442. */
  443. static void qerror_print(QError *qerror)
  444. {
  445. QString *qstring = qerror_human(qerror);
  446. loc_push_restore(&qerror->loc);
  447. error_report("%s", qstring_get_str(qstring));
  448. loc_pop(&qerror->loc);
  449. QDECREF(qstring);
  450. }
  451. void qerror_report(ErrorClass eclass, const char *fmt, ...)
  452. {
  453. va_list va;
  454. QError *qerror;
  455. va_start(va, fmt);
  456. qerror = qerror_from_info(eclass, fmt, &va);
  457. va_end(va);
  458. if (monitor_cur_is_qmp()) {
  459. monitor_set_error(cur_mon, qerror);
  460. } else {
  461. qerror_print(qerror);
  462. QDECREF(qerror);
  463. }
  464. }
  465. /* Evil... */
  466. struct Error
  467. {
  468. QDict *obj;
  469. char *msg;
  470. ErrorClass err_class;
  471. };
  472. void qerror_report_err(Error *err)
  473. {
  474. QError *qerr;
  475. qerr = qerror_new();
  476. loc_save(&qerr->loc);
  477. QINCREF(err->obj);
  478. qerr->error = err->obj;
  479. qerr->err_msg = g_strdup(err->msg);
  480. qerr->err_class = err->err_class;
  481. if (monitor_cur_is_qmp()) {
  482. monitor_set_error(cur_mon, qerr);
  483. } else {
  484. qerror_print(qerr);
  485. QDECREF(qerr);
  486. }
  487. }
  488. void assert_no_error(Error *err)
  489. {
  490. if (err) {
  491. qerror_report_err(err);
  492. abort();
  493. }
  494. }
  495. /**
  496. * qobject_to_qerror(): Convert a QObject into a QError
  497. */
  498. static QError *qobject_to_qerror(const QObject *obj)
  499. {
  500. if (qobject_type(obj) != QTYPE_QERROR) {
  501. return NULL;
  502. }
  503. return container_of(obj, QError, base);
  504. }
  505. /**
  506. * qerror_destroy_obj(): Free all memory allocated by a QError
  507. */
  508. static void qerror_destroy_obj(QObject *obj)
  509. {
  510. QError *qerr;
  511. assert(obj != NULL);
  512. qerr = qobject_to_qerror(obj);
  513. QDECREF(qerr->error);
  514. g_free(qerr->err_msg);
  515. g_free(qerr);
  516. }