chiark / gitweb /
[PATCH] another patch for path problem
[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
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 void sysfs_close_cls_dev(void *dev)
27 {
28         sysfs_close_class_device((struct sysfs_class_device *)dev);
29 }
30
31 /**
32  * class_name_equal: compares class_devices' name
33  * @a: class_name looking for
34  * @b: sysfs_class_device being compared
35  */
36 static int class_name_equal(void *a, void *b)
37 {
38         if (a == NULL || b == NULL)
39                 return 0;
40
41         if (strcmp(((unsigned char *)a), ((struct sysfs_class_device *)b)->name)
42                 == 0)
43                 return 1;
44
45         return 0;
46 }
47
48 /**
49  * sysfs_close_class_device: closes a single class device.
50  * @dev: class device to close.
51  */
52 void sysfs_close_class_device(struct sysfs_class_device *dev)
53 {
54         if (dev != NULL) {
55                 if (dev->directory != NULL)
56                         sysfs_close_directory(dev->directory);
57                 if (dev->sysdevice != NULL)
58                         sysfs_close_device(dev->sysdevice);
59                 if (dev->driver != NULL)
60                         sysfs_close_driver(dev->driver);
61                 free(dev);
62         }
63 }
64
65 /**
66  * sysfs_close_class: close single class
67  * @class: class structure
68  */
69 void sysfs_close_class(struct sysfs_class *cls)
70 {
71         if (cls != NULL) {
72                 if (cls->directory != NULL)
73                         sysfs_close_directory(cls->directory);
74                 if (cls->devices != NULL) 
75                         dlist_destroy(cls->devices);
76                 free(cls);
77         }
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         return (struct sysfs_class_device *)
87                                 calloc(1, sizeof(struct sysfs_class_device));
88 }
89
90 /**
91  * alloc_class: mallocs new class structure
92  * returns sysfs_class struct or NULL
93  */
94 static struct sysfs_class *alloc_class(void)
95 {
96         return (struct sysfs_class *)calloc(1, sizeof(struct sysfs_class));
97 }
98
99 /**
100  * open_class_dir: opens up sysfs class directory
101  * returns sysfs_directory struct with success and NULL with error
102  */
103 static struct sysfs_directory *open_class_dir(const unsigned char *name)
104 {
105         struct sysfs_directory *classdir = NULL;
106         unsigned char classpath[SYSFS_PATH_MAX];
107
108         if (name == NULL) {
109                 errno = EINVAL;
110                 return NULL;
111         }
112
113         memset(classpath, 0, SYSFS_PATH_MAX);
114         if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) {
115                 dprintf("Sysfs not supported on this system\n");
116                 return NULL;
117         }
118
119         if (sysfs_trailing_slash(classpath) == 0)
120                 strcat(classpath, "/");
121         /* 
122          * We shall now treat "block" also as a class. Hence, check here
123          * if "name" is "block" and proceed accordingly
124          */
125         if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
126                 strcat(classpath, SYSFS_BLOCK_NAME);
127         } else {
128                 strcat(classpath, SYSFS_CLASS_NAME);
129                 strcat(classpath, "/");
130                 strcat(classpath, name);
131         }
132         classdir = sysfs_open_directory(classpath);
133         if (classdir == NULL) {
134                 errno = EINVAL;
135                 dprintf("Class %s not supported on this system\n", name);
136                 return NULL;
137         }
138         if ((sysfs_read_directory(classdir)) != 0) {
139                 dprintf("Error reading %s class dir %s\n", name, classpath);
140                 sysfs_close_directory(classdir);
141                 return NULL;
142         }
143
144         return classdir;
145 }
146
147 /** 
148  * set_classdev_classname: Grabs classname from path
149  * @cdev: class device to set
150  * Returns nothing
151  */
152 static void set_classdev_classname(struct sysfs_class_device *cdev)
153 {
154         unsigned char *c = NULL, *e = NULL;
155         int count = 0;
156
157         c = strstr(cdev->path, SYSFS_CLASS_DIR);
158         if (c == NULL) 
159                 c = strstr(cdev->path, SYSFS_BLOCK_DIR);
160         else {
161                 c++;
162                 while (c != NULL && *c != '/') 
163                         c++;
164         }
165
166         if (c == NULL) 
167                 strcpy(cdev->classname, SYSFS_UNKNOWN);
168
169         else {
170                 c++;
171                 e = c;
172                 while (e != NULL && *e != '/' && *e != '\0') {
173                         e++;
174                         count++;
175                 }
176                 strncpy(cdev->classname, c, count);
177         }
178 }
179
180 /**
181  * sysfs_open_class_device: Opens and populates class device
182  * @path: path to class device.
183  * returns struct sysfs_class_device with success and NULL with error.
184  */
185 struct sysfs_class_device *sysfs_open_class_device(const unsigned char *path)
186 {
187         struct sysfs_class_device *cdev = NULL;
188         struct sysfs_directory *dir = NULL;
189         struct sysfs_link *curl = NULL;
190         struct sysfs_device *sdev = NULL;
191         struct sysfs_driver *drv = NULL;
192
193         if (path == NULL) {
194                 errno = EINVAL;
195                 return NULL;
196         }
197         cdev = alloc_class_device();
198         if (cdev == NULL) {
199                 dprintf("calloc failed\n");
200                 return NULL;
201         }
202         if ((sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) != 0) {
203                 errno = EINVAL;
204                 dprintf("Invalid class device path %s\n", path);
205                 sysfs_close_class_device(cdev);
206                 return NULL;
207         }
208
209         dir = sysfs_open_directory(path);
210         if (dir == NULL) {
211                 dprintf("Error opening class device at %s\n", path);
212                 sysfs_close_class_device(cdev);
213                 return NULL;
214         }
215         if ((sysfs_read_directory(dir)) != 0) {
216                 dprintf("Error reading class device at %s\n", path);
217                 sysfs_close_directory(dir);
218                 sysfs_close_class_device(cdev);
219                 return NULL;
220         }
221         sysfs_read_all_subdirs(dir);
222         cdev->directory = dir;
223         strcpy(cdev->path, dir->path);
224         set_classdev_classname(cdev);
225
226         /* get driver and device, if implemented */
227         if (cdev->directory->links != NULL) {
228                 dlist_for_each_data(cdev->directory->links, curl,
229                                 struct sysfs_link) {
230                         if (strncmp(curl->name, SYSFS_DEVICES_NAME, 6) == 0) {
231                                 sdev = sysfs_open_device(curl->target);
232                                 if (sdev != NULL) {
233                                         cdev->sysdevice = sdev;
234                                         if (cdev->driver != NULL) 
235                                                 strncpy(sdev->driver_name,
236                                                         cdev->driver->name, 
237                                                         SYSFS_NAME_LEN);
238                                 }
239                         } else if (strncmp(curl->name, 
240                                                 SYSFS_DRIVERS_NAME, 6) == 0) {
241                                 drv = sysfs_open_driver(curl->target);
242                                 if (drv != NULL) {
243                                         cdev->driver = drv;
244                                         if (cdev->sysdevice != NULL) {
245                                                 strncpy(cdev->sysdevice->name,
246                                                                 drv->name, 
247                                                                 SYSFS_NAME_LEN);
248                                                 if (drv->devices == NULL)
249                                                         drv->devices = 
250                                                                 dlist_new
251                                                                 (sizeof(struct 
252                                                                 sysfs_device));
253                                                 dlist_unshift(drv->devices, 
254                                                         cdev->sysdevice);
255                                         }
256                                 }
257                         }
258                 }
259         }
260         return cdev;
261 }
262
263 /**
264  * get_all_class_devices: gets all devices for class
265  * @class: class to get devices for
266  * returns 0 with success and -1 with failure
267  */
268 static int get_all_class_devices(struct sysfs_class *cls)
269 {
270         struct sysfs_class_device *dev = NULL;
271         struct sysfs_directory *cur = NULL;
272
273         if (cls == NULL || cls->directory == NULL) {
274                 errno = EINVAL;
275                 return -1;
276         }
277         if (cls->directory->subdirs == NULL)
278                 return 0;
279         dlist_for_each_data(cls->directory->subdirs, cur, 
280                         struct sysfs_directory) {
281                 dev = sysfs_open_class_device(cur->path);
282                 if (dev == NULL) {
283                         dprintf("Error opening device at %s\n", cur->path);
284                         continue;
285                 }
286                 if (cls->devices == NULL)
287                         cls->devices = dlist_new_with_delete
288                                         (sizeof(struct sysfs_class_device),
289                                                         sysfs_close_cls_dev);
290                 dlist_unshift(cls->devices, dev);
291         }
292         return 0;
293 }
294
295 /**
296  * sysfs_open_class: opens specific class and all its devices on system
297  * returns sysfs_class structure with success or NULL with error.
298  */
299 struct sysfs_class *sysfs_open_class(const unsigned char *name)
300 {
301         struct sysfs_class *cls = NULL;
302         struct sysfs_directory *classdir = NULL;
303
304         if (name == NULL) {
305                 errno = EINVAL;
306                 return NULL;
307         }
308
309         cls = alloc_class();
310         if (cls == NULL) {
311                 dprintf("calloc failed\n");
312                 return NULL;
313         }
314         strcpy(cls->name, name);        
315         classdir = open_class_dir(name);
316         if (classdir == NULL) {
317                 dprintf("Invalid class, %s not supported on this system\n",
318                         name);
319                 sysfs_close_class(cls);
320                 return NULL;
321         }
322         cls->directory = classdir;
323         strcpy(cls->path, classdir->path);
324         if ((get_all_class_devices(cls)) != 0) {
325                 dprintf("Error reading %s class devices\n", name);
326                 sysfs_close_class(cls);
327                 return NULL;
328         }
329
330         return cls;
331 }
332
333 /**
334  * sysfs_get_class_device: Get specific class device using the device's id
335  * @class: class to find device on
336  * @name: class name of the device
337  */ 
338 struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *class,
339                                         unsigned char *name)
340 {
341         if (class == NULL || name == NULL) {
342                 errno = EINVAL;
343                 return NULL;
344         }
345
346         return (struct sysfs_class_device *)dlist_find_custom(class->devices,
347                         name, class_name_equal);
348 }
349
350 /**
351  * get_classdev_path: given the class and a device in the class, return the
352  *              absolute path to the device
353  * @classname: name of the class
354  * @clsdev: the class device
355  * @path: buffer to return path
356  * @psize: size of "path"
357  * Returns 0 on SUCCESS or -1 on error
358  */
359 static int get_classdev_path(const unsigned char *classname, 
360                 const unsigned char *clsdev, unsigned char *path, size_t len)
361 {
362         if (classname == NULL || clsdev == NULL || path == NULL) {
363                 errno = EINVAL;
364                 return -1;
365         }
366         if (sysfs_get_mnt_path(path, len) != 0) {
367                 dprintf("Error getting sysfs mount path\n");
368                 return -1;
369         }
370         if (sysfs_trailing_slash(path) == 0)
371                 strcat(path, "/");
372
373         if (strcmp(classname, SYSFS_BLOCK_NAME) == 0) {
374                 strcat(path, SYSFS_BLOCK_NAME);
375         } else {
376                 strcat(path, SYSFS_CLASS_NAME);
377                 strcat(path, "/");
378                 strcat(path, classname);
379         }
380         strcat(path, "/");
381         strcat(path, clsdev);
382         return 0;
383 }
384
385 /**
386  * sysfs_open_class_device_by_name: Locates a specific class_device and returns it.
387  * Class_device must be closed using sysfs_close_class_device
388  * @classname: Class to search
389  * @name: name of the class_device
390  * 
391  * NOTE:
392  *      Call sysfs_close_class_device() to close the class device
393  */
394 struct sysfs_class_device *sysfs_open_class_device_by_name
395                 (const unsigned char *classname, const unsigned char *name)
396 {
397         unsigned char devpath[SYSFS_PATH_MAX];
398         struct sysfs_class_device *cdev = NULL;
399
400         if (classname == NULL || name == NULL) {
401                 errno = EINVAL;
402                 return NULL;
403         }
404         
405         memset(devpath, 0, SYSFS_PATH_MAX);
406         if ((get_classdev_path(classname, name, devpath, 
407                                         SYSFS_PATH_MAX)) != 0) {
408                 dprintf("Error getting to device %s on class %s\n",
409                                                         name, classname);
410                 return NULL;
411         }
412         
413         cdev = sysfs_open_class_device(devpath);
414         if (cdev == NULL) {
415                 dprintf("Error getting class device %s from class %s\n",
416                                 name, classname);
417                 return NULL;
418         }
419         return cdev;
420 }
421
422 /**
423  * sysfs_get_classdev_attributes: returns a dlist of attributes for
424  *      the requested class_device
425  * @cdev: sysfs_class_dev for which attributes are needed
426  * returns a dlist of attributes if exists, NULL otherwise
427  */
428 struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *cdev)
429 {
430         if (cdev == NULL || cdev->directory == NULL)
431                 return NULL;
432
433         return (cdev->directory->attributes);
434 }
435
436 /**
437  * sysfs_get_classdev_attr: searches class device's attributes by name
438  * @clsdev: class device to look through
439  * @name: attribute name to get
440  * returns sysfs_attribute reference with success or NULL with error
441  */
442 struct sysfs_attribute *sysfs_get_classdev_attr
443                 (struct sysfs_class_device *clsdev, const unsigned char *name)
444 {
445         struct sysfs_attribute *cur = NULL;
446
447         if (clsdev == NULL || clsdev->directory == NULL ||
448                 clsdev->directory->attributes == NULL || name == NULL) {
449                 errno = EINVAL;
450                 return NULL;
451         }
452
453         cur = sysfs_get_directory_attribute(clsdev->directory,
454                                                 (unsigned char *)name);
455         if (cur != NULL)
456                 return cur;
457
458         return NULL;
459 }
460
461 /**
462  * sysfs_open_classdev_attr: read an attribute for a given class device
463  * @classname: name of the class on which to look
464  * @dev: class device name for which the attribute has to be read
465  * @attrib: attribute to read
466  * Returns sysfs_attribute * on SUCCESS and NULL on error
467  * 
468  * NOTE:
469  *      A call to sysfs_close_attribute() is required to close the
470  *      attribute returned and to free memory
471  */
472 struct sysfs_attribute *sysfs_open_classdev_attr(const unsigned char *classname,
473                 const unsigned char *dev, const unsigned char *attrib)
474 {
475         struct sysfs_attribute *attribute = NULL;
476         unsigned char path[SYSFS_PATH_MAX];
477
478         if (classname == NULL || dev == NULL || attrib == NULL) {
479                 errno = EINVAL;
480                 return NULL;
481         }
482         memset(path, 0, SYSFS_PATH_MAX);
483         if ((get_classdev_path(classname, dev, path, SYSFS_PATH_MAX)) != 0) {
484                 dprintf("Error getting to device %s on class %s\n",
485                                                 dev, classname);
486                 return NULL;
487         }
488         strcat(path, "/");
489         strcat(path, attrib);
490         attribute = sysfs_open_attribute(path);
491         if (attribute == NULL) {
492                 dprintf("Error opening attribute %s on class device %s\n",
493                                 attrib, dev);
494                 return NULL;
495         }
496         if ((sysfs_read_attribute(attribute)) != 0) {
497                 dprintf("Error reading attribute %s for class device %s\n",
498                                 attrib, dev);
499                 sysfs_close_attribute(attribute);
500                 return NULL;
501         }
502         return attribute;
503 }