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