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