X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udev%2Flib%2Ftest-libudev.c;h=db08d4845f31572bc078624c10201465855ca907;hp=929fd667301e344acd1023b945deae910a420964;hb=19d7e87cc0ef364fb13a18411cb165b2427b3529;hpb=7b3a52f4999fa66914868f92b6ef139ba636648b diff --git a/udev/lib/test-libudev.c b/udev/lib/test-libudev.c index 929fd6673..db08d4845 100644 --- a/udev/lib/test-libudev.c +++ b/udev/lib/test-libudev.c @@ -3,29 +3,21 @@ * * Copyright (C) 2008 Kay Sievers * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. */ -#include "config.h" - #include #include +#include #include #include #include #include #include +#include #include #include "libudev.h" @@ -38,40 +30,77 @@ static void log_fn(struct udev *udev, vprintf(format, args); } -static int print_devlinks_cb(struct udev_device *udev_device, const char *value, void *data) -{ - printf("link: '%s'\n", value); - return 0; -} - -static int print_properties_cb(struct udev_device *udev_device, const char *key, const char *value, void *data) -{ - printf("property: '%s=%s'\n", key, value); - return 0; -} - static void print_device(struct udev_device *device) { const char *str; + dev_t devnum; int count; + struct udev_list_entry *list_entry; printf("*** device: %p ***\n", device); str = udev_device_get_action(device); - printf("action: '%s'\n", str); + if (str != NULL) + printf("action: '%s'\n", str); + str = udev_device_get_syspath(device); printf("syspath: '%s'\n", str); + + str = udev_device_get_sysname(device); + printf("sysname: '%s'\n", str); + + str = udev_device_get_sysnum(device); + if (str != NULL) + printf("sysnum: '%s'\n", str); + str = udev_device_get_devpath(device); printf("devpath: '%s'\n", str); + str = udev_device_get_subsystem(device); - printf("subsystem: '%s'\n", str); + if (str != NULL) + printf("subsystem: '%s'\n", str); + + str = udev_device_get_devtype(device); + if (str != NULL) + printf("devtype: '%s'\n", str); + str = udev_device_get_driver(device); - printf("driver: '%s'\n", str); - str = udev_device_get_devname(device); - printf("devname: '%s'\n", str); - count = udev_device_get_devlinks(device, print_devlinks_cb, NULL); - printf("found %i links\n", count); - count = udev_device_get_properties(device, print_properties_cb, NULL); - printf("found %i properties\n", count); + if (str != NULL) + printf("driver: '%s'\n", str); + + str = udev_device_get_devnode(device); + if (str != NULL) + printf("devname: '%s'\n", str); + + devnum = udev_device_get_devnum(device); + if (major(devnum) > 0) + printf("devnum: %u:%u\n", major(devnum), minor(devnum)); + + count = 0; + udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device)) { + printf("link: '%s'\n", udev_list_entry_get_name(list_entry)); + count++; + } + if (count > 0) + printf("found %i links\n", count); + + count = 0; + udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device)) { + printf("property: '%s=%s'\n", + udev_list_entry_get_name(list_entry), + udev_list_entry_get_value(list_entry)); + count++; + } + if (count > 0) + printf("found %i properties\n", count); + + str = udev_device_get_property_value(device, "MAJOR"); + if (str != NULL) + printf("MAJOR: '%s'\n", str); + + str = udev_device_get_sysattr_value(device, "dev"); + if (str != NULL) + printf("attr{dev}: '%s'\n", str); + printf("\n"); } @@ -82,7 +111,7 @@ static int test_device(struct udev *udev, const char *syspath) printf("looking at device: %s\n", syspath); device = udev_device_new_from_syspath(udev, syspath); if (device == NULL) { - printf("no device\n"); + printf("no device found\n"); return -1; } print_device(device); @@ -100,12 +129,14 @@ static int test_device_parents(struct udev *udev, const char *syspath) if (device == NULL) return -1; + printf("looking at parents\n"); device_parent = device; do { print_device(device_parent); device_parent = udev_device_get_parent(device_parent); } while (device_parent != NULL); + printf("looking at parents again\n"); device_parent = device; do { print_device(device_parent); @@ -116,34 +147,93 @@ static int test_device_parents(struct udev *udev, const char *syspath) return 0; } -static int devices_enum_cb(struct udev *udev, - const char *devpath, const char *subsystem, const char *name, - void *data) +static int test_device_devnum(struct udev *udev) { - printf("device: '%s' (%s) '%s'\n", devpath, subsystem, name); + dev_t devnum = makedev(1, 3); + struct udev_device *device; + + printf("looking up device: %u:%u\n", major(devnum), minor(devnum)); + device = udev_device_new_from_devnum(udev, 'c', devnum); + if (device == NULL) + return -1; + print_device(device); + udev_device_unref(device); return 0; } -static int test_enumerate(struct udev *udev, const char *subsystem) +static int test_device_subsys_name(struct udev *udev) { - int count; + struct udev_device *device; + + printf("looking up device: 'block':'sda'\n"); + device = udev_device_new_from_subsystem_sysname(udev, "block", "sda"); + if (device == NULL) + return -1; + print_device(device); + udev_device_unref(device); + + printf("looking up device: 'subsystem':'pci'\n"); + device = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci"); + if (device == NULL) + return -1; + print_device(device); + udev_device_unref(device); - count = udev_enumerate_devices(udev, subsystem, devices_enum_cb, NULL); + printf("looking up device: 'drivers':'scsi:sd'\n"); + device = udev_device_new_from_subsystem_sysname(udev, "drivers", "scsi:sd"); + if (device == NULL) + return -1; + print_device(device); + udev_device_unref(device); + + printf("looking up device: 'module':'printk'\n"); + device = udev_device_new_from_subsystem_sysname(udev, "module", "printk"); + if (device == NULL) + return -1; + print_device(device); + udev_device_unref(device); + return 0; +} + +static int test_enumerate_print_list(struct udev_enumerate *enumerate) +{ + 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) { + printf("device: '%s' (%s)\n", + udev_device_get_syspath(device), + udev_device_get_subsystem(device)); + udev_device_unref(device); + count++; + } + } printf("found %i devices\n\n", count); return count; } -static int test_monitor(struct udev *udev, const char *socket_path) +static int test_monitor(struct udev *udev) { struct udev_monitor *udev_monitor; fd_set readfds; int fd; - udev_monitor = udev_monitor_new_from_socket(udev, socket_path); + udev_monitor = udev_monitor_new_from_netlink(udev, "udev"); if (udev_monitor == NULL) { printf("no socket\n"); return -1; } + if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "block", NULL) < 0 || + udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "tty", NULL) < 0 || + udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device") < 0) { + printf("filter failed\n"); + return -1; + } if (udev_monitor_enable_receiving(udev_monitor) < 0) { printf("bind failed\n"); return -1; @@ -159,7 +249,7 @@ static int test_monitor(struct udev *udev, const char *socket_path) FD_SET(STDIN_FILENO, &readfds); FD_SET(fd, &readfds); - printf("waiting for events on %s, press ENTER to exit\n", socket_path); + printf("waiting for events from udev, press ENTER to exit\n"); fdcount = select(fd+1, &readfds, NULL, NULL, NULL); printf("select fd count: %i\n", fdcount); @@ -183,21 +273,123 @@ static int test_monitor(struct udev *udev, const char *socket_path) return 0; } -int main(int argc, char *argv[], char *envp[]) +static int test_queue(struct udev *udev) +{ + struct udev_queue *udev_queue; + unsigned long long int seqnum; + struct udev_list_entry *list_entry; + + udev_queue = udev_queue_new(udev); + if (udev_queue == NULL) + return -1; + seqnum = udev_queue_get_kernel_seqnum(udev_queue); + printf("seqnum kernel: %llu\n", seqnum); + seqnum = udev_queue_get_udev_seqnum(udev_queue); + printf("seqnum udev : %llu\n", seqnum); + + if (udev_queue_get_queue_is_empty(udev_queue)) + printf("queue is empty\n"); + printf("get queue list\n"); + udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue)) + printf("queued: '%s' [%s]\n", udev_list_entry_get_name(list_entry), udev_list_entry_get_value(list_entry)); + printf("\n"); + printf("get queue list again\n"); + udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue)) + printf("queued: '%s' [%s]\n", udev_list_entry_get_name(list_entry), udev_list_entry_get_value(list_entry)); + printf("\n"); + printf("get failed list\n"); + udev_list_entry_foreach(list_entry, udev_queue_get_failed_list_entry(udev_queue)) + printf("failed: '%s'\n", udev_list_entry_get_name(list_entry)); + printf("\n"); + + list_entry = udev_queue_get_queued_list_entry(udev_queue); + if (list_entry != NULL) { + printf("event [%llu] is queued\n", seqnum); + seqnum = strtoull(udev_list_entry_get_value(list_entry), NULL, 10); + if (udev_queue_get_seqnum_is_finished(udev_queue, seqnum)) + printf("event [%llu] is not finished\n", seqnum); + else + printf("event [%llu] is finished\n", seqnum); + } + printf("\n"); + udev_queue_unref(udev_queue); + return 0; +} + +static int test_enumerate(struct udev *udev, const char *subsystem) +{ + struct udev_enumerate *udev_enumerate; + + printf("enumerate '%s'\n", subsystem == NULL ? "" : subsystem); + 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); + + printf("enumerate 'block'\n"); + udev_enumerate = udev_enumerate_new(udev); + if (udev_enumerate == NULL) + return -1; + udev_enumerate_add_match_subsystem(udev_enumerate,"block"); + udev_enumerate_scan_devices(udev_enumerate); + test_enumerate_print_list(udev_enumerate); + udev_enumerate_unref(udev_enumerate); + + printf("enumerate 'not block'\n"); + 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); + + printf("enumerate 'pci, mem, vc'\n"); + 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); + + printf("enumerate 'subsystem'\n"); + 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); + + printf("enumerate 'property IF_FS_*=filesystem'\n"); + 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; +} + +int main(int argc, char *argv[]) { struct udev *udev = NULL; static const struct option options[] = { - { "syspath", 1, NULL, 'p' }, - { "subsystem", 1, NULL, 's' }, - { "socket", 1, NULL, 'S' }, - { "debug", 0, NULL, 'd' }, - { "help", 0, NULL, 'h' }, - { "version", 0, NULL, 'V' }, + { "syspath", required_argument, NULL, 'p' }, + { "subsystem", required_argument, NULL, 's' }, + { "debug", no_argument, NULL, 'd' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, {} }; const char *syspath = "/devices/virtual/mem/null"; const char *subsystem = NULL; - const char *socket = "@/org/kernel/udev/monitor"; char path[1024]; const char *str; @@ -224,15 +416,12 @@ int main(int argc, char *argv[], char *envp[]) case 's': subsystem = optarg; break; - case 'S': - socket = optarg; - break; case 'd': if (udev_get_log_priority(udev) < LOG_INFO) udev_set_log_priority(udev, LOG_INFO); break; case 'h': - printf("--debug --syspath= --subsystem= --socket= --help\n"); + printf("--debug --syspath= --subsystem= --help\n"); goto out; case 'V': printf("%s\n", VERSION); @@ -254,9 +443,15 @@ int main(int argc, char *argv[], char *envp[]) } test_device(udev, syspath); + test_device_devnum(udev); + test_device_subsys_name(udev); test_device_parents(udev, syspath); + test_enumerate(udev, subsystem); - test_monitor(udev, socket); + + test_queue(udev); + + test_monitor(udev); out: udev_unref(udev); return 0;