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