chiark / gitweb /
4ea7f41c99c293410482acfed888498e47f79d27
[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
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 static void sysfs_close_cls_dev(void *dev)
27 {
28         sysfs_close_class_device((struct sysfs_class_device *)dev);
29 }
30
31 /**
32  * class_name_equal: compares class_devices' name
33  * @a: class_name looking for
34  * @b: sysfs_class_device being compared
35  */
36 static int class_name_equal(void *a, void *b)
37 {
38         if (a == NULL || b == NULL)
39                 return 0;
40
41         if (strcmp(((char *)a), ((struct sysfs_class_device *)b)->name) == 0)
42                 return 1;
43
44         return 0;
45 }
46
47 /**
48  * sysfs_close_class_device: closes a single class device.
49  * @dev: class device to close.
50  */
51 void sysfs_close_class_device(struct sysfs_class_device *dev)
52 {
53         if (dev != NULL) {
54                 if (dev->directory != NULL)
55                         sysfs_close_directory(dev->directory);
56                 if (dev->sysdevice != NULL)
57                         sysfs_close_device(dev->sysdevice);
58                 if (dev->driver != NULL)
59                         sysfs_close_driver(dev->driver);
60                 if (dev->parent != NULL)
61                         sysfs_close_class_device(dev->parent);
62                 free(dev);
63         }
64 }
65
66 /**
67  * sysfs_close_class: close single class
68  * @cls: class structure
69  */
70 void sysfs_close_class(struct sysfs_class *cls)
71 {
72         if (cls != NULL) {
73                 if (cls->directory != NULL)
74                         sysfs_close_directory(cls->directory);
75                 if (cls->devices != NULL) 
76                         dlist_destroy(cls->devices);
77                 free(cls);
78         }
79 }
80
81 /**
82  * alloc_class_device: mallocs and initializes new class device struct.
83  * returns sysfs_class_device or NULL.
84  */
85 static struct sysfs_class_device *alloc_class_device(void)
86 {
87         return (struct sysfs_class_device *)
88                                 calloc(1, sizeof(struct sysfs_class_device));
89 }
90
91 /**
92  * alloc_class: mallocs new class structure
93  * returns sysfs_class struct or NULL
94  */
95 static struct sysfs_class *alloc_class(void)
96 {
97         return (struct sysfs_class *)calloc(1, sizeof(struct sysfs_class));
98 }
99
100 /** 
101  * set_classdev_classname: Grabs classname from path
102  * @cdev: class device to set
103  * Returns nothing
104  */
105 static void set_classdev_classname(struct sysfs_class_device *cdev)
106 {
107         char *c = NULL, *e = NULL;
108         int count = 0;
109
110         c = strstr(cdev->path, SYSFS_CLASS_NAME);
111         if (c == NULL) {
112                 c = strstr(cdev->path, SYSFS_BLOCK_NAME);
113         } else {
114                 c = strchr(c, '/');
115         }
116
117         if (c == NULL)
118                 safestrcpy(cdev->classname, SYSFS_UNKNOWN);
119         else {
120                 if (*c == '/')
121                         c++;
122                 e = c;
123                 while (e != NULL && *e != '/' && *e != '\0') {
124                         e++;
125                         count++;
126                 }
127                 strncpy(cdev->classname, c, count);
128         }
129 }
130
131 /**
132  * sysfs_open_class_device_path: Opens and populates class device
133  * @path: path to class device.
134  * returns struct sysfs_class_device with success and NULL with error.
135  */
136 struct sysfs_class_device *sysfs_open_class_device_path(const char *path)
137 {
138         struct sysfs_class_device *cdev = NULL;
139
140         if (path == NULL) {
141                 errno = EINVAL;
142                 return NULL;
143         }
144         if ((sysfs_path_is_dir(path)) != 0) {
145                 dprintf("%s is not a valid path to a class device\n", path);
146                 return NULL;
147         }
148         cdev = alloc_class_device();
149         if (cdev == NULL) {
150                 dprintf("calloc failed\n");
151                 return NULL;
152         }
153         if ((sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) != 0) {
154                 errno = EINVAL;
155                 dprintf("Error getting class device name\n");
156                 sysfs_close_class_device(cdev);
157                 return NULL;
158         }
159
160         safestrcpy(cdev->path, path);
161         if ((sysfs_remove_trailing_slash(cdev->path)) != 0) {
162                 dprintf("Invalid path to class device %s\n", cdev->path);
163                 sysfs_close_class_device(cdev);
164                 return NULL;
165         }
166         set_classdev_classname(cdev);
167
168         return cdev;
169 }
170
171 /**
172  * sysfs_get_class_devices: gets all devices for class
173  * @cls: class to get devices for
174  * returns dlist of class_devices with success and NULL with error
175  */
176 struct dlist *sysfs_get_class_devices(struct sysfs_class *cls)
177 {
178         struct sysfs_class_device *dev = NULL;
179         struct sysfs_directory *cur = NULL;
180
181         if (cls == NULL) {
182                 errno = EINVAL;
183                 return NULL;
184         }
185
186         if (cls->devices != NULL) 
187                 return cls->devices;
188
189         if (cls->directory == NULL) {
190                 cls->directory = sysfs_open_directory(cls->path);
191                 if (cls->directory == NULL) 
192                         return NULL;
193         }
194
195         if ((sysfs_read_dir_subdirs(cls->directory)) != 0) 
196                 return NULL;
197
198         if (cls->directory->subdirs != NULL) {
199                 dlist_for_each_data(cls->directory->subdirs, cur, 
200                                                 struct sysfs_directory) {
201                         dev = sysfs_open_class_device_path(cur->path);
202                         if (dev == NULL) {
203                                 dprintf("Error opening device at %s\n", 
204                                                                 cur->path);
205                                 continue;
206                         }
207                         if (cls->devices == NULL)
208                                 cls->devices = dlist_new_with_delete
209                                         (sizeof(struct sysfs_class_device),
210                                                         sysfs_close_cls_dev);
211                         dlist_unshift_sorted(cls->devices, dev, sort_list);
212                 }
213         }
214         return cls->devices;
215 }
216
217 /**
218  * sysfs_open_class: opens specific class and all its devices on system
219  * returns sysfs_class structure with success or NULL with error.
220  */
221 struct sysfs_class *sysfs_open_class(const char *name)
222 {
223         struct sysfs_class *cls = NULL;
224         char classpath[SYSFS_PATH_MAX];
225
226         if (name == NULL) {
227                 errno = EINVAL;
228                 return NULL;
229         }
230
231         memset(classpath, 0, SYSFS_PATH_MAX);
232         if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) {
233                 dprintf("Sysfs not supported on this system\n");
234                 return NULL;
235         }
236
237         /* 
238          * We shall now treat "block" also as a class. Hence, check here
239          * if "name" is "block" and proceed accordingly
240          */
241         if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
242                 safestrcat(classpath, "/");
243                 safestrcat(classpath, SYSFS_BLOCK_NAME);
244         } else {
245                 safestrcat(classpath, "/");
246                 safestrcat(classpath, SYSFS_CLASS_NAME);
247                 safestrcat(classpath, "/");
248                 safestrcat(classpath, name);
249         }
250         if ((sysfs_path_is_dir(classpath)) != 0) {
251                 dprintf("Class %s not found on the system\n", name);
252                 return NULL;
253         }
254
255         cls = alloc_class();
256         if (cls == NULL) {
257                 dprintf("calloc failed\n");
258                 return NULL;
259         }
260         safestrcpy(cls->name, name);    
261         safestrcpy(cls->path, classpath);
262         if ((sysfs_remove_trailing_slash(cls->path)) != 0) {
263                 dprintf("Invalid path to class device %s\n", cls->path);
264                 sysfs_close_class(cls);
265                 return NULL;
266         }
267         
268         return cls;
269 }
270
271 /**
272  * sysfs_get_class_device: Get specific class device using the device's id
273  * @class: class to find device on
274  * @name: class name of the device
275  */ 
276 struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *cls,
277                                         char *name)
278 {
279         if (cls == NULL || name == NULL) {
280                 errno = EINVAL;
281                 return NULL;
282         }
283
284         if (cls->devices == NULL) {
285                 cls->devices = sysfs_get_class_devices(cls);
286                 if (cls->devices == NULL) 
287                         return NULL;
288         }
289         return (struct sysfs_class_device *)dlist_find_custom(cls->devices,
290                         name, class_name_equal);
291 }
292
293 /**
294  * sysfs_get_classdev_device: returns the sysfs_device corresponding to
295  *              sysfs_class_device, if present
296  * @clsdev: class device whose sysfs_device is required
297  * Returns sysfs_device on success, NULL on error or if device is not
298  * implemented
299  */ 
300 struct sysfs_device *sysfs_get_classdev_device
301                         (struct sysfs_class_device *clsdev)
302 {
303         struct sysfs_link *devlink = NULL;
304         char devpath[SYSFS_PATH_MAX];
305         
306         if (clsdev == NULL) {
307                 errno = EINVAL;
308                 return NULL;
309         }
310         safestrcpy(devpath, clsdev->path);
311         safestrcat(devpath, "/device");
312         if ((sysfs_path_is_link(devpath)) != 0) {
313                 if (clsdev->sysdevice != NULL) {
314                         sysfs_close_device(clsdev->sysdevice);
315                         clsdev->sysdevice = NULL;
316                 }
317                 return NULL;
318         }
319         
320         if (clsdev->directory == NULL) {
321                 clsdev->directory = sysfs_open_directory(clsdev->path);
322                 if (clsdev->directory == NULL)
323                         return NULL;
324         }
325         devlink = sysfs_get_directory_link(clsdev->directory, "device");
326         if (devlink == NULL) {
327                 if (clsdev->sysdevice != NULL) {
328                         dprintf("Device link no longer exists\n");
329                         sysfs_close_device(clsdev->sysdevice);
330                         clsdev->sysdevice = NULL;
331                 }
332                 return NULL;
333         }
334
335         if (clsdev->sysdevice != NULL) {
336                 if (!strncmp(devlink->target, clsdev->sysdevice->path,
337                                                 SYSFS_PATH_MAX)) 
338                         /* sysdevice hasn't changed */
339                         return (clsdev->sysdevice);
340                 else 
341                         /* come here only if the device link for has changed */
342                         sysfs_close_device(clsdev->sysdevice);
343         }
344
345         clsdev->sysdevice = sysfs_open_device_path(devlink->target);
346         if (clsdev->sysdevice == NULL)
347                 return NULL;
348
349         return (clsdev->sysdevice);
350 }
351                                 
352 /**
353  * sysfs_get_classdev_driver: returns the sysfs_driver corresponding to
354  *              sysfs_class_device, if present
355  * @clsdev: class device whose sysfs_device is required
356  * Returns sysfs_driver on success, NULL on error or if driver is not
357  * implemented
358  */ 
359 struct sysfs_driver *sysfs_get_classdev_driver
360                         (struct sysfs_class_device *clsdev)
361 {
362         struct sysfs_link *drvlink = NULL;
363         char drvpath[SYSFS_PATH_MAX];
364         
365         if (clsdev == NULL) {
366                 errno = EINVAL;
367                 return NULL;
368         }
369         safestrcpy(drvpath, clsdev->path);
370         safestrcat(drvpath, "/driver");
371         if ((sysfs_path_is_link(drvpath)) != 0) {
372                 if (clsdev->driver != NULL) {
373                         sysfs_close_driver(clsdev->driver);
374                         clsdev->driver = NULL;
375                 }
376                 return NULL;
377         }
378          
379         if (clsdev->directory == NULL) {
380                 clsdev->directory = sysfs_open_directory(clsdev->path);
381                 if (clsdev->directory == NULL)
382                         return NULL;
383         }
384         drvlink = sysfs_get_directory_link(clsdev->directory, "driver");
385         if (drvlink == NULL) {
386                 if (clsdev->driver != NULL) {
387                         dprintf("Driver link no longer exists\n");
388                         sysfs_close_driver(clsdev->driver);
389                         clsdev->driver = NULL;
390                 }
391                 return NULL;
392         }
393         if (clsdev->driver != NULL) {
394                 if (!strncmp(drvlink->target, clsdev->driver->path,
395                                                         SYSFS_PATH_MAX))
396                         /* driver hasn't changed */
397                         return (clsdev->driver);
398                 else
399                         /* come here only if the device link for has changed */
400                         sysfs_close_driver(clsdev->driver);
401         }
402                 
403         clsdev->driver = sysfs_open_driver_path(drvlink->target);
404         if (clsdev->driver == NULL)
405                 return NULL;
406
407         return (clsdev->driver);
408 }
409
410 /** 
411  * get_blockdev_parent: Get the parent class device for a "block" subsystem 
412  *              device if present
413  * @clsdev: block subsystem class device whose parent needs to be found
414  * Returns 0 on success and 1 on error
415  */
416 static int get_blockdev_parent(struct sysfs_class_device *clsdev)
417 {
418         char parent_path[SYSFS_PATH_MAX], *c = NULL;
419
420         safestrcpy(parent_path, clsdev->path);
421         c = strstr(parent_path, SYSFS_BLOCK_NAME);
422         if (c == NULL) {
423                 dprintf("Class device %s does not belong to BLOCK subsystem\n",
424                                 clsdev->name);
425                 return 1;
426         }
427         c += strlen(SYSFS_BLOCK_NAME);
428         if (*c == '/')
429                 c++;
430         else
431                 goto errout;
432
433        /* validate whether the given class device is a partition or not */
434         if ((strncmp(c, clsdev->name, strlen(clsdev->name))) == 0) {
435                 dprintf("%s not a partition\n", clsdev->name);
436                 return 1;
437         }
438                       
439         c = strchr(c, '/');
440         if (c == NULL)
441                 goto errout;
442
443         *c = '\0';
444                                         
445         clsdev->parent = sysfs_open_class_device_path(parent_path);
446         if (clsdev->parent == NULL) {
447                 dprintf("Error opening the parent class device at %s\n", 
448                                                                 parent_path);
449                 return 1;
450         }
451         return 0;
452
453 errout:
454         dprintf("Invalid path %s\n", clsdev->path);
455         return 1;
456 }
457
458 /**
459  * sysfs_get_classdev_parent: Retrieves the parent of a class device. 
460  *      eg., when working with hda1, this function can be used to retrieve the
461  *              sysfs_class_device for hda
462  *              
463  * @clsdev: class device whose parent details are required.
464  * Returns sysfs_class_device of the parent on success, NULL on failure
465  */ 
466 struct sysfs_class_device *sysfs_get_classdev_parent
467                                 (struct sysfs_class_device *clsdev)
468 {
469         if (clsdev == NULL) {
470                 errno = EINVAL;
471                 return NULL;
472         }
473         if (clsdev->parent != NULL)
474                 return (clsdev->parent);
475         
476         /* 
477          * As of now, only block devices have a parent child heirarchy in sysfs
478          * We do not know, if, in the future, more classes will have a similar
479          * structure. Hence, we now call a specialized function for block and
480          * later we can add support functions for other subsystems as required.
481          */ 
482         if (!(strncmp(clsdev->classname, SYSFS_BLOCK_NAME, 
483                                         sizeof(SYSFS_BLOCK_NAME)))) {
484                 if ((get_blockdev_parent(clsdev)) == 0) 
485                         return (clsdev->parent);
486         }
487         return NULL;
488 }
489
490 /**
491  * get_classdev_path: given the class and a device in the class, return the
492  *              absolute path to the device
493  * @classname: name of the class
494  * @clsdev: the class device
495  * @path: buffer to return path
496  * @psize: size of "path"
497  * Returns 0 on SUCCESS or -1 on error
498  */
499 static int get_classdev_path(const char *classname, const char *clsdev, 
500                 char *path, size_t len)
501 {
502         if (classname == NULL || clsdev == NULL || path == NULL) {
503                 errno = EINVAL;
504                 return -1;
505         }
506         if (sysfs_get_mnt_path(path, len) != 0) {
507                 dprintf("Error getting sysfs mount path\n");
508                 return -1;
509         }
510         if (strncmp(classname, SYSFS_BLOCK_NAME,
511                                 sizeof(SYSFS_BLOCK_NAME)) == 0) {
512                 safestrcatmax(path, "/", len);
513                 safestrcatmax(path, SYSFS_BLOCK_NAME, len);
514         } else {
515                 safestrcatmax(path, "/", len);
516                 safestrcatmax(path, SYSFS_CLASS_NAME, len);
517                 safestrcatmax(path, "/", len);
518                 safestrcatmax(path, classname, len);
519         }
520         safestrcatmax(path, "/", len);
521         safestrcatmax(path, clsdev, len);
522         return 0;
523 }
524
525 /**
526  * sysfs_open_class_device: Locates a specific class_device and returns it.
527  * Class_device must be closed using sysfs_close_class_device
528  * @classname: Class to search
529  * @name: name of the class_device
530  * 
531  * NOTE:
532  *      Call sysfs_close_class_device() to close the class device
533  */
534 struct sysfs_class_device *sysfs_open_class_device
535                 (const char *classname, const char *name)
536 {
537         char devpath[SYSFS_PATH_MAX];
538         struct sysfs_class_device *cdev = NULL;
539
540         if (classname == NULL || name == NULL) {
541                 errno = EINVAL;
542                 return NULL;
543         }
544         
545         memset(devpath, 0, SYSFS_PATH_MAX);
546         if ((get_classdev_path(classname, name, devpath, 
547                                         SYSFS_PATH_MAX)) != 0) {
548                 dprintf("Error getting to device %s on class %s\n",
549                                                         name, classname);
550                 return NULL;
551         }
552         
553         cdev = sysfs_open_class_device_path(devpath);
554         if (cdev == NULL) {
555                 dprintf("Error getting class device %s from class %s\n",
556                                 name, classname);
557                 return NULL;
558         }
559         return cdev;
560 }
561
562 /**
563  * sysfs_get_classdev_attributes: returns a dlist of attributes for
564  *      the requested class_device
565  * @cdev: sysfs_class_dev for which attributes are needed
566  * returns a dlist of attributes if exists, NULL otherwise
567  */
568 struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *cdev)
569 {
570         if (cdev == NULL)
571                 return NULL;
572
573         if (cdev->directory == NULL) {
574                 cdev->directory = sysfs_open_directory(cdev->path);
575                 if (cdev->directory == NULL) 
576                         return NULL;
577         }
578         if (cdev->directory->attributes == NULL) {
579                 if ((sysfs_read_dir_attributes(cdev->directory)) != 0) 
580                         return NULL;
581         }
582         return (cdev->directory->attributes);
583 }
584
585 /**
586  * sysfs_refresh_clsassdev_attributes: refreshes the driver's list of attributes
587  * @clsdev: sysfs_class_device whose attributes to refresh
588  *
589  * NOTE: Upon return, prior references to sysfs_attributes for this classdev
590  *              _may_ not be valid
591  *
592  * Returns list of attributes on success and NULL on failure
593  */
594 struct dlist *sysfs_refresh_classdev_attributes
595                         (struct sysfs_class_device *clsdev)
596 {
597         if (clsdev == NULL) {
598                 errno = EINVAL;
599                 return NULL;
600         }
601
602         if (clsdev->directory == NULL)
603                 return (sysfs_get_classdev_attributes(clsdev));
604
605         if ((sysfs_refresh_dir_attributes(clsdev->directory)) != 0) {
606                 dprintf("Error refreshing class_device attributes\n");
607                 return NULL;
608         }
609
610         return (clsdev->directory->attributes);
611 }
612
613 /**
614  * sysfs_get_classdev_attr: searches class device's attributes by name
615  * @clsdev: class device to look through
616  * @name: attribute name to get
617  * returns sysfs_attribute reference with success or NULL with error
618  */
619 struct sysfs_attribute *sysfs_get_classdev_attr
620                 (struct sysfs_class_device *clsdev, const char *name)
621 {
622         struct sysfs_attribute *cur = NULL;
623         struct sysfs_directory *sdir = NULL;
624         struct dlist *attrlist = NULL;
625         
626         if (clsdev == NULL || name == NULL) {
627                 errno = EINVAL;
628                 return NULL;
629         }
630         
631         /* 
632          * First, see if it's in the current directory. Then look at 
633          * subdirs since class devices can have subdirs of attributes.
634          */ 
635         attrlist = sysfs_get_classdev_attributes(clsdev);
636         if (attrlist != NULL) {
637                 cur = sysfs_get_directory_attribute(clsdev->directory,
638                                                 (char *)name);
639                 if (cur != NULL)
640                         return cur;
641         }
642
643         if (clsdev->directory == NULL)
644                 return NULL;
645
646         if (clsdev->directory->subdirs == NULL) 
647                 if ((sysfs_read_dir_subdirs(clsdev->directory)) != 0 ||
648                     clsdev->directory->subdirs == NULL) 
649                         return NULL;
650
651         if (clsdev->directory->subdirs != NULL) {
652                 dlist_for_each_data(clsdev->directory->subdirs, sdir,
653                                                 struct sysfs_directory) {
654                         if ((sysfs_path_is_dir(sdir->path)) != 0) 
655                                 continue;
656                         cur = sysfs_get_directory_attribute(sdir,
657                                                         (char *)name);
658                         if (cur == NULL)
659                                 continue;
660                 }
661         }
662         return cur;
663 }
664
665 /**
666  * sysfs_open_classdev_attr: read an attribute for a given class device
667  * @classname: name of the class on which to look
668  * @dev: class device name for which the attribute has to be read
669  * @attrib: attribute to read
670  * Returns sysfs_attribute * on SUCCESS and NULL on error
671  * 
672  * NOTE:
673  *      A call to sysfs_close_attribute() is required to close the
674  *      attribute returned and to free memory
675  */
676 struct sysfs_attribute *sysfs_open_classdev_attr(const char *classname,
677                 const char *dev, const char *attrib)
678 {
679         struct sysfs_attribute *attribute = NULL;
680         char path[SYSFS_PATH_MAX];
681
682         if (classname == NULL || dev == NULL || attrib == NULL) {
683                 errno = EINVAL;
684                 return NULL;
685         }
686         memset(path, 0, SYSFS_PATH_MAX);
687         if ((get_classdev_path(classname, dev, path, SYSFS_PATH_MAX)) != 0) {
688                 dprintf("Error getting to device %s on class %s\n",
689                                                 dev, classname);
690                 return NULL;
691         }
692         safestrcat(path, "/");
693         safestrcat(path, attrib);
694         attribute = sysfs_open_attribute(path);
695         if (attribute == NULL) {
696                 dprintf("Error opening attribute %s on class device %s\n",
697                                 attrib, dev);
698                 return NULL;
699         }
700         if ((sysfs_read_attribute(attribute)) != 0) {
701                 dprintf("Error reading attribute %s for class device %s\n",
702                                 attrib, dev);
703                 sysfs_close_attribute(attribute);
704                 return NULL;
705         }
706         return attribute;
707 }
708