master
c 322 lines 8.83 KB
Raw
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 "trace.h"
26 #include "qapi/error.h"
27 #include "qemu/config-file.h"
28 #include "qemu/option.h"
29
30 #ifdef G_OS_UNIX
31 #include <gio/gunixfdlist.h>
32 #endif
33
34 #include "dbus.h"
35
36 static char *
37 dbus_display_chardev_path(DBusChardev *chr)
38 {
39 return g_strdup_printf(DBUS_DISPLAY1_ROOT "/Chardev_%s",
40 CHARDEV(chr)->label);
41 }
42
43 static void
44 dbus_display_chardev_export(DBusDisplay *dpy, DBusChardev *chr)
45 {
46 g_autoptr(GDBusObjectSkeleton) sk = NULL;
47 g_autofree char *path = dbus_display_chardev_path(chr);
48
49 if (chr->exported) {
50 return;
51 }
52
53 sk = g_dbus_object_skeleton_new(path);
54 g_dbus_object_skeleton_add_interface(
55 sk, G_DBUS_INTERFACE_SKELETON(chr->iface));
56 if (chr->iface_vc_encoding) {
57 const char *interfaces[] = {
58 "org.qemu.Display1.Chardev.VCEncoding",
59 NULL
60 };
61 g_dbus_object_skeleton_add_interface(
62 sk, G_DBUS_INTERFACE_SKELETON(chr->iface_vc_encoding));
63 g_object_set(chr->iface, "interfaces", interfaces, NULL);
64 }
65 g_dbus_object_manager_server_export(dpy->server, sk);
66 chr->exported = true;
67 }
68
69 static void
70 dbus_display_chardev_unexport(DBusDisplay *dpy, DBusChardev *chr)
71 {
72 g_autofree char *path = dbus_display_chardev_path(chr);
73
74 if (!chr->exported) {
75 return;
76 }
77
78 g_dbus_object_manager_server_unexport(dpy->server, path);
79 chr->exported = false;
80 }
81
82 static int
83 dbus_display_chardev_foreach(Object *obj, void *data)
84 {
85 DBusDisplay *dpy = DBUS_DISPLAY(data);
86
87 if (!CHARDEV_IS_DBUS(obj)) {
88 return 0;
89 }
90
91 dbus_display_chardev_export(dpy, DBUS_CHARDEV(obj));
92
93 return 0;
94 }
95
96 static void
97 dbus_display_on_notify(Notifier *notifier, void *data)
98 {
99 DBusDisplay *dpy = container_of(notifier, DBusDisplay, notifier);
100 DBusDisplayEvent *event = data;
101
102 switch (event->type) {
103 case DBUS_DISPLAY_CHARDEV_OPEN:
104 dbus_display_chardev_export(dpy, event->chardev);
105 break;
106 case DBUS_DISPLAY_CHARDEV_CLOSE:
107 dbus_display_chardev_unexport(dpy, event->chardev);
108 break;
109 }
110 }
111
112 void
113 dbus_chardev_init(DBusDisplay *dpy)
114 {
115 dpy->notifier.notify = dbus_display_on_notify;
116 dbus_display_notifier_add(&dpy->notifier);
117
118 object_child_foreach(object_get_container("chardevs"),
119 dbus_display_chardev_foreach, dpy);
120 }
121
122 static gboolean
123 dbus_chr_register(
124 DBusChardev *dc,
125 GDBusMethodInvocation *invocation,
126 #ifdef G_OS_UNIX
127 GUnixFDList *fd_list,
128 #endif
129 GVariant *arg_stream,
130 QemuDBusDisplay1Chardev *object)
131 {
132 g_autoptr(GError) err = NULL;
133 int fd;
134
135 #ifdef G_OS_WIN32
136 if (!dbus_win32_import_socket(invocation, arg_stream, &fd)) {
137 return DBUS_METHOD_INVOCATION_HANDLED;
138 }
139 #else
140 fd = g_unix_fd_list_get(fd_list, g_variant_get_handle(arg_stream), &err);
141 if (err) {
142 g_dbus_method_invocation_return_error(
143 invocation,
144 DBUS_DISPLAY_ERROR,
145 DBUS_DISPLAY_ERROR_FAILED,
146 "Couldn't get peer FD: %s", err->message);
147 return DBUS_METHOD_INVOCATION_HANDLED;
148 }
149 #endif
150
151 if (qemu_chr_add_client(CHARDEV(dc), fd) < 0) {
152 g_dbus_method_invocation_return_error(invocation,
153 DBUS_DISPLAY_ERROR,
154 DBUS_DISPLAY_ERROR_FAILED,
155 "Couldn't register FD!");
156 #ifdef G_OS_WIN32
157 closesocket(fd);
158 #else
159 close(fd);
160 #endif
161 return DBUS_METHOD_INVOCATION_HANDLED;
162 }
163
164 g_object_set(dc->iface,
165 "owner", g_dbus_method_invocation_get_sender(invocation),
166 NULL);
167
168 qemu_dbus_display1_chardev_complete_register(object, invocation
169 #ifndef G_OS_WIN32
170 , NULL
171 #endif
172 );
173 return DBUS_METHOD_INVOCATION_HANDLED;
174 }
175
176 static gboolean
177 dbus_chr_send_break(
178 DBusChardev *dc,
179 GDBusMethodInvocation *invocation,
180 QemuDBusDisplay1Chardev *object)
181 {
182 qemu_chr_be_event(CHARDEV(dc), CHR_EVENT_BREAK);
183
184 qemu_dbus_display1_chardev_complete_send_break(object, invocation);
185 return DBUS_METHOD_INVOCATION_HANDLED;
186 }
187
188 static bool dbus_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp)
189 {
190 ERRP_GUARD();
191
192 DBusChardev *dc = DBUS_CHARDEV(chr);
193 DBusDisplayEvent event = {
194 .type = DBUS_DISPLAY_CHARDEV_OPEN,
195 .chardev = dc,
196 };
197 g_autoptr(ChardevBackend) be = NULL;
198 g_autoptr(QemuOpts) opts = NULL;
199
200 dc->iface = qemu_dbus_display1_chardev_skeleton_new();
201 g_object_set(dc->iface, "name", backend->u.dbus.data->name, NULL);
202 g_object_connect(dc->iface,
203 "swapped-signal::handle-register",
204 dbus_chr_register, dc,
205 "swapped-signal::handle-send-break",
206 dbus_chr_send_break, dc,
207 NULL);
208
209 dbus_display_notify(&event);
210
211 be = g_new0(ChardevBackend, 1);
212 opts = qemu_opts_create(qemu_find_opts("chardev"), NULL, 0, &error_abort);
213 qemu_opt_set(opts, "server", "on", &error_abort);
214 qemu_opt_set(opts, "wait", "off", &error_abort);
215 CHARDEV_CLASS(object_class_by_name(TYPE_CHARDEV_SOCKET))->chr_parse(
216 opts, be, errp);
217 if (*errp) {
218 return false;
219 }
220 return CHARDEV_CLASS(object_class_by_name(TYPE_CHARDEV_SOCKET))->chr_open(
221 chr, be, errp);
222 }
223
224 static void
225 dbus_chr_set_fe_open(Chardev *chr, int fe_open)
226 {
227 DBusChardev *dc = DBUS_CHARDEV(chr);
228
229 g_object_set(dc->iface, "feopened", fe_open, NULL);
230 }
231
232 static void
233 dbus_chr_set_echo(Chardev *chr, bool echo)
234 {
235 DBusChardev *dc = DBUS_CHARDEV(chr);
236
237 g_object_set(dc->iface, "echo", echo, NULL);
238 }
239
240 static void
241 dbus_chr_be_event(Chardev *chr, QEMUChrEvent event)
242 {
243 DBusChardev *dc = DBUS_CHARDEV(chr);
244 DBusChardevClass *klass = DBUS_CHARDEV_GET_CLASS(chr);
245
246 switch (event) {
247 case CHR_EVENT_CLOSED:
248 if (dc->iface) {
249 /* on finalize, iface is set to NULL */
250 g_object_set(dc->iface, "owner", "", NULL);
251 }
252 break;
253 default:
254 break;
255 };
256
257 klass->parent_chr_be_event(chr, event);
258 }
259
260 static void
261 dbus_chr_parse(QemuOpts *opts, ChardevBackend *backend,
262 Error **errp)
263 {
264 const char *name = qemu_opt_get(opts, "name");
265 ChardevDBus *dbus;
266
267 if (name == NULL) {
268 error_setg(errp, "chardev: dbus: no name given");
269 return;
270 }
271
272 backend->type = CHARDEV_BACKEND_KIND_DBUS;
273 dbus = backend->u.dbus.data = g_new0(ChardevDBus, 1);
274 qemu_chr_parse_common(opts, qapi_ChardevDBus_base(dbus));
275 dbus->name = g_strdup(name);
276 }
277
278 static void
279 char_dbus_class_init(ObjectClass *oc, const void *data)
280 {
281 DBusChardevClass *klass = DBUS_CHARDEV_CLASS(oc);
282 ChardevClass *cc = CHARDEV_CLASS(oc);
283
284 cc->chr_parse = dbus_chr_parse;
285 cc->chr_open = dbus_chr_open;
286 cc->chr_set_fe_open = dbus_chr_set_fe_open;
287 cc->chr_set_echo = dbus_chr_set_echo;
288 klass->parent_chr_be_event = cc->chr_be_event;
289 cc->chr_be_event = dbus_chr_be_event;
290 }
291
292 static void
293 char_dbus_finalize(Object *obj)
294 {
295 DBusChardev *dc = DBUS_CHARDEV(obj);
296 DBusDisplayEvent event = {
297 .type = DBUS_DISPLAY_CHARDEV_CLOSE,
298 .chardev = dc,
299 };
300
301 dbus_display_notify(&event);
302 g_clear_object(&dc->iface_vc_encoding);
303 g_clear_object(&dc->iface);
304 }
305
306 static const TypeInfo char_dbus_type_info = {
307 .name = TYPE_CHARDEV_DBUS,
308 .parent = TYPE_CHARDEV_SOCKET,
309 .class_size = sizeof(DBusChardevClass),
310 .instance_size = sizeof(DBusChardev),
311 .instance_finalize = char_dbus_finalize,
312 .class_init = char_dbus_class_init,
313 };
314 module_obj(TYPE_CHARDEV_DBUS);
315
316 static void
317 register_types(void)
318 {
319 type_register_static(&char_dbus_type_info);
320 }
321
322 type_init(register_types);