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
27 * sysfs_get_mnt_path: Gets the mount point for specified filesystem.
28 * @fs_type: filesystem type to retrieve mount point
29 * @mnt_path: place to put the retrieved mount path
30 * @len: size of mnt_path
31 * returns 0 with success and -1 with error.
33 static int sysfs_get_fs_mnt_path(const unsigned char *fs_type,
34 unsigned char *mnt_path, size_t len)
37 struct mntent *mntent;
42 if (fs_type == NULL || mnt_path == NULL) {
47 if ((mnt = setmntent(SYSFS_PROC_MNTS, "r")) == NULL) {
48 dprintf("Error getting mount information\n");
51 while (ret == 0 && dirlen == 0 && (mntent = getmntent(mnt)) != NULL) {
52 if (strcmp(mntent->mnt_type, fs_type) == 0) {
53 dirlen = strlen(mntent->mnt_dir);
54 if (dirlen <= (len - 1)) {
55 strcpy(mnt_path, mntent->mnt_dir);
57 dprintf("Error - mount path too long\n");
63 if (dirlen == 0 && ret == 0) {
64 dprintf("Filesystem %s not found!\n", fs_type);
72 * sysfs_get_mnt_path: Gets the sysfs mount point.
73 * @mnt_path: place to put "sysfs" mount point
74 * @len: size of mnt_path
75 * returns 0 with success and -1 with error.
77 int sysfs_get_mnt_path(unsigned char *mnt_path, size_t len)
82 ret = sysfs_get_fs_mnt_path(SYSFS_FSTYPE_NAME, mnt_path, len);
90 * sysfs_get_name_from_path: returns last name from a "/" delimited path
91 * @path: path to get name from
92 * @name: where to put name
95 int sysfs_get_name_from_path(const unsigned char *path, unsigned char *name,
98 unsigned char *n = NULL;
100 if (path == NULL || name == NULL) {
104 n = strrchr(path, '/');
110 strncpy(name, n, len);
116 * sysfs_get_link: returns link source
117 * @path: symbolic link's path
118 * @target: where to put name
121 int sysfs_get_link(const unsigned char *path, unsigned char *target, size_t len)
123 unsigned char devdir[SYSFS_PATH_MAX];
124 unsigned char linkpath[SYSFS_PATH_MAX];
125 unsigned char *d = NULL;
127 if (path == NULL || target == NULL) {
132 memset(devdir, 0, SYSFS_PATH_MAX);
133 memset(linkpath, 0, SYSFS_PATH_MAX);
135 if ((sysfs_get_mnt_path(devdir, SYSFS_PATH_MAX)) != 0) {
136 dprintf("Sysfs not supported on this system\n");
140 if ((readlink(path, linkpath, SYSFS_PATH_MAX)) < 0) {
146 /* getting rid of leading "../.." */
147 while (*d == '/' || *d == '.')
153 strncpy(target, devdir, len);
160 * sysfs_del_name: free function for sysfs_open_subsystem_list
161 * @name: memory area to be freed
163 void sysfs_del_name(void *name)
170 * sysfs_close_list: generic list free routine
171 * @list: dlist to free
174 void sysfs_close_list(struct dlist *list)
181 * sysfs_open_subsystem_list: gets a list of all supported "name" subsystem
182 * details from the system
183 * @name: name of the subsystem, eg., "bus", "class", "devices"
184 * Returns a dlist of supported names or NULL if subsystem not supported
186 struct dlist *sysfs_open_subsystem_list(unsigned char *name)
188 unsigned char sysfs_path[SYSFS_PATH_MAX], *subsys_name = NULL;
189 struct sysfs_directory *dir = NULL, *cur = NULL;
190 struct dlist *list = NULL;
195 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
196 dprintf("Error getting sysfs mount point\n");
200 strcat(sysfs_path, name);
201 dir = sysfs_open_directory(sysfs_path);
203 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
207 if (sysfs_read_directory(dir) != 0) {
208 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
209 sysfs_close_directory(dir);
213 if (dir->subdirs != NULL) {
214 list = dlist_new_with_delete(SYSFS_NAME_LEN,
217 dprintf("Error creating list\n");
218 sysfs_close_directory(dir);
222 dlist_for_each_data(dir->subdirs, cur,
223 struct sysfs_directory) {
224 subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
225 strcpy(subsys_name, cur->name);
226 dlist_unshift(list, subsys_name);
229 sysfs_close_directory(dir);
235 * sysfs_open_bus_devices_list: gets a list of all devices on "name" bus
236 * @name: name of the subsystem, eg., "pci", "scsi", "usb"
237 * Returns a dlist of supported names or NULL if subsystem not supported
239 struct dlist *sysfs_open_bus_devices_list(unsigned char *name)
241 unsigned char sysfs_path[SYSFS_PATH_MAX], *device_name = NULL;
242 struct sysfs_directory *dir = NULL;
243 struct sysfs_link *cur = NULL;
244 struct dlist *list = NULL;
249 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
250 dprintf("Error getting sysfs mount point\n");
254 strcat(sysfs_path, SYSFS_BUS_DIR);
255 strcat(sysfs_path, "/");
256 strcat(sysfs_path, name);
257 strcat(sysfs_path, SYSFS_DEVICES_DIR);
258 dir = sysfs_open_directory(sysfs_path);
260 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
264 if (sysfs_read_directory(dir) != 0) {
265 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
266 sysfs_close_directory(dir);
270 if (dir->links != NULL) {
271 list = dlist_new_with_delete(SYSFS_NAME_LEN,
274 dprintf("Error creating list\n");
275 sysfs_close_directory(dir);
279 dlist_for_each_data(dir->links, cur,
281 device_name = (char *)calloc(1, SYSFS_NAME_LEN);
282 strcpy(device_name, cur->name);
283 dlist_unshift(list, device_name);
286 sysfs_close_directory(dir);