Blob Blame Raw
From 5c9ffd7bf5d08c3fe57e235d8001e0b9c23a2730 Mon Sep 17 00:00:00 2001
From: David Zeuthen <davidz@redhat.com>
Date: Sat, 21 Apr 2012 12:28:09 -0400
Subject: [PATCH 1/9] Don't set should_automount to TRUE for devices on other seats

This change uses the libsystemd-login library to obtain the seat we're
on and compares it against the Seat property on the D-Bus interface
org.freedesktop.UDisks2.Drive. This property is available in udisks
1.95.0, see

 http://cgit.freedesktop.org/udisks/commit/?id=91106cdc7622d9674f6083dcb524407f026a36c7

Also, since we're still on the stable branch and not everyone may be
using systemd, make this work without hard-requiring either
libsystemd-login or udisks 1.95.0.

Signed-off-by: David Zeuthen <davidz@redhat.com>
---
 configure.ac                        |   22 ++++++
 monitor/udisks2/Makefile.am         |    2 +
 monitor/udisks2/gvfsudisks2volume.c |  125 ++++++++++++++++++++++++++++-------
 3 files changed, 124 insertions(+), 25 deletions(-)

diff --git a/configure.ac b/configure.ac
index 3b77b64..361b0d4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -239,6 +239,27 @@ fi
 
 AM_CONDITIONAL(USE_UDISKS2, [test "$msg_udisks2" = "yes"])
 
+dnl **********************************
+dnl *** Check for libsystemd-login ***
+dnl **********************************
+
+AC_ARG_ENABLE(libsystemd_login, AS_HELP_STRING([--disable-libsystemd-login],[build without liblibsystemd-login]))
+msg_libsystemd_login=no
+LIBSYSTEMD_LOGIN_LIBS=
+LIBSYSTEMD_LOGIN_CFLAGS=
+LIBSYSTEMD_LOGIN_REQUIRED=44
+
+if test "x$enable_libsystemd_login" != "xno"; then
+  PKG_CHECK_EXISTS([libsystemd-login >= $LIBSYSTEMD_LOGIN_REQUIRED], msg_libsystemd_login=yes)
+
+  if test "x$msg_libsystemd_login" = "xyes"; then
+    PKG_CHECK_MODULES([LIBSYSTEMD_LOGIN],[libsystemd-login >= $LIBSYSTEMD_LOGIN_REQUIRED])
+    AC_DEFINE(HAVE_LIBSYSTEMD_LOGIN, 1, [Define to 1 if liblibsystemd_login is available])
+  fi
+fi
+
+AM_CONDITIONAL(USE_LIBSYSTEMD_LOGIN, [test "$msg_libsystemd_login" = "yes"])
+
 dnl **********************
 dnl *** Check for HAL ***
 dnl **********************
@@ -783,6 +804,7 @@ echo "
 	Build HAL volume monitor:     $msg_hal (with fast init path: $have_hal_fast_init)
 	Build GDU volume monitor:     $msg_gdu
 	Build udisks2 volume monitor: $msg_udisks2
+        Use libsystem-login:          $msg_libsystemd_login
 	GNOME Keyring support:        $msg_keyring
 	Bash-completion support:      $msg_bash_completion
 "
diff --git a/monitor/udisks2/Makefile.am b/monitor/udisks2/Makefile.am
index f919df6..776f670 100644
--- a/monitor/udisks2/Makefile.am
+++ b/monitor/udisks2/Makefile.am
@@ -19,6 +19,7 @@ gvfs_udisks2_volume_monitor_CFLAGS =		\
 	$(GLIB_CFLAGS)                          \
 	$(UDISKS2_CFLAGS)                       \
 	$(GUDEV_CFLAGS)                         \
+	$(LIBSYSTEMD_LOGIN_CFLAGS)		\
 	-DGIO_MODULE_DIR=\"$(GIO_MODULE_DIR)\"	\
 	-DGVFS_LOCALEDIR=\""$(localedir)"\"	\
 	-DG_DISABLE_DEPRECATED			\
@@ -32,6 +33,7 @@ gvfs_udisks2_volume_monitor_LDADD  =		     			      	\
 	$(GLIB_LIBS)                                 			      	\
 	$(UDISKS2_LIBS)                                  			\
 	$(GUDEV_LIBS)                                  			      	\
+	$(LIBSYSTEMD_LOGIN_LIBS)						\
 	$(top_builddir)/common/libgvfscommon.la 			      	\
 	$(top_builddir)/monitor/proxy/libgvfsproxyvolumemonitordaemon-noin.la 	\
 	$(NULL)
diff --git a/monitor/udisks2/gvfsudisks2volume.c b/monitor/udisks2/gvfsudisks2volume.c
index 89c2413..243dc14 100644
--- a/monitor/udisks2/gvfsudisks2volume.c
+++ b/monitor/udisks2/gvfsudisks2volume.c
@@ -36,6 +36,41 @@
 #include "gvfsudisks2mount.h"
 #include "gvfsudisks2utils.h"
 
+
+#if defined(HAVE_LIBSYSTEMD_LOGIN)
+#include <systemd/sd-login.h>
+
+static const gchar *
+get_seat (void)
+{
+  static gsize once = 0;
+  static char *seat = NULL;
+
+  if (g_once_init_enter (&once))
+    {
+      char *session = NULL;
+      if (sd_pid_get_session (getpid (), &session) == 0)
+        {
+          sd_session_get_seat (session, &seat);
+          /* we intentionally leak seat here... */
+        }
+      g_once_init_leave (&once, (gsize) 1);
+    }
+  return seat;
+}
+
+#else
+
+static const gchar *
+get_seat (void)
+{
+  return NULL;
+}
+
+#endif
+
+
+
 typedef struct _GVfsUDisks2VolumeClass GVfsUDisks2VolumeClass;
 
 struct _GVfsUDisks2VolumeClass
@@ -178,6 +213,43 @@ apply_options_from_fstab (GVfsUDisks2Volume *volume,
 }
 
 static gboolean
+drive_is_on_our_seat (UDisksDrive *drive)
+{
+  gboolean ret = FALSE;
+  const gchar *seat;
+  const gchar *drive_seat = NULL;
+
+  /* assume our own seat if we don't have seat-support or it doesn't work */
+  seat = get_seat ();
+  if (seat == NULL)
+    {
+      ret = TRUE;
+      goto out;
+    }
+
+  /* Assume seat0 if a) device is not tagged; or b) udisks does not
+   * have seat-support.
+   *
+   * Note that seat support was added in udisks 1.95.0 (and so was the
+   * UDISKS_CHECK_VERSION macro) - for now, be compatible with older
+   * versions instead of bumping requirement in configure.ac
+   */
+#ifdef UDISKS_CHECK_VERSION
+# if UDISKS_CHECK_VERSION(1,95,0)
+  drive_seat = udisks_drive_get_seat (drive);
+# endif
+#endif
+  if (drive_seat == NULL || strlen (drive_seat) == 0)
+    drive_seat = "seat0";
+
+  if (g_strcmp0 (seat, drive_seat) == 0)
+    ret = TRUE;
+
+ out:
+  return ret;
+}
+
+static gboolean
 update_volume (GVfsUDisks2Volume *volume)
 {
   gboolean changed;
@@ -314,35 +386,38 @@ update_volume (GVfsUDisks2Volume *volume)
           if (media_icon != NULL)
             g_object_unref (media_icon);
 
-          /* Only automount filesystems from drives of known types/interconnects:
-           *
-           *  - USB
-           *  - Firewire
-           *  - sdio
-           *  - optical discs
-           *
-           * The mantra here is "be careful" - we really don't want to
-           * automount filesystems from all devices in a SAN etc - We
-           * REALLY need to be CAREFUL here.
-           *
-           * Fortunately udisks provides a property just for this.
-           */
-          if (udisks_block_get_hint_auto (volume->block))
+          /* Only automount drives attached to the same seat as we're running on */
+          if (drive_is_on_our_seat (udisks_drive))
             {
-              gboolean just_plugged_in = FALSE;
-              /* Also, if a volume (partition) appear _much later_ than when media was inserted it
-               * can only be because the media was repartitioned. We don't want to automount
-               * such volumes. So only mark volumes appearing just after their drive.
+              /* Only automount filesystems from drives of known types/interconnects:
                *
-               * There's a catch here - if the volume was discovered at coldplug-time (typically
-               * when the user desktop session started), we can't use this heuristic
+               *  - USB
+               *  - Firewire
+               *  - sdio
+               *  - optical discs
+               *
+               * The mantra here is "be careful" - we really don't want to
+               * automount filesystems from all devices in a SAN etc - We
+               * REALLY need to be CAREFUL here.
+               *
+               * Fortunately udisks provides a property just for this.
                */
-              if (g_get_real_time () - udisks_drive_get_time_media_detected (udisks_drive) < 5 * G_USEC_PER_SEC)
-                just_plugged_in = TRUE;
-              if (volume->coldplug || just_plugged_in)
-                volume->should_automount = TRUE;
+              if (udisks_block_get_hint_auto (volume->block))
+                {
+                  gboolean just_plugged_in = FALSE;
+                  /* Also, if a volume (partition) appear _much later_ than when media was inserted it
+                   * can only be because the media was repartitioned. We don't want to automount
+                   * such volumes. So only mark volumes appearing just after their drive.
+                   *
+                   * There's a catch here - if the volume was discovered at coldplug-time (typically
+                   * when the user desktop session started), we can't use this heuristic
+                   */
+                  if (g_get_real_time () - udisks_drive_get_time_media_detected (udisks_drive) < 5 * G_USEC_PER_SEC)
+                    just_plugged_in = TRUE;
+                  if (volume->coldplug || just_plugged_in)
+                    volume->should_automount = TRUE;
+                }
             }
-
           g_object_unref (udisks_drive);
         }
 
-- 
1.7.3.4