ui-qmp-cmds.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * QMP commands related to UI
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "io/channel-file.h"
  17. #include "monitor/qmp-helpers.h"
  18. #include "qapi/qapi-commands-ui.h"
  19. #include "qapi/qmp/qerror.h"
  20. #include "qemu/coroutine.h"
  21. #include "qemu/cutils.h"
  22. #include "trace.h"
  23. #include "ui/console.h"
  24. #include "ui/dbus-display.h"
  25. #include "ui/qemu-spice.h"
  26. #ifdef CONFIG_PNG
  27. #include <png.h>
  28. #endif
  29. void qmp_set_password(SetPasswordOptions *opts, Error **errp)
  30. {
  31. int rc;
  32. if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
  33. if (!qemu_using_spice(errp)) {
  34. return;
  35. }
  36. rc = qemu_spice.set_passwd(opts->password,
  37. opts->connected == SET_PASSWORD_ACTION_FAIL,
  38. opts->connected == SET_PASSWORD_ACTION_DISCONNECT);
  39. } else {
  40. assert(opts->protocol == DISPLAY_PROTOCOL_VNC);
  41. if (opts->connected != SET_PASSWORD_ACTION_KEEP) {
  42. /* vnc supports "connected=keep" only */
  43. error_setg(errp, "parameter 'connected' must be 'keep'"
  44. " when 'protocol' is 'vnc'");
  45. return;
  46. }
  47. /*
  48. * Note that setting an empty password will not disable login
  49. * through this interface.
  50. */
  51. rc = vnc_display_password(opts->u.vnc.display, opts->password);
  52. }
  53. if (rc != 0) {
  54. error_setg(errp, "Could not set password");
  55. }
  56. }
  57. void qmp_expire_password(ExpirePasswordOptions *opts, Error **errp)
  58. {
  59. time_t when;
  60. int rc;
  61. const char *whenstr = opts->time;
  62. const char *numstr = NULL;
  63. uint64_t num;
  64. if (strcmp(whenstr, "now") == 0) {
  65. when = 0;
  66. } else if (strcmp(whenstr, "never") == 0) {
  67. when = TIME_MAX;
  68. } else if (whenstr[0] == '+') {
  69. when = time(NULL);
  70. numstr = whenstr + 1;
  71. } else {
  72. when = 0;
  73. numstr = whenstr;
  74. }
  75. if (numstr) {
  76. if (qemu_strtou64(numstr, NULL, 10, &num) < 0) {
  77. error_setg(errp, "Parameter 'time' doesn't take value '%s'",
  78. whenstr);
  79. return;
  80. }
  81. when += num;
  82. }
  83. if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
  84. if (!qemu_using_spice(errp)) {
  85. return;
  86. }
  87. rc = qemu_spice.set_pw_expire(when);
  88. } else {
  89. assert(opts->protocol == DISPLAY_PROTOCOL_VNC);
  90. rc = vnc_display_pw_expire(opts->u.vnc.display, when);
  91. }
  92. if (rc != 0) {
  93. error_setg(errp, "Could not set password expire time");
  94. }
  95. }
  96. #ifdef CONFIG_VNC
  97. void qmp_change_vnc_password(const char *password, Error **errp)
  98. {
  99. if (vnc_display_password(NULL, password) < 0) {
  100. error_setg(errp, "Could not set password");
  101. }
  102. }
  103. #endif
  104. bool qmp_add_client_spice(int fd, bool has_skipauth, bool skipauth,
  105. bool has_tls, bool tls, Error **errp)
  106. {
  107. if (!qemu_using_spice(errp)) {
  108. return false;
  109. }
  110. skipauth = has_skipauth ? skipauth : false;
  111. tls = has_tls ? tls : false;
  112. if (qemu_spice.display_add_client(fd, skipauth, tls) < 0) {
  113. error_setg(errp, "spice failed to add client");
  114. return false;
  115. }
  116. return true;
  117. }
  118. #ifdef CONFIG_VNC
  119. bool qmp_add_client_vnc(int fd, bool has_skipauth, bool skipauth,
  120. bool has_tls, bool tls, Error **errp)
  121. {
  122. skipauth = has_skipauth ? skipauth : false;
  123. vnc_display_add_client(NULL, fd, skipauth);
  124. return true;
  125. }
  126. #endif
  127. #ifdef CONFIG_DBUS_DISPLAY
  128. bool qmp_add_client_dbus_display(int fd, bool has_skipauth, bool skipauth,
  129. bool has_tls, bool tls, Error **errp)
  130. {
  131. if (!qemu_using_dbus_display(errp)) {
  132. return false;
  133. }
  134. if (!qemu_dbus_display.add_client(fd, errp)) {
  135. return false;
  136. }
  137. return true;
  138. }
  139. #endif
  140. void qmp_display_reload(DisplayReloadOptions *arg, Error **errp)
  141. {
  142. switch (arg->type) {
  143. case DISPLAY_RELOAD_TYPE_VNC:
  144. #ifdef CONFIG_VNC
  145. if (arg->u.vnc.has_tls_certs && arg->u.vnc.tls_certs) {
  146. vnc_display_reload_certs(NULL, errp);
  147. }
  148. #else
  149. error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
  150. #endif
  151. break;
  152. default:
  153. abort();
  154. }
  155. }
  156. void qmp_display_update(DisplayUpdateOptions *arg, Error **errp)
  157. {
  158. switch (arg->type) {
  159. case DISPLAY_UPDATE_TYPE_VNC:
  160. #ifdef CONFIG_VNC
  161. vnc_display_update(&arg->u.vnc, errp);
  162. #else
  163. error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
  164. #endif
  165. break;
  166. default:
  167. abort();
  168. }
  169. }
  170. void qmp_client_migrate_info(const char *protocol, const char *hostname,
  171. bool has_port, int64_t port,
  172. bool has_tls_port, int64_t tls_port,
  173. const char *cert_subject,
  174. Error **errp)
  175. {
  176. if (strcmp(protocol, "spice") == 0) {
  177. if (!qemu_using_spice(errp)) {
  178. return;
  179. }
  180. if (!has_port && !has_tls_port) {
  181. error_setg(errp, "parameter 'port' or 'tls-port' is required");
  182. return;
  183. }
  184. if (qemu_spice.migrate_info(hostname,
  185. has_port ? port : -1,
  186. has_tls_port ? tls_port : -1,
  187. cert_subject)) {
  188. error_setg(errp, "Could not set up display for migration");
  189. return;
  190. }
  191. return;
  192. }
  193. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
  194. }
  195. #ifdef CONFIG_PIXMAN
  196. #ifdef CONFIG_PNG
  197. /**
  198. * png_save: Take a screenshot as PNG
  199. *
  200. * Saves screendump as a PNG file
  201. *
  202. * Returns true for success or false for error.
  203. *
  204. * @fd: File descriptor for PNG file.
  205. * @image: Image data in pixman format.
  206. * @errp: Pointer to an error.
  207. */
  208. static bool png_save(int fd, pixman_image_t *image, Error **errp)
  209. {
  210. int width = pixman_image_get_width(image);
  211. int height = pixman_image_get_height(image);
  212. png_struct *png_ptr;
  213. png_info *info_ptr;
  214. g_autoptr(pixman_image_t) linebuf =
  215. qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
  216. uint8_t *buf = (uint8_t *)pixman_image_get_data(linebuf);
  217. FILE *f = fdopen(fd, "wb");
  218. int y;
  219. if (!f) {
  220. error_setg_errno(errp, errno,
  221. "Failed to create file from file descriptor");
  222. return false;
  223. }
  224. png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
  225. NULL, NULL);
  226. if (!png_ptr) {
  227. error_setg(errp, "PNG creation failed. Unable to write struct");
  228. fclose(f);
  229. return false;
  230. }
  231. info_ptr = png_create_info_struct(png_ptr);
  232. if (!info_ptr) {
  233. error_setg(errp, "PNG creation failed. Unable to write info");
  234. fclose(f);
  235. png_destroy_write_struct(&png_ptr, &info_ptr);
  236. return false;
  237. }
  238. png_init_io(png_ptr, f);
  239. png_set_IHDR(png_ptr, info_ptr, width, height, 8,
  240. PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  241. PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  242. png_write_info(png_ptr, info_ptr);
  243. for (y = 0; y < height; ++y) {
  244. qemu_pixman_linebuf_fill(linebuf, image, width, 0, y);
  245. png_write_row(png_ptr, buf);
  246. }
  247. png_write_end(png_ptr, NULL);
  248. png_destroy_write_struct(&png_ptr, &info_ptr);
  249. if (fclose(f) != 0) {
  250. error_setg_errno(errp, errno,
  251. "PNG creation failed. Unable to close file");
  252. return false;
  253. }
  254. return true;
  255. }
  256. #else /* no png support */
  257. static bool png_save(int fd, pixman_image_t *image, Error **errp)
  258. {
  259. error_setg(errp, "Enable PNG support with libpng for screendump");
  260. return false;
  261. }
  262. #endif /* CONFIG_PNG */
  263. static bool ppm_save(int fd, pixman_image_t *image, Error **errp)
  264. {
  265. int width = pixman_image_get_width(image);
  266. int height = pixman_image_get_height(image);
  267. g_autoptr(Object) ioc = OBJECT(qio_channel_file_new_fd(fd));
  268. g_autofree char *header = NULL;
  269. g_autoptr(pixman_image_t) linebuf = NULL;
  270. int y;
  271. trace_ppm_save(fd, image);
  272. header = g_strdup_printf("P6\n%d %d\n%d\n", width, height, 255);
  273. if (qio_channel_write_all(QIO_CHANNEL(ioc),
  274. header, strlen(header), errp) < 0) {
  275. return false;
  276. }
  277. linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
  278. for (y = 0; y < height; y++) {
  279. qemu_pixman_linebuf_fill(linebuf, image, width, 0, y);
  280. if (qio_channel_write_all(QIO_CHANNEL(ioc),
  281. (char *)pixman_image_get_data(linebuf),
  282. pixman_image_get_stride(linebuf), errp) < 0) {
  283. return false;
  284. }
  285. }
  286. return true;
  287. }
  288. /* Safety: coroutine-only, concurrent-coroutine safe, main thread only */
  289. void coroutine_fn
  290. qmp_screendump(const char *filename, const char *device,
  291. bool has_head, int64_t head,
  292. bool has_format, ImageFormat format, Error **errp)
  293. {
  294. g_autoptr(pixman_image_t) image = NULL;
  295. QemuConsole *con;
  296. DisplaySurface *surface;
  297. int fd;
  298. if (device) {
  299. con = qemu_console_lookup_by_device_name(device, has_head ? head : 0,
  300. errp);
  301. if (!con) {
  302. return;
  303. }
  304. } else {
  305. if (has_head) {
  306. error_setg(errp, "'head' must be specified together with 'device'");
  307. return;
  308. }
  309. con = qemu_console_lookup_by_index(0);
  310. if (!con) {
  311. error_setg(errp, "There is no console to take a screendump from");
  312. return;
  313. }
  314. }
  315. qemu_console_co_wait_update(con);
  316. /*
  317. * All pending coroutines are woken up, while the BQL is held. No
  318. * further graphic update are possible until it is released. Take
  319. * an image ref before that.
  320. */
  321. surface = qemu_console_surface(con);
  322. if (!surface) {
  323. error_setg(errp, "no surface");
  324. return;
  325. }
  326. image = pixman_image_ref(surface->image);
  327. fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
  328. if (fd == -1) {
  329. error_setg(errp, "failed to open file '%s': %s", filename,
  330. strerror(errno));
  331. return;
  332. }
  333. /*
  334. * The image content could potentially be updated as the coroutine
  335. * yields and releases the BQL. It could produce corrupted dump, but
  336. * it should be otherwise safe.
  337. */
  338. if (has_format && format == IMAGE_FORMAT_PNG) {
  339. /* PNG format specified for screendump */
  340. if (!png_save(fd, image, errp)) {
  341. qemu_unlink(filename);
  342. }
  343. } else {
  344. /* PPM format specified/default for screendump */
  345. if (!ppm_save(fd, image, errp)) {
  346. qemu_unlink(filename);
  347. }
  348. }
  349. }
  350. #endif /* CONFIG_PIXMAN */