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;
136 if (sysfs_path_is_dir(path)) {
137 dprintf("%s is not a valid path to a class device\n", path);
140 cdev = alloc_class_device();
142 dprintf("calloc failed\n");
145 if (sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) {
147 dprintf("Error getting class device name\n");
148 sysfs_close_class_device(cdev);
152 safestrcpy(cdev->path, path);
153 if (sysfs_remove_trailing_slash(cdev->path)) {
154 dprintf("Invalid path to class device %s\n", cdev->path);
155 sysfs_close_class_device(cdev);
158 set_classdev_classname(cdev);
164 * get_blockdev_parent: Get the parent class device for a "block" subsystem
166 * @clsdev: block subsystem class device whose parent needs to be found
167 * Returns 0 on success and 1 on error
169 static int get_blockdev_parent(struct sysfs_class_device *clsdev)
171 char parent_path[SYSFS_PATH_MAX];
174 safestrcpy(parent_path, clsdev->path);
175 c = strstr(parent_path, SYSFS_BLOCK_NAME);
177 dprintf("Class device %s does not belong to BLOCK subsystem\n",
181 c += strlen(SYSFS_BLOCK_NAME);
187 /* validate whether the given class device is a partition or not */
188 if ((strncmp(c, clsdev->name, strlen(clsdev->name))) == 0) {
189 dprintf("%s not a partition\n", clsdev->name);
199 clsdev->parent = sysfs_open_class_device_path(parent_path);
200 if (!clsdev->parent) {
201 dprintf("Error opening the parent class device at %s\n",
208 dprintf("Invalid path %s\n", clsdev->path);
213 * sysfs_get_classdev_parent: Retrieves the parent of a class device.
214 * eg., when working with hda1, this function can be used to retrieve the
215 * sysfs_class_device for hda
217 * @clsdev: class device whose parent details are required.
218 * Returns sysfs_class_device of the parent on success, NULL on failure
220 struct sysfs_class_device *sysfs_get_classdev_parent
221 (struct sysfs_class_device *clsdev)
228 return (clsdev->parent);
231 * As of now, only block devices have a parent child heirarchy in sysfs
232 * We do not know, if, in the future, more classes will have a similar
233 * structure. Hence, we now call a specialized function for block and
234 * later we can add support functions for other subsystems as required.
236 if (!(strncmp(clsdev->classname, SYSFS_BLOCK_NAME,
237 sizeof(SYSFS_BLOCK_NAME)))) {
238 if ((get_blockdev_parent(clsdev)) == 0)
239 return (clsdev->parent);
245 * get_classdev_path: given the class and a device in the class, return the
246 * absolute path to the device
247 * @classname: name of the class
248 * @clsdev: the class device
249 * @path: buffer to return path
250 * @psize: size of "path"
251 * Returns 0 on SUCCESS or -1 on error
253 static int get_classdev_path(const char *classname, const char *clsdev,
254 char *path, size_t len)
256 if (!classname || !clsdev || !path) {
260 if (sysfs_get_mnt_path(path, len) != 0) {
261 dprintf("Error getting sysfs mount path\n");
264 if (strncmp(classname, SYSFS_BLOCK_NAME,
265 sizeof(SYSFS_BLOCK_NAME)) == 0) {
266 safestrcatmax(path, "/", len);
267 safestrcatmax(path, SYSFS_BLOCK_NAME, len);
269 safestrcatmax(path, "/", len);
270 safestrcatmax(path, SYSFS_CLASS_NAME, len);
271 safestrcatmax(path, "/", len);
272 safestrcatmax(path, classname, len);
274 safestrcatmax(path, "/", len);
275 safestrcatmax(path, clsdev, len);
280 * sysfs_open_class_device: Locates a specific class_device and returns it.
281 * Class_device must be closed using sysfs_close_class_device
282 * @classname: Class to search
283 * @name: name of the class_device
286 * Call sysfs_close_class_device() to close the class device
288 struct sysfs_class_device *sysfs_open_class_device
289 (const char *classname, const char *name)
291 char devpath[SYSFS_PATH_MAX];
292 struct sysfs_class_device *cdev;
294 if (!classname || !name) {
299 memset(devpath, 0, SYSFS_PATH_MAX);
300 if ((get_classdev_path(classname, name, devpath,
301 SYSFS_PATH_MAX)) != 0) {
302 dprintf("Error getting to device %s on class %s\n",
307 cdev = sysfs_open_class_device_path(devpath);
309 dprintf("Error getting class device %s from class %s\n",
317 * sysfs_get_classdev_attr: searches class device's attributes by name
318 * @clsdev: class device to look through
319 * @name: attribute name to get
320 * returns sysfs_attribute reference with success or NULL with error
322 struct sysfs_attribute *sysfs_get_classdev_attr
323 (struct sysfs_class_device *clsdev, const char *name)
325 if (!clsdev || !name) {
329 return get_attribute(clsdev, (char *)name);
333 * sysfs_get_classdev_attributes: gets list of classdev attributes
334 * @clsdev: class device whose attributes list is needed
335 * returns dlist of attributes on success or NULL on error
337 struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *clsdev)
343 return get_attributes_list(clsdev);
347 * sysfs_get_classdev_device: gets the sysfs_device associated with the
348 * given sysfs_class_device
349 * @clsdev: class device whose associated sysfs_device is needed
350 * returns struct sysfs_device * on success or NULL on error
352 struct sysfs_device *sysfs_get_classdev_device
353 (struct sysfs_class_device *clsdev)
355 char linkpath[SYSFS_PATH_MAX], devpath[SYSFS_PATH_MAX];
362 if (clsdev->sysdevice)
363 return clsdev->sysdevice;
365 memset(linkpath, 0, SYSFS_PATH_MAX);
366 safestrcpy(linkpath, clsdev->path);
367 safestrcat(linkpath, "/device");
368 if (!sysfs_path_is_link(linkpath)) {
369 memset(devpath, 0, SYSFS_PATH_MAX);
370 if (!sysfs_get_link(linkpath, devpath, SYSFS_PATH_MAX))
371 clsdev->sysdevice = sysfs_open_device_path(devpath);
373 return clsdev->sysdevice;
377 * sysfs_open_class: opens specific class and all its devices on system
378 * returns sysfs_class structure with success or NULL with error.
380 struct sysfs_class *sysfs_open_class(const char *name)
382 struct sysfs_class *cls = NULL;
383 char classpath[SYSFS_PATH_MAX];
390 memset(classpath, 0, SYSFS_PATH_MAX);
391 if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) {
392 dprintf("Sysfs not supported on this system\n");
397 * We shall now treat "block" also as a class. Hence, check here
398 * if "name" is "block" and proceed accordingly
400 if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
401 safestrcat(classpath, "/");
402 safestrcat(classpath, SYSFS_BLOCK_NAME);
404 safestrcat(classpath, "/");
405 safestrcat(classpath, SYSFS_CLASS_NAME);
406 safestrcat(classpath, "/");
407 safestrcat(classpath, name);
409 if (sysfs_path_is_dir(classpath)) {
410 dprintf("Class %s not found on the system\n", name);
416 dprintf("calloc failed\n");
419 safestrcpy(cls->name, name);
420 safestrcpy(cls->path, classpath);
421 if ((sysfs_remove_trailing_slash(cls->path)) != 0) {
422 dprintf("Invalid path to class device %s\n", cls->path);
423 sysfs_close_class(cls);
431 * sysfs_get_class_device: get specific class device using the device's id
432 * @cls: sysfs_class to find the device on
433 * @name: name of the class device to look for
435 * Returns sysfs_class_device * on success and NULL on failure
437 struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *cls,
440 char path[SYSFS_PATH_MAX];
441 struct sysfs_class_device *cdev = NULL;
449 cdev = (struct sysfs_class_device *)dlist_find_custom
450 (cls->devices, (void *)name, cdev_name_equal);
455 safestrcpy(path, cls->path);
456 safestrcat(path, "/");
457 safestrcat(path, name);
458 if (!sysfs_path_is_dir(path)) {
459 cdev = sysfs_open_class_device_path(path);
461 dprintf("Error opening class device at %s\n", path);
466 cls->devices = dlist_new_with_delete
467 (sizeof(struct sysfs_class_device),
468 sysfs_close_cls_dev);
470 dlist_unshift_sorted(cls->devices, cdev, sort_list);
475 * sysfs_get_class_devices: get all class devices in the given class
476 * @cls: sysfs_class whose devices list is needed
478 * Returns a dlist of sysfs_class_device * on success and NULL on failure
480 struct dlist *sysfs_get_class_devices(struct sysfs_class *cls)
482 char path[SYSFS_PATH_MAX];
484 struct sysfs_class_device *cdev = NULL;
485 struct dlist *dirlist;
492 safestrcpy(path, cls->path);
493 dirlist = read_dir_subdirs(path);
495 dlist_for_each_data(dirlist, cdev_name, char) {
497 cdev = (struct sysfs_class_device *)
498 dlist_find_custom(cls->devices,
499 (void *)cdev_name, cdev_name_equal);
503 safestrcpy(path, cls->path);
504 safestrcat(path, "/");
505 safestrcat(path, cdev_name);
506 cdev = sysfs_open_class_device_path(path);
509 cls->devices = dlist_new_with_delete
510 (sizeof(struct sysfs_class_device),
511 sysfs_close_cls_dev);
512 dlist_unshift_sorted(cls->devices, cdev,
516 sysfs_close_list(dirlist);