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-2018 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.
29 #include <sys/prctl.h>
30 #include <sys/socket.h>
40 #include <X11/Xatom.h>
44 A11Y_BUS_STATE_IDLE = 0,
45 A11Y_BUS_STATE_READING_ADDRESS,
46 A11Y_BUS_STATE_RUNNING,
52 gboolean launch_immediately;
53 gboolean a11y_enabled;
54 gboolean screen_reader_enabled;
55 GDBusConnection *session_bus;
56 GSettings *a11y_schema;
57 GSettings *interface_schema;
60 GDBusProxy *client_proxy;
63 /* -1 == error, 0 == pending, > 0 == running */
65 char *a11y_bus_address;
68 char *a11y_launch_error_message;
71 static A11yBusLauncher *_global_app = NULL;
73 static const gchar introspection_xml[] =
75 " <interface name='org.a11y.Bus'>"
76 " <method name='GetAddress'>"
77 " <arg type='s' name='address' direction='out'/>"
80 "<interface name='org.a11y.Status'>"
81 "<property name='IsEnabled' type='b' access='readwrite'/>"
82 "<property name='ScreenReaderEnabled' type='b' access='readwrite'/>"
85 static GDBusNodeInfo *introspection_data = NULL;
88 respond_to_end_session (GDBusProxy *proxy)
92 parameters = g_variant_new ("(bs)", TRUE, "");
94 g_dbus_proxy_call (proxy,
95 "EndSessionResponse", parameters,
96 G_DBUS_CALL_FLAGS_NONE,
97 -1, NULL, NULL, NULL);
101 g_signal_cb (GDBusProxy *proxy,
104 GVariant *parameters,
107 A11yBusLauncher *app = user_data;
109 if (g_strcmp0 (signal_name, "QueryEndSession") == 0)
110 respond_to_end_session (proxy);
111 else if (g_strcmp0 (signal_name, "EndSession") == 0)
112 respond_to_end_session (proxy);
113 else if (g_strcmp0 (signal_name, "Stop") == 0)
114 g_main_loop_quit (app->loop);
118 client_proxy_ready_cb (GObject *source_object,
122 A11yBusLauncher *app = user_data;
123 GError *error = NULL;
125 app->client_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
129 g_warning ("Failed to get a client proxy: %s", error->message);
130 g_error_free (error);
135 g_signal_connect (app->client_proxy, "g-signal",
136 G_CALLBACK (g_signal_cb), app);
140 register_client (A11yBusLauncher *app)
142 GDBusProxyFlags flags;
143 GDBusProxy *sm_proxy;
146 const gchar *autostart_id;
147 gchar *client_startup_id;
148 GVariant *parameters;
152 flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
153 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS;
156 sm_proxy = g_dbus_proxy_new_sync (app->session_bus, flags, NULL,
157 "org.gnome.SessionManager",
158 "/org/gnome/SessionManager",
159 "org.gnome.SessionManager",
164 g_warning ("Failed to get session manager proxy: %s", error->message);
165 g_error_free (error);
170 app_id = "at-spi-bus-launcher";
171 autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
173 if (autostart_id != NULL)
175 client_startup_id = g_strdup (autostart_id);
176 g_unsetenv ("DESKTOP_AUTOSTART_ID");
180 client_startup_id = g_strdup ("");
183 parameters = g_variant_new ("(ss)", app_id, client_startup_id);
184 g_free (client_startup_id);
187 variant = g_dbus_proxy_call_sync (sm_proxy,
188 "RegisterClient", parameters,
189 G_DBUS_CALL_FLAGS_NONE,
192 g_object_unref (sm_proxy);
196 g_warning ("Failed to register client: %s", error->message);
197 g_error_free (error);
202 g_variant_get (variant, "(o)", &object_path);
203 g_variant_unref (variant);
205 flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
206 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION, flags, NULL,
207 "org.gnome.SessionManager", object_path,
208 "org.gnome.SessionManager.ClientPrivate",
209 NULL, client_proxy_ready_cb, app);
211 g_free (object_path);
215 name_appeared_handler (GDBusConnection *connection,
217 const gchar *name_owner,
220 A11yBusLauncher *app = user_data;
222 register_client (app);
226 * unix_read_all_fd_to_string:
228 * Read all data from a file descriptor to a C string buffer.
231 unix_read_all_fd_to_string (int fd,
237 while (max_bytes > 1 && (bytes_read = read (fd, buf, MIN (4096, max_bytes - 1))))
242 max_bytes -= bytes_read;
249 on_bus_exited (GPid pid,
253 A11yBusLauncher *app = data;
255 app->a11y_bus_pid = -1;
256 app->state = A11Y_BUS_STATE_ERROR;
257 if (app->a11y_launch_error_message == NULL)
259 if (WIFEXITED (status))
260 app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
261 else if (WIFSIGNALED (status))
262 app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
263 else if (WIFSTOPPED (status))
264 app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
266 g_main_loop_quit (app->loop);
271 setup_bus_child_daemon (gpointer data)
273 A11yBusLauncher *app = data;
276 close (app->pipefd[0]);
277 dup2 (app->pipefd[1], 3);
278 close (app->pipefd[1]);
280 /* On Linux, tell the bus process to exit if this process goes away */
282 prctl (PR_SET_PDEATHSIG, 15);
287 ensure_a11y_bus_daemon (A11yBusLauncher *app, char *config_path)
289 char *argv[] = { DBUS_DAEMON, config_path, "--nofork", "--print-address", "3", NULL };
292 GError *error = NULL;
294 if (pipe (app->pipefd) < 0)
295 g_error ("Failed to create pipe: %s", strerror (errno));
297 g_clear_pointer (&app->a11y_launch_error_message, g_free);
299 if (!g_spawn_async (NULL,
302 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
303 setup_bus_child_daemon,
308 app->a11y_bus_pid = -1;
309 app->a11y_launch_error_message = g_strdup (error->message);
310 g_clear_error (&error);
314 close (app->pipefd[1]);
317 g_child_watch_add (pid, on_bus_exited, app);
319 app->state = A11Y_BUS_STATE_READING_ADDRESS;
320 app->a11y_bus_pid = pid;
321 g_debug ("Launched a11y bus, child is %ld", (long) pid);
322 if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
324 app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
325 kill (app->a11y_bus_pid, SIGTERM);
328 close (app->pipefd[0]);
330 app->state = A11Y_BUS_STATE_RUNNING;
332 /* Trim the trailing newline */
333 app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
334 g_debug ("a11y bus address: %s", app->a11y_bus_address);
339 close (app->pipefd[0]);
340 close (app->pipefd[1]);
341 app->state = A11Y_BUS_STATE_ERROR;
347 ensure_a11y_bus_daemon (A11yBusLauncher *app, char *config_path)
355 setup_bus_child_broker (gpointer data)
357 A11yBusLauncher *app = data;
361 dup2 (app->listenfd, 3);
362 close (app->listenfd);
363 g_setenv("LISTEN_FDS", "1", TRUE);
365 pid_str = g_strdup_printf("%u", getpid());
366 g_setenv("LISTEN_PID", pid_str, TRUE);
369 /* Tell the bus process to exit if this process goes away */
370 prctl (PR_SET_PDEATHSIG, SIGTERM);
374 ensure_a11y_bus_broker (A11yBusLauncher *app, char *config_path)
376 char *argv[] = { DBUS_BROKER, config_path, "--scope", "user", NULL };
377 struct sockaddr_un addr = { .sun_family = AF_UNIX };
378 socklen_t addr_len = sizeof(addr);
380 GError *error = NULL;
382 if ((app->listenfd = socket (PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0)
383 g_error ("Failed to create listening socket: %s", strerror (errno));
385 if (bind (app->listenfd, (struct sockaddr *)&addr, sizeof(sa_family_t)) < 0)
386 g_error ("Failed to bind listening socket: %s", strerror (errno));
388 if (getsockname (app->listenfd, (struct sockaddr *)&addr, &addr_len) < 0)
389 g_error ("Failed to get socket name for listening socket: %s", strerror(errno));
391 if (listen (app->listenfd, 1024) < 0)
392 g_error ("Failed to listen on socket: %s", strerror(errno));
394 g_clear_pointer (&app->a11y_launch_error_message, g_free);
396 if (!g_spawn_async (NULL,
399 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
400 setup_bus_child_broker,
405 app->a11y_bus_pid = -1;
406 app->a11y_launch_error_message = g_strdup (error->message);
407 g_clear_error (&error);
411 close (app->listenfd);
414 g_child_watch_add (pid, on_bus_exited, app);
415 app->a11y_bus_pid = pid;
416 g_debug ("Launched a11y bus, child is %ld", (long) pid);
417 app->state = A11Y_BUS_STATE_RUNNING;
419 app->a11y_bus_address = g_strconcat("unix:abstract=", addr.sun_path + 1, NULL);
420 g_debug ("a11y bus address: %s", app->a11y_bus_address);
425 close (app->listenfd);
426 app->state = A11Y_BUS_STATE_ERROR;
432 ensure_a11y_bus_broker (A11yBusLauncher *app, char *config_path)
439 ensure_a11y_bus (A11yBusLauncher *app)
441 char *config_path = NULL;
442 gboolean success = FALSE;
444 if (app->a11y_bus_pid != 0)
447 if (g_file_test (SYSCONFDIR"/at-spi2/accessibility.conf", G_FILE_TEST_EXISTS))
448 config_path = "--config-file="SYSCONFDIR"/at-spi2/accessibility.conf";
450 config_path = "--config-file="DATADIR"/defaults/at-spi2/accessibility.conf";
452 #ifdef WANT_DBUS_BROKER
453 success = ensure_a11y_bus_broker (app, config_path);
456 if (!ensure_a11y_bus_daemon (app, config_path))
460 success = ensure_a11y_bus_daemon (app, config_path);
463 if (!ensure_a11y_bus_broker (app, config_path))
470 Display *display = XOpenDisplay (NULL);
473 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
474 XChangeProperty (display,
475 XDefaultRootWindow (display),
477 XA_STRING, 8, PropModeReplace,
478 (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
480 XCloseDisplay (display);
489 handle_method_call (GDBusConnection *connection,
491 const gchar *object_path,
492 const gchar *interface_name,
493 const gchar *method_name,
494 GVariant *parameters,
495 GDBusMethodInvocation *invocation,
498 A11yBusLauncher *app = user_data;
500 if (g_strcmp0 (method_name, "GetAddress") == 0)
502 ensure_a11y_bus (app);
503 if (app->a11y_bus_pid > 0)
504 g_dbus_method_invocation_return_value (invocation,
505 g_variant_new ("(s)", app->a11y_bus_address));
507 g_dbus_method_invocation_return_dbus_error (invocation,
508 "org.a11y.Bus.Error",
509 app->a11y_launch_error_message);
514 handle_get_property (GDBusConnection *connection,
516 const gchar *object_path,
517 const gchar *interface_name,
518 const gchar *property_name,
522 A11yBusLauncher *app = user_data;
524 if (g_strcmp0 (property_name, "IsEnabled") == 0)
525 return g_variant_new ("b", app->a11y_enabled);
526 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
527 return g_variant_new ("b", app->screen_reader_enabled);
533 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
534 gboolean notify_gsettings)
536 GVariantBuilder builder;
537 GVariantBuilder invalidated_builder;
539 if (enabled == app->a11y_enabled)
542 app->a11y_enabled = enabled;
544 if (notify_gsettings && app->interface_schema)
546 g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
551 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
552 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
553 g_variant_builder_add (&builder, "{sv}", "IsEnabled",
554 g_variant_new_boolean (enabled));
556 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
557 "org.freedesktop.DBus.Properties",
559 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
561 &invalidated_builder),
564 g_variant_builder_clear (&builder);
565 g_variant_builder_clear (&invalidated_builder);
569 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
570 gboolean notify_gsettings)
572 GVariantBuilder builder;
573 GVariantBuilder invalidated_builder;
575 if (enabled == app->screen_reader_enabled)
578 /* If the screen reader is being enabled, we should enable accessibility
579 * if it isn't enabled already */
581 handle_a11y_enabled_change (app, enabled, notify_gsettings);
583 app->screen_reader_enabled = enabled;
585 if (notify_gsettings && app->a11y_schema)
587 g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
592 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
593 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
594 g_variant_builder_add (&builder, "{sv}", "ScreenReaderEnabled",
595 g_variant_new_boolean (enabled));
597 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
598 "org.freedesktop.DBus.Properties",
600 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
602 &invalidated_builder),
605 g_variant_builder_clear (&builder);
606 g_variant_builder_clear (&invalidated_builder);
610 handle_set_property (GDBusConnection *connection,
612 const gchar *object_path,
613 const gchar *interface_name,
614 const gchar *property_name,
619 A11yBusLauncher *app = user_data;
620 const gchar *type = g_variant_get_type_string (value);
623 if (g_strcmp0 (type, "b") != 0)
625 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
626 "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
630 enabled = g_variant_get_boolean (value);
632 if (g_strcmp0 (property_name, "IsEnabled") == 0)
634 handle_a11y_enabled_change (app, enabled, TRUE);
637 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
639 handle_screen_reader_enabled_change (app, enabled, TRUE);
644 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
645 "Unknown property '%s'", property_name);
650 static const GDBusInterfaceVTable bus_vtable =
653 NULL, /* handle_get_property, */
654 NULL /* handle_set_property */
657 static const GDBusInterfaceVTable status_vtable =
659 NULL, /* handle_method_call */
665 on_bus_acquired (GDBusConnection *connection,
669 A11yBusLauncher *app = user_data;
671 guint registration_id;
673 if (connection == NULL)
675 g_main_loop_quit (app->loop);
678 app->session_bus = connection;
680 if (app->launch_immediately)
682 ensure_a11y_bus (app);
683 if (app->state == A11Y_BUS_STATE_ERROR)
685 g_main_loop_quit (app->loop);
691 registration_id = g_dbus_connection_register_object (connection,
693 introspection_data->interfaces[0],
698 if (registration_id == 0)
700 g_error ("%s", error->message);
701 g_clear_error (&error);
704 g_dbus_connection_register_object (connection,
706 introspection_data->interfaces[1],
714 on_name_lost (GDBusConnection *connection,
718 A11yBusLauncher *app = user_data;
719 if (app->session_bus == NULL
720 && connection == NULL
721 && app->a11y_launch_error_message == NULL)
722 app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
723 g_main_loop_quit (app->loop);
727 on_name_acquired (GDBusConnection *connection,
731 g_bus_watch_name (G_BUS_TYPE_SESSION,
732 "org.gnome.SessionManager",
733 G_BUS_NAME_WATCHER_FLAGS_NONE,
734 name_appeared_handler, NULL,
738 static int sigterm_pipefd[2];
741 sigterm_handler (int signum)
743 write (sigterm_pipefd[1], "X", 1);
747 on_sigterm_pipe (GIOChannel *channel,
748 GIOCondition condition,
751 A11yBusLauncher *app = data;
753 g_main_loop_quit (app->loop);
759 init_sigterm_handling (A11yBusLauncher *app)
761 GIOChannel *sigterm_channel;
763 if (pipe (sigterm_pipefd) < 0)
764 g_error ("Failed to create pipe: %s", strerror (errno));
765 signal (SIGTERM, sigterm_handler);
767 sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
768 g_io_add_watch (sigterm_channel,
769 G_IO_IN | G_IO_ERR | G_IO_HUP,
780 Display *bridge_display;
782 unsigned char *data = NULL;
783 unsigned long nitems;
784 unsigned long leftover;
785 gboolean result = FALSE;
787 bridge_display = XOpenDisplay (NULL);
791 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
792 XGetWindowProperty (bridge_display,
793 XDefaultRootWindow (bridge_display),
795 (long) BUFSIZ, False,
796 (Atom) 31, &actual_type, &actual_format,
797 &nitems, &leftover, &data);
801 GDBusConnection *bus;
802 bus = g_dbus_connection_new_for_address_sync ((const gchar *)data, 0,
807 g_object_unref (bus);
811 XCloseDisplay (bridge_display);
819 get_schema (const gchar *name)
821 #if GLIB_CHECK_VERSION (2, 32, 0)
822 GSettingsSchemaSource *source = g_settings_schema_source_get_default ();
823 GSettingsSchema *schema = g_settings_schema_source_lookup (source, name, FALSE);
828 return g_settings_new_full (schema, NULL, NULL);
830 const char * const *schemas = NULL;
833 schemas = g_settings_list_schemas ();
834 for (i = 0; schemas[i]; i++)
836 if (!strcmp (schemas[i], name))
837 return g_settings_new (schemas[i]);
845 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
847 gboolean new_val = g_settings_get_boolean (gsettings, key);
849 if (!strcmp (key, "toolkit-accessibility"))
850 handle_a11y_enabled_change (_global_app, new_val, FALSE);
851 else if (!strcmp (key, "screen-reader-enabled"))
852 handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
859 gboolean a11y_set = FALSE;
860 gboolean screen_reader_set = FALSE;
863 if (already_running ())
866 _global_app = g_slice_new0 (A11yBusLauncher);
867 _global_app->loop = g_main_loop_new (NULL, FALSE);
869 for (i = 1; i < argc; i++)
871 if (!strcmp (argv[i], "--launch-immediately"))
872 _global_app->launch_immediately = TRUE;
873 else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 1)
875 else if (sscanf (argv[i], "--screen-reader=%d",
876 &_global_app->screen_reader_enabled) == 1)
877 screen_reader_set = TRUE;
879 g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
882 _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
883 _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
887 _global_app->a11y_enabled = _global_app->interface_schema
888 ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
889 : _global_app->launch_immediately;
892 if (!screen_reader_set)
894 _global_app->screen_reader_enabled = _global_app->a11y_schema
895 ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
899 if (_global_app->interface_schema)
900 g_signal_connect (_global_app->interface_schema,
901 "changed::toolkit-accessibility",
902 G_CALLBACK (gsettings_key_changed), _global_app);
904 if (_global_app->a11y_schema)
905 g_signal_connect (_global_app->a11y_schema,
906 "changed::screen-reader-enabled",
907 G_CALLBACK (gsettings_key_changed), _global_app);
909 init_sigterm_handling (_global_app);
911 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
912 g_assert (introspection_data != NULL);
914 _global_app->name_owner_id =
915 g_bus_own_name (G_BUS_TYPE_SESSION,
917 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
924 g_main_loop_run (_global_app->loop);
926 if (_global_app->a11y_bus_pid > 0)
927 kill (_global_app->a11y_bus_pid, SIGTERM);
929 /* Clear the X property if our bus is gone; in the case where e.g.
930 * GDM is launching a login on an X server it was using before,
931 * we don't want early login processes to pick up the stale address.
935 Display *display = XOpenDisplay (NULL);
938 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
939 XDeleteProperty (display,
940 XDefaultRootWindow (display),
944 XCloseDisplay (display);
949 if (_global_app->a11y_launch_error_message)
951 g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);