chiark / gitweb /
udevadm: info - use "udev_device"
[elogind.git] / udev / lib / libudev-enumerate.c
1 /*
2  * libudev - interface to udev device information
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 <stdlib.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30
31 #include "libudev.h"
32 #include "libudev-private.h"
33
34 static int devices_scan_subsystem(struct udev *udev,
35                                   const char *basedir, const char *subsystem, const char *subdir,
36                                   struct list_head *device_list)
37 {
38         char path[UTIL_PATH_SIZE];
39         DIR *dir;
40         struct dirent *dent;
41         size_t len;
42
43         len = util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
44         util_strlcat(path, basedir, sizeof(path));
45         util_strlcat(path, "/", sizeof(path));
46         util_strlcat(path, subsystem, sizeof(path));
47         if (subdir != NULL)
48                 util_strlcat(path, subdir, sizeof(path));
49         dir = opendir(path);
50         if (dir == NULL)
51                 return -1;
52         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
53                 char devpath[UTIL_PATH_SIZE];
54
55                 if (dent->d_name[0] == '.')
56                         continue;
57                 util_strlcpy(devpath, &path[len], sizeof(devpath));
58                 util_strlcat(devpath, "/", sizeof(devpath));
59                 util_strlcat(devpath, dent->d_name, sizeof(devpath));
60                 util_resolve_sys_link(udev, devpath, sizeof(devpath));
61                 util_name_list_add(udev, device_list, devpath, 1);
62         }
63         closedir(dir);
64         return 0;
65 }
66
67 static int devices_scan_subsystems(struct udev *udev,
68                                    const char *basedir, const char *subsystem, const char *subdir,
69                                    struct list_head *device_list)
70 {
71         char path[UTIL_PATH_SIZE];
72         DIR *dir;
73         struct dirent *dent;
74
75         if (subsystem != NULL)
76                 return devices_scan_subsystem(udev, basedir, subsystem, subdir, device_list);
77
78         util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
79         util_strlcat(path, basedir, sizeof(path));
80         dir = opendir(path);
81         if (dir == NULL)
82                 return -1;
83         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
84                 if (dent->d_name[0] == '.')
85                         continue;
86                 devices_scan_subsystem(udev, basedir, dent->d_name, subdir, device_list);
87         }
88         closedir(dir);
89         return 0;
90 }
91
92 static int devices_delay(struct udev *udev, const char *devpath)
93 {
94         static const char *delay_device_list[] = {
95                 "/block/md",
96                 "/block/dm-",
97                 NULL
98         };
99         int i;
100
101         for (i = 0; delay_device_list[i] != NULL; i++) {
102                 if (strstr(devpath, delay_device_list[i]) != NULL) {
103                         info(udev, "delaying: %s\n", devpath);
104                         return 1;
105                 }
106         }
107         return 0;
108 }
109
110 static int devices_call(struct udev *udev, const char *devpath,
111                         int (*cb)(struct udev *udev,
112                                   const char *devpath, const char *subsystem, const char *name,
113                                   void *data),
114                         void *data,
115                         int *cb_rc)
116 {
117         char subsystem[UTIL_PATH_SIZE];
118         const char *name;
119
120         name = strrchr(devpath, '/');
121         if (name == NULL)
122                 return -1;
123         name++;
124
125         if (util_get_sys_subsystem(udev, devpath, subsystem, sizeof(subsystem)) < 2)
126                 return -1;
127         *cb_rc = cb(udev, devpath, subsystem, name, data);
128         return 0;
129 }
130
131 /**
132  * udev_enumerate_devices:
133  * @udev_device: udev device
134  * @cb: function to be called for every property found
135  * @data: data to be passed to the function
136  *
137  * Retrieve the property key/value pairs belonging to the
138  * udev device. For every key/value pair, the passed function will be
139  * called. If the function returns 1, remaning properties will be
140  * ignored.
141  *
142  * Returns: the number of properties passed to the caller, or a negative value on error
143  **/
144 int udev_enumerate_devices(struct udev *udev, const char *subsystem,
145                            int (*cb)(struct udev *udev,
146                                      const char *devpath, const char *subsystem, const char *name, void *data),
147                            void *data)
148 {
149         char base[UTIL_PATH_SIZE];
150         struct stat statbuf;
151         struct list_head device_list;
152         struct util_name_entry *loop_device;
153         struct util_name_entry *tmp_device;
154         int cb_rc = 0;
155         int count = 0;
156
157         INIT_LIST_HEAD(&device_list);
158
159         /* if we have /sys/subsystem/, forget all the old stuff */
160         util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
161         util_strlcat(base, "/subsystem", sizeof(base));
162         if (stat(base, &statbuf) == 0) {
163                 devices_scan_subsystems(udev, "/subsystem", subsystem, "/devices", &device_list);
164         } else {
165                 devices_scan_subsystems(udev, "/bus", subsystem, "/devices", &device_list);
166                 devices_scan_subsystems(udev, "/class", subsystem, NULL, &device_list);
167         }
168
169         list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
170                 if (devices_delay(udev, loop_device->name))
171                         continue;
172                 if (cb_rc == 0)
173                         if (devices_call(udev, loop_device->name, cb, data, &cb_rc) == 0)
174                                 count++;
175                 list_del(&loop_device->node);
176                 free(loop_device->name);
177                 free(loop_device);
178         }
179
180         /* handle remaining delayed devices */
181         list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
182                 if (cb_rc == 0)
183                         if (devices_call(udev, loop_device->name, cb, data, &cb_rc) == 0)
184                                 count++;
185                 list_del(&loop_device->node);
186                 free(loop_device->name);
187                 free(loop_device);
188         }
189
190         return count;
191 }