chiark / gitweb /
libsysfs: translate devpath of the symlinked class devices to its real path
[elogind.git] / libsysfs / sysfs_class.c
1 /*
2  * sysfs_class.c
3  *
4  * Generic class utility functions for libsysfs
5  *
6  * Copyright (C) IBM Corp. 2003-2005
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23 #include "libsysfs.h"
24 #include "sysfs.h"
25
26 /**
27  * sysfs_close_class_device: closes a single class device.
28  * @dev: class device to close.
29  */
30 void sysfs_close_class_device(struct sysfs_class_device *dev)
31 {
32         if (dev) {
33                 if (dev->parent)
34                         sysfs_close_class_device(dev->parent);
35                 if (dev->sysdevice)
36                         sysfs_close_device(dev->sysdevice);
37                 if (dev->attrlist)
38                         dlist_destroy(dev->attrlist);
39                 free(dev);
40         }
41 }
42
43 static void sysfs_close_cls_dev(void *dev)
44 {
45         sysfs_close_class_device((struct sysfs_class_device *)dev);
46 }
47
48 /**
49  * sysfs_close_class: close the given class
50  * @cls: sysfs_class to close
51  */ 
52 void sysfs_close_class(struct sysfs_class *cls)
53 {
54         if (cls) {
55                 if (cls->devices)
56                         dlist_destroy(cls->devices);
57                 if (cls->attrlist)
58                         dlist_destroy(cls->attrlist);
59                 free(cls);
60         }
61 }
62
63 static int cdev_name_equal(void *a, void *b)
64 {
65         if (!a || !b)
66                 return 0;
67
68         if (strncmp((char *)a, ((struct sysfs_class_device *)b)->name, 
69                                 strlen((char *)a)) == 0)
70                 return 1;
71
72         return 0;
73 }
74
75 static struct sysfs_class *alloc_class(void)
76 {
77         return (struct sysfs_class *) calloc(1, sizeof(struct sysfs_class));
78 }
79
80 /**
81  * alloc_class_device: mallocs and initializes new class device struct.
82  * returns sysfs_class_device or NULL.
83  */
84 static struct sysfs_class_device *alloc_class_device(void)
85 {
86         struct sysfs_class_device *dev;
87
88         dev = calloc(1, sizeof(struct sysfs_class_device));
89         return dev;
90 }
91
92 /**
93  * set_classdev_classname: Grabs classname from path
94  * @cdev: class device to set
95  * Returns nothing
96  */
97 static void set_classdev_classname(struct sysfs_class_device *cdev)
98 {
99         char *c, *e;
100         int count = 0;
101
102         c = strstr(cdev->path, SYSFS_CLASS_NAME);
103         if (c == NULL) {
104                 c = strstr(cdev->path, SYSFS_BLOCK_NAME);
105         } else {
106                 c = strstr(c, "/");
107         }
108
109         if (c == NULL)
110                 safestrcpy(cdev->classname, SYSFS_UNKNOWN);
111         else {
112                 if (*c == '/')
113                         c++;
114                 e = c;
115                 while (e != NULL && *e != '/' && *e != '\0') {
116                         e++;
117                         count++;
118                 }
119                 strncpy(cdev->classname, c, count);
120         }
121 }
122
123 /**
124  * sysfs_open_class_device_path: Opens and populates class device
125  * @path: path to class device.
126  * returns struct sysfs_class_device with success and NULL with error.
127  */
128 struct sysfs_class_device *sysfs_open_class_device_path(const char *path)
129 {
130         struct sysfs_class_device *cdev;
131         char temp_path[SYSFS_PATH_MAX];
132
133         if (!path) {
134                 errno = EINVAL;
135                 return NULL;
136         }
137
138         /*
139          * Post linux-2.6.14 driver model supports nested classes with
140          * links to the nested hierarchy at /sys/class/xxx/. Check for
141          * a link to the actual class device if a directory isn't found
142          */
143         if (sysfs_path_is_dir(path)) {
144                 dprintf("%s: Directory not found, checking for a link\n", path);
145                 if (!sysfs_path_is_link(path)) {
146                         if (sysfs_get_link(path, temp_path, SYSFS_PATH_MAX)) {
147                                 dprintf("Error retrieving link at %s\n", path);
148                                 return NULL;
149                         }
150                 } else {
151                         dprintf("%s is not a valid class device path\n", path);
152                         return NULL;
153                 }
154         } else
155                 safestrcpy(temp_path, path);
156
157         cdev = alloc_class_device();
158         if (!cdev) {
159                 dprintf("calloc failed\n");
160                 return NULL;
161         }
162         if (sysfs_get_name_from_path(temp_path, cdev->name, SYSFS_NAME_LEN)) {
163                 errno = EINVAL;
164                 dprintf("Error getting class device name\n");
165                 sysfs_close_class_device(cdev);
166                 return NULL;
167         }
168
169         safestrcpy(cdev->path, temp_path);
170         if (sysfs_remove_trailing_slash(cdev->path)) {
171                 dprintf("Invalid path to class device %s\n", cdev->path);
172                 sysfs_close_class_device(cdev);
173                 return NULL;
174         }
175         set_classdev_classname(cdev);
176
177         return cdev;
178 }
179
180 /** 
181  * get_blockdev_parent: Get the parent class device for a "block" subsystem 
182  *              device if present
183  * @clsdev: block subsystem class device whose parent needs to be found
184  * Returns 0 on success and 1 on error
185  */
186 static int get_blockdev_parent(struct sysfs_class_device *clsdev)
187 {
188         char parent_path[SYSFS_PATH_MAX];
189         char *c;
190
191         safestrcpy(parent_path, clsdev->path);
192         c = strstr(parent_path, SYSFS_BLOCK_NAME);
193         if (c == NULL) {
194                 dprintf("Class device %s does not belong to BLOCK subsystem\n",
195                                 clsdev->name);
196                 return 1;
197         }
198         c += strlen(SYSFS_BLOCK_NAME);
199         if (*c == '/')
200                 c++;
201         else
202                 goto errout;
203
204         /* validate whether the given class device is a partition or not */
205         if ((strncmp(c, clsdev->name, strlen(clsdev->name))) == 0) {
206                 dprintf("%s not a partition\n", clsdev->name);
207                 return 1;
208         }
209
210         c = strchr(c, '/');
211         if (c == NULL)
212                 goto errout;
213
214         *c = '\0';
215
216         clsdev->parent = sysfs_open_class_device_path(parent_path);
217         if (!clsdev->parent) {
218                 dprintf("Error opening the parent class device at %s\n", 
219                                                                 parent_path);
220                 return 1;
221         }
222         return 0;
223
224 errout:
225         dprintf("Invalid path %s\n", clsdev->path);
226         return 1;
227 }
228
229 /**
230  * sysfs_get_classdev_parent: Retrieves the parent of a class device. 
231  *      eg., when working with hda1, this function can be used to retrieve the
232  *              sysfs_class_device for hda
233  *              
234  * @clsdev: class device whose parent details are required.
235  * Returns sysfs_class_device of the parent on success, NULL on failure
236  */ 
237 struct sysfs_class_device *sysfs_get_classdev_parent
238                                 (struct sysfs_class_device *clsdev)
239 {
240         if (!clsdev) {
241                 errno = EINVAL;
242                 return NULL;
243         }
244         if (clsdev->parent)
245                 return (clsdev->parent);
246
247         /*
248          * As of now, only block devices have a parent child heirarchy in sysfs
249          * We do not know, if, in the future, more classes will have a similar
250          * structure. Hence, we now call a specialized function for block and
251          * later we can add support functions for other subsystems as required.
252          */
253         if (!(strncmp(clsdev->classname, SYSFS_BLOCK_NAME, 
254                                         sizeof(SYSFS_BLOCK_NAME)))) {
255                 if ((get_blockdev_parent(clsdev)) == 0) 
256                         return (clsdev->parent);
257         }
258         return NULL;
259 }
260
261 /**
262  * get_classdev_path: given the class and a device in the class, return the
263  *              absolute path to the device
264  * @classname: name of the class
265  * @clsdev: the class device
266  * @path: buffer to return path
267  * @psize: size of "path"
268  * Returns 0 on SUCCESS or -1 on error
269  */
270 static int get_classdev_path(const char *classname, const char *clsdev, 
271                 char *path, size_t len)
272 {
273         if (!classname || !clsdev || !path) {
274                 errno = EINVAL;
275                 return -1;
276         }
277         if (sysfs_get_mnt_path(path, len) != 0) {
278                 dprintf("Error getting sysfs mount path\n");
279                 return -1;
280         }
281         if (strncmp(classname, SYSFS_BLOCK_NAME,
282                                 sizeof(SYSFS_BLOCK_NAME)) == 0) {
283                 safestrcatmax(path, "/", len);
284                 safestrcatmax(path, SYSFS_BLOCK_NAME, len);
285         } else {
286                 safestrcatmax(path, "/", len);
287                 safestrcatmax(path, SYSFS_CLASS_NAME, len);
288                 safestrcatmax(path, "/", len);
289                 safestrcatmax(path, classname, len);
290         }
291         safestrcatmax(path, "/", len);
292         safestrcatmax(path, clsdev, len);
293         return 0;
294 }
295
296 /**
297  * sysfs_open_class_device: Locates a specific class_device and returns it.
298  * Class_device must be closed using sysfs_close_class_device
299  * @classname: Class to search
300  * @name: name of the class_device
301  * 
302  * NOTE:
303  *      Call sysfs_close_class_device() to close the class device
304  */
305 struct sysfs_class_device *sysfs_open_class_device
306                 (const char *classname, const char *name)
307 {
308         char devpath[SYSFS_PATH_MAX];
309         struct sysfs_class_device *cdev;
310
311         if (!classname || !name) {
312                 errno = EINVAL;
313                 return NULL;
314         }
315         
316         memset(devpath, 0, SYSFS_PATH_MAX);
317         if ((get_classdev_path(classname, name, devpath, 
318                                         SYSFS_PATH_MAX)) != 0) {
319                 dprintf("Error getting to device %s on class %s\n",
320                                                         name, classname);
321                 return NULL;
322         }
323
324         cdev = sysfs_open_class_device_path(devpath);
325         if (!cdev) {
326                 dprintf("Error getting class device %s from class %s\n",
327                                 name, classname);
328                 return NULL;
329         }
330         return cdev;
331 }
332
333 /**
334  * sysfs_get_classdev_attr: searches class device's attributes by name
335  * @clsdev: class device to look through
336  * @name: attribute name to get
337  * returns sysfs_attribute reference with success or NULL with error
338  */
339 struct sysfs_attribute *sysfs_get_classdev_attr
340                 (struct sysfs_class_device *clsdev, const char *name)
341 {
342         if (!clsdev || !name) {
343                 errno = EINVAL;
344                 return NULL;
345         }
346         return get_attribute(clsdev, (char *)name);
347 }
348
349 /**
350  * sysfs_get_classdev_attributes: gets list of classdev attributes
351  * @clsdev: class device whose attributes list is needed
352  * returns dlist of attributes on success or NULL on error
353  */
354 struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *clsdev)
355 {
356         if (!clsdev) {
357                 errno = EINVAL;
358                 return NULL;
359         }
360         return get_attributes_list(clsdev);
361 }
362
363 /**
364  * sysfs_get_classdev_device: gets the sysfs_device associated with the
365  *      given sysfs_class_device
366  * @clsdev: class device whose associated sysfs_device is needed
367  * returns struct sysfs_device * on success or NULL on error
368  */
369 struct sysfs_device *sysfs_get_classdev_device
370                 (struct sysfs_class_device *clsdev)
371 {
372         char linkpath[SYSFS_PATH_MAX], devpath[SYSFS_PATH_MAX];
373
374         if (!clsdev) {
375                 errno = EINVAL;
376                 return NULL;
377         }
378
379         if (clsdev->sysdevice)
380                 return clsdev->sysdevice;
381
382         memset(linkpath, 0, SYSFS_PATH_MAX);
383         safestrcpy(linkpath, clsdev->path);
384         safestrcat(linkpath, "/device");
385         if (!sysfs_path_is_link(linkpath)) {
386                 memset(devpath, 0, SYSFS_PATH_MAX);
387                 if (!sysfs_get_link(linkpath, devpath, SYSFS_PATH_MAX))
388                         clsdev->sysdevice = sysfs_open_device_path(devpath);
389         }
390         return clsdev->sysdevice;
391 }
392
393 /**
394  * sysfs_open_class: opens specific class and all its devices on system
395  * returns sysfs_class structure with success or NULL with error.
396  */
397 struct sysfs_class *sysfs_open_class(const char *name)
398 {
399         struct sysfs_class *cls = NULL;
400         char classpath[SYSFS_PATH_MAX];
401
402         if (!name) {
403                 errno = EINVAL;
404                 return NULL;
405         }
406
407         memset(classpath, 0, SYSFS_PATH_MAX);
408         if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) {
409                 dprintf("Sysfs not supported on this system\n");
410                 return NULL;
411         }
412
413         /* 
414          * We shall now treat "block" also as a class. Hence, check here
415          * if "name" is "block" and proceed accordingly
416          */
417         if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
418                 safestrcat(classpath, "/");
419                 safestrcat(classpath, SYSFS_BLOCK_NAME);
420         } else {
421                 safestrcat(classpath, "/");
422                 safestrcat(classpath, SYSFS_CLASS_NAME);
423                 safestrcat(classpath, "/");
424                 safestrcat(classpath, name);
425         }
426         if (sysfs_path_is_dir(classpath)) {
427                 dprintf("Class %s not found on the system\n", name);
428                 return NULL;
429         }
430
431         cls = alloc_class();
432         if (cls == NULL) {
433                 dprintf("calloc failed\n");
434                 return NULL;
435         }
436         safestrcpy(cls->name, name);    
437         safestrcpy(cls->path, classpath);
438         if ((sysfs_remove_trailing_slash(cls->path)) != 0) {
439                 dprintf("Invalid path to class device %s\n", cls->path);
440                 sysfs_close_class(cls);
441                 return NULL;
442         }
443
444         return cls;
445 }
446
447 /**
448  * sysfs_get_class_device: get specific class device using the device's id
449  * @cls: sysfs_class to find the device on
450  * @name: name of the class device to look for
451  * 
452  * Returns sysfs_class_device * on success and NULL on failure
453  */ 
454 struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *cls,
455                 const char *name)
456 {
457         char path[SYSFS_PATH_MAX];
458         struct sysfs_class_device *cdev = NULL;
459
460         if (!cls || !name) {
461                 errno = EINVAL;
462                 return NULL;
463         }
464
465         if (cls->devices) {
466                 cdev = (struct sysfs_class_device *)dlist_find_custom
467                         (cls->devices, (void *)name, cdev_name_equal);
468                 if (cdev)
469                         return cdev;
470         }
471
472         safestrcpy(path, cls->path);
473         safestrcat(path, "/");
474         safestrcat(path, name);
475         cdev = sysfs_open_class_device_path(path);
476         if (!cdev) {
477                 dprintf("Error opening class device at %s\n", path);
478                 return NULL;
479         }
480         if (!cls->devices)
481                 cls->devices = dlist_new_with_delete
482                         (sizeof(struct sysfs_class_device),
483                                  sysfs_close_cls_dev);
484         
485         dlist_unshift_sorted(cls->devices, cdev, sort_list);
486         return cdev;
487 }
488
489 /**
490  * Add class devices to list
491  */
492 static void add_cdevs_to_classlist(struct sysfs_class *cls, struct dlist *list)
493 {
494         char path[SYSFS_PATH_MAX], *cdev_name;
495         struct sysfs_class_device *cdev = NULL;
496
497         if (cls == NULL || list == NULL)
498                 return;
499
500         dlist_for_each_data(list, cdev_name, char) {
501                 if (cls->devices) {
502                         cdev = (struct sysfs_class_device *)
503                                 dlist_find_custom(cls->devices,
504                                 (void *)cdev_name, cdev_name_equal);
505                         if (cdev)
506                                 continue;
507                 }
508                 safestrcpy(path, cls->path);
509                 safestrcat(path, "/");
510                 safestrcat(path, cdev_name);
511                 cdev = sysfs_open_class_device_path(path);
512                 if (cdev) {
513                         if (!cls->devices)
514                                 cls->devices = dlist_new_with_delete
515                                 (sizeof(struct sysfs_class_device),
516                                  sysfs_close_cls_dev);
517                         dlist_unshift_sorted(cls->devices, cdev,
518                                         sort_list);
519                 }
520         }
521 }
522
523 /**
524  * sysfs_get_class_devices: get all class devices in the given class
525  * @cls: sysfs_class whose devices list is needed
526  *
527  * Returns a dlist of sysfs_class_device * on success and NULL on failure
528  */
529 struct dlist *sysfs_get_class_devices(struct sysfs_class *cls)
530 {
531         char path[SYSFS_PATH_MAX];
532         struct dlist *dirlist, *linklist;
533
534         if (!cls) {
535                 errno = EINVAL;
536                 return NULL;
537         }
538
539         /*
540          * Post linux-2.6.14, we have nested classes and links under
541          * /sys/class/xxx/. are also valid class devices
542          */
543         safestrcpy(path, cls->path);
544         dirlist = read_dir_subdirs(path);
545         if (dirlist) {
546                 add_cdevs_to_classlist(cls, dirlist);
547                 sysfs_close_list(dirlist);
548         }
549
550         linklist = read_dir_links(path);
551         if (linklist) {
552                 add_cdevs_to_classlist(cls, linklist);
553                 sysfs_close_list(linklist);
554         }
555
556         return cls->devices;
557 }