mirror of
https://github.com/systemd/systemd.git
synced 2024-12-23 21:35:11 +03:00
libudev: enumerate - split new() and scan()
This commit is contained in:
parent
b03e89cfba
commit
438d4c3cd4
@ -1,6 +1,6 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
# udev-test
|
||||
# udev test
|
||||
#
|
||||
# Provides automated testing of the udev binary.
|
||||
# The whole test is self contained in this file, except the matching sysfs tree.
|
||||
@ -14,7 +14,7 @@
|
||||
# After creation and removal the result is checked against the
|
||||
# expected value and the result is printed.
|
||||
#
|
||||
# Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
|
||||
# Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
|
||||
# Copyright (C) 2004 Leann Ogasawara <ogasawara@osdl.org>
|
||||
|
||||
use warnings;
|
||||
|
@ -31,8 +31,9 @@ udev_device_get_driver
|
||||
udev_device_get_devnum
|
||||
udev_device_get_seqnum
|
||||
udev_device_get_attr_value
|
||||
udev_enumerate_new_from_devices
|
||||
udev_enumerate_new_from_subsystems
|
||||
udev_enumerate_new
|
||||
udev_enumerate_scan_devices
|
||||
udev_enumerate_scan_subsystems
|
||||
udev_enumerate_ref
|
||||
udev_enumerate_unref
|
||||
udev_enumerate_get_udev
|
||||
|
@ -295,7 +295,7 @@ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, de
|
||||
{
|
||||
char path[UTIL_PATH_SIZE];
|
||||
const char *type_str;
|
||||
struct udev_enumerate *enumerate;
|
||||
struct udev_enumerate *udev_enumerate;
|
||||
struct udev_list_entry *list_entry;
|
||||
struct udev_device *device = NULL;
|
||||
|
||||
@ -312,14 +312,16 @@ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, de
|
||||
if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
|
||||
return udev_device_new_from_syspath(udev, path);
|
||||
|
||||
udev_enumerate = udev_enumerate_new(udev);
|
||||
if (udev_enumerate == NULL)
|
||||
return NULL;
|
||||
|
||||
/* fallback to search sys devices for the major/minor */
|
||||
if (type == 'b')
|
||||
enumerate = udev_enumerate_new_from_devices(udev, "block", NULL);
|
||||
udev_enumerate_scan_devices(udev_enumerate, "block", NULL);
|
||||
else if (type == 'c')
|
||||
enumerate = udev_enumerate_new_from_devices(udev, "!block", NULL);
|
||||
if (enumerate == NULL)
|
||||
return NULL;
|
||||
udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
|
||||
udev_enumerate_scan_devices(udev_enumerate, "!block", NULL);
|
||||
udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
|
||||
struct udev_device *device_loop;
|
||||
|
||||
device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
|
||||
@ -338,7 +340,7 @@ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, de
|
||||
udev_device_unref(device_loop);
|
||||
}
|
||||
}
|
||||
udev_enumerate_unref(enumerate);
|
||||
udev_enumerate_unref(udev_enumerate);
|
||||
return device;
|
||||
}
|
||||
|
||||
|
@ -167,7 +167,13 @@ static int devices_delay(struct udev *udev, const char *syspath)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct udev_enumerate *enumerate_new(struct udev *udev)
|
||||
/**
|
||||
* udev_enumerate_new:
|
||||
* @udev: udev library context
|
||||
*
|
||||
* Returns: an enumeration context
|
||||
**/
|
||||
struct udev_enumerate *udev_enumerate_new(struct udev *udev)
|
||||
{
|
||||
struct udev_enumerate *udev_enumerate;
|
||||
|
||||
@ -182,15 +188,15 @@ static struct udev_enumerate *enumerate_new(struct udev *udev)
|
||||
}
|
||||
|
||||
/**
|
||||
* udev_enumerate_new_from_devices:
|
||||
* @udev: udev library context
|
||||
* udev_enumerate_scan_devices:
|
||||
* @udev_enumerate: udev enumeration context
|
||||
* @subsystem: the list of names of subsystems to look for devices
|
||||
*
|
||||
* Returns: an enumeration context
|
||||
* Returns: 0 on success.
|
||||
**/
|
||||
struct udev_enumerate *udev_enumerate_new_from_devices(struct udev *udev, const char *subsystem, ...)
|
||||
int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate, const char *subsystem, ...)
|
||||
{
|
||||
struct udev_enumerate *udev_enumerate;
|
||||
struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
|
||||
va_list vargs;
|
||||
const char *arg;
|
||||
char base[UTIL_PATH_SIZE];
|
||||
@ -199,12 +205,8 @@ struct udev_enumerate *udev_enumerate_new_from_devices(struct udev *udev, const
|
||||
struct list_node subsystem_exclude_list;
|
||||
struct udev_list_entry *list_entry;
|
||||
|
||||
if (udev == NULL)
|
||||
return NULL;
|
||||
|
||||
udev_enumerate = enumerate_new(udev);
|
||||
if (udev_enumerate == NULL)
|
||||
return NULL;
|
||||
return -EINVAL;
|
||||
|
||||
va_start(vargs, subsystem);
|
||||
list_init(&subsystem_include_list);
|
||||
@ -247,10 +249,11 @@ struct udev_enumerate *udev_enumerate_new_from_devices(struct udev *udev, const
|
||||
int exclude_block = (udev_list_entry_get_by_name(exclude_list, "block") != NULL);
|
||||
|
||||
if (include_block && !exclude_block) {
|
||||
info(udev, "searching '/block/*/*' dir\n");
|
||||
info(udev, "searching '/block/*' dir\n");
|
||||
/* scan disks */
|
||||
devices_scan_subsystem(udev, "/block", NULL, NULL, &udev_enumerate->devices_list);
|
||||
/* scan partitions */
|
||||
info(udev, "searching '/block/*/*' dir\n");
|
||||
devices_scan_subsystems(udev, "/block", NULL,
|
||||
NULL, NULL,
|
||||
&udev_enumerate->devices_list);
|
||||
@ -266,22 +269,24 @@ struct udev_enumerate *udev_enumerate_new_from_devices(struct udev *udev, const
|
||||
if (devices_delay(udev, udev_list_entry_get_name(list_entry)))
|
||||
list_entry_move_to_end(list_entry);
|
||||
}
|
||||
return udev_enumerate;
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct udev_enumerate *udev_enumerate_new_from_subsystems(struct udev *udev)
|
||||
/**
|
||||
* udev_enumerate_scan_subsystems:
|
||||
* @udev_enumerate: udev enumeration context
|
||||
*
|
||||
* Returns: 0 on success.
|
||||
**/
|
||||
int udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate)
|
||||
{
|
||||
struct udev_enumerate *udev_enumerate;
|
||||
struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
|
||||
char base[UTIL_PATH_SIZE];
|
||||
struct stat statbuf;
|
||||
const char *subsysdir;
|
||||
|
||||
if (udev == NULL)
|
||||
return NULL;
|
||||
|
||||
udev_enumerate = enumerate_new(udev);
|
||||
if (udev_enumerate == NULL)
|
||||
return NULL;
|
||||
return -EINVAL;
|
||||
|
||||
util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
|
||||
util_strlcat(base, "/subsystem", sizeof(base));
|
||||
@ -295,5 +300,5 @@ struct udev_enumerate *udev_enumerate_new_from_subsystems(struct udev *udev)
|
||||
devices_scan_subsystems(udev, subsysdir, "/drivers",
|
||||
NULL, NULL,
|
||||
&udev_enumerate->devices_list);
|
||||
return udev_enumerate;
|
||||
return 0;
|
||||
}
|
||||
|
@ -94,11 +94,12 @@ extern struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev
|
||||
|
||||
/* sys enumeration */
|
||||
struct udev_enumerate;
|
||||
extern struct udev_enumerate *udev_enumerate_new_from_devices(struct udev *udev, const char *subsystem, ...);
|
||||
extern struct udev_enumerate *udev_enumerate_new_from_subsystems(struct udev *udev);
|
||||
extern struct udev_enumerate *udev_enumerate_new(struct udev *udev);
|
||||
extern struct udev_enumerate *udev_enumerate_ref(struct udev_enumerate *udev_enumerate);
|
||||
extern struct udev *udev_enumerate_get_udev(struct udev_enumerate *udev_enumerate);
|
||||
extern void udev_enumerate_unref(struct udev_enumerate *udev_enumerate);
|
||||
extern int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate, const char *subsystem, ...);
|
||||
extern int udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate);
|
||||
extern struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *udev_enumerate);
|
||||
|
||||
#endif
|
||||
|
@ -242,7 +242,7 @@ int main(int argc, char *argv[], char *envp[])
|
||||
{ "version", 0, NULL, 'V' },
|
||||
{}
|
||||
};
|
||||
struct udev_enumerate *enumerate;
|
||||
struct udev_enumerate *udev_enumerate;
|
||||
const char *syspath = "/devices/virtual/mem/null";
|
||||
const char *subsystem = NULL;
|
||||
const char *socket = "@/org/kernel/udev/monitor";
|
||||
@ -306,39 +306,44 @@ int main(int argc, char *argv[], char *envp[])
|
||||
test_device_parents(udev, syspath);
|
||||
|
||||
printf("enumerate '%s'\n", subsystem == NULL ? "<all>" : subsystem);
|
||||
enumerate = udev_enumerate_new_from_devices(udev, subsystem, NULL);
|
||||
if (enumerate == NULL)
|
||||
udev_enumerate = udev_enumerate_new(udev);
|
||||
if (udev_enumerate == NULL)
|
||||
return -1;
|
||||
test_enumerate_print_list(enumerate);
|
||||
udev_enumerate_unref(enumerate);
|
||||
udev_enumerate_scan_devices(udev_enumerate, subsystem, NULL);
|
||||
test_enumerate_print_list(udev_enumerate);
|
||||
udev_enumerate_unref(udev_enumerate);
|
||||
|
||||
printf("enumerate 'block'\n");
|
||||
enumerate = udev_enumerate_new_from_devices(udev, "block", NULL);
|
||||
if (enumerate == NULL)
|
||||
udev_enumerate = udev_enumerate_new(udev);
|
||||
if (udev_enumerate == NULL)
|
||||
return -1;
|
||||
test_enumerate_print_list(enumerate);
|
||||
udev_enumerate_unref(enumerate);
|
||||
udev_enumerate_scan_devices(udev_enumerate, "block", NULL);
|
||||
test_enumerate_print_list(udev_enumerate);
|
||||
udev_enumerate_unref(udev_enumerate);
|
||||
|
||||
printf("enumerate '!block'\n");
|
||||
enumerate = udev_enumerate_new_from_devices(udev, "!block", NULL);
|
||||
if (enumerate == NULL)
|
||||
udev_enumerate = udev_enumerate_new(udev);
|
||||
if (udev_enumerate == NULL)
|
||||
return -1;
|
||||
test_enumerate_print_list(enumerate);
|
||||
udev_enumerate_unref(enumerate);
|
||||
udev_enumerate_scan_devices(udev_enumerate, "!block", NULL);
|
||||
test_enumerate_print_list(udev_enumerate);
|
||||
udev_enumerate_unref(udev_enumerate);
|
||||
|
||||
printf("enumerate 'pci, mem, vc'\n");
|
||||
enumerate = udev_enumerate_new_from_devices(udev, "pci", "mem", "vc", NULL);
|
||||
if (enumerate == NULL)
|
||||
udev_enumerate = udev_enumerate_new(udev);
|
||||
if (udev_enumerate == NULL)
|
||||
return -1;
|
||||
test_enumerate_print_list(enumerate);
|
||||
udev_enumerate_unref(enumerate);
|
||||
udev_enumerate_scan_devices(udev_enumerate, "pci", "mem", "vc", NULL);
|
||||
test_enumerate_print_list(udev_enumerate);
|
||||
udev_enumerate_unref(udev_enumerate);
|
||||
|
||||
printf("enumerate 'subsystem'\n");
|
||||
enumerate = udev_enumerate_new_from_subsystems(udev);
|
||||
if (enumerate == NULL)
|
||||
udev_enumerate = udev_enumerate_new(udev);
|
||||
if (udev_enumerate == NULL)
|
||||
return -1;
|
||||
test_enumerate_print_list(enumerate);
|
||||
udev_enumerate_unref(enumerate);
|
||||
udev_enumerate_scan_subsystems(udev_enumerate);
|
||||
test_enumerate_print_list(udev_enumerate);
|
||||
udev_enumerate_unref(udev_enumerate);
|
||||
|
||||
test_monitor(udev, socket);
|
||||
out:
|
||||
|
@ -183,13 +183,14 @@ static int stat_device(const char *name, int export, const char *prefix)
|
||||
|
||||
static int export_devices(struct udev *udev)
|
||||
{
|
||||
struct udev_enumerate *enumerate;
|
||||
struct udev_enumerate *udev_enumerate;
|
||||
struct udev_list_entry *list_entry;
|
||||
|
||||
enumerate = udev_enumerate_new_from_devices(udev, NULL);
|
||||
if (enumerate == NULL)
|
||||
udev_enumerate = udev_enumerate_new(udev);
|
||||
if (udev_enumerate == NULL)
|
||||
return -1;
|
||||
udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
|
||||
udev_enumerate_scan_devices(udev_enumerate, NULL);
|
||||
udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
|
||||
struct udev_device *device;
|
||||
|
||||
device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
|
||||
@ -199,7 +200,7 @@ static int export_devices(struct udev *udev)
|
||||
udev_device_unref(device);
|
||||
}
|
||||
}
|
||||
udev_enumerate_unref(enumerate);
|
||||
udev_enumerate_unref(udev_enumerate);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user