2 * udev_sysfs.c - sysfs access
4 * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 675 Mass Ave, Cambridge, MA 02139, USA.
33 char sysfs_path[PATH_SIZE];
36 static LIST_HEAD(dev_list);
38 /* attribute value cache */
39 static LIST_HEAD(attr_list);
41 struct list_head node;
43 char *value; /* points to value_local if value is cached */
44 char value_local[NAME_SIZE];
51 env = getenv("SYSFS_PATH");
53 strlcpy(sysfs_path, env, sizeof(sysfs_path));
54 remove_trailing_chars(sysfs_path, '/');
56 strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
57 dbg("sysfs_path='%s'", sysfs_path);
59 INIT_LIST_HEAD(&dev_list);
60 INIT_LIST_HEAD(&attr_list);
64 void sysfs_cleanup(void)
66 struct sysfs_attr *attr_loop;
67 struct sysfs_attr *attr_temp;
68 struct sysfs_device *dev_loop;
69 struct sysfs_device *dev_temp;
71 list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
72 list_del(&attr_loop->node);
76 list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
77 list_del(&dev_loop->node);
82 void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath, const char *subsystem)
86 strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
87 if (subsystem != NULL)
88 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
91 pos = strrchr(dev->devpath, '/');
95 strlcpy(dev->kernel_name, &pos[1], sizeof(dev->kernel_name));
96 dbg("kernel_name='%s'", dev->kernel_name);
98 /* some devices have '!' in their name, change that to '/' */
99 pos = dev->kernel_name;
100 while (pos[0] != '\0') {
106 /* get kernel number */
107 pos = &dev->kernel_name[strlen(dev->kernel_name)];
108 while (isdigit(pos[-1]))
110 strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
111 dbg("kernel_number='%s'", dev->kernel_number);
114 struct sysfs_device *sysfs_device_get(const char *devpath)
116 char path[PATH_SIZE];
117 char devpath_real[PATH_SIZE];
118 struct sysfs_device *dev;
119 struct sysfs_device *dev_loop;
121 char link_path[PATH_SIZE];
122 char link_target[PATH_SIZE];
126 dbg("open '%s'", devpath);
127 strlcpy(devpath_real, devpath, sizeof(devpath_real));
128 remove_trailing_chars(devpath_real, '/');
130 /* look for device already in cache (we never put an untranslated path in the cache) */
131 list_for_each_entry(dev_loop, &dev_list, node) {
132 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
133 dbg("found in cache '%s'", dev_loop->devpath);
138 /* if we got a link, resolve it to the real device */
139 strlcpy(path, sysfs_path, sizeof(path));
140 strlcat(path, devpath_real, sizeof(path));
141 if (lstat(path, &statbuf) != 0) {
142 dbg("stat '%s' failed: %s", path, strerror(errno));
145 if (S_ISLNK(statbuf.st_mode)) {
149 len = readlink(path, link_target, sizeof(link_target));
152 link_target[len] = '\0';
153 dbg("devpath link '%s' points to '%s'", path, link_target);
155 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
157 dbg("base '%s', tail '%s', back %i", devpath_real, &link_target[back * 3], back);
158 for (i = 0; i <= back; i++) {
159 pos = strrchr(devpath_real, '/');
164 dbg("after moving back '%s'", devpath_real);
165 strlcat(devpath_real, "/", sizeof(devpath_real));
166 strlcat(devpath_real, &link_target[back * 3], sizeof(devpath_real));
168 /* now look for device in cache after path translation */
169 list_for_each_entry(dev_loop, &dev_list, node) {
170 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
171 dbg("found in cache '%s'", dev_loop->devpath);
177 /* it is a new device */
178 dbg("'%s'", devpath_real);
179 dev = malloc(sizeof(struct sysfs_device));
182 memset(dev, 0x00, sizeof(struct sysfs_device));
184 sysfs_device_set_values(dev, devpath_real, NULL);
187 if (strncmp(dev->devpath, "/class/", 7) == 0) {
188 strlcpy(dev->subsystem, &dev->devpath[7], sizeof(dev->subsystem));
189 pos = strchr(dev->subsystem, '/');
193 dev->subsystem[0] = '\0';
194 } else if (strncmp(dev->devpath, "/block/", 7) == 0) {
195 strlcpy(dev->subsystem, "block", sizeof(dev->subsystem));
196 } else if (strncmp(dev->devpath, "/devices/", 9) == 0) {
197 /* get subsystem from "bus" link */
198 strlcpy(link_path, sysfs_path, sizeof(link_path));
199 strlcat(link_path, dev->devpath, sizeof(link_path));
200 strlcat(link_path, "/bus", sizeof(link_path));
201 len = readlink(link_path, link_target, sizeof(link_target));
203 link_target[len] = '\0';
204 dbg("bus link '%s' points to '%s'", link_path, link_target);
205 pos = strrchr(link_target, '/');
207 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
209 /* get subsystem from "subsystem" link */
210 strlcpy(link_path, sysfs_path, sizeof(link_path));
211 strlcat(link_path, dev->devpath, sizeof(link_path));
212 strlcat(link_path, "/subsystem", sizeof(link_path));
213 len = readlink(link_path, link_target, sizeof(link_target));
215 link_target[len] = '\0';
216 dbg("subsystem link '%s' points to '%s'", link_path, link_target);
217 pos = strrchr(link_target, '/');
219 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
222 /* get driver name */
223 strlcpy(link_path, sysfs_path, sizeof(link_path));
224 strlcat(link_path, dev->devpath, sizeof(link_path));
225 strlcat(link_path, "/driver", sizeof(link_path));
226 len = readlink(link_path, link_target, sizeof(link_target));
228 link_target[len] = '\0';
229 dbg("driver link '%s' points to '%s'", link_path, link_target);
230 pos = strrchr(link_target, '/');
232 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
234 } else if (strncmp(dev->devpath, "/bus/", 5) == 0 && strstr(dev->devpath, "/drivers/")) {
235 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
236 } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
237 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
240 dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'", dev->devpath, dev->subsystem, dev->driver);
241 list_add(&dev->node, &dev_list);
246 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
248 char parent_devpath[PATH_SIZE];
249 char device_link[PATH_SIZE];
250 char device_link_target[PATH_SIZE];
256 if (dev->parent != NULL)
259 /* requesting a parent is only valid for devices */
260 if ((strncmp(dev->devpath, "/devices/", 9) != 0) &&
261 (strncmp(dev->devpath, "/class/", 7) != 0) &&
262 (strncmp(dev->devpath, "/block/", 7) != 0))
265 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
266 dbg("'%s'", parent_devpath);
268 /* strip last element */
269 pos = strrchr(parent_devpath, '/');
270 if (pos == NULL || pos == parent_devpath)
274 /* are we at the top level */
275 if (strcmp(parent_devpath, "/devices") == 0) {
276 dbg("/devices top level");
280 /* at the top level of class/block we want to follow the "device" link */
281 if (strcmp(parent_devpath, "/block") == 0) {
282 dbg("/block top level, look for device link");
285 if (strncmp(parent_devpath, "/class", 6) == 0) {
286 pos = strrchr(parent_devpath, '/');
287 if (pos == &parent_devpath[6] || pos == parent_devpath) {
288 dbg("/class top level, look for device link");
292 dev->parent = sysfs_device_get(parent_devpath);
296 strlcpy(device_link, sysfs_path, sizeof(device_link));
297 strlcat(device_link, dev->devpath, sizeof(device_link));
298 strlcat(device_link, "/device", sizeof(device_link));
299 len = readlink(device_link, device_link_target, sizeof(device_link_target));
302 device_link_target[len] = '\0';
303 dbg("device link '%s' points to '%s'", device_link, device_link_target);
305 for (back = 0; strncmp(&device_link_target[back * 3], "../", 3) == 0; back++)
307 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
308 dbg("base='%s', tail='%s', back=%i", parent_devpath, &device_link_target[back * 3], back);
309 for (i = 0; i < back; i++) {
310 pos = strrchr(parent_devpath, '/');
315 dbg("after moving back '%s'", parent_devpath);
316 strlcat(parent_devpath, "/", sizeof(parent_devpath));
317 strlcat(parent_devpath, &device_link_target[back * 3], sizeof(parent_devpath));
319 dev->parent = sysfs_device_get(parent_devpath);
323 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
325 struct sysfs_device *dev_parent;
327 dev_parent = sysfs_device_get_parent(dev);
328 while (dev_parent != NULL) {
329 if (strcmp(dev_parent->subsystem, subsystem) == 0)
331 dev_parent = sysfs_device_get_parent(dev_parent);
336 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
338 char path_full[PATH_SIZE];
340 char value[NAME_SIZE];
341 struct sysfs_attr *attr_loop;
342 struct sysfs_attr *attr;
347 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
348 path = &path_full[sysfs_len];
349 strlcat(path_full, devpath, sizeof(path_full));
350 strlcat(path_full, "/", sizeof(path_full));
351 strlcat(path_full, attr_name, sizeof(path_full));
353 /* look for attribute in cache */
354 list_for_each_entry(attr_loop, &attr_list, node) {
355 if (strcmp(attr_loop->path, path) == 0) {
356 dbg("found in cache '%s'", attr_loop->path);
357 return attr_loop->value;
361 /* store attribute in cache (also negatives are kept in cache) */
362 attr = malloc(sizeof(struct sysfs_attr));
365 memset(attr, 0x00, sizeof(struct sysfs_attr));
366 strlcpy(attr->path, path, sizeof(attr->path));
367 dbg("add to cache '%s' '%s'", attr->path, attr->value);
368 list_add(&attr->node, &attr_list);
370 /* read attribute value */
371 fd = open(path_full, O_RDONLY);
374 size = read(fd, value, sizeof(value));
378 if (size == sizeof(value))
381 /* got a valid value, store and return it */
383 remove_trailing_chars(value, '\n');
384 strlcpy(attr->value_local, value, sizeof(attr->value_local));
385 attr->value = attr->value_local;