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