4 * Generic class utility functions for libsysfs
6 * Copyright (C) IBM Corp. 2003-2005
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
27 * sysfs_close_class_device: closes a single class device.
28 * @dev: class device to close.
30 void sysfs_close_class_device(struct sysfs_class_device *dev)
34 sysfs_close_class_device(dev->parent);
36 sysfs_close_device(dev->sysdevice);
38 dlist_destroy(dev->attrlist);
43 static void sysfs_close_cls_dev(void *dev)
45 sysfs_close_class_device((struct sysfs_class_device *)dev);
49 * sysfs_close_class: close the given class
50 * @cls: sysfs_class to close
52 void sysfs_close_class(struct sysfs_class *cls)
56 dlist_destroy(cls->devices);
58 dlist_destroy(cls->attrlist);
63 static int cdev_name_equal(void *a, void *b)
68 if (strncmp((char *)a, ((struct sysfs_class_device *)b)->name,
69 strlen((char *)a)) == 0)
75 static struct sysfs_class *alloc_class(void)
77 return (struct sysfs_class *) calloc(1, sizeof(struct sysfs_class));
81 * alloc_class_device: mallocs and initializes new class device struct.
82 * returns sysfs_class_device or NULL.
84 static struct sysfs_class_device *alloc_class_device(void)
86 struct sysfs_class_device *dev;
88 dev = calloc(1, sizeof(struct sysfs_class_device));
93 * set_classdev_classname: Grabs classname from path
94 * @cdev: class device to set
97 static void set_classdev_classname(struct sysfs_class_device *cdev)
102 c = strstr(cdev->path, SYSFS_CLASS_NAME);
104 c = strstr(cdev->path, SYSFS_BLOCK_NAME);
110 safestrcpy(cdev->classname, SYSFS_UNKNOWN);
115 while (e != NULL && *e != '/' && *e != '\0') {
119 strncpy(cdev->classname, c, count);
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.
128 struct sysfs_class_device *sysfs_open_class_device_path(const char *path)
130 struct sysfs_class_device *cdev;
131 char temp_path[SYSFS_PATH_MAX];
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
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);
151 dprintf("%s is not a valid class device path\n", path);
155 safestrcpy(temp_path, path);
157 cdev = alloc_class_device();
159 dprintf("calloc failed\n");
162 if (sysfs_get_name_from_path(temp_path, cdev->name, SYSFS_NAME_LEN)) {
164 dprintf("Error getting class device name\n");
165 sysfs_close_class_device(cdev);
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);
175 set_classdev_classname(cdev);
181 * get_blockdev_parent: Get the parent class device for a "block" subsystem
183 * @clsdev: block subsystem class device whose parent needs to be found
184 * Returns 0 on success and 1 on error
186 static int get_blockdev_parent(struct sysfs_class_device *clsdev)
188 char parent_path[SYSFS_PATH_MAX];
191 safestrcpy(parent_path, clsdev->path);
192 c = strstr(parent_path, SYSFS_BLOCK_NAME);
194 dprintf("Class device %s does not belong to BLOCK subsystem\n",
198 c += strlen(SYSFS_BLOCK_NAME);
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);
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",
225 dprintf("Invalid path %s\n", clsdev->path);
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
234 * @clsdev: class device whose parent details are required.
235 * Returns sysfs_class_device of the parent on success, NULL on failure
237 struct sysfs_class_device *sysfs_get_classdev_parent
238 (struct sysfs_class_device *clsdev)
245 return (clsdev->parent);
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.
253 if (!(strncmp(clsdev->classname, SYSFS_BLOCK_NAME,
254 sizeof(SYSFS_BLOCK_NAME)))) {
255 if ((get_blockdev_parent(clsdev)) == 0)
256 return (clsdev->parent);
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
270 static int get_classdev_path(const char *classname, const char *clsdev,
271 char *path, size_t len)
273 if (!classname || !clsdev || !path) {
277 if (sysfs_get_mnt_path(path, len) != 0) {
278 dprintf("Error getting sysfs mount path\n");
281 if (strncmp(classname, SYSFS_BLOCK_NAME,
282 sizeof(SYSFS_BLOCK_NAME)) == 0) {
283 safestrcatmax(path, "/", len);
284 safestrcatmax(path, SYSFS_BLOCK_NAME, len);
286 safestrcatmax(path, "/", len);
287 safestrcatmax(path, SYSFS_CLASS_NAME, len);
288 safestrcatmax(path, "/", len);
289 safestrcatmax(path, classname, len);
291 safestrcatmax(path, "/", len);
292 safestrcatmax(path, clsdev, len);
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
303 * Call sysfs_close_class_device() to close the class device
305 struct sysfs_class_device *sysfs_open_class_device
306 (const char *classname, const char *name)
308 char devpath[SYSFS_PATH_MAX];
309 struct sysfs_class_device *cdev;
311 if (!classname || !name) {
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",
324 cdev = sysfs_open_class_device_path(devpath);
326 dprintf("Error getting class device %s from class %s\n",
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
339 struct sysfs_attribute *sysfs_get_classdev_attr
340 (struct sysfs_class_device *clsdev, const char *name)
342 if (!clsdev || !name) {
346 return get_attribute(clsdev, (char *)name);
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
354 struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *clsdev)
360 return get_attributes_list(clsdev);
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
369 struct sysfs_device *sysfs_get_classdev_device
370 (struct sysfs_class_device *clsdev)
372 char linkpath[SYSFS_PATH_MAX], devpath[SYSFS_PATH_MAX];
379 if (clsdev->sysdevice)
380 return clsdev->sysdevice;
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);
390 return clsdev->sysdevice;
394 * sysfs_open_class: opens specific class and all its devices on system
395 * returns sysfs_class structure with success or NULL with error.
397 struct sysfs_class *sysfs_open_class(const char *name)
399 struct sysfs_class *cls = NULL;
400 char classpath[SYSFS_PATH_MAX];
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");
414 * We shall now treat "block" also as a class. Hence, check here
415 * if "name" is "block" and proceed accordingly
417 if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
418 safestrcat(classpath, "/");
419 safestrcat(classpath, SYSFS_BLOCK_NAME);
421 safestrcat(classpath, "/");
422 safestrcat(classpath, SYSFS_CLASS_NAME);
423 safestrcat(classpath, "/");
424 safestrcat(classpath, name);
426 if (sysfs_path_is_dir(classpath)) {
427 dprintf("Class %s not found on the system\n", name);
433 dprintf("calloc failed\n");
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);
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
452 * Returns sysfs_class_device * on success and NULL on failure
454 struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *cls,
457 char path[SYSFS_PATH_MAX];
458 struct sysfs_class_device *cdev = NULL;
466 cdev = (struct sysfs_class_device *)dlist_find_custom
467 (cls->devices, (void *)name, cdev_name_equal);
472 safestrcpy(path, cls->path);
473 safestrcat(path, "/");
474 safestrcat(path, name);
475 cdev = sysfs_open_class_device_path(path);
477 dprintf("Error opening class device at %s\n", path);
481 cls->devices = dlist_new_with_delete
482 (sizeof(struct sysfs_class_device),
483 sysfs_close_cls_dev);
485 dlist_unshift_sorted(cls->devices, cdev, sort_list);
490 * Add class devices to list
492 static void add_cdevs_to_classlist(struct sysfs_class *cls, struct dlist *list)
494 char path[SYSFS_PATH_MAX], *cdev_name;
495 struct sysfs_class_device *cdev = NULL;
497 if (cls == NULL || list == NULL)
500 dlist_for_each_data(list, cdev_name, char) {
502 cdev = (struct sysfs_class_device *)
503 dlist_find_custom(cls->devices,
504 (void *)cdev_name, cdev_name_equal);
508 safestrcpy(path, cls->path);
509 safestrcat(path, "/");
510 safestrcat(path, cdev_name);
511 cdev = sysfs_open_class_device_path(path);
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,
524 * sysfs_get_class_devices: get all class devices in the given class
525 * @cls: sysfs_class whose devices list is needed
527 * Returns a dlist of sysfs_class_device * on success and NULL on failure
529 struct dlist *sysfs_get_class_devices(struct sysfs_class *cls)
531 char path[SYSFS_PATH_MAX];
532 struct dlist *dirlist, *linklist;
540 * Post linux-2.6.14, we have nested classes and links under
541 * /sys/class/xxx/. are also valid class devices
543 safestrcpy(path, cls->path);
544 dirlist = read_dir_subdirs(path);
546 add_cdevs_to_classlist(cls, dirlist);
547 sysfs_close_list(dirlist);
550 linklist = read_dir_links(path);
552 add_cdevs_to_classlist(cls, linklist);
553 sysfs_close_list(linklist);