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