[libmultipath] update sysfs code
[platform/upstream/multipath-tools.git] / libmultipath / sysfs.c
1 /*
2  * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
3  *
4  *      This program is free software; you can redistribute it and/or modify it
5  *      under the terms of the GNU General Public License as published by the
6  *      Free Software Foundation version 2 of the License.
7  *
8  *      This program is distributed in the hope that it will be useful, but
9  *      WITHOUT ANY WARRANTY; without even the implied warranty of
10  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  *      General Public License for more details.
12  *
13  *      You should have received a copy of the GNU General Public License along
14  *      with this program; if not, write to the Free Software Foundation, Inc.,
15  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16  *
17  */
18
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <sys/stat.h>
28 #include <string.h>
29
30 #include "checkers.h"
31 #include "vector.h"
32 #include "structs.h"
33 #include "sysfs.h"
34 #include "list.h"
35 #include "util.h"
36
37 char sysfs_path[PATH_SIZE];
38
39 /* attribute value cache */
40 static LIST_HEAD(attr_list);
41 struct sysfs_attr {
42         struct list_head node;
43         char path[PATH_SIZE];
44         char *value;                    /* points to value_local if value is cached */
45         char value_local[NAME_SIZE];
46 };
47
48 /* list of sysfs devices */
49 static LIST_HEAD(sysfs_dev_list);
50 struct sysfs_dev {
51         struct list_head node;
52         struct sysfs_device dev;
53 };
54
55 int sysfs_init(char *path, size_t len)
56 {
57         if (path) {
58                 strlcpy(sysfs_path, path, len);
59                 remove_trailing_chars(sysfs_path, '/');
60         } else
61                 strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
62         dbg("sysfs_path='%s'", sysfs_path);
63
64         INIT_LIST_HEAD(&attr_list);
65         INIT_LIST_HEAD(&sysfs_dev_list);
66         return 0;
67 }
68
69 void sysfs_cleanup(void)
70 {
71         struct sysfs_attr *attr_loop;
72         struct sysfs_attr *attr_temp;
73
74         struct sysfs_dev *sysdev_loop;
75         struct sysfs_dev *sysdev_temp;
76
77         list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
78                 list_del(&attr_loop->node);
79                 free(attr_loop);
80         }
81
82         list_for_each_entry_safe(sysdev_loop, sysdev_temp, &sysfs_dev_list, node) {
83                 list_del(&sysdev_loop->node);
84                 free(sysdev_loop);
85         }
86 }
87
88 void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath,
89                              const char *subsystem, const char *driver)
90 {
91         char *pos;
92
93         strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
94         if (subsystem != NULL)
95                 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
96         if (driver != NULL)
97                 strlcpy(dev->driver, driver, sizeof(dev->driver));
98
99         /* set kernel name */
100         pos = strrchr(dev->devpath, '/');
101         if (pos == NULL)
102                 return;
103         strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
104         dbg("kernel='%s'", dev->kernel);
105
106         /* some devices have '!' in their name, change that to '/' */
107         pos = dev->kernel;
108         while (pos[0] != '\0') {
109                 if (pos[0] == '!')
110                         pos[0] = '/';
111                 pos++;
112         }
113
114         /* get kernel number */
115         pos = &dev->kernel[strlen(dev->kernel)];
116         while (isdigit(pos[-1]))
117                 pos--;
118         strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
119         dbg("kernel_number='%s'", dev->kernel_number);
120 }
121
122 int sysfs_resolve_link(char *devpath, size_t size)
123 {
124         char link_path[PATH_SIZE];
125         char link_target[PATH_SIZE];
126         int len;
127         int i;
128         int back;
129
130         strlcpy(link_path, sysfs_path, sizeof(link_path));
131         strlcat(link_path, devpath, sizeof(link_path));
132         len = readlink(link_path, link_target, sizeof(link_target));
133         if (len <= 0)
134                 return -1;
135         link_target[len] = '\0';
136         dbg("path link '%s' points to '%s'", devpath, link_target);
137
138         for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
139                 ;
140         dbg("base '%s', tail '%s', back %i", devpath, &link_target[back * 3], back);
141         for (i = 0; i <= back; i++) {
142                 char *pos = strrchr(devpath, '/');
143
144                 if (pos == NULL)
145                         return -1;
146                 pos[0] = '\0';
147         }
148         dbg("after moving back '%s'", devpath);
149         strlcat(devpath, "/", size);
150         strlcat(devpath, &link_target[back * 3], size);
151         return 0;
152 }
153
154 struct sysfs_device *sysfs_device_get(const char *devpath)
155 {
156         char path[PATH_SIZE];
157         char devpath_real[PATH_SIZE];
158         struct sysfs_device *dev = NULL;
159         struct sysfs_dev *sysdev_loop, *sysdev;
160         struct stat statbuf;
161         char link_path[PATH_SIZE];
162         char link_target[PATH_SIZE];
163         int len;
164         char *pos;
165
166         /* we handle only these devpathes */
167         if (devpath != NULL &&
168             strncmp(devpath, "/devices/", 9) != 0 &&
169             strncmp(devpath, "/subsystem/", 11) != 0 &&
170             strncmp(devpath, "/module/", 8) != 0 &&
171             strncmp(devpath, "/bus/", 5) != 0 &&
172             strncmp(devpath, "/class/", 7) != 0 &&
173             strncmp(devpath, "/block/", 7) != 0) {
174                 dbg("invalid devpath '%s'", devpath);
175                 return NULL;
176         }
177
178         dbg("open '%s'", devpath);
179         strlcpy(devpath_real, devpath, sizeof(devpath_real));
180         remove_trailing_chars(devpath_real, '/');
181         if (devpath[0] == '\0' )
182                 return NULL;
183
184         /* if we got a link, resolve it to the real device */
185         strlcpy(path, sysfs_path, sizeof(path));
186         strlcat(path, devpath_real, sizeof(path));
187         if (lstat(path, &statbuf) != 0) {
188                 /* if stat fails look in the cache */
189                 dbg("stat '%s' failed: %s", path, strerror(errno));
190                 list_for_each_entry(sysdev_loop, &sysfs_dev_list, node) {
191                         if (strcmp(sysdev_loop->dev.devpath, devpath_real) == 0) {
192                                 dbg("found vanished dev in cache '%s'",
193                                     sysdev_loop->dev.devpath);
194                                 return &sysdev_loop->dev;
195                         }
196                 }
197                 return NULL;
198         }
199
200         if (S_ISLNK(statbuf.st_mode)) {
201                 if (sysfs_resolve_link(devpath_real, sizeof(devpath_real)) != 0)
202                         return NULL;
203         }
204
205         list_for_each_entry(sysdev_loop, &sysfs_dev_list, node) {
206                 if (strcmp(sysdev_loop->dev.devpath, devpath_real) == 0) {
207                         dbg("found dev in cache '%s'", sysdev_loop->dev.devpath);
208                         dev = &sysdev_loop->dev;
209                 }
210         }
211
212         if(!dev) {
213                 /* it is a new device */
214                 dbg("new device '%s'", devpath_real);
215                 sysdev = malloc(sizeof(struct sysfs_dev));
216                 if (sysdev == NULL)
217                         return NULL;
218                 memset(sysdev, 0x00, sizeof(struct sysfs_dev));
219                 list_add(&sysdev->node, &sysfs_dev_list);
220                 dev = &sysdev->dev;
221         }
222
223         sysfs_device_set_values(dev, devpath_real, NULL, NULL);
224
225         /* get subsystem name */
226         strlcpy(link_path, sysfs_path, sizeof(link_path));
227         strlcat(link_path, dev->devpath, sizeof(link_path));
228         strlcat(link_path, "/subsystem", sizeof(link_path));
229         len = readlink(link_path, link_target, sizeof(link_target));
230         if (len > 0) {
231                 /* get subsystem from "subsystem" link */
232                 link_target[len] = '\0';
233                 dbg("subsystem link '%s' points to '%s'", link_path, link_target);
234                 pos = strrchr(link_target, '/');
235                 if (pos != NULL)
236                         strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
237         } else if (strstr(dev->devpath, "/drivers/") != NULL) {
238                 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
239         } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
240                 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
241         } else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) {
242                 pos = strrchr(dev->devpath, '/');
243                 if (pos == &dev->devpath[10])
244                         strlcpy(dev->subsystem, "subsystem",
245                                 sizeof(dev->subsystem));
246         } else if (strncmp(dev->devpath, "/class/", 7) == 0) {
247                 pos = strrchr(dev->devpath, '/');
248                 if (pos == &dev->devpath[6])
249                         strlcpy(dev->subsystem, "subsystem",
250                                 sizeof(dev->subsystem));
251         } else if (strncmp(dev->devpath, "/bus/", 5) == 0) {
252                 pos = strrchr(dev->devpath, '/');
253                 if (pos == &dev->devpath[4])
254                         strlcpy(dev->subsystem, "subsystem",
255                                 sizeof(dev->subsystem));
256         }
257
258         /* get driver name */
259         strlcpy(link_path, sysfs_path, sizeof(link_path));
260         strlcat(link_path, dev->devpath, sizeof(link_path));
261         strlcat(link_path, "/driver", sizeof(link_path));
262         len = readlink(link_path, link_target, sizeof(link_target));
263         if (len > 0) {
264                 link_target[len] = '\0';
265                 dbg("driver link '%s' points to '%s'", link_path, link_target);
266                 pos = strrchr(link_target, '/');
267                 if (pos != NULL)
268                         strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
269         }
270
271         return dev;
272 }
273
274 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
275 {
276         char parent_devpath[PATH_SIZE];
277         char *pos;
278
279         dbg("open '%s'", dev->devpath);
280
281         /* look if we already know the parent */
282         if (dev->parent != NULL)
283                 return dev->parent;
284
285         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
286         dbg("'%s'", parent_devpath);
287
288         /* strip last element */
289         pos = strrchr(parent_devpath, '/');
290         if (pos == NULL || pos == parent_devpath)
291                 return NULL;
292         pos[0] = '\0';
293
294         if (strncmp(parent_devpath, "/class", 6) == 0) {
295                 pos = strrchr(parent_devpath, '/');
296                 if (pos == &parent_devpath[6] || pos == parent_devpath) {
297                         dbg("/class top level, look for device link");
298                         goto device_link;
299                 }
300         }
301         if (strcmp(parent_devpath, "/block") == 0) {
302                 dbg("/block top level, look for device link");
303                 goto device_link;
304         }
305
306         /* are we at the top level? */
307         pos = strrchr(parent_devpath, '/');
308         if (pos == NULL || pos == parent_devpath)
309                 return NULL;
310
311         /* get parent and remember it */
312         dev->parent = sysfs_device_get(parent_devpath);
313         return dev->parent;
314
315 device_link:
316         strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
317         strlcat(parent_devpath, "/device", sizeof(parent_devpath));
318         if (sysfs_resolve_link(parent_devpath, sizeof(parent_devpath)) != 0)
319                 return NULL;
320
321         /* get parent and remember it */
322         dev->parent = sysfs_device_get(parent_devpath);
323         return dev->parent;
324 }
325
326 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
327 {
328         struct sysfs_device *dev_parent;
329
330         dev_parent = sysfs_device_get_parent(dev);
331         while (dev_parent != NULL) {
332                 if (strcmp(dev_parent->subsystem, subsystem) == 0)
333                         return dev_parent;
334                 dev_parent = sysfs_device_get_parent(dev_parent);
335         }
336         return NULL;
337 }
338
339 void sysfs_device_put(struct sysfs_device *dev)
340 {
341         struct sysfs_dev *sysdev_loop;
342
343         list_for_each_entry(sysdev_loop, &sysfs_dev_list, node) {
344                 if (&sysdev_loop->dev == dev) {
345                         dbg("removed dev '%s' from cache",
346                             sysdev_loop->dev.devpath);
347                         list_del(&sysdev_loop->node);
348                         free(sysdev_loop);
349                         return;
350                 }
351         }
352         dbg("dev '%s' not found in cache",
353             sysdev_loop->dev.devpath);
354
355         return;
356 }
357
358 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
359 {
360         char path_full[PATH_SIZE];
361         const char *path;
362         char value[NAME_SIZE];
363         struct sysfs_attr *attr_loop;
364         struct sysfs_attr *attr = NULL;
365         struct stat statbuf;
366         int fd;
367         ssize_t size;
368         size_t sysfs_len;
369
370         dbg("open '%s'/'%s'", devpath, attr_name);
371         sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
372         if(sysfs_len >= sizeof(path_full))
373                 sysfs_len = sizeof(path_full) - 1;
374         path = &path_full[sysfs_len];
375         strlcat(path_full, devpath, sizeof(path_full));
376         strlcat(path_full, "/", sizeof(path_full));
377         strlcat(path_full, attr_name, sizeof(path_full));
378
379         /* look for attribute in cache */
380         list_for_each_entry(attr_loop, &attr_list, node) {
381                 if (strcmp(attr_loop->path, path) == 0) {
382                         dbg("found in cache '%s'", attr_loop->path);
383                         attr = attr_loop;
384                 }
385         }
386         if (!attr) {
387                 /* store attribute in cache */
388                 dbg("new uncached attribute '%s'", path_full);
389                 attr = malloc(sizeof(struct sysfs_attr));
390                 if (attr == NULL)
391                         return NULL;
392                 memset(attr, 0x00, sizeof(struct sysfs_attr));
393                 strlcpy(attr->path, path, sizeof(attr->path));
394                 dbg("add to cache '%s'", path_full);
395                 list_add(&attr->node, &attr_list);
396         } else {
397                 /* clear old value */
398                 if(attr->value)
399                         memset(attr->value, 0x00, sizeof(attr->value));
400         }
401
402         if (lstat(path_full, &statbuf) != 0) {
403                 dbg("stat '%s' failed: %s", path_full, strerror(errno));
404                 goto out;
405         }
406
407         if (S_ISLNK(statbuf.st_mode)) {
408                 /* links return the last element of the target path */
409                 char link_target[PATH_SIZE];
410                 int len;
411                 const char *pos;
412
413                 len = readlink(path_full, link_target, sizeof(link_target));
414                 if (len > 0) {
415                         link_target[len] = '\0';
416                         pos = strrchr(link_target, '/');
417                         if (pos != NULL) {
418                                 dbg("cache '%s' with link value '%s'",
419                                     path_full, value);
420                                 strlcpy(attr->value_local, &pos[1],
421                                         sizeof(attr->value_local));
422                                 attr->value = attr->value_local;
423                         }
424                 }
425                 goto out;
426         }
427
428         /* skip directories */
429         if (S_ISDIR(statbuf.st_mode))
430                 goto out;
431
432         /* skip non-readable files */
433         if ((statbuf.st_mode & S_IRUSR) == 0)
434                 goto out;
435
436         /* read attribute value */
437         fd = open(path_full, O_RDONLY);
438         if (fd < 0) {
439                 dbg("attribute '%s' can not be opened: %s",
440                     path_full, strerror(errno));
441                 goto out;
442         }
443         size = read(fd, value, sizeof(value));
444         close(fd);
445         if (size < 0)
446                 goto out;
447         if (size == sizeof(value))
448                 goto out;
449
450         /* got a valid value, store and return it */
451         value[size] = '\0';
452         remove_trailing_chars(value, '\n');
453         dbg("cache '%s' with attribute value '%s'", path_full, value);
454         strlcpy(attr->value_local, value, sizeof(attr->value_local));
455         attr->value = attr->value_local;
456
457 out:
458         return attr->value;
459 }
460
461 int sysfs_lookup_devpath_by_subsys_id(char *devpath_full, size_t len,
462                                       const char *subsystem, const char *id)
463 {
464         size_t sysfs_len;
465         char path_full[PATH_SIZE];
466         char *path;
467         struct stat statbuf;
468
469         sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
470         path = &path_full[sysfs_len];
471
472         if (strcmp(subsystem, "subsystem") == 0) {
473                 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
474                 strlcat(path, id, sizeof(path_full) - sysfs_len);
475                 if (stat(path_full, &statbuf) == 0)
476                         goto found;
477
478                 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
479                 strlcat(path, id, sizeof(path_full) - sysfs_len);
480                 if (stat(path_full, &statbuf) == 0)
481                         goto found;
482                 goto out;
483
484                 strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
485                 strlcat(path, id, sizeof(path_full) - sysfs_len);
486                 if (stat(path_full, &statbuf) == 0)
487                         goto found;
488         }
489
490         if (strcmp(subsystem, "module") == 0) {
491                 strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
492                 strlcat(path, id, sizeof(path_full) - sysfs_len);
493                 if (stat(path_full, &statbuf) == 0)
494                         goto found;
495                 goto out;
496         }
497
498         if (strcmp(subsystem, "drivers") == 0) {
499                 char subsys[NAME_SIZE];
500                 char *driver;
501
502                 strlcpy(subsys, id, sizeof(subsys));
503                 driver = strchr(subsys, ':');
504                 if (driver != NULL) {
505                         driver[0] = '\0';
506                         driver = &driver[1];
507                         strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
508                         strlcat(path, subsys, sizeof(path_full) - sysfs_len);
509                         strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
510                         strlcat(path, driver, sizeof(path_full) - sysfs_len);
511                         if (stat(path_full, &statbuf) == 0)
512                                 goto found;
513
514                         strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
515                         strlcat(path, subsys, sizeof(path_full) - sysfs_len);
516                         strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
517                         strlcat(path, driver, sizeof(path_full) - sysfs_len);
518                         if (stat(path_full, &statbuf) == 0)
519                                 goto found;
520                 }
521                 goto out;
522         }
523
524         strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
525         strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
526         strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
527         strlcat(path, id, sizeof(path_full) - sysfs_len);
528         if (stat(path_full, &statbuf) == 0)
529                 goto found;
530
531         strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
532         strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
533         strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
534         strlcat(path, id, sizeof(path_full) - sysfs_len);
535         if (stat(path_full, &statbuf) == 0)
536                 goto found;
537
538         strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
539         strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
540         strlcat(path, "/", sizeof(path_full) - sysfs_len);
541         strlcat(path, id, sizeof(path_full) - sysfs_len);
542         if (stat(path_full, &statbuf) == 0)
543                 goto found;
544 out:
545         return 0;
546 found:
547         if (S_ISLNK(statbuf.st_mode))
548                 sysfs_resolve_link(path, sizeof(path_full) - sysfs_len);
549         strlcpy(devpath_full, path, len);
550         return 1;
551 }