chiark / gitweb /
cgroup: simplify how instantiated units are mapped to cgroups
[elogind.git] / src / test / test-libudev.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <getopt.h>
27 #include <syslog.h>
28 #include <fcntl.h>
29 #include <sys/epoll.h>
30
31 #include "libudev.h"
32 #include "util.h"
33
34 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
35
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)
39 {
40         printf("test-libudev: %s %s:%d ", fn, file, line);
41         vprintf(format, args);
42 }
43
44 static void print_device(struct udev_device *device)
45 {
46         const char *str;
47         dev_t devnum;
48         int count;
49         struct udev_list_entry *list_entry;
50
51         printf("*** device: %p ***\n", device);
52         str = udev_device_get_action(device);
53         if (str != NULL)
54                 printf("action:    '%s'\n", str);
55
56         str = udev_device_get_syspath(device);
57         printf("syspath:   '%s'\n", str);
58
59         str = udev_device_get_sysname(device);
60         printf("sysname:   '%s'\n", str);
61
62         str = udev_device_get_sysnum(device);
63         if (str != NULL)
64                 printf("sysnum:    '%s'\n", str);
65
66         str = udev_device_get_devpath(device);
67         printf("devpath:   '%s'\n", str);
68
69         str = udev_device_get_subsystem(device);
70         if (str != NULL)
71                 printf("subsystem: '%s'\n", str);
72
73         str = udev_device_get_devtype(device);
74         if (str != NULL)
75                 printf("devtype:   '%s'\n", str);
76
77         str = udev_device_get_driver(device);
78         if (str != NULL)
79                 printf("driver:    '%s'\n", str);
80
81         str = udev_device_get_devnode(device);
82         if (str != NULL)
83                 printf("devname:   '%s'\n", str);
84
85         devnum = udev_device_get_devnum(device);
86         if (major(devnum) > 0)
87                 printf("devnum:    %u:%u\n", major(devnum), minor(devnum));
88
89         count = 0;
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));
92                 count++;
93         }
94         if (count > 0)
95                 printf("found %i links\n", count);
96
97         count = 0;
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));
102                 count++;
103         }
104         if (count > 0)
105                 printf("found %i properties\n", count);
106
107         str = udev_device_get_property_value(device, "MAJOR");
108         if (str != NULL)
109                 printf("MAJOR: '%s'\n", str);
110
111         str = udev_device_get_sysattr_value(device, "dev");
112         if (str != NULL)
113                 printf("attr{dev}: '%s'\n", str);
114
115         printf("\n");
116 }
117
118 static int test_device(struct udev *udev, const char *syspath)
119 {
120         struct udev_device *device;
121
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");
126                 return -1;
127         }
128         print_device(device);
129         udev_device_unref(device);
130         return 0;
131 }
132
133 static int test_device_parents(struct udev *udev, const char *syspath)
134 {
135         struct udev_device *device;
136         struct udev_device *device_parent;
137
138         printf("looking at device: %s\n", syspath);
139         device = udev_device_new_from_syspath(udev, syspath);
140         if (device == NULL)
141                 return -1;
142
143         printf("looking at parents\n");
144         device_parent = device;
145         do {
146                 print_device(device_parent);
147                 device_parent = udev_device_get_parent(device_parent);
148         } while (device_parent != NULL);
149
150         printf("looking at parents again\n");
151         device_parent = device;
152         do {
153                 print_device(device_parent);
154                 device_parent = udev_device_get_parent(device_parent);
155         } while (device_parent != NULL);
156         udev_device_unref(device);
157
158         return 0;
159 }
160
161 static int test_device_devnum(struct udev *udev)
162 {
163         dev_t devnum = makedev(1, 3);
164         struct udev_device *device;
165
166         printf("looking up device: %u:%u\n", major(devnum), minor(devnum));
167         device = udev_device_new_from_devnum(udev, 'c', devnum);
168         if (device == NULL)
169                 return -1;
170         print_device(device);
171         udev_device_unref(device);
172         return 0;
173 }
174
175 static int test_device_subsys_name(struct udev *udev)
176 {
177         struct udev_device *device;
178
179         printf("looking up device: 'block':'sda'\n");
180         device = udev_device_new_from_subsystem_sysname(udev, "block", "sda");
181         if (device == NULL)
182                 return -1;
183         print_device(device);
184         udev_device_unref(device);
185
186         printf("looking up device: 'subsystem':'pci'\n");
187         device = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci");
188         if (device == NULL)
189                 return -1;
190         print_device(device);
191         udev_device_unref(device);
192
193         printf("looking up device: 'drivers':'scsi:sd'\n");
194         device = udev_device_new_from_subsystem_sysname(udev, "drivers", "scsi:sd");
195         if (device == NULL)
196                 return -1;
197         print_device(device);
198         udev_device_unref(device);
199
200         printf("looking up device: 'module':'printk'\n");
201         device = udev_device_new_from_subsystem_sysname(udev, "module", "printk");
202         if (device == NULL)
203                 return -1;
204         print_device(device);
205         udev_device_unref(device);
206         return 0;
207 }
208
209 static int test_enumerate_print_list(struct udev_enumerate *enumerate)
210 {
211         struct udev_list_entry *list_entry;
212         int count = 0;
213
214         udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
215                 struct udev_device *device;
216
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);
224                         count++;
225                 }
226         }
227         printf("found %i devices\n\n", count);
228         return count;
229 }
230
231 static int test_monitor(struct udev *udev)
232 {
233         struct udev_monitor *udev_monitor = NULL;
234         int fd_ep;
235         int fd_udev = -1;
236         struct epoll_event ep_udev, ep_stdin;
237
238         fd_ep = epoll_create1(EPOLL_CLOEXEC);
239         if (fd_ep < 0) {
240                 printf("error creating epoll fd: %m\n");
241                 goto out;
242         }
243
244         udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
245         if (udev_monitor == NULL) {
246                 printf("no socket\n");
247                 goto out;
248         }
249         fd_udev = udev_monitor_get_fd(udev_monitor);
250
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");
255                 goto out;
256         }
257
258         if (udev_monitor_enable_receiving(udev_monitor) < 0) {
259                 printf("bind failed\n");
260                 goto out;
261         }
262
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");
268                 goto out;
269         }
270
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");
276                 goto out;
277         }
278
279         for (;;) {
280                 int fdcount;
281                 struct epoll_event ev[4];
282                 struct udev_device *device;
283                 int i;
284
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);
288
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");
294                                         continue;
295                                 }
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");
300                                 goto out;
301                         }
302                 }
303         }
304 out:
305         if (fd_ep >= 0)
306                 close(fd_ep);
307         udev_monitor_unref(udev_monitor);
308         return 0;
309 }
310
311 static int test_queue(struct udev *udev)
312 {
313         struct udev_queue *udev_queue;
314         unsigned long long int seqnum;
315         struct udev_list_entry *list_entry;
316
317         udev_queue = udev_queue_new(udev);
318         if (udev_queue == NULL)
319                 return -1;
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);
324
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));
330         printf("\n");
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));
334         printf("\n");
335
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);
342                 else
343                         printf("event [%llu] is finished\n", seqnum);
344         }
345         printf("\n");
346         udev_queue_unref(udev_queue);
347         return 0;
348 }
349
350 static int test_enumerate(struct udev *udev, const char *subsystem)
351 {
352         struct udev_enumerate *udev_enumerate;
353
354         printf("enumerate '%s'\n", subsystem == NULL ? "<all>" : subsystem);
355         udev_enumerate = udev_enumerate_new(udev);
356         if (udev_enumerate == NULL)
357                 return -1;
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);
362
363         printf("enumerate 'net' + duplicated scan + null + zero\n");
364         udev_enumerate = udev_enumerate_new(udev);
365         if (udev_enumerate == NULL)
366                 return -1;
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);
382
383         printf("enumerate 'block'\n");
384         udev_enumerate = udev_enumerate_new(udev);
385         if (udev_enumerate == NULL)
386                 return -1;
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);
392
393         printf("enumerate 'not block'\n");
394         udev_enumerate = udev_enumerate_new(udev);
395         if (udev_enumerate == NULL)
396                 return -1;
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);
401
402         printf("enumerate 'pci, mem, vc'\n");
403         udev_enumerate = udev_enumerate_new(udev);
404         if (udev_enumerate == NULL)
405                 return -1;
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);
412
413         printf("enumerate 'subsystem'\n");
414         udev_enumerate = udev_enumerate_new(udev);
415         if (udev_enumerate == NULL)
416                 return -1;
417         udev_enumerate_scan_subsystems(udev_enumerate);
418         test_enumerate_print_list(udev_enumerate);
419         udev_enumerate_unref(udev_enumerate);
420
421         printf("enumerate 'property IF_FS_*=filesystem'\n");
422         udev_enumerate = udev_enumerate_new(udev);
423         if (udev_enumerate == NULL)
424                 return -1;
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);
429         return 0;
430 }
431
432 static int test_hwdb(struct udev *udev, const char *modalias) {
433         struct udev_hwdb * hwdb;
434         struct udev_list_entry *entry;
435
436         hwdb = udev_hwdb_new(udev);
437
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));
440         printf("\n");
441
442         hwdb = udev_hwdb_unref(hwdb);
443         return 0;
444 }
445
446 int main(int argc, char *argv[])
447 {
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' },
455                 {}
456         };
457         const char *syspath = "/devices/virtual/mem/null";
458         const char *subsystem = NULL;
459         char path[1024];
460
461         udev = udev_new();
462         printf("context: %p\n", udev);
463         if (udev == NULL) {
464                 printf("no context\n");
465                 return 1;
466         }
467         udev_set_log_fn(udev, log_fn);
468         printf("set log: %p\n", log_fn);
469
470         for (;;) {
471                 int option;
472
473                 option = getopt_long(argc, argv, "+p:s:dhV", options, NULL);
474                 if (option == -1)
475                         break;
476
477                 switch (option) {
478                 case 'p':
479                         syspath = optarg;
480                         break;
481                 case 's':
482                         subsystem = optarg;
483                         break;
484                 case 'd':
485                         if (udev_get_log_priority(udev) < LOG_INFO)
486                                 udev_set_log_priority(udev, LOG_INFO);
487                         break;
488                 case 'h':
489                         printf("--debug --syspath= --subsystem= --help\n");
490                         goto out;
491                 case 'V':
492                         printf("%s\n", VERSION);
493                         goto out;
494                 default:
495                         goto out;
496                 }
497         }
498
499         /* add sys path if needed */
500         if (!startswith(syspath, "/sys")) {
501                 snprintf(path, sizeof(path), "/sys/%s", syspath);
502                 syspath = path;
503         }
504
505         test_device(udev, syspath);
506         test_device_devnum(udev);
507         test_device_subsys_name(udev);
508         test_device_parents(udev, syspath);
509
510         test_enumerate(udev, subsystem);
511
512         test_queue(udev);
513
514         test_hwdb(udev, "usb:v0D50p0011*");
515
516         test_monitor(udev);
517 out:
518         udev_unref(udev);
519         return 0;
520 }