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.
32 char sysfs_path[PATH_SIZE];
35 static LIST_HEAD(dev_list);
37 /* attribute value cache */
38 static LIST_HEAD(attr_list);
40 struct list_head node;
42 char value[NAME_SIZE];
49 env = getenv("SYSFS_PATH");
51 strlcpy(sysfs_path, env, sizeof(sysfs_path));
52 remove_trailing_chars(sysfs_path, '/');
54 strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
55 dbg("sysfs_path='%s'", sysfs_path);
57 INIT_LIST_HEAD(&dev_list);
58 INIT_LIST_HEAD(&attr_list);
62 void sysfs_cleanup(void)
64 struct sysfs_attr *attr_loop;
65 struct sysfs_attr *attr_temp;
66 struct sysfs_device *dev_loop;
67 struct sysfs_device *dev_temp;
69 list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
70 list_del(&attr_loop->node);
74 list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
75 list_del(&dev_loop->node);
80 void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath, const char *subsystem)
84 strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
85 if (subsystem != NULL)
86 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
89 pos = strrchr(dev->devpath, '/');
93 strlcpy(dev->kernel_name, &pos[1], sizeof(dev->kernel_name));
94 dbg("kernel_name='%s'", dev->kernel_name);
96 /* some devices have '!' in their name, change that to '/' */
97 pos = dev->kernel_name;
98 while (pos[0] != '\0') {
104 /* get kernel number */
105 pos = &dev->kernel_name[strlen(dev->kernel_name)];
106 while (isdigit(pos[-1]))
108 strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
109 dbg("kernel_number='%s'", dev->kernel_number);
112 struct sysfs_device *sysfs_device_get(const char *devpath)
114 char path[PATH_SIZE];
115 char devpath_real[PATH_SIZE];
116 struct sysfs_device *dev;
117 struct sysfs_device *dev_loop;
119 char link_path[PATH_SIZE];
120 char link_target[PATH_SIZE];
124 dbg("open '%s'", devpath);
125 strlcpy(devpath_real, devpath, sizeof(devpath_real));
126 remove_trailing_chars(devpath_real, '/');
128 strlcpy(path, sysfs_path, sizeof(path));
129 strlcat(path, devpath_real, sizeof(path));
130 if (lstat(path, &statbuf) != 0) {
131 dbg("stat '%s' failed: %s", devpath, strerror(errno));
135 /* if we got a link, resolve it to the real device */
136 if (S_ISLNK(statbuf.st_mode)) {
140 len = readlink(path, link_target, sizeof(link_target));
143 link_target[len] = '\0';
144 dbg("devpath link '%s' points to '%s'", path, link_target);
146 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
148 dbg("base '%s', tail '%s', back %i", devpath_real, &link_target[back * 3], back);
149 for (i = 0; i <= back; i++) {
150 pos = strrchr(devpath_real, '/');
155 dbg("after moving back '%s'", devpath_real);
156 strlcat(devpath_real, "/", sizeof(devpath_real));
157 strlcat(devpath_real, &link_target[back * 3], sizeof(devpath_real));
160 /* look for device in cache */
161 list_for_each_entry(dev_loop, &dev_list, node) {
162 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
163 dbg("found in cache '%s'", dev_loop->devpath);
169 dbg("'%s'", devpath_real);
170 dev = malloc(sizeof(struct sysfs_device));
173 memset(dev, 0x00, sizeof(struct sysfs_device));
175 sysfs_device_set_values(dev, devpath_real, NULL);
178 if (strncmp(dev->devpath, "/class/", 7) == 0) {
179 strlcpy(dev->subsystem, &dev->devpath[7], sizeof(dev->subsystem));
180 pos = strchr(dev->subsystem, '/');
184 dev->subsystem[0] = '\0';
185 } else if (strncmp(devpath, "/block/", 7) == 0) {
186 strlcpy(dev->subsystem, "block", sizeof(dev->subsystem));
187 } else if (strncmp(devpath, "/devices/", 9) == 0) {
188 /* get subsystem from bus name */
189 strlcpy(link_path, sysfs_path, sizeof(link_path));
190 strlcat(link_path, dev->devpath, sizeof(link_path));
191 strlcat(link_path, "/bus", sizeof(link_path));
192 len = readlink(link_path, link_target, sizeof(link_target));
194 link_target[len] = '\0';
195 dbg("bus link '%s' points to '%s'", link_path, link_target);
196 pos = strrchr(link_target, '/');
198 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
201 /* get driver name */
202 strlcpy(link_path, sysfs_path, sizeof(link_path));
203 strlcat(link_path, dev->devpath, sizeof(link_path));
204 strlcat(link_path, "/driver", sizeof(link_path));
205 len = readlink(link_path, link_target, sizeof(link_target));
207 link_target[len] = '\0';
208 dbg("driver link '%s' points to '%s'", link_path, link_target);
209 pos = strrchr(link_target, '/');
211 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
213 } else if (strncmp(devpath, "/bus/", 5) == 0 && strstr(devpath, "/drivers/")) {
214 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
215 } else if (strncmp(devpath, "/module/", 8) == 0) {
216 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
219 dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'", dev->devpath, dev->subsystem, dev->driver);
220 list_add(&dev->node, &dev_list);
225 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
227 char parent_devpath[PATH_SIZE];
228 char device_link[PATH_SIZE];
229 char device_link_target[PATH_SIZE];
235 /* requesting a parent is only valid for devices */
236 if ((strncmp(dev->devpath, "/devices/", 9) != 0) &&
237 (strncmp(dev->devpath, "/class/", 7) != 0) &&
238 (strncmp(dev->devpath, "/block/", 7) != 0))
241 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
242 dbg("'%s'", parent_devpath);
244 /* strip last element */
245 pos = strrchr(parent_devpath, '/');
246 if (pos == NULL || pos == parent_devpath)
250 /* are we at the top level */
251 if (strcmp(parent_devpath, "/devices") == 0) {
252 dbg("/devices top level");
256 /* at the top level we may follow the "device" link */
257 if (strcmp(parent_devpath, "/block") == 0) {
258 dbg("/block top level, look for device link");
262 if (strncmp(parent_devpath, "/class", 6) == 0) {
263 pos = strrchr(parent_devpath, '/');
264 if (pos == &parent_devpath[6] || pos == parent_devpath) {
265 dbg("class top level, look for device link");
269 return sysfs_device_get(parent_devpath);
272 strlcpy(device_link, sysfs_path, sizeof(device_link));
273 strlcat(device_link, dev->devpath, sizeof(device_link));
274 strlcat(device_link, "/device", sizeof(device_link));
275 len = readlink(device_link, device_link_target, sizeof(device_link_target));
278 device_link_target[len] = '\0';
279 dbg("device link '%s' points to '%s'", device_link, device_link_target);
281 for (back = 0; strncmp(&device_link_target[back * 3], "../", 3) == 0; back++)
283 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
284 dbg("base='%s', tail='%s', back=%i", parent_devpath, &device_link_target[back * 3], back);
285 for (i = 0; i < back; i++) {
286 pos = strrchr(parent_devpath, '/');
291 dbg("after moving back '%s'", parent_devpath);
292 strlcat(parent_devpath, "/", sizeof(parent_devpath));
293 strlcat(parent_devpath, &device_link_target[back * 3], sizeof(parent_devpath));
294 return sysfs_device_get(parent_devpath);
297 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
299 char path_full[PATH_SIZE];
301 char value[NAME_SIZE];
302 struct sysfs_attr *attr_loop;
303 struct sysfs_attr *attr;
308 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
309 path = &path_full[sysfs_len];
310 strlcat(path_full, devpath, sizeof(path_full));
311 strlcat(path_full, "/", sizeof(path_full));
312 strlcat(path_full, attr_name, sizeof(path_full));
314 /* look for attribute in cache */
315 list_for_each_entry(attr_loop, &attr_list, node) {
316 if (strcmp(attr_loop->path, path) == 0) {
317 dbg("found in cache '%s'", attr_loop->path);
318 return attr_loop->value;
322 /* read attribute value */
323 fd = open(path_full, O_RDONLY);
326 size = read(fd, value, sizeof(value));
330 if (size == sizeof(value))
333 remove_trailing_chars(value, '\n');
335 /* store attribute in cache */
336 attr = malloc(sizeof(struct sysfs_attr));
339 strlcpy(attr->path, path, sizeof(attr->path));
340 strlcpy(attr->value, value, sizeof(attr->value));
341 dbg("add to cache '%s' '%s'", attr->path, attr->value);
342 list_add(&attr->node, &attr_list);