2.44.1
[platform/upstream/at-spi2-core.git] / bus / at-spi-bus-launcher.c
1 /* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * 
3  * at-spi-bus-launcher: Manage the a11y bus as a child process 
4  *
5  * Copyright 2011-2018 Red Hat, Inc.
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include "config.h"
24
25 #include <unistd.h>
26 #include <string.h>
27 #include <signal.h>
28 #ifdef __linux__
29 #include <sys/prctl.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #endif
33 #include <sys/wait.h>
34 #include <errno.h>
35 #include <stdio.h>
36
37 #include <gio/gio.h>
38 #ifdef HAVE_X11
39 #include <X11/Xlib.h>
40 #include <X11/Xatom.h>
41 #endif
42 #ifdef DBUS_BROKER
43 #include <systemd/sd-login.h>
44 #endif
45 #include <sys/stat.h>
46
47 typedef enum {
48   A11Y_BUS_STATE_IDLE = 0,
49   A11Y_BUS_STATE_READING_ADDRESS,
50   A11Y_BUS_STATE_RUNNING,
51   A11Y_BUS_STATE_ERROR
52 } A11yBusState;
53
54 typedef struct {
55   GMainLoop *loop;
56   gboolean launch_immediately;
57   gboolean a11y_enabled;
58   gboolean screen_reader_enabled;
59   GDBusConnection *session_bus;
60   GSettings *a11y_schema;
61   GSettings *interface_schema;
62   int name_owner_id;
63
64   GDBusProxy *client_proxy;
65
66   A11yBusState state;
67   /* -1 == error, 0 == pending, > 0 == running */
68   GPid a11y_bus_pid;
69   char *socket_name;
70   char *a11y_bus_address;
71 #ifdef HAVE_X11
72   gboolean x11_prop_set;
73 #endif
74   int pipefd[2];
75   int listenfd;
76   char *a11y_launch_error_message;
77   GDBusProxy *sm_proxy;
78 } A11yBusLauncher;
79
80 static A11yBusLauncher *_global_app = NULL;
81
82 static const gchar introspection_xml[] =
83   "<node>"
84   "  <interface name='org.a11y.Bus'>"
85   "    <method name='GetAddress'>"
86   "      <arg type='s' name='address' direction='out'/>"
87   "    </method>"
88   "  </interface>"
89   "  <interface name='org.a11y.Status'>"
90   "    <property name='IsEnabled' type='b' access='readwrite'/>"
91   "    <property name='ScreenReaderEnabled' type='b' access='readwrite'/>"
92   "  </interface>"
93   "</node>";
94 static GDBusNodeInfo *introspection_data = NULL;
95
96 static void
97 respond_to_end_session (GDBusProxy *proxy)
98 {
99   GVariant *parameters;
100
101   parameters = g_variant_new ("(bs)", TRUE, "");
102
103   g_dbus_proxy_call (proxy,
104                      "EndSessionResponse", parameters,
105                      G_DBUS_CALL_FLAGS_NONE,
106                      -1, NULL, NULL, NULL);
107 }
108
109 static void
110 g_signal_cb (GDBusProxy *proxy,
111              gchar      *sender_name,
112              gchar      *signal_name,
113              GVariant   *parameters,
114              gpointer    user_data)
115 {
116   A11yBusLauncher *app = user_data;
117
118   if (g_strcmp0 (signal_name, "QueryEndSession") == 0)
119     respond_to_end_session (proxy);
120   else if (g_strcmp0 (signal_name, "EndSession") == 0)
121     respond_to_end_session (proxy);
122   else if (g_strcmp0 (signal_name, "Stop") == 0)
123     g_main_loop_quit (app->loop);
124 }
125
126 static void
127 client_proxy_ready_cb (GObject      *source_object,
128                        GAsyncResult *res,
129                        gpointer      user_data)
130 {
131   A11yBusLauncher *app = user_data;
132   GError *error = NULL;
133
134   app->client_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
135
136   if (error != NULL)
137     {
138       g_warning ("Failed to get a client proxy: %s", error->message);
139       g_error_free (error);
140
141       return;
142     }
143
144   g_signal_connect (app->client_proxy, "g-signal",
145                     G_CALLBACK (g_signal_cb), app);
146 }
147
148 static void
149 client_registered (GObject *source,
150                    GAsyncResult *result,
151                    gpointer user_data)
152 {
153   A11yBusLauncher *app = user_data;
154   GError *error = NULL;
155   GVariant *variant;
156   gchar *object_path;
157   GDBusProxyFlags flags;
158
159   variant = g_dbus_proxy_call_finish (app->sm_proxy, result, &error);
160   if (!variant)
161     {
162       if (error != NULL)
163         {
164           g_warning ("Failed to register client: %s", error->message);
165           g_error_free (error);
166         }
167     }
168   else
169     {
170       g_variant_get (variant, "(o)", &object_path);
171       g_variant_unref (variant);
172
173       flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
174       g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION, flags, NULL,
175                                 "org.gnome.SessionManager", object_path,
176                                 "org.gnome.SessionManager.ClientPrivate",
177                                 NULL, client_proxy_ready_cb, app);
178
179       g_free (object_path);
180     }
181   g_clear_object (&app->sm_proxy);
182 }
183
184 static void
185 register_client (A11yBusLauncher *app)
186 {
187   GDBusProxyFlags flags;
188   GError *error;
189   const gchar *app_id;
190   const gchar *autostart_id;
191   gchar *client_startup_id;
192   GVariant *parameters;
193
194   flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
195           G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS;
196
197   error = NULL;
198   app->sm_proxy = g_dbus_proxy_new_sync (app->session_bus, flags, NULL,
199                                          "org.gnome.SessionManager",
200                                          "/org/gnome/SessionManager",
201                                          "org.gnome.SessionManager",
202                                          NULL, &error);
203
204   if (error != NULL)
205     {
206       g_warning ("Failed to get session manager proxy: %s", error->message);
207       g_error_free (error);
208
209       return;
210     }
211
212   app_id = "at-spi-bus-launcher";
213   autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
214
215   if (autostart_id != NULL)
216     {
217       client_startup_id = g_strdup (autostart_id);
218       g_unsetenv ("DESKTOP_AUTOSTART_ID");
219     }
220   else
221     {
222       client_startup_id = g_strdup ("");
223     }
224
225   parameters = g_variant_new ("(ss)", app_id, client_startup_id);
226   g_free (client_startup_id);
227
228   error = NULL;
229   g_dbus_proxy_call (app->sm_proxy,
230                      "RegisterClient", parameters,
231                      G_DBUS_CALL_FLAGS_NONE,
232                      G_MAXINT, NULL, client_registered, app);
233
234 }
235
236 static void
237 name_appeared_handler (GDBusConnection *connection,
238                        const gchar     *name,
239                        const gchar     *name_owner,
240                        gpointer         user_data)
241 {
242   A11yBusLauncher *app = user_data;
243
244   register_client (app);
245 }
246
247 /**
248  * unix_read_all_fd_to_string:
249  *
250  * Read all data from a file descriptor to a C string buffer.
251  */
252 static gboolean
253 unix_read_all_fd_to_string (int       fd,
254                             char     *buf,
255                             ssize_t   max_bytes,
256                             char    **error_msg)
257 {
258   g_assert (max_bytes > 1);
259   *error_msg = NULL;
260
261   max_bytes -= 1; /* allow space for nul terminator */
262
263   while (max_bytes > 1)
264     {
265       ssize_t bytes_read;
266
267     again:
268       bytes_read = read (fd, buf, max_bytes);
269
270       if (bytes_read == 0)
271         {
272           break;
273         }
274       else if (bytes_read > 0)
275         {
276           buf += bytes_read;
277           max_bytes -= bytes_read;
278         }
279       else if (errno == EINTR)
280         {
281           goto again;
282         }
283       else
284         {
285           int err_save = errno;
286           *error_msg = g_strdup_printf ("Failed to read data from accessibility bus: %s", g_strerror (err_save));
287           return FALSE;
288         }
289     }
290
291   *buf = '\0';
292   return TRUE;
293 }
294
295 static void
296 on_bus_exited (GPid     pid,
297                gint     status,
298                gpointer data)
299 {
300   A11yBusLauncher *app = data;
301   
302   app->a11y_bus_pid = -1;
303   app->state = A11Y_BUS_STATE_ERROR;
304   if (app->a11y_launch_error_message == NULL)
305     {
306       if (WIFEXITED (status))
307         app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
308       else if (WIFSIGNALED (status))
309         app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
310       else if (WIFSTOPPED (status))
311         app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
312     }
313   g_main_loop_quit (app->loop);
314 }
315
316 #ifdef DBUS_DAEMON
317 static gboolean
318 ensure_a11y_bus_daemon (A11yBusLauncher *app, char *config_path)
319 {
320   char *address_param;
321
322   if (app->socket_name)
323     {
324       gchar *escaped_address = g_dbus_address_escape_value (app->socket_name);
325       address_param = g_strconcat ("--address=unix:path=", escaped_address, NULL);
326       g_free (escaped_address);
327     }
328   else
329     {
330       address_param = NULL;
331     }
332
333   if (pipe (app->pipefd) < 0)
334     g_error ("Failed to create pipe: %s", strerror (errno));
335
336   char *print_address_fd_param = g_strdup_printf ("%d", app->pipefd[1]);
337
338   char *argv[] = { DBUS_DAEMON, config_path, "--nofork", "--print-address", print_address_fd_param, address_param, NULL };
339   gint source_fds[1] = { app->pipefd[1] };
340   gint target_fds[1] = { app->pipefd[1] };
341   G_STATIC_ASSERT (G_N_ELEMENTS (source_fds) == G_N_ELEMENTS (target_fds));
342   GPid pid;
343   char addr_buf[2048];
344   GError *error = NULL;
345   char *error_from_read;
346
347   g_clear_pointer (&app->a11y_launch_error_message, g_free);
348
349   if (!g_spawn_async_with_pipes_and_fds (NULL,
350                                          (const gchar * const *) argv,
351                                          NULL,
352                                          G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_LEAVE_DESCRIPTORS_OPEN,
353                                          NULL, /* child_setup */
354                                          app,
355                                          -1, /* stdin_fd */
356                                          -1, /* stdout_fd */
357                                          -1, /* stdout_fd */
358                                          source_fds,
359                                          target_fds,
360                                          G_N_ELEMENTS (source_fds), /* n_fds in source_fds and target_fds */
361                                          &pid,
362                                          NULL, /* stdin_pipe_out */
363                                          NULL, /* stdout_pipe_out */
364                                          NULL, /* stderr_pipe_out */
365                                          &error))
366     {
367       app->a11y_bus_pid = -1;
368       app->a11y_launch_error_message = g_strdup (error->message);
369       g_clear_error (&error);
370       g_free (address_param);
371       g_free (print_address_fd_param);
372       goto error;
373     }
374
375   g_free (address_param);
376   g_free (print_address_fd_param);
377   close (app->pipefd[1]);
378   app->pipefd[1] = -1;
379
380   g_child_watch_add (pid, on_bus_exited, app);
381
382   app->state = A11Y_BUS_STATE_READING_ADDRESS;
383   app->a11y_bus_pid = pid;
384   g_debug ("Launched a11y bus, child is %ld", (long) pid);
385   error_from_read = NULL;
386   if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf), &error_from_read))
387     {
388       app->a11y_launch_error_message = error_from_read;
389       kill (app->a11y_bus_pid, SIGTERM);
390       g_spawn_close_pid (app->a11y_bus_pid);
391       app->a11y_bus_pid = -1;
392       goto error;
393     }
394   close (app->pipefd[0]);
395   app->pipefd[0] = -1;
396   app->state = A11Y_BUS_STATE_RUNNING;
397
398   /* Trim the trailing newline */
399   app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
400   g_debug ("a11y bus address: %s", app->a11y_bus_address);
401
402   return TRUE;
403
404 error:
405   close (app->pipefd[0]);
406   close (app->pipefd[1]);
407   app->state = A11Y_BUS_STATE_ERROR;
408
409   return FALSE;
410 }
411 #else
412 static gboolean
413 ensure_a11y_bus_daemon (A11yBusLauncher *app, char *config_path)
414 {
415         return FALSE;
416 }
417 #endif
418
419 #ifdef DBUS_BROKER
420 static void
421 setup_bus_child_broker (gpointer data)
422 {
423   A11yBusLauncher *app = data;
424   gchar *pid_str;
425   (void) app;
426
427   dup2 (app->listenfd, 3);
428   close (app->listenfd);
429   g_setenv("LISTEN_FDS", "1", TRUE);
430
431   pid_str = g_strdup_printf("%u", getpid());
432   g_setenv("LISTEN_PID", pid_str, TRUE);
433   g_free(pid_str);
434 }
435
436 static gboolean
437 ensure_a11y_bus_broker (A11yBusLauncher *app, char *config_path)
438 {
439   char *argv[] = { DBUS_BROKER, config_path, "--scope", "user", NULL };
440   char *unit;
441   struct sockaddr_un addr = { .sun_family = AF_UNIX, "" };
442   socklen_t addr_len = sizeof(addr);
443   GPid pid;
444   GError *error = NULL;
445
446   if (app->socket_name)
447     {
448       strcpy (addr.sun_path, app->socket_name);
449       unlink (app->socket_name);
450     }
451
452   /* This detects whether we are running under systemd. We only try to
453    * use dbus-broker if we are running under systemd because D-Bus
454    * service activation won't work otherwise.
455    */
456   if (sd_pid_get_user_unit (getpid (), &unit) >= 0)
457     {
458       free (unit);
459     }
460   else
461     {
462       app->state = A11Y_BUS_STATE_ERROR;
463       return FALSE;
464     }
465
466   if ((app->listenfd = socket (PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0)
467     g_error ("Failed to create listening socket: %s", strerror (errno));
468
469   if (bind (app->listenfd, (struct sockaddr *)&addr, addr_len) < 0)
470     g_error ("Failed to bind listening socket: %s", strerror (errno));
471
472   if (!app->socket_name &&
473       getsockname (app->listenfd, (struct sockaddr *)&addr, &addr_len) < 0)
474     g_error ("Failed to get socket name for listening socket: %s", strerror(errno));
475
476   if (listen (app->listenfd, 1024) < 0)
477     g_error ("Failed to listen on socket: %s", strerror(errno));
478
479   g_clear_pointer (&app->a11y_launch_error_message, g_free);
480
481   if (!g_spawn_async (NULL,
482                       argv,
483                       NULL,
484                       G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
485                       setup_bus_child_broker,
486                       app,
487                       &pid,
488                       &error))
489     {
490       app->a11y_bus_pid = -1;
491       app->a11y_launch_error_message = g_strdup (error->message);
492       g_clear_error (&error);
493       goto error;
494     }
495
496   close (app->listenfd);
497   app->listenfd = -1;
498
499   g_child_watch_add (pid, on_bus_exited, app);
500   app->a11y_bus_pid = pid;
501   g_debug ("Launched a11y bus, child is %ld", (long) pid);
502   app->state = A11Y_BUS_STATE_RUNNING;
503
504   if (app->socket_name)
505     app->a11y_bus_address = g_strconcat("unix:path=", addr.sun_path, NULL);
506   else
507     app->a11y_bus_address = g_strconcat("unix:abstract=", addr.sun_path + 1, NULL);
508   g_debug ("a11y bus address: %s", app->a11y_bus_address);
509
510   return TRUE;
511
512 error:
513   close (app->listenfd);
514   app->state = A11Y_BUS_STATE_ERROR;
515
516   return FALSE;
517 }
518 #else
519 static gboolean
520 ensure_a11y_bus_broker (A11yBusLauncher *app, char *config_path)
521 {
522         return FALSE;
523 }
524 #endif
525
526 static gboolean
527 ensure_a11y_bus (A11yBusLauncher *app)
528 {
529   char *config_path = NULL;
530   gboolean success = FALSE;
531   const gchar *xdg_runtime_dir;
532
533   if (app->a11y_bus_pid != 0)
534     return FALSE;
535
536   if (g_file_test (SYSCONFDIR"/at-spi2/accessibility.conf", G_FILE_TEST_EXISTS))
537       config_path = "--config-file="SYSCONFDIR"/at-spi2/accessibility.conf";
538   else
539       config_path = "--config-file="DATADIR"/defaults/at-spi2/accessibility.conf";
540
541     xdg_runtime_dir = g_get_user_runtime_dir ();
542     if (xdg_runtime_dir)
543       {
544         const gchar *display = g_getenv ("DISPLAY");
545         gchar *at_spi_dir = g_strconcat (xdg_runtime_dir, "/at-spi", NULL);
546         gchar *p;
547         mkdir (xdg_runtime_dir, 0700);
548         if (!g_path_is_absolute (at_spi_dir))
549         {
550           gchar *new_dir = g_canonicalize_filename (at_spi_dir, NULL);
551           g_free (at_spi_dir);
552           at_spi_dir = new_dir;
553         }
554         if (mkdir (at_spi_dir, 0700) == 0 || errno == EEXIST)
555           {
556             app->socket_name = g_strconcat (at_spi_dir, "/bus", display, NULL);
557             g_free (at_spi_dir);
558             p = strchr (app->socket_name, ':');
559             if (p)
560               *p = '_';
561             if (strlen (app->socket_name) >= 100)
562               {
563                 g_free (app->socket_name);
564                 app->socket_name = NULL;
565               }
566           }
567         else
568           g_free (at_spi_dir);
569       }
570
571 #ifdef WANT_DBUS_BROKER
572     success = ensure_a11y_bus_broker (app, config_path);
573     if (!success)
574       {
575         if (!ensure_a11y_bus_daemon (app, config_path))
576             return FALSE;
577       }
578 #else
579     success = ensure_a11y_bus_daemon (app, config_path);
580     if (!success)
581       {
582         if (!ensure_a11y_bus_broker (app, config_path))
583             return FALSE;
584       }
585 #endif
586
587 #ifdef HAVE_X11
588   if (g_getenv ("DISPLAY") != NULL && g_getenv ("WAYLAND_DISPLAY") == NULL)
589     {
590       Display *display = XOpenDisplay (NULL);
591       if (display)
592         {
593           Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
594           XChangeProperty (display,
595                            XDefaultRootWindow (display),
596                            bus_address_atom,
597                            XA_STRING, 8, PropModeReplace,
598                            (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
599           XFlush (display);
600           XCloseDisplay (display);
601           app->x11_prop_set = TRUE;
602         }
603     }
604 #endif
605
606   return TRUE;
607 }
608
609 static void
610 handle_method_call (GDBusConnection       *connection,
611                     const gchar           *sender,
612                     const gchar           *object_path,
613                     const gchar           *interface_name,
614                     const gchar           *method_name,
615                     GVariant              *parameters,
616                     GDBusMethodInvocation *invocation,
617                     gpointer               user_data)
618 {
619   A11yBusLauncher *app = user_data;
620
621   if (g_strcmp0 (method_name, "GetAddress") == 0)
622     {
623       ensure_a11y_bus (app);
624       if (app->a11y_bus_pid > 0)
625         g_dbus_method_invocation_return_value (invocation,
626                                                g_variant_new ("(s)", app->a11y_bus_address));
627       else
628         g_dbus_method_invocation_return_dbus_error (invocation,
629                                                     "org.a11y.Bus.Error",
630                                                     app->a11y_launch_error_message);
631     }
632 }
633
634 static GVariant *
635 handle_get_property  (GDBusConnection       *connection,
636                       const gchar           *sender,
637                       const gchar           *object_path,
638                       const gchar           *interface_name,
639                       const gchar           *property_name,
640                     GError **error,
641                     gpointer               user_data)
642 {
643   A11yBusLauncher *app = user_data;
644
645   if (g_strcmp0 (property_name, "IsEnabled") == 0)
646     return g_variant_new ("b", app->a11y_enabled);
647   else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
648     return g_variant_new ("b", app->screen_reader_enabled);
649   else
650     return NULL;
651 }
652
653 static void
654 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
655                                gboolean notify_gsettings)
656 {
657   GVariantBuilder builder;
658   GVariantBuilder invalidated_builder;
659
660   if (enabled == app->a11y_enabled)
661     return;
662
663   app->a11y_enabled = enabled;
664
665   if (notify_gsettings && app->interface_schema)
666     {
667       g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
668                               enabled);
669       g_settings_sync ();
670     }
671
672   g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
673   g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
674   g_variant_builder_add (&builder, "{sv}", "IsEnabled",
675                          g_variant_new_boolean (enabled));
676
677   g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
678                                  "org.freedesktop.DBus.Properties",
679                                  "PropertiesChanged",
680                                  g_variant_new ("(sa{sv}as)", "org.a11y.Status",
681                                                 &builder,
682                                                 &invalidated_builder),
683                                  NULL);
684
685   g_variant_builder_clear (&builder);
686   g_variant_builder_clear (&invalidated_builder);
687 }
688
689 static void
690 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
691                                gboolean notify_gsettings)
692 {
693   GVariantBuilder builder;
694   GVariantBuilder invalidated_builder;
695
696   if (enabled == app->screen_reader_enabled)
697     return;
698
699   /* If the screen reader is being enabled, we should enable accessibility
700    * if it isn't enabled already */
701   if (enabled)
702     handle_a11y_enabled_change (app, enabled, notify_gsettings);
703
704   app->screen_reader_enabled = enabled;
705
706   if (notify_gsettings && app->a11y_schema)
707     {
708       g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
709                               enabled);
710       g_settings_sync ();
711     }
712
713   g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
714   g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
715   g_variant_builder_add (&builder, "{sv}", "ScreenReaderEnabled",
716                          g_variant_new_boolean (enabled));
717
718   g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
719                                  "org.freedesktop.DBus.Properties",
720                                  "PropertiesChanged",
721                                  g_variant_new ("(sa{sv}as)", "org.a11y.Status",
722                                                 &builder,
723                                                 &invalidated_builder),
724                                  NULL);
725
726   g_variant_builder_clear (&builder);
727   g_variant_builder_clear (&invalidated_builder);
728 }
729
730 static gboolean
731 handle_set_property  (GDBusConnection       *connection,
732                       const gchar           *sender,
733                       const gchar           *object_path,
734                       const gchar           *interface_name,
735                       const gchar           *property_name,
736                       GVariant *value,
737                     GError **error,
738                     gpointer               user_data)
739 {
740   A11yBusLauncher *app = user_data;
741   const gchar *type = g_variant_get_type_string (value);
742   gboolean enabled;
743   
744   if (g_strcmp0 (type, "b") != 0)
745     {
746       g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
747                        "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
748       return FALSE;
749     }
750
751   enabled = g_variant_get_boolean (value);
752
753   if (g_strcmp0 (property_name, "IsEnabled") == 0)
754     {
755       handle_a11y_enabled_change (app, enabled, TRUE);
756       return TRUE;
757     }
758   else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
759     {
760       handle_screen_reader_enabled_change (app, enabled, TRUE);
761       return TRUE;
762     }
763   else
764     {
765       g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
766                        "Unknown property '%s'", property_name);
767       return FALSE;
768     }
769 }
770
771 static const GDBusInterfaceVTable bus_vtable =
772 {
773   handle_method_call,
774   NULL, /* handle_get_property, */
775   NULL  /* handle_set_property */
776 };
777
778 static const GDBusInterfaceVTable status_vtable =
779 {
780   NULL, /* handle_method_call */
781   handle_get_property,
782   handle_set_property
783 };
784
785 static void
786 on_bus_acquired (GDBusConnection *connection,
787                  const gchar     *name,
788                  gpointer         user_data)
789 {
790   A11yBusLauncher *app = user_data;
791   GError *error;
792   guint registration_id;
793   
794   if (connection == NULL)
795     {
796       g_main_loop_quit (app->loop);
797       return;
798     }
799   app->session_bus = connection;
800
801   error = NULL;
802   registration_id = g_dbus_connection_register_object (connection,
803                                                        "/org/a11y/bus",
804                                                        introspection_data->interfaces[0],
805                                                        &bus_vtable,
806                                                        _global_app,
807                                                        NULL,
808                                                        &error);
809   if (registration_id == 0)
810     {
811       g_error ("%s", error->message);
812       g_clear_error (&error);
813     }
814
815   g_dbus_connection_register_object (connection,
816                                      "/org/a11y/bus",
817                                      introspection_data->interfaces[1],
818                                      &status_vtable,
819                                      _global_app,
820                                      NULL,
821                                      NULL);
822 }
823
824 static void
825 on_name_lost (GDBusConnection *connection,
826               const gchar     *name,
827               gpointer         user_data)
828 {
829   A11yBusLauncher *app = user_data;
830   if (app->session_bus == NULL
831       && connection == NULL
832       && app->a11y_launch_error_message == NULL)
833     app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
834   g_main_loop_quit (app->loop);
835 }
836
837 static void
838 on_name_acquired (GDBusConnection *connection,
839                   const gchar     *name,
840                   gpointer         user_data)
841 {
842   A11yBusLauncher *app = user_data;
843
844   if (app->launch_immediately)
845     {
846       ensure_a11y_bus (app);
847       if (app->state == A11Y_BUS_STATE_ERROR)
848         {
849           g_main_loop_quit (app->loop);
850           return;
851         }
852     }
853
854   g_bus_watch_name (G_BUS_TYPE_SESSION,
855                     "org.gnome.SessionManager",
856                     G_BUS_NAME_WATCHER_FLAGS_NONE,
857                     name_appeared_handler, NULL,
858                     user_data, NULL);
859 }
860
861 static int sigterm_pipefd[2];
862
863 static void
864 sigterm_handler (int signum)
865 {
866   write (sigterm_pipefd[1], "X", 1);
867 }
868
869 static gboolean
870 on_sigterm_pipe (GIOChannel  *channel,
871                  GIOCondition condition,
872                  gpointer     data)
873 {
874   A11yBusLauncher *app = data;
875   
876   g_main_loop_quit (app->loop);
877
878   return FALSE;
879 }
880
881 static void
882 init_sigterm_handling (A11yBusLauncher *app)
883 {
884   GIOChannel *sigterm_channel;
885
886   if (pipe (sigterm_pipefd) < 0)
887     g_error ("Failed to create pipe: %s", strerror (errno));
888   signal (SIGTERM, sigterm_handler);
889
890   sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
891   g_io_add_watch (sigterm_channel,
892                   G_IO_IN | G_IO_ERR | G_IO_HUP,
893                   on_sigterm_pipe,
894                   app);
895 }
896
897 static GSettings *
898 get_schema (const gchar *name)
899 {
900 #if GLIB_CHECK_VERSION (2, 32, 0)
901   GSettingsSchemaSource *source = g_settings_schema_source_get_default ();
902   if (!source)
903     {
904       g_error ("Cannot get the default GSettingsSchemaSource - is the gsettings-desktop-schemas package installed?");
905     }
906
907   GSettingsSchema *schema = g_settings_schema_source_lookup (source, name, FALSE);
908
909   if (schema == NULL)
910     return NULL;
911
912   return g_settings_new_full (schema, NULL, NULL);
913 #else
914   const char * const *schemas = NULL;
915   gint i;
916
917   schemas = g_settings_list_schemas ();
918   for (i = 0; schemas[i]; i++)
919   {
920     if (!strcmp (schemas[i], name))
921       return g_settings_new (schemas[i]);
922   }
923
924   return NULL;
925 #endif
926 }
927
928 static void
929 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
930 {
931   gboolean new_val = g_settings_get_boolean (gsettings, key);
932
933   if (!strcmp (key, "toolkit-accessibility"))
934     handle_a11y_enabled_change (_global_app, new_val, FALSE);
935   else if (!strcmp (key, "screen-reader-enabled"))
936     handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
937 }
938
939 int
940 main (int    argc,
941       char **argv)
942 {
943   gboolean a11y_set = FALSE;
944   gboolean screen_reader_set = FALSE;
945   gint i;
946
947   _global_app = g_new0 (A11yBusLauncher, 1);
948   _global_app->loop = g_main_loop_new (NULL, FALSE);
949
950   for (i = 1; i < argc; i++)
951     {
952       if (!strcmp (argv[i], "--launch-immediately"))
953         _global_app->launch_immediately = TRUE;
954       else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 1)
955         a11y_set = TRUE;
956       else if (sscanf (argv[i], "--screen-reader=%d",
957                        &_global_app->screen_reader_enabled) == 1)
958         screen_reader_set = TRUE;
959     else
960       g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
961     }
962
963   _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
964   _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
965
966   if (!a11y_set)
967     {
968       _global_app->a11y_enabled = _global_app->interface_schema
969                                   ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
970                                   : _global_app->launch_immediately;
971     }
972
973   if (!screen_reader_set)
974     {
975       _global_app->screen_reader_enabled = _global_app->a11y_schema
976                                   ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
977                                   : FALSE;
978     }
979
980   if (_global_app->interface_schema)
981     g_signal_connect (_global_app->interface_schema,
982                       "changed::toolkit-accessibility",
983                       G_CALLBACK (gsettings_key_changed), _global_app);
984
985   if (_global_app->a11y_schema)
986     g_signal_connect (_global_app->a11y_schema,
987                       "changed::screen-reader-enabled",
988                       G_CALLBACK (gsettings_key_changed), _global_app);
989
990   init_sigterm_handling (_global_app);
991
992   introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
993   g_assert (introspection_data != NULL);
994
995   _global_app->name_owner_id =
996     g_bus_own_name (G_BUS_TYPE_SESSION,
997                     "org.a11y.Bus",
998                     G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
999                     on_bus_acquired,
1000                     on_name_acquired,
1001                     on_name_lost,
1002                     _global_app,
1003                     NULL);
1004
1005   g_main_loop_run (_global_app->loop);
1006
1007   if (_global_app->a11y_bus_pid > 0)
1008     {
1009       kill (_global_app->a11y_bus_pid, SIGTERM);
1010       g_spawn_close_pid (_global_app->a11y_bus_pid);
1011       _global_app->a11y_bus_pid = -1;
1012     }
1013
1014   /* Clear the X property if our bus is gone; in the case where e.g. 
1015    * GDM is launching a login on an X server it was using before,
1016    * we don't want early login processes to pick up the stale address.
1017    */
1018 #ifdef HAVE_X11
1019   if (_global_app->x11_prop_set)
1020     {
1021       Display *display = XOpenDisplay (NULL);
1022       if (display)
1023         {
1024           Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
1025           XDeleteProperty (display,
1026                            XDefaultRootWindow (display),
1027                            bus_address_atom);
1028
1029           XFlush (display);
1030           XCloseDisplay (display);
1031         }
1032     }
1033 #endif
1034
1035   if (_global_app->a11y_launch_error_message)
1036     {
1037       g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);
1038       return 1;
1039     }
1040   return 0;
1041 }