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