diff options
author | David Zeuthen <davidz@redhat.com> | 2011-08-01 13:08:55 -0400 |
---|---|---|
committer | David Zeuthen <davidz@redhat.com> | 2011-08-01 13:08:55 -0400 |
commit | d65be9ca5209dfbf4cbf6e25eb82918ef0549a63 (patch) | |
tree | 1eb0e94ba251681ca139d924f2bc2ea1cfdab58d | |
parent | 24446e0a7a6d158961cb3e887630e1ce425a8ac5 (diff) |
Add files missed in previous commit
Signed-off-by: David Zeuthen <davidz@redhat.com>
-rw-r--r-- | src/udiskslinuxswapspace.c | 224 | ||||
-rw-r--r-- | src/udiskslinuxswapspace.h | 37 |
2 files changed, 261 insertions, 0 deletions
diff --git a/src/udiskslinuxswapspace.c b/src/udiskslinuxswapspace.c new file mode 100644 index 0000000..5cf5e97 --- /dev/null +++ b/src/udiskslinuxswapspace.c @@ -0,0 +1,224 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2007-2010 David Zeuthen <zeuthen@gmail.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "config.h" +#include <glib/gi18n-lib.h> + +#include <sys/types.h> +#include <pwd.h> +#include <grp.h> +#include <string.h> +#include <stdlib.h> + +#include <glib/gstdio.h> + +#include "udiskslogging.h" +#include "udiskslinuxswapspace.h" +#include "udiskslinuxblock.h" +#include "udisksdaemon.h" +#include "udiskscleanup.h" +#include "udisksdaemonutil.h" + +/** + * SECTION:udiskslinuxswapspace + * @title: UDisksLinuxSwapspace + * @short_description: Swapspace manipulation on Linux + * + * This type provides an implementation of the #UDisksSwapspace interface + * on Linux. + */ + +typedef struct _UDisksLinuxSwapspaceClass UDisksLinuxSwapspaceClass; + +/** + * UDisksLinuxSwapspace: + * + * The #UDisksLinuxSwapspace structure contains only private data and should + * only be accessed using the provided API. + */ +struct _UDisksLinuxSwapspace +{ + UDisksSwapspaceSkeleton parent_instance; +}; + +struct _UDisksLinuxSwapspaceClass +{ + UDisksSwapspaceSkeletonClass parent_class; +}; + +static void swapspace_iface_init (UDisksSwapspaceIface *iface); + +G_DEFINE_TYPE_WITH_CODE (UDisksLinuxSwapspace, udisks_linux_swapspace, UDISKS_TYPE_SWAPSPACE_SKELETON, + G_IMPLEMENT_INTERFACE (UDISKS_TYPE_SWAPSPACE, swapspace_iface_init)); + +/* ---------------------------------------------------------------------------------------------------- */ + +static void +udisks_linux_swapspace_init (UDisksLinuxSwapspace *swapspace) +{ + g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (swapspace), + G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD); +} + +static void +udisks_linux_swapspace_class_init (UDisksLinuxSwapspaceClass *klass) +{ +} + +/** + * udisks_linux_swapspace_new: + * + * Creates a new #UDisksLinuxSwapspace instance. + * + * Returns: A new #UDisksLinuxSwapspace. Free with g_object_unref(). + */ +UDisksSwapspace * +udisks_linux_swapspace_new (void) +{ + return UDISKS_SWAPSPACE (g_object_new (UDISKS_TYPE_LINUX_SWAPSPACE, + NULL)); +} + +/* ---------------------------------------------------------------------------------------------------- */ + + +static void +swapspace_start_on_job_completed (UDisksJob *job, + gboolean success, + const gchar *message, + gpointer user_data) +{ + GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (user_data); + UDisksSwapspace *swapspace; + swapspace = UDISKS_SWAPSPACE (g_dbus_method_invocation_get_user_data (invocation)); + if (success) + udisks_swapspace_complete_start (swapspace, invocation); + else + g_dbus_method_invocation_return_error (invocation, + UDISKS_ERROR, + UDISKS_ERROR_FAILED, + "Error activating swap: %s", + message); +} + +static gboolean +handle_start (UDisksSwapspace *swapspace, + GDBusMethodInvocation *invocation, + GVariant *options) +{ + UDisksObject *object; + UDisksDaemon *daemon; + UDisksBlockDevice *block; + UDisksBaseJob *job; + + object = UDISKS_OBJECT (g_dbus_interface_get_object (G_DBUS_INTERFACE (swapspace))); + daemon = udisks_linux_block_get_daemon (UDISKS_LINUX_BLOCK (object)); + block = udisks_object_peek_block_device (object); + + if (!udisks_daemon_util_check_authorization_sync (daemon, + object, + "org.freedesktop.udisks2.manage-swapspace", + options, + N_("Authentication is required to activate swapspace on $(udisks2.device)"), + invocation)) + goto out; + + job = udisks_daemon_launch_spawned_job (daemon, + NULL, /* cancellable */ + NULL, /* input_string */ + "swapon %s", + udisks_block_device_get_device (block)); + g_signal_connect (job, + "completed", + G_CALLBACK (swapspace_start_on_job_completed), + invocation); + + out: + return TRUE; +} + +static void +swapspace_stop_on_job_completed (UDisksJob *job, + gboolean success, + const gchar *message, + gpointer user_data) +{ + GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (user_data); + UDisksSwapspace *swapspace; + swapspace = UDISKS_SWAPSPACE (g_dbus_method_invocation_get_user_data (invocation)); + if (success) + udisks_swapspace_complete_start (swapspace, invocation); + else + g_dbus_method_invocation_return_error (invocation, + UDISKS_ERROR, + UDISKS_ERROR_FAILED, + "Error deactivating swap: %s", + message); +} + +static gboolean +handle_stop (UDisksSwapspace *swapspace, + GDBusMethodInvocation *invocation, + GVariant *options) +{ + UDisksObject *object; + UDisksDaemon *daemon; + UDisksBlockDevice *block; + UDisksBaseJob *job; + + object = UDISKS_OBJECT (g_dbus_interface_get_object (G_DBUS_INTERFACE (swapspace))); + daemon = udisks_linux_block_get_daemon (UDISKS_LINUX_BLOCK (object)); + block = udisks_object_peek_block_device (object); + + /* Now, check that the user is actually authorized to stop the swap space. + * + * TODO: want nicer authentication message + special treatment if the + * uid that locked the device (e.g. w/o -others). + */ + if (!udisks_daemon_util_check_authorization_sync (daemon, + object, + "org.freedesktop.udisks2.manage-swapspace", + options, + N_("Authentication is required to deactivate swapspace on $(udisks2.device)"), + invocation)) + goto out; + + job = udisks_daemon_launch_spawned_job (daemon, + NULL, /* cancellable */ + NULL, /* input_string */ + "swapoff %s", + udisks_block_device_get_device (block)); + g_signal_connect (job, + "completed", + G_CALLBACK (swapspace_stop_on_job_completed), + invocation); + + out: + return TRUE; +} + +/* ---------------------------------------------------------------------------------------------------- */ + +static void +swapspace_iface_init (UDisksSwapspaceIface *iface) +{ + iface->handle_start = handle_start; + iface->handle_stop = handle_stop; +} diff --git a/src/udiskslinuxswapspace.h b/src/udiskslinuxswapspace.h new file mode 100644 index 0000000..bfe13ad --- /dev/null +++ b/src/udiskslinuxswapspace.h @@ -0,0 +1,37 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2007-2010 David Zeuthen <zeuthen@gmail.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __UDISKS_LINUX_SWAPSPACE_H__ +#define __UDISKS_LINUX_SWAPSPACE_H__ + +#include "udisksdaemontypes.h" + +G_BEGIN_DECLS + +#define UDISKS_TYPE_LINUX_SWAPSPACE (udisks_linux_swapspace_get_type ()) +#define UDISKS_LINUX_SWAPSPACE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), UDISKS_TYPE_LINUX_SWAPSPACE, UDisksLinuxSwapspace)) +#define UDISKS_IS_LINUX_SWAPSPACE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), UDISKS_TYPE_LINUX_SWAPSPACE)) + +GType udisks_linux_swapspace_get_type (void) G_GNUC_CONST; +UDisksSwapspace *udisks_linux_swapspace_new (void); + +G_END_DECLS + +#endif /* __UDISKS_LINUX_SWAPSPACE_H__ */ |