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("new uncached device '%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 dbg("open '%s'", dev->devpath);
258 /* look if we already know the parent */
259 if (dev->parent != NULL)
262 /* requesting a parent is only valid for devices */
263 if ((strncmp(dev->devpath, "/devices/", 9) != 0) &&
264 (strncmp(dev->devpath, "/class/", 7) != 0) &&
265 (strncmp(dev->devpath, "/block/", 7) != 0))
268 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
269 dbg("'%s'", parent_devpath);
271 /* strip last element */
272 pos = strrchr(parent_devpath, '/');
273 if (pos == NULL || pos == parent_devpath)
277 /* are we at the top level of /devices */
278 if (strcmp(parent_devpath, "/devices") == 0) {
279 dbg("/devices top level");
283 /* at the top level of class/block we want to follow the "device" link */
284 if (strcmp(parent_devpath, "/block") == 0) {
285 dbg("/block top level, look for device link");
288 if (strncmp(parent_devpath, "/class", 6) == 0) {
289 pos = strrchr(parent_devpath, '/');
290 if (pos == &parent_devpath[6] || pos == parent_devpath) {
291 dbg("/class top level, look for device link");
295 /* get parent and remember it */
296 dev->parent = sysfs_device_get(parent_devpath);
300 strlcpy(device_link, sysfs_path, sizeof(device_link));
301 strlcat(device_link, dev->devpath, sizeof(device_link));
302 strlcat(device_link, "/device", sizeof(device_link));
303 len = readlink(device_link, device_link_target, sizeof(device_link_target));
306 device_link_target[len] = '\0';
307 dbg("device link '%s' points to '%s'", device_link, device_link_target);
309 for (back = 0; strncmp(&device_link_target[back * 3], "../", 3) == 0; back++)
311 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
312 dbg("base='%s', tail='%s', back=%i", parent_devpath, &device_link_target[back * 3], back);
313 for (i = 0; i < back; i++) {
314 pos = strrchr(parent_devpath, '/');
319 dbg("after moving back '%s'", parent_devpath);
320 strlcat(parent_devpath, "/", sizeof(parent_devpath));
321 strlcat(parent_devpath, &device_link_target[back * 3], sizeof(parent_devpath));
323 /* get parent and remember it */
324 dev->parent = sysfs_device_get(parent_devpath);
328 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
330 struct sysfs_device *dev_parent;
332 dev_parent = sysfs_device_get_parent(dev);
333 while (dev_parent != NULL) {
334 if (strcmp(dev_parent->subsystem, subsystem) == 0)
336 dev_parent = sysfs_device_get_parent(dev_parent);
341 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
343 char path_full[PATH_SIZE];
345 char value[NAME_SIZE];
346 struct sysfs_attr *attr_loop;
347 struct sysfs_attr *attr;
352 dbg("open '%s'/'%s'", devpath, attr_name);
353 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
354 path = &path_full[sysfs_len];
355 strlcat(path_full, devpath, sizeof(path_full));
356 strlcat(path_full, "/", sizeof(path_full));
357 strlcat(path_full, attr_name, sizeof(path_full));
359 /* look for attribute in cache */
360 list_for_each_entry(attr_loop, &attr_list, node) {
361 if (strcmp(attr_loop->path, path) == 0) {
362 dbg("found in cache '%s'", attr_loop->path);
363 return attr_loop->value;
367 /* store attribute in cache (also negatives are kept in cache) */
368 dbg("new uncached attribute '%s'", path_full);
369 attr = malloc(sizeof(struct sysfs_attr));
372 memset(attr, 0x00, sizeof(struct sysfs_attr));
373 strlcpy(attr->path, path, sizeof(attr->path));
374 dbg("add to cache '%s'", path_full);
375 list_add(&attr->node, &attr_list);
377 /* read attribute value */
378 fd = open(path_full, O_RDONLY);
380 dbg("attribute '%s' does not exist", path_full);
383 size = read(fd, value, sizeof(value));
387 if (size == sizeof(value))
390 /* got a valid value, store and return it */
392 remove_trailing_chars(value, '\n');
393 dbg("cache '%s' with value '%s'", path_full, value);
394 strlcpy(attr->value_local, value, sizeof(attr->value_local));
395 attr->value = attr->value_local;