chiark / gitweb /
0e448894fef8f3c6edee1a0cd49cdd631f4d2893
[elogind.git] / udev_sysfs.c
1 /*
2  * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
3  *
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.
7  * 
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.
12  * 
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.
16  *
17  */
18
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <sys/stat.h>
28
29 #include "udev.h"
30
31 char sysfs_path[PATH_SIZE];
32
33 /* device cache */
34 static LIST_HEAD(dev_list);
35
36 /* attribute value cache */
37 static LIST_HEAD(attr_list);
38 struct sysfs_attr {
39         struct list_head node;
40         char path[PATH_SIZE];
41         char *value;                    /* points to value_local if value is cached */
42         char value_local[NAME_SIZE];
43 };
44
45 int sysfs_init(void)
46 {
47         const char *env;
48
49         env = getenv("SYSFS_PATH");
50         if (env) {
51                 strlcpy(sysfs_path, env, sizeof(sysfs_path));
52                 remove_trailing_chars(sysfs_path, '/');
53         } else
54                 strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
55         dbg("sysfs_path='%s'", sysfs_path);
56
57         INIT_LIST_HEAD(&dev_list);
58         INIT_LIST_HEAD(&attr_list);
59         return 0;
60 }
61
62 void sysfs_cleanup(void)
63 {
64         struct sysfs_attr *attr_loop;
65         struct sysfs_attr *attr_temp;
66         struct sysfs_device *dev_loop;
67         struct sysfs_device *dev_temp;
68
69         list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
70                 list_del(&attr_loop->node);
71                 free(attr_loop);
72         }
73
74         list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
75                 list_del(&dev_loop->node);
76                 free(dev_loop);
77         }
78 }
79
80 void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath,
81                              const char *subsystem, const char *driver)
82 {
83         char *pos;
84
85         strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
86         if (subsystem != NULL)
87                 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
88         if (driver != NULL)
89                 strlcpy(dev->driver, driver, sizeof(dev->driver));
90
91         /* set kernel name */
92         pos = strrchr(dev->devpath, '/');
93         if (pos == NULL)
94                 return;
95
96         strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
97         dbg("kernel='%s'", dev->kernel);
98
99         /* some devices have '!' in their name, change that to '/' */
100         pos = dev->kernel;
101         while (pos[0] != '\0') {
102                 if (pos[0] == '!')
103                         pos[0] = '/';
104                 pos++;
105         }
106
107         /* get kernel number */
108         pos = &dev->kernel[strlen(dev->kernel)];
109         while (isdigit(pos[-1]))
110                 pos--;
111         strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
112         dbg("kernel_number='%s'", dev->kernel_number);
113 }
114
115 int sysfs_resolve_link(char *devpath, size_t size)
116 {
117         char link_path[PATH_SIZE];
118         char link_target[PATH_SIZE];
119         int len;
120         int i;
121         int back;
122
123         strlcpy(link_path, sysfs_path, sizeof(link_path));
124         strlcat(link_path, devpath, sizeof(link_path));
125         len = readlink(link_path, link_target, sizeof(link_target));
126         if (len <= 0)
127                 return -1;
128         link_target[len] = '\0';
129         dbg("path link '%s' points to '%s'", devpath, link_target);
130
131         for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
132                 ;
133         dbg("base '%s', tail '%s', back %i", devpath, &link_target[back * 3], back);
134         for (i = 0; i <= back; i++) {
135                 char *pos = strrchr(devpath, '/');
136
137                 if (pos == NULL)
138                         return -1;
139                 pos[0] = '\0';
140         }
141         dbg("after moving back '%s'", devpath);
142         strlcat(devpath, "/", size);
143         strlcat(devpath, &link_target[back * 3], size);
144         return 0;
145 }
146
147 struct sysfs_device *sysfs_device_get(const char *devpath)
148 {
149         char path[PATH_SIZE];
150         char devpath_real[PATH_SIZE];
151         struct sysfs_device *dev;
152         struct sysfs_device *dev_loop;
153         struct stat statbuf;
154         char link_path[PATH_SIZE];
155         char link_target[PATH_SIZE];
156         int len;
157         char *pos;
158
159         dbg("open '%s'", devpath);
160         strlcpy(devpath_real, devpath, sizeof(devpath_real));
161         remove_trailing_chars(devpath_real, '/');
162
163         /* look for device already in cache (we never put an untranslated path in the cache) */
164         list_for_each_entry(dev_loop, &dev_list, node) {
165                 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
166                         dbg("found in cache '%s'", dev_loop->devpath);
167                         return dev_loop;
168                 }
169         }
170
171         /* if we got a link, resolve it to the real device */
172         strlcpy(path, sysfs_path, sizeof(path));
173         strlcat(path, devpath_real, sizeof(path));
174         if (lstat(path, &statbuf) != 0) {
175                 dbg("stat '%s' failed: %s", path, strerror(errno));
176                 return NULL;
177         }
178         if (S_ISLNK(statbuf.st_mode)) {
179                 if (sysfs_resolve_link(devpath_real, sizeof(devpath_real)) != 0)
180                         return NULL;
181
182                 /* now look for device in cache after path translation */
183                 list_for_each_entry(dev_loop, &dev_list, node) {
184                         if (strcmp(dev_loop->devpath, devpath_real) == 0) {
185                                 dbg("found in cache '%s'", dev_loop->devpath);
186                                 return dev_loop;
187                         }
188                 }
189         }
190
191         /* it is a new device */
192         dbg("new uncached device '%s'", devpath_real);
193         dev = malloc(sizeof(struct sysfs_device));
194         if (dev == NULL)
195                 return NULL;
196         memset(dev, 0x00, sizeof(struct sysfs_device));
197
198         sysfs_device_set_values(dev, devpath_real, NULL, NULL);
199
200         /* get subsystem */
201         if (strncmp(dev->devpath, "/class/", 7) == 0) {
202                 strlcpy(dev->subsystem, &dev->devpath[7], sizeof(dev->subsystem));
203                 pos = strchr(dev->subsystem, '/');
204                 if (pos != NULL)
205                         pos[0] = '\0';
206                 else
207                         dev->subsystem[0] = '\0';
208         } else if (strncmp(dev->devpath, "/block/", 7) == 0) {
209                 strlcpy(dev->subsystem, "block", sizeof(dev->subsystem));
210         } else if (strncmp(dev->devpath, "/devices/", 9) == 0) {
211                 /* get subsystem from "bus" link */
212                 strlcpy(link_path, sysfs_path, sizeof(link_path));
213                 strlcat(link_path, dev->devpath, sizeof(link_path));
214                 strlcat(link_path, "/bus", sizeof(link_path));
215                 len = readlink(link_path, link_target, sizeof(link_target));
216                 if (len > 0) {
217                         link_target[len] = '\0';
218                         dbg("bus link '%s' points to '%s'", link_path, link_target);
219                         pos = strrchr(link_target, '/');
220                         if (pos != NULL)
221                                 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
222                 } else {
223                         /* get subsystem from "subsystem" link */
224                         strlcpy(link_path, sysfs_path, sizeof(link_path));
225                         strlcat(link_path, dev->devpath, sizeof(link_path));
226                         strlcat(link_path, "/subsystem", sizeof(link_path));
227                         len = readlink(link_path, link_target, sizeof(link_target));
228                         if (len > 0) {
229                                 link_target[len] = '\0';
230                                 dbg("subsystem link '%s' points to '%s'", link_path, link_target);
231                                 pos = strrchr(link_target, '/');
232                                 if (pos != NULL)
233                                         strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
234                         }
235                 }
236                 /* get driver name */
237                 strlcpy(link_path, sysfs_path, sizeof(link_path));
238                 strlcat(link_path, dev->devpath, sizeof(link_path));
239                 strlcat(link_path, "/driver", sizeof(link_path));
240                 len = readlink(link_path, link_target, sizeof(link_target));
241                 if (len > 0) {
242                         link_target[len] = '\0';
243                         dbg("driver link '%s' points to '%s'", link_path, link_target);
244                         pos = strrchr(link_target, '/');
245                         if (pos != NULL)
246                                 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
247                 }
248         } else if (strncmp(dev->devpath, "/bus/", 5) == 0 && strstr(dev->devpath, "/drivers/")) {
249                 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
250         } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
251                 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
252         }
253
254         dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'", dev->devpath, dev->subsystem, dev->driver);
255         list_add(&dev->node, &dev_list);
256
257         return dev;
258 }
259
260 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
261 {
262         char parent_devpath[PATH_SIZE];
263         char *pos;
264
265         dbg("open '%s'", dev->devpath);
266
267         /* look if we already know the parent */
268         if (dev->parent != NULL)
269                 return dev->parent;
270
271         /* requesting a parent is only valid for devices */
272         if ((strncmp(dev->devpath, "/devices/", 9) != 0) &&
273             (strncmp(dev->devpath, "/class/", 7) != 0) &&
274             (strncmp(dev->devpath, "/block/", 7) != 0))
275                 return NULL;
276
277         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
278         dbg("'%s'", parent_devpath);
279
280         /* strip last element */
281         pos = strrchr(parent_devpath, '/');
282         if (pos == NULL || pos == parent_devpath)
283                 return NULL;
284         pos[0] = '\0';
285
286         /* are we at the top level of /devices */
287         if (strcmp(parent_devpath, "/devices") == 0) {
288                 dbg("/devices top level");
289                 return NULL;
290         }
291
292         /* at the top level of class/block we want to follow the "device" link */
293         if (strcmp(parent_devpath, "/block") == 0) {
294                 dbg("/block top level, look for device link");
295                 goto device_link;
296         }
297         if (strncmp(parent_devpath, "/class", 6) == 0) {
298                 pos = strrchr(parent_devpath, '/');
299                 if (pos == &parent_devpath[6] || pos == parent_devpath) {
300                         dbg("/class top level, look for device link");
301                         goto device_link;
302                 }
303         }
304         /* get parent and remember it */
305         dev->parent = sysfs_device_get(parent_devpath);
306         return dev->parent;
307
308 device_link:
309         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
310         strlcat(parent_devpath, "/device", sizeof(parent_devpath));
311         if (sysfs_resolve_link(parent_devpath, sizeof(parent_devpath)) != 0)
312                 return NULL;
313
314         /* get parent and remember it */
315         dev->parent = sysfs_device_get(parent_devpath);
316         return dev->parent;
317 }
318
319 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
320 {
321         struct sysfs_device *dev_parent;
322
323         dev_parent = sysfs_device_get_parent(dev);
324         while (dev_parent != NULL) {
325                 if (strcmp(dev_parent->subsystem, subsystem) == 0)
326                         return dev_parent;
327                 dev_parent = sysfs_device_get_parent(dev_parent);
328         }
329         return NULL;
330 }
331
332 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
333 {
334         char path_full[PATH_SIZE];
335         const char *path;
336         char value[NAME_SIZE];
337         struct sysfs_attr *attr_loop;
338         struct sysfs_attr *attr;
339         struct stat statbuf;
340         int fd;
341         ssize_t size;
342         size_t sysfs_len;
343
344         dbg("open '%s'/'%s'", devpath, attr_name);
345         sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
346         path = &path_full[sysfs_len];
347         strlcat(path_full, devpath, sizeof(path_full));
348         strlcat(path_full, "/", sizeof(path_full));
349         strlcat(path_full, attr_name, sizeof(path_full));
350
351         /* look for attribute in cache */
352         list_for_each_entry(attr_loop, &attr_list, node) {
353                 if (strcmp(attr_loop->path, path) == 0) {
354                         dbg("found in cache '%s'", attr_loop->path);
355                         return attr_loop->value;
356                 }
357         }
358
359         /* store attribute in cache (also negatives are kept in cache) */
360         dbg("new uncached attribute '%s'", path_full);
361         attr = malloc(sizeof(struct sysfs_attr));
362         if (attr == NULL)
363                 return NULL;
364         memset(attr, 0x00, sizeof(struct sysfs_attr));
365         strlcpy(attr->path, path, sizeof(attr->path));
366         dbg("add to cache '%s'", path_full);
367         list_add(&attr->node, &attr_list);
368
369         if (lstat(path_full, &statbuf) != 0) {
370                 dbg("stat '%s' failed: %s", path_full, strerror(errno));
371                 goto out;
372         }
373
374         if (S_ISLNK(statbuf.st_mode)) {
375                 /* links return the last element of the target path */
376                 char link_target[PATH_SIZE];
377                 int len;
378                 const char *pos;
379
380                 len = readlink(path_full, link_target, sizeof(link_target));
381                 if (len > 0) {
382                         link_target[len] = '\0';
383                         pos = strrchr(link_target, '/');
384                         if (pos != NULL) {
385                                 dbg("cache '%s' with link value '%s'", path_full, value);
386                                 strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
387                                 attr->value = attr->value_local;
388                         }
389                 }
390                 goto out;
391         }
392
393         /* skip directories */
394         if (S_ISDIR(statbuf.st_mode))
395                 goto out;
396
397         /* skip non-readable files */
398         if ((statbuf.st_mode & S_IRUSR) == 0)
399                 goto out;
400
401         /* read attribute value */
402         fd = open(path_full, O_RDONLY);
403         if (fd < 0) {
404                 dbg("attribute '%s' does not exist", path_full);
405                 goto out;
406         }
407         size = read(fd, value, sizeof(value));
408         close(fd);
409         if (size < 0)
410                 goto out;
411         if (size == sizeof(value))
412                 goto out;
413
414         /* got a valid value, store and return it */
415         value[size] = '\0';
416         remove_trailing_chars(value, '\n');
417         dbg("cache '%s' with attribute value '%s'", path_full, value);
418         strlcpy(attr->value_local, value, sizeof(attr->value_local));
419         attr->value = attr->value_local;
420
421 out:
422         return attr->value;
423 }