From 594e733c9344175ea05f5002468679d798be5eba Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Wed, 4 Nov 2015 11:23:03 +0100 Subject: [PATCH 2/3] Add support for mount's remount option. Allow filesystems being remounted via udisksctl. --- src/udiskslinuxfilesystem.c | 158 ++++++++++++++++++++++++++++++++------------ 1 file changed, 116 insertions(+), 42 deletions(-) diff --git a/src/udiskslinuxfilesystem.c b/src/udiskslinuxfilesystem.c index c88d7ed..d9093f0 100644 --- a/src/udiskslinuxfilesystem.c +++ b/src/udiskslinuxfilesystem.c @@ -328,7 +328,7 @@ static const gchar *exfat_allow_gid_self[] = { "gid=", NULL }; /* ------------------------------------------------ */ /* TODO: support context= */ -static const gchar *any_allow[] = { "exec", "noexec", "nodev", "nosuid", "atime", "noatime", "nodiratime", "ro", "rw", "sync", "dirsync", NULL }; +static const gchar *any_allow[] = { "exec", "noexec", "nodev", "nosuid", "atime", "noatime", "nodiratime", "ro", "rw", "sync", "dirsync", "remount", NULL }; static const FSMountOptions fs_mount_options[] = { @@ -1035,6 +1035,28 @@ has_option (const gchar *options, } static gboolean +has_variant_option (GVariant *options, + const gchar *option) +{ + const gchar *option_string; + gboolean ret; + + option_string = NULL; + ret = FALSE; + if (g_variant_lookup (options, + "options", + "&s", &option_string)) + { + if (has_option (option_string, option)) + { + ret = TRUE; + } + } + + return ret; +} + +static gboolean is_in_fstab (UDisksBlock *block, const gchar *fstab_path, gchar **out_mount_point, @@ -1172,6 +1194,7 @@ handle_mount (UDisksFilesystem *filesystem, const gchar *message; gboolean system_managed; gchar *escaped_device = NULL; + gboolean remount_requested; object = NULL; error_message = NULL; @@ -1184,6 +1207,7 @@ handle_mount (UDisksFilesystem *filesystem, escaped_mount_point_to_use = NULL; caller_user_name = NULL; system_managed = FALSE; + remount_requested = FALSE; /* only allow a single call at a time */ g_mutex_lock (&UDISKS_LINUX_FILESYSTEM (filesystem)->lock); @@ -1206,27 +1230,49 @@ handle_mount (UDisksFilesystem *filesystem, system_managed = TRUE; } + /* Check if remount requested. */ + if (has_variant_option (options, "remount")) + { + remount_requested = TRUE; + } + /* First, fail if the device is already mounted */ existing_mount_points = udisks_filesystem_get_mount_points (filesystem); if (existing_mount_points != NULL && g_strv_length ((gchar **) existing_mount_points) > 0) { - GString *str; - guint n; - str = g_string_new (NULL); - for (n = 0; existing_mount_points[n] != NULL; n++) + if (!remount_requested) { - if (n > 0) - g_string_append (str, ", "); - g_string_append_printf (str, "`%s'", existing_mount_points[n]); + GString *str; + guint n; + str = g_string_new (NULL); + for (n = 0; existing_mount_points[n] != NULL; n++) + { + if (n > 0) + g_string_append (str, ", "); + g_string_append_printf (str, "`%s'", existing_mount_points[n]); + } + g_dbus_method_invocation_return_error (invocation, + UDISKS_ERROR, + UDISKS_ERROR_ALREADY_MOUNTED, + "Device %s is already mounted at %s.\n", + udisks_block_get_device (block), + str->str); + g_string_free (str, TRUE); + goto out; + } + } + /* Fail if the device is not mounted and remount requested. */ + else + { + if (remount_requested) + { + g_dbus_method_invocation_return_error (invocation, + UDISKS_ERROR, + UDISKS_ERROR_NOT_MOUNTED, + "Device %s is not mounted.\n", + udisks_block_get_device (block)); + goto out; } - g_dbus_method_invocation_return_error (invocation, - UDISKS_ERROR, - UDISKS_ERROR_ALREADY_MOUNTED, - "Device %s is already mounted at %s.\n", - udisks_block_get_device (block), - str->str); - g_string_free (str, TRUE); - goto out; } error = NULL; @@ -1473,32 +1519,60 @@ handle_mount (UDisksFilesystem *filesystem, escaped_device = udisks_daemon_util_escape_and_quote (udisks_block_get_device (block)); /* run mount(8) */ - if (!udisks_daemon_launch_spawned_job_sync (daemon, - object, - "filesystem-mount", caller_uid, - NULL, /* GCancellable */ - 0, /* uid_t run_as_uid */ - 0, /* uid_t run_as_euid */ - NULL, /* gint *out_status */ - &error_message, - NULL, /* input_string */ - "mount -t %s -o %s %s %s", - escaped_fs_type_to_use, - escaped_mount_options_to_use, - escaped_device, - escaped_mount_point_to_use)) - { - /* ugh, something went wrong.. we need to clean up the created mount point */ - if (g_rmdir (mount_point_to_use) != 0) - udisks_warning ("Error removing directory %s: %m", mount_point_to_use); - g_dbus_method_invocation_return_error (invocation, - UDISKS_ERROR, - UDISKS_ERROR_FAILED, - "Error mounting %s at %s: %s", - udisks_block_get_device (block), - mount_point_to_use, - error_message); - goto out; + if (!remount_requested) + { + if (!udisks_daemon_launch_spawned_job_sync (daemon, + object, + "filesystem-mount", caller_uid, + NULL, /* GCancellable */ + 0, /* uid_t run_as_uid */ + 0, /* uid_t run_as_euid */ + NULL, /* gint *out_status */ + &error_message, + NULL, /* input_string */ + "mount -t %s -o %s %s %s", + escaped_fs_type_to_use, + escaped_mount_options_to_use, + escaped_device, + escaped_mount_point_to_use)) + { + /* ugh, something went wrong.. we need to clean up the created mount point */ + if (g_rmdir (mount_point_to_use) != 0) + udisks_warning ("Error removing directory %s: %m", mount_point_to_use); + g_dbus_method_invocation_return_error (invocation, + UDISKS_ERROR, + UDISKS_ERROR_FAILED, + "Error mounting %s at %s: %s", + udisks_block_get_device (block), + mount_point_to_use, + error_message); + goto out; + } + } + else + { + if (!udisks_daemon_launch_spawned_job_sync (daemon, + object, + "filesystem-mount", caller_uid, + NULL, /* GCancellable */ + 0, /* uid_t run_as_uid */ + 0, /* uid_t run_as_euid */ + NULL, /* gint *out_status */ + &error_message, + NULL, /* input_string */ + "mount -t %s -o %s %s", + escaped_fs_type_to_use, + escaped_mount_options_to_use, + escaped_device)) + { + g_dbus_method_invocation_return_error (invocation, + UDISKS_ERROR, + UDISKS_ERROR_FAILED, + "Error remounting %s: %s", + mount_point_to_use, + error_message); + goto out; + } } /* update the mounted-fs file */ -- 2.4.0