chiark / gitweb /
udevtrigger: add --retry-failed
[elogind.git] / udev_sysfs.c
1 /*
2  * udev_sysfs.c - sysfs access
3  *
4  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30
31 #include "udev.h"
32
33 char sysfs_path[PATH_SIZE];
34
35 /* device cache */
36 static LIST_HEAD(dev_list);
37
38 /* attribute value cache */
39 static LIST_HEAD(attr_list);
40 struct sysfs_attr {
41         struct list_head node;
42         char path[PATH_SIZE];
43         char *value;                    /* points to value_local if value is cached */
44         char value_local[NAME_SIZE];
45 };
46
47 int sysfs_init(void)
48 {
49         const char *env;
50
51         env = getenv("SYSFS_PATH");
52         if (env) {
53                 strlcpy(sysfs_path, env, sizeof(sysfs_path));
54                 remove_trailing_chars(sysfs_path, '/');
55         } else
56                 strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
57         dbg("sysfs_path='%s'", sysfs_path);
58
59         INIT_LIST_HEAD(&dev_list);
60         INIT_LIST_HEAD(&attr_list);
61         return 0;
62 }
63
64 void sysfs_cleanup(void)
65 {
66         struct sysfs_attr *attr_loop;
67         struct sysfs_attr *attr_temp;
68         struct sysfs_device *dev_loop;
69         struct sysfs_device *dev_temp;
70
71         list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
72                 list_del(&attr_loop->node);
73                 free(attr_loop);
74         }
75
76         list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
77                 list_del(&dev_loop->node);
78                 free(dev_loop);
79         }
80 }
81
82 void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath,
83                              const char *subsystem, const char *driver)
84 {
85         char *pos;
86
87         strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
88         if (subsystem != NULL)
89                 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
90         if (driver != NULL)
91                 strlcpy(dev->driver, driver, sizeof(dev->driver));
92
93         /* set kernel name */
94         pos = strrchr(dev->devpath, '/');
95         if (pos == NULL)
96                 return;
97
98         strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
99         dbg("kernel='%s'", dev->kernel);
100
101         /* some devices have '!' in their name, change that to '/' */
102         pos = dev->kernel;
103         while (pos[0] != '\0') {
104                 if (pos[0] == '!')
105                         pos[0] = '/';
106                 pos++;
107         }
108
109         /* get kernel number */
110         pos = &dev->kernel[strlen(dev->kernel)];
111         while (isdigit(pos[-1]))
112                 pos--;
113         strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
114         dbg("kernel_number='%s'", dev->kernel_number);
115 }
116
117 struct sysfs_device *sysfs_device_get(const char *devpath)
118 {
119         char path[PATH_SIZE];
120         char devpath_real[PATH_SIZE];
121         struct sysfs_device *dev;
122         struct sysfs_device *dev_loop;
123         struct stat statbuf;
124         char link_path[PATH_SIZE];
125         char link_target[PATH_SIZE];
126         int len;
127         char *pos;
128
129         dbg("open '%s'", devpath);
130         strlcpy(devpath_real, devpath, sizeof(devpath_real));
131         remove_trailing_chars(devpath_real, '/');
132
133         /* look for device already in cache (we never put an untranslated path in the cache) */
134         list_for_each_entry(dev_loop, &dev_list, node) {
135                 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
136                         dbg("found in cache '%s'", dev_loop->devpath);
137                         return dev_loop;
138                 }
139         }
140
141         /* if we got a link, resolve it to the real device */
142         strlcpy(path, sysfs_path, sizeof(path));
143         strlcat(path, devpath_real, sizeof(path));
144         if (lstat(path, &statbuf) != 0) {
145                 dbg("stat '%s' failed: %s", path, strerror(errno));
146                 return NULL;
147         }
148         if (S_ISLNK(statbuf.st_mode)) {
149                 int i;
150                 int back;
151
152                 len = readlink(path, link_target, sizeof(link_target));
153                 if (len <= 0)
154                         return NULL;
155                 link_target[len] = '\0';
156                 dbg("devpath link '%s' points to '%s'", path, link_target);
157
158                 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
159                         ;
160                 dbg("base '%s', tail '%s', back %i", devpath_real, &link_target[back * 3], back);
161                 for (i = 0; i <= back; i++) {
162                         pos = strrchr(devpath_real, '/');
163                         if (pos == NULL)
164                                 return NULL;
165                         pos[0] = '\0';
166                 }
167                 dbg("after moving back '%s'", devpath_real);
168                 strlcat(devpath_real, "/", sizeof(devpath_real));
169                 strlcat(devpath_real, &link_target[back * 3], sizeof(devpath_real));
170
171                 /* now look for device in cache after path translation */
172                 list_for_each_entry(dev_loop, &dev_list, node) {
173                         if (strcmp(dev_loop->devpath, devpath_real) == 0) {
174                                 dbg("found in cache '%s'", dev_loop->devpath);
175                                 return dev_loop;
176                         }
177                 }
178         }
179
180         /* it is a new device */
181         dbg("new uncached device '%s'", devpath_real);
182         dev = malloc(sizeof(struct sysfs_device));
183         if (dev == NULL)
184                 return NULL;
185         memset(dev, 0x00, sizeof(struct sysfs_device));
186
187         sysfs_device_set_values(dev, devpath_real, NULL, NULL);
188
189         /* get subsystem */
190         if (strncmp(dev->devpath, "/class/", 7) == 0) {
191                 strlcpy(dev->subsystem, &dev->devpath[7], sizeof(dev->subsystem));
192                 pos = strchr(dev->subsystem, '/');
193                 if (pos != NULL)
194                         pos[0] = '\0';
195                 else
196                         dev->subsystem[0] = '\0';
197         } else if (strncmp(dev->devpath, "/block/", 7) == 0) {
198                 strlcpy(dev->subsystem, "block", sizeof(dev->subsystem));
199         } else if (strncmp(dev->devpath, "/devices/", 9) == 0) {
200                 /* get subsystem from "bus" link */
201                 strlcpy(link_path, sysfs_path, sizeof(link_path));
202                 strlcat(link_path, dev->devpath, sizeof(link_path));
203                 strlcat(link_path, "/bus", sizeof(link_path));
204                 len = readlink(link_path, link_target, sizeof(link_target));
205                 if (len > 0) {
206                         link_target[len] = '\0';
207                         dbg("bus link '%s' points to '%s'", link_path, link_target);
208                         pos = strrchr(link_target, '/');
209                         if (pos != NULL)
210                                 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
211                 } else {
212                         /* get subsystem from "subsystem" link */
213                         strlcpy(link_path, sysfs_path, sizeof(link_path));
214                         strlcat(link_path, dev->devpath, sizeof(link_path));
215                         strlcat(link_path, "/subsystem", sizeof(link_path));
216                         len = readlink(link_path, link_target, sizeof(link_target));
217                         if (len > 0) {
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                         }
224                 }
225                 /* get driver name */
226                 strlcpy(link_path, sysfs_path, sizeof(link_path));
227                 strlcat(link_path, dev->devpath, sizeof(link_path));
228                 strlcat(link_path, "/driver", sizeof(link_path));
229                 len = readlink(link_path, link_target, sizeof(link_target));
230                 if (len > 0) {
231                         link_target[len] = '\0';
232                         dbg("driver link '%s' points to '%s'", link_path, link_target);
233                         pos = strrchr(link_target, '/');
234                         if (pos != NULL)
235                                 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
236                 }
237         } else if (strncmp(dev->devpath, "/bus/", 5) == 0 && strstr(dev->devpath, "/drivers/")) {
238                 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
239         } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
240                 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
241         }
242
243         dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'", 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 sysfs_device *dev)
250 {
251         char parent_devpath[PATH_SIZE];
252         char device_link[PATH_SIZE];
253         char device_link_target[PATH_SIZE];
254         char *pos;
255         int i;
256         int len;
257         int back;
258
259         dbg("open '%s'", dev->devpath);
260
261         /* look if we already know the parent */
262         if (dev->parent != NULL)
263                 return dev->parent;
264
265         /* requesting a parent is only valid for devices */
266         if ((strncmp(dev->devpath, "/devices/", 9) != 0) &&
267             (strncmp(dev->devpath, "/class/", 7) != 0) &&
268             (strncmp(dev->devpath, "/block/", 7) != 0))
269                 return NULL;
270
271         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
272         dbg("'%s'", 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         /* are we at the top level of /devices */
281         if (strcmp(parent_devpath, "/devices") == 0) {
282                 dbg("/devices top level");
283                 return NULL;
284         }
285
286         /* at the top level of class/block we want to follow the "device" link */
287         if (strcmp(parent_devpath, "/block") == 0) {
288                 dbg("/block top level, look for device link");
289                 goto device_link;
290         }
291         if (strncmp(parent_devpath, "/class", 6) == 0) {
292                 pos = strrchr(parent_devpath, '/');
293                 if (pos == &parent_devpath[6] || pos == parent_devpath) {
294                         dbg("/class top level, look for device link");
295                         goto device_link;
296                 }
297         }
298         /* get parent and remember it */
299         dev->parent = sysfs_device_get(parent_devpath);
300         return dev->parent;
301
302 device_link:
303         strlcpy(device_link, sysfs_path, sizeof(device_link));
304         strlcat(device_link, dev->devpath, sizeof(device_link));
305         strlcat(device_link, "/device", sizeof(device_link));
306         len = readlink(device_link, device_link_target, sizeof(device_link_target));
307         if (len < 0)
308                 return NULL;
309         device_link_target[len] = '\0';
310         dbg("device link '%s' points to '%s'", device_link, device_link_target);
311
312         for (back = 0; strncmp(&device_link_target[back * 3], "../", 3) == 0; back++)
313                 ;
314         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
315         dbg("base='%s', tail='%s', back=%i", parent_devpath, &device_link_target[back * 3], back);
316         for (i = 0; i < back; i++) {
317                 pos = strrchr(parent_devpath, '/');
318                 if (pos == NULL)
319                         return NULL;
320                 pos[0] = '\0';
321         }
322         dbg("after moving back '%s'", parent_devpath);
323         strlcat(parent_devpath, "/", sizeof(parent_devpath));
324         strlcat(parent_devpath, &device_link_target[back * 3], sizeof(parent_devpath));
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         int fd;
352         ssize_t size;
353         size_t sysfs_len;
354
355         dbg("open '%s'/'%s'", devpath, attr_name);
356         sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
357         path = &path_full[sysfs_len];
358         strlcat(path_full, devpath, sizeof(path_full));
359         strlcat(path_full, "/", sizeof(path_full));
360         strlcat(path_full, attr_name, sizeof(path_full));
361
362         /* look for attribute in cache */
363         list_for_each_entry(attr_loop, &attr_list, node) {
364                 if (strcmp(attr_loop->path, path) == 0) {
365                         dbg("found in cache '%s'", attr_loop->path);
366                         return attr_loop->value;
367                 }
368         }
369
370         /* store attribute in cache (also negatives are kept in cache) */
371         dbg("new uncached attribute '%s'", path_full);
372         attr = malloc(sizeof(struct sysfs_attr));
373         if (attr == NULL)
374                 return NULL;
375         memset(attr, 0x00, sizeof(struct sysfs_attr));
376         strlcpy(attr->path, path, sizeof(attr->path));
377         dbg("add to cache '%s'", path_full);
378         list_add(&attr->node, &attr_list);
379
380         /* read attribute value */
381         fd = open(path_full, O_RDONLY);
382         if (fd < 0) {
383                 dbg("attribute '%s' does not exist", path_full);
384                 goto out;
385         }
386         size = read(fd, value, sizeof(value));
387         close(fd);
388         if (size < 0)
389                 goto out;
390         if (size == sizeof(value))
391                 goto out;
392
393         /* got a valid value, store and return it */
394         value[size] = '\0';
395         remove_trailing_chars(value, '\n');
396         dbg("cache '%s' with value '%s'", path_full, value);
397         strlcpy(attr->value_local, value, sizeof(attr->value_local));
398         attr->value = attr->value_local;
399
400 out:
401         return attr->value;
402 }