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)
87 char *sysfs_path = NULL;
90 if (mnt_path == NULL) {
94 sysfs_path = getenv(SYSFS_PATH_ENV);
95 if (sysfs_path != NULL)
96 strncpy(mnt_path, sysfs_path, len);
98 ret = sysfs_get_fs_mnt_path(SYSFS_FSTYPE_NAME, mnt_path, len);
104 * sysfs_get_name_from_path: returns last name from a "/" delimited path
105 * @path: path to get name from
106 * @name: where to put name
109 int sysfs_get_name_from_path(const unsigned char *path, unsigned char *name,
112 unsigned char *n = NULL;
114 if (path == NULL || name == NULL) {
118 n = strrchr(path, '/');
124 strncpy(name, n, len);
130 * sysfs_get_link: returns link source
131 * @path: symbolic link's path
132 * @target: where to put name
135 int sysfs_get_link(const unsigned char *path, unsigned char *target, size_t len)
137 unsigned char devdir[SYSFS_PATH_MAX];
138 unsigned char linkpath[SYSFS_PATH_MAX];
139 unsigned char *d = NULL, *s = NULL;
140 int slashes = 0, count = 0;
142 if (path == NULL || target == NULL) {
147 memset(devdir, 0, SYSFS_PATH_MAX);
148 memset(linkpath, 0, SYSFS_PATH_MAX);
149 strncpy(devdir, path, SYSFS_PATH_MAX);
151 if ((readlink(path, linkpath, SYSFS_PATH_MAX)) < 0) {
157 /* getting rid of leading "../.." */
158 while (*d == '/' || *d == '.') {
166 s = &devdir[strlen(devdir)-1];
167 while (s != NULL && count != (slashes+1)) {
176 strncpy(s, d, (SYSFS_PATH_MAX-strlen(devdir)));
177 strncpy(target, devdir, len);
184 * sysfs_del_name: free function for sysfs_open_subsystem_list
185 * @name: memory area to be freed
187 void sysfs_del_name(void *name)
194 * sysfs_close_list: generic list free routine
195 * @list: dlist to free
198 void sysfs_close_list(struct dlist *list)
205 * sysfs_open_subsystem_list: gets a list of all supported "name" subsystem
206 * details from the system
207 * @name: name of the subsystem, eg., "bus", "class", "devices"
208 * Returns a dlist of supported names or NULL if subsystem not supported
210 struct dlist *sysfs_open_subsystem_list(unsigned char *name)
212 unsigned char sysfs_path[SYSFS_PATH_MAX], *subsys_name = NULL;
213 struct sysfs_directory *dir = NULL, *cur = NULL;
214 struct dlist *list = NULL;
219 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
220 dprintf("Error getting sysfs mount point\n");
224 strcat(sysfs_path, name);
225 dir = sysfs_open_directory(sysfs_path);
227 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
231 if (sysfs_read_directory(dir) != 0) {
232 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
233 sysfs_close_directory(dir);
237 if (dir->subdirs != NULL) {
238 list = dlist_new_with_delete(SYSFS_NAME_LEN,
241 dprintf("Error creating list\n");
242 sysfs_close_directory(dir);
246 dlist_for_each_data(dir->subdirs, cur,
247 struct sysfs_directory) {
248 subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
249 strcpy(subsys_name, cur->name);
250 dlist_unshift(list, subsys_name);
253 sysfs_close_directory(dir);
259 * sysfs_open_bus_devices_list: gets a list of all devices on "name" bus
260 * @name: name of the subsystem, eg., "pci", "scsi", "usb"
261 * Returns a dlist of supported names or NULL if subsystem not supported
263 struct dlist *sysfs_open_bus_devices_list(unsigned char *name)
265 unsigned char sysfs_path[SYSFS_PATH_MAX], *device_name = NULL;
266 struct sysfs_directory *dir = NULL;
267 struct sysfs_link *cur = NULL;
268 struct dlist *list = NULL;
273 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
274 dprintf("Error getting sysfs mount point\n");
278 strcat(sysfs_path, SYSFS_BUS_DIR);
279 strcat(sysfs_path, "/");
280 strcat(sysfs_path, name);
281 strcat(sysfs_path, SYSFS_DEVICES_DIR);
282 dir = sysfs_open_directory(sysfs_path);
284 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
288 if (sysfs_read_directory(dir) != 0) {
289 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
290 sysfs_close_directory(dir);
294 if (dir->links != NULL) {
295 list = dlist_new_with_delete(SYSFS_NAME_LEN,
298 dprintf("Error creating list\n");
299 sysfs_close_directory(dir);
303 dlist_for_each_data(dir->links, cur,
305 device_name = (char *)calloc(1, SYSFS_NAME_LEN);
306 strcpy(device_name, cur->name);
307 dlist_unshift(list, device_name);
310 sysfs_close_directory(dir);