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