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