chiark / gitweb /
release 127
[elogind.git] / udev / 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 <string.h>
25 #include <fcntl.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <sys/stat.h>
29
30 #include "udev.h"
31
32 char sysfs_path[PATH_SIZE];
33
34 /* device cache */
35 static LIST_HEAD(dev_list);
36
37 /* attribute value cache */
38 static LIST_HEAD(attr_list);
39 struct sysfs_attr {
40         struct list_head node;
41         char path[PATH_SIZE];
42         char *value;                    /* points to value_local if value is cached */
43         char value_local[NAME_SIZE];
44 };
45
46 int sysfs_init(void)
47 {
48         const char *env;
49
50         env = getenv("SYSFS_PATH");
51         if (env) {
52                 strlcpy(sysfs_path, env, sizeof(sysfs_path));
53                 remove_trailing_chars(sysfs_path, '/');
54         } else
55                 strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
56         dbg("sysfs_path='%s'\n", sysfs_path);
57
58         INIT_LIST_HEAD(&dev_list);
59         INIT_LIST_HEAD(&attr_list);
60         return 0;
61 }
62
63 void sysfs_cleanup(void)
64 {
65         struct sysfs_attr *attr_loop;
66         struct sysfs_attr *attr_temp;
67         struct sysfs_device *dev_loop;
68         struct sysfs_device *dev_temp;
69
70         list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
71                 list_del(&attr_loop->node);
72                 free(attr_loop);
73         }
74
75         list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
76                 list_del(&dev_loop->node);
77                 free(dev_loop);
78         }
79 }
80
81 void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath,
82                              const char *subsystem, const char *driver)
83 {
84         char *pos;
85
86         strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
87         if (subsystem != NULL)
88                 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
89         if (driver != NULL)
90                 strlcpy(dev->driver, driver, sizeof(dev->driver));
91
92         /* set kernel name */
93         pos = strrchr(dev->devpath, '/');
94         if (pos == NULL)
95                 return;
96         strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
97         dbg("kernel='%s'\n", 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'\n", 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'\n", 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\n", 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'\n", 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         /* we handle only these devpathes */
160         if (devpath != NULL &&
161             strncmp(devpath, "/devices/", 9) != 0 &&
162             strncmp(devpath, "/subsystem/", 11) != 0 &&
163             strncmp(devpath, "/module/", 8) != 0 &&
164             strncmp(devpath, "/bus/", 5) != 0 &&
165             strncmp(devpath, "/class/", 7) != 0 &&
166             strncmp(devpath, "/block/", 7) != 0)
167                 return NULL;
168
169         dbg("open '%s'\n", devpath);
170         strlcpy(devpath_real, devpath, sizeof(devpath_real));
171         remove_trailing_chars(devpath_real, '/');
172         if (devpath[0] == '\0' )
173                 return NULL;
174
175         /* look for device already in cache (we never put an untranslated path in the cache) */
176         list_for_each_entry(dev_loop, &dev_list, node) {
177                 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
178                         dbg("found in cache '%s'\n", dev_loop->devpath);
179                         return dev_loop;
180                 }
181         }
182
183         /* if we got a link, resolve it to the real device */
184         strlcpy(path, sysfs_path, sizeof(path));
185         strlcat(path, devpath_real, sizeof(path));
186         if (lstat(path, &statbuf) != 0) {
187                 dbg("stat '%s' failed: %s\n", path, strerror(errno));
188                 return NULL;
189         }
190         if (S_ISLNK(statbuf.st_mode)) {
191                 if (sysfs_resolve_link(devpath_real, sizeof(devpath_real)) != 0)
192                         return NULL;
193
194                 /* now look for device in cache after path translation */
195                 list_for_each_entry(dev_loop, &dev_list, node) {
196                         if (strcmp(dev_loop->devpath, devpath_real) == 0) {
197                                 dbg("found in cache '%s'\n", dev_loop->devpath);
198                                 return dev_loop;
199                         }
200                 }
201         }
202
203         /* it is a new device */
204         dbg("new uncached device '%s'\n", devpath_real);
205         dev = malloc(sizeof(struct sysfs_device));
206         if (dev == NULL)
207                 return NULL;
208         memset(dev, 0x00, sizeof(struct sysfs_device));
209
210         sysfs_device_set_values(dev, devpath_real, NULL, NULL);
211
212         /* get subsystem name */
213         strlcpy(link_path, sysfs_path, sizeof(link_path));
214         strlcat(link_path, dev->devpath, sizeof(link_path));
215         strlcat(link_path, "/subsystem", sizeof(link_path));
216         len = readlink(link_path, link_target, sizeof(link_target));
217         if (len > 0) {
218                 /* get subsystem from "subsystem" link */
219                 link_target[len] = '\0';
220                 dbg("subsystem link '%s' points to '%s'\n", link_path, link_target);
221                 pos = strrchr(link_target, '/');
222                 if (pos != NULL)
223                         strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
224         } else if (strstr(dev->devpath, "/drivers/") != NULL) {
225                 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
226         } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
227                 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
228         } else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) {
229                 pos = strrchr(dev->devpath, '/');
230                 if (pos == &dev->devpath[10])
231                         strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
232         } else if (strncmp(dev->devpath, "/class/", 7) == 0) {
233                 pos = strrchr(dev->devpath, '/');
234                 if (pos == &dev->devpath[6])
235                         strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
236         } else if (strncmp(dev->devpath, "/bus/", 5) == 0) {
237                 pos = strrchr(dev->devpath, '/');
238                 if (pos == &dev->devpath[4])
239                         strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
240         }
241
242         /* get driver name */
243         strlcpy(link_path, sysfs_path, sizeof(link_path));
244         strlcat(link_path, dev->devpath, sizeof(link_path));
245         strlcat(link_path, "/driver", sizeof(link_path));
246         len = readlink(link_path, link_target, sizeof(link_target));
247         if (len > 0) {
248                 link_target[len] = '\0';
249                 dbg("driver link '%s' points to '%s'\n", link_path, link_target);
250                 pos = strrchr(link_target, '/');
251                 if (pos != NULL)
252                         strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
253         }
254
255         dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'\n", dev->devpath, dev->subsystem, dev->driver);
256         list_add(&dev->node, &dev_list);
257
258         return dev;
259 }
260
261 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
262 {
263         char parent_devpath[PATH_SIZE];
264         char *pos;
265
266         dbg("open '%s'\n", dev->devpath);
267
268         /* look if we already know the parent */
269         if (dev->parent != NULL)
270                 return dev->parent;
271
272         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
273         dbg("'%s'\n", parent_devpath);
274
275         /* strip last element */
276         pos = strrchr(parent_devpath, '/');
277         if (pos == NULL || pos == parent_devpath)
278                 return NULL;
279         pos[0] = '\0';
280
281         if (strncmp(parent_devpath, "/class", 6) == 0) {
282                 pos = strrchr(parent_devpath, '/');
283                 if (pos == &parent_devpath[6] || pos == parent_devpath) {
284                         dbg("/class top level, look for device link\n");
285                         goto device_link;
286                 }
287         }
288         if (strcmp(parent_devpath, "/block") == 0) {
289                 dbg("/block top level, look for device link\n");
290                 goto device_link;
291         }
292
293         /* are we at the top level? */
294         pos = strrchr(parent_devpath, '/');
295         if (pos == NULL || pos == parent_devpath)
296                 return NULL;
297
298         /* get parent and remember it */
299         dev->parent = sysfs_device_get(parent_devpath);
300         return dev->parent;
301
302 device_link:
303         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
304         strlcat(parent_devpath, "/device", sizeof(parent_devpath));
305         if (sysfs_resolve_link(parent_devpath, sizeof(parent_devpath)) != 0)
306                 return NULL;
307
308         /* get parent and remember it */
309         dev->parent = sysfs_device_get(parent_devpath);
310         return dev->parent;
311 }
312
313 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
314 {
315         struct sysfs_device *dev_parent;
316
317         dev_parent = sysfs_device_get_parent(dev);
318         while (dev_parent != NULL) {
319                 if (strcmp(dev_parent->subsystem, subsystem) == 0)
320                         return dev_parent;
321                 dev_parent = sysfs_device_get_parent(dev_parent);
322         }
323         return NULL;
324 }
325
326 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
327 {
328         char path_full[PATH_SIZE];
329         const char *path;
330         char value[NAME_SIZE];
331         struct sysfs_attr *attr_loop;
332         struct sysfs_attr *attr;
333         struct stat statbuf;
334         int fd;
335         ssize_t size;
336         size_t sysfs_len;
337
338         dbg("open '%s'/'%s'\n", devpath, attr_name);
339         sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
340         if(sysfs_len >= sizeof(path_full))
341                 sysfs_len = sizeof(path_full) - 1;
342         path = &path_full[sysfs_len];
343         strlcat(path_full, devpath, sizeof(path_full));
344         strlcat(path_full, "/", sizeof(path_full));
345         strlcat(path_full, attr_name, sizeof(path_full));
346
347         /* look for attribute in cache */
348         list_for_each_entry(attr_loop, &attr_list, node) {
349                 if (strcmp(attr_loop->path, path) == 0) {
350                         dbg("found in cache '%s'\n", attr_loop->path);
351                         return attr_loop->value;
352                 }
353         }
354
355         /* store attribute in cache (also negatives are kept in cache) */
356         dbg("new uncached attribute '%s'\n", path_full);
357         attr = malloc(sizeof(struct sysfs_attr));
358         if (attr == NULL)
359                 return NULL;
360         memset(attr, 0x00, sizeof(struct sysfs_attr));
361         strlcpy(attr->path, path, sizeof(attr->path));
362         dbg("add to cache '%s'\n", path_full);
363         list_add(&attr->node, &attr_list);
364
365         if (lstat(path_full, &statbuf) != 0) {
366                 dbg("stat '%s' failed: %s\n", path_full, strerror(errno));
367                 goto out;
368         }
369
370         if (S_ISLNK(statbuf.st_mode)) {
371                 /* links return the last element of the target path */
372                 char link_target[PATH_SIZE];
373                 int len;
374                 const char *pos;
375
376                 len = readlink(path_full, link_target, sizeof(link_target));
377                 if (len > 0) {
378                         link_target[len] = '\0';
379                         pos = strrchr(link_target, '/');
380                         if (pos != NULL) {
381                                 dbg("cache '%s' with link value '%s'\n", path_full, value);
382                                 strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
383                                 attr->value = attr->value_local;
384                         }
385                 }
386                 goto out;
387         }
388
389         /* skip directories */
390         if (S_ISDIR(statbuf.st_mode))
391                 goto out;
392
393         /* skip non-readable files */
394         if ((statbuf.st_mode & S_IRUSR) == 0)
395                 goto out;
396
397         /* read attribute value */
398         fd = open(path_full, O_RDONLY);
399         if (fd < 0) {
400                 dbg("attribute '%s' can not be opened\n", path_full);
401                 goto out;
402         }
403         size = read(fd, value, sizeof(value));
404         close(fd);
405         if (size < 0)
406                 goto out;
407         if (size == sizeof(value))
408                 goto out;
409
410         /* got a valid value, store and return it */
411         value[size] = '\0';
412         remove_trailing_chars(value, '\n');
413         dbg("cache '%s' with attribute value '%s'\n", path_full, value);
414         strlcpy(attr->value_local, value, sizeof(attr->value_local));
415         attr->value = attr->value_local;
416
417 out:
418         return attr->value;
419 }
420
421 int sysfs_lookup_devpath_by_subsys_id(char *devpath_full, size_t len, const char *subsystem, const char *id)
422 {
423         size_t sysfs_len;
424         char path_full[PATH_SIZE];
425         char *path;
426         struct stat statbuf;
427
428         sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
429         path = &path_full[sysfs_len];
430
431         if (strcmp(subsystem, "subsystem") == 0) {
432                 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
433                 strlcat(path, id, sizeof(path_full) - sysfs_len);
434                 if (stat(path_full, &statbuf) == 0)
435                         goto found;
436
437                 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
438                 strlcat(path, id, sizeof(path_full) - sysfs_len);
439                 if (stat(path_full, &statbuf) == 0)
440                         goto found;
441                 goto out;
442
443                 strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
444                 strlcat(path, id, sizeof(path_full) - sysfs_len);
445                 if (stat(path_full, &statbuf) == 0)
446                         goto found;
447         }
448
449         if (strcmp(subsystem, "module") == 0) {
450                 strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
451                 strlcat(path, id, sizeof(path_full) - sysfs_len);
452                 if (stat(path_full, &statbuf) == 0)
453                         goto found;
454                 goto out;
455         }
456
457         if (strcmp(subsystem, "drivers") == 0) {
458                 char subsys[NAME_SIZE];
459                 char *driver;
460
461                 strlcpy(subsys, id, sizeof(subsys));
462                 driver = strchr(subsys, ':');
463                 if (driver != NULL) {
464                         driver[0] = '\0';
465                         driver = &driver[1];
466                         strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
467                         strlcat(path, subsys, sizeof(path_full) - sysfs_len);
468                         strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
469                         strlcat(path, driver, sizeof(path_full) - sysfs_len);
470                         if (stat(path_full, &statbuf) == 0)
471                                 goto found;
472
473                         strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
474                         strlcat(path, subsys, sizeof(path_full) - sysfs_len);
475                         strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
476                         strlcat(path, driver, sizeof(path_full) - sysfs_len);
477                         if (stat(path_full, &statbuf) == 0)
478                                 goto found;
479                 }
480                 goto out;
481         }
482
483         strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
484         strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
485         strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
486         strlcat(path, id, sizeof(path_full) - sysfs_len);
487         if (stat(path_full, &statbuf) == 0)
488                 goto found;
489
490         strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
491         strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
492         strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
493         strlcat(path, id, sizeof(path_full) - sysfs_len);
494         if (stat(path_full, &statbuf) == 0)
495                 goto found;
496
497         strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
498         strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
499         strlcat(path, "/", sizeof(path_full) - sysfs_len);
500         strlcat(path, id, sizeof(path_full) - sysfs_len);
501         if (stat(path_full, &statbuf) == 0)
502                 goto found;
503 out:
504         return 0;
505 found:
506         if (S_ISLNK(statbuf.st_mode))
507                 sysfs_resolve_link(path, sizeof(path_full) - sysfs_len);
508         strlcpy(devpath_full, path, len);
509         return 1;
510 }