chiark / gitweb /
[PATCH] libsysfs changes for sysfsutils 0.3.0
[elogind.git] / libsysfs / sysfs_device.c
1 /*
2  * sysfs_device.c
3  *
4  * Generic device 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 confirm_device_bus(struct sysfs_device *dev, 
27                                 unsigned char *busname, unsigned char *bus_id)
28 {
29         struct sysfs_link *devlink = NULL;
30         unsigned char devpath[SYSFS_PATH_MAX];
31         int result = 0;
32
33         if (busname == NULL || bus_id == NULL)
34                 return -1;
35
36         if (sysfs_get_mnt_path(devpath, SYSFS_PATH_MAX) != 0)
37                 return -1;
38
39         strcat(devpath, SYSFS_BUS_DIR);
40         strcat(devpath, "/");
41         strcat(devpath, busname);
42         strcat(devpath, SYSFS_DEVICES_DIR);
43         strcat(devpath, "/");
44         strcat(devpath, bus_id);
45
46         devlink = sysfs_open_link(devpath);
47         if (devlink == NULL)
48                 return -1;
49
50         if (strcmp(devlink->target, dev->path) == 0)
51                 result++;
52         sysfs_close_link(devlink);
53         return result;
54 }
55
56 /**
57  * get_device_bus: retrieves the bus name the device is on, checks path to
58  *      bus' link to make sure it has correct device.
59  * @dev: device to get busname.
60  * returns 0 with success and -1 with error.
61  */
62 static int get_device_bus(struct sysfs_device *dev)
63 {
64         unsigned char subsys[SYSFS_NAME_LEN], *bus = NULL, *curdev = NULL;
65         struct dlist *buslist = NULL, *device_list = NULL;
66
67         if (dev == NULL) {
68                 errno = EINVAL;
69                 return -1;
70         }
71
72         strcpy(subsys, SYSFS_BUS_DIR);  /* subsys = /bus */
73         buslist = sysfs_open_subsystem_list(subsys);
74         if (buslist != NULL) {
75                 dlist_for_each_data(buslist, bus, char) {
76                         device_list = sysfs_open_bus_devices_list(bus);
77                         if (device_list != NULL) {
78                                 dlist_for_each_data(device_list,
79                                                         curdev, char) {
80                                         if (strcmp(dev->bus_id, curdev) == 0
81                                             && confirm_device_bus(dev, bus,
82                                             curdev) > 0) {
83                                                 strcpy(dev->bus, bus);
84                                                 sysfs_close_list(device_list);
85                                                 sysfs_close_list(buslist);
86                                                 return 0;
87                                         }
88                                 }
89                         sysfs_close_list(device_list);
90                         }
91                 }
92                 sysfs_close_list(buslist);
93         }
94         return -1;
95 }
96
97 /**
98  * sysfs_close_device_tree: closes every device in the supplied tree, 
99  *      closing children only.
100  * @devroot: device root of tree.
101  */
102 static void sysfs_close_device_tree(struct sysfs_device *devroot)
103 {
104         if (devroot != NULL) {
105                 if (devroot->children != NULL) {
106                         struct sysfs_device *child = NULL;
107
108                         dlist_for_each_data(devroot->children, child,
109                                         struct sysfs_device) {
110                                 sysfs_close_device_tree(child);
111                         }
112                 }
113                 sysfs_close_device(devroot);
114         }
115 }
116
117 /**
118  * sysfs_del_device: routine for dlist integration
119  */
120 static void sysfs_del_device(void *dev)
121 {
122         sysfs_close_device((struct sysfs_device *)dev);
123 }
124
125 /**
126  * sysfs_close_dev_tree: routine for dlist integration
127  */
128 static void sysfs_close_dev_tree(void *dev)
129 {
130         sysfs_close_device_tree((struct sysfs_device *)dev);
131 }
132
133 /**
134  * sysfs_close_device: closes and cleans up a device
135  * @dev = device to clean up
136  */
137 void sysfs_close_device(struct sysfs_device *dev)
138 {
139         if (dev != NULL) {
140                 if (dev->directory != NULL)
141                         sysfs_close_directory(dev->directory);
142                 if (dev->children != NULL && dev->children->count == 0)
143                         dlist_destroy(dev->children);
144                 free(dev);
145         }
146 }
147
148 /**
149  * alloc_device: allocates and initializes device structure
150  * returns struct sysfs_device
151  */
152 static struct sysfs_device *alloc_device(void)
153 {
154         return (struct sysfs_device *)calloc(1, sizeof(struct sysfs_device));
155 }
156
157 /**
158  * sysfs_get_device_attr: searches dev's attributes by name
159  * @dev: device to look through
160  * @name: attribute name to get
161  * returns sysfs_attribute reference with success or NULL with error.
162  */
163 struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
164                                                 const unsigned char *name)
165 {
166         struct sysfs_attribute *cur = NULL;
167
168         if (dev == NULL || dev->directory == NULL 
169             || dev->directory->attributes == NULL || name == NULL) {
170                 errno = EINVAL;
171                 return NULL;
172         }
173         
174         cur = sysfs_get_directory_attribute(dev->directory, 
175                         (unsigned char *)name);
176         if (cur != NULL)
177                 return cur;
178
179         return NULL;
180 }
181
182 /**
183  * sysfs_open_device: opens and populates device structure
184  * @path: path to device, this is the /sys/devices/ path
185  * returns sysfs_device structure with success or NULL with error
186  */
187 struct sysfs_device *sysfs_open_device(const unsigned char *path)
188 {
189         struct sysfs_device *dev = NULL;
190         struct sysfs_directory *sdir = NULL;
191
192         if (path == NULL) {
193                 errno = EINVAL;
194                 return NULL;
195         }
196         dev = alloc_device();   
197         if (dev == NULL) {
198                 dprintf("Error allocating device at %s\n", path);
199                 return NULL;
200         }
201         sdir = sysfs_open_directory(path);
202         if (sdir == NULL) {
203                 dprintf("Invalid device at %s\n", path);
204                 errno = EINVAL;
205                 sysfs_close_device(dev);
206                 return NULL;
207         }
208         if ((sysfs_read_directory(sdir)) != 0) {
209                 dprintf("Error reading device directory at %s\n", path);
210                 sysfs_close_directory(sdir);
211                 sysfs_close_device(dev);
212                 return NULL;
213         }
214         dev->directory = sdir;
215         strcpy(dev->bus_id, sdir->name);
216         strcpy(dev->path, sdir->path);
217
218         /* 
219          * The "name" attribute no longer exists... return the device's
220          * sysfs representation instead, in the "dev->name" field, which
221          * implies that the dev->name and dev->bus_id contain same data.
222          */
223         strncpy(dev->name, sdir->name, SYSFS_NAME_LEN);
224         
225         if (get_device_bus(dev) != 0)
226                 strcpy(dev->bus, SYSFS_UNKNOWN);
227
228         return dev;
229 }
230
231 /**
232  * sysfs_open_device_tree: opens root device and all of its children,
233  *      creating a tree of devices. Only opens children.
234  * @path: sysfs path to devices
235  * returns struct sysfs_device and its children with success or NULL with
236  *      error.
237  */
238 static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path)
239 {
240         struct sysfs_device *rootdev = NULL, *new = NULL;
241         struct sysfs_directory *cur = NULL;
242
243         if (path == NULL) {
244                 errno = EINVAL;
245                 return NULL;
246         }
247         rootdev = sysfs_open_device(path);
248         if (rootdev == NULL) {
249                 dprintf("Error opening root device at %s\n", path);
250                 return NULL;
251         }
252         if (rootdev->directory->subdirs != NULL) {
253                 dlist_for_each_data(rootdev->directory->subdirs, cur,
254                                 struct sysfs_directory) {
255                         new = sysfs_open_device_tree(cur->path);
256                         if (new == NULL) {
257                                 dprintf("Error opening device tree at %s\n",
258                                         cur->path);
259                                 sysfs_close_device_tree(rootdev);
260                                 return NULL;
261                         }
262                         if (rootdev->children == NULL)
263                                 rootdev->children = dlist_new_with_delete
264                                         (sizeof(struct sysfs_device),
265                                         sysfs_del_device);
266                         dlist_unshift(rootdev->children, new);
267                 }
268         }
269
270         return rootdev;
271 }
272
273 /**
274  * sysfs_close_root_device: closes root and all devices
275  * @root: root device to close
276  */
277 void sysfs_close_root_device(struct sysfs_root_device *root)
278 {
279         if (root != NULL) {
280                 if (root->devices != NULL)
281                         dlist_destroy(root->devices);
282                 if (root->directory != NULL)
283                         sysfs_close_directory(root->directory);
284                 free(root);
285         }
286 }
287
288 /**
289  * open_root_device_dir: opens up sysfs_directory for specific root dev
290  * @name: name of root
291  * returns struct sysfs_directory with success and NULL with error
292  */
293 static struct sysfs_directory *open_root_device_dir(const unsigned char *name)
294 {
295         struct sysfs_directory *rdir = NULL;
296         unsigned char rootpath[SYSFS_PATH_MAX];
297
298         if (name == NULL) {
299                 errno = EINVAL;
300                 return NULL;
301         }
302
303         memset(rootpath, 0, SYSFS_PATH_MAX);
304         if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) {
305                 dprintf ("Sysfs not supported on this system\n");
306                 return NULL;
307         }
308
309         strcat(rootpath, SYSFS_DEVICES_DIR);
310         strcat(rootpath, "/");
311         strcat(rootpath, name);
312         rdir = sysfs_open_directory(rootpath);
313         if (rdir == NULL) {
314                 errno = EINVAL;
315                 dprintf ("Root device %s not supported on this system\n",
316                         name);
317                 return NULL;
318         }
319         if (sysfs_read_directory(rdir) != 0) {
320                 dprintf ("Error reading %s root device at dir %s\n", name,
321                         rootpath);
322                 sysfs_close_directory(rdir);
323                 return NULL;
324         }
325         
326         return rdir;
327 }
328
329 /**
330  * get_all_root_devices: opens up all the devices under this root device
331  * @root: root device to open devices for
332  * returns 0 with success and -1 with error
333  */
334 static int get_all_root_devices(struct sysfs_root_device *root)
335 {
336         struct sysfs_device *dev = NULL;
337         struct sysfs_directory *cur = NULL;
338
339         if (root == NULL || root->directory == NULL) {
340                 errno = EINVAL;
341                 return -1;
342         }
343         if (root->directory->subdirs == NULL)
344                 return 0;
345
346         dlist_for_each_data(root->directory->subdirs, cur,
347                         struct sysfs_directory) {
348                 dev = sysfs_open_device_tree(cur->path);
349                 if (dev == NULL) {
350                         dprintf ("Error opening device at %s\n", cur->path);
351                         continue;
352                 }
353                 if (root->devices == NULL)
354                         root->devices = dlist_new_with_delete
355                                 (sizeof(struct sysfs_device), 
356                                 sysfs_close_dev_tree);
357                 dlist_unshift(root->devices, dev);
358         }
359
360         return 0;
361 }
362
363 /**
364  * sysfs_open_root_device: opens sysfs devices root and all of its
365  *      devices.
366  * @name: name of /sys/devices/root to open
367  * returns struct sysfs_root_device if success and NULL with error
368  */
369 struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name)
370 {
371         struct sysfs_root_device *root = NULL;
372         struct sysfs_directory *rootdir = NULL;
373
374         if (name == NULL) {
375                 errno = EINVAL;
376                 return NULL;
377         }
378
379         root = (struct sysfs_root_device *)calloc
380                                         (1, sizeof(struct sysfs_root_device));
381         if (root == NULL) {
382                 dprintf("calloc failure\n");
383                 return NULL;
384         }
385         rootdir = open_root_device_dir(name);
386         if (rootdir == NULL) {
387                 dprintf ("Invalid root device, %s not supported\n", name);
388                 sysfs_close_root_device(root);
389                 return NULL;
390         }
391         strcpy(root->path, rootdir->path);
392         root->directory = rootdir;
393         if (get_all_root_devices(root) != 0) {
394                 dprintf ("Error retrieving devices for root %s\n", name);
395                 sysfs_close_root_device(root);
396                 return NULL;
397         }
398
399         return root;
400 }
401
402 /**
403  * sysfs_get_device_attributes: returns a dlist of attributes corresponding to
404  *      the specific device
405  * @device: struct sysfs_device * for which attributes are to be returned
406  */
407 struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
408 {
409         if (device == NULL || device->directory == NULL) 
410                 return NULL;
411
412         return (device->directory->attributes);
413 }
414
415 /**
416  * get_device_absolute_path: looks up the bus the device is on, gets 
417  *              absolute path to the device
418  * @device: device for which path is needed
419  * @path: buffer to store absolute path
420  * @psize: size of "path"
421  * Returns 0 on success -1 on failure
422  */
423 static int get_device_absolute_path(const unsigned char *device,
424                 const unsigned char *bus, unsigned char *path, size_t psize)
425 {
426         unsigned char bus_path[SYSFS_NAME_LEN];
427
428         if (device == NULL || path == NULL) {
429                 errno = EINVAL;
430                 return -1;
431         }
432
433         memset(bus_path, 0, SYSFS_NAME_LEN);
434         if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX) != 0) {
435                 dprintf ("Sysfs not supported on this system\n");
436                 return -1;
437         }
438         strcat(bus_path, SYSFS_BUS_DIR);
439         strcat(bus_path, "/");
440         strcat(bus_path, bus);
441         strcat(bus_path, SYSFS_DEVICES_DIR);
442         strcat(bus_path, "/");
443         strcat(bus_path, device);
444         /*
445          * We now are at /sys/bus/"bus_name"/devices/"device" which is a link.
446          * Now read this link to reach to the device.
447          */ 
448         if ((sysfs_get_link(bus_path, path, SYSFS_PATH_MAX)) != 0) {
449                 dprintf("Error getting to device %s\n", device);
450                 return -1;
451         }
452         return 0;
453 }
454
455 /**
456  * sysfs_open_device_by_id: open a device by id (use the "bus" subsystem)
457  * @bus_id: bus_id of the device to open - has to be the "bus_id" in 
458  *              /sys/bus/xxx/devices
459  * @bus: bus the device belongs to
460  * @bsize: size of the bus buffer
461  * returns struct sysfs_device if found, NULL otherwise
462  * NOTE: 
463  * 1. Use sysfs_close_device to close the device
464  * 2. Bus the device is on must be supplied
465  *      Use sysfs_find_device_bus to get the bus name
466  */
467 struct sysfs_device *sysfs_open_device_by_id(const unsigned char *bus_id, 
468                 const unsigned char *bus, size_t bsize)
469 {
470         char sysfs_path[SYSFS_PATH_MAX];
471         struct sysfs_device *device = NULL;
472
473         if (bus_id == NULL || bus == NULL) {
474                 errno = EINVAL;
475                 return NULL;
476         }
477         memset(sysfs_path, 0, SYSFS_PATH_MAX);
478         if ((get_device_absolute_path(bus_id, bus, sysfs_path, 
479                                                 SYSFS_PATH_MAX)) != 0) {
480                 dprintf("Error getting to device %s\n", bus_id);
481                 return NULL;
482         }
483         
484         device = sysfs_open_device(sysfs_path);
485         if (device == NULL) {
486                 dprintf("Error opening device %s\n", bus_id);
487                 return NULL;
488         }
489
490         return device;
491 }
492
493 /*
494  * sysfs_open_device_attr: open the given device's attribute
495  * @bus: Bus on which to look
496  * @dev_id: device for which attribute is required
497  * @attrname: name of the attribute to look for
498  * Returns struct sysfs_attribute on success and NULL on failure
499  * 
500  * NOTE:
501  *      A call to sysfs_close_attribute() is required to close
502  *      the attribute returned and free memory. 
503  */
504 struct sysfs_attribute *sysfs_open_device_attr(const unsigned char *bus,
505                 const unsigned char *bus_id, const unsigned char *attrib)
506 {
507         struct sysfs_attribute *attribute = NULL;
508         unsigned char devpath[SYSFS_PATH_MAX];
509         
510         if (bus == NULL || bus_id == NULL || attrib == NULL) {
511                 errno = EINVAL;
512                 return NULL;
513         }
514
515         memset(devpath, 0, SYSFS_PATH_MAX);
516         if ((get_device_absolute_path(bus_id, bus, devpath, 
517                                         SYSFS_PATH_MAX)) != 0) {
518                 dprintf("Error getting to device %s\n", bus_id);
519                 return NULL;
520         }
521         strcat(devpath, "/");
522         strcat(devpath, attrib);
523         attribute = sysfs_open_attribute(devpath);
524         if (attribute == NULL) {
525                 dprintf("Error opening attribute %s for device %s\n",
526                                 attrib, bus_id);
527                 return NULL;
528         }
529         if ((sysfs_read_attribute(attribute)) != 0) {
530                 dprintf("Error reading attribute %s for device %s\n",
531                                 attrib, bus_id);
532                 sysfs_close_attribute(attribute);
533                 return NULL;
534         }
535         return attribute;
536 }
537