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