chiark / gitweb /
remove outdated docs/README-gcov_for_udev
[elogind.git] / udev / udev_sysfs.c
1 /*
2  * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <stddef.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <sys/stat.h>
27
28 #include "udev.h"
29
30 /* device cache */
31 static LIST_HEAD(dev_list);
32
33 /* attribute value cache */
34 static LIST_HEAD(attr_list);
35
36 struct sysfs_attr {
37         struct list_head node;
38         char path[UTIL_PATH_SIZE];
39         char *value;                    /* points to value_local if value is cached */
40         char value_local[UTIL_NAME_SIZE];
41 };
42
43 static int resolve_sys_link(struct udev *udev, char *path, size_t size)
44 {
45         char link_path[UTIL_PATH_SIZE];
46         char link_target[UTIL_PATH_SIZE];
47
48         int len;
49         int i;
50         int back;
51
52         util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
53         util_strlcat(link_path, path, sizeof(link_path));
54         len = readlink(link_path, link_target, sizeof(link_target));
55         if (len <= 0)
56                 return -1;
57         link_target[len] = '\0';
58         dbg(udev, "path link '%s' points to '%s'\n", path, link_target);
59
60         for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
61                 ;
62         dbg(udev, "base '%s', tail '%s', back %i\n", path, &link_target[back * 3], back);
63         for (i = 0; i <= back; i++) {
64                 char *pos = strrchr(path, '/');
65
66                 if (pos == NULL)
67                         return -1;
68                 pos[0] = '\0';
69         }
70         dbg(udev, "after moving back '%s'\n", path);
71         util_strlcat(path, "/", size);
72         util_strlcat(path, &link_target[back * 3], size);
73         return 0;
74 }
75
76 int sysfs_init(void)
77 {
78         INIT_LIST_HEAD(&dev_list);
79         INIT_LIST_HEAD(&attr_list);
80         return 0;
81 }
82
83 void sysfs_cleanup(void)
84 {
85         struct sysfs_attr *attr_loop;
86         struct sysfs_attr *attr_temp;
87         struct sysfs_device *dev_loop;
88         struct sysfs_device *dev_temp;
89
90         list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
91                 list_del(&attr_loop->node);
92                 free(attr_loop);
93         }
94
95         list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
96                 list_del(&dev_loop->node);
97                 free(dev_loop);
98         }
99 }
100
101 void sysfs_device_set_values(struct udev *udev,
102                              struct sysfs_device *dev, const char *devpath,
103                              const char *subsystem, const char *driver)
104 {
105         char *pos;
106
107         util_strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
108         if (subsystem != NULL)
109                 util_strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
110         if (driver != NULL)
111                 util_strlcpy(dev->driver, driver, sizeof(dev->driver));
112
113         /* set kernel name */
114         pos = strrchr(dev->devpath, '/');
115         if (pos == NULL)
116                 return;
117         util_strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
118         dbg(udev, "kernel='%s'\n", dev->kernel);
119
120         /* some devices have '!' in their name, change that to '/' */
121         pos = dev->kernel;
122         while (pos[0] != '\0') {
123                 if (pos[0] == '!')
124                         pos[0] = '/';
125                 pos++;
126         }
127
128         /* get kernel number */
129         pos = &dev->kernel[strlen(dev->kernel)];
130         while (isdigit(pos[-1]))
131                 pos--;
132         util_strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
133         dbg(udev, "kernel_number='%s'\n", dev->kernel_number);
134 }
135
136 struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath)
137 {
138         char path[UTIL_PATH_SIZE];
139         char devpath_real[UTIL_PATH_SIZE];
140         struct sysfs_device *dev;
141         struct sysfs_device *dev_loop;
142         struct stat statbuf;
143         char link_path[UTIL_PATH_SIZE];
144         char link_target[UTIL_PATH_SIZE];
145         int len;
146         char *pos;
147
148         /* we handle only these devpathes */
149         if (devpath != NULL &&
150             strncmp(devpath, "/devices/", 9) != 0 &&
151             strncmp(devpath, "/subsystem/", 11) != 0 &&
152             strncmp(devpath, "/module/", 8) != 0 &&
153             strncmp(devpath, "/bus/", 5) != 0 &&
154             strncmp(devpath, "/class/", 7) != 0 &&
155             strncmp(devpath, "/block/", 7) != 0)
156                 return NULL;
157
158         dbg(udev, "open '%s'\n", devpath);
159         util_strlcpy(devpath_real, devpath, sizeof(devpath_real));
160         util_remove_trailing_chars(devpath_real, '/');
161         if (devpath[0] == '\0' )
162                 return NULL;
163
164         /* look for device already in cache (we never put an untranslated path in the cache) */
165         list_for_each_entry(dev_loop, &dev_list, node) {
166                 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
167                         dbg(udev, "found in cache '%s'\n", dev_loop->devpath);
168                         return dev_loop;
169                 }
170         }
171
172         /* if we got a link, resolve it to the real device */
173         util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
174         util_strlcat(path, devpath_real, sizeof(path));
175         if (lstat(path, &statbuf) != 0) {
176                 dbg(udev, "stat '%s' failed: %m\n", path);
177                 return NULL;
178         }
179         if (S_ISLNK(statbuf.st_mode)) {
180                 if (resolve_sys_link(udev, devpath_real, sizeof(devpath_real)) != 0)
181                         return NULL;
182
183                 /* now look for device in cache after path translation */
184                 list_for_each_entry(dev_loop, &dev_list, node) {
185                         if (strcmp(dev_loop->devpath, devpath_real) == 0) {
186                                 dbg(udev, "found in cache '%s'\n", dev_loop->devpath);
187                                 return dev_loop;
188                         }
189                 }
190         }
191
192         /* it is a new device */
193         dbg(udev, "new uncached device '%s'\n", devpath_real);
194         dev = malloc(sizeof(struct sysfs_device));
195         if (dev == NULL)
196                 return NULL;
197         memset(dev, 0x00, sizeof(struct sysfs_device));
198
199         sysfs_device_set_values(udev, dev, devpath_real, NULL, NULL);
200
201         /* get subsystem name */
202         util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
203         util_strlcat(link_path, dev->devpath, sizeof(link_path));
204         util_strlcat(link_path, "/subsystem", sizeof(link_path));
205         len = readlink(link_path, link_target, sizeof(link_target));
206         if (len > 0) {
207                 /* get subsystem from "subsystem" link */
208                 link_target[len] = '\0';
209                 dbg(udev, "subsystem link '%s' points to '%s'\n", link_path, link_target);
210                 pos = strrchr(link_target, '/');
211                 if (pos != NULL)
212                         util_strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
213         } else if (strstr(dev->devpath, "/drivers/") != NULL) {
214                 util_strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
215         } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
216                 util_strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
217         } else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) {
218                 pos = strrchr(dev->devpath, '/');
219                 if (pos == &dev->devpath[10])
220                         util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
221         } else if (strncmp(dev->devpath, "/class/", 7) == 0) {
222                 pos = strrchr(dev->devpath, '/');
223                 if (pos == &dev->devpath[6])
224                         util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
225         } else if (strncmp(dev->devpath, "/bus/", 5) == 0) {
226                 pos = strrchr(dev->devpath, '/');
227                 if (pos == &dev->devpath[4])
228                         util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
229         }
230
231         /* get driver name */
232         util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
233         util_strlcat(link_path, dev->devpath, sizeof(link_path));
234         util_strlcat(link_path, "/driver", sizeof(link_path));
235         len = readlink(link_path, link_target, sizeof(link_target));
236         if (len > 0) {
237                 link_target[len] = '\0';
238                 dbg(udev, "driver link '%s' points to '%s'\n", link_path, link_target);
239                 pos = strrchr(link_target, '/');
240                 if (pos != NULL)
241                         util_strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
242         }
243
244         dbg(udev, "add to cache 'devpath=%s', subsystem='%s', driver='%s'\n", dev->devpath, dev->subsystem, dev->driver);
245         list_add(&dev->node, &dev_list);
246
247         return dev;
248 }
249
250 struct sysfs_device *sysfs_device_get_parent(struct udev *udev, struct sysfs_device *dev)
251 {
252         char parent_devpath[UTIL_PATH_SIZE];
253         char *pos;
254
255         dbg(udev, "open '%s'\n", dev->devpath);
256
257         /* look if we already know the parent */
258         if (dev->parent != NULL)
259                 return dev->parent;
260
261         util_strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
262         dbg(udev, "'%s'\n", parent_devpath);
263
264         /* strip last element */
265         pos = strrchr(parent_devpath, '/');
266         if (pos == NULL || pos == parent_devpath)
267                 return NULL;
268         pos[0] = '\0';
269
270         if (strncmp(parent_devpath, "/class", 6) == 0) {
271                 pos = strrchr(parent_devpath, '/');
272                 if (pos == &parent_devpath[6] || pos == parent_devpath) {
273                         dbg(udev, "/class top level, look for device link\n");
274                         goto device_link;
275                 }
276         }
277         if (strcmp(parent_devpath, "/block") == 0) {
278                 dbg(udev, "/block top level, look for device link\n");
279                 goto device_link;
280         }
281
282         /* are we at the top level? */
283         pos = strrchr(parent_devpath, '/');
284         if (pos == NULL || pos == parent_devpath)
285                 return NULL;
286
287         /* get parent and remember it */
288         dev->parent = sysfs_device_get(udev, parent_devpath);
289         return dev->parent;
290
291 device_link:
292         util_strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
293         util_strlcat(parent_devpath, "/device", sizeof(parent_devpath));
294         if (resolve_sys_link(udev, parent_devpath, sizeof(parent_devpath)) != 0)
295                 return NULL;
296
297         /* get parent and remember it */
298         dev->parent = sysfs_device_get(udev, parent_devpath);
299         return dev->parent;
300 }
301
302 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct udev *udev, struct sysfs_device *dev, const char *subsystem)
303 {
304         struct sysfs_device *dev_parent;
305
306         dev_parent = sysfs_device_get_parent(udev, dev);
307         while (dev_parent != NULL) {
308                 if (strcmp(dev_parent->subsystem, subsystem) == 0)
309                         return dev_parent;
310                 dev_parent = sysfs_device_get_parent(udev, dev_parent);
311         }
312         return NULL;
313 }
314
315 char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *attr_name)
316 {
317         char path_full[UTIL_PATH_SIZE];
318         const char *path;
319         char value[UTIL_NAME_SIZE];
320         struct sysfs_attr *attr_loop;
321         struct sysfs_attr *attr;
322         struct stat statbuf;
323         int fd;
324         ssize_t size;
325         size_t sysfs_len;
326
327         dbg(udev, "open '%s'/'%s'\n", devpath, attr_name);
328         sysfs_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
329         if(sysfs_len >= sizeof(path_full))
330                 sysfs_len = sizeof(path_full) - 1;
331         path = &path_full[sysfs_len];
332         util_strlcat(path_full, devpath, sizeof(path_full));
333         util_strlcat(path_full, "/", sizeof(path_full));
334         util_strlcat(path_full, attr_name, sizeof(path_full));
335
336         /* look for attribute in cache */
337         list_for_each_entry(attr_loop, &attr_list, node) {
338                 if (strcmp(attr_loop->path, path) == 0) {
339                         dbg(udev, "found in cache '%s'\n", attr_loop->path);
340                         return attr_loop->value;
341                 }
342         }
343
344         /* store attribute in cache (also negatives are kept in cache) */
345         dbg(udev, "new uncached attribute '%s'\n", path_full);
346         attr = malloc(sizeof(struct sysfs_attr));
347         if (attr == NULL)
348                 return NULL;
349         memset(attr, 0x00, sizeof(struct sysfs_attr));
350         util_strlcpy(attr->path, path, sizeof(attr->path));
351         dbg(udev, "add to cache '%s'\n", path_full);
352         list_add(&attr->node, &attr_list);
353
354         if (lstat(path_full, &statbuf) != 0) {
355                 dbg(udev, "stat '%s' failed: %m\n", path_full);
356                 goto out;
357         }
358
359         if (S_ISLNK(statbuf.st_mode)) {
360                 /* links return the last element of the target path */
361                 char link_target[UTIL_PATH_SIZE];
362                 int len;
363                 const char *pos;
364
365                 len = readlink(path_full, link_target, sizeof(link_target));
366                 if (len > 0) {
367                         link_target[len] = '\0';
368                         pos = strrchr(link_target, '/');
369                         if (pos != NULL) {
370                                 dbg(udev, "cache '%s' with link value '%s'\n", path_full, value);
371                                 util_strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
372                                 attr->value = attr->value_local;
373                         }
374                 }
375                 goto out;
376         }
377
378         /* skip directories */
379         if (S_ISDIR(statbuf.st_mode))
380                 goto out;
381
382         /* skip non-readable files */
383         if ((statbuf.st_mode & S_IRUSR) == 0)
384                 goto out;
385
386         /* read attribute value */
387         fd = open(path_full, O_RDONLY);
388         if (fd < 0) {
389                 dbg(udev, "attribute '%s' can not be opened\n", path_full);
390                 goto out;
391         }
392         size = read(fd, value, sizeof(value));
393         close(fd);
394         if (size < 0)
395                 goto out;
396         if (size == sizeof(value))
397                 goto out;
398
399         /* got a valid value, store and return it */
400         value[size] = '\0';
401         util_remove_trailing_chars(value, '\n');
402         dbg(udev, "cache '%s' with attribute value '%s'\n", path_full, value);
403         util_strlcpy(attr->value_local, value, sizeof(attr->value_local));
404         attr->value = attr->value_local;
405
406 out:
407         return attr->value;
408 }
409
410 int sysfs_lookup_devpath_by_subsys_id(struct udev *udev, char *devpath_full, size_t len, const char *subsystem, const char *id)
411 {
412         size_t sysfs_len;
413         char path_full[UTIL_PATH_SIZE];
414         char *path;
415         struct stat statbuf;
416
417         sysfs_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
418         path = &path_full[sysfs_len];
419
420         if (strcmp(subsystem, "subsystem") == 0) {
421                 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
422                 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
423                 if (stat(path_full, &statbuf) == 0)
424                         goto found;
425
426                 util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
427                 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
428                 if (stat(path_full, &statbuf) == 0)
429                         goto found;
430                 goto out;
431
432                 util_strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
433                 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
434                 if (stat(path_full, &statbuf) == 0)
435                         goto found;
436         }
437
438         if (strcmp(subsystem, "module") == 0) {
439                 util_strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
440                 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
441                 if (stat(path_full, &statbuf) == 0)
442                         goto found;
443                 goto out;
444         }
445
446         if (strcmp(subsystem, "drivers") == 0) {
447                 char subsys[UTIL_NAME_SIZE];
448                 char *driver;
449
450                 util_strlcpy(subsys, id, sizeof(subsys));
451                 driver = strchr(subsys, ':');
452                 if (driver != NULL) {
453                         driver[0] = '\0';
454                         driver = &driver[1];
455                         util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
456                         util_strlcat(path, subsys, sizeof(path_full) - sysfs_len);
457                         util_strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
458                         util_strlcat(path, driver, sizeof(path_full) - sysfs_len);
459                         if (stat(path_full, &statbuf) == 0)
460                                 goto found;
461
462                         util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
463                         util_strlcat(path, subsys, sizeof(path_full) - sysfs_len);
464                         util_strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
465                         util_strlcat(path, driver, sizeof(path_full) - sysfs_len);
466                         if (stat(path_full, &statbuf) == 0)
467                                 goto found;
468                 }
469                 goto out;
470         }
471
472         util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
473         util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
474         util_strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
475         util_strlcat(path, id, sizeof(path_full) - sysfs_len);
476         if (stat(path_full, &statbuf) == 0)
477                 goto found;
478
479         util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
480         util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
481         util_strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
482         util_strlcat(path, id, sizeof(path_full) - sysfs_len);
483         if (stat(path_full, &statbuf) == 0)
484                 goto found;
485
486         util_strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
487         util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
488         util_strlcat(path, "/", sizeof(path_full) - sysfs_len);
489         util_strlcat(path, id, sizeof(path_full) - sysfs_len);
490         if (stat(path_full, &statbuf) == 0)
491                 goto found;
492 out:
493         return 0;
494 found:
495         if (S_ISLNK(statbuf.st_mode))
496                 resolve_sys_link(udev, path, sizeof(path_full) - sysfs_len);
497         util_strlcpy(devpath_full, path, len);
498         return 1;
499 }