Fix for invalid at-spi configuration
[platform/upstream/at-spi2-core.git] / bus / at-spi-bus-launcher.c
index d180a15..e42a26d 100644 (file)
@@ -2,7 +2,7 @@
  *
  * at-spi-bus-launcher: Manage the a11y bus as a child process
  *
- * Copyright 2011 Red Hat, Inc.
+ * 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
 #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>
 #endif
 
 //TODO: move to vconf/vconf-internal-setting-keys.h?
-#define VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH "db/setting/accessibility/universal-switch"
+#define VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_CONFIGURATION_SERVICE "db/setting/accessibility/universal-switch/configuration-service"
+#define VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE "db/setting/accessibility/universal-switch/interaction-service"
+
+#define MAX_NUMBER_OF_KEYS_PER_CLIENT 2
 
 #define APP_CONTROL_OPERATION_SCREEN_READ "http://tizen.org/appcontrol/operation/read_screen"
 #define APP_CONTROL_OPERATION_UNIVERSAL_SWITCH "http://tizen.org/appcontrol/operation/universal_switch"
@@ -80,7 +88,8 @@ typedef enum {
 typedef struct {
   const char * name;
   const char * app_control_operation;
-  const char * vconf_key;
+  const char * vconf_key[MAX_NUMBER_OF_KEYS_PER_CLIENT];
+  int number_of_keys;
   int launch_repeats;
   int pid;
 } A11yBusClient;
@@ -94,16 +103,20 @@ typedef struct {
   GDBusConnection *session_bus;
   GSettings *a11y_schema;
   GSettings *interface_schema;
+  int name_owner_id;
 
   A11yBusClient screen_reader;
   A11yBusClient universal_switch;
 
+  GDBusProxy *client_proxy;
+
   A11yBusState state;
 
   /* -1 == error, 0 == pending, > 0 == running */
   int a11y_bus_pid;
   char *a11y_bus_address;
   int pipefd[2];
+  int listenfd;
   char *a11y_launch_error_message;
 } A11yBusLauncher;
 
@@ -124,20 +137,141 @@ static const gchar introspection_xml[] =
 static GDBusNodeInfo *introspection_data = NULL;
 
 static void
-setup_bus_child (gpointer data)
+respond_to_end_session (GDBusProxy *proxy)
 {
-  A11yBusLauncher *app = data;
-  (void) app;
+  GVariant *parameters;
 
-  close (app->pipefd[0]);
-  dup2 (app->pipefd[1], 3);
-  close (app->pipefd[1]);
+  parameters = g_variant_new ("(bs)", TRUE, "");
 
-  /* 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
+  g_dbus_proxy_call (proxy,
+                     "EndSessionResponse", parameters,
+                     G_DBUS_CALL_FLAGS_NONE,
+                     -1, NULL, NULL, NULL);
+}
+
+static void
+g_signal_cb (GDBusProxy *proxy,
+             gchar      *sender_name,
+             gchar      *signal_name,
+             GVariant   *parameters,
+             gpointer    user_data)
+{
+  A11yBusLauncher *app = user_data;
+
+  if (g_strcmp0 (signal_name, "QueryEndSession") == 0)
+    respond_to_end_session (proxy);
+  else if (g_strcmp0 (signal_name, "EndSession") == 0)
+    respond_to_end_session (proxy);
+  else if (g_strcmp0 (signal_name, "Stop") == 0)
+    g_main_loop_quit (app->loop);
+}
+
+static void
+client_proxy_ready_cb (GObject      *source_object,
+                       GAsyncResult *res,
+                       gpointer      user_data)
+{
+  A11yBusLauncher *app = user_data;
+  GError *error = NULL;
+
+  app->client_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
+
+  if (error != NULL)
+    {
+      g_warning ("Failed to get a client proxy: %s", error->message);
+      g_error_free (error);
+
+      return;
+    }
+
+  g_signal_connect (app->client_proxy, "g-signal",
+                    G_CALLBACK (g_signal_cb), app);
+}
+
+static void
+register_client (A11yBusLauncher *app)
+{
+  GDBusProxyFlags flags;
+  GDBusProxy *sm_proxy;
+  GError *error;
+  const gchar *app_id;
+  const gchar *autostart_id;
+  gchar *client_startup_id;
+  GVariant *parameters;
+  GVariant *variant;
+  gchar *object_path;
+
+  flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
+          G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS;
+
+  error = NULL;
+  sm_proxy = g_dbus_proxy_new_sync (app->session_bus, flags, NULL,
+                                    "org.gnome.SessionManager",
+                                    "/org/gnome/SessionManager",
+                                    "org.gnome.SessionManager",
+                                    NULL, &error);
+
+  if (error != NULL)
+    {
+      g_warning ("Failed to get session manager proxy: %s", error->message);
+      g_error_free (error);
+
+      return;
+    }
+
+  app_id = "at-spi-bus-launcher";
+  autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
+
+  if (autostart_id != NULL)
+    {
+      client_startup_id = g_strdup (autostart_id);
+      g_unsetenv ("DESKTOP_AUTOSTART_ID");
+    }
+  else
+    {
+      client_startup_id = g_strdup ("");
+    }
+
+  parameters = g_variant_new ("(ss)", app_id, client_startup_id);
+  g_free (client_startup_id);
+
+  error = NULL;
+  variant = g_dbus_proxy_call_sync (sm_proxy,
+                                    "RegisterClient", parameters,
+                                    G_DBUS_CALL_FLAGS_NONE,
+                                    -1, NULL, &error);
+
+  g_object_unref (sm_proxy);
+
+  if (error != NULL)
+    {
+      g_warning ("Failed to register client: %s", error->message);
+      g_error_free (error);
+
+      return;
+    }
+
+  g_variant_get (variant, "(o)", &object_path);
+  g_variant_unref (variant);
+
+  flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
+  g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION, flags, NULL,
+                            "org.gnome.SessionManager", object_path,
+                            "org.gnome.SessionManager.ClientPrivate",
+                            NULL, client_proxy_ready_cb, app);
+
+  g_free (object_path);
+}
+
+static void
+name_appeared_handler (GDBusConnection *connection,
+                       const gchar     *name,
+                       const gchar     *name_owner,
+                       gpointer         user_data)
+{
+  A11yBusLauncher *app = user_data;
+
+  register_client (app);
 }
 
 /**
@@ -152,7 +286,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;
@@ -184,27 +318,46 @@ 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;
 
   if (app->a11y_bus_pid != 0)
     return FALSE;
 
-  argv[1] = g_strdup_printf ("--config-file=%s/at-spi2/accessibility.conf", SYSCONFDIR);
+  argv[1] = (char*)config_path;
 
   if (pipe (app->pipefd) < 0)
     g_error ("Failed to create pipe: %s", strerror (errno));
 
+  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))
@@ -237,6 +390,138 @@ ensure_a11y_bus (A11yBusLauncher *app)
   app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
   LOGD("a11y bus address: %s", app->a11y_bus_address);
 
+  return TRUE;
+
+error:
+  close (app->pipefd[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)
+    g_error ("Failed to create listening socket: %s", strerror (errno));
+
+  if (bind (app->listenfd, (struct sockaddr *)&addr, sizeof(sa_family_t)) < 0)
+    g_error ("Failed to bind listening socket: %s", strerror (errno));
+
+  if (getsockname (app->listenfd, (struct sockaddr *)&addr, &addr_len) < 0)
+    g_error ("Failed to get socket name for listening socket: %s", strerror(errno));
+
+  if (listen (app->listenfd, 1024) < 0)
+    g_error ("Failed to listen on socket: %s", strerror(errno));
+
+  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->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
   {
     Display *display = XOpenDisplay (NULL);
@@ -254,17 +539,7 @@ ensure_a11y_bus (A11yBusLauncher *app)
   }
 #endif
 
-  if (argv[1]) g_free(argv[1]);
-
   return TRUE;
-
- error:
-  if (argv[1]) g_free(argv[1]);
-  close (app->pipefd[0]);
-  close (app->pipefd[1]);
-  app->state = A11Y_BUS_STATE_ERROR;
-
-  return FALSE;
 }
 
 static void
@@ -342,6 +617,7 @@ handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
                                                 &builder,
                                                 &invalidated_builder),
                                  NULL);
+
   g_variant_builder_clear (&builder);
   g_variant_builder_clear (&invalidated_builder);
 }
@@ -561,8 +837,11 @@ on_name_acquired (GDBusConnection *connection,
                   const gchar     *name,
                   gpointer         user_data)
 {
-  A11yBusLauncher *app = user_data;
-  (void) app;
+  g_bus_watch_name (G_BUS_TYPE_SESSION,
+                    "org.gnome.SessionManager",
+                    G_BUS_NAME_WATCHER_FLAGS_NONE,
+                    name_appeared_handler, NULL,
+                    user_data, NULL);
 }
 
 static int sigterm_pipefd[2];
@@ -648,6 +927,17 @@ already_running ()
 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;
 
@@ -659,6 +949,7 @@ get_schema (const gchar *name)
   }
 
   return NULL;
+#endif
 }
 
 static void
@@ -824,10 +1115,26 @@ _terminate_client(A11yBusClient *client)
 void vconf_client_cb(keynode_t *node, void *user_data)
 {
    A11yBusClient *client = user_data;
-   int client_needed = vconf_keynode_get_bool(node);
-   LOGD("vconf_keynode_get_bool(node): %i", client_needed);
-   if (client_needed < 0)
-     return;
+
+   gboolean client_needed = FALSE;
+   int i;
+   for (i = 0; i < client->number_of_keys; i++) {
+      int status = 0;
+      int ret =vconf_get_bool(client->vconf_key[i], &status);
+      if (ret != 0)
+      {
+        LOGD("Could not read %s key value.\n", client->vconf_key[i]);
+        return;
+      }
+      LOGD("vconf_keynode_get_bool(node): %i", status);
+      if (status < 0)
+        return;
+
+      if (status == 1) {
+        client_needed = TRUE;
+        break;
+      }
+   }
 
    //check if process really exists (e.g didn't crash)
    if (client->pid > 0)
@@ -846,30 +1153,36 @@ void vconf_client_cb(keynode_t *node, void *user_data)
 }
 
 
-static gboolean register_client(A11yBusClient *client)
+static gboolean register_executable(A11yBusClient *client)
 {
   gboolean client_needed = FALSE;
 
-  if(!client->vconf_key) {
-         LOGE("Vconf_key missing for client: %s \n", client->vconf_key);
-         return FALSE;
-  }
+  int i;
+  for (i = 0; i < client->number_of_keys; i++) {
+    if (!client->vconf_key[i]) {
+      LOGE("Vconf_key missing for client: %d \n", i);
+      return FALSE;
+    }
 
-  int ret = vconf_get_bool(client->vconf_key, &client_needed);
-  if (ret != 0)
-       {
-         LOGD("Could not read %s key value.\n", client->vconf_key);
-         return FALSE;
-       }
-  ret = vconf_notify_key_changed(client->vconf_key, vconf_client_cb, client);
-  if(ret != 0)
-       {
-         LOGD("Could not add information level callback\n");
-         return FALSE;
-       }
+    int status = 0;
+    int ret = vconf_get_bool(client->vconf_key[i], &status);
+    if (ret != 0)
+    {
+      LOGD("Could not read %s key value.\n", client->vconf_key[i]);
+      return FALSE;
+    }
+    ret = vconf_notify_key_changed(client->vconf_key[i], vconf_client_cb, client);
+    if (ret != 0)
+    {
+      LOGD("Could not add information level callback\n");
+      return FALSE;
+    }
+    if (status)
+      client_needed = TRUE;
+  }
 
   if (client_needed)
-       g_timeout_add_seconds(2,_launch_process_repeat_until_success, client);
+  g_timeout_add_seconds(2,_launch_process_repeat_until_success, client);
   return TRUE;
 }
 
@@ -882,6 +1195,7 @@ main (int    argc,
 #endif
 
   LOGD("Starting atspi bus launcher");
+
   gboolean a11y_set = FALSE;
   gboolean screen_reader_set = FALSE;
   gint i;
@@ -898,20 +1212,23 @@ main (int    argc,
 
   _global_app->screen_reader.name = "screen-reader";
   _global_app->screen_reader.app_control_operation = APP_CONTROL_OPERATION_SCREEN_READ;
-  _global_app->screen_reader.vconf_key = VCONFKEY_SETAPPL_ACCESSIBILITY_TTS;
+  _global_app->screen_reader.vconf_key[0] = VCONFKEY_SETAPPL_ACCESSIBILITY_TTS;
+  _global_app->screen_reader.number_of_keys = 1;
 
   _global_app->universal_switch.name = "universal-switch";
   _global_app->universal_switch.app_control_operation = APP_CONTROL_OPERATION_UNIVERSAL_SWITCH;
-  _global_app->universal_switch.vconf_key = VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH;
+  _global_app->universal_switch.vconf_key[0] = VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_CONFIGURATION_SERVICE;
+  _global_app->universal_switch.vconf_key[1] = VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE;
+  _global_app->universal_switch.number_of_keys = 2;
 
   for (i = 1; i < argc; i++)
     {
       if (!strcmp (argv[i], "--launch-immediately"))
         _global_app->launch_immediately = TRUE;
-      else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 2)
+      else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 1)
         a11y_set = TRUE;
       else if (sscanf (argv[i], "--screen-reader=%d",
-                       &_global_app->screen_reader_enabled) == 2)
+                       &_global_app->screen_reader_enabled) == 1)
         screen_reader_set = TRUE;
     else
       g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
@@ -958,8 +1275,8 @@ main (int    argc,
                                   _global_app,
                                   NULL);
 
-  register_client (&_global_app->screen_reader);
-  register_client (&_global_app->universal_switch);
+  register_executable (&_global_app->screen_reader);
+  register_executable (&_global_app->universal_switch);
 
   g_main_loop_run (_global_app->loop);