Skip to content

Commit

Permalink
ui/dbus: add chardev backend & interface
Browse files Browse the repository at this point in the history
Add a new chardev backend which allows D-Bus client to handle the
chardev stream & events.

Signed-off-by: Marc-André Lureau <[email protected]>
Acked-by: Gerd Hoffmann <[email protected]>
  • Loading branch information
elmarco committed Dec 21, 2021
1 parent 4085b87 commit 3e301c8
Show file tree
Hide file tree
Showing 8 changed files with 476 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/chardev/char-socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ typedef enum {
TCP_CHARDEV_STATE_CONNECTED,
} TCPChardevState;

typedef ChardevClass SocketChardevClass;

struct SocketChardev {
Chardev parent;
QIOChannel *ioc; /* Client I/O channel */
Expand Down
5 changes: 5 additions & 0 deletions include/qemu/dbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

#include <gio/gio.h>

#include "qom/object.h"
#include "chardev/char.h"
#include "qemu/notify.h"
#include "qemu/typedefs.h"

/* glib/gio 2.68 */
#define DBUS_METHOD_INVOCATION_HANDLED TRUE
#define DBUS_METHOD_INVOCATION_UNHANDLED FALSE
Expand Down
27 changes: 27 additions & 0 deletions qapi/char.json
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,20 @@
'base': 'ChardevCommon',
'if': 'CONFIG_SPICE' }

##
# @ChardevDBus:
#
# Configuration info for DBus chardevs.
#
# @name: name of the channel (following docs/spice-port-fqdn.txt)
#
# Since: 7.0
##
{ 'struct': 'ChardevDBus',
'data': { 'name': 'str' },
'base': 'ChardevCommon',
'if': 'CONFIG_DBUS_DISPLAY' }

##
# @ChardevVC:
#
Expand Down Expand Up @@ -422,6 +436,7 @@
# @spicevmc: Since 1.5
# @spiceport: Since 1.5
# @qemu-vdagent: Since 6.1
# @dbus: Since 7.0
# @vc: v1.5
# @ringbuf: Since 1.6
# @memory: Since 1.5
Expand All @@ -447,6 +462,7 @@
{ 'name': 'spicevmc', 'if': 'CONFIG_SPICE' },
{ 'name': 'spiceport', 'if': 'CONFIG_SPICE' },
{ 'name': 'qemu-vdagent', 'if': 'CONFIG_SPICE_PROTOCOL' },
{ 'name': 'dbus', 'if': 'CONFIG_DBUS_DISPLAY' },
'vc',
'ringbuf',
# next one is just for compatibility
Expand Down Expand Up @@ -535,6 +551,15 @@
'data': { 'data': 'ChardevQemuVDAgent' },
'if': 'CONFIG_SPICE_PROTOCOL' }

##
# @ChardevDBusWrapper:
#
# Since: 7.0
##
{ 'struct': 'ChardevDBusWrapper',
'data': { 'data': 'ChardevDBus' },
'if': 'CONFIG_DBUS_DISPLAY' }

##
# @ChardevVCWrapper:
#
Expand Down Expand Up @@ -582,6 +607,8 @@
'if': 'CONFIG_SPICE' },
'qemu-vdagent': { 'type': 'ChardevQemuVDAgentWrapper',
'if': 'CONFIG_SPICE_PROTOCOL' },
'dbus': { 'type': 'ChardevDBusWrapper',
'if': 'CONFIG_DBUS_DISPLAY' },
'vc': 'ChardevVCWrapper',
'ringbuf': 'ChardevRingbufWrapper',
# next one is just for compatibility
Expand Down
296 changes: 296 additions & 0 deletions ui/dbus-chardev.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
/*
* QEMU DBus display
*
* Copyright (c) 2021 Marc-André Lureau <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "qemu/osdep.h"
#include "trace.h"
#include "qapi/error.h"
#include "qemu/config-file.h"
#include "qemu/option.h"

#include <gio/gunixfdlist.h>

#include "dbus.h"

static char *
dbus_display_chardev_path(DBusChardev *chr)
{
return g_strdup_printf(DBUS_DISPLAY1_ROOT "/Chardev_%s",
CHARDEV(chr)->label);
}

static void
dbus_display_chardev_export(DBusDisplay *dpy, DBusChardev *chr)
{
g_autoptr(GDBusObjectSkeleton) sk = NULL;
g_autofree char *path = dbus_display_chardev_path(chr);

if (chr->exported) {
return;
}

sk = g_dbus_object_skeleton_new(path);
g_dbus_object_skeleton_add_interface(
sk, G_DBUS_INTERFACE_SKELETON(chr->iface));
g_dbus_object_manager_server_export(dpy->server, sk);
chr->exported = true;
}

static void
dbus_display_chardev_unexport(DBusDisplay *dpy, DBusChardev *chr)
{
g_autofree char *path = dbus_display_chardev_path(chr);

if (!chr->exported) {
return;
}

g_dbus_object_manager_server_unexport(dpy->server, path);
chr->exported = false;
}

static int
dbus_display_chardev_foreach(Object *obj, void *data)
{
DBusDisplay *dpy = DBUS_DISPLAY(data);

if (!CHARDEV_IS_DBUS(obj)) {
return 0;
}

dbus_display_chardev_export(dpy, DBUS_CHARDEV(obj));

return 0;
}

static void
dbus_display_on_notify(Notifier *notifier, void *data)
{
DBusDisplay *dpy = container_of(notifier, DBusDisplay, notifier);
DBusDisplayEvent *event = data;

switch (event->type) {
case DBUS_DISPLAY_CHARDEV_OPEN:
dbus_display_chardev_export(dpy, event->chardev);
break;
case DBUS_DISPLAY_CHARDEV_CLOSE:
dbus_display_chardev_unexport(dpy, event->chardev);
break;
}
}

void
dbus_chardev_init(DBusDisplay *dpy)
{
dpy->notifier.notify = dbus_display_on_notify;
dbus_display_notifier_add(&dpy->notifier);

object_child_foreach(container_get(object_get_root(), "/chardevs"),
dbus_display_chardev_foreach, dpy);
}

static gboolean
dbus_chr_register(
DBusChardev *dc,
GDBusMethodInvocation *invocation,
GUnixFDList *fd_list,
GVariant *arg_stream,
QemuDBusDisplay1Chardev *object)
{
g_autoptr(GError) err = NULL;
int fd;

fd = g_unix_fd_list_get(fd_list, g_variant_get_handle(arg_stream), &err);
if (err) {
g_dbus_method_invocation_return_error(
invocation,
DBUS_DISPLAY_ERROR,
DBUS_DISPLAY_ERROR_FAILED,
"Couldn't get peer FD: %s", err->message);
return DBUS_METHOD_INVOCATION_HANDLED;
}

if (qemu_chr_add_client(CHARDEV(dc), fd) < 0) {
g_dbus_method_invocation_return_error(invocation,
DBUS_DISPLAY_ERROR,
DBUS_DISPLAY_ERROR_FAILED,
"Couldn't register FD!");
close(fd);
return DBUS_METHOD_INVOCATION_HANDLED;
}

g_object_set(dc->iface,
"owner", g_dbus_method_invocation_get_sender(invocation),
NULL);

qemu_dbus_display1_chardev_complete_register(object, invocation, NULL);
return DBUS_METHOD_INVOCATION_HANDLED;
}

static gboolean
dbus_chr_send_break(
DBusChardev *dc,
GDBusMethodInvocation *invocation,
QemuDBusDisplay1Chardev *object)
{
qemu_chr_be_event(CHARDEV(dc), CHR_EVENT_BREAK);

qemu_dbus_display1_chardev_complete_send_break(object, invocation);
return DBUS_METHOD_INVOCATION_HANDLED;
}

static void
dbus_chr_open(Chardev *chr, ChardevBackend *backend,
bool *be_opened, Error **errp)
{
ERRP_GUARD();

DBusChardev *dc = DBUS_CHARDEV(chr);
DBusDisplayEvent event = {
.type = DBUS_DISPLAY_CHARDEV_OPEN,
.chardev = dc,
};
g_autoptr(ChardevBackend) be = NULL;
g_autoptr(QemuOpts) opts = NULL;

dc->iface = qemu_dbus_display1_chardev_skeleton_new();
g_object_set(dc->iface, "name", backend->u.dbus.data->name, NULL);
g_object_connect(dc->iface,
"swapped-signal::handle-register",
dbus_chr_register, dc,
"swapped-signal::handle-send-break",
dbus_chr_send_break, dc,
NULL);

dbus_display_notify(&event);

be = g_new0(ChardevBackend, 1);
opts = qemu_opts_create(qemu_find_opts("chardev"), NULL, 0, &error_abort);
qemu_opt_set(opts, "server", "on", &error_abort);
qemu_opt_set(opts, "wait", "off", &error_abort);
CHARDEV_CLASS(object_class_by_name(TYPE_CHARDEV_SOCKET))->parse(
opts, be, errp);
if (*errp) {
return;
}
CHARDEV_CLASS(object_class_by_name(TYPE_CHARDEV_SOCKET))->open(
chr, be, be_opened, errp);
}

static void
dbus_chr_set_fe_open(Chardev *chr, int fe_open)
{
DBusChardev *dc = DBUS_CHARDEV(chr);

g_object_set(dc->iface, "feopened", fe_open, NULL);
}

static void
dbus_chr_set_echo(Chardev *chr, bool echo)
{
DBusChardev *dc = DBUS_CHARDEV(chr);

g_object_set(dc->iface, "echo", echo, NULL);
}

static void
dbus_chr_be_event(Chardev *chr, QEMUChrEvent event)
{
DBusChardev *dc = DBUS_CHARDEV(chr);
DBusChardevClass *klass = DBUS_CHARDEV_GET_CLASS(chr);

switch (event) {
case CHR_EVENT_CLOSED:
if (dc->iface) {
/* on finalize, iface is set to NULL */
g_object_set(dc->iface, "owner", "", NULL);
}
break;
default:
break;
};

klass->parent_chr_be_event(chr, event);
}

static void
dbus_chr_parse(QemuOpts *opts, ChardevBackend *backend,
Error **errp)
{
const char *name = qemu_opt_get(opts, "name");
ChardevDBus *dbus;

if (name == NULL) {
error_setg(errp, "chardev: dbus: no name given");
return;
}

backend->type = CHARDEV_BACKEND_KIND_DBUS;
dbus = backend->u.dbus.data = g_new0(ChardevDBus, 1);
qemu_chr_parse_common(opts, qapi_ChardevDBus_base(dbus));
dbus->name = g_strdup(name);
}

static void
char_dbus_class_init(ObjectClass *oc, void *data)
{
DBusChardevClass *klass = DBUS_CHARDEV_CLASS(oc);
ChardevClass *cc = CHARDEV_CLASS(oc);

cc->parse = dbus_chr_parse;
cc->open = dbus_chr_open;
cc->chr_set_fe_open = dbus_chr_set_fe_open;
cc->chr_set_echo = dbus_chr_set_echo;
klass->parent_chr_be_event = cc->chr_be_event;
cc->chr_be_event = dbus_chr_be_event;
}

static void
char_dbus_finalize(Object *obj)
{
DBusChardev *dc = DBUS_CHARDEV(obj);
DBusDisplayEvent event = {
.type = DBUS_DISPLAY_CHARDEV_CLOSE,
.chardev = dc,
};

dbus_display_notify(&event);
g_clear_object(&dc->iface);
}

static const TypeInfo char_dbus_type_info = {
.name = TYPE_CHARDEV_DBUS,
.parent = TYPE_CHARDEV_SOCKET,
.class_size = sizeof(DBusChardevClass),
.instance_size = sizeof(DBusChardev),
.instance_finalize = char_dbus_finalize,
.class_init = char_dbus_class_init,
};
module_obj(TYPE_CHARDEV_DBUS);

static void
register_types(void)
{
type_register_static(&char_dbus_type_info);
}

type_init(register_types);
Loading

0 comments on commit 3e301c8

Please sign in to comment.