chiark / gitweb /
fix BUS, ID, $id usage
[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", devpath, 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(devpath, "/block/", 7) == 0) {
186                 strlcpy(dev->subsystem, "block", sizeof(dev->subsystem));
187         } else if (strncmp(devpath, "/devices/", 9) == 0) {
188                 /* get subsystem from bus name */
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                 }
200
201                 /* get driver name */
202                 strlcpy(link_path, sysfs_path, sizeof(link_path));
203                 strlcat(link_path, dev->devpath, sizeof(link_path));
204                 strlcat(link_path, "/driver", sizeof(link_path));
205                 len = readlink(link_path, link_target, sizeof(link_target));
206                 if (len > 0) {
207                         link_target[len] = '\0';
208                         dbg("driver link '%s' points to '%s'", link_path, link_target);
209                         pos = strrchr(link_target, '/');
210                         if (pos != NULL)
211                                 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
212                 }
213         } else if (strncmp(devpath, "/bus/", 5) == 0 && strstr(devpath, "/drivers/")) {
214                 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
215         } else if (strncmp(devpath, "/module/", 8) == 0) {
216                 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
217         }
218
219         dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'", dev->devpath, dev->subsystem, dev->driver);
220         list_add(&dev->node, &dev_list);
221
222         return dev;
223 }
224
225 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
226 {
227         char parent_devpath[PATH_SIZE];
228         char device_link[PATH_SIZE];
229         char device_link_target[PATH_SIZE];
230         char *pos;
231         int i;
232         int len;
233         int back;
234
235         /* requesting a parent is only valid for devices */
236         if ((strncmp(dev->devpath, "/devices/", 9) != 0) &&
237             (strncmp(dev->devpath, "/class/", 7) != 0) &&
238             (strncmp(dev->devpath, "/block/", 7) != 0))
239                 return NULL;
240
241         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
242         dbg("'%s'", parent_devpath);
243
244         /* strip last element */
245         pos = strrchr(parent_devpath, '/');
246         if (pos == NULL || pos == parent_devpath)
247                 return NULL;
248         pos[0] = '\0';
249
250         /* are we at the top level */
251         if (strcmp(parent_devpath, "/devices") == 0) {
252                 dbg("/devices top level");
253                 return NULL;
254         }
255
256         /* at the top level we may follow the "device" link */
257         if (strcmp(parent_devpath, "/block") == 0) {
258                 dbg("/block top level, look for device link");
259                 goto device_link;
260         }
261
262         if (strncmp(parent_devpath, "/class", 6) == 0) {
263                 pos = strrchr(parent_devpath, '/');
264                 if (pos == &parent_devpath[6] || pos == parent_devpath) {
265                         dbg("class top level, look for device link");
266                         goto device_link;
267                 }
268         }
269         return sysfs_device_get(parent_devpath);
270
271 device_link:
272         strlcpy(device_link, sysfs_path, sizeof(device_link));
273         strlcat(device_link, dev->devpath, sizeof(device_link));
274         strlcat(device_link, "/device", sizeof(device_link));
275         len = readlink(device_link, device_link_target, sizeof(device_link_target));
276         if (len < 0)
277                 return NULL;
278         device_link_target[len] = '\0';
279         dbg("device link '%s' points to '%s'", device_link, device_link_target);
280
281         for (back = 0; strncmp(&device_link_target[back * 3], "../", 3) == 0; back++)
282                 ;
283         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
284         dbg("base='%s', tail='%s', back=%i", parent_devpath, &device_link_target[back * 3], back);
285         for (i = 0; i < back; i++) {
286                 pos = strrchr(parent_devpath, '/');
287                 if (pos == NULL)
288                         return NULL;
289                 pos[0] = '\0';
290         }
291         dbg("after moving back '%s'", parent_devpath);
292         strlcat(parent_devpath, "/", sizeof(parent_devpath));
293         strlcat(parent_devpath, &device_link_target[back * 3], sizeof(parent_devpath));
294         return sysfs_device_get(parent_devpath);
295 }
296
297 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
298 {
299         char path_full[PATH_SIZE];
300         const char *path;
301         char value[NAME_SIZE];
302         struct sysfs_attr *attr_loop;
303         struct sysfs_attr *attr;
304         int fd;
305         ssize_t size;
306         size_t sysfs_len;
307
308         sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
309         path = &path_full[sysfs_len];
310         strlcat(path_full, devpath, sizeof(path_full));
311         strlcat(path_full, "/", sizeof(path_full));
312         strlcat(path_full, attr_name, sizeof(path_full));
313
314         /* look for attribute in cache */
315         list_for_each_entry(attr_loop, &attr_list, node) {
316                 if (strcmp(attr_loop->path, path) == 0) {
317                         dbg("found in cache '%s'", attr_loop->path);
318                         return attr_loop->value;
319                 }
320         }
321
322         /* read attribute value */
323         fd = open(path_full, O_RDONLY);
324         if (fd < 0)
325                 return NULL;
326         size = read(fd, value, sizeof(value));
327         close(fd);
328         if (size < 0)
329                 return NULL;
330         if (size == sizeof(value))
331                 return NULL;
332         value[size] = '\0';
333         remove_trailing_chars(value, '\n');
334
335         /* store attribute in cache */
336         attr = malloc(sizeof(struct sysfs_attr));
337         if (attr == NULL)
338                 return NULL;
339         strlcpy(attr->path, path, sizeof(attr->path));
340         strlcpy(attr->value, value, sizeof(attr->value));
341         dbg("add to cache '%s' '%s'", attr->path, attr->value);
342         list_add(&attr->node, &attr_list);
343
344         return attr->value;
345 }