Skip to content

Commit 04a727d

Browse files
committed
udev_device.c: consider device valid even if no subsystem symlink exists
Some devices may not have subsystem symlinks, therefore we cannot rely on that to check if device is valid. Instead, check uevent file which should be always present in valid device. Reference: openwrt/openwrt#12350
1 parent e5587c6 commit 04a727d

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

udev_device.c

+12-14
Original file line numberDiff line numberDiff line change
@@ -342,17 +342,17 @@ static char *read_symlink(const char *syspath, const char *name)
342342
return strdup(strrchr(link, '/') + 1);
343343
}
344344

345-
static void set_properties_from_uevent(struct udev_device *udev_device)
345+
static int set_properties_from_uevent(struct udev_device *udev_device, const char *syspath)
346346
{
347-
char line[LINE_MAX], path[PATH_MAX], devnode[PATH_MAX];
347+
char line[LINE_MAX], path[PATH_MAX + sizeof("/uevent")], devnode[PATH_MAX];
348348
FILE *file;
349349
char *pos;
350350

351-
snprintf(path, sizeof(path), "%s/uevent", udev_device_get_syspath(udev_device));
351+
snprintf(path, sizeof(path), "%s/uevent", syspath);
352352
file = fopen(path, "r");
353353

354354
if (!file) {
355-
return;
355+
return -1;
356356
}
357357

358358
while (fgets(line, sizeof(line), file)) {
@@ -364,11 +364,12 @@ static void set_properties_from_uevent(struct udev_device *udev_device)
364364
}
365365
else if ((pos = strchr(line, '='))) {
366366
*pos = '\0';
367-
udev_list_entry_add(&udev_device->properties, line, pos + 1, 1);
367+
udev_list_entry_add(&udev_device->properties, line, pos + 1, 0);
368368
}
369369
}
370370

371371
fclose(file);
372+
return 0;
372373
}
373374

374375
static void make_bit(unsigned long *arr, int cnt, const char *str)
@@ -543,21 +544,13 @@ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *
543544
return NULL;
544545
}
545546

546-
subsystem = read_symlink(syspath, "subsystem");
547-
548-
if (!subsystem) {
549-
return NULL;
550-
}
551-
552547
if (!realpath(syspath, path)) {
553-
free(subsystem);
554548
return NULL;
555549
}
556550

557551
udev_device = calloc(1, sizeof(*udev_device));
558552

559553
if (!udev_device) {
560-
free(subsystem);
561554
return NULL;
562555
}
563556

@@ -568,11 +561,17 @@ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *
568561
udev_list_entry_init(&udev_device->properties);
569562
udev_list_entry_init(&udev_device->sysattrs);
570563

564+
if (set_properties_from_uevent(udev_device, path) == -1) {
565+
free(udev_device);
566+
return NULL;
567+
}
568+
571569
udev_list_entry_add(&udev_device->properties, "SYSPATH", path, 0);
572570
udev_list_entry_add(&udev_device->properties, "DEVPATH", path + 4, 0);
573571

574572
sysname = strrchr(path, '/') + 1;
575573
driver = read_symlink(path, "driver");
574+
subsystem = read_symlink(path, "subsystem");
576575

577576
udev_list_entry_add(&udev_device->properties, "SUBSYSTEM", subsystem, 0);
578577
udev_list_entry_add(&udev_device->properties, "SYSNAME", sysname, 0);
@@ -585,7 +584,6 @@ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *
585584
}
586585
}
587586

588-
set_properties_from_uevent(udev_device);
589587
set_properties_from_evdev(udev_device);
590588
set_properties_from_props(udev_device);
591589

0 commit comments

Comments
 (0)