/** * udisks_daemon_util_on_user_seat: * @daemon: A #UDisksDaemon. * @object: The #GDBusObject that the call is on or %NULL. * @user: The user to check for. * * Checks whether the device represented by @object (if any) is plugged into * a seat where the caller represented by @user is logged in and active. * * This works if @object is a drive or a block object. * * Returns: %TRUE if @object is on the same seat as one of @user's * active sessions, %FALSE otherwise. */ gboolean udisks_daemon_util_on_user_seat (UDisksDaemon *daemon, UDisksObject *object, uid_t user) { #if !defined(HAVE_LIBSYSTEMD_LOGIN) /* if we don't have systemd, assume it's always the same seat */ return TRUE; #else gboolean ret = FALSE; char *session = NULL; char *seat = NULL; const gchar *drive_seat; UDisksObject *drive_object = NULL; UDisksDrive *drive = NULL; /* if we don't have logind, assume it's always the same seat */ if (!LOGIND_AVAILABLE()) return TRUE; if (UDISKS_IS_LINUX_BLOCK_OBJECT (object)) { UDisksLinuxBlockObject *linux_block_object; UDisksBlock *block; linux_block_object = UDISKS_LINUX_BLOCK_OBJECT (object); block = udisks_object_get_block (UDISKS_OBJECT (linux_block_object)); if (block != NULL) { drive_object = udisks_daemon_find_object (daemon, udisks_block_get_drive (block)); g_object_unref (block); } } else if (UDISKS_IS_LINUX_DRIVE_OBJECT (object)) { drive_object = g_object_ref (object); } if (drive_object == NULL) goto out; drive = udisks_object_get_drive (UDISKS_OBJECT (drive_object)); if (drive == NULL) goto out; drive_seat = udisks_drive_get_seat (drive); if (drive_seat != NULL && sd_uid_is_on_seat (user, TRUE, drive_seat) > 0) { ret = TRUE; goto out; } out: free (seat); free (session); g_clear_object (&drive_object); g_clear_object (&drive); return ret; #endif /* HAVE_LIBSYSTEMD_LOGIN */ }
/** * udisks_daemon_util_on_same_seat: * @daemon: A #UDisksDaemon. * @object: The #GDBusObject that the call is on or %NULL. * @process: The process to check for. * * Checks whether the device represented by @object (if any) is plugged into * a seat where the caller represented by @process is logged in. * * This works if @object is a drive or a block object. * * Returns: %TRUE if @object and @process is on the same seat, %FALSE otherwise. */ gboolean udisks_daemon_util_on_same_seat (UDisksDaemon *daemon, UDisksObject *object, pid_t process) { #if !defined(HAVE_LIBSYSTEMD_LOGIN) /* if we don't have systemd, assume it's always the same seat */ return TRUE; #else gboolean ret = FALSE; char *session = NULL; char *seat = NULL; const gchar *drive_seat; UDisksObject *drive_object = NULL; UDisksDrive *drive = NULL; /* if we don't have logind, assume it's always the same seat */ if (!LOGIND_AVAILABLE()) return TRUE; if (UDISKS_IS_LINUX_BLOCK_OBJECT (object)) { UDisksLinuxBlockObject *linux_block_object; UDisksBlock *block; linux_block_object = UDISKS_LINUX_BLOCK_OBJECT (object); block = udisks_object_get_block (UDISKS_OBJECT (linux_block_object)); if (block != NULL) { drive_object = udisks_daemon_find_object (daemon, udisks_block_get_drive (block)); g_object_unref (block); } } else if (UDISKS_IS_LINUX_DRIVE_OBJECT (object)) { drive_object = g_object_ref (object); } if (drive_object == NULL) goto out; drive = udisks_object_get_drive (UDISKS_OBJECT (drive_object)); if (drive == NULL) goto out; /* It's not unexpected to not find a session, nor a seat associated with @process */ if (sd_pid_get_session (process, &session) == 0) sd_session_get_seat (session, &seat); /* If we don't know the seat of the caller, we assume the device is always on another seat */ if (seat == NULL) goto out; drive_seat = udisks_drive_get_seat (drive); if (g_strcmp0 (seat, drive_seat) == 0) { ret = TRUE; goto out; } out: free (seat); free (session); g_clear_object (&drive_object); g_clear_object (&drive); return ret; #endif /* HAVE_LIBSYSTEMD_LOGIN */ }