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,
83 const char *subsystem, const char *driver)
87 strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
88 if (subsystem != NULL)
89 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
91 strlcpy(dev->driver, driver, sizeof(dev->driver));
94 pos = strrchr(dev->devpath, '/');
98 strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
99 dbg("kernel='%s'", dev->kernel);
101 /* some devices have '!' in their name, change that to '/' */
103 while (pos[0] != '\0') {
109 /* get kernel number */
110 pos = &dev->kernel[strlen(dev->kernel)];
111 while (isdigit(pos[-1]))
113 strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
114 dbg("kernel_number='%s'", dev->kernel_number);
117 struct sysfs_device *sysfs_device_get(const char *devpath)
119 char path[PATH_SIZE];
120 char devpath_real[PATH_SIZE];
121 struct sysfs_device *dev;
122 struct sysfs_device *dev_loop;
124 char link_path[PATH_SIZE];
125 char link_target[PATH_SIZE];
129 dbg("open '%s'", devpath);
130 strlcpy(devpath_real, devpath, sizeof(devpath_real));
131 remove_trailing_chars(devpath_real, '/');
133 /* look for device already in cache (we never put an untranslated path in the cache) */
134 list_for_each_entry(dev_loop, &dev_list, node) {
135 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
136 dbg("found in cache '%s'", dev_loop->devpath);
141 /* if we got a link, resolve it to the real device */
142 strlcpy(path, sysfs_path, sizeof(path));
143 strlcat(path, devpath_real, sizeof(path));
144 if (lstat(path, &statbuf) != 0) {
145 dbg("stat '%s' failed: %s", path, strerror(errno));
148 if (S_ISLNK(statbuf.st_mode)) {
152 len = readlink(path, link_target, sizeof(link_target));
155 link_target[len] = '\0';
156 dbg("devpath link '%s' points to '%s'", path, link_target);
158 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
160 dbg("base '%s', tail '%s', back %i", devpath_real, &link_target[back * 3], back);
161 for (i = 0; i <= back; i++) {
162 pos = strrchr(devpath_real, '/');
167 dbg("after moving back '%s'", devpath_real);
168 strlcat(devpath_real, "/", sizeof(devpath_real));
169 strlcat(devpath_real, &link_target[back * 3], sizeof(devpath_real));
171 /* now look for device in cache after path translation */
172 list_for_each_entry(dev_loop, &dev_list, node) {
173 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
174 dbg("found in cache '%s'", dev_loop->devpath);
180 /* it is a new device */
181 dbg("new uncached device '%s'", devpath_real);
182 dev = malloc(sizeof(struct sysfs_device));
185 memset(dev, 0x00, sizeof(struct sysfs_device));
187 sysfs_device_set_values(dev, devpath_real, NULL, NULL);
190 if (strncmp(dev->devpath, "/class/", 7) == 0) {
191 strlcpy(dev->subsystem, &dev->devpath[7], sizeof(dev->subsystem));
192 pos = strchr(dev->subsystem, '/');
196 dev->subsystem[0] = '\0';
197 } else if (strncmp(dev->devpath, "/block/", 7) == 0) {
198 strlcpy(dev->subsystem, "block", sizeof(dev->subsystem));
199 } else if (strncmp(dev->devpath, "/devices/", 9) == 0) {
200 /* get subsystem from "bus" link */
201 strlcpy(link_path, sysfs_path, sizeof(link_path));
202 strlcat(link_path, dev->devpath, sizeof(link_path));
203 strlcat(link_path, "/bus", sizeof(link_path));
204 len = readlink(link_path, link_target, sizeof(link_target));
206 link_target[len] = '\0';
207 dbg("bus link '%s' points to '%s'", link_path, link_target);
208 pos = strrchr(link_target, '/');
210 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
212 /* get subsystem from "subsystem" link */
213 strlcpy(link_path, sysfs_path, sizeof(link_path));
214 strlcat(link_path, dev->devpath, sizeof(link_path));
215 strlcat(link_path, "/subsystem", sizeof(link_path));
216 len = readlink(link_path, link_target, sizeof(link_target));
218 link_target[len] = '\0';
219 dbg("subsystem link '%s' points to '%s'", link_path, link_target);
220 pos = strrchr(link_target, '/');
222 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
225 /* get driver name */
226 strlcpy(link_path, sysfs_path, sizeof(link_path));
227 strlcat(link_path, dev->devpath, sizeof(link_path));
228 strlcat(link_path, "/driver", sizeof(link_path));
229 len = readlink(link_path, link_target, sizeof(link_target));
231 link_target[len] = '\0';
232 dbg("driver link '%s' points to '%s'", link_path, link_target);
233 pos = strrchr(link_target, '/');
235 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
237 } else if (strncmp(dev->devpath, "/bus/", 5) == 0 && strstr(dev->devpath, "/drivers/")) {
238 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
239 } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
240 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
243 dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'", dev->devpath, dev->subsystem, dev->driver);
244 list_add(&dev->node, &dev_list);
249 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
251 char parent_devpath[PATH_SIZE];
252 char device_link[PATH_SIZE];
253 char device_link_target[PATH_SIZE];
259 dbg("open '%s'", dev->devpath);
261 /* look if we already know the parent */
262 if (dev->parent != NULL)
265 /* requesting a parent is only valid for devices */
266 if ((strncmp(dev->devpath, "/devices/", 9) != 0) &&
267 (strncmp(dev->devpath, "/class/", 7) != 0) &&
268 (strncmp(dev->devpath, "/block/", 7) != 0))
271 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
272 dbg("'%s'", parent_devpath);
274 /* strip last element */
275 pos = strrchr(parent_devpath, '/');
276 if (pos == NULL || pos == parent_devpath)
280 /* are we at the top level of /devices */
281 if (strcmp(parent_devpath, "/devices") == 0) {
282 dbg("/devices top level");
286 /* at the top level of class/block we want to follow the "device" link */
287 if (strcmp(parent_devpath, "/block") == 0) {
288 dbg("/block top level, look for device link");
291 if (strncmp(parent_devpath, "/class", 6) == 0) {
292 pos = strrchr(parent_devpath, '/');
293 if (pos == &parent_devpath[6] || pos == parent_devpath) {
294 dbg("/class top level, look for device link");
298 /* get parent and remember it */
299 dev->parent = sysfs_device_get(parent_devpath);
303 strlcpy(device_link, sysfs_path, sizeof(device_link));
304 strlcat(device_link, dev->devpath, sizeof(device_link));
305 strlcat(device_link, "/device", sizeof(device_link));
306 len = readlink(device_link, device_link_target, sizeof(device_link_target));
309 device_link_target[len] = '\0';
310 dbg("device link '%s' points to '%s'", device_link, device_link_target);
312 for (back = 0; strncmp(&device_link_target[back * 3], "../", 3) == 0; back++)
314 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
315 dbg("base='%s', tail='%s', back=%i", parent_devpath, &device_link_target[back * 3], back);
316 for (i = 0; i < back; i++) {
317 pos = strrchr(parent_devpath, '/');
322 dbg("after moving back '%s'", parent_devpath);
323 strlcat(parent_devpath, "/", sizeof(parent_devpath));
324 strlcat(parent_devpath, &device_link_target[back * 3], sizeof(parent_devpath));
326 /* get parent and remember it */
327 dev->parent = sysfs_device_get(parent_devpath);
331 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
333 struct sysfs_device *dev_parent;
335 dev_parent = sysfs_device_get_parent(dev);
336 while (dev_parent != NULL) {
337 if (strcmp(dev_parent->subsystem, subsystem) == 0)
339 dev_parent = sysfs_device_get_parent(dev_parent);
344 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
346 char path_full[PATH_SIZE];
348 char value[NAME_SIZE];
349 struct sysfs_attr *attr_loop;
350 struct sysfs_attr *attr;
355 dbg("open '%s'/'%s'", devpath, attr_name);
356 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
357 path = &path_full[sysfs_len];
358 strlcat(path_full, devpath, sizeof(path_full));
359 strlcat(path_full, "/", sizeof(path_full));
360 strlcat(path_full, attr_name, sizeof(path_full));
362 /* look for attribute in cache */
363 list_for_each_entry(attr_loop, &attr_list, node) {
364 if (strcmp(attr_loop->path, path) == 0) {
365 dbg("found in cache '%s'", attr_loop->path);
366 return attr_loop->value;
370 /* store attribute in cache (also negatives are kept in cache) */
371 dbg("new uncached attribute '%s'", path_full);
372 attr = malloc(sizeof(struct sysfs_attr));
375 memset(attr, 0x00, sizeof(struct sysfs_attr));
376 strlcpy(attr->path, path, sizeof(attr->path));
377 dbg("add to cache '%s'", path_full);
378 list_add(&attr->node, &attr_list);
380 /* read attribute value */
381 fd = open(path_full, O_RDONLY);
383 dbg("attribute '%s' does not exist", path_full);
386 size = read(fd, value, sizeof(value));
390 if (size == sizeof(value))
393 /* got a valid value, store and return it */
395 remove_trailing_chars(value, '\n');
396 dbg("cache '%s' with value '%s'", path_full, value);
397 strlcpy(attr->value_local, value, sizeof(attr->value_local));
398 attr->value = attr->value_local;