4 * Generic class utility functions for libsysfs
6 * Copyright (C) IBM Corp. 2003
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.
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.
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
23 #include "sysfs/libsysfs.h"
26 static void sysfs_close_cls_dev(void *dev)
28 sysfs_close_class_device((struct sysfs_class_device *)dev);
32 * class_name_equal: compares class_devices' name
33 * @a: class_name looking for
34 * @b: sysfs_class_device being compared
36 static int class_name_equal(void *a, void *b)
38 if (a == NULL || b == NULL)
41 if (strcmp(((char *)a), ((struct sysfs_class_device *)b)->name) == 0)
48 * sysfs_close_class_device: closes a single class device.
49 * @dev: class device to close.
51 void sysfs_close_class_device(struct sysfs_class_device *dev)
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);
67 * sysfs_close_class: close single class
68 * @cls: class structure
70 void sysfs_close_class(struct sysfs_class *cls)
73 if (cls->directory != NULL)
74 sysfs_close_directory(cls->directory);
75 if (cls->devices != NULL)
76 dlist_destroy(cls->devices);
82 * alloc_class_device: mallocs and initializes new class device struct.
83 * returns sysfs_class_device or NULL.
85 static struct sysfs_class_device *alloc_class_device(void)
87 return (struct sysfs_class_device *)
88 calloc(1, sizeof(struct sysfs_class_device));
92 * alloc_class: mallocs new class structure
93 * returns sysfs_class struct or NULL
95 static struct sysfs_class *alloc_class(void)
97 return (struct sysfs_class *)calloc(1, sizeof(struct sysfs_class));
101 * set_classdev_classname: Grabs classname from path
102 * @cdev: class device to set
105 static void set_classdev_classname(struct sysfs_class_device *cdev)
107 char *c = NULL, *e = NULL;
110 c = strstr(cdev->path, SYSFS_CLASS_NAME);
112 c = strstr(cdev->path, SYSFS_BLOCK_NAME);
118 safestrcpy(cdev->classname, SYSFS_UNKNOWN);
123 while (e != NULL && *e != '/' && *e != '\0') {
127 strncpy(cdev->classname, c, count);
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.
136 struct sysfs_class_device *sysfs_open_class_device_path(const char *path)
138 struct sysfs_class_device *cdev = NULL;
144 if ((sysfs_path_is_dir(path)) != 0) {
145 dprintf("%s is not a valid path to a class device\n", path);
148 cdev = alloc_class_device();
150 dprintf("calloc failed\n");
153 if ((sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) != 0) {
155 dprintf("Error getting class device name\n");
156 sysfs_close_class_device(cdev);
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);
166 set_classdev_classname(cdev);
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
176 struct dlist *sysfs_get_class_devices(struct sysfs_class *cls)
178 struct sysfs_class_device *dev = NULL;
179 struct sysfs_directory *cur = NULL;
186 if (cls->devices != NULL)
189 if (cls->directory == NULL) {
190 cls->directory = sysfs_open_directory(cls->path);
191 if (cls->directory == NULL)
195 if ((sysfs_read_dir_subdirs(cls->directory)) != 0)
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);
203 dprintf("Error opening device at %s\n",
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);
218 * sysfs_open_class: opens specific class and all its devices on system
219 * returns sysfs_class structure with success or NULL with error.
221 struct sysfs_class *sysfs_open_class(const char *name)
223 struct sysfs_class *cls = NULL;
224 char classpath[SYSFS_PATH_MAX];
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");
238 * We shall now treat "block" also as a class. Hence, check here
239 * if "name" is "block" and proceed accordingly
241 if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
242 safestrcat(classpath, "/");
243 safestrcat(classpath, SYSFS_BLOCK_NAME);
245 safestrcat(classpath, "/");
246 safestrcat(classpath, SYSFS_CLASS_NAME);
247 safestrcat(classpath, "/");
248 safestrcat(classpath, name);
250 if ((sysfs_path_is_dir(classpath)) != 0) {
251 dprintf("Class %s not found on the system\n", name);
257 dprintf("calloc failed\n");
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);
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
276 struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *cls,
279 if (cls == NULL || name == NULL) {
284 if (cls->devices == NULL) {
285 cls->devices = sysfs_get_class_devices(cls);
286 if (cls->devices == NULL)
289 return (struct sysfs_class_device *)dlist_find_custom(cls->devices,
290 name, class_name_equal);
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
300 struct sysfs_device *sysfs_get_classdev_device
301 (struct sysfs_class_device *clsdev)
303 struct sysfs_link *devlink = NULL;
304 char devpath[SYSFS_PATH_MAX];
306 if (clsdev == NULL) {
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;
320 if (clsdev->directory == NULL) {
321 clsdev->directory = sysfs_open_directory(clsdev->path);
322 if (clsdev->directory == NULL)
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;
335 if (clsdev->sysdevice != NULL) {
336 if (!strncmp(devlink->target, clsdev->sysdevice->path,
338 /* sysdevice hasn't changed */
339 return (clsdev->sysdevice);
341 /* come here only if the device link for has changed */
342 sysfs_close_device(clsdev->sysdevice);
345 clsdev->sysdevice = sysfs_open_device_path(devlink->target);
346 if (clsdev->sysdevice == NULL)
349 return (clsdev->sysdevice);
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
359 struct sysfs_driver *sysfs_get_classdev_driver
360 (struct sysfs_class_device *clsdev)
362 struct sysfs_link *drvlink = NULL;
363 char drvpath[SYSFS_PATH_MAX];
365 if (clsdev == NULL) {
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;
379 if (clsdev->directory == NULL) {
380 clsdev->directory = sysfs_open_directory(clsdev->path);
381 if (clsdev->directory == NULL)
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;
393 if (clsdev->driver != NULL) {
394 if (!strncmp(drvlink->target, clsdev->driver->path,
396 /* driver hasn't changed */
397 return (clsdev->driver);
399 /* come here only if the device link for has changed */
400 sysfs_close_driver(clsdev->driver);
403 clsdev->driver = sysfs_open_driver_path(drvlink->target);
404 if (clsdev->driver == NULL)
407 return (clsdev->driver);
411 * get_blockdev_parent: Get the parent class device for a "block" subsystem
413 * @clsdev: block subsystem class device whose parent needs to be found
414 * Returns 0 on success and 1 on error
416 static int get_blockdev_parent(struct sysfs_class_device *clsdev)
418 char parent_path[SYSFS_PATH_MAX], *c = NULL;
420 safestrcpy(parent_path, clsdev->path);
421 c = strstr(parent_path, SYSFS_BLOCK_NAME);
423 dprintf("Class device %s does not belong to BLOCK subsystem\n",
427 c += strlen(SYSFS_BLOCK_NAME);
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);
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",
454 dprintf("Invalid path %s\n", clsdev->path);
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
463 * @clsdev: class device whose parent details are required.
464 * Returns sysfs_class_device of the parent on success, NULL on failure
466 struct sysfs_class_device *sysfs_get_classdev_parent
467 (struct sysfs_class_device *clsdev)
469 if (clsdev == NULL) {
473 if (clsdev->parent != NULL)
474 return (clsdev->parent);
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.
482 if (!(strncmp(clsdev->classname, SYSFS_BLOCK_NAME,
483 sizeof(SYSFS_BLOCK_NAME)))) {
484 if ((get_blockdev_parent(clsdev)) == 0)
485 return (clsdev->parent);
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
499 static int get_classdev_path(const char *classname, const char *clsdev,
500 char *path, size_t len)
502 if (classname == NULL || clsdev == NULL || path == NULL) {
506 if (sysfs_get_mnt_path(path, len) != 0) {
507 dprintf("Error getting sysfs mount path\n");
510 if (strncmp(classname, SYSFS_BLOCK_NAME,
511 sizeof(SYSFS_BLOCK_NAME)) == 0) {
512 safestrcatmax(path, "/", len);
513 safestrcatmax(path, SYSFS_BLOCK_NAME, len);
515 safestrcatmax(path, "/", len);
516 safestrcatmax(path, SYSFS_CLASS_NAME, len);
517 safestrcatmax(path, "/", len);
518 safestrcatmax(path, classname, len);
520 safestrcatmax(path, "/", len);
521 safestrcatmax(path, clsdev, len);
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
532 * Call sysfs_close_class_device() to close the class device
534 struct sysfs_class_device *sysfs_open_class_device
535 (const char *classname, const char *name)
537 char devpath[SYSFS_PATH_MAX];
538 struct sysfs_class_device *cdev = NULL;
540 if (classname == NULL || name == NULL) {
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",
553 cdev = sysfs_open_class_device_path(devpath);
555 dprintf("Error getting class device %s from class %s\n",
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
568 struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *cdev)
573 if (cdev->directory == NULL) {
574 cdev->directory = sysfs_open_directory(cdev->path);
575 if (cdev->directory == NULL)
578 if (cdev->directory->attributes == NULL) {
579 if ((sysfs_read_dir_attributes(cdev->directory)) != 0)
582 return (cdev->directory->attributes);
586 * sysfs_refresh_clsassdev_attributes: refreshes the driver's list of attributes
587 * @clsdev: sysfs_class_device whose attributes to refresh
589 * NOTE: Upon return, prior references to sysfs_attributes for this classdev
592 * Returns list of attributes on success and NULL on failure
594 struct dlist *sysfs_refresh_classdev_attributes
595 (struct sysfs_class_device *clsdev)
597 if (clsdev == NULL) {
602 if (clsdev->directory == NULL)
603 return (sysfs_get_classdev_attributes(clsdev));
605 if ((sysfs_refresh_dir_attributes(clsdev->directory)) != 0) {
606 dprintf("Error refreshing class_device attributes\n");
610 return (clsdev->directory->attributes);
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
619 struct sysfs_attribute *sysfs_get_classdev_attr
620 (struct sysfs_class_device *clsdev, const char *name)
622 struct sysfs_attribute *cur = NULL;
623 struct sysfs_directory *sdir = NULL;
624 struct dlist *attrlist = NULL;
626 if (clsdev == NULL || name == NULL) {
632 * First, see if it's in the current directory. Then look at
633 * subdirs since class devices can have subdirs of attributes.
635 attrlist = sysfs_get_classdev_attributes(clsdev);
636 if (attrlist != NULL) {
637 cur = sysfs_get_directory_attribute(clsdev->directory,
643 if (clsdev->directory->subdirs == NULL)
644 if ((sysfs_read_dir_subdirs(clsdev->directory)) != 0 ||
645 clsdev->directory->subdirs == NULL)
648 if (clsdev->directory->subdirs != NULL) {
649 dlist_for_each_data(clsdev->directory->subdirs, sdir,
650 struct sysfs_directory) {
651 if ((sysfs_path_is_dir(sdir->path)) != 0)
653 cur = sysfs_get_directory_attribute(sdir,
663 * sysfs_open_classdev_attr: read an attribute for a given class device
664 * @classname: name of the class on which to look
665 * @dev: class device name for which the attribute has to be read
666 * @attrib: attribute to read
667 * Returns sysfs_attribute * on SUCCESS and NULL on error
670 * A call to sysfs_close_attribute() is required to close the
671 * attribute returned and to free memory
673 struct sysfs_attribute *sysfs_open_classdev_attr(const char *classname,
674 const char *dev, const char *attrib)
676 struct sysfs_attribute *attribute = NULL;
677 char path[SYSFS_PATH_MAX];
679 if (classname == NULL || dev == NULL || attrib == NULL) {
683 memset(path, 0, SYSFS_PATH_MAX);
684 if ((get_classdev_path(classname, dev, path, SYSFS_PATH_MAX)) != 0) {
685 dprintf("Error getting to device %s on class %s\n",
689 safestrcat(path, "/");
690 safestrcat(path, attrib);
691 attribute = sysfs_open_attribute(path);
692 if (attribute == NULL) {
693 dprintf("Error opening attribute %s on class device %s\n",
697 if ((sysfs_read_attribute(attribute)) != 0) {
698 dprintf("Error reading attribute %s for class device %s\n",
700 sysfs_close_attribute(attribute);