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