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/>.
27 #include <sys/select.h>
31 static void log_fn(struct udev *udev,
32 int priority, const char *file, int line, const char *fn,
33 const char *format, va_list args)
35 printf("test-libudev: %s %s:%d ", fn, file, line);
36 vprintf(format, args);
39 static int print_devlinks_cb(struct udev_device *udev_device, const char *value, void *data)
41 printf("link: '%s'\n", value);
45 static int print_properties_cb(struct udev_device *udev_device, const char *key, const char *value, void *data)
47 printf("property: '%s=%s'\n", key, value);
51 static void print_device(struct udev_device *device)
56 printf("*** device: %p ***\n", device);
57 str = udev_device_get_devpath(device);
58 printf("devpath: '%s'\n", str);
59 str = udev_device_get_subsystem(device);
60 printf("subsystem: '%s'\n", str);
61 str = udev_device_get_driver(device);
62 printf("driver: '%s'\n", str);
63 str = udev_device_get_syspath(device);
64 printf("syspath: '%s'\n", str);
65 str = udev_device_get_devname(device);
66 printf("devname: '%s'\n", str);
67 count = udev_device_get_devlinks(device, print_devlinks_cb, NULL);
68 printf("found %i links\n", count);
69 count = udev_device_get_properties(device, print_properties_cb, NULL);
70 printf("found %i properties\n", count);
74 static int test_device(struct udev *udev, const char *devpath)
76 struct udev_device *device;
78 printf("looking at device: %s\n", devpath);
79 device = udev_device_new_from_devpath(udev, devpath);
81 printf("no device\n");
85 udev_device_unref(device);
89 static int devices_enum_cb(struct udev *udev,
90 const char *devpath, const char *subsystem, const char *name,
93 printf("device: '%s' (%s) '%s'\n", devpath, subsystem, name);
97 static int test_enumerate(struct udev *udev, const char *subsystem)
101 count = udev_enumerate_devices(udev, subsystem, devices_enum_cb, NULL);
102 printf("found %i devices\n\n", count);
106 static int test_monitor(struct udev *udev, const char *socket_path)
108 struct udev_monitor *udev_monitor;
112 udev_monitor = udev_monitor_new_from_socket(udev, socket_path);
113 if (udev_monitor == NULL) {
114 printf("no socket\n");
117 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
118 printf("bind failed\n");
122 fd = udev_monitor_get_fd(udev_monitor);
126 struct udev_device *device;
129 FD_SET(STDIN_FILENO, &readfds);
130 FD_SET(fd, &readfds);
132 printf("waiting for events on %s, press ENTER to exit\n", socket_path);
133 fdcount = select(fd+1, &readfds, NULL, NULL, NULL);
134 printf("select fd count: %i\n", fdcount);
136 if (FD_ISSET(fd, &readfds)) {
137 device = udev_monitor_receive_device(udev_monitor);
138 if (device == NULL) {
139 printf("no device from socket\n");
142 print_device(device);
143 udev_device_unref(device);
146 if (FD_ISSET(STDIN_FILENO, &readfds)) {
147 printf("exiting loop\n");
152 udev_monitor_unref(udev_monitor);
156 int main(int argc, char *argv[], char *envp[])
159 const char *devpath = "/devices/virtual/mem/null";
160 const char *subsystem = NULL;
161 const char *socket = "@/org/kernel/udev/monitor";
164 if (argv[1] != NULL) {
173 printf("context: %p\n", udev);
175 printf("no context\n");
178 udev_set_log_fn(udev, log_fn);
179 printf("set log: %p\n", log_fn);
181 str = udev_get_sys_path(udev);
182 printf("sys_path: '%s'\n", str);
183 str = udev_get_dev_path(udev);
184 printf("dev_path: '%s'\n", str);
186 test_device(udev, devpath);
187 test_enumerate(udev, subsystem);
188 test_monitor(udev, socket);