migration: Clean up local variable shadowing
[qemu/ar7.git] / ui / dbus-chardev.c
blob1d3a7122a114234ba88b5cea76e2eedab203ece9
1 /*
2 * QEMU DBus display
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
22 * THE SOFTWARE.
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"
30 #ifdef G_OS_UNIX
31 #include <gio/gunixfdlist.h>
32 #endif
34 #include "dbus.h"
36 static char *
37 dbus_display_chardev_path(DBusChardev *chr)
39 return g_strdup_printf(DBUS_DISPLAY1_ROOT "/Chardev_%s",
40 CHARDEV(chr)->label);
43 static void
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);
49 if (chr->exported) {
50 return;
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);
57 chr->exported = true;
60 static void
61 dbus_display_chardev_unexport(DBusDisplay *dpy, DBusChardev *chr)
63 g_autofree char *path = dbus_display_chardev_path(chr);
65 if (!chr->exported) {
66 return;
69 g_dbus_object_manager_server_unexport(dpy->server, path);
70 chr->exported = false;
73 static int
74 dbus_display_chardev_foreach(Object *obj, void *data)
76 DBusDisplay *dpy = DBUS_DISPLAY(data);
78 if (!CHARDEV_IS_DBUS(obj)) {
79 return 0;
82 dbus_display_chardev_export(dpy, DBUS_CHARDEV(obj));
84 return 0;
87 static void
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);
96 break;
97 case DBUS_DISPLAY_CHARDEV_CLOSE:
98 dbus_display_chardev_unexport(dpy, event->chardev);
99 break;
103 void
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);
113 static gboolean
114 dbus_chr_register(
115 DBusChardev *dc,
116 GDBusMethodInvocation *invocation,
117 #ifdef G_OS_UNIX
118 GUnixFDList *fd_list,
119 #endif
120 GVariant *arg_stream,
121 QemuDBusDisplay1Chardev *object)
123 g_autoptr(GError) err = NULL;
124 int fd;
126 #ifdef G_OS_WIN32
127 if (!dbus_win32_import_socket(invocation, arg_stream, &fd)) {
128 return DBUS_METHOD_INVOCATION_HANDLED;
130 #else
131 fd = g_unix_fd_list_get(fd_list, g_variant_get_handle(arg_stream), &err);
132 if (err) {
133 g_dbus_method_invocation_return_error(
134 invocation,
135 DBUS_DISPLAY_ERROR,
136 DBUS_DISPLAY_ERROR_FAILED,
137 "Couldn't get peer FD: %s", err->message);
138 return DBUS_METHOD_INVOCATION_HANDLED;
140 #endif
142 if (qemu_chr_add_client(CHARDEV(dc), fd) < 0) {
143 g_dbus_method_invocation_return_error(invocation,
144 DBUS_DISPLAY_ERROR,
145 DBUS_DISPLAY_ERROR_FAILED,
146 "Couldn't register FD!");
147 #ifdef G_OS_WIN32
148 closesocket(fd);
149 #else
150 close(fd);
151 #endif
152 return DBUS_METHOD_INVOCATION_HANDLED;
155 g_object_set(dc->iface,
156 "owner", g_dbus_method_invocation_get_sender(invocation),
157 NULL);
159 qemu_dbus_display1_chardev_complete_register(object, invocation
160 #ifndef G_OS_WIN32
161 , NULL
162 #endif
164 return DBUS_METHOD_INVOCATION_HANDLED;
167 static gboolean
168 dbus_chr_send_break(
169 DBusChardev *dc,
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;
179 static void
180 dbus_chr_open(Chardev *chr, ChardevBackend *backend,
181 bool *be_opened, Error **errp)
183 ERRP_GUARD();
185 DBusChardev *dc = DBUS_CHARDEV(chr);
186 DBusDisplayEvent event = {
187 .type = DBUS_DISPLAY_CHARDEV_OPEN,
188 .chardev = dc,
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,
200 NULL);
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(
209 opts, be, errp);
210 if (*errp) {
211 return;
213 CHARDEV_CLASS(object_class_by_name(TYPE_CHARDEV_SOCKET))->open(
214 chr, be, be_opened, errp);
217 static void
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);
225 static void
226 dbus_chr_set_echo(Chardev *chr, bool echo)
228 DBusChardev *dc = DBUS_CHARDEV(chr);
230 g_object_set(dc->iface, "echo", echo, NULL);
233 static void
234 dbus_chr_be_event(Chardev *chr, QEMUChrEvent event)
236 DBusChardev *dc = DBUS_CHARDEV(chr);
237 DBusChardevClass *klass = DBUS_CHARDEV_GET_CLASS(chr);
239 switch (event) {
240 case CHR_EVENT_CLOSED:
241 if (dc->iface) {
242 /* on finalize, iface is set to NULL */
243 g_object_set(dc->iface, "owner", "", NULL);
245 break;
246 default:
247 break;
250 klass->parent_chr_be_event(chr, event);
253 static void
254 dbus_chr_parse(QemuOpts *opts, ChardevBackend *backend,
255 Error **errp)
257 const char *name = qemu_opt_get(opts, "name");
258 ChardevDBus *dbus;
260 if (name == NULL) {
261 error_setg(errp, "chardev: dbus: no name given");
262 return;
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);
271 static void
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;
285 static void
286 char_dbus_finalize(Object *obj)
288 DBusChardev *dc = DBUS_CHARDEV(obj);
289 DBusDisplayEvent event = {
290 .type = DBUS_DISPLAY_CHARDEV_CLOSE,
291 .chardev = dc,
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);
308 static void
309 register_types(void)
311 type_register_static(&char_dbus_type_info);
314 type_init(register_types);