4 * System 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
30 * sysfs_get_mnt_path: Gets the mount point for specified filesystem.
31 * @fs_type: filesystem type to retrieve mount point
32 * @mnt_path: place to put the retrieved mount path
33 * @len: size of mnt_path
34 * returns 0 with success and -1 with error.
36 static int sysfs_get_fs_mnt_path(const unsigned char *fs_type,
37 unsigned char *mnt_path, size_t len)
40 strcpy(mnt_path, "/sys");
44 struct mntent *mntent;
49 if (fs_type == NULL || mnt_path == NULL) {
54 if ((mnt = setmntent(SYSFS_PROC_MNTS, "r")) == NULL) {
55 dprintf("Error getting mount information\n");
58 while (ret == 0 && dirlen == 0 && (mntent = getmntent(mnt)) != NULL) {
59 if (strcmp(mntent->mnt_type, fs_type) == 0) {
60 dirlen = strlen(mntent->mnt_dir);
61 if (dirlen <= (len - 1)) {
62 strcpy(mnt_path, mntent->mnt_dir);
64 dprintf("Error - mount path too long\n");
70 if (dirlen == 0 && ret == 0) {
71 dprintf("Filesystem %s not found!\n", fs_type);
80 * sysfs_get_mnt_path: Gets the sysfs mount point.
81 * @mnt_path: place to put "sysfs" mount point
82 * @len: size of mnt_path
83 * returns 0 with success and -1 with error.
85 int sysfs_get_mnt_path(unsigned char *mnt_path, size_t len)
90 ret = sysfs_get_fs_mnt_path(SYSFS_FSTYPE_NAME, mnt_path, len);
98 * sysfs_get_name_from_path: returns last name from a "/" delimited path
99 * @path: path to get name from
100 * @name: where to put name
103 int sysfs_get_name_from_path(const unsigned char *path, unsigned char *name,
106 unsigned char *n = NULL;
108 if (path == NULL || name == NULL) {
112 n = strrchr(path, '/');
118 strncpy(name, n, len);
124 * sysfs_get_link: returns link source
125 * @path: symbolic link's path
126 * @target: where to put name
129 int sysfs_get_link(const unsigned char *path, unsigned char *target, size_t len)
131 unsigned char devdir[SYSFS_PATH_MAX];
132 unsigned char linkpath[SYSFS_PATH_MAX];
133 unsigned char *d = NULL;
135 if (path == NULL || target == NULL) {
140 memset(devdir, 0, SYSFS_PATH_MAX);
141 memset(linkpath, 0, SYSFS_PATH_MAX);
143 if ((sysfs_get_mnt_path(devdir, SYSFS_PATH_MAX)) != 0) {
144 dprintf("Sysfs not supported on this system\n");
148 if ((readlink(path, linkpath, SYSFS_PATH_MAX)) < 0) {
154 /* getting rid of leading "../.." */
155 while (*d == '/' || *d == '.')
161 strncpy(target, devdir, len);
168 * sysfs_del_name: free function for sysfs_open_subsystem_list
169 * @name: memory area to be freed
171 void sysfs_del_name(void *name)
178 * sysfs_close_list: generic list free routine
179 * @list: dlist to free
182 void sysfs_close_list(struct dlist *list)
189 * sysfs_open_subsystem_list: gets a list of all supported "name" subsystem
190 * details from the system
191 * @name: name of the subsystem, eg., "bus", "class", "devices"
192 * Returns a dlist of supported names or NULL if subsystem not supported
194 struct dlist *sysfs_open_subsystem_list(unsigned char *name)
196 unsigned char sysfs_path[SYSFS_PATH_MAX], *subsys_name = NULL;
197 struct sysfs_directory *dir = NULL, *cur = NULL;
198 struct dlist *list = NULL;
203 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
204 dprintf("Error getting sysfs mount point\n");
208 strcat(sysfs_path, name);
209 dir = sysfs_open_directory(sysfs_path);
211 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
215 if (sysfs_read_directory(dir) != 0) {
216 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
217 sysfs_close_directory(dir);
221 if (dir->subdirs != NULL) {
222 list = dlist_new_with_delete(SYSFS_NAME_LEN,
225 dprintf("Error creating list\n");
226 sysfs_close_directory(dir);
230 dlist_for_each_data(dir->subdirs, cur,
231 struct sysfs_directory) {
232 subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
233 strcpy(subsys_name, cur->name);
234 dlist_unshift(list, subsys_name);
237 sysfs_close_directory(dir);
243 * sysfs_open_bus_devices_list: gets a list of all devices on "name" bus
244 * @name: name of the subsystem, eg., "pci", "scsi", "usb"
245 * Returns a dlist of supported names or NULL if subsystem not supported
247 struct dlist *sysfs_open_bus_devices_list(unsigned char *name)
249 unsigned char sysfs_path[SYSFS_PATH_MAX], *device_name = NULL;
250 struct sysfs_directory *dir = NULL;
251 struct sysfs_link *cur = NULL;
252 struct dlist *list = NULL;
257 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
258 dprintf("Error getting sysfs mount point\n");
262 strcat(sysfs_path, SYSFS_BUS_DIR);
263 strcat(sysfs_path, "/");
264 strcat(sysfs_path, name);
265 strcat(sysfs_path, SYSFS_DEVICES_DIR);
266 dir = sysfs_open_directory(sysfs_path);
268 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
272 if (sysfs_read_directory(dir) != 0) {
273 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
274 sysfs_close_directory(dir);
278 if (dir->links != NULL) {
279 list = dlist_new_with_delete(SYSFS_NAME_LEN,
282 dprintf("Error creating list\n");
283 sysfs_close_directory(dir);
287 dlist_for_each_data(dir->links, cur,
289 device_name = (char *)calloc(1, SYSFS_NAME_LEN);
290 strcpy(device_name, cur->name);
291 dlist_unshift(list, device_name);
294 sysfs_close_directory(dir);