4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <sys/select.h>
32 static void log_fn(struct udev *udev,
33 int priority, const char *file, int line, const char *fn,
34 const char *format, va_list args)
36 printf("test-libudev: %s %s:%d ", fn, file, line);
37 vprintf(format, args);
40 static void print_device(struct udev_device *device)
44 struct udev_list *list;
46 printf("*** device: %p ***\n", device);
47 str = udev_device_get_action(device);
48 printf("action: '%s'\n", str);
49 str = udev_device_get_syspath(device);
50 printf("syspath: '%s'\n", str);
51 str = udev_device_get_devpath(device);
52 printf("devpath: '%s'\n", str);
53 str = udev_device_get_subsystem(device);
54 printf("subsystem: '%s'\n", str);
55 str = udev_device_get_driver(device);
56 printf("driver: '%s'\n", str);
57 str = udev_device_get_devnode(device);
58 printf("devname: '%s'\n", str);
61 list = udev_device_get_devlinks_list(device);
62 while (list != NULL) {
63 printf("link: '%s'\n", udev_list_get_name(list));
65 list = udev_list_get_next(list);
67 printf("found %i links\n", count);
70 list = udev_device_get_properties_list(device);
71 while (list != NULL) {
72 printf("property: '%s=%s'\n", udev_list_get_name(list), udev_list_get_value(list));
74 list = udev_list_get_next(list);
76 printf("found %i properties\n", count);
78 str = udev_device_get_attr_value(device, "dev");
79 printf("attr{dev}: '%s'\n", str);
84 static int test_device(struct udev *udev, const char *syspath)
86 struct udev_device *device;
88 printf("looking at device: %s\n", syspath);
89 device = udev_device_new_from_syspath(udev, syspath);
91 printf("no device\n");
95 udev_device_unref(device);
99 static int test_device_parents(struct udev *udev, const char *syspath)
101 struct udev_device *device;
102 struct udev_device *device_parent;
104 printf("looking at device: %s\n", syspath);
105 device = udev_device_new_from_syspath(udev, syspath);
109 printf("looking at parents\n");
110 device_parent = device;
112 print_device(device_parent);
113 device_parent = udev_device_get_parent(device_parent);
114 } while (device_parent != NULL);
116 printf("looking at parents again\n");
117 device_parent = device;
119 print_device(device_parent);
120 device_parent = udev_device_get_parent(device_parent);
121 } while (device_parent != NULL);
122 udev_device_unref(device);
127 static int test_device_devnum(struct udev *udev)
129 dev_t devnum = makedev(1, 3);
130 struct udev_device *device;
132 printf("looking up device: %u:%u\n", major(devnum), minor(devnum));
133 device = udev_device_new_from_devnum(udev, 'c', devnum);
136 print_device(device);
137 udev_device_unref(device);
141 static int test_enumerate(struct udev *udev, const char *subsystem)
143 struct udev_enumerate *enumerate;
144 struct udev_list *list;
147 enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
148 if (enumerate == NULL)
150 list = udev_enumerate_get_devices_list(enumerate);
151 while (list != NULL) {
152 struct udev_device *device;
154 device = udev_device_new_from_syspath(udev, udev_list_get_name(list));
155 if (device != NULL) {
156 printf("device: '%s' (%s) '%s'\n",
157 udev_device_get_syspath(device),
158 udev_device_get_subsystem(device),
159 udev_device_get_sysname(device));
160 udev_device_unref(device);
163 list = udev_list_get_next(list);
165 udev_enumerate_unref(enumerate);
166 printf("found %i devices\n\n", count);
170 static int test_monitor(struct udev *udev, const char *socket_path)
172 struct udev_monitor *udev_monitor;
176 udev_monitor = udev_monitor_new_from_socket(udev, socket_path);
177 if (udev_monitor == NULL) {
178 printf("no socket\n");
181 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
182 printf("bind failed\n");
186 fd = udev_monitor_get_fd(udev_monitor);
190 struct udev_device *device;
193 FD_SET(STDIN_FILENO, &readfds);
194 FD_SET(fd, &readfds);
196 printf("waiting for events on %s, press ENTER to exit\n", socket_path);
197 fdcount = select(fd+1, &readfds, NULL, NULL, NULL);
198 printf("select fd count: %i\n", fdcount);
200 if (FD_ISSET(fd, &readfds)) {
201 device = udev_monitor_receive_device(udev_monitor);
202 if (device == NULL) {
203 printf("no device from socket\n");
206 print_device(device);
207 udev_device_unref(device);
210 if (FD_ISSET(STDIN_FILENO, &readfds)) {
211 printf("exiting loop\n");
216 udev_monitor_unref(udev_monitor);
220 int main(int argc, char *argv[], char *envp[])
222 struct udev *udev = NULL;
223 static const struct option options[] = {
224 { "syspath", 1, NULL, 'p' },
225 { "subsystem", 1, NULL, 's' },
226 { "socket", 1, NULL, 'S' },
227 { "debug", 0, NULL, 'd' },
228 { "help", 0, NULL, 'h' },
229 { "version", 0, NULL, 'V' },
232 const char *syspath = "/devices/virtual/mem/null";
233 const char *subsystem = NULL;
234 const char *socket = "@/org/kernel/udev/monitor";
239 printf("context: %p\n", udev);
241 printf("no context\n");
244 udev_set_log_fn(udev, log_fn);
245 printf("set log: %p\n", log_fn);
250 option = getopt_long(argc, argv, "+dhV", options, NULL);
265 if (udev_get_log_priority(udev) < LOG_INFO)
266 udev_set_log_priority(udev, LOG_INFO);
269 printf("--debug --syspath= --subsystem= --socket= --help\n");
272 printf("%s\n", VERSION);
279 str = udev_get_sys_path(udev);
280 printf("sys_path: '%s'\n", str);
281 str = udev_get_dev_path(udev);
282 printf("dev_path: '%s'\n", str);
284 /* add sys path if needed */
285 if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0) {
286 snprintf(path, sizeof(path), "%s%s", udev_get_sys_path(udev), syspath);
290 test_device(udev, syspath);
291 test_device_devnum(udev);
292 test_device_parents(udev, syspath);
293 test_enumerate(udev, subsystem);
294 test_monitor(udev, socket);