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