chiark / gitweb /
7795f5f0fba2a165fa5659fb76ca8b85ecdbba45
[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 "config.h"
21
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <sys/select.h>
28
29 #include "libudev.h"
30
31 static void log_fn(struct udev *udev,
32                    int priority, const char *file, int line, const char *fn,
33                    const char *format, va_list args)
34 {
35         printf("test-libudev: %s %s:%d ", fn, file, line);
36         vprintf(format, args);
37 }
38
39 static int print_devlinks_cb(struct udev_device *udev_device, const char *value, void *data)
40 {
41         printf("link:      '%s'\n", value);
42         return 0;
43 }
44
45 static int print_properties_cb(struct udev_device *udev_device, const char *key, const char *value, void *data)
46 {
47         printf("property:  '%s=%s'\n", key, value);
48         return 0;
49 }
50
51 static void print_device(struct udev_device *device)
52 {
53         const char *str;
54         int count;
55
56         printf("*** device: %p ***\n", device);
57         str = udev_device_get_devpath(device);
58         printf("devpath:   '%s'\n", str);
59         str = udev_device_get_subsystem(device);
60         printf("subsystem: '%s'\n", str);
61         str = udev_device_get_driver(device);
62         printf("driver:    '%s'\n", str);
63         str = udev_device_get_syspath(device);
64         printf("syspath:   '%s'\n", str);
65         str = udev_device_get_devname(device);
66         printf("devname:   '%s'\n", str);
67         count = udev_device_get_devlinks(device, print_devlinks_cb, NULL);
68         printf("found %i links\n", count);
69         count = udev_device_get_properties(device, print_properties_cb, NULL);
70         printf("found %i properties\n", count);
71         printf("\n");
72 }
73
74 static int test_device(struct udev *udev, const char *devpath)
75 {
76         struct udev_device *device;
77
78         printf("looking at device: %s\n", devpath);
79         device = udev_device_new_from_devpath(udev, devpath);
80         if (device == NULL) {
81                 printf("no device\n");
82                 return -1;
83         }
84         print_device(device);
85         udev_device_unref(device);
86         return 0;
87 }
88
89 static int test_device_parents(struct udev *udev, const char *devpath)
90 {
91         struct udev_device *device;
92
93         printf("looking at device: %s\n", devpath);
94         device = udev_device_new_from_devpath(udev, devpath);
95         while (device != NULL) {
96                 struct udev_device *device_parent;
97
98                 print_device(device);
99                 device_parent = udev_device_new_from_parent(device);
100                 udev_device_unref(device);
101                 device = device_parent;
102         }
103         return 0;
104 }
105
106 static int devices_enum_cb(struct udev *udev,
107                            const char *devpath, const char *subsystem, const char *name,
108                            void *data)
109 {
110         printf("device:    '%s' (%s) '%s'\n", devpath, subsystem, name);
111         return 0;
112 }
113
114 static int test_enumerate(struct udev *udev, const char *subsystem)
115 {
116         int count;
117
118         count = udev_enumerate_devices(udev, subsystem, devices_enum_cb, NULL);
119         printf("found %i devices\n\n", count);
120         return count;
121 }
122
123 static int test_monitor(struct udev *udev, const char *socket_path)
124 {
125         struct udev_monitor *udev_monitor;
126         fd_set readfds;
127         int fd;
128
129         udev_monitor = udev_monitor_new_from_socket(udev, socket_path);
130         if (udev_monitor == NULL) {
131                 printf("no socket\n");
132                 return -1;
133         }
134         if (udev_monitor_enable_receiving(udev_monitor) < 0) {
135                 printf("bind failed\n");
136                 return -1;
137         }
138
139         fd = udev_monitor_get_fd(udev_monitor);
140         FD_ZERO(&readfds);
141
142         while (1) {
143                 struct udev_device *device;
144                 int fdcount;
145
146                 FD_SET(STDIN_FILENO, &readfds);
147                 FD_SET(fd, &readfds);
148
149                 printf("waiting for events on %s, press ENTER to exit\n", socket_path);
150                 fdcount = select(fd+1, &readfds, NULL, NULL, NULL);
151                 printf("select fd count: %i\n", fdcount);
152
153                 if (FD_ISSET(fd, &readfds)) {
154                         device = udev_monitor_receive_device(udev_monitor);
155                         if (device == NULL) {
156                                 printf("no device from socket\n");
157                                 continue;
158                         }
159                         print_device(device);
160                         udev_device_unref(device);
161                 }
162
163                 if (FD_ISSET(STDIN_FILENO, &readfds)) {
164                         printf("exiting loop\n");
165                         break;
166                 }
167         }
168
169         udev_monitor_unref(udev_monitor);
170         return 0;
171 }
172
173 int main(int argc, char *argv[], char *envp[])
174 {
175         struct udev *udev;
176         const char *devpath = "/devices/virtual/mem/null";
177         const char *subsystem = NULL;
178         const char *socket = "@/org/kernel/udev/monitor";
179         const char *str;
180
181         if (argv[1] != NULL) {
182                 devpath = argv[1];
183                 if (argv[2] != NULL)
184                         subsystem = argv[2];
185                         if (argv[3] != NULL)
186                                 socket = argv[3];
187         }
188
189         udev = udev_new();
190         printf("context: %p\n", udev);
191         if (udev == NULL) {
192                 printf("no context\n");
193                 return 1;
194         }
195         udev_set_log_fn(udev, log_fn);
196         printf("set log: %p\n", log_fn);
197
198         str = udev_get_sys_path(udev);
199         printf("sys_path: '%s'\n", str);
200         str = udev_get_dev_path(udev);
201         printf("dev_path: '%s'\n", str);
202
203         test_device(udev, devpath);
204         test_device_parents(udev, devpath);
205         test_enumerate(udev, subsystem);
206         test_monitor(udev, socket);
207
208         udev_unref(udev);
209         return 0;
210 }