1 /* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
3 * at-spi-bus-launcher: Manage the a11y bus as a child process
5 * Copyright 2011 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
34 #include <X11/Xatom.h>
37 A11Y_BUS_STATE_IDLE = 0,
38 A11Y_BUS_STATE_READING_ADDRESS,
39 A11Y_BUS_STATE_RUNNING,
45 gboolean launch_immediately;
46 GDBusConnection *session_bus;
49 /* -1 == error, 0 == pending, > 0 == running */
51 char *a11y_bus_address;
53 char *a11y_launch_error_message;
56 static A11yBusLauncher *_global_app = NULL;
58 static const gchar introspection_xml[] =
60 " <interface name='org.a11y.Bus'>"
61 " <method name='GetAddress'>"
62 " <arg type='s' name='address' direction='out'/>"
65 "<interface name='org.a11y.Status'>"
66 "<property name='IsEnabled' type='b' access='read'/>"
69 static GDBusNodeInfo *introspection_data = NULL;
72 setup_bus_child (gpointer data)
74 A11yBusLauncher *app = data;
77 close (app->pipefd[0]);
78 dup2 (app->pipefd[1], 3);
79 close (app->pipefd[1]);
81 /* On Linux, tell the bus process to exit if this process goes away */
83 #include <sys/prctl.h>
84 prctl (PR_SET_PDEATHSIG, 15);
89 * unix_read_all_fd_to_string:
91 * Read all data from a file descriptor to a C string buffer.
94 unix_read_all_fd_to_string (int fd,
100 while (max_bytes > 1 && (bytes_read = read (fd, buf, MAX (4096, max_bytes - 1))))
105 max_bytes -= bytes_read;
112 on_bus_exited (GPid pid,
116 A11yBusLauncher *app = data;
118 app->a11y_bus_pid = -1;
119 app->state = A11Y_BUS_STATE_ERROR;
120 if (app->a11y_launch_error_message == NULL)
122 if (WIFEXITED (status))
123 app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
124 else if (WIFSIGNALED (status))
125 app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
126 else if (WIFSTOPPED (status))
127 app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
129 g_main_loop_quit (app->loop);
133 ensure_a11y_bus (A11yBusLauncher *app)
136 char *argv[] = { DBUS_DAEMON, NULL, "--nofork", "--print-address", "3", NULL };
138 GError *error = NULL;
140 if (app->a11y_bus_pid != 0)
143 argv[1] = g_strdup_printf ("--config-file=%s/at-spi2/accessibility.conf", SYSCONFDIR);
145 if (pipe (app->pipefd) < 0)
146 g_error ("Failed to create pipe: %s", strerror (errno));
148 if (!g_spawn_async (NULL,
151 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
157 app->a11y_bus_pid = -1;
158 app->a11y_launch_error_message = g_strdup (error->message);
159 g_clear_error (&error);
163 close (app->pipefd[1]);
166 g_child_watch_add (pid, on_bus_exited, app);
168 app->state = A11Y_BUS_STATE_READING_ADDRESS;
169 app->a11y_bus_pid = pid;
170 g_debug ("Launched a11y bus, child is %ld", (long) pid);
171 if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
173 app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
174 kill (app->a11y_bus_pid, SIGTERM);
177 close (app->pipefd[0]);
179 app->state = A11Y_BUS_STATE_RUNNING;
181 /* Trim the trailing newline */
182 app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
183 g_debug ("a11y bus address: %s", app->a11y_bus_address);
186 Display *display = XOpenDisplay (NULL);
189 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
190 XChangeProperty (display,
191 XDefaultRootWindow (display),
193 XA_STRING, 8, PropModeReplace,
194 (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
196 XCloseDisplay (display);
203 close (app->pipefd[0]);
204 close (app->pipefd[1]);
205 app->state = A11Y_BUS_STATE_ERROR;
209 handle_method_call (GDBusConnection *connection,
211 const gchar *object_path,
212 const gchar *interface_name,
213 const gchar *method_name,
214 GVariant *parameters,
215 GDBusMethodInvocation *invocation,
218 A11yBusLauncher *app = user_data;
220 if (g_strcmp0 (method_name, "GetAddress") == 0)
222 ensure_a11y_bus (app);
223 if (app->a11y_bus_pid > 0)
224 g_dbus_method_invocation_return_value (invocation,
225 g_variant_new ("(s)", app->a11y_bus_address));
227 g_dbus_method_invocation_return_dbus_error (invocation,
228 "org.a11y.Bus.Error",
229 app->a11y_launch_error_message);
234 handle_get_property (GDBusConnection *connection,
236 const gchar *object_path,
237 const gchar *interface_name,
238 const gchar *property_name,
242 A11yBusLauncher *app = user_data;
244 if (g_strcmp0 (property_name, "IsEnabled") == 0)
246 gboolean result = (app->a11y_bus_pid > 0);
247 return g_variant_new ("(b)", result);
253 static const GDBusInterfaceVTable bus_vtable =
256 NULL, /* handle_get_property, */
257 NULL /* handle_set_property */
260 static const GDBusInterfaceVTable status_vtable =
262 NULL, /* handle_method_call */
264 NULL /* handle_set_property */
268 on_bus_acquired (GDBusConnection *connection,
272 A11yBusLauncher *app = user_data;
274 guint registration_id;
276 if (connection == NULL)
278 g_main_loop_quit (app->loop);
281 app->session_bus = connection;
283 if (app->launch_immediately)
285 ensure_a11y_bus (app);
286 if (app->state == A11Y_BUS_STATE_ERROR)
288 g_main_loop_quit (app->loop);
294 registration_id = g_dbus_connection_register_object (connection,
296 introspection_data->interfaces[0],
301 if (registration_id == 0)
302 g_error ("%s", error->message);
304 g_dbus_connection_register_object (connection,
306 introspection_data->interfaces[1],
314 on_name_lost (GDBusConnection *connection,
318 A11yBusLauncher *app = user_data;
319 if (app->session_bus == NULL
320 && connection == NULL
321 && app->a11y_launch_error_message == NULL)
322 app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
323 g_main_loop_quit (app->loop);
327 on_name_acquired (GDBusConnection *connection,
331 A11yBusLauncher *app = user_data;
335 static int sigterm_pipefd[2];
338 sigterm_handler (int signum)
340 write (sigterm_pipefd[1], "X", 1);
344 on_sigterm_pipe (GIOChannel *channel,
345 GIOCondition condition,
348 A11yBusLauncher *app = data;
350 g_main_loop_quit (app->loop);
356 init_sigterm_handling (A11yBusLauncher *app)
358 GIOChannel *sigterm_channel;
360 if (pipe (sigterm_pipefd) < 0)
361 g_error ("Failed to create pipe: %s", strerror (errno));
362 signal (SIGTERM, sigterm_handler);
364 sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
365 g_io_add_watch (sigterm_channel,
366 G_IO_IN | G_IO_ERR | G_IO_HUP,
376 Display *bridge_display;
378 unsigned char *data = NULL;
379 unsigned long nitems;
380 unsigned long leftover;
381 gboolean result = FALSE;
383 bridge_display = XOpenDisplay (NULL);
387 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
388 XGetWindowProperty (bridge_display,
389 XDefaultRootWindow (bridge_display),
391 (long) BUFSIZ, False,
392 (Atom) 31, &actual_type, &actual_format,
393 &nitems, &leftover, &data);
397 GDBusConnection *bus;
398 GError *error = NULL;
399 const gchar *old_session = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
400 /* TODO: Is there a better way to connect? This is really hacky */
401 g_setenv ("DBUS_SESSION_BUS_ADDRESS", data, TRUE);
402 bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
403 g_setenv ("DBUS_SESSION_BUS_ADDRESS", old_session, TRUE);
406 g_object_unref (bus);
409 XCloseDisplay (bridge_display);
418 GError *error = NULL;
420 GDBusConnection *session_bus;
425 if (already_running ())
428 _global_app = g_slice_new0 (A11yBusLauncher);
429 _global_app->loop = g_main_loop_new (NULL, FALSE);
430 _global_app->launch_immediately = (argc == 2 && strcmp (argv[1], "--launch-immediately") == 0);
432 init_sigterm_handling (_global_app);
434 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
435 g_assert (introspection_data != NULL);
437 name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
439 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
446 g_main_loop_run (_global_app->loop);
448 if (_global_app->a11y_bus_pid > 0)
449 kill (_global_app->a11y_bus_pid, SIGTERM);
451 /* Clear the X property if our bus is gone; in the case where e.g.
452 * GDM is launching a login on an X server it was using before,
453 * we don't want early login processes to pick up the stale address.
456 Display *display = XOpenDisplay (NULL);
459 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
460 XDeleteProperty (display,
461 XDefaultRootWindow (display),
465 XCloseDisplay (display);
469 if (_global_app->a11y_launch_error_message)
471 g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);