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 Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 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 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, 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;
67 gboolean x11_prop_set;
71 char *a11y_launch_error_message;
74 static A11yBusLauncher *_global_app = NULL;
76 static const gchar introspection_xml[] =
78 " <interface name='org.a11y.Bus'>"
79 " <method name='GetAddress'>"
80 " <arg type='s' name='address' direction='out'/>"
83 "<interface name='org.a11y.Status'>"
84 "<property name='IsEnabled' type='b' access='readwrite'/>"
85 "<property name='ScreenReaderEnabled' type='b' access='readwrite'/>"
88 static GDBusNodeInfo *introspection_data = NULL;
91 respond_to_end_session (GDBusProxy *proxy)
95 parameters = g_variant_new ("(bs)", TRUE, "");
97 g_dbus_proxy_call (proxy,
98 "EndSessionResponse", parameters,
99 G_DBUS_CALL_FLAGS_NONE,
100 -1, NULL, NULL, NULL);
104 g_signal_cb (GDBusProxy *proxy,
107 GVariant *parameters,
110 A11yBusLauncher *app = user_data;
112 if (g_strcmp0 (signal_name, "QueryEndSession") == 0)
113 respond_to_end_session (proxy);
114 else if (g_strcmp0 (signal_name, "EndSession") == 0)
115 respond_to_end_session (proxy);
116 else if (g_strcmp0 (signal_name, "Stop") == 0)
117 g_main_loop_quit (app->loop);
121 client_proxy_ready_cb (GObject *source_object,
125 A11yBusLauncher *app = user_data;
126 GError *error = NULL;
128 app->client_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
132 g_warning ("Failed to get a client proxy: %s", error->message);
133 g_error_free (error);
138 g_signal_connect (app->client_proxy, "g-signal",
139 G_CALLBACK (g_signal_cb), app);
143 register_client (A11yBusLauncher *app)
145 GDBusProxyFlags flags;
146 GDBusProxy *sm_proxy;
149 const gchar *autostart_id;
150 gchar *client_startup_id;
151 GVariant *parameters;
155 flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
156 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS;
159 sm_proxy = g_dbus_proxy_new_sync (app->session_bus, flags, NULL,
160 "org.gnome.SessionManager",
161 "/org/gnome/SessionManager",
162 "org.gnome.SessionManager",
167 g_warning ("Failed to get session manager proxy: %s", error->message);
168 g_error_free (error);
173 app_id = "at-spi-bus-launcher";
174 autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
176 if (autostart_id != NULL)
178 client_startup_id = g_strdup (autostart_id);
179 g_unsetenv ("DESKTOP_AUTOSTART_ID");
183 client_startup_id = g_strdup ("");
186 parameters = g_variant_new ("(ss)", app_id, client_startup_id);
187 g_free (client_startup_id);
190 variant = g_dbus_proxy_call_sync (sm_proxy,
191 "RegisterClient", parameters,
192 G_DBUS_CALL_FLAGS_NONE,
195 g_object_unref (sm_proxy);
199 g_warning ("Failed to register client: %s", error->message);
200 g_error_free (error);
205 g_variant_get (variant, "(o)", &object_path);
206 g_variant_unref (variant);
208 flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
209 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION, flags, NULL,
210 "org.gnome.SessionManager", object_path,
211 "org.gnome.SessionManager.ClientPrivate",
212 NULL, client_proxy_ready_cb, app);
214 g_free (object_path);
218 name_appeared_handler (GDBusConnection *connection,
220 const gchar *name_owner,
223 A11yBusLauncher *app = user_data;
225 register_client (app);
229 * unix_read_all_fd_to_string:
231 * Read all data from a file descriptor to a C string buffer.
234 unix_read_all_fd_to_string (int fd,
240 while (max_bytes > 1 && (bytes_read = read (fd, buf, MIN (4096, max_bytes - 1))))
245 max_bytes -= bytes_read;
252 on_bus_exited (GPid pid,
256 A11yBusLauncher *app = data;
258 app->a11y_bus_pid = -1;
259 app->state = A11Y_BUS_STATE_ERROR;
260 if (app->a11y_launch_error_message == NULL)
262 if (WIFEXITED (status))
263 app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
264 else if (WIFSIGNALED (status))
265 app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
266 else if (WIFSTOPPED (status))
267 app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
269 g_main_loop_quit (app->loop);
274 setup_bus_child_daemon (gpointer data)
276 A11yBusLauncher *app = data;
279 close (app->pipefd[0]);
280 dup2 (app->pipefd[1], 3);
281 close (app->pipefd[1]);
283 /* On Linux, tell the bus process to exit if this process goes away */
285 prctl (PR_SET_PDEATHSIG, 15);
290 ensure_a11y_bus_daemon (A11yBusLauncher *app, char *config_path)
292 char *argv[] = { DBUS_DAEMON, config_path, "--nofork", "--print-address", "3", NULL };
295 GError *error = NULL;
297 if (pipe (app->pipefd) < 0)
298 g_error ("Failed to create pipe: %s", strerror (errno));
300 g_clear_pointer (&app->a11y_launch_error_message, g_free);
302 if (!g_spawn_async (NULL,
305 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
306 setup_bus_child_daemon,
311 app->a11y_bus_pid = -1;
312 app->a11y_launch_error_message = g_strdup (error->message);
313 g_clear_error (&error);
317 close (app->pipefd[1]);
320 g_child_watch_add (pid, on_bus_exited, app);
322 app->state = A11Y_BUS_STATE_READING_ADDRESS;
323 app->a11y_bus_pid = pid;
324 g_debug ("Launched a11y bus, child is %ld", (long) pid);
325 if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
327 app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
328 kill (app->a11y_bus_pid, SIGTERM);
329 app->a11y_bus_pid = -1;
332 close (app->pipefd[0]);
334 app->state = A11Y_BUS_STATE_RUNNING;
336 /* Trim the trailing newline */
337 app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
338 g_debug ("a11y bus address: %s", app->a11y_bus_address);
343 close (app->pipefd[0]);
344 close (app->pipefd[1]);
345 app->state = A11Y_BUS_STATE_ERROR;
351 ensure_a11y_bus_daemon (A11yBusLauncher *app, char *config_path)
359 setup_bus_child_broker (gpointer data)
361 A11yBusLauncher *app = data;
365 dup2 (app->listenfd, 3);
366 close (app->listenfd);
367 g_setenv("LISTEN_FDS", "1", TRUE);
369 pid_str = g_strdup_printf("%u", getpid());
370 g_setenv("LISTEN_PID", pid_str, TRUE);
373 /* Tell the bus process to exit if this process goes away */
374 prctl (PR_SET_PDEATHSIG, SIGTERM);
378 ensure_a11y_bus_broker (A11yBusLauncher *app, char *config_path)
380 char *argv[] = { DBUS_BROKER, config_path, "--scope", "user", NULL };
381 struct sockaddr_un addr = { .sun_family = AF_UNIX };
382 socklen_t addr_len = sizeof(addr);
384 GError *error = NULL;
386 if ((app->listenfd = socket (PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0)
387 g_error ("Failed to create listening socket: %s", strerror (errno));
389 if (bind (app->listenfd, (struct sockaddr *)&addr, sizeof(sa_family_t)) < 0)
390 g_error ("Failed to bind listening socket: %s", strerror (errno));
392 if (getsockname (app->listenfd, (struct sockaddr *)&addr, &addr_len) < 0)
393 g_error ("Failed to get socket name for listening socket: %s", strerror(errno));
395 if (listen (app->listenfd, 1024) < 0)
396 g_error ("Failed to listen on socket: %s", strerror(errno));
398 g_clear_pointer (&app->a11y_launch_error_message, g_free);
400 if (!g_spawn_async (NULL,
403 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
404 setup_bus_child_broker,
409 app->a11y_bus_pid = -1;
410 app->a11y_launch_error_message = g_strdup (error->message);
411 g_clear_error (&error);
415 close (app->listenfd);
418 g_child_watch_add (pid, on_bus_exited, app);
419 app->a11y_bus_pid = pid;
420 g_debug ("Launched a11y bus, child is %ld", (long) pid);
421 app->state = A11Y_BUS_STATE_RUNNING;
423 app->a11y_bus_address = g_strconcat("unix:abstract=", addr.sun_path + 1, NULL);
424 g_debug ("a11y bus address: %s", app->a11y_bus_address);
429 close (app->listenfd);
430 app->state = A11Y_BUS_STATE_ERROR;
436 ensure_a11y_bus_broker (A11yBusLauncher *app, char *config_path)
443 ensure_a11y_bus (A11yBusLauncher *app)
445 char *config_path = NULL;
446 gboolean success = FALSE;
448 if (app->a11y_bus_pid != 0)
451 if (g_file_test (SYSCONFDIR"/at-spi2/accessibility.conf", G_FILE_TEST_EXISTS))
452 config_path = "--config-file="SYSCONFDIR"/at-spi2/accessibility.conf";
454 config_path = "--config-file="DATADIR"/defaults/at-spi2/accessibility.conf";
456 #ifdef WANT_DBUS_BROKER
457 success = ensure_a11y_bus_broker (app, config_path);
460 if (!ensure_a11y_bus_daemon (app, config_path))
464 success = ensure_a11y_bus_daemon (app, config_path);
467 if (!ensure_a11y_bus_broker (app, config_path))
473 if (g_getenv ("DISPLAY") != NULL && g_getenv ("WAYLAND_DISPLAY") == NULL)
475 Display *display = XOpenDisplay (NULL);
478 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
479 XChangeProperty (display,
480 XDefaultRootWindow (display),
482 XA_STRING, 8, PropModeReplace,
483 (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
485 XCloseDisplay (display);
486 app->x11_prop_set = TRUE;
495 handle_method_call (GDBusConnection *connection,
497 const gchar *object_path,
498 const gchar *interface_name,
499 const gchar *method_name,
500 GVariant *parameters,
501 GDBusMethodInvocation *invocation,
504 A11yBusLauncher *app = user_data;
506 if (g_strcmp0 (method_name, "GetAddress") == 0)
508 ensure_a11y_bus (app);
509 if (app->a11y_bus_pid > 0)
510 g_dbus_method_invocation_return_value (invocation,
511 g_variant_new ("(s)", app->a11y_bus_address));
513 g_dbus_method_invocation_return_dbus_error (invocation,
514 "org.a11y.Bus.Error",
515 app->a11y_launch_error_message);
520 handle_get_property (GDBusConnection *connection,
522 const gchar *object_path,
523 const gchar *interface_name,
524 const gchar *property_name,
528 A11yBusLauncher *app = user_data;
530 if (g_strcmp0 (property_name, "IsEnabled") == 0)
531 return g_variant_new ("b", app->a11y_enabled);
532 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
533 return g_variant_new ("b", app->screen_reader_enabled);
539 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
540 gboolean notify_gsettings)
542 GVariantBuilder builder;
543 GVariantBuilder invalidated_builder;
545 if (enabled == app->a11y_enabled)
548 app->a11y_enabled = enabled;
550 if (notify_gsettings && app->interface_schema)
552 g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
557 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
558 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
559 g_variant_builder_add (&builder, "{sv}", "IsEnabled",
560 g_variant_new_boolean (enabled));
562 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
563 "org.freedesktop.DBus.Properties",
565 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
567 &invalidated_builder),
570 g_variant_builder_clear (&builder);
571 g_variant_builder_clear (&invalidated_builder);
575 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
576 gboolean notify_gsettings)
578 GVariantBuilder builder;
579 GVariantBuilder invalidated_builder;
581 if (enabled == app->screen_reader_enabled)
584 /* If the screen reader is being enabled, we should enable accessibility
585 * if it isn't enabled already */
587 handle_a11y_enabled_change (app, enabled, notify_gsettings);
589 app->screen_reader_enabled = enabled;
591 if (notify_gsettings && app->a11y_schema)
593 g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
598 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
599 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
600 g_variant_builder_add (&builder, "{sv}", "ScreenReaderEnabled",
601 g_variant_new_boolean (enabled));
603 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
604 "org.freedesktop.DBus.Properties",
606 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
608 &invalidated_builder),
611 g_variant_builder_clear (&builder);
612 g_variant_builder_clear (&invalidated_builder);
616 handle_set_property (GDBusConnection *connection,
618 const gchar *object_path,
619 const gchar *interface_name,
620 const gchar *property_name,
625 A11yBusLauncher *app = user_data;
626 const gchar *type = g_variant_get_type_string (value);
629 if (g_strcmp0 (type, "b") != 0)
631 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
632 "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
636 enabled = g_variant_get_boolean (value);
638 if (g_strcmp0 (property_name, "IsEnabled") == 0)
640 handle_a11y_enabled_change (app, enabled, TRUE);
643 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
645 handle_screen_reader_enabled_change (app, enabled, TRUE);
650 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
651 "Unknown property '%s'", property_name);
656 static const GDBusInterfaceVTable bus_vtable =
659 NULL, /* handle_get_property, */
660 NULL /* handle_set_property */
663 static const GDBusInterfaceVTable status_vtable =
665 NULL, /* handle_method_call */
671 on_bus_acquired (GDBusConnection *connection,
675 A11yBusLauncher *app = user_data;
677 guint registration_id;
679 if (connection == NULL)
681 g_main_loop_quit (app->loop);
684 app->session_bus = connection;
687 registration_id = g_dbus_connection_register_object (connection,
689 introspection_data->interfaces[0],
694 if (registration_id == 0)
696 g_error ("%s", error->message);
697 g_clear_error (&error);
700 g_dbus_connection_register_object (connection,
702 introspection_data->interfaces[1],
710 on_name_lost (GDBusConnection *connection,
714 A11yBusLauncher *app = user_data;
715 if (app->session_bus == NULL
716 && connection == NULL
717 && app->a11y_launch_error_message == NULL)
718 app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
719 g_main_loop_quit (app->loop);
723 on_name_acquired (GDBusConnection *connection,
727 A11yBusLauncher *app = user_data;
729 if (app->launch_immediately)
731 ensure_a11y_bus (app);
732 if (app->state == A11Y_BUS_STATE_ERROR)
734 g_main_loop_quit (app->loop);
739 g_bus_watch_name (G_BUS_TYPE_SESSION,
740 "org.gnome.SessionManager",
741 G_BUS_NAME_WATCHER_FLAGS_NONE,
742 name_appeared_handler, NULL,
746 static int sigterm_pipefd[2];
749 sigterm_handler (int signum)
751 write (sigterm_pipefd[1], "X", 1);
755 on_sigterm_pipe (GIOChannel *channel,
756 GIOCondition condition,
759 A11yBusLauncher *app = data;
761 g_main_loop_quit (app->loop);
767 init_sigterm_handling (A11yBusLauncher *app)
769 GIOChannel *sigterm_channel;
771 if (pipe (sigterm_pipefd) < 0)
772 g_error ("Failed to create pipe: %s", strerror (errno));
773 signal (SIGTERM, sigterm_handler);
775 sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
776 g_io_add_watch (sigterm_channel,
777 G_IO_IN | G_IO_ERR | G_IO_HUP,
783 get_schema (const gchar *name)
785 #if GLIB_CHECK_VERSION (2, 32, 0)
786 GSettingsSchemaSource *source = g_settings_schema_source_get_default ();
787 GSettingsSchema *schema = g_settings_schema_source_lookup (source, name, FALSE);
792 return g_settings_new_full (schema, NULL, NULL);
794 const char * const *schemas = NULL;
797 schemas = g_settings_list_schemas ();
798 for (i = 0; schemas[i]; i++)
800 if (!strcmp (schemas[i], name))
801 return g_settings_new (schemas[i]);
809 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
811 gboolean new_val = g_settings_get_boolean (gsettings, key);
813 if (!strcmp (key, "toolkit-accessibility"))
814 handle_a11y_enabled_change (_global_app, new_val, FALSE);
815 else if (!strcmp (key, "screen-reader-enabled"))
816 handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
823 gboolean a11y_set = FALSE;
824 gboolean screen_reader_set = FALSE;
827 _global_app = g_slice_new0 (A11yBusLauncher);
828 _global_app->loop = g_main_loop_new (NULL, FALSE);
830 for (i = 1; i < argc; i++)
832 if (!strcmp (argv[i], "--launch-immediately"))
833 _global_app->launch_immediately = TRUE;
834 else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 1)
836 else if (sscanf (argv[i], "--screen-reader=%d",
837 &_global_app->screen_reader_enabled) == 1)
838 screen_reader_set = TRUE;
840 g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
843 _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
844 _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
848 _global_app->a11y_enabled = _global_app->interface_schema
849 ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
850 : _global_app->launch_immediately;
853 if (!screen_reader_set)
855 _global_app->screen_reader_enabled = _global_app->a11y_schema
856 ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
860 if (_global_app->interface_schema)
861 g_signal_connect (_global_app->interface_schema,
862 "changed::toolkit-accessibility",
863 G_CALLBACK (gsettings_key_changed), _global_app);
865 if (_global_app->a11y_schema)
866 g_signal_connect (_global_app->a11y_schema,
867 "changed::screen-reader-enabled",
868 G_CALLBACK (gsettings_key_changed), _global_app);
870 init_sigterm_handling (_global_app);
872 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
873 g_assert (introspection_data != NULL);
875 _global_app->name_owner_id =
876 g_bus_own_name (G_BUS_TYPE_SESSION,
878 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
885 g_main_loop_run (_global_app->loop);
887 if (_global_app->a11y_bus_pid > 0)
888 kill (_global_app->a11y_bus_pid, SIGTERM);
890 /* Clear the X property if our bus is gone; in the case where e.g.
891 * GDM is launching a login on an X server it was using before,
892 * we don't want early login processes to pick up the stale address.
895 if (_global_app->x11_prop_set)
897 Display *display = XOpenDisplay (NULL);
900 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
901 XDeleteProperty (display,
902 XDefaultRootWindow (display),
906 XCloseDisplay (display);
911 if (_global_app->a11y_launch_error_message)
913 g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);