1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
3
* Copyright (C) 2007-2010 David Zeuthen <zeuthen@gmail.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <glib/gi18n-lib.h>
24
#include <sys/types.h>
30
#include <glib/gstdio.h>
32
#include "udiskslogging.h"
33
#include "udiskslinuxswapspace.h"
34
#include "udiskslinuxblockobject.h"
35
#include "udisksdaemon.h"
36
#include "udisksdaemonutil.h"
37
#include "udisksmountmonitor.h"
38
#include "udiskslinuxdevice.h"
41
* SECTION:udiskslinuxswapspace
42
* @title: UDisksLinuxSwapspace
43
* @short_description: Linux implementation of #UDisksSwapspace
45
* This type provides an implementation of the #UDisksSwapspace interface
49
typedef struct _UDisksLinuxSwapspaceClass UDisksLinuxSwapspaceClass;
52
* UDisksLinuxSwapspace:
54
* The #UDisksLinuxSwapspace structure contains only private data and should
55
* only be accessed using the provided API.
57
struct _UDisksLinuxSwapspace
59
UDisksSwapspaceSkeleton parent_instance;
62
struct _UDisksLinuxSwapspaceClass
64
UDisksSwapspaceSkeletonClass parent_class;
67
static void swapspace_iface_init (UDisksSwapspaceIface *iface);
69
G_DEFINE_TYPE_WITH_CODE (UDisksLinuxSwapspace, udisks_linux_swapspace, UDISKS_TYPE_SWAPSPACE_SKELETON,
70
G_IMPLEMENT_INTERFACE (UDISKS_TYPE_SWAPSPACE, swapspace_iface_init));
72
/* ---------------------------------------------------------------------------------------------------- */
75
udisks_linux_swapspace_init (UDisksLinuxSwapspace *swapspace)
77
g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (swapspace),
78
G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
82
udisks_linux_swapspace_class_init (UDisksLinuxSwapspaceClass *klass)
87
* udisks_linux_swapspace_new:
89
* Creates a new #UDisksLinuxSwapspace instance.
91
* Returns: A new #UDisksLinuxSwapspace. Free with g_object_unref().
94
udisks_linux_swapspace_new (void)
96
return UDISKS_SWAPSPACE (g_object_new (UDISKS_TYPE_LINUX_SWAPSPACE,
100
/* ---------------------------------------------------------------------------------------------------- */
103
* udisks_linux_swapspace_update:
104
* @swapspace: A #UDisksLinuxSwapspace.
105
* @object: The enclosing #UDisksLinuxBlockObject instance.
107
* Updates the interface.
110
udisks_linux_swapspace_update (UDisksLinuxSwapspace *swapspace,
111
UDisksLinuxBlockObject *object)
113
UDisksMountMonitor *mount_monitor;
114
UDisksLinuxDevice *device;
115
UDisksMountType mount_type;
118
mount_monitor = udisks_daemon_get_mount_monitor (udisks_linux_block_object_get_daemon (object));
119
device = udisks_linux_block_object_get_device (object);
122
if (udisks_mount_monitor_is_dev_in_use (mount_monitor, g_udev_device_get_device_number (device->udev_device), &mount_type) &&
123
mount_type == UDISKS_MOUNT_TYPE_SWAP)
125
udisks_swapspace_set_active (UDISKS_SWAPSPACE (swapspace), active);
127
g_object_unref (device);
130
/* ---------------------------------------------------------------------------------------------------- */
134
swapspace_start_on_job_completed (UDisksJob *job,
136
const gchar *message,
139
GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (user_data);
140
UDisksSwapspace *swapspace;
141
swapspace = UDISKS_SWAPSPACE (g_dbus_method_invocation_get_user_data (invocation));
143
udisks_swapspace_complete_start (swapspace, invocation);
145
g_dbus_method_invocation_return_error (invocation,
148
"Error activating swap: %s",
153
handle_start (UDisksSwapspace *swapspace,
154
GDBusMethodInvocation *invocation,
157
UDisksObject *object;
158
UDisksDaemon *daemon;
162
gchar *escaped_device = NULL;
167
object = udisks_daemon_util_dup_object (swapspace, &error);
170
g_dbus_method_invocation_take_error (invocation, error);
174
daemon = udisks_linux_block_object_get_daemon (UDISKS_LINUX_BLOCK_OBJECT (object));
175
block = udisks_object_peek_block (object);
178
if (!udisks_daemon_util_get_caller_uid_sync (daemon,
180
NULL /* GCancellable */,
186
g_dbus_method_invocation_return_gerror (invocation, error);
187
g_error_free (error);
191
if (!udisks_daemon_util_check_authorization_sync (daemon,
193
"org.freedesktop.udisks2.manage-swapspace",
195
/* Translators: Shown in authentication dialog when the user
196
* requests activating a swap device.
198
* Do not translate $(drive), it's a placeholder and
199
* will be replaced by the name of the drive/device in question
201
N_("Authentication is required to activate swapspace on $(drive)"),
205
escaped_device = udisks_daemon_util_escape_and_quote (udisks_block_get_device (block));
207
job = udisks_daemon_launch_spawned_job (daemon,
209
"swapspace-start", caller_uid,
210
NULL, /* cancellable */
211
0, /* uid_t run_as_uid */
212
0, /* uid_t run_as_euid */
213
NULL, /* input_string */
216
g_signal_connect (job,
218
G_CALLBACK (swapspace_start_on_job_completed),
222
g_free (escaped_device);
223
g_clear_object (&object);
228
swapspace_stop_on_job_completed (UDisksJob *job,
230
const gchar *message,
233
GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (user_data);
234
UDisksSwapspace *swapspace;
235
swapspace = UDISKS_SWAPSPACE (g_dbus_method_invocation_get_user_data (invocation));
237
udisks_swapspace_complete_start (swapspace, invocation);
239
g_dbus_method_invocation_return_error (invocation,
242
"Error deactivating swap: %s",
247
handle_stop (UDisksSwapspace *swapspace,
248
GDBusMethodInvocation *invocation,
251
UDisksObject *object;
252
UDisksDaemon *daemon;
257
gchar *escaped_device = NULL;
258
GError *error = NULL;
260
object = UDISKS_OBJECT (g_dbus_interface_get_object (G_DBUS_INTERFACE (swapspace)));
261
daemon = udisks_linux_block_object_get_daemon (UDISKS_LINUX_BLOCK_OBJECT (object));
262
block = udisks_object_peek_block (object);
265
if (!udisks_daemon_util_get_caller_uid_sync (daemon,
267
NULL /* GCancellable */,
273
g_dbus_method_invocation_return_gerror (invocation, error);
274
g_error_free (error);
278
/* Now, check that the user is actually authorized to stop the swap space.
280
* TODO: want nicer authentication message + special treatment if the
281
* uid that locked the device (e.g. w/o -others).
283
if (!udisks_daemon_util_check_authorization_sync (daemon,
285
"org.freedesktop.udisks2.manage-swapspace",
287
/* Translators: Shown in authentication dialog when the user
288
* requests deactivating a swap device.
290
* Do not translate $(drive), it's a placeholder and
291
* will be replaced by the name of the drive/device in question
293
N_("Authentication is required to deactivate swapspace on $(drive)"),
297
escaped_device = udisks_daemon_util_escape_and_quote (udisks_block_get_device (block));
299
job = udisks_daemon_launch_spawned_job (daemon,
301
"swapspace-stop", caller_uid,
302
NULL, /* cancellable */
303
0, /* uid_t run_as_uid */
304
0, /* uid_t run_as_euid */
305
NULL, /* input_string */
308
g_signal_connect (job,
310
G_CALLBACK (swapspace_stop_on_job_completed),
314
g_free (escaped_device);
318
/* ---------------------------------------------------------------------------------------------------- */
321
swapspace_iface_init (UDisksSwapspaceIface *iface)
323
iface->handle_start = handle_start;
324
iface->handle_stop = handle_stop;