chiark / gitweb /
release 125
[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'\n", 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'\n", 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'\n", 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'\n", 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\n", 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'\n", 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'\n", 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'\n", 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\n", 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'\n", dev_loop->devpath);
197                                 return dev_loop;
198                         }
199                 }
200         }
201
202         /* it is a new device */
203         dbg("new uncached device '%s'\n", 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'\n", 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 (strstr(dev->devpath, "/drivers/") != NULL) {
224                 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
225         } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
226                 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
227         } else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) {
228                 pos = strrchr(dev->devpath, '/');
229                 if (pos == &dev->devpath[10])
230                         strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
231         } else if (strncmp(dev->devpath, "/class/", 7) == 0) {
232                 pos = strrchr(dev->devpath, '/');
233                 if (pos == &dev->devpath[6])
234                         strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
235         } else if (strncmp(dev->devpath, "/bus/", 5) == 0) {
236                 pos = strrchr(dev->devpath, '/');
237                 if (pos == &dev->devpath[4])
238                         strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
239         }
240
241         /* get driver name */
242         strlcpy(link_path, sysfs_path, sizeof(link_path));
243         strlcat(link_path, dev->devpath, sizeof(link_path));
244         strlcat(link_path, "/driver", sizeof(link_path));
245         len = readlink(link_path, link_target, sizeof(link_target));
246         if (len > 0) {
247                 link_target[len] = '\0';
248                 dbg("driver link '%s' points to '%s'\n", link_path, link_target);
249                 pos = strrchr(link_target, '/');
250                 if (pos != NULL)
251                         strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
252         }
253
254         dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'\n", 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'\n", dev->devpath);
266
267         /* look if we already know the parent */
268         if (dev->parent != NULL)
269                 return dev->parent;
270
271         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
272         dbg("'%s'\n", parent_devpath);
273
274         /* strip last element */
275         pos = strrchr(parent_devpath, '/');
276         if (pos == NULL || pos == parent_devpath)
277                 return NULL;
278         pos[0] = '\0';
279
280         if (strncmp(parent_devpath, "/class", 6) == 0) {
281                 pos = strrchr(parent_devpath, '/');
282                 if (pos == &parent_devpath[6] || pos == parent_devpath) {
283                         dbg("/class top level, look for device link\n");
284                         goto device_link;
285                 }
286         }
287         if (strcmp(parent_devpath, "/block") == 0) {
288                 dbg("/block top level, look for device link\n");
289                 goto device_link;
290         }
291
292         /* are we at the top level? */
293         pos = strrchr(parent_devpath, '/');
294         if (pos == NULL || pos == parent_devpath)
295                 return NULL;
296
297         /* get parent and remember it */
298         dev->parent = sysfs_device_get(parent_devpath);
299         return dev->parent;
300
301 device_link:
302         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
303         strlcat(parent_devpath, "/device", sizeof(parent_devpath));
304         if (sysfs_resolve_link(parent_devpath, sizeof(parent_devpath)) != 0)
305                 return NULL;
306
307         /* get parent and remember it */
308         dev->parent = sysfs_device_get(parent_devpath);
309         return dev->parent;
310 }
311
312 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
313 {
314         struct sysfs_device *dev_parent;
315
316         dev_parent = sysfs_device_get_parent(dev);
317         while (dev_parent != NULL) {
318                 if (strcmp(dev_parent->subsystem, subsystem) == 0)
319                         return dev_parent;
320                 dev_parent = sysfs_device_get_parent(dev_parent);
321         }
322         return NULL;
323 }
324
325 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
326 {
327         char path_full[PATH_SIZE];
328         const char *path;
329         char value[NAME_SIZE];
330         struct sysfs_attr *attr_loop;
331         struct sysfs_attr *attr;
332         struct stat statbuf;
333         int fd;
334         ssize_t size;
335         size_t sysfs_len;
336
337         dbg("open '%s'/'%s'\n", devpath, attr_name);
338         sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
339         if(sysfs_len >= sizeof(path_full))
340                 sysfs_len = sizeof(path_full) - 1;
341         path = &path_full[sysfs_len];
342         strlcat(path_full, devpath, sizeof(path_full));
343         strlcat(path_full, "/", sizeof(path_full));
344         strlcat(path_full, attr_name, sizeof(path_full));
345
346         /* look for attribute in cache */
347         list_for_each_entry(attr_loop, &attr_list, node) {
348                 if (strcmp(attr_loop->path, path) == 0) {
349                         dbg("found in cache '%s'\n", attr_loop->path);
350                         return attr_loop->value;
351                 }
352         }
353
354         /* store attribute in cache (also negatives are kept in cache) */
355         dbg("new uncached attribute '%s'\n", path_full);
356         attr = malloc(sizeof(struct sysfs_attr));
357         if (attr == NULL)
358                 return NULL;
359         memset(attr, 0x00, sizeof(struct sysfs_attr));
360         strlcpy(attr->path, path, sizeof(attr->path));
361         dbg("add to cache '%s'\n", path_full);
362         list_add(&attr->node, &attr_list);
363
364         if (lstat(path_full, &statbuf) != 0) {
365                 dbg("stat '%s' failed: %s\n", path_full, strerror(errno));
366                 goto out;
367         }
368
369         if (S_ISLNK(statbuf.st_mode)) {
370                 /* links return the last element of the target path */
371                 char link_target[PATH_SIZE];
372                 int len;
373                 const char *pos;
374
375                 len = readlink(path_full, link_target, sizeof(link_target));
376                 if (len > 0) {
377                         link_target[len] = '\0';
378                         pos = strrchr(link_target, '/');
379                         if (pos != NULL) {
380                                 dbg("cache '%s' with link value '%s'\n", path_full, value);
381                                 strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
382                                 attr->value = attr->value_local;
383                         }
384                 }
385                 goto out;
386         }
387
388         /* skip directories */
389         if (S_ISDIR(statbuf.st_mode))
390                 goto out;
391
392         /* skip non-readable files */
393         if ((statbuf.st_mode & S_IRUSR) == 0)
394                 goto out;
395
396         /* read attribute value */
397         fd = open(path_full, O_RDONLY);
398         if (fd < 0) {
399                 dbg("attribute '%s' can not be opened\n", path_full);
400                 goto out;
401         }
402         size = read(fd, value, sizeof(value));
403         close(fd);
404         if (size < 0)
405                 goto out;
406         if (size == sizeof(value))
407                 goto out;
408
409         /* got a valid value, store and return it */
410         value[size] = '\0';
411         remove_trailing_chars(value, '\n');
412         dbg("cache '%s' with attribute value '%s'\n", path_full, value);
413         strlcpy(attr->value_local, value, sizeof(attr->value_local));
414         attr->value = attr->value_local;
415
416 out:
417         return attr->value;
418 }
419
420 int sysfs_lookup_devpath_by_subsys_id(char *devpath_full, size_t len, const char *subsystem, const char *id)
421 {
422         size_t sysfs_len;
423         char path_full[PATH_SIZE];
424         char *path;
425         struct stat statbuf;
426
427         sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
428         path = &path_full[sysfs_len];
429
430         if (strcmp(subsystem, "subsystem") == 0) {
431                 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
432                 strlcat(path, id, sizeof(path_full) - sysfs_len);
433                 if (stat(path_full, &statbuf) == 0)
434                         goto found;
435
436                 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
437                 strlcat(path, id, sizeof(path_full) - sysfs_len);
438                 if (stat(path_full, &statbuf) == 0)
439                         goto found;
440                 goto out;
441
442                 strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
443                 strlcat(path, id, sizeof(path_full) - sysfs_len);
444                 if (stat(path_full, &statbuf) == 0)
445                         goto found;
446         }
447
448         if (strcmp(subsystem, "module") == 0) {
449                 strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
450                 strlcat(path, id, sizeof(path_full) - sysfs_len);
451                 if (stat(path_full, &statbuf) == 0)
452                         goto found;
453                 goto out;
454         }
455
456         if (strcmp(subsystem, "drivers") == 0) {
457                 char subsys[NAME_SIZE];
458                 char *driver;
459
460                 strlcpy(subsys, id, sizeof(subsys));
461                 driver = strchr(subsys, ':');
462                 if (driver != NULL) {
463                         driver[0] = '\0';
464                         driver = &driver[1];
465                         strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
466                         strlcat(path, subsys, sizeof(path_full) - sysfs_len);
467                         strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
468                         strlcat(path, driver, sizeof(path_full) - sysfs_len);
469                         if (stat(path_full, &statbuf) == 0)
470                                 goto found;
471
472                         strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
473                         strlcat(path, subsys, sizeof(path_full) - sysfs_len);
474                         strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
475                         strlcat(path, driver, sizeof(path_full) - sysfs_len);
476                         if (stat(path_full, &statbuf) == 0)
477                                 goto found;
478                 }
479                 goto out;
480         }
481
482         strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
483         strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
484         strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
485         strlcat(path, id, sizeof(path_full) - sysfs_len);
486         if (stat(path_full, &statbuf) == 0)
487                 goto found;
488
489         strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
490         strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
491         strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
492         strlcat(path, id, sizeof(path_full) - sysfs_len);
493         if (stat(path_full, &statbuf) == 0)
494                 goto found;
495
496         strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
497         strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
498         strlcat(path, "/", sizeof(path_full) - sysfs_len);
499         strlcat(path, id, sizeof(path_full) - sysfs_len);
500         if (stat(path_full, &statbuf) == 0)
501                 goto found;
502 out:
503         return 0;
504 found:
505         if (S_ISLNK(statbuf.st_mode))
506                 sysfs_resolve_link(path, sizeof(path_full) - sysfs_len);
507         strlcpy(devpath_full, path, len);
508         return 1;
509 }