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)
45 struct udev_list_entry *list_entry;
47 printf("*** device: %p ***\n", device);
48 str = udev_device_get_action(device);
50 printf("action: '%s'\n", str);
52 str = udev_device_get_syspath(device);
53 printf("syspath: '%s'\n", str);
55 str = udev_device_get_devpath(device);
56 printf("devpath: '%s'\n", str);
58 str = udev_device_get_subsystem(device);
60 printf("subsystem: '%s'\n", str);
62 str = udev_device_get_driver(device);
64 printf("driver: '%s'\n", str);
66 str = udev_device_get_devnode(device);
68 printf("devname: '%s'\n", str);
70 devnum = udev_device_get_devnum(device);
71 if (major(devnum) > 0)
72 printf("devnum: %u:%u\n", major(devnum), minor(devnum));
75 list_entry = udev_device_get_devlinks_list_entry(device);
76 while (list_entry != NULL) {
77 printf("link: '%s'\n", udev_list_entry_get_name(list_entry));
79 list_entry = udev_list_entry_get_next(list_entry);
82 printf("found %i links\n", count);
85 list_entry = udev_device_get_properties_list_entry(device);
86 while (list_entry != NULL) {
87 printf("property: '%s=%s'\n",
88 udev_list_entry_get_name(list_entry),
89 udev_list_entry_get_value(list_entry));
91 list_entry = udev_list_entry_get_next(list_entry);
94 printf("found %i properties\n", count);
96 str = udev_device_get_attr_value(device, "dev");
98 printf("attr{dev}: '%s'\n", str);
103 static int test_device(struct udev *udev, const char *syspath)
105 struct udev_device *device;
107 printf("looking at device: %s\n", syspath);
108 device = udev_device_new_from_syspath(udev, syspath);
109 if (device == NULL) {
110 printf("no device\n");
113 print_device(device);
114 udev_device_unref(device);
118 static int test_device_parents(struct udev *udev, const char *syspath)
120 struct udev_device *device;
121 struct udev_device *device_parent;
123 printf("looking at device: %s\n", syspath);
124 device = udev_device_new_from_syspath(udev, syspath);
128 printf("looking at parents\n");
129 device_parent = device;
131 print_device(device_parent);
132 device_parent = udev_device_get_parent(device_parent);
133 } while (device_parent != NULL);
135 printf("looking at parents again\n");
136 device_parent = device;
138 print_device(device_parent);
139 device_parent = udev_device_get_parent(device_parent);
140 } while (device_parent != NULL);
141 udev_device_unref(device);
146 static int test_device_devnum(struct udev *udev)
148 dev_t devnum = makedev(1, 3);
149 struct udev_device *device;
151 printf("looking up device: %u:%u\n", major(devnum), minor(devnum));
152 device = udev_device_new_from_devnum(udev, 'c', devnum);
155 print_device(device);
156 udev_device_unref(device);
160 static int test_enumerate(struct udev *udev, const char *subsystem)
162 struct udev_enumerate *enumerate;
163 struct udev_list_entry *list_entry;
166 enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
167 if (enumerate == NULL)
169 list_entry = udev_enumerate_get_list_entry(enumerate);
170 while (list_entry != NULL) {
171 struct udev_device *device;
173 device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
174 if (device != NULL) {
175 printf("device: '%s' (%s) '%s'\n",
176 udev_device_get_syspath(device),
177 udev_device_get_subsystem(device),
178 udev_device_get_sysname(device));
179 udev_device_unref(device);
182 list_entry = udev_list_entry_get_next(list_entry);
184 udev_enumerate_unref(enumerate);
185 printf("found %i devices\n\n", count);
189 static int test_monitor(struct udev *udev, const char *socket_path)
191 struct udev_monitor *udev_monitor;
195 udev_monitor = udev_monitor_new_from_socket(udev, socket_path);
196 if (udev_monitor == NULL) {
197 printf("no socket\n");
200 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
201 printf("bind failed\n");
205 fd = udev_monitor_get_fd(udev_monitor);
209 struct udev_device *device;
212 FD_SET(STDIN_FILENO, &readfds);
213 FD_SET(fd, &readfds);
215 printf("waiting for events on %s, press ENTER to exit\n", socket_path);
216 fdcount = select(fd+1, &readfds, NULL, NULL, NULL);
217 printf("select fd count: %i\n", fdcount);
219 if (FD_ISSET(fd, &readfds)) {
220 device = udev_monitor_receive_device(udev_monitor);
221 if (device == NULL) {
222 printf("no device from socket\n");
225 print_device(device);
226 udev_device_unref(device);
229 if (FD_ISSET(STDIN_FILENO, &readfds)) {
230 printf("exiting loop\n");
235 udev_monitor_unref(udev_monitor);
239 int main(int argc, char *argv[], char *envp[])
241 struct udev *udev = NULL;
242 static const struct option options[] = {
243 { "syspath", 1, NULL, 'p' },
244 { "subsystem", 1, NULL, 's' },
245 { "socket", 1, NULL, 'S' },
246 { "debug", 0, NULL, 'd' },
247 { "help", 0, NULL, 'h' },
248 { "version", 0, NULL, 'V' },
251 const char *syspath = "/devices/virtual/mem/null";
252 const char *subsystem = NULL;
253 const char *socket = "@/org/kernel/udev/monitor";
258 printf("context: %p\n", udev);
260 printf("no context\n");
263 udev_set_log_fn(udev, log_fn);
264 printf("set log: %p\n", log_fn);
269 option = getopt_long(argc, argv, "+dhV", options, NULL);
284 if (udev_get_log_priority(udev) < LOG_INFO)
285 udev_set_log_priority(udev, LOG_INFO);
288 printf("--debug --syspath= --subsystem= --socket= --help\n");
291 printf("%s\n", VERSION);
298 str = udev_get_sys_path(udev);
299 printf("sys_path: '%s'\n", str);
300 str = udev_get_dev_path(udev);
301 printf("dev_path: '%s'\n", str);
303 /* add sys path if needed */
304 if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0) {
305 snprintf(path, sizeof(path), "%s%s", udev_get_sys_path(udev), syspath);
309 test_device(udev, syspath);
310 test_device_devnum(udev);
311 test_device_parents(udev, syspath);
312 test_enumerate(udev, subsystem);
313 test_monitor(udev, socket);