chiark / gitweb /
[PATCH] clean up the aoe char device rules, and delete the block one as it's not...
[elogind.git] / libsysfs / sysfs / libsysfs.h
1 /*
2  * libsysfs.h
3  *
4  * Header Definitions 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 #ifndef _LIBSYSFS_H_
24 #define _LIBSYSFS_H_
25
26 #include <sys/types.h>
27 #include <string.h>
28 #include "dlist.h"
29
30 /* 
31  * Defines to prevent buffer overruns
32  */
33 #define safestrcpy(to, from)    strncpy(to, from, sizeof(to)-1)
34 #define safestrcat(to, from)    strncat(to, from, sizeof(to) - strlen(to)-1)
35
36 #define safestrcpymax(to, from, max) \
37 do { \
38         to[max-1] = '\0'; \
39         strncpy(to, from, max-1); \
40 } while (0)
41
42 #define safestrcatmax(to, from, max) \
43 do { \
44         to[max-1] = '\0'; \
45         strncat(to, from, max - strlen(to)-1); \
46 } while (0)
47
48 /*
49  * Generic #defines go here..
50  */ 
51 #define SYSFS_FSTYPE_NAME       "sysfs"
52 #define SYSFS_PROC_MNTS         "/proc/mounts"
53 #define SYSFS_BUS_NAME          "bus"
54 #define SYSFS_CLASS_NAME        "class"
55 #define SYSFS_BLOCK_NAME        "block"
56 #define SYSFS_DEVICES_NAME      "devices"
57 #define SYSFS_DRIVERS_NAME      "drivers"
58 #define SYSFS_NAME_ATTRIBUTE    "name"
59 #define SYSFS_UNKNOWN           "unknown"
60 #define SYSFS_PATH_ENV          "SYSFS_PATH"
61
62 #define SYSFS_PATH_MAX          255
63 #define SYSFS_NAME_LEN          50
64 #define SYSFS_BUS_ID_SIZE       20
65
66 #define SYSFS_METHOD_SHOW       0x01    /* attr can be read by user */
67 #define SYSFS_METHOD_STORE      0x02    /* attr can be changed by user */
68
69 /*
70  * NOTE: We have the statically allocated "name" as the first element of all 
71  * the structures. This feature is used in the "sorter" function for dlists
72  */
73
74 struct sysfs_attribute {
75         char name[SYSFS_NAME_LEN];
76         char path[SYSFS_PATH_MAX];
77         char *value;
78         unsigned short len;             /* value length */
79         unsigned short method;          /* show and store */
80 };
81
82 struct sysfs_link {
83         char name[SYSFS_NAME_LEN];
84         char path[SYSFS_PATH_MAX];
85         char target[SYSFS_PATH_MAX];
86 };
87
88 struct sysfs_directory {
89         char name[SYSFS_NAME_LEN];
90         char path[SYSFS_PATH_MAX];
91
92         /* Private: for internal use only */
93         struct dlist *subdirs;  
94         struct dlist *links;            
95         struct dlist *attributes;
96 };
97
98 struct sysfs_driver {
99         char name[SYSFS_NAME_LEN];
100         char path[SYSFS_PATH_MAX];
101
102         /* Private: for internal use only */
103         struct dlist *devices;
104         struct sysfs_directory *directory;      
105 };
106
107 struct sysfs_device {
108         char name[SYSFS_NAME_LEN];
109         char bus_id[SYSFS_NAME_LEN];
110         char bus[SYSFS_NAME_LEN];
111         char driver_name[SYSFS_NAME_LEN];
112         char path[SYSFS_PATH_MAX];
113
114         /* Private: for internal use only */
115         struct sysfs_device *parent;            
116         struct dlist *children; 
117         struct sysfs_directory *directory;      
118 };
119
120 struct sysfs_root_device {
121         char name[SYSFS_NAME_LEN];
122         char path[SYSFS_PATH_MAX];
123
124         /* Private: for internal use only */
125         struct dlist *devices;
126         struct sysfs_directory *directory;
127 };
128
129 struct sysfs_bus {
130         char name[SYSFS_NAME_LEN];
131         char path[SYSFS_PATH_MAX];
132
133         /* Private: for internal use only */
134         struct dlist *drivers;
135         struct dlist *devices;
136         struct sysfs_directory *directory;      
137 };
138
139 struct sysfs_class_device {
140         char name[SYSFS_NAME_LEN];
141         char classname[SYSFS_NAME_LEN];
142         char path[SYSFS_PATH_MAX];
143
144         /* Private: for internal use only */
145         struct sysfs_class_device *parent;      
146         struct sysfs_device *sysdevice;         /* NULL if virtual */
147         struct sysfs_driver *driver;            /* NULL if not implemented */
148         struct sysfs_directory *directory;      
149 };
150
151 struct sysfs_class {
152         char name[SYSFS_NAME_LEN];
153         char path[SYSFS_PATH_MAX];
154
155         /* Private: for internal use only */
156         struct dlist *devices;
157         struct sysfs_directory *directory;      
158 };
159
160 #ifdef __cplusplus
161 extern "C" {
162 #endif
163
164 /*
165  * Function Prototypes
166  */
167 extern int sysfs_get_mnt_path(char *mnt_path, size_t len);
168 extern int sysfs_remove_trailing_slash(char *path);
169 extern int sysfs_get_name_from_path(const char *path, char *name, size_t len);
170 extern int sysfs_path_is_dir(const char *path);
171 extern int sysfs_path_is_link(const char *path);
172 extern int sysfs_path_is_file(const char *path);
173 extern int sysfs_get_link(const char *path, char *target, size_t len);
174 extern struct dlist *sysfs_open_subsystem_list(char *name);
175 extern struct dlist *sysfs_open_bus_devices_list(char *name);
176 extern void sysfs_close_list(struct dlist *list);
177
178 /* sysfs directory and file access */
179 extern void sysfs_close_attribute(struct sysfs_attribute *sysattr);
180 extern struct sysfs_attribute *sysfs_open_attribute(const char *path);
181 extern int sysfs_read_attribute(struct sysfs_attribute *sysattr);
182 extern int sysfs_read_attribute_value(const char *attrpath, 
183                 char *value, size_t vsize);
184 extern int sysfs_write_attribute(struct sysfs_attribute *sysattr,
185                 const char *new_value, size_t len);
186 extern char *sysfs_get_value_from_attributes(struct dlist *attr, 
187                 const char *name);
188 extern int sysfs_refresh_dir_attributes(struct sysfs_directory *sysdir);
189 extern int sysfs_refresh_dir_links(struct sysfs_directory *sysdir);
190 extern int sysfs_refresh_dir_subdirs(struct sysfs_directory *sysdir);
191 extern void sysfs_close_directory(struct sysfs_directory *sysdir);
192 extern struct sysfs_directory *sysfs_open_directory(const char *path);
193 extern int sysfs_read_dir_attributes(struct sysfs_directory *sysdir);
194 extern int sysfs_read_dir_links(struct sysfs_directory *sysdir);
195 extern int sysfs_read_dir_subdirs(struct sysfs_directory *sysdir);
196 extern int sysfs_read_directory(struct sysfs_directory *sysdir);
197 extern int sysfs_read_all_subdirs(struct sysfs_directory *sysdir);
198 extern struct sysfs_directory *sysfs_get_subdirectory
199         (struct sysfs_directory *dir, char *subname);
200 extern void sysfs_close_link(struct sysfs_link *ln);
201 extern struct sysfs_link *sysfs_open_link(const char *lnpath);
202 extern struct sysfs_link *sysfs_get_directory_link
203         (struct sysfs_directory *dir, char *linkname);
204 extern struct sysfs_link *sysfs_get_subdirectory_link
205         (struct sysfs_directory *dir, char *linkname);
206 extern struct sysfs_attribute *sysfs_get_directory_attribute
207         (struct sysfs_directory *dir, char *attrname);
208 extern struct dlist *sysfs_get_dir_attributes(struct sysfs_directory *dir);
209 extern struct dlist *sysfs_get_dir_links(struct sysfs_directory *dir);
210 extern struct dlist *sysfs_get_dir_subdirs(struct sysfs_directory *dir);
211
212 /* sysfs driver access */
213 extern void sysfs_close_driver(struct sysfs_driver *driver);
214 extern struct sysfs_driver *sysfs_open_driver
215         (const char *bus_name, const char *drv_name);
216 extern struct sysfs_driver *sysfs_open_driver_path(const char *path);
217 extern struct sysfs_attribute *sysfs_get_driver_attr
218         (struct sysfs_driver *drv, const char *name);
219 extern struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver);
220 extern struct dlist *sysfs_get_driver_devices(struct sysfs_driver *driver);
221 extern struct dlist *sysfs_refresh_driver_devices(struct sysfs_driver *driver);
222 extern struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver);
223 extern struct sysfs_device *sysfs_get_driver_device
224         (struct sysfs_driver *driver, const char *name);
225 extern struct dlist *sysfs_refresh_driver_attributes
226         (struct sysfs_driver *driver);
227 extern struct sysfs_attribute *sysfs_open_driver_attr
228         (const char *bus, const char *drv, const char *attrib);
229
230 /* generic sysfs device access */
231 extern void sysfs_close_root_device(struct sysfs_root_device *root);
232 extern struct sysfs_root_device *sysfs_open_root_device(const char *name);
233 extern struct dlist *sysfs_get_root_devices(struct sysfs_root_device *root);
234 extern void sysfs_close_device_tree(struct sysfs_device *device);
235 extern struct sysfs_device *sysfs_open_device_tree(const char *path);
236 extern void sysfs_close_device(struct sysfs_device *dev);
237 extern struct sysfs_device *sysfs_open_device
238         (const char *bus, const char *bus_id);
239 extern struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev);
240 extern struct sysfs_device *sysfs_open_device_path(const char *path);
241 extern int sysfs_get_device_bus(struct sysfs_device *dev);
242 extern struct sysfs_attribute *sysfs_get_device_attr
243         (struct sysfs_device *dev, const char *name);
244 extern struct dlist *sysfs_get_device_attributes(struct sysfs_device *device);
245 extern struct dlist *sysfs_refresh_device_attributes
246         (struct sysfs_device *device);
247 extern struct sysfs_attribute *sysfs_open_device_attr(const char *bus, 
248                 const char *bus_id, const char *attrib);
249
250 /* generic sysfs bus access */
251 extern void sysfs_close_bus(struct sysfs_bus *bus);
252 extern struct sysfs_bus *sysfs_open_bus(const char *name);
253 extern struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus, 
254                 char *id);
255 extern struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus,
256                 char *drvname);
257 extern struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus);
258 extern struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus);
259 extern struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus);
260 extern struct dlist *sysfs_refresh_bus_attributes(struct sysfs_bus *bus);
261 extern struct sysfs_attribute *sysfs_get_bus_attribute
262         (struct sysfs_bus *bus, char *attrname);
263 extern int sysfs_find_driver_bus(const char *driver, char *busname, 
264                 size_t bsize);
265
266 /* generic sysfs class access */
267 extern void sysfs_close_class_device(struct sysfs_class_device *dev);
268 extern struct sysfs_class_device *sysfs_open_class_device_path
269         (const char *path);
270 extern struct sysfs_class_device *sysfs_open_class_device
271         (const char *classname, const char *name);
272 extern struct sysfs_device *sysfs_get_classdev_device
273         (struct sysfs_class_device *clsdev);
274 extern struct sysfs_driver *sysfs_get_classdev_driver
275         (struct sysfs_class_device *clsdev);
276 extern struct sysfs_class_device *sysfs_get_classdev_parent
277         (struct sysfs_class_device *clsdev);
278 extern void sysfs_close_class(struct sysfs_class *cls);
279 extern struct sysfs_class *sysfs_open_class(const char *name);
280 extern struct dlist *sysfs_get_class_devices(struct sysfs_class *cls);
281 extern struct sysfs_class_device *sysfs_get_class_device
282         (struct sysfs_class *cls, char *name);
283 extern struct dlist *sysfs_get_classdev_attributes
284         (struct sysfs_class_device *cdev);
285 extern struct dlist *sysfs_refresh_classdev_attributes
286         (struct sysfs_class_device *cdev);
287 extern struct sysfs_attribute *sysfs_get_classdev_attr
288         (struct sysfs_class_device *clsdev, const char *name);
289 extern struct sysfs_attribute *sysfs_open_classdev_attr
290         (const char *classname, const char *dev, 
291                                                 const char *attrib); 
292
293 /**
294  * sort_list: sorter function to keep list elements sorted in alphabetical 
295  *      order. Just does a strncmp as you can see :)
296  *      
297  * Returns 1 if less than 0 otherwise
298  *
299  * NOTE: We take care to have a statically allocated "name" as the first 
300  *      lement of all libsysfs structures. Hence, this function will work 
301  *      AS IS for _ALL_ the lists that have to be sorted.
302  */
303 static inline int sort_list(void *new_elem, void *old_elem)
304 {
305         return ((strncmp(((struct sysfs_attribute *)new_elem)->name,
306                 ((struct sysfs_attribute *)old_elem)->name,
307                 strlen(((struct sysfs_attribute *)new_elem)->name))) < 0 ? 1 : 0);
308 }
309
310
311 #ifdef __cplusplus
312 }
313 #endif
314
315 #endif /* _LIBSYSFS_H_ */