chiark / gitweb /
add a bunch of private device properties to udev_device
[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_syspath(device);
62         printf("syspath:   '%s'\n", str);
63         str = udev_device_get_devname(device);
64         printf("devname:   '%s'\n", str);
65         count = udev_device_get_devlinks(device, print_devlinks_cb, NULL);
66         printf("found %i links\n", count);
67         count = udev_device_get_properties(device, print_properties_cb, NULL);
68         printf("found %i properties\n", count);
69         printf("\n");
70 }
71
72 static int test_device(struct udev *udev, const char *devpath)
73 {
74         struct udev_device *device;
75
76         printf("looking at device: %s\n", devpath);
77         device = udev_device_new_from_devpath(udev, devpath);
78         if (device == NULL) {
79                 printf("no device\n");
80                 return -1;
81         }
82         print_device(device);
83         udev_device_unref(device);
84         return 0;
85 }
86
87 static int devices_enum_cb(struct udev *udev,
88                            const char *devpath, const char *subsystem, const char *name,
89                            void *data)
90 {
91         printf("device:    '%s' (%s) '%s'\n", devpath, subsystem, name);
92         return 0;
93 }
94
95 static int test_enumerate(struct udev *udev, const char *subsystem)
96 {
97         int count;
98
99         count = udev_devices_enumerate(udev, subsystem, devices_enum_cb, NULL);
100         printf("found %i devices\n\n", count);
101         return count;
102 }
103
104 static int test_monitor(struct udev *udev, const char *socket_path)
105 {
106         struct udev_monitor *udev_monitor;
107         fd_set readfds;
108         int fd;
109
110         udev_monitor = udev_monitor_new_from_socket(udev, socket_path);
111         if (udev_monitor == NULL) {
112                 printf("no socket\n");
113                 return -1;
114         }
115         if (udev_monitor_enable_receiving(udev_monitor) < 0) {
116                 printf("bind failed\n");
117                 return -1;
118         }
119
120         fd = udev_monitor_get_fd(udev_monitor);
121         FD_ZERO(&readfds);
122
123         while (1) {
124                 struct udev_device *device;
125                 int fdcount;
126
127                 FD_SET(STDIN_FILENO, &readfds);
128                 FD_SET(fd, &readfds);
129
130                 printf("waiting for events on %s, press ENTER to exit\n", socket_path);
131                 fdcount = select(fd+1, &readfds, NULL, NULL, NULL);
132                 printf("select fd count: %i\n", fdcount);
133
134                 if (FD_ISSET(fd, &readfds)) {
135                         device = udev_monitor_receive_device(udev_monitor);
136                         if (device == NULL) {
137                                 printf("no device from socket\n");
138                                 continue;
139                         }
140                         print_device(device);
141                         udev_device_unref(device);
142                 }
143
144                 if (FD_ISSET(STDIN_FILENO, &readfds)) {
145                         printf("exiting loop\n");
146                         break;
147                 }
148         }
149
150         udev_monitor_unref(udev_monitor);
151         return 0;
152 }
153
154 int main(int argc, char *argv[], char *envp[])
155 {
156         struct udev *udev;
157         const char *devpath = "/devices/virtual/mem/null";
158         const char *subsystem = NULL;
159         const char *socket = "@/org/kernel/udev/monitor";
160         const char *str;
161
162         if (argv[1] != NULL) {
163                 devpath = argv[1];
164                 if (argv[2] != NULL)
165                         subsystem = argv[2];
166                         if (argv[3] != NULL)
167                                 socket = argv[3];
168         }
169
170         udev = udev_new();
171         printf("context: %p\n", udev);
172         if (udev == NULL) {
173                 printf("no context\n");
174                 return 1;
175         }
176         udev_set_log_fn(udev, log_fn);
177         printf("set log: %p\n", log_fn);
178
179         str = udev_get_sys_path(udev);
180         printf("sys_path: '%s'\n", str);
181         str = udev_get_dev_path(udev);
182         printf("dev_path: '%s'\n", str);
183
184         test_device(udev, devpath);
185         test_enumerate(udev, subsystem);
186         test_monitor(udev, socket);
187
188         udev_unref(udev);
189         return 0;
190 }