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