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
26 static int sort_char(void *new, void *old)
28 return ((strncmp((char *)new, (char *)old,
29 strlen((char *)new))) < 0 ? 1 : 0);
33 * sysfs_remove_trailing_slash: Removes any trailing '/' in the given path
34 * @path: Path to look for the trailing '/'
35 * Returns 0 on success 1 on error
37 int sysfs_remove_trailing_slash(char *path)
45 c = strrchr(path, '/');
47 dprintf("Invalid path %s\n", path);
57 * sysfs_get_fs_mnt_path: Gets the mount point for specified filesystem.
58 * @fs_type: filesystem type to retrieve mount point
59 * @mnt_path: place to put the retrieved mount path
60 * @len: size of mnt_path
61 * returns 0 with success and -1 with error.
63 static int sysfs_get_fs_mnt_path(const char *fs_type,
64 char *mnt_path, size_t len)
67 struct mntent *mntent;
72 if (fs_type == NULL || mnt_path == NULL || len == 0) {
77 if ((mnt = setmntent(SYSFS_PROC_MNTS, "r")) == NULL) {
78 dprintf("Error getting mount information\n");
81 while (ret == 0 && dirlen == 0 && (mntent = getmntent(mnt)) != NULL) {
82 if (strcmp(mntent->mnt_type, fs_type) == 0) {
83 dirlen = strlen(mntent->mnt_dir);
84 if (dirlen <= (len - 1)) {
85 safestrcpymax(mnt_path, mntent->mnt_dir, len);
87 dprintf("Error - mount path too long\n");
93 if (dirlen == 0 && ret == 0) {
94 dprintf("Filesystem %s not found!\n", fs_type);
98 if ((sysfs_remove_trailing_slash(mnt_path)) != 0)
105 * sysfs_get_mnt_path: Gets the sysfs mount point.
106 * @mnt_path: place to put "sysfs" mount point
107 * @len: size of mnt_path
108 * returns 0 with success and -1 with error.
110 int sysfs_get_mnt_path(char *mnt_path, size_t len)
112 char *sysfs_path = NULL;
115 if (mnt_path == NULL || len == 0) {
119 sysfs_path = getenv(SYSFS_PATH_ENV);
120 if (sysfs_path != NULL) {
121 safestrcpymax(mnt_path, sysfs_path, len);
122 if ((sysfs_remove_trailing_slash(mnt_path)) != 0)
125 ret = sysfs_get_fs_mnt_path(SYSFS_FSTYPE_NAME, mnt_path, len);
131 * sysfs_get_name_from_path: returns last name from a "/" delimited path
132 * @path: path to get name from
133 * @name: where to put name
136 int sysfs_get_name_from_path(const char *path, char *name, size_t len)
138 char tmp[SYSFS_PATH_MAX];
141 if (path == NULL || name == NULL || len == 0) {
145 memset(tmp, 0, SYSFS_PATH_MAX);
146 safestrcpy(tmp, path);
147 n = strrchr(tmp, '/');
152 if (*(n+1) == '\0') {
154 n = strrchr(tmp, '/');
161 safestrcpymax(name, n, len);
166 * sysfs_get_link: returns link source
167 * @path: symbolic link's path
168 * @target: where to put name
171 int sysfs_get_link(const char *path, char *target, size_t len)
173 char devdir[SYSFS_PATH_MAX];
174 char linkpath[SYSFS_PATH_MAX];
175 char temp_path[SYSFS_PATH_MAX];
176 char *d = NULL, *s = NULL;
177 int slashes = 0, count = 0;
179 if (path == NULL || target == NULL || len == 0) {
184 memset(devdir, 0, SYSFS_PATH_MAX);
185 memset(linkpath, 0, SYSFS_PATH_MAX);
186 memset(temp_path, 0, SYSFS_PATH_MAX);
187 safestrcpy(devdir, path);
189 if ((readlink(path, linkpath, SYSFS_PATH_MAX)) < 0) {
195 * 1. relative path => format ../..
196 * 2. absolute path => format /abcd/efgh
197 * 3. relative path _from_ this dir => format abcd/efgh
202 * handle the case where link is of type ./abcd/xxx
204 safestrcpy(temp_path, devdir);
207 else if (*(d+1) == '.')
209 s = strrchr(temp_path, '/');
212 safestrcat(temp_path, d);
214 safestrcpy(temp_path, d);
216 safestrcpymax(target, temp_path, len);
220 * getting rid of leading "../.."
223 while (*d == '/' || *d == '.') {
229 s = &devdir[strlen(devdir)-1];
230 while (s != NULL && count != (slashes+1)) {
235 safestrcpymax(s, d, (SYSFS_PATH_MAX-strlen(devdir)));
236 safestrcpymax(target, devdir, len);
239 /* absolute path - copy as is */
240 safestrcpymax(target, linkpath, len);
243 /* relative path from this directory */
244 safestrcpy(temp_path, devdir);
245 s = strrchr(temp_path, '/');
248 safestrcat(temp_path, linkpath);
250 safestrcpy(temp_path, linkpath);
252 safestrcpymax(target, temp_path, len);
258 * sysfs_del_name: free function for sysfs_open_subsystem_list
259 * @name: memory area to be freed
261 static void sysfs_del_name(void *name)
268 * sysfs_close_list: generic list free routine
269 * @list: dlist to free
272 void sysfs_close_list(struct dlist *list)
279 * sysfs_open_subsystem_list: gets a list of all supported "name" subsystem
280 * details from the system
281 * @name: name of the subsystem, eg., "bus", "class", "devices"
282 * Returns a dlist of supported names or NULL if subsystem not supported
284 struct dlist *sysfs_open_subsystem_list(char *name)
286 char sysfs_path[SYSFS_PATH_MAX], *subsys_name = NULL;
288 struct sysfs_directory *dir = NULL, *cur = NULL;
289 struct dlist *list = NULL;
294 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
295 dprintf("Error getting sysfs mount point\n");
299 safestrcat(sysfs_path, "/");
300 safestrcat(sysfs_path, name);
301 dir = sysfs_open_directory(sysfs_path);
303 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
307 if ((sysfs_read_dir_subdirs(dir)) != 0) {
308 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
309 sysfs_close_directory(dir);
313 if (dir->subdirs != NULL) {
314 list = dlist_new_with_delete(SYSFS_NAME_LEN,
317 dprintf("Error creating list\n");
318 sysfs_close_directory(dir);
322 dlist_for_each_data(dir->subdirs, cur,
323 struct sysfs_directory) {
324 subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
325 safestrcpymax(subsys_name, cur->name, SYSFS_NAME_LEN);
326 dlist_unshift_sorted(list, subsys_name, sort_char);
329 sysfs_close_directory(dir);
331 * We are now considering "block" as a "class". Hence, if the subsys
332 * name requested here is "class", verify if "block" is supported on
333 * this system and return the same.
335 if (strcmp(name, SYSFS_CLASS_NAME) == 0) {
336 c = strstr(sysfs_path, SYSFS_CLASS_NAME);
340 safestrcpymax(c, SYSFS_BLOCK_NAME,
341 sizeof(sysfs_path) - strlen(sysfs_path));
342 if ((sysfs_path_is_dir(sysfs_path)) == 0) {
343 subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
344 safestrcpymax(subsys_name, SYSFS_BLOCK_NAME,
346 dlist_unshift_sorted(list, subsys_name, sort_char);
355 * sysfs_open_bus_devices_list: gets a list of all devices on "name" bus
356 * @name: name of the subsystem, eg., "pci", "scsi", "usb"
357 * Returns a dlist of supported names or NULL if subsystem not supported
359 struct dlist *sysfs_open_bus_devices_list(char *name)
361 char sysfs_path[SYSFS_PATH_MAX], *device_name = NULL;
362 struct sysfs_directory *dir = NULL;
363 struct sysfs_link *cur = NULL;
364 struct dlist *list = NULL;
369 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
370 dprintf("Error getting sysfs mount point\n");
374 safestrcat(sysfs_path, "/");
375 safestrcat(sysfs_path, SYSFS_BUS_NAME);
376 safestrcat(sysfs_path, "/");
377 safestrcat(sysfs_path, name);
378 safestrcat(sysfs_path, "/");
379 safestrcat(sysfs_path, SYSFS_DEVICES_NAME);
380 dir = sysfs_open_directory(sysfs_path);
382 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
386 if ((sysfs_read_dir_links(dir)) != 0) {
387 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
388 sysfs_close_directory(dir);
392 if (dir->links != NULL) {
393 list = dlist_new_with_delete(SYSFS_NAME_LEN,
396 dprintf("Error creating list\n");
397 sysfs_close_directory(dir);
401 dlist_for_each_data(dir->links, cur,
403 device_name = (char *)calloc(1, SYSFS_NAME_LEN);
404 safestrcpymax(device_name, cur->name, SYSFS_NAME_LEN);
405 dlist_unshift_sorted(list, device_name, sort_char);
408 sysfs_close_directory(dir);
413 * sysfs_path_is_dir: Check if the path supplied points to a directory
414 * @path: path to validate
415 * Returns 0 if path points to dir, 1 otherwise
417 int sysfs_path_is_dir(const char *path)
425 if ((lstat(path, &astats)) != 0) {
426 dprintf("stat() failed\n");
429 if (S_ISDIR(astats.st_mode))
436 * sysfs_path_is_link: Check if the path supplied points to a link
437 * @path: path to validate
438 * Returns 0 if path points to link, 1 otherwise
440 int sysfs_path_is_link(const char *path)
448 if ((lstat(path, &astats)) != 0) {
449 dprintf("stat() failed\n");
452 if (S_ISLNK(astats.st_mode))
459 * sysfs_path_is_file: Check if the path supplied points to a file
460 * @path: path to validate
461 * Returns 0 if path points to file, 1 otherwise
463 int sysfs_path_is_file(const char *path)
471 if ((lstat(path, &astats)) != 0) {
472 dprintf("stat() failed\n");
475 if (S_ISREG(astats.st_mode))