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 int print_devlinks_cb(struct udev_device *udev_device, const char *value, void *data)
42 printf("link: '%s'\n", value);
46 static int print_properties_cb(struct udev_device *udev_device, const char *key, const char *value, void *data)
48 printf("property: '%s=%s'\n", key, value);
52 static void print_device(struct udev_device *device)
57 printf("*** device: %p ***\n", device);
58 str = udev_device_get_action(device);
59 printf("action: '%s'\n", str);
60 str = udev_device_get_syspath(device);
61 printf("syspath: '%s'\n", str);
62 str = udev_device_get_devpath(device);
63 printf("devpath: '%s'\n", str);
64 str = udev_device_get_subsystem(device);
65 printf("subsystem: '%s'\n", str);
66 str = udev_device_get_driver(device);
67 printf("driver: '%s'\n", str);
68 str = udev_device_get_devnode(device);
69 printf("devname: '%s'\n", str);
70 count = udev_device_get_devlinks(device, print_devlinks_cb, NULL);
71 printf("found %i links\n", count);
72 count = udev_device_get_properties(device, print_properties_cb, NULL);
73 printf("found %i properties\n", count);
77 static int test_device(struct udev *udev, const char *syspath)
79 struct udev_device *device;
81 printf("looking at device: %s\n", syspath);
82 device = udev_device_new_from_syspath(udev, syspath);
84 printf("no device\n");
88 udev_device_unref(device);
92 static int test_device_parents(struct udev *udev, const char *syspath)
94 struct udev_device *device;
95 struct udev_device *device_parent;
97 printf("looking at device: %s\n", syspath);
98 device = udev_device_new_from_syspath(udev, syspath);
102 device_parent = device;
104 print_device(device_parent);
105 device_parent = udev_device_get_parent(device_parent);
106 } while (device_parent != NULL);
108 device_parent = device;
110 print_device(device_parent);
111 device_parent = udev_device_get_parent(device_parent);
112 } while (device_parent != NULL);
113 udev_device_unref(device);
118 static int test_device_devnum(struct udev *udev)
120 dev_t devnum = makedev(1, 3);
121 struct udev_device *device;
123 printf("looking up device: %u:%u\n", major(devnum), minor(devnum));
124 device = udev_device_new_from_devnum(udev, 'c', devnum);
127 print_device(device);
128 udev_device_unref(device);
132 static int devices_enum_cb(struct udev_device *device, void *data)
134 printf("device: '%s' (%s) '%s'\n",
135 udev_device_get_syspath(device),
136 udev_device_get_subsystem(device),
137 udev_device_get_sysname(device));
141 static int test_enumerate(struct udev *udev, const char *subsystem)
145 count = udev_enumerate_devices(udev, subsystem, devices_enum_cb, NULL);
146 printf("found %i devices\n\n", count);
150 static int test_monitor(struct udev *udev, const char *socket_path)
152 struct udev_monitor *udev_monitor;
156 udev_monitor = udev_monitor_new_from_socket(udev, socket_path);
157 if (udev_monitor == NULL) {
158 printf("no socket\n");
161 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
162 printf("bind failed\n");
166 fd = udev_monitor_get_fd(udev_monitor);
170 struct udev_device *device;
173 FD_SET(STDIN_FILENO, &readfds);
174 FD_SET(fd, &readfds);
176 printf("waiting for events on %s, press ENTER to exit\n", socket_path);
177 fdcount = select(fd+1, &readfds, NULL, NULL, NULL);
178 printf("select fd count: %i\n", fdcount);
180 if (FD_ISSET(fd, &readfds)) {
181 device = udev_monitor_receive_device(udev_monitor);
182 if (device == NULL) {
183 printf("no device from socket\n");
186 print_device(device);
187 udev_device_unref(device);
190 if (FD_ISSET(STDIN_FILENO, &readfds)) {
191 printf("exiting loop\n");
196 udev_monitor_unref(udev_monitor);
200 int main(int argc, char *argv[], char *envp[])
202 struct udev *udev = NULL;
203 static const struct option options[] = {
204 { "syspath", 1, NULL, 'p' },
205 { "subsystem", 1, NULL, 's' },
206 { "socket", 1, NULL, 'S' },
207 { "debug", 0, NULL, 'd' },
208 { "help", 0, NULL, 'h' },
209 { "version", 0, NULL, 'V' },
212 const char *syspath = "/devices/virtual/mem/null";
213 const char *subsystem = NULL;
214 const char *socket = "@/org/kernel/udev/monitor";
219 printf("context: %p\n", udev);
221 printf("no context\n");
224 udev_set_log_fn(udev, log_fn);
225 printf("set log: %p\n", log_fn);
230 option = getopt_long(argc, argv, "+dhV", options, NULL);
245 if (udev_get_log_priority(udev) < LOG_INFO)
246 udev_set_log_priority(udev, LOG_INFO);
249 printf("--debug --syspath= --subsystem= --socket= --help\n");
252 printf("%s\n", VERSION);
259 str = udev_get_sys_path(udev);
260 printf("sys_path: '%s'\n", str);
261 str = udev_get_dev_path(udev);
262 printf("dev_path: '%s'\n", str);
264 /* add sys path if needed */
265 if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0) {
266 snprintf(path, sizeof(path), "%s%s", udev_get_sys_path(udev), syspath);
270 test_device(udev, syspath);
271 test_device_devnum(udev);
272 test_device_parents(udev, syspath);
273 test_enumerate(udev, subsystem);
274 test_monitor(udev, socket);