1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/* -*- 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 <string.h>
#include "udiskslogging.h"
#include "udisksdaemon.h"
#include "udisksprovider.h"
#include "udiskslinuxprovider.h"
#include "udiskslinuxblock.h"
#include "udiskslinuxdrive.h"
#include "udiskslinuxmanager.h"
#include "udiskscleanup.h"
/**
* SECTION:udiskslinuxprovider
* @title: UDisksLinuxProvider
* @short_description: Provider of Linux-specific objects
*
* This object is used to add/remove Linux specific objects. Right now
* it handles #UDisksLinuxBlock and #UDisksLinuxDrive objects.
*/
typedef struct _UDisksLinuxProviderClass UDisksLinuxProviderClass;
/**
* UDisksLinuxProvider:
*
* The #UDisksLinuxProvider structure contains only private data and
* should only be accessed using the provided API.
*/
struct _UDisksLinuxProvider
{
UDisksProvider parent_instance;
GUdevClient *gudev_client;
UDisksObjectSkeleton *manager_object;
/* maps from sysfs path to UDisksLinuxBlock objects */
GHashTable *sysfs_to_block;
/* maps from VPD (serial, wwn) and sysfs_path to UDisksLinuxDrive objects */
GHashTable *vpd_to_drive;
GHashTable *sysfs_path_to_drive;
/* maps from sysfs path to UDisksLinuxController objects */
GHashTable *sysfs_to_controller;
};
struct _UDisksLinuxProviderClass
{
UDisksProviderClass parent_class;
};
static void
udisks_linux_provider_handle_uevent (UDisksLinuxProvider *provider,
const gchar *action,
GUdevDevice *device);
G_DEFINE_TYPE (UDisksLinuxProvider, udisks_linux_provider, UDISKS_TYPE_PROVIDER);
static void
udisks_linux_provider_finalize (GObject *object)
{
UDisksLinuxProvider *provider = UDISKS_LINUX_PROVIDER (object);
g_hash_table_unref (provider->sysfs_to_block);
g_hash_table_unref (provider->vpd_to_drive);
g_hash_table_unref (provider->sysfs_path_to_drive);
g_hash_table_unref (provider->sysfs_to_controller);
g_object_unref (provider->gudev_client);
udisks_object_skeleton_set_manager (provider->manager_object, NULL);
g_object_unref (provider->manager_object);
if (G_OBJECT_CLASS (udisks_linux_provider_parent_class)->finalize != NULL)
G_OBJECT_CLASS (udisks_linux_provider_parent_class)->finalize (object);
}
static void
on_uevent (GUdevClient *client,
const gchar *action,
GUdevDevice *device,
gpointer user_data)
{
UDisksLinuxProvider *provider = UDISKS_LINUX_PROVIDER (user_data);
//g_print ("%s:%s: entering\n", G_STRLOC, G_STRFUNC);
udisks_linux_provider_handle_uevent (provider, action, device);
}
static void
udisks_linux_provider_init (UDisksLinuxProvider *provider)
{
const gchar *subsystems[] = {"block", "iscsi_connection", "scsi", NULL};
/* get ourselves an udev client */
provider->gudev_client = g_udev_client_new (subsystems);
g_signal_connect (provider->gudev_client,
"uevent",
G_CALLBACK (on_uevent),
provider);
}
static void
udisks_linux_provider_start (UDisksProvider *_provider)
{
UDisksLinuxProvider *provider = UDISKS_LINUX_PROVIDER (_provider);
UDisksDaemon *daemon;
UDisksManager *manager;
GList *devices;
GList *l;
if (UDISKS_PROVIDER_CLASS (udisks_linux_provider_parent_class)->start != NULL)
UDISKS_PROVIDER_CLASS (udisks_linux_provider_parent_class)->start (_provider);
daemon = udisks_provider_get_daemon (UDISKS_PROVIDER (provider));
provider->manager_object = udisks_object_skeleton_new ("/org/freedesktop/UDisks2/Manager");
manager = udisks_linux_manager_new (daemon);
udisks_object_skeleton_set_manager (provider->manager_object, manager);
g_object_unref (manager);
g_dbus_object_manager_server_export (udisks_daemon_get_object_manager (daemon),
G_DBUS_OBJECT_SKELETON (provider->manager_object));
provider->sysfs_to_block = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
(GDestroyNotify) g_object_unref);
provider->vpd_to_drive = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
(GDestroyNotify) g_object_unref);
provider->sysfs_path_to_drive = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
NULL);
provider->sysfs_to_controller = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
(GDestroyNotify) g_object_unref);
devices = g_udev_client_query_by_subsystem (provider->gudev_client, "block");
for (l = devices; l != NULL; l = l->next)
udisks_linux_provider_handle_uevent (provider, "add", G_UDEV_DEVICE (l->data));
g_list_foreach (devices, (GFunc) g_object_unref, NULL);
g_list_free (devices);
}
static void
udisks_linux_provider_class_init (UDisksLinuxProviderClass *klass)
{
GObjectClass *gobject_class;
UDisksProviderClass *provider_class;
gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = udisks_linux_provider_finalize;
provider_class = UDISKS_PROVIDER_CLASS (klass);
provider_class->start = udisks_linux_provider_start;
}
/**
* udisks_linux_provider_new:
* @daemon: A #UDisksDaemon.
*
* Create a new provider object for Linux-specific objects / functionality.
*
* Returns: A #UDisksLinuxProvider object. Free with g_object_unref().
*/
UDisksLinuxProvider *
udisks_linux_provider_new (UDisksDaemon *daemon)
{
g_return_val_if_fail (UDISKS_IS_DAEMON (daemon), NULL);
return UDISKS_LINUX_PROVIDER (g_object_new (UDISKS_TYPE_LINUX_PROVIDER,
"daemon", daemon,
NULL));
}
/**
* udisks_linux_provider_get_udev_client:
* @provider: A #UDisksLinuxProvider.
*
* Gets the #GUdevClient used by @provider.
*
* Returns: A #GUdevClient owned by @provider. Do not free.
*/
GUdevClient *
udisks_linux_provider_get_udev_client (UDisksLinuxProvider *provider)
{
g_return_val_if_fail (UDISKS_IS_LINUX_PROVIDER (provider), NULL);
return provider->gudev_client;
}
/* ---------------------------------------------------------------------------------------------------- */
static void
handle_block_uevent_for_drive (UDisksLinuxProvider *provider,
const gchar *action,
GUdevDevice *device)
{
UDisksLinuxDrive *drive;
UDisksDaemon *daemon;
const gchar *sysfs_path;
gchar *vpd;
vpd = NULL;
daemon = udisks_provider_get_daemon (UDISKS_PROVIDER (provider));
sysfs_path = g_udev_device_get_sysfs_path (device);
if (g_strcmp0 (action, "remove") == 0)
{
drive = g_hash_table_lookup (provider->sysfs_path_to_drive, sysfs_path);
if (drive != NULL)
{
GList *devices;
udisks_linux_drive_uevent (drive, action, device);
g_warn_if_fail (g_hash_table_remove (provider->sysfs_path_to_drive, sysfs_path));
devices = udisks_linux_drive_get_devices (drive);
if (devices == NULL)
{
const gchar *vpd;
vpd = g_object_get_data (G_OBJECT (drive), "x-vpd");
g_dbus_object_manager_server_unexport (udisks_daemon_get_object_manager (daemon),
g_dbus_object_get_object_path (G_DBUS_OBJECT (drive)));
g_warn_if_fail (g_hash_table_remove (provider->vpd_to_drive, vpd));
}
g_list_foreach (devices, (GFunc) g_object_unref, NULL);
g_list_free (devices);
}
}
else
{
if (!udisks_linux_drive_should_include_device (device, &vpd))
goto out;
if (vpd == NULL)
{
udisks_debug ("Ignoring block device %s with no serial or WWN",
g_udev_device_get_sysfs_path (device));
goto out;
}
drive = g_hash_table_lookup (provider->vpd_to_drive, vpd);
if (drive != NULL)
{
if (g_hash_table_lookup (provider->sysfs_path_to_drive, sysfs_path) == NULL)
g_hash_table_insert (provider->sysfs_path_to_drive, g_strdup (sysfs_path), drive);
udisks_linux_drive_uevent (drive, action, device);
}
else
{
drive = udisks_linux_drive_new (daemon, device);
if (drive != NULL)
{
g_object_set_data_full (G_OBJECT (drive), "x-vpd", g_strdup (vpd), g_free);
g_dbus_object_manager_server_export_uniquely (udisks_daemon_get_object_manager (daemon),
G_DBUS_OBJECT_SKELETON (drive));
g_hash_table_insert (provider->vpd_to_drive, g_strdup (vpd), drive);
g_hash_table_insert (provider->sysfs_path_to_drive, g_strdup (sysfs_path), drive);
}
}
}
out:
g_free (vpd);
}
static void
handle_block_uevent_for_block (UDisksLinuxProvider *provider,
const gchar *action,
GUdevDevice *device)
{
const gchar *sysfs_path;
UDisksLinuxBlock *block;
UDisksDaemon *daemon;
daemon = udisks_provider_get_daemon (UDISKS_PROVIDER (provider));
sysfs_path = g_udev_device_get_sysfs_path (device);
if (g_strcmp0 (action, "remove") == 0)
{
block = g_hash_table_lookup (provider->sysfs_to_block, sysfs_path);
if (block != NULL)
{
g_dbus_object_manager_server_unexport (udisks_daemon_get_object_manager (daemon),
g_dbus_object_get_object_path (G_DBUS_OBJECT (block)));
g_warn_if_fail (g_hash_table_remove (provider->sysfs_to_block, sysfs_path));
}
}
else
{
block = g_hash_table_lookup (provider->sysfs_to_block, sysfs_path);
if (block != NULL)
{
udisks_linux_block_uevent (block, action, device);
}
else
{
block = udisks_linux_block_new (daemon, device);
g_dbus_object_manager_server_export_uniquely (udisks_daemon_get_object_manager (daemon),
G_DBUS_OBJECT_SKELETON (block));
g_hash_table_insert (provider->sysfs_to_block, g_strdup (sysfs_path), block);
}
}
}
static void
handle_block_uevent (UDisksLinuxProvider *provider,
const gchar *action,
GUdevDevice *device)
{
/* We use the sysfs block device for both Drive and BlockDevice
* objects. Ensure that drive are added before and removed after
* BlockDevice
*/
if (g_strcmp0 (action, "remove") == 0)
{
handle_block_uevent_for_block (provider, action, device);
handle_block_uevent_for_drive (provider, action, device);
}
else
{
handle_block_uevent_for_drive (provider, action, device);
handle_block_uevent_for_block (provider, action, device);
}
if (g_strcmp0 (action, "add") != 0)
{
/* Possibly need to clean up */
udisks_cleanup_check (udisks_daemon_get_cleanup (udisks_provider_get_daemon (UDISKS_PROVIDER (provider))));
}
}
static void
udisks_linux_provider_handle_uevent (UDisksLinuxProvider *provider,
const gchar *action,
GUdevDevice *device)
{
const gchar *subsystem;
udisks_debug ("uevent %s %s",
action,
g_udev_device_get_sysfs_path (device));
subsystem = g_udev_device_get_subsystem (device);
if (g_strcmp0 (subsystem, "block") == 0)
{
handle_block_uevent (provider, action, device);
}
}
|