chiark / gitweb /
bff55cfcd61180c9166ce595ea195bec90e90f1e
[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_entry *list_entry;
46
47         printf("*** device: %p ***\n", device);
48         str = udev_device_get_action(device);
49         if (str != NULL)
50                 printf("action:    '%s'\n", str);
51
52         str = udev_device_get_syspath(device);
53         printf("syspath:   '%s'\n", str);
54
55         str = udev_device_get_devpath(device);
56         printf("devpath:   '%s'\n", str);
57
58         str = udev_device_get_subsystem(device);
59         if (str != NULL)
60                 printf("subsystem: '%s'\n", str);
61
62         str = udev_device_get_driver(device);
63         if (str != NULL)
64                 printf("driver:    '%s'\n", str);
65
66         str = udev_device_get_devnode(device);
67         if (str != NULL)
68                 printf("devname:   '%s'\n", str);
69
70         devnum = udev_device_get_devnum(device);
71         if (major(devnum) > 0)
72                 printf("devnum:    %u:%u\n", major(devnum), minor(devnum));
73
74         count = 0;
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));
78                 count++;
79                 list_entry = udev_list_entry_get_next(list_entry);
80         }
81         if (count > 0)
82                 printf("found %i links\n", count);
83
84         count = 0;
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));
90                 count++;
91                 list_entry = udev_list_entry_get_next(list_entry);
92         }
93         if (count > 0)
94                 printf("found %i properties\n", count);
95
96         str = udev_device_get_attr_value(device, "dev");
97         if (str != NULL)
98                 printf("attr{dev}: '%s'\n", str);
99
100         printf("\n");
101 }
102
103 static int test_device(struct udev *udev, const char *syspath)
104 {
105         struct udev_device *device;
106
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");
111                 return -1;
112         }
113         print_device(device);
114         udev_device_unref(device);
115         return 0;
116 }
117
118 static int test_device_parents(struct udev *udev, const char *syspath)
119 {
120         struct udev_device *device;
121         struct udev_device *device_parent;
122
123         printf("looking at device: %s\n", syspath);
124         device = udev_device_new_from_syspath(udev, syspath);
125         if (device == NULL)
126                 return -1;
127
128         printf("looking at parents\n");
129         device_parent = device;
130         do {
131                 print_device(device_parent);
132                 device_parent = udev_device_get_parent(device_parent);
133         } while (device_parent != NULL);
134
135         printf("looking at parents again\n");
136         device_parent = device;
137         do {
138                 print_device(device_parent);
139                 device_parent = udev_device_get_parent(device_parent);
140         } while (device_parent != NULL);
141         udev_device_unref(device);
142
143         return 0;
144 }
145
146 static int test_device_devnum(struct udev *udev)
147 {
148         dev_t devnum = makedev(1, 3);
149         struct udev_device *device;
150
151         printf("looking up device: %u:%u\n", major(devnum), minor(devnum));
152         device = udev_device_new_from_devnum(udev, 'c', devnum);
153         if (device == NULL)
154                 return -1;
155         print_device(device);
156         udev_device_unref(device);
157         return 0;
158 }
159
160 static int test_enumerate(struct udev *udev, const char *subsystem)
161 {
162         struct udev_enumerate *enumerate;
163         struct udev_list_entry *list_entry;
164         int count = 0;
165
166         enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
167         if (enumerate == NULL)
168                 return -1;
169         list_entry = udev_enumerate_get_list_entry(enumerate);
170         while (list_entry != NULL) {
171                 struct udev_device *device;
172
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);
180                         count++;
181                 }
182                 list_entry = udev_list_entry_get_next(list_entry);
183         }
184         udev_enumerate_unref(enumerate);
185         printf("found %i devices\n\n", count);
186         return count;
187 }
188
189 static int test_monitor(struct udev *udev, const char *socket_path)
190 {
191         struct udev_monitor *udev_monitor;
192         fd_set readfds;
193         int fd;
194
195         udev_monitor = udev_monitor_new_from_socket(udev, socket_path);
196         if (udev_monitor == NULL) {
197                 printf("no socket\n");
198                 return -1;
199         }
200         if (udev_monitor_enable_receiving(udev_monitor) < 0) {
201                 printf("bind failed\n");
202                 return -1;
203         }
204
205         fd = udev_monitor_get_fd(udev_monitor);
206         FD_ZERO(&readfds);
207
208         while (1) {
209                 struct udev_device *device;
210                 int fdcount;
211
212                 FD_SET(STDIN_FILENO, &readfds);
213                 FD_SET(fd, &readfds);
214
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);
218
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");
223                                 continue;
224                         }
225                         print_device(device);
226                         udev_device_unref(device);
227                 }
228
229                 if (FD_ISSET(STDIN_FILENO, &readfds)) {
230                         printf("exiting loop\n");
231                         break;
232                 }
233         }
234
235         udev_monitor_unref(udev_monitor);
236         return 0;
237 }
238
239 int main(int argc, char *argv[], char *envp[])
240 {
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' },
249                 {}
250         };
251         const char *syspath = "/devices/virtual/mem/null";
252         const char *subsystem = NULL;
253         const char *socket = "@/org/kernel/udev/monitor";
254         char path[1024];
255         const char *str;
256
257         udev = udev_new();
258         printf("context: %p\n", udev);
259         if (udev == NULL) {
260                 printf("no context\n");
261                 return 1;
262         }
263         udev_set_log_fn(udev, log_fn);
264         printf("set log: %p\n", log_fn);
265
266         while (1) {
267                 int option;
268
269                 option = getopt_long(argc, argv, "+dhV", options, NULL);
270                 if (option == -1)
271                         break;
272
273                 switch (option) {
274                 case 'p':
275                         syspath = optarg;
276                         break;
277                 case 's':
278                         subsystem = optarg;
279                         break;
280                 case 'S':
281                         socket = optarg;
282                         break;
283                 case 'd':
284                         if (udev_get_log_priority(udev) < LOG_INFO)
285                                 udev_set_log_priority(udev, LOG_INFO);
286                         break;
287                 case 'h':
288                         printf("--debug --syspath= --subsystem= --socket= --help\n");
289                         goto out;
290                 case 'V':
291                         printf("%s\n", VERSION);
292                         goto out;
293                 default:
294                         goto out;
295                 }
296         }
297
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);
302
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);
306                 syspath = path;
307         }
308
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);
314 out:
315         udev_unref(udev);
316         return 0;
317 }