4 * Header Definitions for libsysfs
6 * Copyright (C) IBM Corp. 2003
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.
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.
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
26 #include <sys/types.h>
31 * Defines to prevent buffer overruns
33 #define safestrcpy(to, from) strncpy(to, from, sizeof(to)-1)
34 #define safestrcat(to, from) strncat(to, from, sizeof(to) - strlen(to)-1)
36 #define safestrcpymax(to, from, max) \
39 strncpy(to, from, max-1); \
42 #define safestrcatmax(to, from, max) \
45 strncat(to, from, max - strlen(to)-1); \
49 * Generic #defines go here..
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"
62 #define SYSFS_PATH_MAX 255
63 #define SYSFS_NAME_LEN 50
64 #define SYSFS_BUS_ID_SIZE 20
66 #define SYSFS_METHOD_SHOW 0x01 /* attr can be read by user */
67 #define SYSFS_METHOD_STORE 0x02 /* attr can be changed by user */
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
74 struct sysfs_attribute {
75 char name[SYSFS_NAME_LEN];
76 char path[SYSFS_PATH_MAX];
78 unsigned short len; /* value length */
79 unsigned short method; /* show and store */
83 char name[SYSFS_NAME_LEN];
84 char path[SYSFS_PATH_MAX];
85 char target[SYSFS_PATH_MAX];
88 struct sysfs_directory {
89 char name[SYSFS_NAME_LEN];
90 char path[SYSFS_PATH_MAX];
92 /* Private: for internal use only */
93 struct dlist *subdirs;
95 struct dlist *attributes;
99 char name[SYSFS_NAME_LEN];
100 char path[SYSFS_PATH_MAX];
102 /* Private: for internal use only */
103 struct dlist *devices;
104 struct sysfs_directory *directory;
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];
114 /* Private: for internal use only */
115 struct sysfs_device *parent;
116 struct dlist *children;
117 struct sysfs_directory *directory;
120 struct sysfs_root_device {
121 char name[SYSFS_NAME_LEN];
122 char path[SYSFS_PATH_MAX];
124 /* Private: for internal use only */
125 struct dlist *devices;
126 struct sysfs_directory *directory;
130 char name[SYSFS_NAME_LEN];
131 char path[SYSFS_PATH_MAX];
133 /* Private: for internal use only */
134 struct dlist *drivers;
135 struct dlist *devices;
136 struct sysfs_directory *directory;
139 struct sysfs_class_device {
140 char name[SYSFS_NAME_LEN];
141 char classname[SYSFS_NAME_LEN];
142 char path[SYSFS_PATH_MAX];
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;
152 char name[SYSFS_NAME_LEN];
153 char path[SYSFS_PATH_MAX];
155 /* Private: for internal use only */
156 struct dlist *devices;
157 struct sysfs_directory *directory;
165 * Function Prototypes
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);
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,
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);
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);
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);
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,
255 extern struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus,
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,
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
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,
294 * sort_list: sorter function to keep list elements sorted in alphabetical
295 * order. Just does a strncmp as you can see :)
297 * Returns 1 if less than 0 otherwise
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.
303 static inline int sort_list(void *new_elem, void *old_elem)
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);
315 #endif /* _LIBSYSFS_H_ */