chiark / gitweb /
[PATCH] extras/multipath update
[elogind.git] / libsysfs / sysfs_driver.c
1 /*
2  * sysfs_driver.c
3  *
4  * Driver 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_driver_by_name_dev(void *device)
27 {
28         sysfs_close_device((struct sysfs_device *)device);
29 }
30
31 /**
32  * sysfs_close_driver: closes and cleans up driver structure
33  * NOTE: This routine does not deallocate devices list
34  * @driver: driver to close
35  */
36 void sysfs_close_driver(struct sysfs_driver *driver)
37 {
38         if (driver != NULL) {
39                 if (driver->devices != NULL) {
40                         dlist_for_each(driver->devices) 
41                                 dlist_shift(driver->devices);
42                         free(driver->devices);
43                         driver->devices = NULL;
44                 }
45                 if (driver->directory != NULL)
46                         sysfs_close_directory(driver->directory);
47                 free(driver);
48         }
49 }
50
51 /** 
52  * sysfs_close_driver_by_name: closes driver and deletes device lists too
53  * @driver: driver to close
54  */ 
55 void sysfs_close_driver_by_name(struct sysfs_driver *driver)
56 {
57         if (driver != NULL) {
58                 if (driver->devices != NULL) 
59                         dlist_destroy(driver->devices);
60                 if (driver->directory != NULL)
61                         sysfs_close_directory(driver->directory);
62                 free(driver);
63         }
64 }
65                 
66 /**
67  * alloc_driver: allocates and initializes driver
68  * returns struct sysfs_driver with success and NULL with error.
69  */
70 static struct sysfs_driver *alloc_driver(void)
71 {
72         return (struct sysfs_driver *)calloc(1, sizeof(struct sysfs_driver));
73 }
74
75 /**
76  * sysfs_open_driver: opens and initializes driver structure
77  * @path: path to driver directory
78  * returns struct sysfs_driver with success and NULL with error
79  */
80 struct sysfs_driver *sysfs_open_driver(const unsigned char *path)
81 {
82         struct sysfs_driver *driver = NULL;
83         struct sysfs_directory *sdir = NULL;
84
85         if (path == NULL) {
86                 errno = EINVAL;
87                 return NULL;
88         }
89         sdir = sysfs_open_directory(path);
90         if (sdir == NULL) {
91                 dprintf("Error opening directory %s\n", path);
92                 return NULL;
93         }
94         if ((sysfs_read_directory(sdir)) != 0) {
95                 dprintf("Error reading directory %s\n", path);
96                 sysfs_close_directory(sdir);
97                 return NULL;
98         }
99         driver = alloc_driver();
100         if (driver == NULL) {
101                 dprintf("Error allocating driver at %s\n", path);
102                 sysfs_close_directory(sdir);
103                 return NULL;
104         }
105         strcpy(driver->name, sdir->name);
106         driver->directory = sdir;       
107         strcpy(driver->path, sdir->path);
108         
109         return driver;
110 }
111
112 /**
113  * sysfs_get_driver_attributes: gets list of attributes for the given driver
114  * @driver: sysfs_driver for which attributes are required
115  * returns a dlist of attributes corresponding to the driver if present
116  *      NULL otherwise
117  */
118 struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver)
119 {
120         if (driver == NULL || driver->directory == NULL)
121                 return NULL;
122
123         return(driver->directory->attributes);
124 }
125
126 /**
127  * sysfs_get_driver_attr: searches driver's attributes by name
128  * @drv: driver to look through
129  * @name: attribute name to get
130  * returns sysfs_attribute reference on success or NULL with error
131  */ 
132 struct sysfs_attribute *sysfs_get_driver_attr(struct sysfs_driver *drv,
133                                         const unsigned char *name)
134 {
135         struct sysfs_attribute *cur = NULL;
136
137         if (drv == NULL || drv->directory == NULL
138             || drv->directory->attributes == NULL || name == NULL) {
139                 errno = EINVAL;
140                 return NULL;
141         }
142
143         cur = sysfs_get_directory_attribute(drv->directory,
144                                         (unsigned char *)name);
145         if (cur != NULL)
146                 return cur;
147
148         return NULL;
149 }
150
151 /**
152  * sysfs_get_driver_links: gets list of links from the given driver
153  * @driver: sysfs_driver for which links list is required
154  * returns a dlist of links corresponding to the driver if present
155  *      NULL otherwise
156  */
157 struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver)
158 {
159         if (driver == NULL || driver->directory == NULL)
160                 return NULL;
161
162         return(driver->directory->links);
163 }
164
165 /**
166  * get_driver_path: looks up the bus the driver is on and builds path to
167  *              the driver.
168  * @bus: bus on which to search
169  * @drv: driver to look for
170  * @path: buffer to return path to driver
171  * @psize: size of "path"
172  * Returns 0 on success and -1 on error
173  */
174 static int get_driver_path(const unsigned char *bus, const unsigned char *drv, 
175                                 unsigned char *path, size_t psize)
176 {
177         if (bus == NULL || drv == NULL || path == NULL) {
178                 errno = EINVAL;
179                 return -1;
180         }
181         if (sysfs_get_mnt_path(path, psize) != 0) {
182                 dprintf("Error getting sysfs mount path\n");
183                 return -1;
184         }
185         if (sysfs_trailing_slash(path) == 0)
186                 strcat(path, "/");
187         strcat(path, SYSFS_BUS_NAME);
188         strcat(path, "/");
189         strcat(path, bus);
190         strcat(path, SYSFS_DRIVERS_DIR);
191         strcat(path, "/");
192         strcat(path, drv);
193         return 0;
194 }
195
196 /**
197  * sysfs_open_driver_by_name: open a driver by name and return the bus
198  * the driver is on.
199  * @drv_name: driver to open
200  * @bus: the driver bus
201  * @bsize: size of bus buffer
202  * returns struct sysfs_driver if found, NULL otherwise
203  * NOTE: 
204  * 1. Need to call sysfs_close_driver_by_name to free up memory
205  * 2. Bus the driver is registered with must be supplied.
206  *      Use sysfs_find_driver_bus() to obtain the bus name
207  */
208 struct sysfs_driver *sysfs_open_driver_by_name(const unsigned char *drv_name,
209                                 const unsigned char *bus, size_t bsize)
210 {
211         struct sysfs_driver *driver = NULL;
212         struct sysfs_device *device = NULL;
213         struct sysfs_link *curlink = NULL;
214         unsigned char path[SYSFS_PATH_MAX];
215
216         if (drv_name == NULL || bus == NULL) {
217                 errno = EINVAL;
218                 return NULL;
219         }
220
221         memset(path, 0, SYSFS_PATH_MAX);
222         if (get_driver_path(bus, drv_name, path, SYSFS_PATH_MAX) != 0) {
223                 dprintf("Error getting to driver %s\n", drv_name);
224                 return NULL;
225         }
226         driver = sysfs_open_driver(path);
227         if (driver == NULL) {
228                 dprintf("Could not open driver %s\n", drv_name);
229                 return NULL;
230         }
231         if (driver->directory->links != NULL) {
232                 dlist_for_each_data(driver->directory->links, curlink, 
233                                                         struct sysfs_link) {
234                         device = sysfs_open_device(curlink->target);
235                         if (device == NULL) {
236                                 dprintf("Error opening device at %s\n", 
237                                                 curlink->target);
238                                 sysfs_close_driver_by_name(driver);
239                                 return NULL;
240                         }
241                         strcpy(device->driver_name, drv_name);
242                         if (driver->devices == NULL) 
243                                 driver->devices = dlist_new_with_delete
244                                                 (sizeof(struct sysfs_device),
245                                                         sysfs_close_driver_by_name_dev);
246                         dlist_unshift(driver->devices, device);
247                 }
248         }
249         return driver;
250 }
251
252
253 /**
254  * sysfs_open_driver_attr: read the user supplied driver attribute
255  * @bus: bus on which to look 
256  * @drv: driver whose attribute has to be read
257  * @attrib: Attribute to be read
258  * Returns struct sysfs_attribute on success and NULL on failure
259  *
260  * NOTE:
261  *      A call to sysfs_close_attribute() is required to close the
262  *      attribute returned and to free memory
263  */ 
264 struct sysfs_attribute *sysfs_open_driver_attr(const unsigned char *bus, 
265                 const unsigned char *drv, const unsigned char *attrib)
266 {
267         struct sysfs_attribute *attribute = NULL;
268         unsigned char path[SYSFS_PATH_MAX];
269
270         if (bus == NULL || drv == NULL || attrib == NULL) {
271                 errno = EINVAL;
272                 return NULL;
273         }
274
275         memset(path, 0, SYSFS_NAME_LEN);
276         if ((get_driver_path(bus, drv, path, SYSFS_PATH_MAX)) != 0) {
277                 dprintf("Error getting to driver %s\n", drv);
278                 return NULL;
279         }
280         strcat(path, "/");
281         strcat(path, attrib);
282         attribute = sysfs_open_attribute(path);
283         if (attribute == NULL) {
284                 dprintf("Error opening attribute %s for driver %s\n",
285                                 attrib, drv);
286                 return NULL;
287         }
288         if ((sysfs_read_attribute(attribute)) != 0) {
289                 dprintf("Error reading attribute %s for driver %s\n", 
290                                 attrib, drv);
291                 sysfs_close_attribute(attribute);
292                 return NULL;
293         }
294         return attribute;
295 }
296