chiark / gitweb /
[PATCH] udev-test.pl: use more common user/group names
[elogind.git] / libsysfs / sysfs_class.c
1 /*
2  * sysfs_class.c
3  *
4  * Generic class utility functions for libsysfs
5  *
6  * Copyright (C) IBM Corp. 2003-2005
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 /**
27  * sysfs_close_class_device: closes a single class device.
28  * @dev: class device to close.
29  */
30 void sysfs_close_class_device(struct sysfs_class_device *dev)
31 {
32         if (dev) {
33                 if (dev->parent)
34                         sysfs_close_class_device(dev->parent);
35                 if (dev->sysdevice)
36                         sysfs_close_device(dev->sysdevice);
37                 if (dev->attrlist)
38                         dlist_destroy(dev->attrlist);
39                 free(dev);
40         }
41 }
42
43 /**
44  * alloc_class_device: mallocs and initializes new class device struct.
45  * returns sysfs_class_device or NULL.
46  */
47 static struct sysfs_class_device *alloc_class_device(void)
48 {
49         struct sysfs_class_device *dev;
50
51         dev = calloc(1, sizeof(struct sysfs_class_device));
52         return dev;
53 }
54
55 /**
56  * set_classdev_classname: Grabs classname from path
57  * @cdev: class device to set
58  * Returns nothing
59  */
60 static void set_classdev_classname(struct sysfs_class_device *cdev)
61 {
62         char *c, *e;
63         int count = 0;
64
65         c = strstr(cdev->path, SYSFS_CLASS_NAME);
66         if (c == NULL) {
67                 c = strstr(cdev->path, SYSFS_BLOCK_NAME);
68         } else {
69                 c = strstr(c, "/");
70         }
71
72         if (c == NULL)
73                 safestrcpy(cdev->classname, SYSFS_UNKNOWN);
74         else {
75                 if (*c == '/')
76                         c++;
77                 e = c;
78                 while (e != NULL && *e != '/' && *e != '\0') {
79                         e++;
80                         count++;
81                 }
82                 strncpy(cdev->classname, c, count);
83         }
84 }
85
86 /**
87  * sysfs_open_class_device_path: Opens and populates class device
88  * @path: path to class device.
89  * returns struct sysfs_class_device with success and NULL with error.
90  */
91 struct sysfs_class_device *sysfs_open_class_device_path(const char *path)
92 {
93         struct sysfs_class_device *cdev;
94
95         if (!path) {
96                 errno = EINVAL;
97                 return NULL;
98         }
99         if ((sysfs_path_is_dir(path)) != 0) {
100                 dprintf("%s is not a valid path to a class device\n", path);
101                 return NULL;
102         }
103         cdev = alloc_class_device();
104         if (!cdev) {
105                 dprintf("calloc failed\n");
106                 return NULL;
107         }
108         if (sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) {
109                 errno = EINVAL;
110                 dprintf("Error getting class device name\n");
111                 sysfs_close_class_device(cdev);
112                 return NULL;
113         }
114
115         safestrcpy(cdev->path, path);
116         if ((sysfs_remove_trailing_slash(cdev->path)) != 0) {
117                 dprintf("Invalid path to class device %s\n", cdev->path);
118                 sysfs_close_class_device(cdev);
119                 return NULL;
120         }
121         set_classdev_classname(cdev);
122
123         return cdev;
124 }
125
126 /** 
127  * get_blockdev_parent: Get the parent class device for a "block" subsystem 
128  *              device if present
129  * @clsdev: block subsystem class device whose parent needs to be found
130  * Returns 0 on success and 1 on error
131  */
132 static int get_blockdev_parent(struct sysfs_class_device *clsdev)
133 {
134         char parent_path[SYSFS_PATH_MAX];
135         char *c;
136
137         safestrcpy(parent_path, clsdev->path);
138         c = strstr(parent_path, SYSFS_BLOCK_NAME);
139         if (c == NULL) {
140                 dprintf("Class device %s does not belong to BLOCK subsystem\n",
141                                 clsdev->name);
142                 return 1;
143         }
144         c += strlen(SYSFS_BLOCK_NAME);
145         if (*c == '/')
146                 c++;
147         else
148                 goto errout;
149
150         /* validate whether the given class device is a partition or not */
151         if ((strncmp(c, clsdev->name, strlen(clsdev->name))) == 0) {
152                 dprintf("%s not a partition\n", clsdev->name);
153                 return 1;
154         }
155
156         c = strchr(c, '/');
157         if (c == NULL)
158                 goto errout;
159
160         *c = '\0';
161
162         clsdev->parent = sysfs_open_class_device_path(parent_path);
163         if (!clsdev->parent) {
164                 dprintf("Error opening the parent class device at %s\n", 
165                                                                 parent_path);
166                 return 1;
167         }
168         return 0;
169
170 errout:
171         dprintf("Invalid path %s\n", clsdev->path);
172         return 1;
173 }
174
175 /**
176  * sysfs_get_classdev_parent: Retrieves the parent of a class device. 
177  *      eg., when working with hda1, this function can be used to retrieve the
178  *              sysfs_class_device for hda
179  *              
180  * @clsdev: class device whose parent details are required.
181  * Returns sysfs_class_device of the parent on success, NULL on failure
182  */ 
183 struct sysfs_class_device *sysfs_get_classdev_parent
184                                 (struct sysfs_class_device *clsdev)
185 {
186         if (!clsdev) {
187                 errno = EINVAL;
188                 return NULL;
189         }
190         if (clsdev->parent)
191                 return (clsdev->parent);
192
193         /*
194          * As of now, only block devices have a parent child heirarchy in sysfs
195          * We do not know, if, in the future, more classes will have a similar
196          * structure. Hence, we now call a specialized function for block and
197          * later we can add support functions for other subsystems as required.
198          */
199         if (!(strncmp(clsdev->classname, SYSFS_BLOCK_NAME, 
200                                         sizeof(SYSFS_BLOCK_NAME)))) {
201                 if ((get_blockdev_parent(clsdev)) == 0) 
202                         return (clsdev->parent);
203         }
204         return NULL;
205 }
206
207 /**
208  * get_classdev_path: given the class and a device in the class, return the
209  *              absolute path to the device
210  * @classname: name of the class
211  * @clsdev: the class device
212  * @path: buffer to return path
213  * @psize: size of "path"
214  * Returns 0 on SUCCESS or -1 on error
215  */
216 static int get_classdev_path(const char *classname, const char *clsdev, 
217                 char *path, size_t len)
218 {
219         if (!classname || !clsdev || !path) {
220                 errno = EINVAL;
221                 return -1;
222         }
223         if (sysfs_get_mnt_path(path, len) != 0) {
224                 dprintf("Error getting sysfs mount path\n");
225                 return -1;
226         }
227         if (strncmp(classname, SYSFS_BLOCK_NAME,
228                                 sizeof(SYSFS_BLOCK_NAME)) == 0) {
229                 safestrcatmax(path, "/", len);
230                 safestrcatmax(path, SYSFS_BLOCK_NAME, len);
231         } else {
232                 safestrcatmax(path, "/", len);
233                 safestrcatmax(path, SYSFS_CLASS_NAME, len);
234                 safestrcatmax(path, "/", len);
235                 safestrcatmax(path, classname, len);
236         }
237         safestrcatmax(path, "/", len);
238         safestrcatmax(path, clsdev, len);
239         return 0;
240 }
241
242 /**
243  * sysfs_open_class_device: Locates a specific class_device and returns it.
244  * Class_device must be closed using sysfs_close_class_device
245  * @classname: Class to search
246  * @name: name of the class_device
247  * 
248  * NOTE:
249  *      Call sysfs_close_class_device() to close the class device
250  */
251 struct sysfs_class_device *sysfs_open_class_device
252                 (const char *classname, const char *name)
253 {
254         char devpath[SYSFS_PATH_MAX];
255         struct sysfs_class_device *cdev;
256
257         if (!classname || !name) {
258                 errno = EINVAL;
259                 return NULL;
260         }
261         
262         memset(devpath, 0, SYSFS_PATH_MAX);
263         if ((get_classdev_path(classname, name, devpath, 
264                                         SYSFS_PATH_MAX)) != 0) {
265                 dprintf("Error getting to device %s on class %s\n",
266                                                         name, classname);
267                 return NULL;
268         }
269
270         cdev = sysfs_open_class_device_path(devpath);
271         if (!cdev) {
272                 dprintf("Error getting class device %s from class %s\n",
273                                 name, classname);
274                 return NULL;
275         }
276         return cdev;
277 }
278
279 /**
280  * sysfs_get_classdev_attr: searches class device's attributes by name
281  * @clsdev: class device to look through
282  * @name: attribute name to get
283  * returns sysfs_attribute reference with success or NULL with error
284  */
285 struct sysfs_attribute *sysfs_get_classdev_attr
286                 (struct sysfs_class_device *clsdev, const char *name)
287 {
288         if (!clsdev || !name) {
289                 errno = EINVAL;
290                 return NULL;
291         }
292         return get_attribute(clsdev, (char *)name);
293 }
294
295 /**
296  * sysfs_get_classdev_attributes: gets list of classdev attributes
297  * @clsdev: class device whose attributes list is needed
298  * returns dlist of attributes on success or NULL on error
299  */
300 struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *clsdev)
301 {
302         if (!clsdev) {
303                 errno = EINVAL;
304                 return NULL;
305         }
306         return get_attributes_list(clsdev);
307 }
308
309 /**
310  * sysfs_get_classdev_device: gets the sysfs_device associated with the
311  *      given sysfs_class_device
312  * @clsdev: class device whose associated sysfs_device is needed
313  * returns struct sysfs_device * on success or NULL on error
314  */
315 struct sysfs_device *sysfs_get_classdev_device
316                 (struct sysfs_class_device *clsdev)
317 {
318         char linkpath[SYSFS_PATH_MAX], devpath[SYSFS_PATH_MAX];
319
320         if (!clsdev) {
321                 errno = EINVAL;
322                 return NULL;
323         }
324
325         if (clsdev->sysdevice)
326                 return clsdev->sysdevice;
327
328         memset(linkpath, 0, SYSFS_PATH_MAX);
329         safestrcpy(linkpath, clsdev->path);
330         safestrcat(linkpath, "/device");
331         if (!sysfs_path_is_link(linkpath)) {
332                 memset(devpath, 0, SYSFS_PATH_MAX);
333                 if (!sysfs_get_link(linkpath, devpath, SYSFS_PATH_MAX))
334                         clsdev->sysdevice = sysfs_open_device_path(devpath);
335         }
336         return clsdev->sysdevice;
337 }