chiark / gitweb /
[PATCH] clean up the aoe char device rules, and delete the block one as it's not...
[elogind.git] / libsysfs / sysfs_utils.c
1 /*
2  * sysfs_utils.c
3  *
4  * System utility functions for libsysfs
5  *
6  * Copyright (C) IBM Corp. 2003
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23 #include "libsysfs.h"
24 #include "sysfs.h"
25
26 static int sort_char(void *new, void *old)
27 {
28         return ((strncmp((char *)new, (char *)old, 
29                         strlen((char *)new))) < 0 ? 1 : 0);
30 }
31
32 /**
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
36  */ 
37 int sysfs_remove_trailing_slash(char *path)
38 {
39         char *c = NULL;
40
41         if (path == NULL) {
42                 errno = EINVAL;
43                 return 1;
44         }
45         c = strrchr(path, '/');
46         if (c == NULL) {
47                 dprintf("Invalid path %s\n", path);
48                 errno = EINVAL;
49                 return 1;
50         }
51         if (*(c+1) == '\0') 
52                 *c = '\0';
53         return 0;
54 }
55
56 /**
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.
62  */
63 static int sysfs_get_fs_mnt_path(const char *fs_type, 
64                                 char *mnt_path, size_t len)
65 {
66         FILE *mnt;
67         struct mntent *mntent;
68         int ret = 0;
69         size_t dirlen = 0;
70
71         /* check arg */
72         if (fs_type == NULL || mnt_path == NULL || len == 0) {
73                 errno = EINVAL;
74                 return -1;
75         }
76
77         if ((mnt = setmntent(SYSFS_PROC_MNTS, "r")) == NULL) {
78                 dprintf("Error getting mount information\n");
79                 return -1;
80         }
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);
86                         } else {
87                                 dprintf("Error - mount path too long\n");
88                                 ret = -1;
89                         }
90                 }
91         }
92         endmntent(mnt);
93         if (dirlen == 0 && ret == 0) {
94                 dprintf("Filesystem %s not found!\n", fs_type);
95                 errno = EINVAL;
96                 ret = -1;
97         }
98         if ((sysfs_remove_trailing_slash(mnt_path)) != 0)
99                 ret = -1;
100         
101         return ret;
102 }
103
104 /*
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.
109  */
110 int sysfs_get_mnt_path(char *mnt_path, size_t len)
111 {
112         char *sysfs_path = NULL;
113         int ret = 0;
114
115         if (mnt_path == NULL || len == 0) {
116                 errno = EINVAL;
117                 return -1;
118         }
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)
123                         return 1;
124         } else
125                 ret = sysfs_get_fs_mnt_path(SYSFS_FSTYPE_NAME, mnt_path, len);
126
127         return ret;
128 }
129
130 /**
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
134  * @len: size of name
135  */
136 int sysfs_get_name_from_path(const char *path, char *name, size_t len)
137 {
138         char tmp[SYSFS_PATH_MAX];
139         char *n = NULL;
140                                                                                 
141         if (path == NULL || name == NULL || len == 0) {
142                 errno = EINVAL;
143                 return -1;
144         }
145         memset(tmp, 0, SYSFS_PATH_MAX);
146         safestrcpy(tmp, path);
147         n = strrchr(tmp, '/');
148         if (n == NULL) {
149                 errno = EINVAL;
150                 return -1;
151         }
152         if (*(n+1) == '\0') {
153                 *n = '\0';
154                 n = strrchr(tmp, '/');
155                 if (n == NULL) {
156                         errno = EINVAL;
157                         return -1;
158                 }
159         }
160         n++;
161         safestrcpymax(name, n, len);
162         return 0;
163 }
164         
165 /**
166  * sysfs_get_link: returns link source
167  * @path: symbolic link's path
168  * @target: where to put name
169  * @len: size of name
170  */
171 int sysfs_get_link(const char *path, char *target, size_t len)
172 {
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;
178
179         if (path == NULL || target == NULL || len == 0) {
180                 errno = EINVAL;
181                 return -1;
182         }
183
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);
188
189         if ((readlink(path, linkpath, SYSFS_PATH_MAX)) < 0) {
190                 return -1;
191         }
192         d = linkpath;
193         /* 
194          * Three cases here:
195          * 1. relative path => format ../..
196          * 2. absolute path => format /abcd/efgh
197          * 3. relative path _from_ this dir => format abcd/efgh
198          */ 
199         switch (*d) {
200                 case '.': 
201                         /* 
202                          * handle the case where link is of type ./abcd/xxx
203                          */
204                         safestrcpy(temp_path, devdir);
205                         if (*(d+1) == '/')
206                                 d += 2;
207                         else if (*(d+1) == '.')
208                                 goto parse_path;
209                         s = strrchr(temp_path, '/');
210                         if (s != NULL) {
211                                 *(s+1) = '\0';
212                                 safestrcat(temp_path, d);
213                         } else {
214                                 safestrcpy(temp_path, d);
215                         }
216                         safestrcpymax(target, temp_path, len);
217                         break;
218                         /* 
219                          * relative path  
220                          * getting rid of leading "../.." 
221                          */
222 parse_path:
223                         while (*d == '/' || *d == '.') {
224                                 if (*d == '/')
225                                         slashes++;
226                                 d++;
227                         }
228                         d--;
229                         s = &devdir[strlen(devdir)-1];
230                         while (s != NULL && count != (slashes+1)) {
231                                 s--;
232                                 if (*s == '/')
233                                         count++;
234                         }
235                         safestrcpymax(s, d, (SYSFS_PATH_MAX-strlen(devdir)));
236                         safestrcpymax(target, devdir, len);
237                         break;
238                 case '/':
239                         /* absolute path - copy as is */
240                         safestrcpymax(target, linkpath, len);
241                         break;
242                 default:
243                         /* relative path from this directory */
244                         safestrcpy(temp_path, devdir);
245                         s = strrchr(temp_path, '/');
246                         if (s != NULL) {
247                                 *(s+1) = '\0';
248                                 safestrcat(temp_path, linkpath);
249                         } else {
250                                 safestrcpy(temp_path, linkpath);
251                         }
252                         safestrcpymax(target, temp_path, len);
253         }
254         return 0;
255 }
256
257 /**
258  * sysfs_del_name: free function for sysfs_open_subsystem_list
259  * @name: memory area to be freed
260  */ 
261 static void sysfs_del_name(void *name)
262 {
263         free(name);
264 }
265
266
267 /**
268  * sysfs_close_list: generic list free routine
269  * @list: dlist to free
270  * Returns nothing
271  */
272 void sysfs_close_list(struct dlist *list)
273 {
274         if (list != NULL)
275                 dlist_destroy(list);
276 }
277
278 /**
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
283  */ 
284 struct dlist *sysfs_open_subsystem_list(char *name)
285 {
286         char sysfs_path[SYSFS_PATH_MAX], *subsys_name = NULL;
287         char *c = NULL;
288         struct sysfs_directory *dir = NULL, *cur = NULL;
289         struct dlist *list = NULL;
290         
291         if (name == NULL)
292                 return NULL;
293
294         if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
295                 dprintf("Error getting sysfs mount point\n");
296                 return NULL;
297         }
298
299         safestrcat(sysfs_path, "/");
300         safestrcat(sysfs_path, name);
301         dir = sysfs_open_directory(sysfs_path);
302         if (dir == NULL) {
303                 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
304                 return NULL;
305         }
306
307         if ((sysfs_read_dir_subdirs(dir)) != 0) {
308                 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
309                 sysfs_close_directory(dir);
310                 return NULL;
311         }
312
313         if (dir->subdirs != NULL) {
314                 list = dlist_new_with_delete(SYSFS_NAME_LEN,
315                                 sysfs_del_name);
316                 if (list == NULL) {
317                         dprintf("Error creating list\n");
318                         sysfs_close_directory(dir);
319                         return NULL;
320                 }
321
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);
327                 }
328         }
329         sysfs_close_directory(dir);
330         /*
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.
334          */ 
335         if (strcmp(name, SYSFS_CLASS_NAME) == 0) {
336                 c = strstr(sysfs_path, SYSFS_CLASS_NAME);
337                 if (c == NULL)
338                         goto out;
339                 *c = '\0';
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, 
345                                         SYSFS_NAME_LEN);
346                         dlist_unshift_sorted(list, subsys_name, sort_char);
347                 }
348         }
349 out:
350         return list;
351 }
352
353
354 /**
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
358  */ 
359 struct dlist *sysfs_open_bus_devices_list(char *name)
360 {
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;
365         
366         if (name == NULL)
367                 return NULL;
368
369         if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
370                 dprintf("Error getting sysfs mount point\n");
371                 return NULL;
372         }
373
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);
381         if (dir == NULL) {
382                 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
383                 return NULL;
384         }
385
386         if ((sysfs_read_dir_links(dir)) != 0) {
387                 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
388                 sysfs_close_directory(dir);
389                 return NULL;
390         }
391
392         if (dir->links != NULL) {
393                 list = dlist_new_with_delete(SYSFS_NAME_LEN,
394                                 sysfs_del_name);
395                 if (list == NULL) {
396                         dprintf("Error creating list\n");
397                         sysfs_close_directory(dir);
398                         return NULL;
399                 }
400
401                 dlist_for_each_data(dir->links, cur,
402                                 struct sysfs_link) {
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);
406                 }
407         }
408         sysfs_close_directory(dir);
409         return list;
410 }
411
412 /**
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
416  */
417 int sysfs_path_is_dir(const char *path)
418 {
419         struct stat astats;
420
421         if (path == NULL) {
422                 errno = EINVAL;
423                 return 1;
424         }
425         if ((lstat(path, &astats)) != 0) {
426                 dprintf("stat() failed\n");
427                 return 1;
428         }
429         if (S_ISDIR(astats.st_mode))
430                 return 0;
431                 
432         return 1;
433 }
434
435 /**
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
439  */
440 int sysfs_path_is_link(const char *path)
441 {
442         struct stat astats;
443
444         if (path == NULL) {
445                 errno = EINVAL;
446                 return 1;
447         }
448         if ((lstat(path, &astats)) != 0) {
449                 dprintf("stat() failed\n");
450                 return 1;
451         }
452         if (S_ISLNK(astats.st_mode))
453                 return 0;
454                 
455         return 1;
456 }
457
458 /**
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
462  */
463 int sysfs_path_is_file(const char *path)
464 {
465         struct stat astats;
466
467         if (path == NULL) {
468                 errno = EINVAL;
469                 return 1;
470         }
471         if ((lstat(path, &astats)) != 0) {
472                 dprintf("stat() failed\n");
473                 return 1;
474         }
475         if (S_ISREG(astats.st_mode))
476                 return 0;
477                 
478         return 1;
479 }