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