chiark / gitweb /
e86c660e6488fd1219793b4b90890af756328375
[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 "libudev.h"
25
26 static void log_fn(struct udev *udev,
27                    int priority, const char *file, int line, const char *fn,
28                    const char *format, va_list args)
29 {
30         printf("test-libudev: %s %s:%d ", fn, file, line);
31         vprintf(format, args);
32 }
33
34 static int devlinks_cb(struct udev_device *udev_device, const char *value, void *data)
35 {
36         printf("link: %s\n", value);
37         return 0;
38 }
39
40 static int devices_enum_cb(struct udev *udev,
41                            const char *devpath, const char *subsystem, const char *name,
42                            void *data)
43 {
44         printf("device: %s (%s) %s\n", devpath, subsystem, name);
45         return 0;
46 }
47
48 static int properties_cb(struct udev_device *udev_device, const char *key, const char *value, void *data)
49 {
50         printf("property: %s=%s\n", key, value);
51         return 0;
52 }
53
54 int main(int argc, char *argv[], char *envp[])
55 {
56         struct udev *udev;
57         struct udev_device *device;
58         const char *str;
59         const char *devpath = "/devices/virtual/mem/null";
60         const char *subsystem = NULL;
61
62         if (argv[1] != NULL) {
63                 devpath = argv[1];
64                 if (argv[2] != NULL)
65                         subsystem = argv[2];
66         }
67
68         udev = udev_new();
69         printf("context: %p\n", udev);
70         if (udev == NULL) {
71                 printf("no context\n");
72                 return 1;
73         }
74         udev_set_log_fn(udev, log_fn);
75         printf("set log: %p\n", log_fn);
76
77         str = udev_get_sys_path(udev);
78         printf("sys_path: %s\n", str);
79         str = udev_get_dev_path(udev);
80         printf("dev_path: %s\n", str);
81
82         printf("looking at device: %s\n", devpath);
83         device = udev_device_new_from_devpath(udev, devpath);
84         printf("device: %p\n", device);
85         if (device == NULL) {
86                 printf("no device\n");
87                 return 1;
88         }
89         str = udev_device_get_devpath(device);
90         printf("devpath: %s\n", str);
91         str = udev_device_get_subsystem(device);
92         printf("subsystem: %s\n", str);
93         str = udev_device_get_devname(device);
94         printf("devname: %s\n", str);
95         udev_device_get_devlinks(device, devlinks_cb, NULL);
96         udev_device_get_properties(device, properties_cb, NULL);
97         udev_device_unref(device);
98
99         if (subsystem == NULL)
100                 printf("enumerating devices from all subsystems\n");
101         else
102                 printf("enumerating devices from subsystem: %s\n", subsystem);
103         udev_devices_enumerate(udev, subsystem, devices_enum_cb, NULL);
104
105         udev_unref(udev);
106         return 0;
107 }