2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/epoll.h>
34 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
36 static void log_fn(struct udev *udev,
37 int priority, const char *file, int line, const char *fn,
38 const char *format, va_list args)
40 printf("test-libudev: %s %s:%d ", fn, file, line);
41 vprintf(format, args);
44 static void print_device(struct udev_device *device)
49 struct udev_list_entry *list_entry;
51 printf("*** device: %p ***\n", device);
52 str = udev_device_get_action(device);
54 printf("action: '%s'\n", str);
56 str = udev_device_get_syspath(device);
57 printf("syspath: '%s'\n", str);
59 str = udev_device_get_sysname(device);
60 printf("sysname: '%s'\n", str);
62 str = udev_device_get_sysnum(device);
64 printf("sysnum: '%s'\n", str);
66 str = udev_device_get_devpath(device);
67 printf("devpath: '%s'\n", str);
69 str = udev_device_get_subsystem(device);
71 printf("subsystem: '%s'\n", str);
73 str = udev_device_get_devtype(device);
75 printf("devtype: '%s'\n", str);
77 str = udev_device_get_driver(device);
79 printf("driver: '%s'\n", str);
81 str = udev_device_get_devnode(device);
83 printf("devname: '%s'\n", str);
85 devnum = udev_device_get_devnum(device);
86 if (major(devnum) > 0)
87 printf("devnum: %u:%u\n", major(devnum), minor(devnum));
90 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device)) {
91 printf("link: '%s'\n", udev_list_entry_get_name(list_entry));
95 printf("found %i links\n", count);
98 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device)) {
99 printf("property: '%s=%s'\n",
100 udev_list_entry_get_name(list_entry),
101 udev_list_entry_get_value(list_entry));
105 printf("found %i properties\n", count);
107 str = udev_device_get_property_value(device, "MAJOR");
109 printf("MAJOR: '%s'\n", str);
111 str = udev_device_get_sysattr_value(device, "dev");
113 printf("attr{dev}: '%s'\n", str);
118 static int test_device(struct udev *udev, const char *syspath)
120 struct udev_device *device;
122 printf("looking at device: %s\n", syspath);
123 device = udev_device_new_from_syspath(udev, syspath);
124 if (device == NULL) {
125 printf("no device found\n");
128 print_device(device);
129 udev_device_unref(device);
133 static int test_device_parents(struct udev *udev, const char *syspath)
135 struct udev_device *device;
136 struct udev_device *device_parent;
138 printf("looking at device: %s\n", syspath);
139 device = udev_device_new_from_syspath(udev, syspath);
143 printf("looking at parents\n");
144 device_parent = device;
146 print_device(device_parent);
147 device_parent = udev_device_get_parent(device_parent);
148 } while (device_parent != NULL);
150 printf("looking at parents again\n");
151 device_parent = device;
153 print_device(device_parent);
154 device_parent = udev_device_get_parent(device_parent);
155 } while (device_parent != NULL);
156 udev_device_unref(device);
161 static int test_device_devnum(struct udev *udev)
163 dev_t devnum = makedev(1, 3);
164 struct udev_device *device;
166 printf("looking up device: %u:%u\n", major(devnum), minor(devnum));
167 device = udev_device_new_from_devnum(udev, 'c', devnum);
170 print_device(device);
171 udev_device_unref(device);
175 static int test_device_subsys_name(struct udev *udev)
177 struct udev_device *device;
179 printf("looking up device: 'block':'sda'\n");
180 device = udev_device_new_from_subsystem_sysname(udev, "block", "sda");
183 print_device(device);
184 udev_device_unref(device);
186 printf("looking up device: 'subsystem':'pci'\n");
187 device = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci");
190 print_device(device);
191 udev_device_unref(device);
193 printf("looking up device: 'drivers':'scsi:sd'\n");
194 device = udev_device_new_from_subsystem_sysname(udev, "drivers", "scsi:sd");
197 print_device(device);
198 udev_device_unref(device);
200 printf("looking up device: 'module':'printk'\n");
201 device = udev_device_new_from_subsystem_sysname(udev, "module", "printk");
204 print_device(device);
205 udev_device_unref(device);
209 static int test_enumerate_print_list(struct udev_enumerate *enumerate)
211 struct udev_list_entry *list_entry;
214 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
215 struct udev_device *device;
217 device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
218 udev_list_entry_get_name(list_entry));
219 if (device != NULL) {
220 printf("device: '%s' (%s)\n",
221 udev_device_get_syspath(device),
222 udev_device_get_subsystem(device));
223 udev_device_unref(device);
227 printf("found %i devices\n\n", count);
231 static int test_monitor(struct udev *udev)
233 struct udev_monitor *udev_monitor = NULL;
236 struct epoll_event ep_udev, ep_stdin;
238 fd_ep = epoll_create1(EPOLL_CLOEXEC);
240 printf("error creating epoll fd: %m\n");
244 udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
245 if (udev_monitor == NULL) {
246 printf("no socket\n");
249 fd_udev = udev_monitor_get_fd(udev_monitor);
251 if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "block", NULL) < 0 ||
252 udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "tty", NULL) < 0 ||
253 udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device") < 0) {
254 printf("filter failed\n");
258 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
259 printf("bind failed\n");
263 memset(&ep_udev, 0, sizeof(struct epoll_event));
264 ep_udev.events = EPOLLIN;
265 ep_udev.data.fd = fd_udev;
266 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0) {
267 printf("fail to add fd to epoll: %m\n");
271 memset(&ep_stdin, 0, sizeof(struct epoll_event));
272 ep_stdin.events = EPOLLIN;
273 ep_stdin.data.fd = STDIN_FILENO;
274 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, STDIN_FILENO, &ep_stdin) < 0) {
275 printf("fail to add fd to epoll: %m\n");
281 struct epoll_event ev[4];
282 struct udev_device *device;
285 printf("waiting for events from udev, press ENTER to exit\n");
286 fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), -1);
287 printf("epoll fd count: %i\n", fdcount);
289 for (i = 0; i < fdcount; i++) {
290 if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) {
291 device = udev_monitor_receive_device(udev_monitor);
292 if (device == NULL) {
293 printf("no device from socket\n");
296 print_device(device);
297 udev_device_unref(device);
298 } else if (ev[i].data.fd == STDIN_FILENO && ev[i].events & EPOLLIN) {
299 printf("exiting loop\n");
307 udev_monitor_unref(udev_monitor);
311 static int test_queue(struct udev *udev)
313 struct udev_queue *udev_queue;
314 unsigned long long int seqnum;
315 struct udev_list_entry *list_entry;
317 udev_queue = udev_queue_new(udev);
318 if (udev_queue == NULL)
320 seqnum = udev_queue_get_kernel_seqnum(udev_queue);
321 printf("seqnum kernel: %llu\n", seqnum);
322 seqnum = udev_queue_get_udev_seqnum(udev_queue);
323 printf("seqnum udev : %llu\n", seqnum);
325 if (udev_queue_get_queue_is_empty(udev_queue))
326 printf("queue is empty\n");
327 printf("get queue list\n");
328 udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue))
329 printf("queued: '%s' [%s]\n", udev_list_entry_get_name(list_entry), udev_list_entry_get_value(list_entry));
331 printf("get queue list again\n");
332 udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue))
333 printf("queued: '%s' [%s]\n", udev_list_entry_get_name(list_entry), udev_list_entry_get_value(list_entry));
336 list_entry = udev_queue_get_queued_list_entry(udev_queue);
337 if (list_entry != NULL) {
338 printf("event [%llu] is queued\n", seqnum);
339 seqnum = strtoull(udev_list_entry_get_value(list_entry), NULL, 10);
340 if (udev_queue_get_seqnum_is_finished(udev_queue, seqnum))
341 printf("event [%llu] is not finished\n", seqnum);
343 printf("event [%llu] is finished\n", seqnum);
346 udev_queue_unref(udev_queue);
350 static int test_enumerate(struct udev *udev, const char *subsystem)
352 struct udev_enumerate *udev_enumerate;
354 printf("enumerate '%s'\n", subsystem == NULL ? "<all>" : subsystem);
355 udev_enumerate = udev_enumerate_new(udev);
356 if (udev_enumerate == NULL)
358 udev_enumerate_add_match_subsystem(udev_enumerate, subsystem);
359 udev_enumerate_scan_devices(udev_enumerate);
360 test_enumerate_print_list(udev_enumerate);
361 udev_enumerate_unref(udev_enumerate);
363 printf("enumerate 'net' + duplicated scan + null + zero\n");
364 udev_enumerate = udev_enumerate_new(udev);
365 if (udev_enumerate == NULL)
367 udev_enumerate_add_match_subsystem(udev_enumerate, "net");
368 udev_enumerate_scan_devices(udev_enumerate);
369 udev_enumerate_scan_devices(udev_enumerate);
370 udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
371 udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
372 udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
373 udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
374 udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
375 udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
376 udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
377 udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
378 udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
379 udev_enumerate_scan_devices(udev_enumerate);
380 test_enumerate_print_list(udev_enumerate);
381 udev_enumerate_unref(udev_enumerate);
383 printf("enumerate 'block'\n");
384 udev_enumerate = udev_enumerate_new(udev);
385 if (udev_enumerate == NULL)
387 udev_enumerate_add_match_subsystem(udev_enumerate,"block");
388 udev_enumerate_add_match_is_initialized(udev_enumerate);
389 udev_enumerate_scan_devices(udev_enumerate);
390 test_enumerate_print_list(udev_enumerate);
391 udev_enumerate_unref(udev_enumerate);
393 printf("enumerate 'not block'\n");
394 udev_enumerate = udev_enumerate_new(udev);
395 if (udev_enumerate == NULL)
397 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
398 udev_enumerate_scan_devices(udev_enumerate);
399 test_enumerate_print_list(udev_enumerate);
400 udev_enumerate_unref(udev_enumerate);
402 printf("enumerate 'pci, mem, vc'\n");
403 udev_enumerate = udev_enumerate_new(udev);
404 if (udev_enumerate == NULL)
406 udev_enumerate_add_match_subsystem(udev_enumerate, "pci");
407 udev_enumerate_add_match_subsystem(udev_enumerate, "mem");
408 udev_enumerate_add_match_subsystem(udev_enumerate, "vc");
409 udev_enumerate_scan_devices(udev_enumerate);
410 test_enumerate_print_list(udev_enumerate);
411 udev_enumerate_unref(udev_enumerate);
413 printf("enumerate 'subsystem'\n");
414 udev_enumerate = udev_enumerate_new(udev);
415 if (udev_enumerate == NULL)
417 udev_enumerate_scan_subsystems(udev_enumerate);
418 test_enumerate_print_list(udev_enumerate);
419 udev_enumerate_unref(udev_enumerate);
421 printf("enumerate 'property IF_FS_*=filesystem'\n");
422 udev_enumerate = udev_enumerate_new(udev);
423 if (udev_enumerate == NULL)
425 udev_enumerate_add_match_property(udev_enumerate, "ID_FS*", "filesystem");
426 udev_enumerate_scan_devices(udev_enumerate);
427 test_enumerate_print_list(udev_enumerate);
428 udev_enumerate_unref(udev_enumerate);
432 static int test_hwdb(struct udev *udev, const char *modalias) {
433 struct udev_hwdb *hwdb;
434 struct udev_list_entry *entry;
436 hwdb = udev_hwdb_new(udev);
438 udev_list_entry_foreach(entry, udev_hwdb_get_properties_list_entry(hwdb, modalias, 0))
439 printf("'%s'='%s'\n", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry));
442 hwdb = udev_hwdb_unref(hwdb);
446 int main(int argc, char *argv[])
448 struct udev *udev = NULL;
449 static const struct option options[] = {
450 { "syspath", required_argument, NULL, 'p' },
451 { "subsystem", required_argument, NULL, 's' },
452 { "debug", no_argument, NULL, 'd' },
453 { "help", no_argument, NULL, 'h' },
454 { "version", no_argument, NULL, 'V' },
457 const char *syspath = "/devices/virtual/mem/null";
458 const char *subsystem = NULL;
462 printf("context: %p\n", udev);
464 printf("no context\n");
467 udev_set_log_fn(udev, log_fn);
468 printf("set log: %p\n", log_fn);
473 option = getopt_long(argc, argv, "+p:s:dhV", options, NULL);
485 if (udev_get_log_priority(udev) < LOG_INFO)
486 udev_set_log_priority(udev, LOG_INFO);
489 printf("--debug --syspath= --subsystem= --help\n");
492 printf("%s\n", VERSION);
499 /* add sys path if needed */
500 if (!startswith(syspath, "/sys")) {
501 snprintf(path, sizeof(path), "/sys/%s", syspath);
505 test_device(udev, syspath);
506 test_device_devnum(udev);
507 test_device_subsys_name(udev);
508 test_device_parents(udev, syspath);
510 test_enumerate(udev, subsystem);
514 test_hwdb(udev, "usb:v0D50p0011*");