chiark / gitweb /
add man pages for *_id programs
[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 static void sysfs_close_cls_dev(void *dev)
44 {
45         sysfs_close_class_device((struct sysfs_class_device *)dev);
46 }
47
48 /**
49  * sysfs_close_class: close the given class
50  * @cls: sysfs_class to close
51  */ 
52 void sysfs_close_class(struct sysfs_class *cls)
53 {
54         if (cls) {
55                 if (cls->devices)
56                         dlist_destroy(cls->devices);
57                 if (cls->attrlist)
58                         dlist_destroy(cls->attrlist);
59                 free(cls);
60         }
61 }
62
63 static int cdev_name_equal(void *a, void *b)
64 {
65         if (!a || !b)
66                 return 0;
67
68         if (strncmp((char *)a, ((struct sysfs_class_device *)b)->name, 
69                                 strlen((char *)a)) == 0)
70                 return 1;
71
72         return 0;
73 }
74
75 static struct sysfs_class *alloc_class(void)
76 {
77         return (struct sysfs_class *) calloc(1, sizeof(struct sysfs_class));
78 }
79
80 /**
81  * alloc_class_device: mallocs and initializes new class device struct.
82  * returns sysfs_class_device or NULL.
83  */
84 static struct sysfs_class_device *alloc_class_device(void)
85 {
86         struct sysfs_class_device *dev;
87
88         dev = calloc(1, sizeof(struct sysfs_class_device));
89         return dev;
90 }
91
92 /**
93  * set_classdev_classname: Grabs classname from path
94  * @cdev: class device to set
95  * Returns nothing
96  */
97 static void set_classdev_classname(struct sysfs_class_device *cdev)
98 {
99         char *c, *e;
100         int count = 0;
101
102         c = strstr(cdev->path, SYSFS_CLASS_NAME);
103         if (c == NULL) {
104                 c = strstr(cdev->path, SYSFS_BLOCK_NAME);
105         } else {
106                 c = strstr(c, "/");
107         }
108
109         if (c == NULL)
110                 safestrcpy(cdev->classname, SYSFS_UNKNOWN);
111         else {
112                 if (*c == '/')
113                         c++;
114                 e = c;
115                 while (e != NULL && *e != '/' && *e != '\0') {
116                         e++;
117                         count++;
118                 }
119                 strncpy(cdev->classname, c, count);
120         }
121 }
122
123 /**
124  * sysfs_open_class_device_path: Opens and populates class device
125  * @path: path to class device.
126  * returns struct sysfs_class_device with success and NULL with error.
127  */
128 struct sysfs_class_device *sysfs_open_class_device_path(const char *path)
129 {
130         struct sysfs_class_device *cdev;
131
132         if (!path) {
133                 errno = EINVAL;
134                 return NULL;
135         }
136         if (sysfs_path_is_dir(path)) {
137                 dprintf("%s is not a valid path to a class device\n", path);
138                 return NULL;
139         }
140         cdev = alloc_class_device();
141         if (!cdev) {
142                 dprintf("calloc failed\n");
143                 return NULL;
144         }
145         if (sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) {
146                 errno = EINVAL;
147                 dprintf("Error getting class device name\n");
148                 sysfs_close_class_device(cdev);
149                 return NULL;
150         }
151
152         safestrcpy(cdev->path, path);
153         if (sysfs_remove_trailing_slash(cdev->path)) {
154                 dprintf("Invalid path to class device %s\n", cdev->path);
155                 sysfs_close_class_device(cdev);
156                 return NULL;
157         }
158         set_classdev_classname(cdev);
159
160         return cdev;
161 }
162
163 /** 
164  * get_blockdev_parent: Get the parent class device for a "block" subsystem 
165  *              device if present
166  * @clsdev: block subsystem class device whose parent needs to be found
167  * Returns 0 on success and 1 on error
168  */
169 static int get_blockdev_parent(struct sysfs_class_device *clsdev)
170 {
171         char parent_path[SYSFS_PATH_MAX];
172         char *c;
173
174         safestrcpy(parent_path, clsdev->path);
175         c = strstr(parent_path, SYSFS_BLOCK_NAME);
176         if (c == NULL) {
177                 dprintf("Class device %s does not belong to BLOCK subsystem\n",
178                                 clsdev->name);
179                 return 1;
180         }
181         c += strlen(SYSFS_BLOCK_NAME);
182         if (*c == '/')
183                 c++;
184         else
185                 goto errout;
186
187         /* validate whether the given class device is a partition or not */
188         if ((strncmp(c, clsdev->name, strlen(clsdev->name))) == 0) {
189                 dprintf("%s not a partition\n", clsdev->name);
190                 return 1;
191         }
192
193         c = strchr(c, '/');
194         if (c == NULL)
195                 goto errout;
196
197         *c = '\0';
198
199         clsdev->parent = sysfs_open_class_device_path(parent_path);
200         if (!clsdev->parent) {
201                 dprintf("Error opening the parent class device at %s\n", 
202                                                                 parent_path);
203                 return 1;
204         }
205         return 0;
206
207 errout:
208         dprintf("Invalid path %s\n", clsdev->path);
209         return 1;
210 }
211
212 /**
213  * sysfs_get_classdev_parent: Retrieves the parent of a class device. 
214  *      eg., when working with hda1, this function can be used to retrieve the
215  *              sysfs_class_device for hda
216  *              
217  * @clsdev: class device whose parent details are required.
218  * Returns sysfs_class_device of the parent on success, NULL on failure
219  */ 
220 struct sysfs_class_device *sysfs_get_classdev_parent
221                                 (struct sysfs_class_device *clsdev)
222 {
223         if (!clsdev) {
224                 errno = EINVAL;
225                 return NULL;
226         }
227         if (clsdev->parent)
228                 return (clsdev->parent);
229
230         /*
231          * As of now, only block devices have a parent child heirarchy in sysfs
232          * We do not know, if, in the future, more classes will have a similar
233          * structure. Hence, we now call a specialized function for block and
234          * later we can add support functions for other subsystems as required.
235          */
236         if (!(strncmp(clsdev->classname, SYSFS_BLOCK_NAME, 
237                                         sizeof(SYSFS_BLOCK_NAME)))) {
238                 if ((get_blockdev_parent(clsdev)) == 0) 
239                         return (clsdev->parent);
240         }
241         return NULL;
242 }
243
244 /**
245  * get_classdev_path: given the class and a device in the class, return the
246  *              absolute path to the device
247  * @classname: name of the class
248  * @clsdev: the class device
249  * @path: buffer to return path
250  * @psize: size of "path"
251  * Returns 0 on SUCCESS or -1 on error
252  */
253 static int get_classdev_path(const char *classname, const char *clsdev, 
254                 char *path, size_t len)
255 {
256         if (!classname || !clsdev || !path) {
257                 errno = EINVAL;
258                 return -1;
259         }
260         if (sysfs_get_mnt_path(path, len) != 0) {
261                 dprintf("Error getting sysfs mount path\n");
262                 return -1;
263         }
264         if (strncmp(classname, SYSFS_BLOCK_NAME,
265                                 sizeof(SYSFS_BLOCK_NAME)) == 0) {
266                 safestrcatmax(path, "/", len);
267                 safestrcatmax(path, SYSFS_BLOCK_NAME, len);
268         } else {
269                 safestrcatmax(path, "/", len);
270                 safestrcatmax(path, SYSFS_CLASS_NAME, len);
271                 safestrcatmax(path, "/", len);
272                 safestrcatmax(path, classname, len);
273         }
274         safestrcatmax(path, "/", len);
275         safestrcatmax(path, clsdev, len);
276         return 0;
277 }
278
279 /**
280  * sysfs_open_class_device: Locates a specific class_device and returns it.
281  * Class_device must be closed using sysfs_close_class_device
282  * @classname: Class to search
283  * @name: name of the class_device
284  * 
285  * NOTE:
286  *      Call sysfs_close_class_device() to close the class device
287  */
288 struct sysfs_class_device *sysfs_open_class_device
289                 (const char *classname, const char *name)
290 {
291         char devpath[SYSFS_PATH_MAX];
292         struct sysfs_class_device *cdev;
293
294         if (!classname || !name) {
295                 errno = EINVAL;
296                 return NULL;
297         }
298         
299         memset(devpath, 0, SYSFS_PATH_MAX);
300         if ((get_classdev_path(classname, name, devpath, 
301                                         SYSFS_PATH_MAX)) != 0) {
302                 dprintf("Error getting to device %s on class %s\n",
303                                                         name, classname);
304                 return NULL;
305         }
306
307         cdev = sysfs_open_class_device_path(devpath);
308         if (!cdev) {
309                 dprintf("Error getting class device %s from class %s\n",
310                                 name, classname);
311                 return NULL;
312         }
313         return cdev;
314 }
315
316 /**
317  * sysfs_get_classdev_attr: searches class device's attributes by name
318  * @clsdev: class device to look through
319  * @name: attribute name to get
320  * returns sysfs_attribute reference with success or NULL with error
321  */
322 struct sysfs_attribute *sysfs_get_classdev_attr
323                 (struct sysfs_class_device *clsdev, const char *name)
324 {
325         if (!clsdev || !name) {
326                 errno = EINVAL;
327                 return NULL;
328         }
329         return get_attribute(clsdev, (char *)name);
330 }
331
332 /**
333  * sysfs_get_classdev_attributes: gets list of classdev attributes
334  * @clsdev: class device whose attributes list is needed
335  * returns dlist of attributes on success or NULL on error
336  */
337 struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *clsdev)
338 {
339         if (!clsdev) {
340                 errno = EINVAL;
341                 return NULL;
342         }
343         return get_attributes_list(clsdev);
344 }
345
346 /**
347  * sysfs_get_classdev_device: gets the sysfs_device associated with the
348  *      given sysfs_class_device
349  * @clsdev: class device whose associated sysfs_device is needed
350  * returns struct sysfs_device * on success or NULL on error
351  */
352 struct sysfs_device *sysfs_get_classdev_device
353                 (struct sysfs_class_device *clsdev)
354 {
355         char linkpath[SYSFS_PATH_MAX], devpath[SYSFS_PATH_MAX];
356
357         if (!clsdev) {
358                 errno = EINVAL;
359                 return NULL;
360         }
361
362         if (clsdev->sysdevice)
363                 return clsdev->sysdevice;
364
365         memset(linkpath, 0, SYSFS_PATH_MAX);
366         safestrcpy(linkpath, clsdev->path);
367         safestrcat(linkpath, "/device");
368         if (!sysfs_path_is_link(linkpath)) {
369                 memset(devpath, 0, SYSFS_PATH_MAX);
370                 if (!sysfs_get_link(linkpath, devpath, SYSFS_PATH_MAX))
371                         clsdev->sysdevice = sysfs_open_device_path(devpath);
372         }
373         return clsdev->sysdevice;
374 }
375
376 /**
377  * sysfs_open_class: opens specific class and all its devices on system
378  * returns sysfs_class structure with success or NULL with error.
379  */
380 struct sysfs_class *sysfs_open_class(const char *name)
381 {
382         struct sysfs_class *cls = NULL;
383         char classpath[SYSFS_PATH_MAX];
384
385         if (!name) {
386                 errno = EINVAL;
387                 return NULL;
388         }
389
390         memset(classpath, 0, SYSFS_PATH_MAX);
391         if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) {
392                 dprintf("Sysfs not supported on this system\n");
393                 return NULL;
394         }
395
396         /* 
397          * We shall now treat "block" also as a class. Hence, check here
398          * if "name" is "block" and proceed accordingly
399          */
400         if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
401                 safestrcat(classpath, "/");
402                 safestrcat(classpath, SYSFS_BLOCK_NAME);
403         } else {
404                 safestrcat(classpath, "/");
405                 safestrcat(classpath, SYSFS_CLASS_NAME);
406                 safestrcat(classpath, "/");
407                 safestrcat(classpath, name);
408         }
409         if (sysfs_path_is_dir(classpath)) {
410                 dprintf("Class %s not found on the system\n", name);
411                 return NULL;
412         }
413
414         cls = alloc_class();
415         if (cls == NULL) {
416                 dprintf("calloc failed\n");
417                 return NULL;
418         }
419         safestrcpy(cls->name, name);    
420         safestrcpy(cls->path, classpath);
421         if ((sysfs_remove_trailing_slash(cls->path)) != 0) {
422                 dprintf("Invalid path to class device %s\n", cls->path);
423                 sysfs_close_class(cls);
424                 return NULL;
425         }
426
427         return cls;
428 }
429
430 /**
431  * sysfs_get_class_device: get specific class device using the device's id
432  * @cls: sysfs_class to find the device on
433  * @name: name of the class device to look for
434  * 
435  * Returns sysfs_class_device * on success and NULL on failure
436  */ 
437 struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *cls,
438                 const char *name)
439 {
440         char path[SYSFS_PATH_MAX];
441         struct sysfs_class_device *cdev = NULL;
442
443         if (!cls || !name) {
444                 errno = EINVAL;
445                 return NULL;
446         }
447
448         if (cls->devices) {
449                 cdev = (struct sysfs_class_device *)dlist_find_custom
450                         (cls->devices, (void *)name, cdev_name_equal);
451                 if (cdev)
452                         return cdev;
453         }
454
455         safestrcpy(path, cls->path);
456         safestrcat(path, "/");
457         safestrcat(path, name);
458         if (!sysfs_path_is_dir(path)) {
459                 cdev = sysfs_open_class_device_path(path);
460                 if (!cdev) {
461                         dprintf("Error opening class device at %s\n", path);
462                         return NULL;
463                 }
464         }
465         if (!cls->devices)
466                 cls->devices = dlist_new_with_delete
467                         (sizeof(struct sysfs_class_device),
468                                  sysfs_close_cls_dev);
469         
470         dlist_unshift_sorted(cls->devices, cdev, sort_list);
471         return cdev;
472 }
473
474 /**
475  * sysfs_get_class_devices: get all class devices in the given class
476  * @cls: sysfs_class whose devices list is needed
477  *
478  * Returns a dlist of sysfs_class_device * on success and NULL on failure
479  */
480 struct dlist *sysfs_get_class_devices(struct sysfs_class *cls)
481 {
482         char path[SYSFS_PATH_MAX];
483         char *cdev_name;
484         struct sysfs_class_device *cdev = NULL;
485         struct dlist *dirlist;
486
487         if (!cls) {
488                 errno = EINVAL;
489                 return NULL;
490         }
491
492         safestrcpy(path, cls->path);
493         dirlist = read_dir_subdirs(path);
494         if (dirlist) {
495                 dlist_for_each_data(dirlist, cdev_name, char) {
496                         if (cls->devices) {
497                                 cdev = (struct sysfs_class_device *)
498                                         dlist_find_custom(cls->devices, 
499                                         (void *)cdev_name, cdev_name_equal);
500                                 if (cdev)
501                                         continue;
502                         }
503                         safestrcpy(path, cls->path);
504                         safestrcat(path, "/");
505                         safestrcat(path, cdev_name);
506                         cdev = sysfs_open_class_device_path(path);
507                         if (cdev) {
508                                 if (!cls->devices)
509                                         cls->devices = dlist_new_with_delete
510                                         (sizeof(struct sysfs_class_device),
511                                          sysfs_close_cls_dev);
512                                 dlist_unshift_sorted(cls->devices, cdev,
513                                                 sort_list);
514                         }
515                 }
516                 sysfs_close_list(dirlist);
517         }
518         
519         return cls->devices;
520 }
521