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 *list;
47 printf("*** device: %p ***\n", device);
48 str = udev_device_get_action(device);
49 printf("action: '%s'\n", str);
50 str = udev_device_get_syspath(device);
51 printf("syspath: '%s'\n", str);
52 str = udev_device_get_devpath(device);
53 printf("devpath: '%s'\n", str);
54 str = udev_device_get_subsystem(device);
55 printf("subsystem: '%s'\n", str);
56 str = udev_device_get_driver(device);
57 printf("driver: '%s'\n", str);
58 str = udev_device_get_devnode(device);
59 printf("devname: '%s'\n", str);
60 devnum = udev_device_get_devnum(device);
61 printf("devnum: %u:%u\n", major(devnum), minor(devnum));
64 list = udev_device_get_devlinks_list(device);
65 while (list != NULL) {
66 printf("link: '%s'\n", udev_list_get_name(list));
68 list = udev_list_get_next(list);
70 printf("found %i links\n", count);
73 list = udev_device_get_properties_list(device);
74 while (list != NULL) {
75 printf("property: '%s=%s'\n", udev_list_get_name(list), udev_list_get_value(list));
77 list = udev_list_get_next(list);
79 printf("found %i properties\n", count);
81 str = udev_device_get_attr_value(device, "dev");
82 printf("attr{dev}: '%s'\n", str);
87 static int test_device(struct udev *udev, const char *syspath)
89 struct udev_device *device;
91 printf("looking at device: %s\n", syspath);
92 device = udev_device_new_from_syspath(udev, syspath);
94 printf("no device\n");
98 udev_device_unref(device);
102 static int test_device_parents(struct udev *udev, const char *syspath)
104 struct udev_device *device;
105 struct udev_device *device_parent;
107 printf("looking at device: %s\n", syspath);
108 device = udev_device_new_from_syspath(udev, syspath);
112 printf("looking at parents\n");
113 device_parent = device;
115 print_device(device_parent);
116 device_parent = udev_device_get_parent(device_parent);
117 } while (device_parent != NULL);
119 printf("looking at parents again\n");
120 device_parent = device;
122 print_device(device_parent);
123 device_parent = udev_device_get_parent(device_parent);
124 } while (device_parent != NULL);
125 udev_device_unref(device);
130 static int test_device_devnum(struct udev *udev)
132 dev_t devnum = makedev(1, 3);
133 struct udev_device *device;
135 printf("looking up device: %u:%u\n", major(devnum), minor(devnum));
136 device = udev_device_new_from_devnum(udev, 'c', devnum);
139 print_device(device);
140 udev_device_unref(device);
144 static int test_enumerate(struct udev *udev, const char *subsystem)
146 struct udev_enumerate *enumerate;
147 struct udev_list *list;
150 enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
151 if (enumerate == NULL)
153 list = udev_enumerate_get_devices_list(enumerate);
154 while (list != NULL) {
155 struct udev_device *device;
157 device = udev_device_new_from_syspath(udev, udev_list_get_name(list));
158 if (device != NULL) {
159 printf("device: '%s' (%s) '%s'\n",
160 udev_device_get_syspath(device),
161 udev_device_get_subsystem(device),
162 udev_device_get_sysname(device));
163 udev_device_unref(device);
166 list = udev_list_get_next(list);
168 udev_enumerate_unref(enumerate);
169 printf("found %i devices\n\n", count);
173 static int test_monitor(struct udev *udev, const char *socket_path)
175 struct udev_monitor *udev_monitor;
179 udev_monitor = udev_monitor_new_from_socket(udev, socket_path);
180 if (udev_monitor == NULL) {
181 printf("no socket\n");
184 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
185 printf("bind failed\n");
189 fd = udev_monitor_get_fd(udev_monitor);
193 struct udev_device *device;
196 FD_SET(STDIN_FILENO, &readfds);
197 FD_SET(fd, &readfds);
199 printf("waiting for events on %s, press ENTER to exit\n", socket_path);
200 fdcount = select(fd+1, &readfds, NULL, NULL, NULL);
201 printf("select fd count: %i\n", fdcount);
203 if (FD_ISSET(fd, &readfds)) {
204 device = udev_monitor_receive_device(udev_monitor);
205 if (device == NULL) {
206 printf("no device from socket\n");
209 print_device(device);
210 udev_device_unref(device);
213 if (FD_ISSET(STDIN_FILENO, &readfds)) {
214 printf("exiting loop\n");
219 udev_monitor_unref(udev_monitor);
223 int main(int argc, char *argv[], char *envp[])
225 struct udev *udev = NULL;
226 static const struct option options[] = {
227 { "syspath", 1, NULL, 'p' },
228 { "subsystem", 1, NULL, 's' },
229 { "socket", 1, NULL, 'S' },
230 { "debug", 0, NULL, 'd' },
231 { "help", 0, NULL, 'h' },
232 { "version", 0, NULL, 'V' },
235 const char *syspath = "/devices/virtual/mem/null";
236 const char *subsystem = NULL;
237 const char *socket = "@/org/kernel/udev/monitor";
242 printf("context: %p\n", udev);
244 printf("no context\n");
247 udev_set_log_fn(udev, log_fn);
248 printf("set log: %p\n", log_fn);
253 option = getopt_long(argc, argv, "+dhV", options, NULL);
268 if (udev_get_log_priority(udev) < LOG_INFO)
269 udev_set_log_priority(udev, LOG_INFO);
272 printf("--debug --syspath= --subsystem= --socket= --help\n");
275 printf("%s\n", VERSION);
282 str = udev_get_sys_path(udev);
283 printf("sys_path: '%s'\n", str);
284 str = udev_get_dev_path(udev);
285 printf("dev_path: '%s'\n", str);
287 /* add sys path if needed */
288 if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0) {
289 snprintf(path, sizeof(path), "%s%s", udev_get_sys_path(udev), syspath);
293 test_device(udev, syspath);
294 test_device_devnum(udev);
295 test_device_parents(udev, syspath);
296 test_enumerate(udev, subsystem);
297 test_monitor(udev, socket);