4 * Copyright (c) 2021 Marc-André Lureau <marcandre.lureau@redhat.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/config-file.h"
28 #include "qemu/option.h"
31 #include <gio/gunixfdlist.h>
37 dbus_display_chardev_path(DBusChardev
*chr
)
39 return g_strdup_printf(DBUS_DISPLAY1_ROOT
"/Chardev_%s",
44 dbus_display_chardev_export(DBusDisplay
*dpy
, DBusChardev
*chr
)
46 g_autoptr(GDBusObjectSkeleton
) sk
= NULL
;
47 g_autofree
char *path
= dbus_display_chardev_path(chr
);
53 sk
= g_dbus_object_skeleton_new(path
);
54 g_dbus_object_skeleton_add_interface(
55 sk
, G_DBUS_INTERFACE_SKELETON(chr
->iface
));
56 g_dbus_object_manager_server_export(dpy
->server
, sk
);
61 dbus_display_chardev_unexport(DBusDisplay
*dpy
, DBusChardev
*chr
)
63 g_autofree
char *path
= dbus_display_chardev_path(chr
);
69 g_dbus_object_manager_server_unexport(dpy
->server
, path
);
70 chr
->exported
= false;
74 dbus_display_chardev_foreach(Object
*obj
, void *data
)
76 DBusDisplay
*dpy
= DBUS_DISPLAY(data
);
78 if (!CHARDEV_IS_DBUS(obj
)) {
82 dbus_display_chardev_export(dpy
, DBUS_CHARDEV(obj
));
88 dbus_display_on_notify(Notifier
*notifier
, void *data
)
90 DBusDisplay
*dpy
= container_of(notifier
, DBusDisplay
, notifier
);
91 DBusDisplayEvent
*event
= data
;
93 switch (event
->type
) {
94 case DBUS_DISPLAY_CHARDEV_OPEN
:
95 dbus_display_chardev_export(dpy
, event
->chardev
);
97 case DBUS_DISPLAY_CHARDEV_CLOSE
:
98 dbus_display_chardev_unexport(dpy
, event
->chardev
);
104 dbus_chardev_init(DBusDisplay
*dpy
)
106 dpy
->notifier
.notify
= dbus_display_on_notify
;
107 dbus_display_notifier_add(&dpy
->notifier
);
109 object_child_foreach(container_get(object_get_root(), "/chardevs"),
110 dbus_display_chardev_foreach
, dpy
);
116 GDBusMethodInvocation
*invocation
,
118 GUnixFDList
*fd_list
,
120 GVariant
*arg_stream
,
121 QemuDBusDisplay1Chardev
*object
)
123 g_autoptr(GError
) err
= NULL
;
127 if (!dbus_win32_import_socket(invocation
, arg_stream
, &fd
)) {
128 return DBUS_METHOD_INVOCATION_HANDLED
;
131 fd
= g_unix_fd_list_get(fd_list
, g_variant_get_handle(arg_stream
), &err
);
133 g_dbus_method_invocation_return_error(
136 DBUS_DISPLAY_ERROR_FAILED
,
137 "Couldn't get peer FD: %s", err
->message
);
138 return DBUS_METHOD_INVOCATION_HANDLED
;
142 if (qemu_chr_add_client(CHARDEV(dc
), fd
) < 0) {
143 g_dbus_method_invocation_return_error(invocation
,
145 DBUS_DISPLAY_ERROR_FAILED
,
146 "Couldn't register FD!");
152 return DBUS_METHOD_INVOCATION_HANDLED
;
155 g_object_set(dc
->iface
,
156 "owner", g_dbus_method_invocation_get_sender(invocation
),
159 qemu_dbus_display1_chardev_complete_register(object
, invocation
164 return DBUS_METHOD_INVOCATION_HANDLED
;
170 GDBusMethodInvocation
*invocation
,
171 QemuDBusDisplay1Chardev
*object
)
173 qemu_chr_be_event(CHARDEV(dc
), CHR_EVENT_BREAK
);
175 qemu_dbus_display1_chardev_complete_send_break(object
, invocation
);
176 return DBUS_METHOD_INVOCATION_HANDLED
;
180 dbus_chr_open(Chardev
*chr
, ChardevBackend
*backend
,
181 bool *be_opened
, Error
**errp
)
185 DBusChardev
*dc
= DBUS_CHARDEV(chr
);
186 DBusDisplayEvent event
= {
187 .type
= DBUS_DISPLAY_CHARDEV_OPEN
,
190 g_autoptr(ChardevBackend
) be
= NULL
;
191 g_autoptr(QemuOpts
) opts
= NULL
;
193 dc
->iface
= qemu_dbus_display1_chardev_skeleton_new();
194 g_object_set(dc
->iface
, "name", backend
->u
.dbus
.data
->name
, NULL
);
195 g_object_connect(dc
->iface
,
196 "swapped-signal::handle-register",
197 dbus_chr_register
, dc
,
198 "swapped-signal::handle-send-break",
199 dbus_chr_send_break
, dc
,
202 dbus_display_notify(&event
);
204 be
= g_new0(ChardevBackend
, 1);
205 opts
= qemu_opts_create(qemu_find_opts("chardev"), NULL
, 0, &error_abort
);
206 qemu_opt_set(opts
, "server", "on", &error_abort
);
207 qemu_opt_set(opts
, "wait", "off", &error_abort
);
208 CHARDEV_CLASS(object_class_by_name(TYPE_CHARDEV_SOCKET
))->parse(
213 CHARDEV_CLASS(object_class_by_name(TYPE_CHARDEV_SOCKET
))->open(
214 chr
, be
, be_opened
, errp
);
218 dbus_chr_set_fe_open(Chardev
*chr
, int fe_open
)
220 DBusChardev
*dc
= DBUS_CHARDEV(chr
);
222 g_object_set(dc
->iface
, "feopened", fe_open
, NULL
);
226 dbus_chr_set_echo(Chardev
*chr
, bool echo
)
228 DBusChardev
*dc
= DBUS_CHARDEV(chr
);
230 g_object_set(dc
->iface
, "echo", echo
, NULL
);
234 dbus_chr_be_event(Chardev
*chr
, QEMUChrEvent event
)
236 DBusChardev
*dc
= DBUS_CHARDEV(chr
);
237 DBusChardevClass
*klass
= DBUS_CHARDEV_GET_CLASS(chr
);
240 case CHR_EVENT_CLOSED
:
242 /* on finalize, iface is set to NULL */
243 g_object_set(dc
->iface
, "owner", "", NULL
);
250 klass
->parent_chr_be_event(chr
, event
);
254 dbus_chr_parse(QemuOpts
*opts
, ChardevBackend
*backend
,
257 const char *name
= qemu_opt_get(opts
, "name");
261 error_setg(errp
, "chardev: dbus: no name given");
265 backend
->type
= CHARDEV_BACKEND_KIND_DBUS
;
266 dbus
= backend
->u
.dbus
.data
= g_new0(ChardevDBus
, 1);
267 qemu_chr_parse_common(opts
, qapi_ChardevDBus_base(dbus
));
268 dbus
->name
= g_strdup(name
);
272 char_dbus_class_init(ObjectClass
*oc
, void *data
)
274 DBusChardevClass
*klass
= DBUS_CHARDEV_CLASS(oc
);
275 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
277 cc
->parse
= dbus_chr_parse
;
278 cc
->open
= dbus_chr_open
;
279 cc
->chr_set_fe_open
= dbus_chr_set_fe_open
;
280 cc
->chr_set_echo
= dbus_chr_set_echo
;
281 klass
->parent_chr_be_event
= cc
->chr_be_event
;
282 cc
->chr_be_event
= dbus_chr_be_event
;
286 char_dbus_finalize(Object
*obj
)
288 DBusChardev
*dc
= DBUS_CHARDEV(obj
);
289 DBusDisplayEvent event
= {
290 .type
= DBUS_DISPLAY_CHARDEV_CLOSE
,
294 dbus_display_notify(&event
);
295 g_clear_object(&dc
->iface
);
298 static const TypeInfo char_dbus_type_info
= {
299 .name
= TYPE_CHARDEV_DBUS
,
300 .parent
= TYPE_CHARDEV_SOCKET
,
301 .class_size
= sizeof(DBusChardevClass
),
302 .instance_size
= sizeof(DBusChardev
),
303 .instance_finalize
= char_dbus_finalize
,
304 .class_init
= char_dbus_class_init
,
306 module_obj(TYPE_CHARDEV_DBUS
);
311 type_register_static(&char_dbus_type_info
);
314 type_init(register_types
);