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[NAME_SIZE];
50 env = getenv("SYSFS_PATH");
52 strlcpy(sysfs_path, env, sizeof(sysfs_path));
53 remove_trailing_chars(sysfs_path, '/');
55 strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
56 dbg("sysfs_path='%s'", sysfs_path);
58 INIT_LIST_HEAD(&dev_list);
59 INIT_LIST_HEAD(&attr_list);
63 void sysfs_cleanup(void)
65 struct sysfs_attr *attr_loop;
66 struct sysfs_attr *attr_temp;
67 struct sysfs_device *dev_loop;
68 struct sysfs_device *dev_temp;
70 list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
71 list_del(&attr_loop->node);
75 list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
76 list_del(&dev_loop->node);
81 void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath, const char *subsystem)
85 strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
86 if (subsystem != NULL)
87 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
90 pos = strrchr(dev->devpath, '/');
94 strlcpy(dev->kernel_name, &pos[1], sizeof(dev->kernel_name));
95 dbg("kernel_name='%s'", dev->kernel_name);
97 /* some devices have '!' in their name, change that to '/' */
98 pos = dev->kernel_name;
99 while (pos[0] != '\0') {
105 /* get kernel number */
106 pos = &dev->kernel_name[strlen(dev->kernel_name)];
107 while (isdigit(pos[-1]))
109 strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
110 dbg("kernel_number='%s'", dev->kernel_number);
113 struct sysfs_device *sysfs_device_get(const char *devpath)
115 char path[PATH_SIZE];
116 char devpath_real[PATH_SIZE];
117 struct sysfs_device *dev;
118 struct sysfs_device *dev_loop;
120 char link_path[PATH_SIZE];
121 char link_target[PATH_SIZE];
125 dbg("open '%s'", devpath);
126 strlcpy(devpath_real, devpath, sizeof(devpath_real));
127 remove_trailing_chars(devpath_real, '/');
129 strlcpy(path, sysfs_path, sizeof(path));
130 strlcat(path, devpath_real, sizeof(path));
131 if (lstat(path, &statbuf) != 0) {
132 dbg("stat '%s' failed: %s", path, strerror(errno));
136 /* if we got a link, resolve it to the real device */
137 if (S_ISLNK(statbuf.st_mode)) {
141 len = readlink(path, link_target, sizeof(link_target));
144 link_target[len] = '\0';
145 dbg("devpath link '%s' points to '%s'", path, link_target);
147 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
149 dbg("base '%s', tail '%s', back %i", devpath_real, &link_target[back * 3], back);
150 for (i = 0; i <= back; i++) {
151 pos = strrchr(devpath_real, '/');
156 dbg("after moving back '%s'", devpath_real);
157 strlcat(devpath_real, "/", sizeof(devpath_real));
158 strlcat(devpath_real, &link_target[back * 3], sizeof(devpath_real));
161 /* look for device in cache */
162 list_for_each_entry(dev_loop, &dev_list, node) {
163 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
164 dbg("found in cache '%s'", dev_loop->devpath);
170 dbg("'%s'", devpath_real);
171 dev = malloc(sizeof(struct sysfs_device));
174 memset(dev, 0x00, sizeof(struct sysfs_device));
176 sysfs_device_set_values(dev, devpath_real, NULL);
179 if (strncmp(dev->devpath, "/class/", 7) == 0) {
180 strlcpy(dev->subsystem, &dev->devpath[7], sizeof(dev->subsystem));
181 pos = strchr(dev->subsystem, '/');
185 dev->subsystem[0] = '\0';
186 } else if (strncmp(dev->devpath, "/block/", 7) == 0) {
187 strlcpy(dev->subsystem, "block", sizeof(dev->subsystem));
188 } else if (strncmp(dev->devpath, "/devices/", 9) == 0) {
189 /* get subsystem from "bus" link */
190 strlcpy(link_path, sysfs_path, sizeof(link_path));
191 strlcat(link_path, dev->devpath, sizeof(link_path));
192 strlcat(link_path, "/bus", sizeof(link_path));
193 len = readlink(link_path, link_target, sizeof(link_target));
195 link_target[len] = '\0';
196 dbg("bus link '%s' points to '%s'", link_path, link_target);
197 pos = strrchr(link_target, '/');
199 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
201 /* get subsystem from "subsystem" link */
202 strlcpy(link_path, sysfs_path, sizeof(link_path));
203 strlcat(link_path, dev->devpath, sizeof(link_path));
204 strlcat(link_path, "/subsystem", sizeof(link_path));
205 len = readlink(link_path, link_target, sizeof(link_target));
207 link_target[len] = '\0';
208 dbg("subsystem link '%s' points to '%s'", link_path, link_target);
209 pos = strrchr(link_target, '/');
211 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
214 /* get driver name */
215 strlcpy(link_path, sysfs_path, sizeof(link_path));
216 strlcat(link_path, dev->devpath, sizeof(link_path));
217 strlcat(link_path, "/driver", sizeof(link_path));
218 len = readlink(link_path, link_target, sizeof(link_target));
220 link_target[len] = '\0';
221 dbg("driver link '%s' points to '%s'", link_path, link_target);
222 pos = strrchr(link_target, '/');
224 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
226 } else if (strncmp(dev->devpath, "/bus/", 5) == 0 && strstr(dev->devpath, "/drivers/")) {
227 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
228 } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
229 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
232 dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'", dev->devpath, dev->subsystem, dev->driver);
233 list_add(&dev->node, &dev_list);
238 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
240 char parent_devpath[PATH_SIZE];
241 char device_link[PATH_SIZE];
242 char device_link_target[PATH_SIZE];
248 /* requesting a parent is only valid for devices */
249 if ((strncmp(dev->devpath, "/devices/", 9) != 0) &&
250 (strncmp(dev->devpath, "/class/", 7) != 0) &&
251 (strncmp(dev->devpath, "/block/", 7) != 0))
254 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
255 dbg("'%s'", parent_devpath);
257 /* strip last element */
258 pos = strrchr(parent_devpath, '/');
259 if (pos == NULL || pos == parent_devpath)
263 /* are we at the top level */
264 if (strcmp(parent_devpath, "/devices") == 0) {
265 dbg("/devices top level");
269 /* at the top level we may follow the "device" link */
270 if (strcmp(parent_devpath, "/block") == 0) {
271 dbg("/block top level, look for device link");
275 if (strncmp(parent_devpath, "/class", 6) == 0) {
276 pos = strrchr(parent_devpath, '/');
277 if (pos == &parent_devpath[6] || pos == parent_devpath) {
278 dbg("/class top level, look for device link");
282 return sysfs_device_get(parent_devpath);
285 strlcpy(device_link, sysfs_path, sizeof(device_link));
286 strlcat(device_link, dev->devpath, sizeof(device_link));
287 strlcat(device_link, "/device", sizeof(device_link));
288 len = readlink(device_link, device_link_target, sizeof(device_link_target));
291 device_link_target[len] = '\0';
292 dbg("device link '%s' points to '%s'", device_link, device_link_target);
294 for (back = 0; strncmp(&device_link_target[back * 3], "../", 3) == 0; back++)
296 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
297 dbg("base='%s', tail='%s', back=%i", parent_devpath, &device_link_target[back * 3], back);
298 for (i = 0; i < back; i++) {
299 pos = strrchr(parent_devpath, '/');
304 dbg("after moving back '%s'", parent_devpath);
305 strlcat(parent_devpath, "/", sizeof(parent_devpath));
306 strlcat(parent_devpath, &device_link_target[back * 3], sizeof(parent_devpath));
307 return sysfs_device_get(parent_devpath);
310 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
312 struct sysfs_device *dev_parent;
314 dev_parent = sysfs_device_get_parent(dev);
315 while (dev_parent != NULL) {
316 if (strcmp(dev_parent->subsystem, subsystem) == 0)
318 dev_parent = sysfs_device_get_parent(dev_parent);
323 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
325 char path_full[PATH_SIZE];
327 char value[NAME_SIZE];
328 struct sysfs_attr *attr_loop;
329 struct sysfs_attr *attr;
334 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
335 path = &path_full[sysfs_len];
336 strlcat(path_full, devpath, sizeof(path_full));
337 strlcat(path_full, "/", sizeof(path_full));
338 strlcat(path_full, attr_name, sizeof(path_full));
340 /* look for attribute in cache */
341 list_for_each_entry(attr_loop, &attr_list, node) {
342 if (strcmp(attr_loop->path, path) == 0) {
343 dbg("found in cache '%s'", attr_loop->path);
344 return attr_loop->value;
348 /* read attribute value */
349 fd = open(path_full, O_RDONLY);
352 size = read(fd, value, sizeof(value));
356 if (size == sizeof(value))
359 remove_trailing_chars(value, '\n');
361 /* store attribute in cache */
362 attr = malloc(sizeof(struct sysfs_attr));
365 strlcpy(attr->path, path, sizeof(attr->path));
366 strlcpy(attr->value, value, sizeof(attr->value));
367 dbg("add to cache '%s' '%s'", attr->path, attr->value);
368 list_add(&attr->node, &attr_list);