test-libudev.c revision 1298001ec5e320f9f9b6a9b925c8939b2579396d
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering int priority, const char *file, int line, const char *fn,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("test-libudev: %s %s:%d ", fn, file, line);
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poetteringstatic void print_device(struct udev_device *device)
5bcd08db289cd02aad8a89b37b2a46244a7bd473Lennart Poettering printf("devnum: %u:%u\n", major(devnum), minor(devnum));
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device)) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("link: '%s'\n", udev_list_entry_get_name(list_entry));
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device)) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering str = udev_device_get_property_value(device, "MAJOR");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering str = udev_device_get_sysattr_value(device, "dev");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int test_device(struct udev *udev, const char *syspath)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("looking at device: %s\n", syspath);
5bcd08db289cd02aad8a89b37b2a46244a7bd473Lennart Poettering device = udev_device_new_from_syspath(udev, syspath);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int test_device_parents(struct udev *udev, const char *syspath)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("looking at device: %s\n", syspath);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering device = udev_device_new_from_syspath(udev, syspath);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering device_parent = udev_device_get_parent(device_parent);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering device_parent = udev_device_get_parent(device_parent);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poetteringstatic int test_device_devnum(struct udev *udev)
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering printf("looking up device: %u:%u\n", major(devnum), minor(devnum));
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering device = udev_device_new_from_devnum(udev, 'c', devnum);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poetteringstatic int test_device_subsys_name(struct udev *udev)
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering printf("looking up device: 'block':'sda'\n");
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering device = udev_device_new_from_subsystem_sysname(udev, "block", "sda");
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering printf("looking up device: 'subsystem':'pci'\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering device = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("looking up device: 'drivers':'scsi:sd'\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering device = udev_device_new_from_subsystem_sysname(udev, "drivers", "scsi:sd");
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering printf("looking up device: 'module':'printk'\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering device = udev_device_new_from_subsystem_sysname(udev, "module", "printk");
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poetteringstatic int test_enumerate_print_list(struct udev_enumerate *enumerate)
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek udev_device_get_syspath(device),
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic int test_monitor(struct udev *udev)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering struct udev_monitor *udev_monitor = NULL;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering fd_udev = udev_monitor_get_fd(udev_monitor);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "block", NULL) < 0 ||
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "tty", NULL) < 0 ||
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device") < 0) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (udev_monitor_enable_receiving(udev_monitor) < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering memset(&ep_udev, 0, sizeof(struct epoll_event));
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0) {
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering memset(&ep_stdin, 0, sizeof(struct epoll_event));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, STDIN_FILENO, &ep_stdin) < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("waiting for events from udev, press ENTER to exit\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), -1);
7f0d207d2c816e0a8cb2742b0a789911f7c99356Lennart Poettering for (i = 0; i < fdcount; i++) {
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering device = udev_monitor_receive_device(udev_monitor);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (ev[i].data.fd == STDIN_FILENO && ev[i].events & EPOLLIN) {
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering unsigned long long int seqnum;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering seqnum = udev_queue_get_kernel_seqnum(udev_queue);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering seqnum = udev_queue_get_udev_seqnum(udev_queue);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (udev_queue_get_queue_is_empty(udev_queue))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue))
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("queued: '%s' [%s]\n", udev_list_entry_get_name(list_entry), udev_list_entry_get_value(list_entry));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue))
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering printf("queued: '%s' [%s]\n", udev_list_entry_get_name(list_entry), udev_list_entry_get_value(list_entry));
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering list_entry = udev_queue_get_queued_list_entry(udev_queue);
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering printf("event [%llu] is queued\n", seqnum);
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering seqnum = strtoull(udev_list_entry_get_value(list_entry), NULL, 10);
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering if (udev_queue_get_seqnum_is_finished(udev_queue, seqnum))
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering printf("event [%llu] is not finished\n", seqnum);
554604b3073467af75dc94fac9e2343148603289Lennart Poettering printf("event [%llu] is finished\n", seqnum);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poetteringstatic int test_enumerate(struct udev *udev, const char *subsystem)
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering printf("enumerate '%s'\n", subsystem == NULL ? "<all>" : subsystem);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate = udev_enumerate_new(udev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_match_subsystem(udev_enumerate, subsystem);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_scan_devices(udev_enumerate);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_enumerate_print_list(udev_enumerate);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering printf("enumerate 'net' + duplicated scan + null + zero\n");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate = udev_enumerate_new(udev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_match_subsystem(udev_enumerate, "net");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_scan_devices(udev_enumerate);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_scan_devices(udev_enumerate);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_enumerate_scan_devices(udev_enumerate);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering test_enumerate_print_list(udev_enumerate);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_enumerate = udev_enumerate_new(udev);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_add_match_subsystem(udev_enumerate,"block");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_add_match_is_initialized(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_scan_devices(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering test_enumerate_print_list(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate = udev_enumerate_new(udev);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_scan_devices(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering test_enumerate_print_list(udev_enumerate);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering udev_enumerate = udev_enumerate_new(udev);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_add_match_subsystem(udev_enumerate, "pci");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_add_match_subsystem(udev_enumerate, "mem");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_add_match_subsystem(udev_enumerate, "vc");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_scan_devices(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering test_enumerate_print_list(udev_enumerate);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate = udev_enumerate_new(udev);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_enumerate_scan_subsystems(udev_enumerate);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_enumerate_print_list(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering printf("enumerate 'property IF_FS_*=filesystem'\n");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate = udev_enumerate_new(udev);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering udev_enumerate_add_match_property(udev_enumerate, "ID_FS*", "filesystem");
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_enumerate_scan_devices(udev_enumerate);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering test_enumerate_print_list(udev_enumerate);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poetteringstatic int test_hwdb(struct udev *udev, const char *modalias) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering udev_list_entry_foreach(entry, udev_hwdb_get_properties_list_entry(hwdb, modalias, 0))
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering printf("'%s'='%s'\n", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry));
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering { "syspath", required_argument, NULL, 'p' },
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering { "subsystem", required_argument, NULL, 's' },
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering const char *syspath = "/devices/virtual/mem/null";
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering option = getopt_long(argc, argv, "+p:s:dhV", options, NULL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (udev_get_log_priority(udev) < LOG_INFO)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("--debug --syspath= --subsystem= --help\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* add sys path if needed */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering snprintf(path, sizeof(path), "/sys/%s", syspath);