chiark / gitweb /
add joystick support to persistent input rules
[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("new uncached device '%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         dbg("open '%s'", dev->devpath);
257
258         /* look if we already know the parent */
259         if (dev->parent != NULL)
260                 return dev->parent;
261
262         /* requesting a parent is only valid for devices */
263         if ((strncmp(dev->devpath, "/devices/", 9) != 0) &&
264             (strncmp(dev->devpath, "/class/", 7) != 0) &&
265             (strncmp(dev->devpath, "/block/", 7) != 0))
266                 return NULL;
267
268         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
269         dbg("'%s'", parent_devpath);
270
271         /* strip last element */
272         pos = strrchr(parent_devpath, '/');
273         if (pos == NULL || pos == parent_devpath)
274                 return NULL;
275         pos[0] = '\0';
276
277         /* are we at the top level of /devices */
278         if (strcmp(parent_devpath, "/devices") == 0) {
279                 dbg("/devices top level");
280                 return NULL;
281         }
282
283         /* at the top level of class/block we want to follow the "device" link */
284         if (strcmp(parent_devpath, "/block") == 0) {
285                 dbg("/block top level, look for device link");
286                 goto device_link;
287         }
288         if (strncmp(parent_devpath, "/class", 6) == 0) {
289                 pos = strrchr(parent_devpath, '/');
290                 if (pos == &parent_devpath[6] || pos == parent_devpath) {
291                         dbg("/class top level, look for device link");
292                         goto device_link;
293                 }
294         }
295         /* get parent and remember it */
296         dev->parent = sysfs_device_get(parent_devpath);
297         return dev->parent;
298
299 device_link:
300         strlcpy(device_link, sysfs_path, sizeof(device_link));
301         strlcat(device_link, dev->devpath, sizeof(device_link));
302         strlcat(device_link, "/device", sizeof(device_link));
303         len = readlink(device_link, device_link_target, sizeof(device_link_target));
304         if (len < 0)
305                 return NULL;
306         device_link_target[len] = '\0';
307         dbg("device link '%s' points to '%s'", device_link, device_link_target);
308
309         for (back = 0; strncmp(&device_link_target[back * 3], "../", 3) == 0; back++)
310                 ;
311         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
312         dbg("base='%s', tail='%s', back=%i", parent_devpath, &device_link_target[back * 3], back);
313         for (i = 0; i < back; i++) {
314                 pos = strrchr(parent_devpath, '/');
315                 if (pos == NULL)
316                         return NULL;
317                 pos[0] = '\0';
318         }
319         dbg("after moving back '%s'", parent_devpath);
320         strlcat(parent_devpath, "/", sizeof(parent_devpath));
321         strlcat(parent_devpath, &device_link_target[back * 3], sizeof(parent_devpath));
322
323         /* get parent and remember it */
324         dev->parent = sysfs_device_get(parent_devpath);
325         return dev->parent;
326 }
327
328 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
329 {
330         struct sysfs_device *dev_parent;
331
332         dev_parent = sysfs_device_get_parent(dev);
333         while (dev_parent != NULL) {
334                 if (strcmp(dev_parent->subsystem, subsystem) == 0)
335                         return dev_parent;
336                 dev_parent = sysfs_device_get_parent(dev_parent);
337         }
338         return NULL;
339 }
340
341 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
342 {
343         char path_full[PATH_SIZE];
344         const char *path;
345         char value[NAME_SIZE];
346         struct sysfs_attr *attr_loop;
347         struct sysfs_attr *attr;
348         int fd;
349         ssize_t size;
350         size_t sysfs_len;
351
352         dbg("open '%s'/'%s'", devpath, attr_name);
353         sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
354         path = &path_full[sysfs_len];
355         strlcat(path_full, devpath, sizeof(path_full));
356         strlcat(path_full, "/", sizeof(path_full));
357         strlcat(path_full, attr_name, sizeof(path_full));
358
359         /* look for attribute in cache */
360         list_for_each_entry(attr_loop, &attr_list, node) {
361                 if (strcmp(attr_loop->path, path) == 0) {
362                         dbg("found in cache '%s'", attr_loop->path);
363                         return attr_loop->value;
364                 }
365         }
366
367         /* store attribute in cache (also negatives are kept in cache) */
368         dbg("new uncached attribute '%s'", path_full);
369         attr = malloc(sizeof(struct sysfs_attr));
370         if (attr == NULL)
371                 return NULL;
372         memset(attr, 0x00, sizeof(struct sysfs_attr));
373         strlcpy(attr->path, path, sizeof(attr->path));
374         dbg("add to cache '%s'", path_full);
375         list_add(&attr->node, &attr_list);
376
377         /* read attribute value */
378         fd = open(path_full, O_RDONLY);
379         if (fd < 0) {
380                 dbg("attribute '%s' does not exist", path_full);
381                 goto out;
382         }
383         size = read(fd, value, sizeof(value));
384         close(fd);
385         if (size < 0)
386                 goto out;
387         if (size == sizeof(value))
388                 goto out;
389
390         /* got a valid value, store and return it */
391         value[size] = '\0';
392         remove_trailing_chars(value, '\n');
393         dbg("cache '%s' with value '%s'", path_full, value);
394         strlcpy(attr->value_local, value, sizeof(attr->value_local));
395         attr->value = attr->value_local;
396
397 out:
398         return attr->value;
399 }