4 * Generic device 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"
27 * get_dev_driver: fills in the dev->driver_name field
29 * Returns 0 on SUCCESS and 1 on error
31 static int get_dev_driver(struct sysfs_device *dev)
33 struct dlist *drvlist = NULL;
34 char path[SYSFS_PATH_MAX], devpath[SYSFS_PATH_MAX];
35 char *drv = NULL, *c = NULL;
41 if (dev->bus[0] == '\0')
43 memset(path, 0, SYSFS_PATH_MAX);
44 memset(devpath, 0, SYSFS_PATH_MAX);
45 safestrcpy(path, SYSFS_BUS_NAME);
46 safestrcat(path, "/");
47 safestrcat(path, dev->bus);
48 safestrcat(path, "/");
49 safestrcat(path, SYSFS_DRIVERS_NAME);
51 safestrcpy(devpath, dev->path);
52 c = strstr(devpath, SYSFS_DEVICES_NAME);
56 safestrcatmax(c, path, (sizeof(devpath) - strlen(devpath)));
58 drvlist = sysfs_open_subsystem_list(path);
59 if (drvlist != NULL) {
60 dlist_for_each_data(drvlist, drv, char) {
61 safestrcpy(path, devpath);
62 safestrcat(path, "/");
63 safestrcat(path, drv);
64 safestrcat(path, "/");
65 safestrcat(path, dev->bus_id);
66 if (sysfs_path_is_link(path) == 0) {
67 safestrcpy(dev->driver_name, drv);
68 sysfs_close_list(drvlist);
72 sysfs_close_list(drvlist);
78 * sysfs_get_device_bus: retrieves the bus name the device is on, checks path
79 * to bus' link to make sure it has correct device.
80 * @dev: device to get busname.
81 * returns 0 with success and -1 with error.
83 int sysfs_get_device_bus(struct sysfs_device *dev)
85 char subsys[SYSFS_NAME_LEN], path[SYSFS_PATH_MAX];
86 char target[SYSFS_PATH_MAX], *bus = NULL, *c = NULL;
87 struct dlist *buslist = NULL;
94 memset(subsys, 0, SYSFS_NAME_LEN);
95 safestrcpy(subsys, SYSFS_BUS_NAME); /* subsys = bus */
96 buslist = sysfs_open_subsystem_list(subsys);
97 if (buslist != NULL) {
98 dlist_for_each_data(buslist, bus, char) {
99 memset(path, 0, SYSFS_PATH_MAX);
100 safestrcpy(path, dev->path);
101 c = strstr(path, "/devices");
103 dprintf("Invalid path to device %s\n", path);
104 sysfs_close_list(buslist);
108 safestrcat(path, "/");
109 safestrcat(path, SYSFS_BUS_NAME);
110 safestrcat(path, "/");
111 safestrcat(path, bus);
112 safestrcat(path, "/");
113 safestrcat(path, SYSFS_DEVICES_NAME);
114 safestrcat(path, "/");
115 safestrcat(path, dev->bus_id);
116 if ((sysfs_path_is_link(path)) == 0) {
117 memset(target, 0, SYSFS_PATH_MAX);
118 if ((sysfs_get_link(path, target,
119 SYSFS_PATH_MAX)) != 0) {
120 dprintf("Error getting link target\n");
121 sysfs_close_list(buslist);
124 if (!(strncmp(target, dev->path,
126 safestrcpy(dev->bus, bus);
127 sysfs_close_list(buslist);
132 sysfs_close_list(buslist);
138 * sysfs_close_device_tree: closes every device in the supplied tree,
139 * closing children only.
140 * @devroot: device root of tree.
142 void sysfs_close_device_tree(struct sysfs_device *devroot)
144 if (devroot != NULL) {
145 if (devroot->children != NULL) {
146 struct sysfs_device *child = NULL;
148 dlist_for_each_data(devroot->children, child,
149 struct sysfs_device) {
150 sysfs_close_device_tree(child);
153 sysfs_close_device(devroot);
158 * sysfs_close_dev_tree: routine for dlist integration
160 static void sysfs_close_dev_tree(void *dev)
162 sysfs_close_device_tree((struct sysfs_device *)dev);
166 * sysfs_close_device: closes and cleans up a device
167 * @dev = device to clean up
169 void sysfs_close_device(struct sysfs_device *dev)
172 if (dev->parent != NULL)
173 sysfs_close_device(dev->parent);
174 if (dev->directory != NULL)
175 sysfs_close_directory(dev->directory);
176 if (dev->children != NULL && dev->children->count == 0)
177 dlist_destroy(dev->children);
183 * alloc_device: allocates and initializes device structure
184 * returns struct sysfs_device
186 static struct sysfs_device *alloc_device(void)
188 return (struct sysfs_device *)calloc(1, sizeof(struct sysfs_device));
192 * open_device_dir: opens up sysfs_directory for specific root dev
193 * @name: name of root
194 * returns struct sysfs_directory with success and NULL with error
196 static struct sysfs_directory *open_device_dir(const char *path)
198 struct sysfs_directory *rdir = NULL;
205 rdir = sysfs_open_directory(path);
208 dprintf ("Device %s not supported on this system\n", path);
211 if ((sysfs_read_dir_subdirs(rdir)) != 0) {
212 dprintf ("Error reading device at dir %s\n", path);
213 sysfs_close_directory(rdir);
221 * sysfs_open_device_path: opens and populates device structure
222 * @path: path to device, this is the /sys/devices/ path
223 * returns sysfs_device structure with success or NULL with error
225 struct sysfs_device *sysfs_open_device_path(const char *path)
227 struct sysfs_device *dev = NULL;
233 if ((sysfs_path_is_dir(path)) != 0) {
234 dprintf("Incorrect path to device: %s\n", path);
237 dev = alloc_device();
239 dprintf("Error allocating device at %s\n", path);
242 if ((sysfs_get_name_from_path(path, dev->bus_id,
243 SYSFS_NAME_LEN)) != 0) {
245 dprintf("Error getting device bus_id\n");
246 sysfs_close_device(dev);
249 safestrcpy(dev->path, path);
250 if ((sysfs_remove_trailing_slash(dev->path)) != 0) {
251 dprintf("Invalid path to device %s\n", dev->path);
252 sysfs_close_device(dev);
256 * The "name" attribute no longer exists... return the device's
257 * sysfs representation instead, in the "dev->name" field, which
258 * implies that the dev->name and dev->bus_id contain same data.
260 safestrcpy(dev->name, dev->bus_id);
262 if (sysfs_get_device_bus(dev) != 0)
263 dprintf("Could not get device bus\n");
265 if (get_dev_driver(dev) != 0) {
266 dprintf("Could not get device %s's driver\n", dev->bus_id);
267 safestrcpy(dev->driver_name, SYSFS_UNKNOWN);
274 * sysfs_open_device_tree: opens root device and all of its children,
275 * creating a tree of devices. Only opens children.
276 * @path: sysfs path to devices
277 * returns struct sysfs_device and its children with success or NULL with
280 struct sysfs_device *sysfs_open_device_tree(const char *path)
282 struct sysfs_device *rootdev = NULL, *new = NULL;
283 struct sysfs_directory *cur = NULL;
289 rootdev = sysfs_open_device_path(path);
290 if (rootdev == NULL) {
291 dprintf("Error opening root device at %s\n", path);
294 if (rootdev->directory == NULL) {
295 rootdev->directory = open_device_dir(rootdev->path);
296 if (rootdev->directory == NULL)
299 if (rootdev->directory->subdirs != NULL) {
300 dlist_for_each_data(rootdev->directory->subdirs, cur,
301 struct sysfs_directory) {
302 new = sysfs_open_device_tree(cur->path);
304 dprintf("Error opening device tree at %s\n",
306 sysfs_close_device_tree(rootdev);
309 if (rootdev->children == NULL)
310 rootdev->children = dlist_new_with_delete
311 (sizeof(struct sysfs_device),
312 sysfs_close_dev_tree);
313 dlist_unshift_sorted(rootdev->children,
322 * sysfs_close_root_device: closes root and all devices
323 * @root: root device to close
325 void sysfs_close_root_device(struct sysfs_root_device *root)
328 if (root->devices != NULL)
329 dlist_destroy(root->devices);
330 if (root->directory != NULL)
331 sysfs_close_directory(root->directory);
337 * sysfs_get_root_devices: opens up all the devices under this root device
338 * @root: root device to open devices for
339 * returns dlist of devices with success and NULL with error
341 struct dlist *sysfs_get_root_devices(struct sysfs_root_device *root)
343 struct sysfs_device *dev = NULL;
344 struct sysfs_directory *cur = NULL;
350 if (root->directory == NULL) {
351 root->directory = open_device_dir(root->path);
352 if (root->directory == NULL)
356 if (root->directory->subdirs == NULL)
359 dlist_for_each_data(root->directory->subdirs, cur,
360 struct sysfs_directory) {
361 dev = sysfs_open_device_tree(cur->path);
363 dprintf ("Error opening device at %s\n", cur->path);
366 if (root->devices == NULL)
367 root->devices = dlist_new_with_delete
368 (sizeof(struct sysfs_device),
369 sysfs_close_dev_tree);
370 dlist_unshift_sorted(root->devices, dev, sort_list);
373 return root->devices;
377 * sysfs_open_root_device: opens sysfs devices root and all of its
379 * @name: name of /sys/devices/root to open
380 * returns struct sysfs_root_device if success and NULL with error
382 struct sysfs_root_device *sysfs_open_root_device(const char *name)
384 struct sysfs_root_device *root = NULL;
385 char rootpath[SYSFS_PATH_MAX];
392 memset(rootpath, 0, SYSFS_PATH_MAX);
393 if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) {
394 dprintf ("Sysfs not supported on this system\n");
398 safestrcat(rootpath, "/");
399 safestrcat(rootpath, SYSFS_DEVICES_NAME);
400 safestrcat(rootpath, "/");
401 safestrcat(rootpath, name);
402 if ((sysfs_path_is_dir(rootpath)) != 0) {
404 dprintf("Invalid root device: %s\n", name);
407 root = (struct sysfs_root_device *)calloc
408 (1, sizeof(struct sysfs_root_device));
410 dprintf("calloc failure\n");
413 safestrcpy(root->name, name);
414 safestrcpy(root->path, rootpath);
415 if ((sysfs_remove_trailing_slash(root->path)) != 0) {
416 dprintf("Invalid path to root device %s\n", root->path);
417 sysfs_close_root_device(root);
424 * sysfs_get_device_attributes: returns a dlist of attributes corresponding to
425 * the specific device
426 * @device: struct sysfs_device * for which attributes are to be returned
428 struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
430 if (device == NULL) {
435 if (device->directory == NULL) {
436 device->directory = sysfs_open_directory(device->path);
437 if (device->directory == NULL)
440 if (device->directory->attributes == NULL) {
441 if ((sysfs_read_dir_attributes(device->directory)) != 0)
444 return (device->directory->attributes);
448 * sysfs_refresh_device_attributes: refreshes the device's list of attributes
449 * @device: sysfs_device whose attributes to refresh
451 * NOTE: Upon return, prior references to sysfs_attributes for this device
454 * Returns list of attributes on success and NULL on failure
456 struct dlist *sysfs_refresh_device_attributes(struct sysfs_device *device)
458 if (device == NULL) {
463 if (device->directory == NULL)
464 return (sysfs_get_device_attributes(device));
466 if ((sysfs_refresh_dir_attributes(device->directory)) != 0) {
467 dprintf("Error refreshing device attributes\n");
471 return (device->directory->attributes);
475 * sysfs_get_device_attr: searches dev's attributes by name
476 * @dev: device to look through
477 * @name: attribute name to get
478 * returns sysfs_attribute reference with success or NULL with error.
480 struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
483 struct dlist *attrlist = NULL;
485 if (dev == NULL || name == NULL) {
490 attrlist = sysfs_get_device_attributes(dev);
491 if (attrlist == NULL)
494 return sysfs_get_directory_attribute(dev->directory, (char *)name);
498 * get_device_absolute_path: looks up the bus the device is on, gets
499 * absolute path to the device
500 * @device: device for which path is needed
501 * @path: buffer to store absolute path
502 * @psize: size of "path"
503 * Returns 0 on success -1 on failure
505 static int get_device_absolute_path(const char *device, const char *bus,
506 char *path, size_t psize)
508 char bus_path[SYSFS_PATH_MAX];
510 if (device == NULL || path == NULL) {
515 memset(bus_path, 0, SYSFS_PATH_MAX);
516 if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX) != 0) {
517 dprintf ("Sysfs not supported on this system\n");
520 safestrcat(bus_path, "/");
521 safestrcat(bus_path, SYSFS_BUS_NAME);
522 safestrcat(bus_path, "/");
523 safestrcat(bus_path, bus);
524 safestrcat(bus_path, "/");
525 safestrcat(bus_path, SYSFS_DEVICES_NAME);
526 safestrcat(bus_path, "/");
527 safestrcat(bus_path, device);
529 * We now are at /sys/bus/"bus_name"/devices/"device" which is a link.
530 * Now read this link to reach to the device.
532 if ((sysfs_get_link(bus_path, path, psize)) != 0) {
533 dprintf("Error getting to device %s\n", device);
540 * sysfs_open_device: open a device by id (use the "bus" subsystem)
541 * @bus: bus the device belongs to
542 * @bus_id: bus_id of the device to open - has to be the "bus_id" in
543 * /sys/bus/xxx/devices
544 * returns struct sysfs_device if found, NULL otherwise
546 * 1. Use sysfs_close_device to close the device
547 * 2. Bus the device is on must be supplied
548 * Use sysfs_find_device_bus to get the bus name
550 struct sysfs_device *sysfs_open_device(const char *bus, const char *bus_id)
552 char sysfs_path[SYSFS_PATH_MAX];
553 struct sysfs_device *device = NULL;
555 if (bus_id == NULL || bus == NULL) {
559 memset(sysfs_path, 0, SYSFS_PATH_MAX);
560 if ((get_device_absolute_path(bus_id, bus, sysfs_path,
561 SYSFS_PATH_MAX)) != 0) {
562 dprintf("Error getting to device %s\n", bus_id);
566 device = sysfs_open_device_path(sysfs_path);
567 if (device == NULL) {
568 dprintf("Error opening device %s\n", bus_id);
576 * sysfs_get_device_parent: opens up given device's parent and returns a
577 * reference to its sysfs_device
578 * @dev: sysfs_device whose parent is requested
579 * Returns sysfs_device of the parent on success and NULL on failure
581 struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev)
583 char ppath[SYSFS_PATH_MAX], *tmp = NULL;
590 if (dev->parent != NULL)
591 return (dev->parent);
593 memset(ppath, 0, SYSFS_PATH_MAX);
594 safestrcpy(ppath, dev->path);
595 tmp = strrchr(ppath, '/');
597 dprintf("Invalid path to device %s\n", ppath);
600 if (*(tmp + 1) == '\0') {
602 tmp = strrchr(tmp, '/');
604 dprintf("Invalid path to device %s\n", ppath);
611 * All "devices" have the "detach_state" attribute - validate here
613 safestrcat(ppath, "/detach_state");
614 if ((sysfs_path_is_file(ppath)) != 0) {
615 dprintf("Device at %s does not have a parent\n", dev->path);
618 tmp = strrchr(ppath, '/');
620 dev->parent = sysfs_open_device_path(ppath);
621 if (dev->parent == NULL) {
622 dprintf("Error opening device %s's parent at %s\n",
626 return (dev->parent);
630 * sysfs_open_device_attr: open the given device's attribute
631 * @bus: Bus on which to look
632 * @dev_id: device for which attribute is required
633 * @attrname: name of the attribute to look for
634 * Returns struct sysfs_attribute on success and NULL on failure
637 * A call to sysfs_close_attribute() is required to close
638 * the attribute returned and free memory.
640 struct sysfs_attribute *sysfs_open_device_attr(const char *bus,
641 const char *bus_id, const char *attrib)
643 struct sysfs_attribute *attribute = NULL;
644 char devpath[SYSFS_PATH_MAX];
646 if (bus == NULL || bus_id == NULL || attrib == NULL) {
651 memset(devpath, 0, SYSFS_PATH_MAX);
652 if ((get_device_absolute_path(bus_id, bus, devpath,
653 SYSFS_PATH_MAX)) != 0) {
654 dprintf("Error getting to device %s\n", bus_id);
657 safestrcat(devpath, "/");
658 safestrcat(devpath, attrib);
659 attribute = sysfs_open_attribute(devpath);
660 if (attribute == NULL) {
661 dprintf("Error opening attribute %s for device %s\n",
665 if ((sysfs_read_attribute(attribute)) != 0) {
666 dprintf("Error reading attribute %s for device %s\n",
668 sysfs_close_attribute(attribute);