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
23 #include "sysfs/libsysfs.h"
29 static int sort_char(void *new_elem, void *old_elem)
31 return ((strncmp((char *)new_elem, (char *)old_elem,
32 strlen((char *)new_elem))) < 0 ? 1 : 0);
36 * sysfs_remove_trailing_slash: Removes any trailing '/' in the given path
37 * @path: Path to look for the trailing '/'
38 * Returns 0 on success 1 on error
40 int sysfs_remove_trailing_slash(char *path)
48 c = strrchr(path, '/');
50 dprintf("Invalid path %s\n", path);
60 * sysfs_get_fs_mnt_path: Gets the mount point for specified filesystem.
61 * @fs_type: filesystem type to retrieve mount point
62 * @mnt_path: place to put the retrieved mount path
63 * @len: size of mnt_path
64 * returns 0 with success and -1 with error.
66 static int sysfs_get_fs_mnt_path(const char *fs_type,
67 char *mnt_path, size_t len)
70 safestrcpymax(mnt_path, "/sys", len);
74 struct mntent *mntent;
79 if (fs_type == NULL || mnt_path == NULL || len == 0) {
84 if ((mnt = setmntent(SYSFS_PROC_MNTS, "r")) == NULL) {
85 dprintf("Error getting mount information\n");
88 while (ret == 0 && dirlen == 0 && (mntent = getmntent(mnt)) != NULL) {
89 if (strcmp(mntent->mnt_type, fs_type) == 0) {
90 dirlen = strlen(mntent->mnt_dir);
91 if (dirlen <= (len - 1)) {
92 safestrcpymax(mnt_path, mntent->mnt_dir, len);
94 dprintf("Error - mount path too long\n");
100 if (dirlen == 0 && ret == 0) {
101 dprintf("Filesystem %s not found!\n", fs_type);
105 if ((sysfs_remove_trailing_slash(mnt_path)) != 0)
113 * sysfs_get_mnt_path: Gets the sysfs mount point.
114 * @mnt_path: place to put "sysfs" mount point
115 * @len: size of mnt_path
116 * returns 0 with success and -1 with error.
118 int sysfs_get_mnt_path(char *mnt_path, size_t len)
120 char *sysfs_path = NULL;
123 if (mnt_path == NULL || len == 0) {
127 sysfs_path = getenv(SYSFS_PATH_ENV);
128 if (sysfs_path != NULL) {
129 safestrcpymax(mnt_path, sysfs_path, len);
130 if ((sysfs_remove_trailing_slash(mnt_path)) != 0)
133 ret = sysfs_get_fs_mnt_path(SYSFS_FSTYPE_NAME, mnt_path, len);
139 * sysfs_get_name_from_path: returns last name from a "/" delimited path
140 * @path: path to get name from
141 * @name: where to put name
144 int sysfs_get_name_from_path(const char *path, char *name, size_t len)
146 char tmp[SYSFS_PATH_MAX];
149 if (path == NULL || name == NULL || len == 0) {
153 memset(tmp, 0, SYSFS_PATH_MAX);
154 safestrcpy(tmp, path);
155 n = strrchr(tmp, '/');
160 if (*(n+1) == '\0') {
162 n = strrchr(tmp, '/');
169 safestrcpymax(name, n, len);
174 * sysfs_get_link: returns link source
175 * @path: symbolic link's path
176 * @target: where to put name
179 int sysfs_get_link(const char *path, char *target, size_t len)
181 char devdir[SYSFS_PATH_MAX];
182 char linkpath[SYSFS_PATH_MAX];
183 char temp_path[SYSFS_PATH_MAX];
184 char *d = NULL, *s = NULL;
185 int slashes = 0, count = 0;
187 if (path == NULL || target == NULL || len == 0) {
192 memset(devdir, 0, SYSFS_PATH_MAX);
193 memset(linkpath, 0, SYSFS_PATH_MAX);
194 memset(temp_path, 0, SYSFS_PATH_MAX);
195 safestrcpy(devdir, path);
197 if ((readlink(path, linkpath, SYSFS_PATH_MAX)) < 0) {
203 * 1. relative path => format ../..
204 * 2. absolute path => format /abcd/efgh
205 * 3. relative path _from_ this dir => format abcd/efgh
210 * handle the case where link is of type ./abcd/xxx
212 safestrcpy(temp_path, devdir);
215 else if (*(d+1) == '.')
217 s = strrchr(temp_path, '/');
220 safestrcat(temp_path, d);
222 safestrcpy(temp_path, d);
224 safestrcpymax(target, temp_path, len);
228 * getting rid of leading "../.."
231 while (*d == '/' || *d == '.') {
237 s = &devdir[strlen(devdir)-1];
238 while (s != NULL && count != (slashes+1)) {
243 safestrcpymax(s, d, (SYSFS_PATH_MAX-strlen(devdir)));
244 safestrcpymax(target, devdir, len);
247 /* absolute path - copy as is */
248 safestrcpymax(target, linkpath, len);
251 /* relative path from this directory */
252 safestrcpy(temp_path, devdir);
253 s = strrchr(temp_path, '/');
256 safestrcat(temp_path, linkpath);
258 safestrcpy(temp_path, linkpath);
260 safestrcpymax(target, temp_path, len);
266 * sysfs_del_name: free function for sysfs_open_subsystem_list
267 * @name: memory area to be freed
269 static void sysfs_del_name(void *name)
276 * sysfs_close_list: generic list free routine
277 * @list: dlist to free
280 void sysfs_close_list(struct dlist *list)
287 * sysfs_open_subsystem_list: gets a list of all supported "name" subsystem
288 * details from the system
289 * @name: name of the subsystem, eg., "bus", "class", "devices"
290 * Returns a dlist of supported names or NULL if subsystem not supported
292 struct dlist *sysfs_open_subsystem_list(char *name)
294 char sysfs_path[SYSFS_PATH_MAX], *subsys_name = NULL;
296 struct sysfs_directory *dir = NULL, *cur = NULL;
297 struct dlist *list = NULL;
302 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
303 dprintf("Error getting sysfs mount point\n");
307 safestrcat(sysfs_path, "/");
308 safestrcat(sysfs_path, name);
309 dir = sysfs_open_directory(sysfs_path);
311 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
315 if ((sysfs_read_dir_subdirs(dir)) != 0) {
316 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
317 sysfs_close_directory(dir);
321 if (dir->subdirs != NULL) {
322 list = dlist_new_with_delete(SYSFS_NAME_LEN,
325 dprintf("Error creating list\n");
326 sysfs_close_directory(dir);
330 dlist_for_each_data(dir->subdirs, cur,
331 struct sysfs_directory) {
332 subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
333 safestrcpymax(subsys_name, cur->name, SYSFS_NAME_LEN);
334 dlist_unshift_sorted(list, subsys_name, sort_char);
337 sysfs_close_directory(dir);
339 * We are now considering "block" as a "class". Hence, if the subsys
340 * name requested here is "class", verify if "block" is supported on
341 * this system and return the same.
343 if (strcmp(name, SYSFS_CLASS_NAME) == 0) {
344 c = strstr(sysfs_path, SYSFS_CLASS_NAME);
348 safestrcpymax(c, SYSFS_BLOCK_NAME,
349 sizeof(sysfs_path) - strlen(sysfs_path));
350 if ((sysfs_path_is_dir(sysfs_path)) == 0) {
351 subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
352 safestrcpymax(subsys_name, SYSFS_BLOCK_NAME,
354 dlist_unshift_sorted(list, subsys_name, sort_char);
363 * sysfs_open_bus_devices_list: gets a list of all devices on "name" bus
364 * @name: name of the subsystem, eg., "pci", "scsi", "usb"
365 * Returns a dlist of supported names or NULL if subsystem not supported
367 struct dlist *sysfs_open_bus_devices_list(char *name)
369 char sysfs_path[SYSFS_PATH_MAX], *device_name = NULL;
370 struct sysfs_directory *dir = NULL;
371 struct sysfs_link *cur = NULL;
372 struct dlist *list = NULL;
377 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
378 dprintf("Error getting sysfs mount point\n");
382 safestrcat(sysfs_path, "/");
383 safestrcat(sysfs_path, SYSFS_BUS_NAME);
384 safestrcat(sysfs_path, "/");
385 safestrcat(sysfs_path, name);
386 safestrcat(sysfs_path, "/");
387 safestrcat(sysfs_path, SYSFS_DEVICES_NAME);
388 dir = sysfs_open_directory(sysfs_path);
390 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
394 if ((sysfs_read_dir_links(dir)) != 0) {
395 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
396 sysfs_close_directory(dir);
400 if (dir->links != NULL) {
401 list = dlist_new_with_delete(SYSFS_NAME_LEN,
404 dprintf("Error creating list\n");
405 sysfs_close_directory(dir);
409 dlist_for_each_data(dir->links, cur,
411 device_name = (char *)calloc(1, SYSFS_NAME_LEN);
412 safestrcpymax(device_name, cur->name, SYSFS_NAME_LEN);
413 dlist_unshift_sorted(list, device_name, sort_char);
416 sysfs_close_directory(dir);
421 * sysfs_path_is_dir: Check if the path supplied points to a directory
422 * @path: path to validate
423 * Returns 0 if path points to dir, 1 otherwise
425 int sysfs_path_is_dir(const char *path)
433 if ((lstat(path, &astats)) != 0) {
434 dprintf("stat() failed\n");
437 if (S_ISDIR(astats.st_mode))
444 * sysfs_path_is_link: Check if the path supplied points to a link
445 * @path: path to validate
446 * Returns 0 if path points to link, 1 otherwise
448 int sysfs_path_is_link(const char *path)
456 if ((lstat(path, &astats)) != 0) {
457 dprintf("stat() failed\n");
460 if (S_ISLNK(astats.st_mode))
467 * sysfs_path_is_file: Check if the path supplied points to a file
468 * @path: path to validate
469 * Returns 0 if path points to file, 1 otherwise
471 int sysfs_path_is_file(const char *path)
479 if ((lstat(path, &astats)) != 0) {
480 dprintf("stat() failed\n");
483 if (S_ISREG(astats.st_mode))