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