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_remove_trailing_slash: Removes any trailing '/' in the given path
31 * @path: Path to look for the trailing '/'
32 * Returns 0 on success 1 on error
34 int sysfs_remove_trailing_slash(unsigned char *path)
36 unsigned char *c = NULL;
42 c = strrchr(path, '/');
44 dprintf("Invalid path %s\n", path);
54 * sysfs_get_mnt_path: Gets the mount point for specified filesystem.
55 * @fs_type: filesystem type to retrieve mount point
56 * @mnt_path: place to put the retrieved mount path
57 * @len: size of mnt_path
58 * returns 0 with success and -1 with error.
60 static int sysfs_get_fs_mnt_path(const unsigned char *fs_type,
61 unsigned char *mnt_path, size_t len)
64 strcpy(mnt_path, "/sys");
68 struct mntent *mntent;
73 if (fs_type == NULL || mnt_path == NULL) {
78 if ((mnt = setmntent(SYSFS_PROC_MNTS, "r")) == NULL) {
79 dprintf("Error getting mount information\n");
82 while (ret == 0 && dirlen == 0 && (mntent = getmntent(mnt)) != NULL) {
83 if (strcmp(mntent->mnt_type, fs_type) == 0) {
84 dirlen = strlen(mntent->mnt_dir);
85 if (dirlen <= (len - 1)) {
86 strcpy(mnt_path, mntent->mnt_dir);
88 dprintf("Error - mount path too long\n");
94 if (dirlen == 0 && ret == 0) {
95 dprintf("Filesystem %s not found!\n", fs_type);
99 if ((sysfs_remove_trailing_slash(mnt_path)) != 0)
107 * sysfs_get_mnt_path: Gets the sysfs mount point.
108 * @mnt_path: place to put "sysfs" mount point
109 * @len: size of mnt_path
110 * returns 0 with success and -1 with error.
112 int sysfs_get_mnt_path(unsigned char *mnt_path, size_t len)
114 char *sysfs_path = NULL;
117 if (mnt_path == NULL) {
121 sysfs_path = getenv(SYSFS_PATH_ENV);
122 if (sysfs_path != NULL) {
123 strncpy(mnt_path, sysfs_path, len);
124 if ((sysfs_remove_trailing_slash(mnt_path)) != 0)
127 ret = sysfs_get_fs_mnt_path(SYSFS_FSTYPE_NAME, mnt_path, len);
133 * sysfs_get_name_from_path: returns last name from a "/" delimited path
134 * @path: path to get name from
135 * @name: where to put name
138 int sysfs_get_name_from_path(const unsigned char *path, unsigned char *name,
141 unsigned char tmp[SYSFS_PATH_MAX];
142 unsigned char *n = NULL;
144 if (path == NULL || name == NULL) {
148 memset(tmp, 0, SYSFS_PATH_MAX);
150 n = strrchr(tmp, '/');
155 if (*(n+1) == '\0') {
157 n = strrchr(tmp, '/');
164 strncpy(name, n, len);
169 * sysfs_get_link: returns link source
170 * @path: symbolic link's path
171 * @target: where to put name
174 int sysfs_get_link(const unsigned char *path, unsigned char *target, size_t len)
176 unsigned char devdir[SYSFS_PATH_MAX];
177 unsigned char linkpath[SYSFS_PATH_MAX];
178 unsigned char *d = NULL, *s = NULL;
179 int slashes = 0, count = 0;
181 if (path == NULL || target == NULL) {
186 memset(devdir, 0, SYSFS_PATH_MAX);
187 memset(linkpath, 0, SYSFS_PATH_MAX);
188 strncpy(devdir, path, SYSFS_PATH_MAX);
190 if ((readlink(path, linkpath, SYSFS_PATH_MAX)) < 0) {
196 * 1. relative path => format ../..
197 * 2. absolute path => format /abcd/efgh
198 * 3. relative path _from_ this dir => format abcd/efgh
203 * handle the case where link is of type ./abcd/xxx
205 strncpy(target, devdir, len);
208 else if (*(d+1) == '.')
210 s = strrchr(target, '/');
220 * getting rid of leading "../.."
223 while (*d == '/' || *d == '.') {
229 s = &devdir[strlen(devdir)-1];
230 while (s != NULL && count != (slashes+1)) {
235 strncpy(s, d, (SYSFS_PATH_MAX-strlen(devdir)));
236 strncpy(target, devdir, len);
239 /* absolute path - copy as is */
240 strncpy(target, linkpath, len);
243 /* relative path from this directory */
244 strncpy(target, devdir, len);
245 s = strrchr(target, '/');
248 strcat(target, linkpath);
250 strcpy(target, linkpath);
257 * sysfs_del_name: free function for sysfs_open_subsystem_list
258 * @name: memory area to be freed
260 static void sysfs_del_name(void *name)
267 * sysfs_close_list: generic list free routine
268 * @list: dlist to free
271 void sysfs_close_list(struct dlist *list)
278 * sysfs_open_subsystem_list: gets a list of all supported "name" subsystem
279 * details from the system
280 * @name: name of the subsystem, eg., "bus", "class", "devices"
281 * Returns a dlist of supported names or NULL if subsystem not supported
283 struct dlist *sysfs_open_subsystem_list(unsigned char *name)
285 unsigned char sysfs_path[SYSFS_PATH_MAX], *subsys_name = NULL;
286 unsigned char *c = NULL;
287 struct sysfs_directory *dir = NULL, *cur = NULL;
288 struct dlist *list = NULL;
293 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
294 dprintf("Error getting sysfs mount point\n");
298 strcat(sysfs_path, "/");
299 strcat(sysfs_path, name);
300 dir = sysfs_open_directory(sysfs_path);
302 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
306 if ((sysfs_read_dir_subdirs(dir)) != 0) {
307 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
308 sysfs_close_directory(dir);
312 if (dir->subdirs != NULL) {
313 list = dlist_new_with_delete(SYSFS_NAME_LEN,
316 dprintf("Error creating list\n");
317 sysfs_close_directory(dir);
321 dlist_for_each_data(dir->subdirs, cur,
322 struct sysfs_directory) {
323 subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
324 strcpy(subsys_name, cur->name);
325 dlist_unshift(list, subsys_name);
328 sysfs_close_directory(dir);
330 * We are now considering "block" as a "class". Hence, if the subsys
331 * name requested here is "class", verify if "block" is supported on
332 * this system and return the same.
334 if (strcmp(name, SYSFS_CLASS_NAME) == 0) {
335 c = strstr(sysfs_path, SYSFS_CLASS_NAME);
338 strcpy(c, SYSFS_BLOCK_NAME);
339 if ((sysfs_path_is_dir(sysfs_path)) == 0) {
340 subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
341 strcpy(subsys_name, SYSFS_BLOCK_NAME);
342 dlist_unshift(list, subsys_name);
351 * sysfs_open_bus_devices_list: gets a list of all devices on "name" bus
352 * @name: name of the subsystem, eg., "pci", "scsi", "usb"
353 * Returns a dlist of supported names or NULL if subsystem not supported
355 struct dlist *sysfs_open_bus_devices_list(unsigned char *name)
357 unsigned char sysfs_path[SYSFS_PATH_MAX], *device_name = NULL;
358 struct sysfs_directory *dir = NULL;
359 struct sysfs_link *cur = NULL;
360 struct dlist *list = NULL;
365 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
366 dprintf("Error getting sysfs mount point\n");
370 strcat(sysfs_path, "/");
371 strcat(sysfs_path, SYSFS_BUS_NAME);
372 strcat(sysfs_path, "/");
373 strcat(sysfs_path, name);
374 strcat(sysfs_path, "/");
375 strcat(sysfs_path, SYSFS_DEVICES_NAME);
376 dir = sysfs_open_directory(sysfs_path);
378 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
382 if ((sysfs_read_dir_links(dir)) != 0) {
383 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
384 sysfs_close_directory(dir);
388 if (dir->links != NULL) {
389 list = dlist_new_with_delete(SYSFS_NAME_LEN,
392 dprintf("Error creating list\n");
393 sysfs_close_directory(dir);
397 dlist_for_each_data(dir->links, cur,
399 device_name = (char *)calloc(1, SYSFS_NAME_LEN);
400 strcpy(device_name, cur->name);
401 dlist_unshift(list, device_name);
404 sysfs_close_directory(dir);
409 * sysfs_path_is_dir: Check if the path supplied points to a directory
410 * @path: path to validate
411 * Returns 0 if path points to dir, 1 otherwise
413 int sysfs_path_is_dir(const unsigned char *path)
421 if ((lstat(path, &astats)) != 0) {
422 dprintf("stat() failed\n");
425 if (S_ISDIR(astats.st_mode))
432 * sysfs_path_is_link: Check if the path supplied points to a link
433 * @path: path to validate
434 * Returns 0 if path points to link, 1 otherwise
436 int sysfs_path_is_link(const unsigned char *path)
444 if ((lstat(path, &astats)) != 0) {
445 dprintf("stat() failed\n");
448 if (S_ISLNK(astats.st_mode))
455 * sysfs_path_is_file: Check if the path supplied points to a file
456 * @path: path to validate
457 * Returns 0 if path points to file, 1 otherwise
459 int sysfs_path_is_file(const unsigned char *path)
467 if ((lstat(path, &astats)) != 0) {
468 dprintf("stat() failed\n");
471 if (S_ISREG(astats.st_mode))