BGO#652797: Remove unused AtkMisc instance
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / bridge.c
index 8bad17c..51b12bc 100644 (file)
@@ -22,8 +22,8 @@
  * Boston, MA 02111-1307, USA.
  */
 
+#define _GNU_SOURCE
 #include "config.h"
-#include "dbus/dbus-glib-lowlevel.h"
 
 #include <X11/Xlib.h>
 #include <X11/Xatom.h>
@@ -38,6 +38,7 @@
 #include <droute/droute.h>
 #include <gmodule.h>
 #include <glib/gi18n.h>
+#include <atspi/atspi.h>
 
 #include "bridge.h"
 #include "event.h"
@@ -55,8 +56,6 @@
 
 SpiBridge *spi_global_app_data = NULL;
 
-static const AtkMisc *atk_misc = NULL;
-
 /*static Display *bridge_display = NULL;*/
 
 /*---------------------------------------------------------------------------*/
@@ -100,78 +99,6 @@ spi_display_name (void)
 
 /*---------------------------------------------------------------------------*/
 
-/*
- * Gets the IOR from the XDisplay.
- * Not currently used in D-Bus version, but something similar
- * may be employed in the future for accessing the registry daemon
- * bus name.
- */
-
-static DBusConnection *
-spi_atk_bridge_get_bus (void)
-{
-  Atom AT_SPI_BUS;
-  Atom actual_type;
-  Display *bridge_display;
-  int actual_format;
-  unsigned char *data = NULL;
-  unsigned long nitems;
-  unsigned long leftover;
-
-  DBusConnection *bus = NULL;
-  DBusError error;
-
-  bridge_display = XOpenDisplay (spi_display_name ());
-  if (!bridge_display)
-    {
-      g_warning ("AT_SPI: Could not get the display\n");
-      return NULL;
-    }
-
-  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);
-
-  dbus_error_init (&error);
-
-  if (data == NULL)
-    {
-      g_warning
-        ("AT-SPI: Accessibility bus not found - Using session bus.\n");
-      bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
-      if (!bus)
-        {
-          g_warning ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
-          return NULL;
-        }
-    }
-  else
-    {
-      bus = dbus_connection_open (data, &error);
-      XFree (data);
-      if (!bus)
-        {
-          g_warning ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
-          return NULL;
-        }
-      else
-        {
-          if (!dbus_bus_register (bus, &error))
-            {
-              g_warning ("AT-SPI: Couldn't register with bus: %s\n", error.message);
-              return NULL;
-            }
-          dbus_connection_set_exit_on_disconnect (bus, FALSE);
-        }
-    }
-
-  return bus;
-}
-
 static void
 set_reply (DBusPendingCall *pending, void *user_data)
 {
@@ -508,13 +435,56 @@ install_plug_hooks ()
   socket_class->embed = socket_embed_hook;
 }
 
+static uint
+get_ancestral_uid (uint pid)
+{
+  FILE *fp;
+  char buf [80];
+  int ppid = 0;
+  int uid = 0;
+  gboolean got_ppid = 0;
+  gboolean got_uid = 0;
+
+  sprintf (buf, "/proc/%d/status", pid);
+  fp = fopen (buf, "r");
+  if (!fp)
+    return 0;
+  while ((!got_ppid || !got_uid) && fgets (buf, sizeof (buf), fp))
+  {
+    if (sscanf (buf, "PPid:\t%d", &ppid) == 1)
+      got_ppid = TRUE;
+    else if (sscanf (buf, "Uid:\t%d", &uid) == 1)
+      got_uid = TRUE;
+  }
+  fclose (fp);
+
+  if (!got_ppid || !got_uid)
+    return 0;
+  if (uid != 0)
+    return uid;
+  if (ppid == 0 || ppid == 1)
+    return 0;
+  return get_ancestral_uid (ppid);
+}
+
+static dbus_bool_t
+user_check (DBusConnection *bus, unsigned long uid)
+{
+  if (uid == getuid () || uid == geteuid ())
+    return TRUE;
+  if (getuid () == 0)
+    return get_ancestral_uid (getpid ()) == uid;
+  return FALSE;
+}
+
 static void
 new_connection_cb (DBusServer *server, DBusConnection *con, void *data)
 {
   GList *new_list;
 
+  dbus_connection_set_unix_user_function (con, user_check, NULL, NULL);
   dbus_connection_ref(con);
-  dbus_connection_setup_with_g_main(con, NULL);
+  atspi_dbus_connection_setup_with_g_main(con, NULL);
   droute_intercept_dbus (con);
   droute_context_register (spi_global_app_data->droute, con);
 
@@ -542,7 +512,7 @@ setup_bus (void)
     return -1;
   }
 
-  dbus_server_setup_with_g_main(server, NULL);
+  atspi_dbus_server_setup_with_g_main(server, NULL);
   dbus_server_set_new_connection_function(server, new_connection_cb, NULL, NULL);
 
   spi_global_app_data->server = server;
@@ -707,12 +677,11 @@ adaptor_init (gint * argc, gchar ** argv[])
 
   /* Allocate global data and do ATK initializations */
   spi_global_app_data = g_new0 (SpiBridge, 1);
-  atk_misc = atk_misc_get_instance ();
   spi_global_app_data->root = g_object_ref (root);
 
   /* Set up D-Bus connection and register bus name */
   dbus_error_init (&error);
-  spi_global_app_data->bus = spi_atk_bridge_get_bus ();
+  spi_global_app_data->bus = atspi_get_a11y_bus ();
   if (!spi_global_app_data->bus)
     {
       g_free (spi_global_app_data);
@@ -736,13 +705,9 @@ adaptor_init (gint * argc, gchar ** argv[])
         }
     }
 
-#ifndef DISABLE_P2P
   spi_global_app_data->main_context = g_main_context_new ();
-#else
-  spi_global_app_data->main_context = NULL;
-#endif
 
-  dbus_connection_setup_with_g_main (spi_global_app_data->bus, NULL);
+  atspi_dbus_connection_setup_with_g_main (spi_global_app_data->bus, NULL);
 
   /* Hook our plug-and socket functions */
   install_plug_hooks ();