]>
Commit | Line | Data |
---|---|---|
33a5cc29 KS |
1 | /* |
2 | * test-libudev | |
3 | * | |
4 | * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org> | |
5 | * | |
4061ab9f KS |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2.1 of the License, or (at your option) any later version. | |
33a5cc29 KS |
10 | */ |
11 | ||
33a5cc29 KS |
12 | #include <stdio.h> |
13 | #include <stdarg.h> | |
64ccdf82 | 14 | #include <stdlib.h> |
ba6929f6 KS |
15 | #include <unistd.h> |
16 | #include <errno.h> | |
17 | #include <string.h> | |
b2d9e4f2 KS |
18 | #include <getopt.h> |
19 | #include <syslog.h> | |
4c9dff47 | 20 | #include <fcntl.h> |
f2fd4d27 | 21 | #include <sys/epoll.h> |
ba6929f6 | 22 | |
33a5cc29 | 23 | #include "libudev.h" |
33502ffe | 24 | #include "util.h" |
33a5cc29 | 25 | |
f2fd4d27 KS |
26 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
27 | ||
33a5cc29 | 28 | static void log_fn(struct udev *udev, |
912541b0 KS |
29 | int priority, const char *file, int line, const char *fn, |
30 | const char *format, va_list args) | |
33a5cc29 | 31 | { |
912541b0 KS |
32 | printf("test-libudev: %s %s:%d ", fn, file, line); |
33 | vprintf(format, args); | |
33a5cc29 KS |
34 | } |
35 | ||
ba6929f6 KS |
36 | static void print_device(struct udev_device *device) |
37 | { | |
912541b0 KS |
38 | const char *str; |
39 | dev_t devnum; | |
40 | int count; | |
41 | struct udev_list_entry *list_entry; | |
42 | ||
43 | printf("*** device: %p ***\n", device); | |
44 | str = udev_device_get_action(device); | |
45 | if (str != NULL) | |
46 | printf("action: '%s'\n", str); | |
47 | ||
48 | str = udev_device_get_syspath(device); | |
49 | printf("syspath: '%s'\n", str); | |
50 | ||
51 | str = udev_device_get_sysname(device); | |
52 | printf("sysname: '%s'\n", str); | |
53 | ||
54 | str = udev_device_get_sysnum(device); | |
55 | if (str != NULL) | |
56 | printf("sysnum: '%s'\n", str); | |
57 | ||
58 | str = udev_device_get_devpath(device); | |
59 | printf("devpath: '%s'\n", str); | |
60 | ||
61 | str = udev_device_get_subsystem(device); | |
62 | if (str != NULL) | |
63 | printf("subsystem: '%s'\n", str); | |
64 | ||
65 | str = udev_device_get_devtype(device); | |
66 | if (str != NULL) | |
67 | printf("devtype: '%s'\n", str); | |
68 | ||
69 | str = udev_device_get_driver(device); | |
70 | if (str != NULL) | |
71 | printf("driver: '%s'\n", str); | |
72 | ||
73 | str = udev_device_get_devnode(device); | |
74 | if (str != NULL) | |
75 | printf("devname: '%s'\n", str); | |
76 | ||
77 | devnum = udev_device_get_devnum(device); | |
78 | if (major(devnum) > 0) | |
79 | printf("devnum: %u:%u\n", major(devnum), minor(devnum)); | |
80 | ||
81 | count = 0; | |
82 | udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device)) { | |
83 | printf("link: '%s'\n", udev_list_entry_get_name(list_entry)); | |
84 | count++; | |
85 | } | |
86 | if (count > 0) | |
87 | printf("found %i links\n", count); | |
88 | ||
89 | count = 0; | |
90 | udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device)) { | |
91 | printf("property: '%s=%s'\n", | |
92 | udev_list_entry_get_name(list_entry), | |
93 | udev_list_entry_get_value(list_entry)); | |
94 | count++; | |
95 | } | |
96 | if (count > 0) | |
97 | printf("found %i properties\n", count); | |
98 | ||
99 | str = udev_device_get_property_value(device, "MAJOR"); | |
100 | if (str != NULL) | |
101 | printf("MAJOR: '%s'\n", str); | |
102 | ||
103 | str = udev_device_get_sysattr_value(device, "dev"); | |
104 | if (str != NULL) | |
105 | printf("attr{dev}: '%s'\n", str); | |
106 | ||
107 | printf("\n"); | |
ba6929f6 KS |
108 | } |
109 | ||
8753fadf | 110 | static int test_device(struct udev *udev, const char *syspath) |
ba6929f6 | 111 | { |
912541b0 KS |
112 | struct udev_device *device; |
113 | ||
114 | printf("looking at device: %s\n", syspath); | |
115 | device = udev_device_new_from_syspath(udev, syspath); | |
116 | if (device == NULL) { | |
117 | printf("no device found\n"); | |
118 | return -1; | |
119 | } | |
120 | print_device(device); | |
121 | udev_device_unref(device); | |
122 | return 0; | |
33a5cc29 KS |
123 | } |
124 | ||
8753fadf | 125 | static int test_device_parents(struct udev *udev, const char *syspath) |
4ad3a37f | 126 | { |
912541b0 KS |
127 | struct udev_device *device; |
128 | struct udev_device *device_parent; | |
129 | ||
130 | printf("looking at device: %s\n", syspath); | |
131 | device = udev_device_new_from_syspath(udev, syspath); | |
132 | if (device == NULL) | |
133 | return -1; | |
134 | ||
135 | printf("looking at parents\n"); | |
136 | device_parent = device; | |
137 | do { | |
138 | print_device(device_parent); | |
139 | device_parent = udev_device_get_parent(device_parent); | |
140 | } while (device_parent != NULL); | |
141 | ||
142 | printf("looking at parents again\n"); | |
143 | device_parent = device; | |
144 | do { | |
145 | print_device(device_parent); | |
146 | device_parent = udev_device_get_parent(device_parent); | |
147 | } while (device_parent != NULL); | |
148 | udev_device_unref(device); | |
149 | ||
150 | return 0; | |
4ad3a37f KS |
151 | } |
152 | ||
4c9dff47 KS |
153 | static int test_device_devnum(struct udev *udev) |
154 | { | |
912541b0 KS |
155 | dev_t devnum = makedev(1, 3); |
156 | struct udev_device *device; | |
157 | ||
158 | printf("looking up device: %u:%u\n", major(devnum), minor(devnum)); | |
159 | device = udev_device_new_from_devnum(udev, 'c', devnum); | |
160 | if (device == NULL) | |
161 | return -1; | |
162 | print_device(device); | |
163 | udev_device_unref(device); | |
164 | return 0; | |
4c9dff47 KS |
165 | } |
166 | ||
90d80c2e KS |
167 | static int test_device_subsys_name(struct udev *udev) |
168 | { | |
912541b0 KS |
169 | struct udev_device *device; |
170 | ||
171 | printf("looking up device: 'block':'sda'\n"); | |
172 | device = udev_device_new_from_subsystem_sysname(udev, "block", "sda"); | |
173 | if (device == NULL) | |
174 | return -1; | |
175 | print_device(device); | |
176 | udev_device_unref(device); | |
177 | ||
178 | printf("looking up device: 'subsystem':'pci'\n"); | |
179 | device = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci"); | |
180 | if (device == NULL) | |
181 | return -1; | |
182 | print_device(device); | |
183 | udev_device_unref(device); | |
184 | ||
185 | printf("looking up device: 'drivers':'scsi:sd'\n"); | |
186 | device = udev_device_new_from_subsystem_sysname(udev, "drivers", "scsi:sd"); | |
187 | if (device == NULL) | |
188 | return -1; | |
189 | print_device(device); | |
190 | udev_device_unref(device); | |
191 | ||
192 | printf("looking up device: 'module':'printk'\n"); | |
193 | device = udev_device_new_from_subsystem_sysname(udev, "module", "printk"); | |
194 | if (device == NULL) | |
195 | return -1; | |
196 | print_device(device); | |
197 | udev_device_unref(device); | |
198 | return 0; | |
90d80c2e KS |
199 | } |
200 | ||
bc8184ed | 201 | static int test_enumerate_print_list(struct udev_enumerate *enumerate) |
33a5cc29 | 202 | { |
912541b0 KS |
203 | struct udev_list_entry *list_entry; |
204 | int count = 0; | |
205 | ||
206 | udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) { | |
207 | struct udev_device *device; | |
208 | ||
209 | device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate), | |
210 | udev_list_entry_get_name(list_entry)); | |
211 | if (device != NULL) { | |
212 | printf("device: '%s' (%s)\n", | |
213 | udev_device_get_syspath(device), | |
214 | udev_device_get_subsystem(device)); | |
215 | udev_device_unref(device); | |
216 | count++; | |
217 | } | |
218 | } | |
219 | printf("found %i devices\n\n", count); | |
220 | return count; | |
ba6929f6 KS |
221 | } |
222 | ||
f1986e09 | 223 | static int test_monitor(struct udev *udev) |
ba6929f6 | 224 | { |
912541b0 KS |
225 | struct udev_monitor *udev_monitor = NULL; |
226 | int fd_ep; | |
227 | int fd_udev = -1; | |
228 | struct epoll_event ep_udev, ep_stdin; | |
229 | ||
230 | fd_ep = epoll_create1(EPOLL_CLOEXEC); | |
231 | if (fd_ep < 0) { | |
232 | printf("error creating epoll fd: %m\n"); | |
233 | goto out; | |
234 | } | |
235 | ||
236 | udev_monitor = udev_monitor_new_from_netlink(udev, "udev"); | |
237 | if (udev_monitor == NULL) { | |
238 | printf("no socket\n"); | |
239 | goto out; | |
240 | } | |
241 | fd_udev = udev_monitor_get_fd(udev_monitor); | |
242 | ||
243 | if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "block", NULL) < 0 || | |
244 | udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "tty", NULL) < 0 || | |
245 | udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device") < 0) { | |
246 | printf("filter failed\n"); | |
247 | goto out; | |
248 | } | |
249 | ||
250 | if (udev_monitor_enable_receiving(udev_monitor) < 0) { | |
251 | printf("bind failed\n"); | |
252 | goto out; | |
253 | } | |
254 | ||
255 | memset(&ep_udev, 0, sizeof(struct epoll_event)); | |
256 | ep_udev.events = EPOLLIN; | |
257 | ep_udev.data.fd = fd_udev; | |
258 | if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0) { | |
259 | printf("fail to add fd to epoll: %m\n"); | |
260 | goto out; | |
261 | } | |
262 | ||
263 | memset(&ep_stdin, 0, sizeof(struct epoll_event)); | |
264 | ep_stdin.events = EPOLLIN; | |
265 | ep_stdin.data.fd = STDIN_FILENO; | |
266 | if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, STDIN_FILENO, &ep_stdin) < 0) { | |
267 | printf("fail to add fd to epoll: %m\n"); | |
268 | goto out; | |
269 | } | |
270 | ||
271 | for (;;) { | |
272 | int fdcount; | |
273 | struct epoll_event ev[4]; | |
274 | struct udev_device *device; | |
275 | int i; | |
276 | ||
277 | printf("waiting for events from udev, press ENTER to exit\n"); | |
278 | fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), -1); | |
279 | printf("epoll fd count: %i\n", fdcount); | |
280 | ||
281 | for (i = 0; i < fdcount; i++) { | |
282 | if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) { | |
283 | device = udev_monitor_receive_device(udev_monitor); | |
284 | if (device == NULL) { | |
285 | printf("no device from socket\n"); | |
286 | continue; | |
287 | } | |
288 | print_device(device); | |
289 | udev_device_unref(device); | |
290 | } else if (ev[i].data.fd == STDIN_FILENO && ev[i].events & EPOLLIN) { | |
291 | printf("exiting loop\n"); | |
292 | goto out; | |
293 | } | |
294 | } | |
295 | } | |
f2fd4d27 | 296 | out: |
912541b0 KS |
297 | if (fd_ep >= 0) |
298 | close(fd_ep); | |
299 | udev_monitor_unref(udev_monitor); | |
300 | return 0; | |
33a5cc29 KS |
301 | } |
302 | ||
64ccdf82 KS |
303 | static int test_queue(struct udev *udev) |
304 | { | |
912541b0 KS |
305 | struct udev_queue *udev_queue; |
306 | unsigned long long int seqnum; | |
307 | struct udev_list_entry *list_entry; | |
308 | ||
309 | udev_queue = udev_queue_new(udev); | |
310 | if (udev_queue == NULL) | |
311 | return -1; | |
312 | seqnum = udev_queue_get_kernel_seqnum(udev_queue); | |
313 | printf("seqnum kernel: %llu\n", seqnum); | |
314 | seqnum = udev_queue_get_udev_seqnum(udev_queue); | |
315 | printf("seqnum udev : %llu\n", seqnum); | |
316 | ||
317 | if (udev_queue_get_queue_is_empty(udev_queue)) | |
318 | printf("queue is empty\n"); | |
319 | printf("get queue list\n"); | |
320 | udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue)) | |
321 | printf("queued: '%s' [%s]\n", udev_list_entry_get_name(list_entry), udev_list_entry_get_value(list_entry)); | |
322 | printf("\n"); | |
323 | printf("get queue list again\n"); | |
324 | udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue)) | |
325 | printf("queued: '%s' [%s]\n", udev_list_entry_get_name(list_entry), udev_list_entry_get_value(list_entry)); | |
326 | printf("\n"); | |
327 | ||
328 | list_entry = udev_queue_get_queued_list_entry(udev_queue); | |
329 | if (list_entry != NULL) { | |
330 | printf("event [%llu] is queued\n", seqnum); | |
331 | seqnum = strtoull(udev_list_entry_get_value(list_entry), NULL, 10); | |
332 | if (udev_queue_get_seqnum_is_finished(udev_queue, seqnum)) | |
333 | printf("event [%llu] is not finished\n", seqnum); | |
334 | else | |
335 | printf("event [%llu] is finished\n", seqnum); | |
336 | } | |
337 | printf("\n"); | |
338 | udev_queue_unref(udev_queue); | |
339 | return 0; | |
64ccdf82 KS |
340 | } |
341 | ||
90d80c2e KS |
342 | static int test_enumerate(struct udev *udev, const char *subsystem) |
343 | { | |
912541b0 KS |
344 | struct udev_enumerate *udev_enumerate; |
345 | ||
346 | printf("enumerate '%s'\n", subsystem == NULL ? "<all>" : subsystem); | |
347 | udev_enumerate = udev_enumerate_new(udev); | |
348 | if (udev_enumerate == NULL) | |
349 | return -1; | |
350 | udev_enumerate_add_match_subsystem(udev_enumerate, subsystem); | |
351 | udev_enumerate_scan_devices(udev_enumerate); | |
352 | test_enumerate_print_list(udev_enumerate); | |
353 | udev_enumerate_unref(udev_enumerate); | |
354 | ||
355 | printf("enumerate 'net' + duplicated scan + null + zero\n"); | |
356 | udev_enumerate = udev_enumerate_new(udev); | |
357 | if (udev_enumerate == NULL) | |
358 | return -1; | |
359 | udev_enumerate_add_match_subsystem(udev_enumerate, "net"); | |
360 | udev_enumerate_scan_devices(udev_enumerate); | |
361 | udev_enumerate_scan_devices(udev_enumerate); | |
362 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero"); | |
363 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null"); | |
364 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero"); | |
365 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null"); | |
366 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero"); | |
367 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null"); | |
368 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null"); | |
369 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero"); | |
370 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero"); | |
371 | udev_enumerate_scan_devices(udev_enumerate); | |
372 | test_enumerate_print_list(udev_enumerate); | |
373 | udev_enumerate_unref(udev_enumerate); | |
374 | ||
375 | printf("enumerate 'block'\n"); | |
376 | udev_enumerate = udev_enumerate_new(udev); | |
377 | if (udev_enumerate == NULL) | |
378 | return -1; | |
379 | udev_enumerate_add_match_subsystem(udev_enumerate,"block"); | |
380 | udev_enumerate_add_match_is_initialized(udev_enumerate); | |
381 | udev_enumerate_scan_devices(udev_enumerate); | |
382 | test_enumerate_print_list(udev_enumerate); | |
383 | udev_enumerate_unref(udev_enumerate); | |
384 | ||
385 | printf("enumerate 'not block'\n"); | |
386 | udev_enumerate = udev_enumerate_new(udev); | |
387 | if (udev_enumerate == NULL) | |
388 | return -1; | |
389 | udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block"); | |
390 | udev_enumerate_scan_devices(udev_enumerate); | |
391 | test_enumerate_print_list(udev_enumerate); | |
392 | udev_enumerate_unref(udev_enumerate); | |
393 | ||
394 | printf("enumerate 'pci, mem, vc'\n"); | |
395 | udev_enumerate = udev_enumerate_new(udev); | |
396 | if (udev_enumerate == NULL) | |
397 | return -1; | |
398 | udev_enumerate_add_match_subsystem(udev_enumerate, "pci"); | |
399 | udev_enumerate_add_match_subsystem(udev_enumerate, "mem"); | |
400 | udev_enumerate_add_match_subsystem(udev_enumerate, "vc"); | |
401 | udev_enumerate_scan_devices(udev_enumerate); | |
402 | test_enumerate_print_list(udev_enumerate); | |
403 | udev_enumerate_unref(udev_enumerate); | |
404 | ||
405 | printf("enumerate 'subsystem'\n"); | |
406 | udev_enumerate = udev_enumerate_new(udev); | |
407 | if (udev_enumerate == NULL) | |
408 | return -1; | |
409 | udev_enumerate_scan_subsystems(udev_enumerate); | |
410 | test_enumerate_print_list(udev_enumerate); | |
411 | udev_enumerate_unref(udev_enumerate); | |
412 | ||
413 | printf("enumerate 'property IF_FS_*=filesystem'\n"); | |
414 | udev_enumerate = udev_enumerate_new(udev); | |
415 | if (udev_enumerate == NULL) | |
416 | return -1; | |
417 | udev_enumerate_add_match_property(udev_enumerate, "ID_FS*", "filesystem"); | |
418 | udev_enumerate_scan_devices(udev_enumerate); | |
419 | test_enumerate_print_list(udev_enumerate); | |
420 | udev_enumerate_unref(udev_enumerate); | |
421 | return 0; | |
90d80c2e KS |
422 | } |
423 | ||
2001208c KS |
424 | static int test_hwdb(struct udev *udev, const char *modalias) { |
425 | struct udev_hwdb * hwdb; | |
426 | struct udev_list_entry *entry; | |
427 | ||
428 | hwdb = udev_hwdb_new(udev); | |
429 | ||
430 | udev_list_entry_foreach(entry, udev_hwdb_get_properties_list_entry(hwdb, modalias, 0)) | |
431 | printf("'%s'='%s'\n", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry)); | |
432 | printf("\n"); | |
433 | ||
434 | hwdb = udev_hwdb_unref(hwdb); | |
435 | return 0; | |
436 | } | |
437 | ||
90d80c2e | 438 | int main(int argc, char *argv[]) |
33a5cc29 | 439 | { |
912541b0 KS |
440 | struct udev *udev = NULL; |
441 | static const struct option options[] = { | |
442 | { "syspath", required_argument, NULL, 'p' }, | |
443 | { "subsystem", required_argument, NULL, 's' }, | |
444 | { "debug", no_argument, NULL, 'd' }, | |
445 | { "help", no_argument, NULL, 'h' }, | |
446 | { "version", no_argument, NULL, 'V' }, | |
447 | {} | |
448 | }; | |
449 | const char *syspath = "/devices/virtual/mem/null"; | |
450 | const char *subsystem = NULL; | |
451 | char path[1024]; | |
912541b0 KS |
452 | |
453 | udev = udev_new(); | |
454 | printf("context: %p\n", udev); | |
455 | if (udev == NULL) { | |
456 | printf("no context\n"); | |
457 | return 1; | |
458 | } | |
459 | udev_set_log_fn(udev, log_fn); | |
460 | printf("set log: %p\n", log_fn); | |
461 | ||
462 | for (;;) { | |
463 | int option; | |
464 | ||
465 | option = getopt_long(argc, argv, "+p:s:dhV", options, NULL); | |
466 | if (option == -1) | |
467 | break; | |
468 | ||
469 | switch (option) { | |
470 | case 'p': | |
471 | syspath = optarg; | |
472 | break; | |
473 | case 's': | |
474 | subsystem = optarg; | |
475 | break; | |
476 | case 'd': | |
477 | if (udev_get_log_priority(udev) < LOG_INFO) | |
478 | udev_set_log_priority(udev, LOG_INFO); | |
479 | break; | |
480 | case 'h': | |
481 | printf("--debug --syspath= --subsystem= --help\n"); | |
482 | goto out; | |
483 | case 'V': | |
484 | printf("%s\n", VERSION); | |
485 | goto out; | |
486 | default: | |
487 | goto out; | |
488 | } | |
489 | } | |
490 | ||
912541b0 | 491 | /* add sys path if needed */ |
33502ffe | 492 | if (!startswith(syspath, "/sys")) { |
6ada823a | 493 | snprintf(path, sizeof(path), "/sys/%s", syspath); |
912541b0 KS |
494 | syspath = path; |
495 | } | |
496 | ||
497 | test_device(udev, syspath); | |
498 | test_device_devnum(udev); | |
499 | test_device_subsys_name(udev); | |
500 | test_device_parents(udev, syspath); | |
501 | ||
502 | test_enumerate(udev, subsystem); | |
503 | ||
504 | test_queue(udev); | |
505 | ||
2001208c KS |
506 | test_hwdb(udev, "usb:v0D50p0011*"); |
507 | ||
912541b0 | 508 | test_monitor(udev); |
b2d9e4f2 | 509 | out: |
912541b0 KS |
510 | udev_unref(udev); |
511 | return 0; | |
33a5cc29 | 512 | } |