chiark / gitweb /
421d0511a122e2d2361b974dee773847bbc301e5
[elogind.git] / udev / lib / test-libudev.c
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 <stdio.h>
21 #include <stdarg.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <getopt.h>
26 #include <syslog.h>
27 #include <fcntl.h>
28 #include <sys/select.h>
29
30 #include "libudev.h"
31
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)
35 {
36         printf("test-libudev: %s %s:%d ", fn, file, line);
37         vprintf(format, args);
38 }
39
40 static void print_device(struct udev_device *device)
41 {
42         const char *str;
43         dev_t devnum;
44         int count;
45         struct udev_list *list;
46
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));
62
63         count = 0;
64         list = udev_device_get_devlinks_list(device);
65         while (list != NULL) {
66                 printf("link:      '%s'\n", udev_list_get_name(list));
67                 count++;
68                 list = udev_list_get_next(list);
69         }
70         printf("found %i links\n", count);
71
72         count = 0;
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));
76                 count++;
77                 list = udev_list_get_next(list);
78         }
79         printf("found %i properties\n", count);
80
81         str = udev_device_get_attr_value(device, "dev");
82         printf("attr{dev}: '%s'\n", str);
83
84         printf("\n");
85 }
86
87 static int test_device(struct udev *udev, const char *syspath)
88 {
89         struct udev_device *device;
90
91         printf("looking at device: %s\n", syspath);
92         device = udev_device_new_from_syspath(udev, syspath);
93         if (device == NULL) {
94                 printf("no device\n");
95                 return -1;
96         }
97         print_device(device);
98         udev_device_unref(device);
99         return 0;
100 }
101
102 static int test_device_parents(struct udev *udev, const char *syspath)
103 {
104         struct udev_device *device;
105         struct udev_device *device_parent;
106
107         printf("looking at device: %s\n", syspath);
108         device = udev_device_new_from_syspath(udev, syspath);
109         if (device == NULL)
110                 return -1;
111
112         printf("looking at parents\n");
113         device_parent = device;
114         do {
115                 print_device(device_parent);
116                 device_parent = udev_device_get_parent(device_parent);
117         } while (device_parent != NULL);
118
119         printf("looking at parents again\n");
120         device_parent = device;
121         do {
122                 print_device(device_parent);
123                 device_parent = udev_device_get_parent(device_parent);
124         } while (device_parent != NULL);
125         udev_device_unref(device);
126
127         return 0;
128 }
129
130 static int test_device_devnum(struct udev *udev)
131 {
132         dev_t devnum = makedev(1, 3);
133         struct udev_device *device;
134
135         printf("looking up device: %u:%u\n", major(devnum), minor(devnum));
136         device = udev_device_new_from_devnum(udev, 'c', devnum);
137         if (device == NULL)
138                 return -1;
139         print_device(device);
140         udev_device_unref(device);
141         return 0;
142 }
143
144 static int test_enumerate(struct udev *udev, const char *subsystem)
145 {
146         struct udev_enumerate *enumerate;
147         struct udev_list *list;
148         int count = 0;
149
150         enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
151         if (enumerate == NULL)
152                 return -1;
153         list = udev_enumerate_get_devices_list(enumerate);
154         while (list != NULL) {
155                 struct udev_device *device;
156
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);
164                         count++;
165                 }
166                 list = udev_list_get_next(list);
167         }
168         udev_enumerate_unref(enumerate);
169         printf("found %i devices\n\n", count);
170         return count;
171 }
172
173 static int test_monitor(struct udev *udev, const char *socket_path)
174 {
175         struct udev_monitor *udev_monitor;
176         fd_set readfds;
177         int fd;
178
179         udev_monitor = udev_monitor_new_from_socket(udev, socket_path);
180         if (udev_monitor == NULL) {
181                 printf("no socket\n");
182                 return -1;
183         }
184         if (udev_monitor_enable_receiving(udev_monitor) < 0) {
185                 printf("bind failed\n");
186                 return -1;
187         }
188
189         fd = udev_monitor_get_fd(udev_monitor);
190         FD_ZERO(&readfds);
191
192         while (1) {
193                 struct udev_device *device;
194                 int fdcount;
195
196                 FD_SET(STDIN_FILENO, &readfds);
197                 FD_SET(fd, &readfds);
198
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);
202
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");
207                                 continue;
208                         }
209                         print_device(device);
210                         udev_device_unref(device);
211                 }
212
213                 if (FD_ISSET(STDIN_FILENO, &readfds)) {
214                         printf("exiting loop\n");
215                         break;
216                 }
217         }
218
219         udev_monitor_unref(udev_monitor);
220         return 0;
221 }
222
223 int main(int argc, char *argv[], char *envp[])
224 {
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' },
233                 {}
234         };
235         const char *syspath = "/devices/virtual/mem/null";
236         const char *subsystem = NULL;
237         const char *socket = "@/org/kernel/udev/monitor";
238         char path[1024];
239         const char *str;
240
241         udev = udev_new();
242         printf("context: %p\n", udev);
243         if (udev == NULL) {
244                 printf("no context\n");
245                 return 1;
246         }
247         udev_set_log_fn(udev, log_fn);
248         printf("set log: %p\n", log_fn);
249
250         while (1) {
251                 int option;
252
253                 option = getopt_long(argc, argv, "+dhV", options, NULL);
254                 if (option == -1)
255                         break;
256
257                 switch (option) {
258                 case 'p':
259                         syspath = optarg;
260                         break;
261                 case 's':
262                         subsystem = optarg;
263                         break;
264                 case 'S':
265                         socket = optarg;
266                         break;
267                 case 'd':
268                         if (udev_get_log_priority(udev) < LOG_INFO)
269                                 udev_set_log_priority(udev, LOG_INFO);
270                         break;
271                 case 'h':
272                         printf("--debug --syspath= --subsystem= --socket= --help\n");
273                         goto out;
274                 case 'V':
275                         printf("%s\n", VERSION);
276                         goto out;
277                 default:
278                         goto out;
279                 }
280         }
281
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);
286
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);
290                 syspath = path;
291         }
292
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);
298 out:
299         udev_unref(udev);
300         return 0;
301 }