[PATCH] avoid race condition in gvfs monitors when multiple users logged in




When multiple users logged in and have a gvfs monitros running,
inserting a media casuses a mounting for rundom user.
This patch implemets quering logind about session state and blocks
signals emiting when session is not active.
---
 monitor/proxy/gvfsproxyvolumemonitordaemon.c | 77 +++++++++++++++++++++++++++-
 1 file changed, 76 insertions(+), 1 deletion(-)

diff --git a/monitor/proxy/gvfsproxyvolumemonitordaemon.c b/monitor/proxy/gvfsproxyvolumemonitordaemon.c
index 8e65d9f..1c422bb 100644
--- a/monitor/proxy/gvfsproxyvolumemonitordaemon.c
+++ b/monitor/proxy/gvfsproxyvolumemonitordaemon.c
@@ -44,6 +44,8 @@ static GHashTable *unique_names_being_watched = NULL;
 
 static GVfsRemoteVolumeMonitor *monitor_daemon = NULL;
 
+static GDBusProxy *session_active_proxy = NULL;
+
 #define DEBUG_ENABLED
 
 #ifdef DEBUG_ENABLED
@@ -1745,7 +1747,30 @@ emit_signal (GVfsRemoteVolumeMonitor *instance, MonitorSignalFunc signal_func, v
 {
   char *id;
   GVariant *val;
-  
+  GVariant *res, *inner;
+  GError *error = NULL;
+  gchar *state;
+
+  res = g_dbus_proxy_call_sync (session_active_proxy , "Get", 
+                            g_variant_new( "(ss)", 
+                                   "org.freedesktop.login1.Session",
+                                   "State"),
+                            G_DBUS_CALL_FLAGS_NONE,
+                            -1,
+                            NULL,
+                            &error
+                            );
+
+
+  if (error == NULL && res != NULL) {
+        g_variant_get(res, "(v)", &inner );
+        state = g_variant_get_string(inner,NULL);
+  } else if (error != NULL ) {
+    print_debug ("Error in dbus - %s", error->message);
+    g_error_free (error);
+  }
+  if ( g_strcmp0(state,"active") == 0 ){
+
   print_debug ("emit_signal: %p", object);
   
   id = g_strdup_printf ("%p", object);
@@ -1754,6 +1779,11 @@ emit_signal (GVfsRemoteVolumeMonitor *instance, MonitorSignalFunc signal_func, v
   signal_func (instance, the_dbus_name, id, val);
 
   g_free (id);
+  }
+  else  print_debug ("inactive session\n");
+  g_variant_unref (inner);
+  g_variant_unref (res);
+
 }
 
 static void
@@ -1975,6 +2005,10 @@ g_vfs_proxy_volume_monitor_daemon_main (int argc,
                                         GType volume_monitor_type)
 {
   guint name_owner_id;
+  GDBusProxy *logind_proxy;
+  GVariant *res;
+  GError *error = NULL;
+  gchar *session_obj_path;
 
   name_owner_id = 0;
 
@@ -1989,6 +2023,47 @@ g_vfs_proxy_volume_monitor_daemon_main (int argc,
   the_dbus_name = dbus_name;
   unique_names_being_watched = g_hash_table_new_full (g_str_hash, g_int_equal, g_free, NULL);
 
+  logind_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
+                NULL,
+                "org.freedesktop.login1",
+                "/org/freedesktop/login1",
+                "org.freedesktop.login1.Manager",
+                NULL,
+                &error );
+  if (logind_proxy == NULL) {
+    print_debug("Error creating proxy - %s", error->message);
+    g_error_free (error);
+  }
+  res = g_dbus_proxy_call_sync (logind_proxy, "GetSessionByPID",
+                 g_variant_new( "(u)", getpid() ),
+                 G_DBUS_CALL_FLAGS_NONE,
+                 -1,
+                 NULL,
+                 &error
+                 );
+  if (error == NULL && res != NULL) {
+        g_variant_get(res,"(o)", &session_obj_path);
+  }
+  else {
+    print_debug( "Error getting session - %s", error->message);
+  }
+
+   g_object_unref(logind_proxy);
+session_active_proxy =  g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
+                NULL,
+                "org.freedesktop.login1",
+                session_obj_path,
+                "org.freedesktop.DBus.Properties",
+                NULL,
+                &error );
+  if (session_active_proxy == NULL) {
+         g_error_free (error);
+  }
+
+
+
   /* try and create the monitor */
   monitor_try_create ();
 
-- 
1.8.1.6



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]