2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2008-08-27 17:11:58 +02:00
|
|
|
|
2008-09-12 00:58:40 +02:00
|
|
|
#include <getopt.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stdio.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
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
#include "fd-util.h"
|
2018-08-22 09:25:17 +02:00
|
|
|
#include "libudev-private.h"
|
2016-03-02 03:40:56 +01:00
|
|
|
#include "log.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"
|
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);
|
|
|
|
if (str != NULL)
|
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);
|
|
|
|
if (str != NULL)
|
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);
|
|
|
|
if (str != NULL)
|
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);
|
|
|
|
if (str != NULL)
|
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);
|
|
|
|
if (str != NULL)
|
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);
|
|
|
|
if (str != NULL)
|
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");
|
|
|
|
if (str != NULL)
|
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");
|
|
|
|
if (str != NULL)
|
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
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("looking at device: %s", syspath);
|
2012-01-10 01:34:15 +01:00
|
|
|
device = udev_device_new_from_syspath(udev, syspath);
|
2016-03-02 03:40:56 +01:00
|
|
|
if (device == NULL)
|
|
|
|
log_warning_errno(errno, "udev_device_new_from_syspath: %m");
|
|
|
|
else
|
|
|
|
print_device(device);
|
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;
|
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("looking at device: %s", syspath);
|
2012-01-10 01:34:15 +01:00
|
|
|
device = udev_device_new_from_syspath(udev, syspath);
|
|
|
|
if (device == NULL)
|
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
|
|
|
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("looking up device: %u:%u", major(devnum), minor(devnum));
|
2012-01-10 01:34:15 +01:00
|
|
|
device = udev_device_new_from_devnum(udev, 'c', devnum);
|
|
|
|
if (device == NULL)
|
2016-03-02 03:40:56 +01:00
|
|
|
log_warning_errno(errno, "udev_device_new_from_devnum: %m");
|
|
|
|
else
|
|
|
|
print_device(device);
|
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);
|
2012-01-10 01:34:15 +01:00
|
|
|
if (device == NULL)
|
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
|
|
|
}
|
|
|
|
|
2013-12-03 22:27:45 +01:00
|
|
|
static int test_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));
|
|
|
|
if (device != NULL) {
|
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
|
|
|
|
|
|
|
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);
|
|
|
|
if (device == NULL) {
|
|
|
|
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
|
|
|
|
|
|
|
udev_queue = udev_queue_new(udev);
|
2016-03-02 03:40:56 +01:00
|
|
|
assert_se(udev_queue);
|
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
|
|
|
|
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);
|
|
|
|
if (udev_enumerate == NULL)
|
|
|
|
return -1;
|
|
|
|
udev_enumerate_add_match_subsystem(udev_enumerate, subsystem);
|
|
|
|
udev_enumerate_scan_devices(udev_enumerate);
|
|
|
|
test_enumerate_print_list(udev_enumerate);
|
|
|
|
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);
|
|
|
|
if (udev_enumerate == NULL)
|
|
|
|
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);
|
|
|
|
test_enumerate_print_list(udev_enumerate);
|
|
|
|
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);
|
|
|
|
if (udev_enumerate == NULL)
|
|
|
|
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);
|
|
|
|
test_enumerate_print_list(udev_enumerate);
|
|
|
|
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);
|
|
|
|
if (udev_enumerate == NULL)
|
|
|
|
return -1;
|
|
|
|
udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
|
|
|
|
udev_enumerate_scan_devices(udev_enumerate);
|
|
|
|
test_enumerate_print_list(udev_enumerate);
|
|
|
|
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);
|
|
|
|
if (udev_enumerate == NULL)
|
|
|
|
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);
|
|
|
|
test_enumerate_print_list(udev_enumerate);
|
|
|
|
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);
|
|
|
|
if (udev_enumerate == NULL)
|
|
|
|
return -1;
|
|
|
|
udev_enumerate_scan_subsystems(udev_enumerate);
|
|
|
|
test_enumerate_print_list(udev_enumerate);
|
|
|
|
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);
|
|
|
|
if (udev_enumerate == NULL)
|
|
|
|
return -1;
|
|
|
|
udev_enumerate_add_match_property(udev_enumerate, "ID_FS*", "filesystem");
|
|
|
|
udev_enumerate_scan_devices(udev_enumerate);
|
|
|
|
test_enumerate_print_list(udev_enumerate);
|
|
|
|
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;
|
|
|
|
|
|
|
|
hwdb = udev_hwdb_new(udev);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-03 22:27:45 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
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_unrefp) struct udev *udev = NULL;
|
2016-03-02 03:44:08 +01:00
|
|
|
bool arg_monitor = false;
|
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
|
|
|
{}
|
|
|
|
};
|
|
|
|
const char *syspath = "/devices/virtual/mem/null";
|
|
|
|
const char *subsystem = NULL;
|
2014-08-02 17:12:21 +02:00
|
|
|
int c;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
udev = udev_new();
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("context: %p", udev);
|
2012-01-10 01:34:15 +01:00
|
|
|
if (udev == NULL) {
|
2016-03-02 03:40:56 +01:00
|
|
|
log_info("no context");
|
2012-01-10 01:34:15 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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':
|
|
|
|
syspath = optarg;
|
|
|
|
break;
|
2014-08-02 17:12:21 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
case 's':
|
|
|
|
subsystem = optarg;
|
|
|
|
break;
|
2014-08-02 17:12:21 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'd':
|
2014-11-13 13:11:04 +01:00
|
|
|
if (log_get_max_level() < LOG_INFO)
|
|
|
|
log_set_max_level(LOG_INFO);
|
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");
|
2016-03-02 03:40:56 +01:00
|
|
|
return EXIT_SUCCESS;
|
2014-08-02 17:12:21 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'V':
|
2017-04-03 18:15:33 +02:00
|
|
|
printf("%s\n", PACKAGE_VERSION);
|
2016-03-02 03:40:56 +01:00
|
|
|
return EXIT_SUCCESS;
|
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 '?':
|
2016-03-02 03:40:56 +01:00
|
|
|
return EXIT_FAILURE;
|
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
|
|
|
|
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
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2008-08-27 17:11:58 +02:00
|
|
|
}
|