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);
44 * alloc_class_device: mallocs and initializes new class device struct.
45 * returns sysfs_class_device or NULL.
47 static struct sysfs_class_device *alloc_class_device(void)
49 struct sysfs_class_device *dev;
51 dev = calloc(1, sizeof(struct sysfs_class_device));
56 * set_classdev_classname: Grabs classname from path
57 * @cdev: class device to set
60 static void set_classdev_classname(struct sysfs_class_device *cdev)
65 c = strstr(cdev->path, SYSFS_CLASS_NAME);
67 c = strstr(cdev->path, SYSFS_BLOCK_NAME);
73 safestrcpy(cdev->classname, SYSFS_UNKNOWN);
78 while (e != NULL && *e != '/' && *e != '\0') {
82 strncpy(cdev->classname, c, count);
87 * sysfs_open_class_device_path: Opens and populates class device
88 * @path: path to class device.
89 * returns struct sysfs_class_device with success and NULL with error.
91 struct sysfs_class_device *sysfs_open_class_device_path(const char *path)
93 struct sysfs_class_device *cdev;
99 if ((sysfs_path_is_dir(path)) != 0) {
100 dprintf("%s is not a valid path to a class device\n", path);
103 cdev = alloc_class_device();
105 dprintf("calloc failed\n");
108 if (sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) {
110 dprintf("Error getting class device name\n");
111 sysfs_close_class_device(cdev);
115 safestrcpy(cdev->path, path);
116 if ((sysfs_remove_trailing_slash(cdev->path)) != 0) {
117 dprintf("Invalid path to class device %s\n", cdev->path);
118 sysfs_close_class_device(cdev);
121 set_classdev_classname(cdev);
127 * get_blockdev_parent: Get the parent class device for a "block" subsystem
129 * @clsdev: block subsystem class device whose parent needs to be found
130 * Returns 0 on success and 1 on error
132 static int get_blockdev_parent(struct sysfs_class_device *clsdev)
134 char parent_path[SYSFS_PATH_MAX];
137 safestrcpy(parent_path, clsdev->path);
138 c = strstr(parent_path, SYSFS_BLOCK_NAME);
140 dprintf("Class device %s does not belong to BLOCK subsystem\n",
144 c += strlen(SYSFS_BLOCK_NAME);
150 /* validate whether the given class device is a partition or not */
151 if ((strncmp(c, clsdev->name, strlen(clsdev->name))) == 0) {
152 dprintf("%s not a partition\n", clsdev->name);
162 clsdev->parent = sysfs_open_class_device_path(parent_path);
163 if (!clsdev->parent) {
164 dprintf("Error opening the parent class device at %s\n",
171 dprintf("Invalid path %s\n", clsdev->path);
176 * sysfs_get_classdev_parent: Retrieves the parent of a class device.
177 * eg., when working with hda1, this function can be used to retrieve the
178 * sysfs_class_device for hda
180 * @clsdev: class device whose parent details are required.
181 * Returns sysfs_class_device of the parent on success, NULL on failure
183 struct sysfs_class_device *sysfs_get_classdev_parent
184 (struct sysfs_class_device *clsdev)
191 return (clsdev->parent);
194 * As of now, only block devices have a parent child heirarchy in sysfs
195 * We do not know, if, in the future, more classes will have a similar
196 * structure. Hence, we now call a specialized function for block and
197 * later we can add support functions for other subsystems as required.
199 if (!(strncmp(clsdev->classname, SYSFS_BLOCK_NAME,
200 sizeof(SYSFS_BLOCK_NAME)))) {
201 if ((get_blockdev_parent(clsdev)) == 0)
202 return (clsdev->parent);
208 * get_classdev_path: given the class and a device in the class, return the
209 * absolute path to the device
210 * @classname: name of the class
211 * @clsdev: the class device
212 * @path: buffer to return path
213 * @psize: size of "path"
214 * Returns 0 on SUCCESS or -1 on error
216 static int get_classdev_path(const char *classname, const char *clsdev,
217 char *path, size_t len)
219 if (!classname || !clsdev || !path) {
223 if (sysfs_get_mnt_path(path, len) != 0) {
224 dprintf("Error getting sysfs mount path\n");
227 if (strncmp(classname, SYSFS_BLOCK_NAME,
228 sizeof(SYSFS_BLOCK_NAME)) == 0) {
229 safestrcatmax(path, "/", len);
230 safestrcatmax(path, SYSFS_BLOCK_NAME, len);
232 safestrcatmax(path, "/", len);
233 safestrcatmax(path, SYSFS_CLASS_NAME, len);
234 safestrcatmax(path, "/", len);
235 safestrcatmax(path, classname, len);
237 safestrcatmax(path, "/", len);
238 safestrcatmax(path, clsdev, len);
243 * sysfs_open_class_device: Locates a specific class_device and returns it.
244 * Class_device must be closed using sysfs_close_class_device
245 * @classname: Class to search
246 * @name: name of the class_device
249 * Call sysfs_close_class_device() to close the class device
251 struct sysfs_class_device *sysfs_open_class_device
252 (const char *classname, const char *name)
254 char devpath[SYSFS_PATH_MAX];
255 struct sysfs_class_device *cdev;
257 if (!classname || !name) {
262 memset(devpath, 0, SYSFS_PATH_MAX);
263 if ((get_classdev_path(classname, name, devpath,
264 SYSFS_PATH_MAX)) != 0) {
265 dprintf("Error getting to device %s on class %s\n",
270 cdev = sysfs_open_class_device_path(devpath);
272 dprintf("Error getting class device %s from class %s\n",
280 * sysfs_get_classdev_attr: searches class device's attributes by name
281 * @clsdev: class device to look through
282 * @name: attribute name to get
283 * returns sysfs_attribute reference with success or NULL with error
285 struct sysfs_attribute *sysfs_get_classdev_attr
286 (struct sysfs_class_device *clsdev, const char *name)
288 if (!clsdev || !name) {
292 return get_attribute(clsdev, (char *)name);
296 * sysfs_get_classdev_attributes: gets list of classdev attributes
297 * @clsdev: class device whose attributes list is needed
298 * returns dlist of attributes on success or NULL on error
300 struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *clsdev)
306 return get_attributes_list(clsdev);
310 * sysfs_get_classdev_device: gets the sysfs_device associated with the
311 * given sysfs_class_device
312 * @clsdev: class device whose associated sysfs_device is needed
313 * returns struct sysfs_device * on success or NULL on error
315 struct sysfs_device *sysfs_get_classdev_device
316 (struct sysfs_class_device *clsdev)
318 char linkpath[SYSFS_PATH_MAX], devpath[SYSFS_PATH_MAX];
325 if (clsdev->sysdevice)
326 return clsdev->sysdevice;
328 memset(linkpath, 0, SYSFS_PATH_MAX);
329 safestrcpy(linkpath, clsdev->path);
330 safestrcat(linkpath, "/device");
331 if (!sysfs_path_is_link(linkpath)) {
332 memset(devpath, 0, SYSFS_PATH_MAX);
333 if (!sysfs_get_link(linkpath, devpath, SYSFS_PATH_MAX))
334 clsdev->sysdevice = sysfs_open_device_path(devpath);
336 return clsdev->sysdevice;