chiark / gitweb /
udevinfo: relax check for the correct device if looked up by name
[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         strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
96         dbg("kernel='%s'", dev->kernel);
97
98         /* some devices have '!' in their name, change that to '/' */
99         pos = dev->kernel;
100         while (pos[0] != '\0') {
101                 if (pos[0] == '!')
102                         pos[0] = '/';
103                 pos++;
104         }
105
106         /* get kernel number */
107         pos = &dev->kernel[strlen(dev->kernel)];
108         while (isdigit(pos[-1]))
109                 pos--;
110         strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
111         dbg("kernel_number='%s'", dev->kernel_number);
112 }
113
114 int sysfs_resolve_link(char *devpath, size_t size)
115 {
116         char link_path[PATH_SIZE];
117         char link_target[PATH_SIZE];
118         int len;
119         int i;
120         int back;
121
122         strlcpy(link_path, sysfs_path, sizeof(link_path));
123         strlcat(link_path, devpath, sizeof(link_path));
124         len = readlink(link_path, link_target, sizeof(link_target));
125         if (len <= 0)
126                 return -1;
127         link_target[len] = '\0';
128         dbg("path link '%s' points to '%s'", devpath, link_target);
129
130         for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
131                 ;
132         dbg("base '%s', tail '%s', back %i", devpath, &link_target[back * 3], back);
133         for (i = 0; i <= back; i++) {
134                 char *pos = strrchr(devpath, '/');
135
136                 if (pos == NULL)
137                         return -1;
138                 pos[0] = '\0';
139         }
140         dbg("after moving back '%s'", devpath);
141         strlcat(devpath, "/", size);
142         strlcat(devpath, &link_target[back * 3], size);
143         return 0;
144 }
145
146 struct sysfs_device *sysfs_device_get(const char *devpath)
147 {
148         char path[PATH_SIZE];
149         char devpath_real[PATH_SIZE];
150         struct sysfs_device *dev;
151         struct sysfs_device *dev_loop;
152         struct stat statbuf;
153         char link_path[PATH_SIZE];
154         char link_target[PATH_SIZE];
155         int len;
156         char *pos;
157
158         dbg("open '%s'", devpath);
159         strlcpy(devpath_real, devpath, sizeof(devpath_real));
160         remove_trailing_chars(devpath_real, '/');
161
162         /* look for device already in cache (we never put an untranslated path in the cache) */
163         list_for_each_entry(dev_loop, &dev_list, node) {
164                 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
165                         dbg("found in cache '%s'", dev_loop->devpath);
166                         return dev_loop;
167                 }
168         }
169
170         /* if we got a link, resolve it to the real device */
171         strlcpy(path, sysfs_path, sizeof(path));
172         strlcat(path, devpath_real, sizeof(path));
173         if (lstat(path, &statbuf) != 0) {
174                 dbg("stat '%s' failed: %s", path, strerror(errno));
175                 return NULL;
176         }
177         if (S_ISLNK(statbuf.st_mode)) {
178                 if (sysfs_resolve_link(devpath_real, sizeof(devpath_real)) != 0)
179                         return NULL;
180
181                 /* now look for device in cache after path translation */
182                 list_for_each_entry(dev_loop, &dev_list, node) {
183                         if (strcmp(dev_loop->devpath, devpath_real) == 0) {
184                                 dbg("found in cache '%s'", dev_loop->devpath);
185                                 return dev_loop;
186                         }
187                 }
188         }
189
190         /* it is a new device */
191         dbg("new uncached device '%s'", devpath_real);
192         dev = malloc(sizeof(struct sysfs_device));
193         if (dev == NULL)
194                 return NULL;
195         memset(dev, 0x00, sizeof(struct sysfs_device));
196
197         sysfs_device_set_values(dev, devpath_real, NULL, NULL);
198
199         /* get subsystem name */
200         strlcpy(link_path, sysfs_path, sizeof(link_path));
201         strlcat(link_path, dev->devpath, sizeof(link_path));
202         strlcat(link_path, "/subsystem", sizeof(link_path));
203         len = readlink(link_path, link_target, sizeof(link_target));
204         if (len > 0) {
205                 /* get subsystem from "subsystem" link */
206                 link_target[len] = '\0';
207                 dbg("subsystem link '%s' points to '%s'", link_path, link_target);
208                 pos = strrchr(link_target, '/');
209                 if (pos != NULL)
210                         strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
211         } else if (strncmp(dev->devpath, "/class/", 7) == 0) {
212                 /* get subsystem from class dir */
213                 strlcpy(dev->subsystem, &dev->devpath[7], sizeof(dev->subsystem));
214                 pos = strchr(dev->subsystem, '/');
215                 if (pos != NULL)
216                         pos[0] = '\0';
217                 else
218                         dev->subsystem[0] = '\0';
219         } else if (strncmp(dev->devpath, "/block/", 7) == 0) {
220                 strlcpy(dev->subsystem, "block", sizeof(dev->subsystem));
221         } else if (strncmp(dev->devpath, "/devices/", 9) == 0) {
222                 /* get subsystem from "bus" link */
223                 strlcpy(link_path, sysfs_path, sizeof(link_path));
224                 strlcat(link_path, dev->devpath, sizeof(link_path));
225                 strlcat(link_path, "/bus", sizeof(link_path));
226                 len = readlink(link_path, link_target, sizeof(link_target));
227                 if (len > 0) {
228                         link_target[len] = '\0';
229                         dbg("bus link '%s' points to '%s'", link_path, link_target);
230                         pos = strrchr(link_target, '/');
231                         if (pos != NULL)
232                                 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
233                 }
234         } else if (strstr(dev->devpath, "/drivers/") != NULL) {
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));
238         }
239
240         /* get driver name */
241         strlcpy(link_path, sysfs_path, sizeof(link_path));
242         strlcat(link_path, dev->devpath, sizeof(link_path));
243         strlcat(link_path, "/driver", sizeof(link_path));
244         len = readlink(link_path, link_target, sizeof(link_target));
245         if (len > 0) {
246                 link_target[len] = '\0';
247                 dbg("driver link '%s' points to '%s'", link_path, link_target);
248                 pos = strrchr(link_target, '/');
249                 if (pos != NULL)
250                         strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
251         }
252
253         dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'", dev->devpath, dev->subsystem, dev->driver);
254         list_add(&dev->node, &dev_list);
255
256         return dev;
257 }
258
259 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
260 {
261         char parent_devpath[PATH_SIZE];
262         char *pos;
263
264         dbg("open '%s'", dev->devpath);
265
266         /* look if we already know the parent */
267         if (dev->parent != NULL)
268                 return dev->parent;
269
270         /* requesting a parent is only valid for devices */
271         if ((strncmp(dev->devpath, "/devices/", 9) != 0) &&
272             (strncmp(dev->devpath, "/subsystem/", 11) != 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 subsystems top level we want to follow the old-style "device" link */
293         if (strncmp(parent_devpath, "/subsystem", 10) == 0) {
294                 pos = strrchr(parent_devpath, '/');
295                 if (pos == &parent_devpath[10] || pos == parent_devpath || strcmp(pos, "/devices") == 0) {
296                         dbg("/subsystem top level, look for device link");
297                         goto device_link;
298                 }
299         }
300         if (strncmp(parent_devpath, "/class", 6) == 0) {
301                 pos = strrchr(parent_devpath, '/');
302                 if (pos == &parent_devpath[6] || pos == parent_devpath) {
303                         dbg("/class top level, look for device link");
304                         goto device_link;
305                 }
306         }
307         if (strcmp(parent_devpath, "/block") == 0) {
308                 dbg("/block top level, look for device link");
309                 goto device_link;
310         }
311
312         /* get parent and remember it */
313         dev->parent = sysfs_device_get(parent_devpath);
314         return dev->parent;
315
316 device_link:
317         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
318         strlcat(parent_devpath, "/device", sizeof(parent_devpath));
319         if (sysfs_resolve_link(parent_devpath, sizeof(parent_devpath)) != 0)
320                 return NULL;
321
322         /* get parent and remember it */
323         dev->parent = sysfs_device_get(parent_devpath);
324         return dev->parent;
325 }
326
327 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
328 {
329         struct sysfs_device *dev_parent;
330
331         dev_parent = sysfs_device_get_parent(dev);
332         while (dev_parent != NULL) {
333                 if (strcmp(dev_parent->subsystem, subsystem) == 0)
334                         return dev_parent;
335                 dev_parent = sysfs_device_get_parent(dev_parent);
336         }
337         return NULL;
338 }
339
340 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
341 {
342         char path_full[PATH_SIZE];
343         const char *path;
344         char value[NAME_SIZE];
345         struct sysfs_attr *attr_loop;
346         struct sysfs_attr *attr;
347         struct stat statbuf;
348         int fd;
349         ssize_t size;
350         size_t sysfs_len;
351
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));
358
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;
364                 }
365         }
366
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));
370         if (attr == NULL)
371                 return NULL;
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);
376
377         if (lstat(path_full, &statbuf) != 0) {
378                 dbg("stat '%s' failed: %s", path_full, strerror(errno));
379                 goto out;
380         }
381
382         if (S_ISLNK(statbuf.st_mode)) {
383                 /* links return the last element of the target path */
384                 char link_target[PATH_SIZE];
385                 int len;
386                 const char *pos;
387
388                 len = readlink(path_full, link_target, sizeof(link_target));
389                 if (len > 0) {
390                         link_target[len] = '\0';
391                         pos = strrchr(link_target, '/');
392                         if (pos != NULL) {
393                                 dbg("cache '%s' with link value '%s'", path_full, value);
394                                 strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
395                                 attr->value = attr->value_local;
396                         }
397                 }
398                 goto out;
399         }
400
401         /* skip directories */
402         if (S_ISDIR(statbuf.st_mode))
403                 goto out;
404
405         /* skip non-readable files */
406         if ((statbuf.st_mode & S_IRUSR) == 0)
407                 goto out;
408
409         /* read attribute value */
410         fd = open(path_full, O_RDONLY);
411         if (fd < 0) {
412                 dbg("attribute '%s' does not exist", path_full);
413                 goto out;
414         }
415         size = read(fd, value, sizeof(value));
416         close(fd);
417         if (size < 0)
418                 goto out;
419         if (size == sizeof(value))
420                 goto out;
421
422         /* got a valid value, store and return it */
423         value[size] = '\0';
424         remove_trailing_chars(value, '\n');
425         dbg("cache '%s' with attribute value '%s'", path_full, value);
426         strlcpy(attr->value_local, value, sizeof(attr->value_local));
427         attr->value = attr->value_local;
428
429 out:
430         return attr->value;
431 }