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