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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
|
/***************************************************************************
* CVSID: $Id$
*
* hotplug.c : Handling of hotplug events
*
* Copyright (C) 2004 David Zeuthen, <david@fubar.dk>
*
* Licensed under the Academic Free License version 2.1
*
* 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
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <ctype.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include "../device_info.h"
#include "../hald.h"
#include "../logger.h"
#include "../osspec.h"
#include "acpi.h"
#include "apm.h"
#include "blockdev.h"
#include "classdev.h"
#include "osspec_linux.h"
#include "physdev.h"
#include "pmu.h"
#include "hotplug.h"
/** Queue of ordered hotplug events */
GQueue *hotplug_event_queue;
/** List of HotplugEvent objects we are currently processing */
GSList *hotplug_events_in_progress = NULL;
void
hotplug_event_end (void *end_token)
{
HotplugEvent *hotplug_event = (HotplugEvent *) end_token;
hotplug_events_in_progress = g_slist_remove (hotplug_events_in_progress, hotplug_event);
g_free (hotplug_event);
hotplug_event_process_queue ();
}
void
hotplug_event_reposted (void *end_token)
{
HotplugEvent *hotplug_event = (HotplugEvent *) end_token;
hotplug_events_in_progress = g_slist_remove (hotplug_events_in_progress, hotplug_event);
hotplug_event_process_queue ();
}
static void
fixup_net_device_for_renaming (HotplugEvent *hotplug_event)
{
/* fixup net devices by looking at ifindex */
if (strcmp (hotplug_event->sysfs.subsystem, "net") == 0 && hotplug_event->sysfs.net_ifindex != -1) {
int ifindex;
if (!hal_util_get_int_from_file (hotplug_event->sysfs.sysfs_path, "ifindex", &ifindex, 10) ||
(ifindex != hotplug_event->sysfs.net_ifindex)) {
GDir *dir;
char path[HAL_PATH_MAX];
char path1[HAL_PATH_MAX];
GError *err = NULL;
const gchar *f;
/* search for new name */
HAL_WARNING (("Net interface @ %s with ifindex %d was probably renamed",
hotplug_event->sysfs.sysfs_path, hotplug_event->sysfs.net_ifindex));
g_snprintf (path, HAL_PATH_MAX, "%s/class/net" , get_hal_sysfs_path());
if ((dir = g_dir_open (path, 0, &err)) == NULL) {
HAL_ERROR (("Unable to open %s/class/net: %s", get_hal_sysfs_path(), err->message));
g_error_free (err);
goto out;
}
while ((f = g_dir_read_name (dir)) != NULL) {
g_snprintf (path1, HAL_PATH_MAX, "%s/class/net/%s" , get_hal_sysfs_path (), f);
if (hal_util_get_int_from_file (path1, "ifindex", &ifindex, 10)) {
if (ifindex == hotplug_event->sysfs.net_ifindex) {
HAL_INFO (("Using sysfs path %s for ifindex %d", path1, ifindex));
strncpy (hotplug_event->sysfs.sysfs_path, path1, HAL_PATH_MAX);
g_dir_close (dir);
goto out;
}
}
}
g_dir_close (dir);
}
}
out:
return;
}
static void
hotplug_event_begin_sysfs (HotplugEvent *hotplug_event)
{
HalDevice *d;
char subsystem[HAL_PATH_MAX];
gchar *subsystem_target;
d = hal_device_store_match_key_value_string (hald_get_gdl (),
"linux.sysfs_path",
hotplug_event->sysfs.sysfs_path);
/* FIXME: we should reprobe the device instead of skipping the event */
if (d != NULL && hotplug_event->action == HOTPLUG_ACTION_ADD) {
HAL_ERROR (("devpath %s already present in the store, ignore event", hotplug_event->sysfs.sysfs_path));
hotplug_event_end ((void *) hotplug_event);
return;
}
/* get device type from already known device object */
if (d != NULL) {
HotplugEventType type;
type = hal_device_property_get_int (d, "linux.hotplug_type");
if (type == HOTPLUG_EVENT_SYSFS_BUS) {
HAL_INFO (("%s is a bus device (store)", hotplug_event->sysfs.sysfs_path));
hotplug_event->type = HOTPLUG_EVENT_SYSFS_BUS;
} else if (type == HOTPLUG_EVENT_SYSFS_CLASS) {
HAL_INFO (("%s is a class device (store)", hotplug_event->sysfs.sysfs_path));
hotplug_event->type = HOTPLUG_EVENT_SYSFS_CLASS;
} else if (type == HOTPLUG_EVENT_SYSFS_BLOCK) {
HAL_INFO (("%s is a block device (store)", hotplug_event->sysfs.sysfs_path));
hotplug_event->type = HOTPLUG_EVENT_SYSFS_BLOCK;
}
}
/*
* determine device type by "subsystem" link (from kernel 2.6.18, class devices
* start to move from /class to /devices and have a "subsystem" link pointing
* back to the "class" or "bus" directory
*/
if (hotplug_event->type == HOTPLUG_EVENT_SYSFS) {
g_snprintf (subsystem, HAL_PATH_MAX, "%s/subsystem", hotplug_event->sysfs.sysfs_path);
subsystem_target = g_file_read_link (subsystem, NULL);
if (subsystem_target != NULL) {
if (strstr(subsystem_target, "/bus/") != NULL) {
HAL_INFO (("%s is a bus device (subsystem)", hotplug_event->sysfs.sysfs_path));
hotplug_event->type = HOTPLUG_EVENT_SYSFS_BUS;
} else if (strstr(subsystem_target, "/class/") != NULL) {
HAL_INFO (("%s is a class device (subsystem)", hotplug_event->sysfs.sysfs_path));
hotplug_event->type = HOTPLUG_EVENT_SYSFS_CLASS;
} else if (strstr(subsystem_target, "/block") != NULL) {
HAL_INFO (("%s is a block device (subsystem)", hotplug_event->sysfs.sysfs_path));
hotplug_event->type = HOTPLUG_EVENT_SYSFS_BLOCK;
}
g_free (subsystem_target);
}
}
/* older kernels get the device type from the devpath */
if (hotplug_event->type == HOTPLUG_EVENT_SYSFS) {
char sys_devices_path[HAL_PATH_MAX];
char sys_class_path[HAL_PATH_MAX];
char sys_block_path[HAL_PATH_MAX];
gsize sys_devices_path_len;
gsize sys_class_path_len;
gsize sys_block_path_len;
sys_devices_path_len = g_snprintf (sys_devices_path, HAL_PATH_MAX, "%s/devices", get_hal_sysfs_path ());
sys_class_path_len = g_snprintf (sys_class_path, HAL_PATH_MAX, "%s/class", get_hal_sysfs_path ());
sys_block_path_len = g_snprintf (sys_block_path, HAL_PATH_MAX, "%s/block", get_hal_sysfs_path ());
if (strncmp (hotplug_event->sysfs.sysfs_path, sys_devices_path, sys_devices_path_len) == 0) {
HAL_INFO (("%s is a bus device (devpath)", hotplug_event->sysfs.sysfs_path));
hotplug_event->type = HOTPLUG_EVENT_SYSFS_BUS;
} else if (strncmp (hotplug_event->sysfs.sysfs_path, sys_class_path, sys_class_path_len) == 0) {
HAL_INFO (("%s is a class device (devpath)", hotplug_event->sysfs.sysfs_path));
hotplug_event->type = HOTPLUG_EVENT_SYSFS_CLASS;
} else if (strncmp (hotplug_event->sysfs.sysfs_path, sys_block_path, sys_block_path_len) == 0) {
HAL_INFO (("%s is a block device (devpath)", hotplug_event->sysfs.sysfs_path));
hotplug_event->type = HOTPLUG_EVENT_SYSFS_BLOCK;
}
}
if (hotplug_event->type == HOTPLUG_EVENT_SYSFS_BUS) {
if (hotplug_event->action == HOTPLUG_ACTION_ADD) {
HalDevice *parent;
parent = hal_util_find_closest_ancestor (hotplug_event->sysfs.sysfs_path);
hotplug_event_begin_add_physdev (hotplug_event->sysfs.subsystem,
hotplug_event->sysfs.sysfs_path,
parent,
(void *) hotplug_event);
} else if (hotplug_event->action == HOTPLUG_ACTION_REMOVE) {
hotplug_event_begin_remove_physdev (hotplug_event->sysfs.subsystem,
hotplug_event->sysfs.sysfs_path,
(void *) hotplug_event);
}
} else if (hotplug_event->type == HOTPLUG_EVENT_SYSFS_CLASS) {
if (hotplug_event->action == HOTPLUG_ACTION_ADD) {
gchar *target;
HalDevice *physdev;
char physdevpath[HAL_PATH_MAX];
gchar *sysfs_path_in_devices;
sysfs_path_in_devices = NULL;
/* /sbin/ifrename may be called from a hotplug handler before we process this,
* so if index doesn't match, go ahead and find a new sysfs path
*/
fixup_net_device_for_renaming (hotplug_event);
g_snprintf (physdevpath, HAL_PATH_MAX, "%s/device", hotplug_event->sysfs.sysfs_path);
if (((target = g_file_read_link (physdevpath, NULL)) != NULL)) {
gchar *normalized_target;
normalized_target = hal_util_get_normalized_path (hotplug_event->sysfs.sysfs_path, target);
g_free (target);
sysfs_path_in_devices = g_strdup (normalized_target);
/* there may be ''holes'' in /sys/devices so try hard to find the closest match */
do {
physdev = hal_device_store_match_key_value_string (hald_get_gdl (),
"linux.sysfs_path_device",
normalized_target);
if (physdev != NULL)
break;
/* go up one directory */
if (!hal_util_path_ascend (normalized_target))
break;
} while (physdev == NULL);
g_free (normalized_target);
} else {
physdev = NULL;
}
hotplug_event_begin_add_classdev (hotplug_event->sysfs.subsystem,
hotplug_event->sysfs.sysfs_path,
hotplug_event->sysfs.device_file,
physdev,
sysfs_path_in_devices,
(void *) hotplug_event);
g_free (sysfs_path_in_devices);
} else if (hotplug_event->action == HOTPLUG_ACTION_REMOVE) {
hotplug_event_begin_remove_classdev (hotplug_event->sysfs.subsystem,
hotplug_event->sysfs.sysfs_path,
(void *) hotplug_event);
}
} else if (hotplug_event->type == HOTPLUG_EVENT_SYSFS_BLOCK) {
if (hotplug_event->action == HOTPLUG_ACTION_ADD) {
HalDevice *parent = NULL;
int range;
gboolean is_partition;
gboolean is_fakevolume;
/* it's a partition if and only if it doesn't have the range file...
*
* notably the device mapper partitions do have a range file, but that's
* fine, we don't count them as partitions anyway...
*
* also, if the sysfs ends with "fakevolume" the hotplug event is synthesized
* from within HAL for partitions on the main block device
*/
is_fakevolume = FALSE;
if (strcmp (hal_util_get_last_element (hotplug_event->sysfs.sysfs_path), "fakevolume") == 0) {
is_fakevolume = TRUE;
}
is_partition = TRUE;
if (is_fakevolume ||
hal_util_get_int_from_file (hotplug_event->sysfs.sysfs_path, "range", &range, 0)) {
is_partition = FALSE;
}
if (is_partition || is_fakevolume) {
gchar *parent_path;
parent_path = hal_util_get_parent_path (hotplug_event->sysfs.sysfs_path);
parent = hal_device_store_match_key_value_string (hald_get_gdl (),
"linux.sysfs_path_device",
parent_path);
g_free (parent_path);
} else {
gchar *target;
char physdevpath[HAL_PATH_MAX];
g_snprintf (physdevpath, HAL_PATH_MAX, "%s/device", hotplug_event->sysfs.sysfs_path);
if (((target = g_file_read_link (physdevpath, NULL)) != NULL)) {
gchar *normalized_target;
normalized_target = hal_util_get_normalized_path (hotplug_event->sysfs.sysfs_path, target);
g_free (target);
parent = hal_device_store_match_key_value_string (hald_get_gdl (),
"linux.sysfs_path_device",
normalized_target);
g_free (normalized_target);
}
}
hotplug_event_begin_add_blockdev (hotplug_event->sysfs.sysfs_path,
hotplug_event->sysfs.device_file,
is_partition,
parent,
(void *) hotplug_event);
} else if (hotplug_event->action == HOTPLUG_ACTION_REMOVE) {
hotplug_event_begin_remove_blockdev (hotplug_event->sysfs.sysfs_path,
(void *) hotplug_event);
}
} else {
/* just ignore this hotplug event */
hotplug_event_end ((void *) hotplug_event);
}
}
static void
hotplug_event_begin_acpi (HotplugEvent *hotplug_event)
{
if (hotplug_event->action == HOTPLUG_ACTION_ADD) {
hotplug_event_begin_add_acpi (hotplug_event->acpi.acpi_path,
hotplug_event->acpi.acpi_type,
NULL,
(void *) hotplug_event);
} else if (hotplug_event->action == HOTPLUG_ACTION_REMOVE) {
hotplug_event_begin_remove_acpi (hotplug_event->acpi.acpi_path,
hotplug_event->acpi.acpi_type,
(void *) hotplug_event);
}
}
static void
hotplug_event_begin_apm (HotplugEvent *hotplug_event)
{
if (hotplug_event->action == HOTPLUG_ACTION_ADD) {
hotplug_event_begin_add_apm (hotplug_event->apm.apm_path,
hotplug_event->apm.apm_type,
NULL,
(void *) hotplug_event);
} else if (hotplug_event->action == HOTPLUG_ACTION_REMOVE) {
hotplug_event_begin_remove_apm (hotplug_event->apm.apm_path,
hotplug_event->apm.apm_type,
(void *) hotplug_event);
}
}
static void
hotplug_event_begin_pmu (HotplugEvent *hotplug_event)
{
if (hotplug_event->action == HOTPLUG_ACTION_ADD) {
hotplug_event_begin_add_pmu (hotplug_event->pmu.pmu_path,
hotplug_event->pmu.pmu_type,
NULL,
(void *) hotplug_event);
} else if (hotplug_event->action == HOTPLUG_ACTION_REMOVE) {
hotplug_event_begin_remove_pmu (hotplug_event->pmu.pmu_path,
hotplug_event->pmu.pmu_type,
(void *) hotplug_event);
}
}
static void
hotplug_event_begin (HotplugEvent *hotplug_event)
{
switch (hotplug_event->type) {
/* explicit fallthrough */
case HOTPLUG_EVENT_SYSFS:
case HOTPLUG_EVENT_SYSFS_BUS:
case HOTPLUG_EVENT_SYSFS_CLASS:
case HOTPLUG_EVENT_SYSFS_BLOCK:
hotplug_event_begin_sysfs (hotplug_event);
break;
case HOTPLUG_EVENT_ACPI:
hotplug_event_begin_acpi (hotplug_event);
break;
case HOTPLUG_EVENT_APM:
hotplug_event_begin_apm (hotplug_event);
break;
case HOTPLUG_EVENT_PMU:
hotplug_event_begin_pmu (hotplug_event);
break;
default:
HAL_ERROR (("Unknown hotplug event type %d", hotplug_event->type));
hotplug_event_end ((void *) hotplug_event);
break;
}
}
void
hotplug_event_enqueue (HotplugEvent *hotplug_event)
{
if (hotplug_event_queue == NULL)
hotplug_event_queue = g_queue_new ();
g_queue_push_tail (hotplug_event_queue, hotplug_event);
}
void
hotplug_event_enqueue_at_front (HotplugEvent *hotplug_event)
{
if (hotplug_event_queue == NULL)
hotplug_event_queue = g_queue_new ();
g_queue_push_head (hotplug_event_queue, hotplug_event);
}
void
hotplug_event_process_queue (void)
{
HotplugEvent *hotplug_event;
if (hotplug_events_in_progress == NULL &&
(hotplug_event_queue == NULL || g_queue_is_empty (hotplug_event_queue))) {
hotplug_queue_now_empty ();
goto out;
}
/* do not process events if some other event is in progress
*
* TODO: optimize so we can do add events in parallel by inspecting the
* wait_for_sysfs_path parameter and hotplug_events_in_progress list
*/
if (hotplug_events_in_progress != NULL && g_slist_length (hotplug_events_in_progress) > 0)
goto out;
hotplug_event = g_queue_pop_head (hotplug_event_queue);
if (hotplug_event == NULL)
goto out;
hotplug_events_in_progress = g_slist_append (hotplug_events_in_progress, hotplug_event);
hotplug_event_begin (hotplug_event);
out:
;
}
gboolean
hotplug_rescan_device (HalDevice *d)
{
gboolean ret;
switch (hal_device_property_get_int (d, "linux.hotplug_type")) {
case HOTPLUG_EVENT_SYSFS_BUS:
ret = physdev_rescan_device (d);
break;
case HOTPLUG_EVENT_SYSFS_CLASS:
ret = classdev_rescan_device (d);
break;
case HOTPLUG_EVENT_SYSFS_BLOCK:
ret = blockdev_rescan_device (d);
break;
case HOTPLUG_EVENT_ACPI:
ret = acpi_rescan_device (d);
break;
case HOTPLUG_EVENT_APM:
ret = apm_rescan_device (d);
break;
case HOTPLUG_EVENT_PMU:
ret = pmu_rescan_device (d);
break;
default:
HAL_INFO (("Unknown hotplug type for udi=%s", d->udi));
ret = FALSE;
break;
}
return ret;
}
static void
hotplug_reprobe_generate_remove_events (HalDevice *d)
{
GSList *i;
GSList *childs;
HotplugEvent *e;
/* first remove childs */
childs = hal_device_store_match_multiple_key_value_string (hald_get_gdl (), "info.parent", d->udi);
for (i = childs; i != NULL; i = g_slist_next (i)) {
HalDevice *child;
child = HAL_DEVICE (i->data);
hotplug_reprobe_generate_remove_events (child);
}
/* then remove self */
HAL_INFO (("Generate remove event for udi %s", d->udi));
switch (hal_device_property_get_int (d, "linux.hotplug_type")) {
case HOTPLUG_EVENT_SYSFS_BUS:
e = physdev_generate_remove_hotplug_event (d);
break;
case HOTPLUG_EVENT_SYSFS_CLASS:
e = classdev_generate_remove_hotplug_event (d);
break;
case HOTPLUG_EVENT_SYSFS_BLOCK:
e = blockdev_generate_remove_hotplug_event (d);
break;
case HOTPLUG_EVENT_ACPI:
e = acpi_generate_remove_hotplug_event (d);
break;
case HOTPLUG_EVENT_APM:
e = apm_generate_remove_hotplug_event (d);
break;
case HOTPLUG_EVENT_PMU:
e = pmu_generate_remove_hotplug_event (d);
break;
default:
e = NULL;
HAL_INFO (("Unknown hotplug type for udi=%s", d->udi));
break;
}
if (e != NULL) {
hotplug_event_enqueue (e);
}
}
static void
hotplug_reprobe_generate_add_events (HalDevice *d)
{
GSList *i;
GSList *childs;
HotplugEvent *e;
/* first add self */
HAL_INFO (("Generate add event for udi %s", d->udi));
switch (hal_device_property_get_int (d, "linux.hotplug_type")) {
case HOTPLUG_EVENT_SYSFS_BUS:
e = physdev_generate_add_hotplug_event (d);
break;
case HOTPLUG_EVENT_SYSFS_CLASS:
e = classdev_generate_add_hotplug_event (d);
break;
case HOTPLUG_EVENT_SYSFS_BLOCK:
e = blockdev_generate_add_hotplug_event (d);
break;
case HOTPLUG_EVENT_ACPI:
e = acpi_generate_add_hotplug_event (d);
break;
case HOTPLUG_EVENT_APM:
e = apm_generate_add_hotplug_event (d);
break;
case HOTPLUG_EVENT_PMU:
e = pmu_generate_add_hotplug_event (d);
break;
default:
e = NULL;
HAL_INFO (("Unknown hotplug type for udi=%s", d->udi));
break;
}
if (e != NULL) {
hotplug_event_enqueue (e);
}
/* then add childs */
childs = hal_device_store_match_multiple_key_value_string (hald_get_gdl (), "info.parent", d->udi);
for (i = childs; i != NULL; i = g_slist_next (i)) {
HalDevice *child;
child = HAL_DEVICE (i->data);
hotplug_reprobe_generate_add_events (child);
}
}
gboolean
hotplug_reprobe_tree (HalDevice *d)
{
hotplug_reprobe_generate_remove_events (d);
hotplug_reprobe_generate_add_events (d);
hotplug_event_process_queue ();
return FALSE;
}
|