Merge tag 'upstream/2.34.0' into tizen
[platform/upstream/at-spi2-core.git] / bus / at-spi-bus-launcher.c
index 9fb138e..afd4edb 100644 (file)
@@ -1,23 +1,23 @@
 /* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
- * 
- * at-spi-bus-launcher: Manage the a11y bus as a child process 
  *
- * Copyright 2011 Red Hat, Inc.
+ * at-spi-bus-launcher: Manage the a11y bus as a child process
+ *
+ * Copyright 2011-2018 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 #include "config.h"
 #include <unistd.h>
 #include <string.h>
 #include <signal.h>
+#ifdef __linux__
+#include <sys/prctl.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#endif
 #include <sys/wait.h>
 #include <errno.h>
 #include <stdio.h>
@@ -98,6 +103,7 @@ typedef struct {
   GDBusConnection *session_bus;
   GSettings *a11y_schema;
   GSettings *interface_schema;
+  int name_owner_id;
 
   A11yBusClient screen_reader;
   A11yBusClient universal_switch;
@@ -109,7 +115,11 @@ typedef struct {
   /* -1 == error, 0 == pending, > 0 == running */
   int a11y_bus_pid;
   char *a11y_bus_address;
+#ifdef HAVE_X11
+  gboolean x11_prop_set;
+#endif
   int pipefd[2];
+  int listenfd;
   char *a11y_launch_error_message;
 } A11yBusLauncher;
 
@@ -267,23 +277,6 @@ name_appeared_handler (GDBusConnection *connection,
   register_client (app);
 }
 
-static void
-setup_bus_child (gpointer data)
-{
-  A11yBusLauncher *app = data;
-  (void) app;
-
-  close (app->pipefd[0]);
-  dup2 (app->pipefd[1], 3);
-  close (app->pipefd[1]);
-
-  /* On Linux, tell the bus process to exit if this process goes away */
-#ifdef __linux
-#include <sys/prctl.h>
-  prctl (PR_SET_PDEATHSIG, 15);
-#endif
-}
-
 /**
  * unix_read_all_fd_to_string:
  *
@@ -296,7 +289,7 @@ unix_read_all_fd_to_string (int      fd,
 {
   ssize_t bytes_read;
 
-  while (max_bytes > 1 && (bytes_read = read (fd, buf, MAX (4096, max_bytes - 1))))
+  while (max_bytes > 1 && (bytes_read = read (fd, buf, MIN (4096, max_bytes - 1))))
     {
       if (bytes_read < 0)
         return FALSE;
@@ -328,33 +321,50 @@ on_bus_exited (GPid     pid,
   g_main_loop_quit (app->loop);
 }
 
+#ifdef DBUS_DAEMON
+static void
+setup_bus_child_daemon (gpointer data)
+{
+  A11yBusLauncher *app = data;
+  (void) app;
+
+  close (app->pipefd[0]);
+  dup2 (app->pipefd[1], 3);
+  close (app->pipefd[1]);
+
+  /* On Linux, tell the bus process to exit if this process goes away */
+#ifdef __linux__
+  prctl (PR_SET_PDEATHSIG, 15);
+#endif
+}
+
 static gboolean
-ensure_a11y_bus (A11yBusLauncher *app)
+ensure_a11y_bus_daemon (A11yBusLauncher *app, char *config_path)
 {
+  char *argv[] = { DBUS_DAEMON, config_path, "--nofork", "--print-address", "3", NULL };
   GPid pid;
-  char *argv[] = { DBUS_DAEMON, NULL, "--nofork", "--print-address", "3", NULL };
   char addr_buf[2048];
   GError *error = NULL;
-  const char *config_path = NULL;
 
   if (app->a11y_bus_pid != 0)
     return FALSE;
 
-  if (g_file_test (SYSCONFDIR"/at-spi2/accessibility.conf", G_FILE_TEST_EXISTS))
-      config_path = "--config-file="SYSCONFDIR"/at-spi2/accessibility.conf";
-  else
-      config_path = "--config-file="DATADIR"/defaults/at-spi2/accessibility.conf";
-
   argv[1] = (char*)config_path;
 
   if (pipe (app->pipefd) < 0)
-    g_error ("Failed to create pipe: %s", strerror (errno));
+    {
+      char buf[4096] = { 0 };
+      strerror_r (errno, buf, sizeof(buf));
+      g_error ("Failed to create pipe: %s", buf);
+    }
+
+  g_clear_pointer (&app->a11y_launch_error_message, g_free);
 
   if (!g_spawn_async (NULL,
                       argv,
                       NULL,
                       G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
-                      setup_bus_child,
+                      setup_bus_child_daemon,
                       app,
                       &pid,
                       &error))
@@ -375,8 +385,11 @@ ensure_a11y_bus (A11yBusLauncher *app)
   LOGD("Launched a11y bus, child is %ld", (long) pid);
   if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
     {
-      app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
+      char buf[4096] = { 0 };
+      strerror_r (errno, buf, sizeof(buf));
+      app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", buf);
       kill (app->a11y_bus_pid, SIGTERM);
+      app->a11y_bus_pid = -1;
       goto error;
     }
   close (app->pipefd[0]);
@@ -384,35 +397,179 @@ ensure_a11y_bus (A11yBusLauncher *app)
   app->state = A11Y_BUS_STATE_RUNNING;
 
   /* Trim the trailing newline */
+  if (app->a11y_bus_address) g_free(app->a11y_bus_address);
   app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
   LOGD("a11y bus address: %s", app->a11y_bus_address);
 
-#ifdef HAVE_X11
-  {
-    Display *display = XOpenDisplay (NULL);
-    if (display)
-      {
-        Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
-        XChangeProperty (display,
-                         XDefaultRootWindow (display),
-                         bus_address_atom,
-                         XA_STRING, 8, PropModeReplace,
-                         (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
-        XFlush (display);
-        XCloseDisplay (display);
-      }
-  }
+  return TRUE;
+
+error:
+  if (app->pipefd[0] > 0) close (app->pipefd[0]);
+  if (app->pipefd[1] > 0) close (app->pipefd[1]);
+  app->state = A11Y_BUS_STATE_ERROR;
+
+  return FALSE;
+}
+#else
+static gboolean
+ensure_a11y_bus_daemon (A11yBusLauncher *app, char *config_path)
+{
+       return FALSE;
+}
 #endif
 
+#ifdef DBUS_BROKER
+static void
+setup_bus_child_broker (gpointer data)
+{
+  A11yBusLauncher *app = data;
+  gchar *pid_str;
+  (void) app;
+
+  dup2 (app->listenfd, 3);
+  close (app->listenfd);
+  g_setenv("LISTEN_FDS", "1", TRUE);
+
+  pid_str = g_strdup_printf("%u", getpid());
+  g_setenv("LISTEN_PID", pid_str, TRUE);
+  g_free(pid_str);
+
+  /* Tell the bus process to exit if this process goes away */
+  prctl (PR_SET_PDEATHSIG, SIGTERM);
+}
+
+static gboolean
+ensure_a11y_bus_broker (A11yBusLauncher *app, char *config_path)
+{
+  char *argv[] = { DBUS_BROKER, config_path, "--scope", "user", NULL };
+  struct sockaddr_un addr = { .sun_family = AF_UNIX };
+  socklen_t addr_len = sizeof(addr);
+  GPid pid;
+  GError *error = NULL;
+
+  if ((app->listenfd = socket (PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0)
+    {
+      char buf[4096] = { 0 };
+      strerror_r (errno, buf, sizeof(buf));
+      g_error ("Failed to create listening socket: %s", buf);
+    }
+
+  if (bind (app->listenfd, (struct sockaddr *)&addr, sizeof(sa_family_t)) < 0)
+    {
+      char buf[4096] = { 0 };
+      strerror_r (errno, buf, sizeof(buf));
+      g_error ("Failed to bind listening socket: %s", buf);
+    }
+
+  if (getsockname (app->listenfd, (struct sockaddr *)&addr, &addr_len) < 0)
+    {
+      char buf[4096] = { 0 };
+      strerror_r (errno, buf, sizeof(buf));
+      g_error ("Failed to get socket name for listening socket: %s", buf);
+    }
+
+  if (listen (app->listenfd, 1024) < 0)
+    {
+      char buf[4096] = { 0 };
+      strerror_r (errno, buf, sizeof(buf));
+      g_error ("Failed to listen on socket: %s", buf);
+    }
+
+  g_clear_pointer (&app->a11y_launch_error_message, g_free);
+
+  if (!g_spawn_async (NULL,
+                      argv,
+                      NULL,
+                      G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
+                      setup_bus_child_broker,
+                      app,
+                      &pid,
+                      &error))
+    {
+      app->a11y_bus_pid = -1;
+      app->a11y_launch_error_message = g_strdup (error->message);
+      g_clear_error (&error);
+      goto error;
+    }
+
+  close (app->listenfd);
+  app->listenfd = -1;
+
+  g_child_watch_add (pid, on_bus_exited, app);
+  app->a11y_bus_pid = pid;
+  g_debug ("Launched a11y bus, child is %ld", (long) pid);
+  app->state = A11Y_BUS_STATE_RUNNING;
+
+  app->a11y_bus_address = g_strconcat("unix:abstract=", addr.sun_path + 1, NULL);
+  g_debug ("a11y bus address: %s", app->a11y_bus_address);
+
   return TRUE;
 
- error:
-  close (app->pipefd[0]);
-  close (app->pipefd[1]);
+error:
+  close (app->listenfd);
   app->state = A11Y_BUS_STATE_ERROR;
 
   return FALSE;
 }
+#else
+static gboolean
+ensure_a11y_bus_broker (A11yBusLauncher *app, char *config_path)
+{
+       return FALSE;
+}
+#endif
+
+static gboolean
+ensure_a11y_bus (A11yBusLauncher *app)
+{
+  char *config_path = NULL;
+  gboolean success = FALSE;
+
+  if (app->a11y_bus_pid != 0)
+    return FALSE;
+
+  if (g_file_test (SYSCONFDIR"/at-spi2/accessibility.conf", G_FILE_TEST_EXISTS))
+      config_path = "--config-file="SYSCONFDIR"/at-spi2/accessibility.conf";
+  else
+      config_path = "--config-file="DATADIR"/defaults/at-spi2/accessibility.conf";
+
+#ifdef WANT_DBUS_BROKER
+    success = ensure_a11y_bus_broker (app, config_path);
+    if (!success)
+      {
+        if (!ensure_a11y_bus_daemon (app, config_path))
+            return FALSE;
+      }
+#else
+    success = ensure_a11y_bus_daemon (app, config_path);
+    if (!success)
+      {
+        if (!ensure_a11y_bus_broker (app, config_path))
+            return FALSE;
+      }
+#endif
+
+#ifdef HAVE_X11
+  if (g_getenv ("DISPLAY") != NULL && g_getenv ("WAYLAND_DISPLAY") == NULL)
+    {
+      Display *display = XOpenDisplay (NULL);
+      if (display)
+        {
+          Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
+          XChangeProperty (display,
+                           XDefaultRootWindow (display),
+                           bus_address_atom,
+                           XA_STRING, 8, PropModeReplace,
+                           (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
+          XFlush (display);
+          XCloseDisplay (display);
+          app->x11_prop_set = TRUE;
+        }
+    }
+#endif
+
+  return TRUE;
+}
 
 static void
 handle_method_call (GDBusConnection       *connection,
@@ -658,16 +815,6 @@ on_bus_acquired (GDBusConnection *connection,
     }
   app->session_bus = connection;
 
-  if (app->launch_immediately)
-    {
-      ensure_a11y_bus (app);
-      if (app->state == A11Y_BUS_STATE_ERROR)
-        {
-          g_main_loop_quit (app->loop);
-          return;
-        }
-    }
-
   error = NULL;
   registration_id = g_dbus_connection_register_object (connection,
                                                        "/org/a11y/bus",
@@ -709,6 +856,18 @@ on_name_acquired (GDBusConnection *connection,
                   const gchar     *name,
                   gpointer         user_data)
 {
+  A11yBusLauncher *app = user_data;
+
+  if (app->launch_immediately)
+    {
+      ensure_a11y_bus (app);
+      if (app->state == A11Y_BUS_STATE_ERROR)
+        {
+          g_main_loop_quit (app->loop);
+          return;
+        }
+    }
+
   g_bus_watch_name (G_BUS_TYPE_SESSION,
                     "org.gnome.SessionManager",
                     G_BUS_NAME_WATCHER_FLAGS_NONE,
@@ -742,7 +901,11 @@ init_sigterm_handling (A11yBusLauncher *app)
   GIOChannel *sigterm_channel;
 
   if (pipe (sigterm_pipefd) < 0)
-    g_error ("Failed to create pipe: %s", strerror (errno));
+    {
+      char buf[4096] = { 0 };
+      strerror_r (errno, buf, sizeof(buf));
+      g_error ("Failed to create pipe: %s", buf);
+    }
   signal (SIGTERM, sigterm_handler);
 
   sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
@@ -752,53 +915,20 @@ init_sigterm_handling (A11yBusLauncher *app)
                   app);
 }
 
-static gboolean
-already_running ()
-{
-#ifdef HAVE_X11
-  Atom AT_SPI_BUS;
-  Atom actual_type;
-  Display *bridge_display;
-  int actual_format;
-  unsigned char *data = NULL;
-  unsigned long nitems;
-  unsigned long leftover;
-  gboolean result = FALSE;
-
-  bridge_display = XOpenDisplay (NULL);
-  if (!bridge_display)
-             return FALSE;
-
-  AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
-  XGetWindowProperty (bridge_display,
-                     XDefaultRootWindow (bridge_display),
-                     AT_SPI_BUS, 0L,
-                     (long) BUFSIZ, False,
-                     (Atom) 31, &actual_type, &actual_format,
-                     &nitems, &leftover, &data);
-
-  if (data)
-  {
-    GDBusConnection *bus;
-    bus = g_dbus_connection_new_for_address_sync ((const gchar *)data, 0,
-                                                  NULL, NULL, NULL);
-    if (bus != NULL)
-      {
-        result = TRUE;
-        g_object_unref (bus);
-      }
-  }
-
-  XCloseDisplay (bridge_display);
-  return result;
-#else
-  return FALSE;
-#endif
-}
-
 static GSettings *
 get_schema (const gchar *name)
 {
+#if GLIB_CHECK_VERSION (2, 32, 0)
+  GSettingsSchemaSource *source = g_settings_schema_source_get_default ();
+  if (!source) return NULL;
+
+  GSettingsSchema *schema = g_settings_schema_source_lookup (source, name, FALSE);
+
+  if (schema == NULL)
+    return NULL;
+
+  return g_settings_new_full (schema, NULL, NULL);
+#else
   const char * const *schemas = NULL;
   gint i;
 
@@ -810,6 +940,7 @@ get_schema (const gchar *name)
   }
 
   return NULL;
+#endif
 }
 
 static void
@@ -1055,16 +1186,11 @@ main (int    argc,
 #endif
 
   LOGD("Starting atspi bus launcher");
+
   gboolean a11y_set = FALSE;
   gboolean screen_reader_set = FALSE;
   gint i;
 
-  if (already_running ())
-    {
-       LOGD("atspi bus launcher is already running");
-       return 0;
-    }
-
   _global_app = g_slice_new0 (A11yBusLauncher);
   _global_app->loop = g_main_loop_new (NULL, FALSE);
   _global_app->client_watcher_id = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
@@ -1147,19 +1273,20 @@ main (int    argc,
    * we don't want early login processes to pick up the stale address.
    */
 #ifdef HAVE_X11
-  {
-    Display *display = XOpenDisplay (NULL);
-    if (display)
-      {
-        Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
-        XDeleteProperty (display,
-                         XDefaultRootWindow (display),
-                         bus_address_atom);
+  if (_global_app->x11_prop_set)
+    {
+      Display *display = XOpenDisplay (NULL);
+      if (display)
+        {
+          Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
+          XDeleteProperty (display,
+                           XDefaultRootWindow (display),
+                           bus_address_atom);
 
-        XFlush (display);
-        XCloseDisplay (display);
-      }
-  }
+          XFlush (display);
+          XCloseDisplay (display);
+        }
+    }
 #endif
 
   if (_global_app->a11y_launch_error_message)