qemu

FORK: QEMU emulator
git clone https://git.neptards.moe/neptards/qemu.git
Log | Files | Refs | Submodules | LICENSE

dbus.c (14871B)


      1 /*
      2  * QEMU DBus display
      3  *
      4  * Copyright (c) 2021 Marc-André Lureau <marcandre.lureau@redhat.com>
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a copy
      7  * of this software and associated documentation files (the "Software"), to deal
      8  * in the Software without restriction, including without limitation the rights
      9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     10  * copies of the Software, and to permit persons to whom the Software is
     11  * furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
     19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     22  * THE SOFTWARE.
     23  */
     24 #include "qemu/osdep.h"
     25 #include "qemu/cutils.h"
     26 #include "qemu/dbus.h"
     27 #include "qemu/main-loop.h"
     28 #include "qemu/option.h"
     29 #include "qom/object_interfaces.h"
     30 #include "sysemu/sysemu.h"
     31 #include "ui/dbus-module.h"
     32 #include "ui/egl-helpers.h"
     33 #include "ui/egl-context.h"
     34 #include "audio/audio.h"
     35 #include "audio/audio_int.h"
     36 #include "qapi/error.h"
     37 #include "trace.h"
     38 
     39 #include "dbus.h"
     40 
     41 static DBusDisplay *dbus_display;
     42 
     43 static QEMUGLContext dbus_create_context(DisplayGLCtx *dgc,
     44                                          QEMUGLParams *params)
     45 {
     46     eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
     47                    qemu_egl_rn_ctx);
     48     return qemu_egl_create_context(dgc, params);
     49 }
     50 
     51 static bool
     52 dbus_is_compatible_dcl(DisplayGLCtx *dgc,
     53                        DisplayChangeListener *dcl)
     54 {
     55     return dcl->ops == &dbus_gl_dcl_ops || dcl->ops == &dbus_console_dcl_ops;
     56 }
     57 
     58 static void
     59 dbus_create_texture(DisplayGLCtx *ctx, DisplaySurface *surface)
     60 {
     61     surface_gl_create_texture(ctx->gls, surface);
     62 }
     63 
     64 static void
     65 dbus_destroy_texture(DisplayGLCtx *ctx, DisplaySurface *surface)
     66 {
     67     surface_gl_destroy_texture(ctx->gls, surface);
     68 }
     69 
     70 static void
     71 dbus_update_texture(DisplayGLCtx *ctx, DisplaySurface *surface,
     72                     int x, int y, int w, int h)
     73 {
     74     surface_gl_update_texture(ctx->gls, surface, x, y, w, h);
     75 }
     76 
     77 static const DisplayGLCtxOps dbus_gl_ops = {
     78     .dpy_gl_ctx_is_compatible_dcl = dbus_is_compatible_dcl,
     79     .dpy_gl_ctx_create       = dbus_create_context,
     80     .dpy_gl_ctx_destroy      = qemu_egl_destroy_context,
     81     .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
     82     .dpy_gl_ctx_create_texture = dbus_create_texture,
     83     .dpy_gl_ctx_destroy_texture = dbus_destroy_texture,
     84     .dpy_gl_ctx_update_texture = dbus_update_texture,
     85 };
     86 
     87 static NotifierList dbus_display_notifiers =
     88     NOTIFIER_LIST_INITIALIZER(dbus_display_notifiers);
     89 
     90 void
     91 dbus_display_notifier_add(Notifier *notifier)
     92 {
     93     notifier_list_add(&dbus_display_notifiers, notifier);
     94 }
     95 
     96 static void
     97 dbus_display_notifier_remove(Notifier *notifier)
     98 {
     99     notifier_remove(notifier);
    100 }
    101 
    102 void
    103 dbus_display_notify(DBusDisplayEvent *event)
    104 {
    105     notifier_list_notify(&dbus_display_notifiers, event);
    106 }
    107 
    108 static void
    109 dbus_display_init(Object *o)
    110 {
    111     DBusDisplay *dd = DBUS_DISPLAY(o);
    112     g_autoptr(GDBusObjectSkeleton) vm = NULL;
    113 
    114     dd->glctx.ops = &dbus_gl_ops;
    115     if (display_opengl) {
    116         dd->glctx.gls = qemu_gl_init_shader();
    117     }
    118     dd->iface = qemu_dbus_display1_vm_skeleton_new();
    119     dd->consoles = g_ptr_array_new_with_free_func(g_object_unref);
    120 
    121     dd->server = g_dbus_object_manager_server_new(DBUS_DISPLAY1_ROOT);
    122 
    123     vm = g_dbus_object_skeleton_new(DBUS_DISPLAY1_ROOT "/VM");
    124     g_dbus_object_skeleton_add_interface(
    125         vm, G_DBUS_INTERFACE_SKELETON(dd->iface));
    126     g_dbus_object_manager_server_export(dd->server, vm);
    127 
    128     dbus_clipboard_init(dd);
    129     dbus_chardev_init(dd);
    130 }
    131 
    132 static void
    133 dbus_display_finalize(Object *o)
    134 {
    135     DBusDisplay *dd = DBUS_DISPLAY(o);
    136 
    137     if (dd->notifier.notify) {
    138         dbus_display_notifier_remove(&dd->notifier);
    139     }
    140 
    141     qemu_clipboard_peer_unregister(&dd->clipboard_peer);
    142     g_clear_object(&dd->clipboard);
    143 
    144     g_clear_object(&dd->server);
    145     g_clear_pointer(&dd->consoles, g_ptr_array_unref);
    146     if (dd->add_client_cancellable) {
    147         g_cancellable_cancel(dd->add_client_cancellable);
    148     }
    149     g_clear_object(&dd->add_client_cancellable);
    150     g_clear_object(&dd->bus);
    151     g_clear_object(&dd->iface);
    152     g_free(dd->dbus_addr);
    153     g_free(dd->audiodev);
    154     g_clear_pointer(&dd->glctx.gls, qemu_gl_fini_shader);
    155     dbus_display = NULL;
    156 }
    157 
    158 static bool
    159 dbus_display_add_console(DBusDisplay *dd, int idx, Error **errp)
    160 {
    161     QemuConsole *con;
    162     DBusDisplayConsole *dbus_console;
    163 
    164     con = qemu_console_lookup_by_index(idx);
    165     assert(con);
    166 
    167     if (qemu_console_is_graphic(con) &&
    168         dd->gl_mode != DISPLAYGL_MODE_OFF) {
    169         qemu_console_set_display_gl_ctx(con, &dd->glctx);
    170     }
    171 
    172     dbus_console = dbus_display_console_new(dd, con);
    173     g_ptr_array_insert(dd->consoles, idx, dbus_console);
    174     g_dbus_object_manager_server_export(dd->server,
    175                                         G_DBUS_OBJECT_SKELETON(dbus_console));
    176     return true;
    177 }
    178 
    179 static void
    180 dbus_display_complete(UserCreatable *uc, Error **errp)
    181 {
    182     DBusDisplay *dd = DBUS_DISPLAY(uc);
    183     g_autoptr(GError) err = NULL;
    184     g_autofree char *uuid = qemu_uuid_unparse_strdup(&qemu_uuid);
    185     g_autoptr(GArray) consoles = NULL;
    186     GVariant *console_ids;
    187     int idx;
    188 
    189     if (!object_resolve_path_type("", TYPE_DBUS_DISPLAY, NULL)) {
    190         error_setg(errp, "There is already an instance of %s",
    191                    TYPE_DBUS_DISPLAY);
    192         return;
    193     }
    194 
    195     if (dd->p2p) {
    196         /* wait for dbus_display_add_client() */
    197         dbus_display = dd;
    198     } else if (dd->dbus_addr && *dd->dbus_addr) {
    199         dd->bus = g_dbus_connection_new_for_address_sync(dd->dbus_addr,
    200                         G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
    201                         G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
    202                         NULL, NULL, &err);
    203     } else {
    204         dd->bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &err);
    205     }
    206     if (err) {
    207         error_setg(errp, "failed to connect to DBus: %s", err->message);
    208         return;
    209     }
    210 
    211     if (dd->audiodev && *dd->audiodev) {
    212         AudioState *audio_state = audio_state_by_name(dd->audiodev);
    213         if (!audio_state) {
    214             error_setg(errp, "Audiodev '%s' not found", dd->audiodev);
    215             return;
    216         }
    217         if (!g_str_equal(audio_state->drv->name, "dbus")) {
    218             error_setg(errp, "Audiodev '%s' is not compatible with DBus",
    219                        dd->audiodev);
    220             return;
    221         }
    222         audio_state->drv->set_dbus_server(audio_state, dd->server);
    223     }
    224 
    225     consoles = g_array_new(FALSE, FALSE, sizeof(guint32));
    226     for (idx = 0;; idx++) {
    227         if (!qemu_console_lookup_by_index(idx)) {
    228             break;
    229         }
    230         if (!dbus_display_add_console(dd, idx, errp)) {
    231             return;
    232         }
    233         g_array_append_val(consoles, idx);
    234     }
    235 
    236     console_ids = g_variant_new_from_data(
    237         G_VARIANT_TYPE("au"),
    238         consoles->data, consoles->len * sizeof(guint32), TRUE,
    239         (GDestroyNotify)g_array_unref, consoles);
    240     g_steal_pointer(&consoles);
    241     g_object_set(dd->iface,
    242                  "name", qemu_name ?: "QEMU " QEMU_VERSION,
    243                  "uuid", uuid,
    244                  "console-ids", console_ids,
    245                  NULL);
    246 
    247     if (dd->bus) {
    248         g_dbus_object_manager_server_set_connection(dd->server, dd->bus);
    249         g_bus_own_name_on_connection(dd->bus, "org.qemu",
    250                                      G_BUS_NAME_OWNER_FLAGS_NONE,
    251                                      NULL, NULL, NULL, NULL);
    252     }
    253 }
    254 
    255 static void
    256 dbus_display_add_client_ready(GObject *source_object,
    257                               GAsyncResult *res,
    258                               gpointer user_data)
    259 {
    260     g_autoptr(GError) err = NULL;
    261     g_autoptr(GDBusConnection) conn = NULL;
    262 
    263     g_clear_object(&dbus_display->add_client_cancellable);
    264 
    265     conn = g_dbus_connection_new_finish(res, &err);
    266     if (!conn) {
    267         error_printf("Failed to accept D-Bus client: %s", err->message);
    268     }
    269 
    270     g_dbus_object_manager_server_set_connection(dbus_display->server, conn);
    271     g_dbus_connection_start_message_processing(conn);
    272 }
    273 
    274 
    275 static bool
    276 dbus_display_add_client(int csock, Error **errp)
    277 {
    278     g_autoptr(GError) err = NULL;
    279     g_autoptr(GSocket) socket = NULL;
    280     g_autoptr(GSocketConnection) conn = NULL;
    281     g_autofree char *guid = g_dbus_generate_guid();
    282 
    283     if (!dbus_display) {
    284         error_setg(errp, "p2p connections not accepted in bus mode");
    285         return false;
    286     }
    287 
    288     if (dbus_display->add_client_cancellable) {
    289         g_cancellable_cancel(dbus_display->add_client_cancellable);
    290     }
    291 
    292     socket = g_socket_new_from_fd(csock, &err);
    293     if (!socket) {
    294         error_setg(errp, "Failed to setup D-Bus socket: %s", err->message);
    295         return false;
    296     }
    297 
    298     conn = g_socket_connection_factory_create_connection(socket);
    299 
    300     dbus_display->add_client_cancellable = g_cancellable_new();
    301 
    302     g_dbus_connection_new(G_IO_STREAM(conn),
    303                           guid,
    304                           G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
    305                           G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
    306                           NULL,
    307                           dbus_display->add_client_cancellable,
    308                           dbus_display_add_client_ready,
    309                           NULL);
    310 
    311     return true;
    312 }
    313 
    314 static bool
    315 get_dbus_p2p(Object *o, Error **errp)
    316 {
    317     DBusDisplay *dd = DBUS_DISPLAY(o);
    318 
    319     return dd->p2p;
    320 }
    321 
    322 static void
    323 set_dbus_p2p(Object *o, bool p2p, Error **errp)
    324 {
    325     DBusDisplay *dd = DBUS_DISPLAY(o);
    326 
    327     dd->p2p = p2p;
    328 }
    329 
    330 static char *
    331 get_dbus_addr(Object *o, Error **errp)
    332 {
    333     DBusDisplay *dd = DBUS_DISPLAY(o);
    334 
    335     return g_strdup(dd->dbus_addr);
    336 }
    337 
    338 static void
    339 set_dbus_addr(Object *o, const char *str, Error **errp)
    340 {
    341     DBusDisplay *dd = DBUS_DISPLAY(o);
    342 
    343     g_free(dd->dbus_addr);
    344     dd->dbus_addr = g_strdup(str);
    345 }
    346 
    347 static char *
    348 get_audiodev(Object *o, Error **errp)
    349 {
    350     DBusDisplay *dd = DBUS_DISPLAY(o);
    351 
    352     return g_strdup(dd->audiodev);
    353 }
    354 
    355 static void
    356 set_audiodev(Object *o, const char *str, Error **errp)
    357 {
    358     DBusDisplay *dd = DBUS_DISPLAY(o);
    359 
    360     g_free(dd->audiodev);
    361     dd->audiodev = g_strdup(str);
    362 }
    363 
    364 
    365 static int
    366 get_gl_mode(Object *o, Error **errp)
    367 {
    368     DBusDisplay *dd = DBUS_DISPLAY(o);
    369 
    370     return dd->gl_mode;
    371 }
    372 
    373 static void
    374 set_gl_mode(Object *o, int val, Error **errp)
    375 {
    376     DBusDisplay *dd = DBUS_DISPLAY(o);
    377 
    378     dd->gl_mode = val;
    379 }
    380 
    381 static void
    382 dbus_display_class_init(ObjectClass *oc, void *data)
    383 {
    384     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
    385 
    386     ucc->complete = dbus_display_complete;
    387     object_class_property_add_bool(oc, "p2p", get_dbus_p2p, set_dbus_p2p);
    388     object_class_property_add_str(oc, "addr", get_dbus_addr, set_dbus_addr);
    389     object_class_property_add_str(oc, "audiodev", get_audiodev, set_audiodev);
    390     object_class_property_add_enum(oc, "gl-mode",
    391                                    "DisplayGLMode", &DisplayGLMode_lookup,
    392                                    get_gl_mode, set_gl_mode);
    393 }
    394 
    395 #define TYPE_CHARDEV_VC "chardev-vc"
    396 
    397 typedef struct DBusVCClass {
    398     DBusChardevClass parent_class;
    399 
    400     void (*parent_parse)(QemuOpts *opts, ChardevBackend *b, Error **errp);
    401 } DBusVCClass;
    402 
    403 DECLARE_CLASS_CHECKERS(DBusVCClass, DBUS_VC,
    404                        TYPE_CHARDEV_VC)
    405 
    406 static void
    407 dbus_vc_parse(QemuOpts *opts, ChardevBackend *backend,
    408               Error **errp)
    409 {
    410     DBusVCClass *klass = DBUS_VC_CLASS(object_class_by_name(TYPE_CHARDEV_VC));
    411     const char *name = qemu_opt_get(opts, "name");
    412     const char *id = qemu_opts_id(opts);
    413 
    414     if (name == NULL) {
    415         if (g_str_has_prefix(id, "compat_monitor")) {
    416             name = "org.qemu.monitor.hmp.0";
    417         } else if (g_str_has_prefix(id, "serial")) {
    418             name = "org.qemu.console.serial.0";
    419         } else {
    420             name = "";
    421         }
    422         if (!qemu_opt_set(opts, "name", name, errp)) {
    423             return;
    424         }
    425     }
    426 
    427     klass->parent_parse(opts, backend, errp);
    428 }
    429 
    430 static void
    431 dbus_vc_class_init(ObjectClass *oc, void *data)
    432 {
    433     DBusVCClass *klass = DBUS_VC_CLASS(oc);
    434     ChardevClass *cc = CHARDEV_CLASS(oc);
    435 
    436     klass->parent_parse = cc->parse;
    437     cc->parse = dbus_vc_parse;
    438 }
    439 
    440 static const TypeInfo dbus_vc_type_info = {
    441     .name = TYPE_CHARDEV_VC,
    442     .parent = TYPE_CHARDEV_DBUS,
    443     .class_size = sizeof(DBusVCClass),
    444     .class_init = dbus_vc_class_init,
    445 };
    446 
    447 static void
    448 early_dbus_init(DisplayOptions *opts)
    449 {
    450     DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_OFF;
    451 
    452     if (mode != DISPLAYGL_MODE_OFF) {
    453         if (egl_rendernode_init(opts->u.dbus.rendernode, mode) < 0) {
    454             error_report("dbus: render node init failed");
    455             exit(1);
    456         }
    457 
    458         display_opengl = 1;
    459     }
    460 
    461     type_register(&dbus_vc_type_info);
    462 }
    463 
    464 static void
    465 dbus_init(DisplayState *ds, DisplayOptions *opts)
    466 {
    467     DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_OFF;
    468 
    469     if (opts->u.dbus.addr && opts->u.dbus.p2p) {
    470         error_report("dbus: can't accept both addr=X and p2p=yes options");
    471         exit(1);
    472     }
    473 
    474     using_dbus_display = 1;
    475 
    476     object_new_with_props(TYPE_DBUS_DISPLAY,
    477                           object_get_objects_root(),
    478                           "dbus-display", &error_fatal,
    479                           "addr", opts->u.dbus.addr ?: "",
    480                           "audiodev", opts->u.dbus.audiodev ?: "",
    481                           "gl-mode", DisplayGLMode_str(mode),
    482                           "p2p", yes_no(opts->u.dbus.p2p),
    483                           NULL);
    484 }
    485 
    486 static const TypeInfo dbus_display_info = {
    487     .name = TYPE_DBUS_DISPLAY,
    488     .parent = TYPE_OBJECT,
    489     .instance_size = sizeof(DBusDisplay),
    490     .instance_init = dbus_display_init,
    491     .instance_finalize = dbus_display_finalize,
    492     .class_init = dbus_display_class_init,
    493     .interfaces = (InterfaceInfo[]) {
    494         { TYPE_USER_CREATABLE },
    495         { }
    496     }
    497 };
    498 
    499 static QemuDisplay qemu_display_dbus = {
    500     .type       = DISPLAY_TYPE_DBUS,
    501     .early_init = early_dbus_init,
    502     .init       = dbus_init,
    503 };
    504 
    505 static void register_dbus(void)
    506 {
    507     qemu_dbus_display = (struct QemuDBusDisplayOps) {
    508         .add_client = dbus_display_add_client,
    509     };
    510     type_register_static(&dbus_display_info);
    511     qemu_display_register(&qemu_display_dbus);
    512 }
    513 
    514 type_init(register_dbus);
    515 
    516 #ifdef CONFIG_OPENGL
    517 module_dep("ui-opengl");
    518 #endif