chiark / gitweb /
[PATCH] klibc specific tweaks
[elogind.git] / libsysfs / sysfs_utils.c
1 /*
2  * syfs_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 #ifndef __KLIBC__
26 #include <mntent.h>
27 #endif
28
29 /**
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.
35  */
36 static int sysfs_get_fs_mnt_path(const unsigned char *fs_type, 
37                                 unsigned char *mnt_path, size_t len)
38 {
39 #ifdef __KLIBC__
40         strcpy(mnt_path, "/sys");
41         return 0;
42 #else
43         FILE *mnt;
44         struct mntent *mntent;
45         int ret = 0;
46         size_t dirlen = 0;
47
48         /* check arg */
49         if (fs_type == NULL || mnt_path == NULL) {
50                 errno = EINVAL;
51                 return -1;
52         }
53
54         if ((mnt = setmntent(SYSFS_PROC_MNTS, "r")) == NULL) {
55                 dprintf("Error getting mount information\n");
56                 return -1;
57         }
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);
63                         } else {
64                                 dprintf("Error - mount path too long\n");
65                                 ret = -1;
66                         }
67                 }
68         }
69         endmntent(mnt);
70         if (dirlen == 0 && ret == 0) {
71                 dprintf("Filesystem %s not found!\n", fs_type);
72                 errno = EINVAL;
73                 ret = -1;
74         }
75         return ret;
76 #endif
77 }
78
79 /*
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.
84  */
85 int sysfs_get_mnt_path(unsigned char *mnt_path, size_t len)
86 {
87         int ret = -1;
88
89         if (mnt_path != NULL)
90                 ret = sysfs_get_fs_mnt_path(SYSFS_FSTYPE_NAME, mnt_path, len);
91         else
92                 errno = EINVAL;
93
94         return ret;
95 }
96
97 /**
98  * sysfs_get_name_from_path: returns last name from a "/" delimited path
99  * @path: path to get name from
100  * @name: where to put name
101  * @len: size of name
102  */
103 int sysfs_get_name_from_path(const unsigned char *path, unsigned char *name, 
104                                                                 size_t len)
105 {
106         unsigned char *n = NULL;
107                                                                                 
108         if (path == NULL || name == NULL) {
109                 errno = EINVAL;
110                 return -1;
111         }
112         n = strrchr(path, '/');
113         if (n == NULL) {
114                 errno = EINVAL;
115                 return -1;
116         }
117         n++;
118         strncpy(name, n, len);
119
120         return 0;
121 }
122
123 /**
124  * sysfs_get_link: returns link source
125  * @path: symbolic link's path
126  * @target: where to put name
127  * @len: size of name
128  */
129 int sysfs_get_link(const unsigned char *path, unsigned char *target, size_t len)
130 {
131         unsigned char devdir[SYSFS_PATH_MAX];
132         unsigned char linkpath[SYSFS_PATH_MAX];
133         unsigned char *d = NULL;
134
135         if (path == NULL || target == NULL) {
136                 errno = EINVAL;
137                 return -1;
138         }
139
140         memset(devdir, 0, SYSFS_PATH_MAX);
141         memset(linkpath, 0, SYSFS_PATH_MAX);
142
143         if ((sysfs_get_mnt_path(devdir, SYSFS_PATH_MAX)) != 0) {
144                 dprintf("Sysfs not supported on this system\n");
145                 return -1;
146         }
147                                                                         
148         if ((readlink(path, linkpath, SYSFS_PATH_MAX)) < 0) {
149                 return -1;
150         }
151                                                                                 
152         d = linkpath;
153
154         /* getting rid of leading "../.." */    
155         while (*d == '/' || *d == '.')
156                 d++;
157
158         d--;
159         
160         strcat(devdir, d);
161         strncpy(target, devdir, len);
162
163         return 0;
164 }
165
166
167 /**
168  * sysfs_del_name: free function for sysfs_open_subsystem_list
169  * @name: memory area to be freed
170  */ 
171 void sysfs_del_name(void *name)
172 {
173         free(name);
174 }
175
176
177 /**
178  * sysfs_close_list: generic list free routine
179  * @list: dlist to free
180  * Returns nothing
181  */
182 void sysfs_close_list(struct dlist *list)
183 {
184         if (list != NULL)
185                 dlist_destroy(list);
186 }
187
188 /**
189  * sysfs_open_subsystem_list: gets a list of all supported "name" subsystem
190  *      details from the system
191  * @name: name of the subsystem, eg., "bus", "class", "devices"
192  * Returns a dlist of supported names or NULL if subsystem not supported
193  */ 
194 struct dlist *sysfs_open_subsystem_list(unsigned char *name)
195 {
196         unsigned char sysfs_path[SYSFS_PATH_MAX], *subsys_name = NULL;
197         struct sysfs_directory *dir = NULL, *cur = NULL;
198         struct dlist *list = NULL;
199         
200         if (name == NULL)
201                 return NULL;
202
203         if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
204                 dprintf("Error getting sysfs mount point\n");
205                 return NULL;
206         }
207
208         strcat(sysfs_path, name);
209         dir = sysfs_open_directory(sysfs_path);
210         if (dir == NULL) {
211                 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
212                 return NULL;
213         }
214
215         if (sysfs_read_directory(dir) != 0) {
216                 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
217                 sysfs_close_directory(dir);
218                 return NULL;
219         }
220
221         if (dir->subdirs != NULL) {
222                 list = dlist_new_with_delete(SYSFS_NAME_LEN,
223                                 sysfs_del_name);
224                 if (list == NULL) {
225                         dprintf("Error creating list\n");
226                         sysfs_close_directory(dir);
227                         return NULL;
228                 }
229
230                 dlist_for_each_data(dir->subdirs, cur,
231                                 struct sysfs_directory) {
232                         subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
233                         strcpy(subsys_name, cur->name);
234                         dlist_unshift(list, subsys_name);
235                 }
236         }
237         sysfs_close_directory(dir);
238         return list;
239 }
240
241
242 /**
243  * sysfs_open_bus_devices_list: gets a list of all devices on "name" bus
244  * @name: name of the subsystem, eg., "pci", "scsi", "usb"
245  * Returns a dlist of supported names or NULL if subsystem not supported
246  */ 
247 struct dlist *sysfs_open_bus_devices_list(unsigned char *name)
248 {
249         unsigned char sysfs_path[SYSFS_PATH_MAX], *device_name = NULL;
250         struct sysfs_directory *dir = NULL;
251         struct sysfs_link *cur = NULL;
252         struct dlist *list = NULL;
253         
254         if (name == NULL)
255                 return NULL;
256
257         if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
258                 dprintf("Error getting sysfs mount point\n");
259                 return NULL;
260         }
261
262         strcat(sysfs_path, SYSFS_BUS_DIR);
263         strcat(sysfs_path, "/");
264         strcat(sysfs_path, name);
265         strcat(sysfs_path, SYSFS_DEVICES_DIR);
266         dir = sysfs_open_directory(sysfs_path);
267         if (dir == NULL) {
268                 dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
269                 return NULL;
270         }
271
272         if (sysfs_read_directory(dir) != 0) {
273                 dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
274                 sysfs_close_directory(dir);
275                 return NULL;
276         }
277
278         if (dir->links != NULL) {
279                 list = dlist_new_with_delete(SYSFS_NAME_LEN,
280                                 sysfs_del_name);
281                 if (list == NULL) {
282                         dprintf("Error creating list\n");
283                         sysfs_close_directory(dir);
284                         return NULL;
285                 }
286
287                 dlist_for_each_data(dir->links, cur,
288                                 struct sysfs_link) {
289                         device_name = (char *)calloc(1, SYSFS_NAME_LEN);
290                         strcpy(device_name, cur->name);
291                         dlist_unshift(list, device_name);
292                 }
293         }
294         sysfs_close_directory(dir);
295         return list;
296 }
297