]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev/lib/test-libudev.c
libudev: switch API from devpath to syspath
[thirdparty/systemd.git] / udev / lib / test-libudev.c
CommitLineData
33a5cc29
KS
1/*
2 * test-libudev
3 *
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include "config.h"
21
22#include <stdio.h>
23#include <stdarg.h>
ba6929f6
KS
24#include <unistd.h>
25#include <errno.h>
26#include <string.h>
b2d9e4f2
KS
27#include <getopt.h>
28#include <syslog.h>
ba6929f6
KS
29#include <sys/select.h>
30
33a5cc29
KS
31#include "libudev.h"
32
33static void log_fn(struct udev *udev,
34 int priority, const char *file, int line, const char *fn,
35 const char *format, va_list args)
36{
37 printf("test-libudev: %s %s:%d ", fn, file, line);
38 vprintf(format, args);
39}
40
ba6929f6 41static int print_devlinks_cb(struct udev_device *udev_device, const char *value, void *data)
33a5cc29 42{
ba6929f6
KS
43 printf("link: '%s'\n", value);
44 return 0;
45}
46
47static int print_properties_cb(struct udev_device *udev_device, const char *key, const char *value, void *data)
48{
49 printf("property: '%s=%s'\n", key, value);
50 return 0;
51}
52
53static void print_device(struct udev_device *device)
54{
55 const char *str;
56 int count;
57
58 printf("*** device: %p ***\n", device);
8753fadf
KS
59 str = udev_device_get_syspath(device);
60 printf("syspath: '%s'\n", str);
ba6929f6
KS
61 str = udev_device_get_devpath(device);
62 printf("devpath: '%s'\n", str);
63 str = udev_device_get_subsystem(device);
64 printf("subsystem: '%s'\n", str);
95d90c4f
KS
65 str = udev_device_get_driver(device);
66 printf("driver: '%s'\n", str);
ba6929f6
KS
67 str = udev_device_get_devname(device);
68 printf("devname: '%s'\n", str);
69 count = udev_device_get_devlinks(device, print_devlinks_cb, NULL);
70 printf("found %i links\n", count);
71 count = udev_device_get_properties(device, print_properties_cb, NULL);
72 printf("found %i properties\n", count);
73 printf("\n");
74}
75
8753fadf 76static int test_device(struct udev *udev, const char *syspath)
ba6929f6
KS
77{
78 struct udev_device *device;
79
8753fadf
KS
80 printf("looking at device: %s\n", syspath);
81 device = udev_device_new_from_syspath(udev, syspath);
ba6929f6
KS
82 if (device == NULL) {
83 printf("no device\n");
84 return -1;
85 }
86 print_device(device);
87 udev_device_unref(device);
33a5cc29
KS
88 return 0;
89}
90
8753fadf 91static int test_device_parents(struct udev *udev, const char *syspath)
4ad3a37f
KS
92{
93 struct udev_device *device;
b2d9e4f2 94 struct udev_device *device_parent;
4ad3a37f 95
8753fadf
KS
96 printf("looking at device: %s\n", syspath);
97 device = udev_device_new_from_syspath(udev, syspath);
b2d9e4f2
KS
98 if (device == NULL)
99 return -1;
100
101 device_parent = device;
102 do {
103 print_device(device_parent);
104 device_parent = udev_device_get_parent(device_parent);
105 } while (device_parent != NULL);
106
107 device_parent = device;
108 do {
109 print_device(device_parent);
110 device_parent = udev_device_get_parent(device_parent);
111 } while (device_parent != NULL);
112 udev_device_unref(device);
4ad3a37f 113
4ad3a37f
KS
114 return 0;
115}
116
33a5cc29
KS
117static int devices_enum_cb(struct udev *udev,
118 const char *devpath, const char *subsystem, const char *name,
119 void *data)
120{
ba6929f6 121 printf("device: '%s' (%s) '%s'\n", devpath, subsystem, name);
33a5cc29
KS
122 return 0;
123}
124
ba6929f6 125static int test_enumerate(struct udev *udev, const char *subsystem)
33a5cc29 126{
ba6929f6
KS
127 int count;
128
6b12bdb6 129 count = udev_enumerate_devices(udev, subsystem, devices_enum_cb, NULL);
ba6929f6
KS
130 printf("found %i devices\n\n", count);
131 return count;
132}
133
134static int test_monitor(struct udev *udev, const char *socket_path)
135{
136 struct udev_monitor *udev_monitor;
137 fd_set readfds;
138 int fd;
139
140 udev_monitor = udev_monitor_new_from_socket(udev, socket_path);
141 if (udev_monitor == NULL) {
142 printf("no socket\n");
143 return -1;
144 }
d59f11e1
KS
145 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
146 printf("bind failed\n");
147 return -1;
148 }
ba6929f6
KS
149
150 fd = udev_monitor_get_fd(udev_monitor);
151 FD_ZERO(&readfds);
152
153 while (1) {
154 struct udev_device *device;
155 int fdcount;
156
157 FD_SET(STDIN_FILENO, &readfds);
158 FD_SET(fd, &readfds);
159
160 printf("waiting for events on %s, press ENTER to exit\n", socket_path);
161 fdcount = select(fd+1, &readfds, NULL, NULL, NULL);
162 printf("select fd count: %i\n", fdcount);
163
164 if (FD_ISSET(fd, &readfds)) {
d59f11e1 165 device = udev_monitor_receive_device(udev_monitor);
ba6929f6
KS
166 if (device == NULL) {
167 printf("no device from socket\n");
168 continue;
169 }
170 print_device(device);
171 udev_device_unref(device);
172 }
173
174 if (FD_ISSET(STDIN_FILENO, &readfds)) {
175 printf("exiting loop\n");
176 break;
177 }
178 }
179
180 udev_monitor_unref(udev_monitor);
33a5cc29
KS
181 return 0;
182}
183
184int main(int argc, char *argv[], char *envp[])
185{
b2d9e4f2
KS
186 struct udev *udev = NULL;
187 static const struct option options[] = {
8753fadf 188 { "syspath", 1, NULL, 'p' },
b2d9e4f2
KS
189 { "subsystem", 1, NULL, 's' },
190 { "socket", 1, NULL, 'S' },
191 { "debug", 0, NULL, 'd' },
192 { "help", 0, NULL, 'h' },
193 { "version", 0, NULL, 'V' },
194 {}
195 };
8753fadf 196 const char *syspath = "/devices/virtual/mem/null";
33a5cc29 197 const char *subsystem = NULL;
ba6929f6 198 const char *socket = "@/org/kernel/udev/monitor";
8753fadf 199 char path[1024];
ba6929f6 200 const char *str;
33a5cc29 201
33a5cc29
KS
202 udev = udev_new();
203 printf("context: %p\n", udev);
204 if (udev == NULL) {
205 printf("no context\n");
206 return 1;
207 }
208 udev_set_log_fn(udev, log_fn);
209 printf("set log: %p\n", log_fn);
210
b2d9e4f2
KS
211 while (1) {
212 int option;
213
214 option = getopt_long(argc, argv, "+dhV", options, NULL);
215 if (option == -1)
216 break;
217
218 switch (option) {
219 case 'p':
8753fadf 220 syspath = optarg;
b2d9e4f2
KS
221 break;
222 case 's':
223 subsystem = optarg;
224 break;
225 case 'S':
226 socket = optarg;
227 break;
228 case 'd':
229 if (udev_get_log_priority(udev) < LOG_INFO)
230 udev_set_log_priority(udev, LOG_INFO);
231 break;
232 case 'h':
8753fadf 233 printf("--debug --syspath= --subsystem= --socket= --help\n");
b2d9e4f2
KS
234 goto out;
235 case 'V':
236 printf("%s\n", VERSION);
237 goto out;
238 default:
239 goto out;
240 }
241 }
242
33a5cc29 243 str = udev_get_sys_path(udev);
ba6929f6 244 printf("sys_path: '%s'\n", str);
33a5cc29 245 str = udev_get_dev_path(udev);
ba6929f6 246 printf("dev_path: '%s'\n", str);
33a5cc29 247
8753fadf
KS
248 /* add sys path if needed */
249 if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0) {
250 snprintf(path, sizeof(path), "%s%s", udev_get_sys_path(udev), syspath);
251 syspath = path;
252 }
253
254 test_device(udev, syspath);
255 test_device_parents(udev, syspath);
ba6929f6
KS
256 test_enumerate(udev, subsystem);
257 test_monitor(udev, socket);
b2d9e4f2 258out:
33a5cc29
KS
259 udev_unref(udev);
260 return 0;
261}