2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2008-08-27 17:11:58 +02:00
|
|
|
|
2018-11-16 03:44:17 +01:00
|
|
|
#include <errno.h>
|
2008-09-12 00:58:40 +02:00
|
|
|
#include <getopt.h>
|
2011-04-15 12:18:06 +02:00
|
|
|
#include <sys/epoll.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <unistd.h>
|
2008-08-28 23:05:01 +02:00
|
|
|
|
2018-11-21 08:17:29 +01:00
|
|
|
#include "alloc-util.h"
|
2018-12-20 20:35:25 +01:00
|
|
|
#include "build.h"
|
2016-03-02 03:40:56 +01:00
|
|
|
#include "fd-util.h"
|
2018-11-21 05:43:55 +01:00
|
|
|
#include "libudev-list-internal.h"
|
2018-11-16 03:44:17 +01:00
|
|
|
#include "libudev-util.h"
|
2016-03-02 03:40:56 +01:00
|
|
|
#include "log.h"
|
2019-01-21 09:58:51 +01:00
|
|
|
#include "main-func.h"
|
2016-01-12 15:34:20 +01:00
|
|
|
#include "stdio-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2019-01-21 09:58:51 +01:00
|
|
|
#include "tests.h"
|
|
|
|
|
|
|
|
static bool arg_monitor = false;
|
2008-08-27 17:11:58 +02:00
|
|
|
|
2013-12-03 22:27:45 +01:00
|
|
|
static void print_device(struct udev_device *device) {
|
2012-01-10 01:34:15 +01:00
|
|
|
const char *str;
|
|
|
|
dev_t devnum;
|
|
|
|
int count;
|
|
|
|
struct udev_list_entry *list_entry;
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("*** device: %p ***", device);
|
2012-01-10 01:34:15 +01:00
|
|
|
str = udev_device_get_action(device);
|
2019-01-21 09:58:51 +01:00
|
|
|
if (str)
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("action: '%s'", str);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
str = udev_device_get_syspath(device);
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("syspath: '%s'", str);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
str = udev_device_get_sysname(device);
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("sysname: '%s'", str);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
str = udev_device_get_sysnum(device);
|
2019-01-21 09:58:51 +01:00
|
|
|
if (str)
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("sysnum: '%s'", str);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
str = udev_device_get_devpath(device);
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("devpath: '%s'", str);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
str = udev_device_get_subsystem(device);
|
2019-01-21 09:58:51 +01:00
|
|
|
if (str)
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("subsystem: '%s'", str);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
str = udev_device_get_devtype(device);
|
2019-01-21 09:58:51 +01:00
|
|
|
if (str)
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("devtype: '%s'", str);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
str = udev_device_get_driver(device);
|
2019-01-21 09:58:51 +01:00
|
|
|
if (str)
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("driver: '%s'", str);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
str = udev_device_get_devnode(device);
|
2019-01-21 09:58:51 +01:00
|
|
|
if (str)
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("devname: '%s'", str);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
devnum = udev_device_get_devnum(device);
|
|
|
|
if (major(devnum) > 0)
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("devnum: %u:%u", major(devnum), minor(devnum));
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
count = 0;
|
|
|
|
udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device)) {
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("link: '%s'", udev_list_entry_get_name(list_entry));
|
2012-01-10 01:34:15 +01:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
if (count > 0)
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("found %i links", count);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
count = 0;
|
|
|
|
udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device)) {
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("property: '%s=%s'",
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_list_entry_get_name(list_entry),
|
|
|
|
udev_list_entry_get_value(list_entry));
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
if (count > 0)
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("found %i properties", count);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
str = udev_device_get_property_value(device, "MAJOR");
|
2019-01-21 09:58:51 +01:00
|
|
|
if (str)
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("MAJOR: '%s'", str);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
str = udev_device_get_sysattr_value(device, "dev");
|
2019-01-21 09:58:51 +01:00
|
|
|
if (str)
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("attr{dev}: '%s'", str);
|
2008-08-28 23:05:01 +02:00
|
|
|
}
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
static void test_device(struct udev *udev, const char *syspath) {
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(udev_device_unrefp) struct udev_device *device;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2019-01-21 09:58:51 +01:00
|
|
|
log_info("/* %s, device %s */", __func__, syspath);
|
2012-01-10 01:34:15 +01:00
|
|
|
device = udev_device_new_from_syspath(udev, syspath);
|
2019-01-21 09:58:51 +01:00
|
|
|
if (device)
|
2016-03-02 03:40:56 +01:00
|
|
|
print_device(device);
|
2019-01-21 09:58:51 +01:00
|
|
|
else
|
|
|
|
log_warning_errno(errno, "udev_device_new_from_syspath: %m");
|
2008-08-27 17:11:58 +02:00
|
|
|
}
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
static void test_device_parents(struct udev *udev, const char *syspath) {
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(udev_device_unrefp) struct udev_device *device;
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_device *device_parent;
|
|
|
|
|
2019-01-21 09:58:51 +01:00
|
|
|
log_info("/* %s, device %s */", __func__, syspath);
|
2012-01-10 01:34:15 +01:00
|
|
|
device = udev_device_new_from_syspath(udev, syspath);
|
2020-10-09 14:59:44 +02:00
|
|
|
if (!device)
|
2016-03-02 03:40:56 +01:00
|
|
|
return;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("looking at parents");
|
2012-01-10 01:34:15 +01:00
|
|
|
device_parent = device;
|
|
|
|
do {
|
|
|
|
print_device(device_parent);
|
|
|
|
device_parent = udev_device_get_parent(device_parent);
|
|
|
|
} while (device_parent != NULL);
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("looking at parents again");
|
2012-01-10 01:34:15 +01:00
|
|
|
device_parent = device;
|
|
|
|
do {
|
|
|
|
print_device(device_parent);
|
|
|
|
device_parent = udev_device_get_parent(device_parent);
|
|
|
|
} while (device_parent != NULL);
|
2008-09-11 17:08:12 +02:00
|
|
|
}
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
static void test_device_devnum(struct udev *udev) {
|
2012-01-10 01:34:15 +01:00
|
|
|
dev_t devnum = makedev(1, 3);
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(udev_device_unrefp) struct udev_device *device;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2019-01-21 09:58:51 +01:00
|
|
|
log_info("/* %s, device %d:%d */", __func__, major(devnum), minor(devnum));
|
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
device = udev_device_new_from_devnum(udev, 'c', devnum);
|
2019-01-21 09:58:51 +01:00
|
|
|
if (device)
|
2016-03-02 03:40:56 +01:00
|
|
|
print_device(device);
|
2019-01-21 09:58:51 +01:00
|
|
|
else
|
|
|
|
log_warning_errno(errno, "udev_device_new_from_devnum: %m");
|
2008-09-22 08:28:56 +02:00
|
|
|
}
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
static void test_device_subsys_name(struct udev *udev, const char *subsys, const char *dev) {
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(udev_device_unrefp) struct udev_device *device;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("looking up device: '%s:%s'", subsys, dev);
|
|
|
|
device = udev_device_new_from_subsystem_sysname(udev, subsys, dev);
|
2020-10-09 14:59:44 +02:00
|
|
|
if (!device)
|
2016-03-02 03:40:56 +01:00
|
|
|
log_warning_errno(errno, "udev_device_new_from_subsystem_sysname: %m");
|
|
|
|
else
|
|
|
|
print_device(device);
|
2008-10-07 20:20:34 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 09:58:51 +01:00
|
|
|
static int enumerate_print_list(struct udev_enumerate *enumerate) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_list_entry *list_entry;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
|
|
|
|
struct udev_device *device;
|
|
|
|
|
|
|
|
device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
|
|
|
|
udev_list_entry_get_name(list_entry));
|
2019-04-28 14:28:49 +02:00
|
|
|
if (device) {
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("device: '%s' (%s)",
|
|
|
|
udev_device_get_syspath(device),
|
|
|
|
udev_device_get_subsystem(device));
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_device_unref(device);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("found %i devices", count);
|
2012-01-10 01:34:15 +01:00
|
|
|
return count;
|
2008-08-28 23:05:01 +02:00
|
|
|
}
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
static void test_monitor(struct udev *udev) {
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(udev_monitor_unrefp) struct udev_monitor *udev_monitor;
|
2016-03-02 03:40:56 +01:00
|
|
|
_cleanup_close_ int fd_ep;
|
|
|
|
int fd_udev;
|
|
|
|
struct epoll_event ep_udev = {
|
|
|
|
.events = EPOLLIN,
|
|
|
|
}, ep_stdin = {
|
|
|
|
.events = EPOLLIN,
|
|
|
|
.data.fd = STDIN_FILENO,
|
|
|
|
};
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2019-01-21 09:58:51 +01:00
|
|
|
log_info("/* %s */", __func__);
|
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
fd_ep = epoll_create1(EPOLL_CLOEXEC);
|
2016-03-02 03:40:56 +01:00
|
|
|
assert_se(fd_ep >= 0);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
|
2016-03-02 03:40:56 +01:00
|
|
|
assert_se(udev_monitor != NULL);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
fd_udev = udev_monitor_get_fd(udev_monitor);
|
|
|
|
ep_udev.data.fd = fd_udev;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
assert_se(udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "block", NULL) >= 0);
|
|
|
|
assert_se(udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "tty", NULL) >= 0);
|
|
|
|
assert_se(udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device") >= 0);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
assert_se(udev_monitor_enable_receiving(udev_monitor) >= 0);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
assert_se(epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) >= 0);
|
|
|
|
assert_se(epoll_ctl(fd_ep, EPOLL_CTL_ADD, STDIN_FILENO, &ep_stdin) >= 0);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int fdcount;
|
|
|
|
struct epoll_event ev[4];
|
|
|
|
struct udev_device *device;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("waiting for events from udev, press ENTER to exit\n");
|
2016-03-02 03:40:56 +01:00
|
|
|
fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
|
2012-01-10 01:34:15 +01:00
|
|
|
printf("epoll fd count: %i\n", fdcount);
|
|
|
|
|
|
|
|
for (i = 0; i < fdcount; i++) {
|
|
|
|
if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) {
|
|
|
|
device = udev_monitor_receive_device(udev_monitor);
|
2020-10-09 14:59:44 +02:00
|
|
|
if (!device) {
|
2012-01-10 01:34:15 +01:00
|
|
|
printf("no device from socket\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
print_device(device);
|
|
|
|
udev_device_unref(device);
|
|
|
|
} else if (ev[i].data.fd == STDIN_FILENO && ev[i].events & EPOLLIN) {
|
|
|
|
printf("exiting loop\n");
|
2016-03-02 03:40:56 +01:00
|
|
|
return;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-27 17:11:58 +02:00
|
|
|
}
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
static void test_queue(struct udev *udev) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_queue *udev_queue;
|
2016-03-02 03:40:56 +01:00
|
|
|
bool empty;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2019-01-21 09:58:51 +01:00
|
|
|
log_info("/* %s */", __func__);
|
|
|
|
|
|
|
|
assert_se(udev_queue = udev_queue_new(udev));
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
empty = udev_queue_get_queue_is_empty(udev_queue);
|
|
|
|
log_info("queue is %s", empty ? "empty" : "not empty");
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_queue_unref(udev_queue);
|
2008-10-01 09:34:07 +02:00
|
|
|
}
|
|
|
|
|
2013-12-03 22:27:45 +01:00
|
|
|
static int test_enumerate(struct udev *udev, const char *subsystem) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_enumerate *udev_enumerate;
|
2015-05-06 18:37:06 +02:00
|
|
|
int r;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2019-01-21 09:58:51 +01:00
|
|
|
log_info("/* %s */", __func__);
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("enumerate '%s'", subsystem == NULL ? "<all>" : subsystem);
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate = udev_enumerate_new(udev);
|
2019-04-28 14:28:49 +02:00
|
|
|
if (!udev_enumerate)
|
2012-01-10 01:34:15 +01:00
|
|
|
return -1;
|
|
|
|
udev_enumerate_add_match_subsystem(udev_enumerate, subsystem);
|
|
|
|
udev_enumerate_scan_devices(udev_enumerate);
|
2019-01-21 09:58:51 +01:00
|
|
|
enumerate_print_list(udev_enumerate);
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate_unref(udev_enumerate);
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("enumerate 'net' + duplicated scan + null + zero");
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate = udev_enumerate_new(udev);
|
2019-04-28 14:28:49 +02:00
|
|
|
if (!udev_enumerate)
|
2012-01-10 01:34:15 +01:00
|
|
|
return -1;
|
|
|
|
udev_enumerate_add_match_subsystem(udev_enumerate, "net");
|
|
|
|
udev_enumerate_scan_devices(udev_enumerate);
|
|
|
|
udev_enumerate_scan_devices(udev_enumerate);
|
|
|
|
udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
|
|
|
|
udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
|
|
|
|
udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
|
|
|
|
udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
|
|
|
|
udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
|
|
|
|
udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
|
|
|
|
udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
|
|
|
|
udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
|
|
|
|
udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
|
|
|
|
udev_enumerate_scan_devices(udev_enumerate);
|
2019-01-21 09:58:51 +01:00
|
|
|
enumerate_print_list(udev_enumerate);
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate_unref(udev_enumerate);
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("enumerate 'block'");
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate = udev_enumerate_new(udev);
|
2019-04-28 14:28:49 +02:00
|
|
|
if (!udev_enumerate)
|
2012-01-10 01:34:15 +01:00
|
|
|
return -1;
|
|
|
|
udev_enumerate_add_match_subsystem(udev_enumerate,"block");
|
2015-05-06 18:37:06 +02:00
|
|
|
r = udev_enumerate_add_match_is_initialized(udev_enumerate);
|
2015-05-11 21:41:31 +02:00
|
|
|
if (r < 0) {
|
|
|
|
udev_enumerate_unref(udev_enumerate);
|
2015-05-06 18:37:06 +02:00
|
|
|
return r;
|
2015-05-11 21:41:31 +02:00
|
|
|
}
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate_scan_devices(udev_enumerate);
|
2019-01-21 09:58:51 +01:00
|
|
|
enumerate_print_list(udev_enumerate);
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate_unref(udev_enumerate);
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("enumerate 'not block'");
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate = udev_enumerate_new(udev);
|
2019-04-28 14:28:49 +02:00
|
|
|
if (!udev_enumerate)
|
2012-01-10 01:34:15 +01:00
|
|
|
return -1;
|
|
|
|
udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
|
|
|
|
udev_enumerate_scan_devices(udev_enumerate);
|
2019-01-21 09:58:51 +01:00
|
|
|
enumerate_print_list(udev_enumerate);
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate_unref(udev_enumerate);
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("enumerate 'pci, mem, vc'");
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate = udev_enumerate_new(udev);
|
2019-04-28 14:28:49 +02:00
|
|
|
if (!udev_enumerate)
|
2012-01-10 01:34:15 +01:00
|
|
|
return -1;
|
|
|
|
udev_enumerate_add_match_subsystem(udev_enumerate, "pci");
|
|
|
|
udev_enumerate_add_match_subsystem(udev_enumerate, "mem");
|
|
|
|
udev_enumerate_add_match_subsystem(udev_enumerate, "vc");
|
|
|
|
udev_enumerate_scan_devices(udev_enumerate);
|
2019-01-21 09:58:51 +01:00
|
|
|
enumerate_print_list(udev_enumerate);
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate_unref(udev_enumerate);
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("enumerate 'subsystem'");
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate = udev_enumerate_new(udev);
|
2019-04-28 14:28:49 +02:00
|
|
|
if (!udev_enumerate)
|
2012-01-10 01:34:15 +01:00
|
|
|
return -1;
|
|
|
|
udev_enumerate_scan_subsystems(udev_enumerate);
|
2019-01-21 09:58:51 +01:00
|
|
|
enumerate_print_list(udev_enumerate);
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate_unref(udev_enumerate);
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("enumerate 'property IF_FS_*=filesystem'");
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate = udev_enumerate_new(udev);
|
2019-04-28 14:28:49 +02:00
|
|
|
if (!udev_enumerate)
|
2012-01-10 01:34:15 +01:00
|
|
|
return -1;
|
|
|
|
udev_enumerate_add_match_property(udev_enumerate, "ID_FS*", "filesystem");
|
|
|
|
udev_enumerate_scan_devices(udev_enumerate);
|
2019-01-21 09:58:51 +01:00
|
|
|
enumerate_print_list(udev_enumerate);
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate_unref(udev_enumerate);
|
|
|
|
return 0;
|
2008-10-07 20:20:34 +02:00
|
|
|
}
|
|
|
|
|
2013-10-12 01:34:17 +02:00
|
|
|
static void test_hwdb(struct udev *udev, const char *modalias) {
|
2013-08-15 00:55:11 +02:00
|
|
|
struct udev_hwdb *hwdb;
|
2012-10-27 20:59:01 +02:00
|
|
|
struct udev_list_entry *entry;
|
|
|
|
|
2019-01-21 09:58:51 +01:00
|
|
|
log_info("/* %s */", __func__);
|
|
|
|
|
2012-10-27 20:59:01 +02:00
|
|
|
hwdb = udev_hwdb_new(udev);
|
2019-01-21 09:58:51 +01:00
|
|
|
if (!hwdb)
|
|
|
|
log_warning_errno(errno, "Failed to open hwdb: %m");
|
2012-10-27 20:59:01 +02:00
|
|
|
|
|
|
|
udev_list_entry_foreach(entry, udev_hwdb_get_properties_list_entry(hwdb, modalias, 0))
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("'%s'='%s'", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry));
|
2012-10-27 20:59:01 +02:00
|
|
|
|
|
|
|
hwdb = udev_hwdb_unref(hwdb);
|
2014-10-04 23:51:45 +02:00
|
|
|
assert_se(hwdb == NULL);
|
2012-10-27 20:59:01 +02:00
|
|
|
}
|
|
|
|
|
2018-11-21 09:29:34 +01:00
|
|
|
static void test_util_resolve_subsys_kernel_one(const char *str, bool read_value, int retval, const char *expected) {
|
2019-01-21 09:58:51 +01:00
|
|
|
char result[UTIL_PATH_SIZE] = "";
|
2018-11-21 09:29:34 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
r = util_resolve_subsys_kernel(str, result, sizeof(result), read_value);
|
2019-01-21 09:58:51 +01:00
|
|
|
log_info("\"%s\" → expect: \"%s\", %d, actual: \"%s\", %d", str, strnull(expected), retval, result, r);
|
2018-11-21 09:29:34 +01:00
|
|
|
assert_se(r == retval);
|
|
|
|
if (r >= 0)
|
|
|
|
assert_se(streq(result, expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_util_resolve_subsys_kernel(void) {
|
2019-01-21 09:58:51 +01:00
|
|
|
log_info("/* %s */", __func__);
|
|
|
|
|
2018-11-21 09:29:34 +01:00
|
|
|
test_util_resolve_subsys_kernel_one("hoge", false, -EINVAL, NULL);
|
|
|
|
test_util_resolve_subsys_kernel_one("[hoge", false, -EINVAL, NULL);
|
|
|
|
test_util_resolve_subsys_kernel_one("[hoge/foo", false, -EINVAL, NULL);
|
|
|
|
test_util_resolve_subsys_kernel_one("[hoge/]", false, -ENODEV, NULL);
|
|
|
|
|
|
|
|
test_util_resolve_subsys_kernel_one("[net/lo]", false, 0, "/sys/devices/virtual/net/lo");
|
|
|
|
test_util_resolve_subsys_kernel_one("[net/lo]/", false, 0, "/sys/devices/virtual/net/lo");
|
|
|
|
test_util_resolve_subsys_kernel_one("[net/lo]hoge", false, 0, "/sys/devices/virtual/net/lo/hoge");
|
|
|
|
test_util_resolve_subsys_kernel_one("[net/lo]/hoge", false, 0, "/sys/devices/virtual/net/lo/hoge");
|
|
|
|
|
|
|
|
test_util_resolve_subsys_kernel_one("[net/lo]", true, -EINVAL, NULL);
|
|
|
|
test_util_resolve_subsys_kernel_one("[net/lo]/", true, -EINVAL, NULL);
|
|
|
|
test_util_resolve_subsys_kernel_one("[net/lo]hoge", true, 0, "");
|
|
|
|
test_util_resolve_subsys_kernel_one("[net/lo]/hoge", true, 0, "");
|
|
|
|
test_util_resolve_subsys_kernel_one("[net/lo]address", true, 0, "00:00:00:00:00:00");
|
|
|
|
test_util_resolve_subsys_kernel_one("[net/lo]/address", true, 0, "00:00:00:00:00:00");
|
|
|
|
}
|
|
|
|
|
2018-11-21 05:43:55 +01:00
|
|
|
static void test_list(void) {
|
2019-06-12 06:03:19 +02:00
|
|
|
_cleanup_(udev_list_freep) struct udev_list *list = NULL;
|
2018-11-21 05:43:55 +01:00
|
|
|
struct udev_list_entry *e;
|
|
|
|
|
|
|
|
/* empty list */
|
2019-06-12 06:03:19 +02:00
|
|
|
assert_se(list = udev_list_new(false));
|
|
|
|
assert_se(!udev_list_get_entry(list));
|
|
|
|
list = udev_list_free(list);
|
2018-11-21 05:43:55 +01:00
|
|
|
|
|
|
|
/* unique == false */
|
2019-06-12 06:03:19 +02:00
|
|
|
assert_se(list = udev_list_new(false));
|
|
|
|
assert_se(udev_list_entry_add(list, "aaa", "hoge"));
|
|
|
|
assert_se(udev_list_entry_add(list, "aaa", "hogehoge"));
|
|
|
|
assert_se(udev_list_entry_add(list, "bbb", "foo"));
|
|
|
|
e = udev_list_get_entry(list);
|
2018-11-21 05:43:55 +01:00
|
|
|
assert_se(e);
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_name(e), "aaa"));
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_value(e), "hoge"));
|
|
|
|
e = udev_list_entry_get_next(e);
|
|
|
|
assert_se(e);
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_name(e), "aaa"));
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_value(e), "hogehoge"));
|
|
|
|
e = udev_list_entry_get_next(e);
|
|
|
|
assert_se(e);
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_name(e), "bbb"));
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_value(e), "foo"));
|
|
|
|
assert_se(!udev_list_entry_get_next(e));
|
|
|
|
|
|
|
|
assert_se(!udev_list_entry_get_by_name(e, "aaa"));
|
|
|
|
assert_se(!udev_list_entry_get_by_name(e, "bbb"));
|
|
|
|
assert_se(!udev_list_entry_get_by_name(e, "ccc"));
|
2019-06-12 06:03:19 +02:00
|
|
|
list = udev_list_free(list);
|
2018-11-21 05:43:55 +01:00
|
|
|
|
|
|
|
/* unique == true */
|
2019-06-12 06:03:19 +02:00
|
|
|
assert_se(list = udev_list_new(true));
|
|
|
|
assert_se(udev_list_entry_add(list, "aaa", "hoge"));
|
|
|
|
assert_se(udev_list_entry_add(list, "aaa", "hogehoge"));
|
|
|
|
assert_se(udev_list_entry_add(list, "bbb", "foo"));
|
|
|
|
e = udev_list_get_entry(list);
|
2018-11-21 05:43:55 +01:00
|
|
|
assert_se(e);
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_name(e), "aaa"));
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_value(e), "hogehoge"));
|
|
|
|
e = udev_list_entry_get_next(e);
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_name(e), "bbb"));
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_value(e), "foo"));
|
|
|
|
assert_se(!udev_list_entry_get_next(e));
|
|
|
|
|
|
|
|
e = udev_list_entry_get_by_name(e, "bbb");
|
|
|
|
assert_se(e);
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_name(e), "bbb"));
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_value(e), "foo"));
|
|
|
|
e = udev_list_entry_get_by_name(e, "aaa");
|
|
|
|
assert_se(e);
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_name(e), "aaa"));
|
|
|
|
assert_se(streq_ptr(udev_list_entry_get_value(e), "hogehoge"));
|
|
|
|
assert_se(!udev_list_entry_get_by_name(e, "ccc"));
|
|
|
|
}
|
|
|
|
|
2019-01-21 09:58:51 +01:00
|
|
|
static int parse_args(int argc, char *argv[], const char **syspath, const char **subsystem) {
|
2012-01-10 01:34:15 +01:00
|
|
|
static const struct option options[] = {
|
2016-03-02 03:40:56 +01:00
|
|
|
{ "syspath", required_argument, NULL, 'p' },
|
2012-01-10 01:34:15 +01:00
|
|
|
{ "subsystem", required_argument, NULL, 's' },
|
2016-03-02 03:40:56 +01:00
|
|
|
{ "debug", no_argument, NULL, 'd' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
2016-03-02 03:44:08 +01:00
|
|
|
{ "monitor", no_argument, NULL, 'm' },
|
2012-01-10 01:34:15 +01:00
|
|
|
{}
|
|
|
|
};
|
2014-08-02 17:12:21 +02:00
|
|
|
int c;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-02-28 02:28:50 +01:00
|
|
|
while ((c = getopt_long(argc, argv, "p:s:dhVm", options, NULL)) >= 0)
|
2014-08-02 17:12:21 +02:00
|
|
|
switch (c) {
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'p':
|
2019-01-21 09:58:51 +01:00
|
|
|
*syspath = optarg;
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
2014-08-02 17:12:21 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
case 's':
|
2019-01-21 09:58:51 +01:00
|
|
|
*subsystem = optarg;
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
2014-08-02 17:12:21 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'd':
|
2019-01-21 09:58:51 +01:00
|
|
|
log_set_max_level(LOG_DEBUG);
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
2014-08-02 17:12:21 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'h':
|
|
|
|
printf("--debug --syspath= --subsystem= --help\n");
|
2019-01-21 09:58:51 +01:00
|
|
|
return 0;
|
2014-08-02 17:12:21 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'V':
|
2018-12-20 20:35:25 +01:00
|
|
|
printf("%s\n", GIT_VERSION);
|
2019-01-21 09:58:51 +01:00
|
|
|
return 0;
|
2014-08-02 17:12:21 +02:00
|
|
|
|
2016-03-02 03:44:08 +01:00
|
|
|
case 'm':
|
|
|
|
arg_monitor = true;
|
|
|
|
break;
|
|
|
|
|
2014-08-02 17:12:21 +02:00
|
|
|
case '?':
|
2019-01-21 09:58:51 +01:00
|
|
|
return -EINVAL;
|
2014-08-02 17:12:21 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert_not_reached("Unhandled option code.");
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
2014-08-02 17:12:21 +02:00
|
|
|
|
2019-01-21 09:58:51 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run(int argc, char *argv[]) {
|
|
|
|
_cleanup_(udev_unrefp) struct udev *udev = NULL;
|
|
|
|
|
|
|
|
const char *syspath = "/devices/virtual/mem/null";
|
|
|
|
const char *subsystem = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
test_setup_logging(LOG_INFO);
|
|
|
|
|
|
|
|
r = parse_args(argc, argv, &syspath, &subsystem);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
assert_se(udev = udev_new());
|
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
/* add sys path if needed */
|
2016-03-02 03:40:56 +01:00
|
|
|
if (!startswith(syspath, "/sys"))
|
|
|
|
syspath = strjoina("/sys/", syspath);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
test_device(udev, syspath);
|
|
|
|
test_device_devnum(udev);
|
2016-03-02 03:40:56 +01:00
|
|
|
test_device_subsys_name(udev, "block", "sda");
|
|
|
|
test_device_subsys_name(udev, "subsystem", "pci");
|
|
|
|
test_device_subsys_name(udev, "drivers", "scsi:sd");
|
|
|
|
test_device_subsys_name(udev, "module", "printk");
|
2012-01-10 01:34:15 +01:00
|
|
|
test_device_parents(udev, syspath);
|
|
|
|
|
|
|
|
test_enumerate(udev, subsystem);
|
|
|
|
|
|
|
|
test_queue(udev);
|
|
|
|
|
2012-10-27 20:59:01 +02:00
|
|
|
test_hwdb(udev, "usb:v0D50p0011*");
|
|
|
|
|
2016-03-02 03:44:08 +01:00
|
|
|
if (arg_monitor)
|
|
|
|
test_monitor(udev);
|
2016-03-02 03:40:56 +01:00
|
|
|
|
2018-11-21 09:29:34 +01:00
|
|
|
test_util_resolve_subsys_kernel();
|
2018-11-21 08:17:29 +01:00
|
|
|
|
2018-11-21 05:43:55 +01:00
|
|
|
test_list();
|
|
|
|
|
2019-01-21 09:58:51 +01:00
|
|
|
return 0;
|
2008-08-27 17:11:58 +02:00
|
|
|
}
|
2019-01-21 09:58:51 +01:00
|
|
|
|
|
|
|
DEFINE_MAIN_FUNCTION(run);
|