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