Debian Codegraph @

modules/lvm2/udiskslinuxmanagerlvm2.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2014 Tomas Bzatek <[email protected]>
4 *
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.
9 *
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.
14 *
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
18 *
19 */
20
21 #include "config.h"
22
23 #include <sys/types.h>
24
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <alloca.h>
29
30 #include <glib/gi18n-lib.h>
31
32 #include <src/udiskslogging.h>
33 #include <src/udisksdaemon.h>
34 #include <src/udisksdaemonutil.h>
35 #include <src/udiskslinuxdevice.h>
36 #include <src/udiskslinuxblockobject.h>
37
38 #include "udiskslvm2types.h"
39 #include "udiskslinuxmodulelvm2.h"
40 #include "udiskslinuxmanagerlvm2.h"
41 #include "udiskslvm2daemonutil.h"
42 #include "jobhelpers.h"
43
44 /**
45 * SECTION:udiskslinuxmanagerlvm2
46 * @title: UDisksLinuxManagerLVM2
47 * @short_description: Linux implementation of #UDisksLinuxManagerLVM2
48 *
49 * This type provides an implementation of the #UDisksLinuxManagerLVM2
50 * interface on Linux.
51 */
52
53 typedef struct _UDisksLinuxManagerLVM2Class UDisksLinuxManagerLVM2Class;
54
55 /**
56 * UDisksLinuxManagerLVM2:
57 *
58 * The #UDisksLinuxManagerLVM2 structure contains only private data and should
59 * only be accessed using the provided API.
60 */
61 struct _UDisksLinuxManagerLVM2
62 {
63 UDisksManagerLVM2Skeleton parent_instance;
64
65 UDisksLinuxModuleLVM2 *module;
66 };
67
68 struct _UDisksLinuxManagerLVM2Class
69 {
70 UDisksManagerLVM2SkeletonClass parent_class;
71 };
72
73 enum
74 {
75 PROP_0,
76 PROP_MODULE
77 };
78
79 static void udisks_linux_manager_lvm2_iface_init (UDisksManagerLVM2Iface *iface);
80
81 G_DEFINE_TYPE_WITH_CODE (UDisksLinuxManagerLVM2, udisks_linux_manager_lvm2, UDISKS_TYPE_MANAGER_LVM2_SKELETON,
82 G_IMPLEMENT_INTERFACE (UDISKS_TYPE_MANAGER_LVM2, udisks_linux_manager_lvm2_iface_init));
83
84 /* ---------------------------------------------------------------------------------------------------- */
85
86 static void
87 udisks_linux_manager_lvm2_get_property (GObject *object,
88 guint prop_id,
89 GValue *value,
90 GParamSpec *pspec)
91 {
92 UDisksLinuxManagerLVM2 *manager = UDISKS_LINUX_MANAGER_LVM2 (object);
93
94 switch (prop_id)
95 {
96 case PROP_MODULE:
97 g_value_set_object (value, udisks_linux_manager_lvm2_get_module (manager));
98 break;
99
100 default:
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
102 break;
103 }
104 }
105
106 static void
107 udisks_linux_manager_lvm2_set_property (GObject *object,
108 guint prop_id,
109 const GValue *value,
110 GParamSpec *pspec)
111 {
112 UDisksLinuxManagerLVM2 *manager = UDISKS_LINUX_MANAGER_LVM2 (object);
113
114 switch (prop_id)
115 {
116 case PROP_MODULE:
117 g_assert (manager->module == NULL);
118 manager->module = g_value_dup_object (value);
119 break;
120
121 default:
122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
123 break;
124 }
125 }
126
127 static void
128 udisks_linux_manager_lvm2_init (UDisksLinuxManagerLVM2 *manager)
129 {
130 g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (manager),
131 G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
132 }
133
134 static void
135 udisks_linux_manager_lvm2_finalize (GObject *object)
136 {
137 UDisksLinuxManagerLVM2 *manager = UDISKS_LINUX_MANAGER_LVM2 (object);
138
139 g_object_unref (manager->module);
140
141 if (G_OBJECT_CLASS (udisks_linux_manager_lvm2_parent_class)->finalize)
142 G_OBJECT_CLASS (udisks_linux_manager_lvm2_parent_class)->finalize (object);
143 }
144
145 static void
146 udisks_linux_manager_lvm2_class_init (UDisksLinuxManagerLVM2Class *klass)
147 {
148 GObjectClass *gobject_class;
149
150 gobject_class = G_OBJECT_CLASS (klass);
151 gobject_class->finalize = udisks_linux_manager_lvm2_finalize;
152 gobject_class->set_property = udisks_linux_manager_lvm2_set_property;
153 gobject_class->get_property = udisks_linux_manager_lvm2_get_property;
154
155 /**
156 * UDisksLinuxManager:module:
157 *
158 * The #UDisksLinuxModuleLVM2 of the manager.
159 */
160 g_object_class_install_property (gobject_class,
161 PROP_MODULE,
162 g_param_spec_object ("module",
163 "Module",
164 "The module for the object",
165 UDISKS_TYPE_LINUX_MODULE_LVM2,
166 G_PARAM_READABLE |
167 G_PARAM_WRITABLE |
168 G_PARAM_CONSTRUCT_ONLY |
169 G_PARAM_STATIC_STRINGS));
170 }
171
172 /**
173 * udisks_linux_manager_lvm2_new:
174 * @module: A #UDisksLinuxModuleLVM2.
175 *
176 * Creates a new #UDisksLinuxManagerLVM2 instance.
177 *
178 * Returns: A new #UDisksLinuxManagerLVM2. Free with g_object_unref().
179 */
180 UDisksLinuxManagerLVM2 *
181 udisks_linux_manager_lvm2_new (UDisksLinuxModuleLVM2 *module)
182 {
183 g_return_val_if_fail (UDISKS_IS_MODULE (module), NULL);
184 return UDISKS_LINUX_MANAGER_LVM2 (g_object_new (UDISKS_TYPE_LINUX_MANAGER_LVM2,
185 "module", module,
186 NULL));
187 }
188
189 /**
190 * udisks_linux_manager_lvm2_get_module:
191 * @manager: A #UDisksLinuxManagerLVM2.
192 *
193 * Gets the module the @manager belongs to.
194 *
195 * Returns: A #UDisksLinuxModuleLVM2. Do not free, the object is owned by @manager.
196 */
197 UDisksLinuxModuleLVM2 *
198 udisks_linux_manager_lvm2_get_module (UDisksLinuxManagerLVM2 *manager)
199 {
200 g_return_val_if_fail (UDISKS_IS_LINUX_MANAGER_LVM2 (manager), NULL);
201 return manager->module;
202 }
203
204 /* ---------------------------------------------------------------------------------------------------- */
205
206 typedef struct
207 {
208 UDisksLinuxModuleLVM2 *module;
209 const gchar *name;
210 } WaitForVolumeGroupObjectData;
211
212 static UDisksObject *
213 wait_for_volume_group_object (UDisksDaemon *daemon,
214 gpointer user_data)
215 {
216 WaitForVolumeGroupObjectData *data = user_data;
217 UDisksLinuxVolumeGroupObject *object;
218
219 object = udisks_linux_module_lvm2_find_volume_group_object (data->module, data->name);
220
221 if (object == NULL)
222 return NULL;
223
224 return g_object_ref (UDISKS_OBJECT (object));
225 }
226
227 static gboolean
228 handle_volume_group_create (UDisksManagerLVM2 *_object,
229 GDBusMethodInvocation *invocation,
230 const gchar *arg_name,
231 const gchar *const *arg_blocks,
232 GVariant *arg_options)
233 {
234 UDisksLinuxManagerLVM2 *manager = UDISKS_LINUX_MANAGER_LVM2(_object);
235 UDisksDaemon *daemon;
236 uid_t caller_uid;
237 GError *error = NULL;
238 GList *blocks = NULL;
239 GList *l;
240 guint n;
241 UDisksObject *group_object = NULL;
242 const gchar **pvs = NULL;
243 VGJobData data;
244 WaitForVolumeGroupObjectData wait_data;
245
246 daemon = udisks_module_get_daemon (UDISKS_MODULE (manager->module));
247 if (!udisks_daemon_util_get_caller_uid_sync (daemon, invocation, NULL /* GCancellable */, &caller_uid, &error))
248 {
249 g_dbus_method_invocation_return_gerror (invocation, error);
250 g_clear_error (&error);
251 goto out;
252 }
253
254 /* Policy check. */
255 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
256 NULL,
257 LVM2_POLICY_ACTION_ID,
258 arg_options,
259 N_("Authentication is required to create a volume group"),
260 invocation);
261
262 if (arg_blocks == NULL || *arg_blocks == NULL)
263 {
264 g_dbus_method_invocation_return_error (invocation,
265 UDISKS_ERROR,
266 UDISKS_ERROR_FAILED,
267 "List of block devices is empty.");
268 goto out;
269 }
270
271 /* Collect and validate block objects
272 *
273 * Also, check we can open the block devices at the same time - this
274 * is to avoid start deleting half the block devices while the other
275 * half is already in use.
276 */
277 for (n = 0; arg_blocks != NULL && arg_blocks[n] != NULL; n++)
278 {
279 UDisksObject *object = NULL;
280 UDisksBlock *block = NULL;
281
282 object = udisks_daemon_find_object (daemon, arg_blocks[n]);
283 if (object == NULL)
284 {
285 g_dbus_method_invocation_return_error (invocation,
286 UDISKS_ERROR,
287 UDISKS_ERROR_FAILED,
288 "Invalid object path %s at index %u",
289 arg_blocks[n], n);
290 goto out;
291 }
292
293 block = udisks_object_get_block (object);
294 if (block == NULL)
295 {
296 g_dbus_method_invocation_return_error (invocation,
297 UDISKS_ERROR,
298 UDISKS_ERROR_FAILED,
299 "Object path %s for index %u is not a block device",
300 arg_blocks[n], n);
301 g_object_unref (object);
302 goto out;
303 }
304
305 if (!udisks_daemon_util_lvm2_block_is_unused (block, &error))
306 {
307 g_dbus_method_invocation_take_error (invocation, error);
308 g_object_unref (object);
309 g_object_unref (block);
310 goto out;
311 }
312
313 blocks = g_list_prepend (blocks, block); /* adopts ownership */
314 g_object_unref (object);
315 }
316 blocks = g_list_reverse (blocks);
317
318 pvs = (const gchar**) alloca (sizeof (gchar*) * (n + 1));
319
320 /* wipe existing devices */
321 for (l = blocks; l != NULL; l = l->next)
322 {
323 if (!udisks_daemon_util_lvm2_wipe_block (daemon, UDISKS_BLOCK (l->data), &error))
324 {
325 g_dbus_method_invocation_take_error (invocation, error);
326 goto out;
327 }
328 }
329
330 /* Create the PVs... */
331 for (n = 0, l = blocks; l != NULL; l = l->next, n++)
332 {
333 UDisksBlock *block = UDISKS_BLOCK (l->data);
334 PVJobData pv_data;
335 pvs[n] = udisks_block_get_device (block);
336 pv_data.path = pvs[n];
337 if (!udisks_daemon_launch_threaded_job_sync (daemon,
338 NULL,
339 "lvm-pv-create",
340 caller_uid,
341 FALSE,
342 pvcreate_job_func,
343 &pv_data,
344 NULL, /* user_data_free_func */
345 NULL, /* GCancellable */
346 &error))
347 {
348 g_dbus_method_invocation_return_error (invocation,
349 UDISKS_ERROR,
350 UDISKS_ERROR_FAILED,
351 "Error creating a physical volume: %s",
352 error->message);
353 g_clear_error (&error);
354 goto out;
355 }
356 }
357 pvs[n] = NULL;
358
359 /* Create the volume group... */
360 data.vg_name = arg_name;
361 data.pvs = pvs;
362
363 if (!udisks_daemon_launch_threaded_job_sync (daemon,
364 NULL,
365 "lvm-vg-create",
366 caller_uid,
367 FALSE,
368 vgcreate_job_func,
369 &data,
370 NULL, /* user_data_free_func */
371 NULL, /* GCancellable */
372 &error))
373 {
374 g_dbus_method_invocation_return_error (invocation,
375 UDISKS_ERROR,
376 UDISKS_ERROR_FAILED,
377 "Error creating volume group: %s",
378 error->message);
379 g_clear_error (&error);
380 goto out;
381 }
382
383 for (l = blocks; l != NULL; l = l->next)
384 {
385 UDisksBlock *block = UDISKS_BLOCK (l->data);
386 UDisksObject *object_for_block;
387 object_for_block = udisks_daemon_util_dup_object (block, &error);
388 if (object_for_block != NULL)
389 udisks_linux_block_object_trigger_uevent_sync (UDISKS_LINUX_BLOCK_OBJECT (object_for_block),
390 UDISKS_DEFAULT_WAIT_TIMEOUT);
391 g_object_unref (object_for_block);
392 }
393
394 /* ... then, sit and wait for the object to show up */
395 wait_data.module = manager->module;
396 wait_data.name = arg_name;
397 group_object = udisks_daemon_wait_for_object_sync (daemon,
398 wait_for_volume_group_object,
399 &wait_data,
400 NULL,
401 UDISKS_DEFAULT_WAIT_TIMEOUT,
402 &error);
403 if (group_object == NULL)
404 {
405 g_prefix_error (&error,
406 "Error waiting for volume group object for '%s': ",
407 arg_name);
408 g_dbus_method_invocation_take_error (invocation, error);
409 goto out;
410 }
411
412 udisks_manager_lvm2_complete_volume_group_create (_object,
413 invocation,
414 g_dbus_object_get_object_path (G_DBUS_OBJECT (group_object)));
415 g_object_unref (group_object);
416
417 out:
418 g_list_free_full (blocks, g_object_unref);
419
420 return TRUE; /* returning TRUE means that we handled the method invocation */
421 }
422
423 /* ---------------------------------------------------------------------------------------------------- */
424
425 static void
426 udisks_linux_manager_lvm2_iface_init (UDisksManagerLVM2Iface *iface)
427 {
428 iface->handle_volume_group_create = handle_volume_group_create;
429 }