2 * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
37 char sysfs_path[PATH_SIZE];
39 /* attribute value cache */
40 static LIST_HEAD(attr_list);
42 struct list_head node;
44 char *value; /* points to value_local if value is cached */
45 char value_local[NAME_SIZE];
48 /* list of sysfs devices */
49 static LIST_HEAD(sysfs_dev_list);
51 struct list_head node;
52 struct sysfs_device dev;
55 int sysfs_init(char *path, size_t len)
58 strlcpy(sysfs_path, path, len);
59 remove_trailing_chars(sysfs_path, '/');
61 strlcpy(sysfs_path, "/sys", len);
62 dbg("sysfs_path='%s'", sysfs_path);
64 INIT_LIST_HEAD(&attr_list);
65 INIT_LIST_HEAD(&sysfs_dev_list);
69 void sysfs_cleanup(void)
71 struct sysfs_attr *attr_loop;
72 struct sysfs_attr *attr_temp;
74 struct sysfs_dev *sysdev_loop;
75 struct sysfs_dev *sysdev_temp;
77 list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
78 list_del(&attr_loop->node);
82 list_for_each_entry_safe(sysdev_loop, sysdev_temp, &sysfs_dev_list, node) {
87 void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath,
88 const char *subsystem, const char *driver)
92 strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
93 if (subsystem != NULL)
94 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
96 strlcpy(dev->driver, driver, sizeof(dev->driver));
99 pos = strrchr(dev->devpath, '/');
102 strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
103 dbg("kernel='%s'", dev->kernel);
105 /* some devices have '!' in their name, change that to '/' */
107 while (pos[0] != '\0') {
113 /* get kernel number */
114 pos = &dev->kernel[strlen(dev->kernel)];
115 while (isdigit(pos[-1]))
117 strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
118 dbg("kernel_number='%s'", dev->kernel_number);
121 int sysfs_resolve_link(char *devpath, size_t size)
123 char link_path[PATH_SIZE];
124 char link_target[PATH_SIZE];
129 strlcpy(link_path, sysfs_path, sizeof(link_path));
130 strlcat(link_path, devpath, sizeof(link_path));
131 len = readlink(link_path, link_target, sizeof(link_target));
134 link_target[len] = '\0';
135 dbg("path link '%s' points to '%s'", devpath, link_target);
137 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
139 dbg("base '%s', tail '%s', back %i", devpath, &link_target[back * 3], back);
140 for (i = 0; i <= back; i++) {
141 char *pos = strrchr(devpath, '/');
147 dbg("after moving back '%s'", devpath);
148 strlcat(devpath, "/", size);
149 strlcat(devpath, &link_target[back * 3], size);
153 struct sysfs_device *sysfs_device_get(const char *devpath)
155 char path[PATH_SIZE];
156 char devpath_real[PATH_SIZE];
157 struct sysfs_device *dev = NULL;
158 struct sysfs_dev *sysdev_loop, *sysdev;
160 char link_path[PATH_SIZE];
161 char link_target[PATH_SIZE];
165 dbg("open '%s'", devpath);
166 strlcpy(devpath_real, devpath, sizeof(devpath_real));
167 remove_trailing_chars(devpath_real, '/');
169 /* if we got a link, resolve it to the real device */
170 strlcpy(path, sysfs_path, sizeof(path));
171 strlcat(path, devpath_real, sizeof(path));
172 if (lstat(path, &statbuf) != 0) {
173 /* if stat fails look in the cache */
174 dbg("stat '%s' failed: %s", path, strerror(errno));
175 list_for_each_entry(sysdev_loop, &sysfs_dev_list, node) {
176 if (strcmp(sysdev_loop->dev.devpath, devpath_real) == 0) {
177 dbg("found vanished dev in cache '%s'", sysdev_loop->dev.devpath);
178 return &sysdev_loop->dev;
183 if (S_ISLNK(statbuf.st_mode)) {
184 if (sysfs_resolve_link(devpath_real, sizeof(devpath_real)) != 0)
189 list_for_each_entry(sysdev_loop, &sysfs_dev_list, node) {
190 if (strcmp(sysdev_loop->dev.devpath, devpath_real) == 0) {
191 dbg("found dev in cache '%s'", sysdev_loop->dev.devpath);
192 dev = &sysdev_loop->dev;
196 /* it is a new device */
197 dbg("new device '%s'", devpath_real);
198 sysdev = malloc(sizeof(struct sysfs_dev));
201 memset(sysdev, 0x00, sizeof(struct sysfs_dev));
202 list_add(&sysdev->node, &sysfs_dev_list);
206 sysfs_device_set_values(dev, devpath_real, NULL, NULL);
208 /* get subsystem name */
209 strlcpy(link_path, sysfs_path, sizeof(link_path));
210 strlcat(link_path, dev->devpath, sizeof(link_path));
211 strlcat(link_path, "/subsystem", sizeof(link_path));
212 len = readlink(link_path, link_target, sizeof(link_target));
214 /* get subsystem from "subsystem" link */
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));
220 } else if (strncmp(dev->devpath, "/class/", 7) == 0) {
221 /* get subsystem from class dir */
222 strlcpy(dev->subsystem, &dev->devpath[7], sizeof(dev->subsystem));
223 pos = strchr(dev->subsystem, '/');
227 dev->subsystem[0] = '\0';
228 } else if (strncmp(dev->devpath, "/block/", 7) == 0) {
229 strlcpy(dev->subsystem, "block", sizeof(dev->subsystem));
230 } else if (strncmp(dev->devpath, "/devices/", 9) == 0) {
231 /* get subsystem from "bus" link */
232 strlcpy(link_path, sysfs_path, sizeof(link_path));
233 strlcat(link_path, dev->devpath, sizeof(link_path));
234 strlcat(link_path, "/bus", sizeof(link_path));
235 len = readlink(link_path, link_target, sizeof(link_target));
237 link_target[len] = '\0';
238 dbg("bus link '%s' points to '%s'", link_path, link_target);
239 pos = strrchr(link_target, '/');
241 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
243 } else if (strstr(dev->devpath, "/drivers/") != NULL) {
244 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
245 } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
246 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
249 /* get driver name */
250 strlcpy(link_path, sysfs_path, sizeof(link_path));
251 strlcat(link_path, dev->devpath, sizeof(link_path));
252 strlcat(link_path, "/driver", sizeof(link_path));
253 len = readlink(link_path, link_target, sizeof(link_target));
255 link_target[len] = '\0';
256 dbg("driver link '%s' points to '%s'", link_path, link_target);
257 pos = strrchr(link_target, '/');
259 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
264 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
266 char parent_devpath[PATH_SIZE];
269 dbg("open '%s'", dev->devpath);
271 /* look if we already know the parent */
272 if (dev->parent != NULL)
275 /* requesting a parent is only valid for devices */
276 if ((strncmp(dev->devpath, "/devices/", 9) != 0) &&
277 (strncmp(dev->devpath, "/subsystem/", 11) != 0) &&
278 (strncmp(dev->devpath, "/class/", 7) != 0) &&
279 (strncmp(dev->devpath, "/block/", 7) != 0))
282 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
283 dbg("'%s'", parent_devpath);
285 /* strip last element */
286 pos = strrchr(parent_devpath, '/');
287 if (pos == NULL || pos == parent_devpath)
291 /* are we at the top level of /devices */
292 if (strcmp(parent_devpath, "/devices") == 0) {
293 dbg("/devices top level");
297 /* at the subsystems top level we want to follow the old-style "device" link */
298 if (strncmp(parent_devpath, "/subsystem", 10) == 0) {
299 pos = strrchr(parent_devpath, '/');
300 if (pos == &parent_devpath[10] || pos == parent_devpath || strcmp(pos, "/devices") == 0) {
301 dbg("/subsystem top level, look for device link");
305 if (strncmp(parent_devpath, "/class", 6) == 0) {
306 pos = strrchr(parent_devpath, '/');
307 if (pos == &parent_devpath[6] || pos == parent_devpath) {
308 dbg("/class top level, look for device link");
312 if (strcmp(parent_devpath, "/block") == 0) {
313 dbg("/block top level, look for device link");
317 /* get parent and remember it */
318 dev->parent = sysfs_device_get(parent_devpath);
322 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
323 strlcat(parent_devpath, "/device", sizeof(parent_devpath));
324 if (sysfs_resolve_link(parent_devpath, sizeof(parent_devpath)) != 0)
327 /* get parent and remember it */
328 dev->parent = sysfs_device_get(parent_devpath);
332 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
334 struct sysfs_device *dev_parent;
336 dev_parent = sysfs_device_get_parent(dev);
337 while (dev_parent != NULL) {
338 if (strcmp(dev_parent->subsystem, subsystem) == 0)
340 dev_parent = sysfs_device_get_parent(dev_parent);
345 void sysfs_device_put(struct sysfs_device *dev)
347 struct sysfs_dev *sysdev_loop;
349 list_for_each_entry(sysdev_loop, &sysfs_dev_list, node) {
350 if (&sysdev_loop->dev == dev) {
351 dbg("removed dev '%s' from cache",
352 sysdev_loop->dev.devpath);
353 list_del(&sysdev_loop->node);
358 dbg("dev '%s' not found in cache",
359 sysdev_loop->dev.devpath);
364 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
366 char path_full[PATH_SIZE];
368 char value[NAME_SIZE];
369 struct sysfs_attr *attr_loop;
370 struct sysfs_attr *attr = NULL;
376 dbg("open '%s'/'%s'", devpath, attr_name);
377 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
378 path = &path_full[sysfs_len];
379 strlcat(path_full, devpath, sizeof(path_full));
380 strlcat(path_full, "/", sizeof(path_full));
381 strlcat(path_full, attr_name, sizeof(path_full));
383 /* look for attribute in cache */
384 list_for_each_entry(attr_loop, &attr_list, node) {
385 if (strcmp(attr_loop->path, path) == 0) {
386 dbg("found in cache '%s'", attr_loop->path);
391 /* store attribute in cache */
392 dbg("new uncached attribute '%s'", path_full);
393 attr = malloc(sizeof(struct sysfs_attr));
396 memset(attr, 0x00, sizeof(struct sysfs_attr));
397 strlcpy(attr->path, path, sizeof(attr->path));
398 dbg("add to cache '%s'", path_full);
399 list_add(&attr->node, &attr_list);
401 /* clear old value */
403 memset(attr->value, 0x00, sizeof(attr->value));
406 if (lstat(path_full, &statbuf) != 0) {
407 dbg("stat '%s' failed: %s", path_full, strerror(errno));
411 if (S_ISLNK(statbuf.st_mode)) {
412 /* links return the last element of the target path */
413 char link_target[PATH_SIZE];
417 len = readlink(path_full, link_target, sizeof(link_target));
419 link_target[len] = '\0';
420 pos = strrchr(link_target, '/');
422 dbg("cache '%s' with link value '%s'", path_full, value);
423 strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
424 attr->value = attr->value_local;
430 /* skip directories */
431 if (S_ISDIR(statbuf.st_mode))
434 /* skip non-readable files */
435 if ((statbuf.st_mode & S_IRUSR) == 0)
438 /* read attribute value */
439 fd = open(path_full, O_RDONLY);
441 dbg("attribute '%s' does not exist", path_full);
444 size = read(fd, value, sizeof(value));
448 if (size == sizeof(value))
451 /* got a valid value, store and return it */
453 remove_trailing_chars(value, '\n');
454 dbg("cache '%s' with attribute value '%s'", path_full, value);
455 strlcpy(attr->value_local, value, sizeof(attr->value_local));
456 attr->value = attr->value_local;