chiark / gitweb /
c1926966896849c827916402f72312c17e02c190
[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'", 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
96         strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
97         dbg("kernel='%s'", dev->kernel);
98
99         /* some devices have '!' in their name, change that to '/' */
100         pos = dev->kernel;
101         while (pos[0] != '\0') {
102                 if (pos[0] == '!')
103                         pos[0] = '/';
104                 pos++;
105         }
106
107         /* get kernel number */
108         pos = &dev->kernel[strlen(dev->kernel)];
109         while (isdigit(pos[-1]))
110                 pos--;
111         strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
112         dbg("kernel_number='%s'", dev->kernel_number);
113 }
114
115 struct sysfs_device *sysfs_device_get(const char *devpath)
116 {
117         char path[PATH_SIZE];
118         char devpath_real[PATH_SIZE];
119         struct sysfs_device *dev;
120         struct sysfs_device *dev_loop;
121         struct stat statbuf;
122         char link_path[PATH_SIZE];
123         char link_target[PATH_SIZE];
124         int len;
125         char *pos;
126
127         dbg("open '%s'", devpath);
128         strlcpy(devpath_real, devpath, sizeof(devpath_real));
129         remove_trailing_chars(devpath_real, '/');
130
131         /* look for device already in cache (we never put an untranslated path in the cache) */
132         list_for_each_entry(dev_loop, &dev_list, node) {
133                 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
134                         dbg("found in cache '%s'", dev_loop->devpath);
135                         return dev_loop;
136                 }
137         }
138
139         /* if we got a link, resolve it to the real device */
140         strlcpy(path, sysfs_path, sizeof(path));
141         strlcat(path, devpath_real, sizeof(path));
142         if (lstat(path, &statbuf) != 0) {
143                 dbg("stat '%s' failed: %s", path, strerror(errno));
144                 return NULL;
145         }
146         if (S_ISLNK(statbuf.st_mode)) {
147                 int i;
148                 int back;
149
150                 len = readlink(path, link_target, sizeof(link_target));
151                 if (len <= 0)
152                         return NULL;
153                 link_target[len] = '\0';
154                 dbg("devpath link '%s' points to '%s'", path, link_target);
155
156                 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
157                         ;
158                 dbg("base '%s', tail '%s', back %i", devpath_real, &link_target[back * 3], back);
159                 for (i = 0; i <= back; i++) {
160                         pos = strrchr(devpath_real, '/');
161                         if (pos == NULL)
162                                 return NULL;
163                         pos[0] = '\0';
164                 }
165                 dbg("after moving back '%s'", devpath_real);
166                 strlcat(devpath_real, "/", sizeof(devpath_real));
167                 strlcat(devpath_real, &link_target[back * 3], sizeof(devpath_real));
168
169                 /* now look for device in cache after path translation */
170                 list_for_each_entry(dev_loop, &dev_list, node) {
171                         if (strcmp(dev_loop->devpath, devpath_real) == 0) {
172                                 dbg("found in cache '%s'", dev_loop->devpath);
173                                 return dev_loop;
174                         }
175                 }
176         }
177
178         /* it is a new device */
179         dbg("new uncached device '%s'", devpath_real);
180         dev = malloc(sizeof(struct sysfs_device));
181         if (dev == NULL)
182                 return NULL;
183         memset(dev, 0x00, sizeof(struct sysfs_device));
184
185         sysfs_device_set_values(dev, devpath_real, NULL, NULL);
186
187         /* get subsystem */
188         if (strncmp(dev->devpath, "/class/", 7) == 0) {
189                 strlcpy(dev->subsystem, &dev->devpath[7], sizeof(dev->subsystem));
190                 pos = strchr(dev->subsystem, '/');
191                 if (pos != NULL)
192                         pos[0] = '\0';
193                 else
194                         dev->subsystem[0] = '\0';
195         } else if (strncmp(dev->devpath, "/block/", 7) == 0) {
196                 strlcpy(dev->subsystem, "block", sizeof(dev->subsystem));
197         } else if (strncmp(dev->devpath, "/devices/", 9) == 0) {
198                 /* get subsystem from "bus" link */
199                 strlcpy(link_path, sysfs_path, sizeof(link_path));
200                 strlcat(link_path, dev->devpath, sizeof(link_path));
201                 strlcat(link_path, "/bus", sizeof(link_path));
202                 len = readlink(link_path, link_target, sizeof(link_target));
203                 if (len > 0) {
204                         link_target[len] = '\0';
205                         dbg("bus link '%s' points to '%s'", link_path, link_target);
206                         pos = strrchr(link_target, '/');
207                         if (pos != NULL)
208                                 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
209                 } else {
210                         /* get subsystem from "subsystem" link */
211                         strlcpy(link_path, sysfs_path, sizeof(link_path));
212                         strlcat(link_path, dev->devpath, sizeof(link_path));
213                         strlcat(link_path, "/subsystem", sizeof(link_path));
214                         len = readlink(link_path, link_target, sizeof(link_target));
215                         if (len > 0) {
216                                 link_target[len] = '\0';
217                                 dbg("subsystem link '%s' points to '%s'", link_path, link_target);
218                                 pos = strrchr(link_target, '/');
219                                 if (pos != NULL)
220                                         strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
221                         }
222                 }
223                 /* get driver name */
224                 strlcpy(link_path, sysfs_path, sizeof(link_path));
225                 strlcat(link_path, dev->devpath, sizeof(link_path));
226                 strlcat(link_path, "/driver", sizeof(link_path));
227                 len = readlink(link_path, link_target, sizeof(link_target));
228                 if (len > 0) {
229                         link_target[len] = '\0';
230                         dbg("driver link '%s' points to '%s'", link_path, link_target);
231                         pos = strrchr(link_target, '/');
232                         if (pos != NULL)
233                                 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
234                 }
235         } else if (strncmp(dev->devpath, "/bus/", 5) == 0 && strstr(dev->devpath, "/drivers/")) {
236                 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
237         } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
238                 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
239         }
240
241         dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'", dev->devpath, dev->subsystem, dev->driver);
242         list_add(&dev->node, &dev_list);
243
244         return dev;
245 }
246
247 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
248 {
249         char parent_devpath[PATH_SIZE];
250         char device_link[PATH_SIZE];
251         char device_link_target[PATH_SIZE];
252         char *pos;
253         int i;
254         int len;
255         int back;
256
257         dbg("open '%s'", dev->devpath);
258
259         /* look if we already know the parent */
260         if (dev->parent != NULL)
261                 return dev->parent;
262
263         /* requesting a parent is only valid for devices */
264         if ((strncmp(dev->devpath, "/devices/", 9) != 0) &&
265             (strncmp(dev->devpath, "/class/", 7) != 0) &&
266             (strncmp(dev->devpath, "/block/", 7) != 0))
267                 return NULL;
268
269         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
270         dbg("'%s'", parent_devpath);
271
272         /* strip last element */
273         pos = strrchr(parent_devpath, '/');
274         if (pos == NULL || pos == parent_devpath)
275                 return NULL;
276         pos[0] = '\0';
277
278         /* are we at the top level of /devices */
279         if (strcmp(parent_devpath, "/devices") == 0) {
280                 dbg("/devices top level");
281                 return NULL;
282         }
283
284         /* at the top level of class/block we want to follow the "device" link */
285         if (strcmp(parent_devpath, "/block") == 0) {
286                 dbg("/block top level, look for device link");
287                 goto device_link;
288         }
289         if (strncmp(parent_devpath, "/class", 6) == 0) {
290                 pos = strrchr(parent_devpath, '/');
291                 if (pos == &parent_devpath[6] || pos == parent_devpath) {
292                         dbg("/class top level, look for device link");
293                         goto device_link;
294                 }
295         }
296         /* get parent and remember it */
297         dev->parent = sysfs_device_get(parent_devpath);
298         return dev->parent;
299
300 device_link:
301         strlcpy(device_link, sysfs_path, sizeof(device_link));
302         strlcat(device_link, dev->devpath, sizeof(device_link));
303         strlcat(device_link, "/device", sizeof(device_link));
304         len = readlink(device_link, device_link_target, sizeof(device_link_target));
305         if (len < 0)
306                 return NULL;
307         device_link_target[len] = '\0';
308         dbg("device link '%s' points to '%s'", device_link, device_link_target);
309
310         for (back = 0; strncmp(&device_link_target[back * 3], "../", 3) == 0; back++)
311                 ;
312         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
313         dbg("base='%s', tail='%s', back=%i", parent_devpath, &device_link_target[back * 3], back);
314         for (i = 0; i < back; i++) {
315                 pos = strrchr(parent_devpath, '/');
316                 if (pos == NULL)
317                         return NULL;
318                 pos[0] = '\0';
319         }
320         dbg("after moving back '%s'", parent_devpath);
321         strlcat(parent_devpath, "/", sizeof(parent_devpath));
322         strlcat(parent_devpath, &device_link_target[back * 3], sizeof(parent_devpath));
323
324         /* get parent and remember it */
325         dev->parent = sysfs_device_get(parent_devpath);
326         return dev->parent;
327 }
328
329 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
330 {
331         struct sysfs_device *dev_parent;
332
333         dev_parent = sysfs_device_get_parent(dev);
334         while (dev_parent != NULL) {
335                 if (strcmp(dev_parent->subsystem, subsystem) == 0)
336                         return dev_parent;
337                 dev_parent = sysfs_device_get_parent(dev_parent);
338         }
339         return NULL;
340 }
341
342 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
343 {
344         char path_full[PATH_SIZE];
345         const char *path;
346         char value[NAME_SIZE];
347         struct sysfs_attr *attr_loop;
348         struct sysfs_attr *attr;
349         int fd;
350         ssize_t size;
351         size_t sysfs_len;
352
353         dbg("open '%s'/'%s'", devpath, attr_name);
354         sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
355         path = &path_full[sysfs_len];
356         strlcat(path_full, devpath, sizeof(path_full));
357         strlcat(path_full, "/", sizeof(path_full));
358         strlcat(path_full, attr_name, sizeof(path_full));
359
360         /* look for attribute in cache */
361         list_for_each_entry(attr_loop, &attr_list, node) {
362                 if (strcmp(attr_loop->path, path) == 0) {
363                         dbg("found in cache '%s'", attr_loop->path);
364                         return attr_loop->value;
365                 }
366         }
367
368         /* store attribute in cache (also negatives are kept in cache) */
369         dbg("new uncached attribute '%s'", path_full);
370         attr = malloc(sizeof(struct sysfs_attr));
371         if (attr == NULL)
372                 return NULL;
373         memset(attr, 0x00, sizeof(struct sysfs_attr));
374         strlcpy(attr->path, path, sizeof(attr->path));
375         dbg("add to cache '%s'", path_full);
376         list_add(&attr->node, &attr_list);
377
378         /* read attribute value */
379         fd = open(path_full, O_RDONLY);
380         if (fd < 0) {
381                 dbg("attribute '%s' does not exist", path_full);
382                 goto out;
383         }
384         size = read(fd, value, sizeof(value));
385         close(fd);
386         if (size < 0)
387                 goto out;
388         if (size == sizeof(value))
389                 goto out;
390
391         /* got a valid value, store and return it */
392         value[size] = '\0';
393         remove_trailing_chars(value, '\n');
394         dbg("cache '%s' with value '%s'", path_full, value);
395         strlcpy(attr->value_local, value, sizeof(attr->value_local));
396         attr->value = attr->value_local;
397
398 out:
399         return attr->value;
400 }