chiark / gitweb /
[PATCH] implement printf-like placeholder support for NAME
[elogind.git] / libsysfs / 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 "dlist.h"
28
29 /*
30  * Generic #defines go here..
31  */ 
32 #define SYSFS_FSTYPE_NAME       "sysfs"
33 #define SYSFS_PROC_MNTS         "/proc/mounts"
34 #define SYSFS_BUS_DIR           "/bus"
35 #define SYSFS_CLASS_DIR         "/class"
36 #define SYSFS_BLOCK_DIR         "/block"
37 #define SYSFS_DEVICES_DIR       "/devices"
38 #define SYSFS_DEVICES_NAME      "devices"
39 #define SYSFS_DRIVERS_DIR       "/drivers"
40 #define SYSFS_DRIVERS_NAME      "drivers"
41 #define SYSFS_NAME_ATTRIBUTE    "name"
42 #define SYSFS_UNKNOWN           "unknown"
43 #define SYSFS_PATH_ENV          "SYSFS_PATH"
44
45 /* Some "block" subsystem specific #defines */
46 #define SYSFS_QUEUE_NAME        "queue"
47 #define SYSFS_IOSCHED_NAME      "iosched"
48
49 #define SYSFS_PATH_MAX          255
50 #define SYSFS_NAME_LEN          50
51 #define SYSFS_BUS_ID_SIZE       20
52
53 #define SYSFS_METHOD_SHOW       0x01    /* attr can be read by user */
54 #define SYSFS_METHOD_STORE      0x02    /* attr can be changed by user */
55
56 struct sysfs_attribute {
57         unsigned char *value;
58         unsigned short len;             /* value length */
59         unsigned short method;          /* show and store */
60         unsigned char name[SYSFS_NAME_LEN];
61         unsigned char path[SYSFS_PATH_MAX];
62 };
63
64 struct sysfs_link {
65         unsigned char name[SYSFS_NAME_LEN];
66         unsigned char path[SYSFS_PATH_MAX];
67         unsigned char target[SYSFS_PATH_MAX];
68 };
69
70 struct sysfs_directory {
71         struct dlist *subdirs;  
72         struct dlist *links;            
73         struct dlist *attributes;
74         unsigned char name[SYSFS_NAME_LEN];
75         unsigned char path[SYSFS_PATH_MAX];
76 };
77
78 struct sysfs_driver {
79         struct dlist *devices;
80         unsigned char name[SYSFS_NAME_LEN];
81         unsigned char path[SYSFS_PATH_MAX];
82
83         /* for internal use only */
84         struct sysfs_directory *directory;      
85 };
86
87 struct sysfs_device {
88         struct sysfs_device *parent;            
89         struct dlist *children; 
90         unsigned char name[SYSFS_NAME_LEN];
91         unsigned char bus_id[SYSFS_NAME_LEN];
92         unsigned char path[SYSFS_PATH_MAX];
93         unsigned char driver_name[SYSFS_NAME_LEN];
94
95         /* for internal use only */
96         struct sysfs_directory *directory;      
97 };
98
99 struct sysfs_root_device {
100         struct dlist *devices;
101         unsigned char name[SYSFS_NAME_LEN];
102         unsigned char path[SYSFS_PATH_MAX];
103
104         /* for internal use only */
105         struct sysfs_directory *directory;
106 };
107
108 struct sysfs_bus {
109         struct dlist *drivers;
110         struct dlist *devices;
111         unsigned char name[SYSFS_NAME_LEN];
112         unsigned char path[SYSFS_PATH_MAX];
113
114         /* internal use only */
115         struct sysfs_directory *directory;      
116 };
117
118 struct sysfs_class_device {
119         struct sysfs_device *sysdevice;         /* NULL if virtual */
120         struct sysfs_driver *driver;            /* NULL if not implemented */
121         unsigned char name[SYSFS_NAME_LEN];
122         unsigned char path[SYSFS_PATH_MAX];
123
124         /* for internal use only */
125         struct sysfs_directory *directory;      
126 };
127
128 struct sysfs_class {
129         struct dlist *devices;
130         unsigned char name[SYSFS_NAME_LEN];
131         unsigned char path[SYSFS_PATH_MAX];
132
133         /* for internal use only */
134         struct sysfs_directory *directory;      
135 };
136
137 #ifdef __cplusplus
138 extern "C" {
139 #endif
140
141 /*
142  * Function Prototypes
143  */
144 extern int sysfs_get_mnt_path(unsigned char *mnt_path, size_t len);
145 extern int sysfs_get_name_from_path(const unsigned char *path, 
146                                         unsigned char *name, size_t len);
147 extern int sysfs_get_link(const unsigned char *path, unsigned char *target, 
148                                                                 size_t len);
149 extern struct dlist *sysfs_open_subsystem_list(unsigned char *name);
150 extern struct dlist *sysfs_open_bus_devices_list(unsigned char *name);
151 extern void sysfs_close_list(struct dlist *list);
152
153 /* sysfs directory and file access */
154 extern void sysfs_close_attribute(struct sysfs_attribute *sysattr);
155 extern struct sysfs_attribute *sysfs_open_attribute(const unsigned char *path);
156 extern int sysfs_read_attribute(struct sysfs_attribute *sysattr);
157 extern int sysfs_read_attribute_value(const unsigned char *attrpath, 
158                                 unsigned char *value, size_t vsize);
159 extern int sysfs_write_attribute(struct sysfs_attribute *sysattr,
160                 const unsigned char *new_value, size_t len);
161 extern unsigned char *sysfs_get_value_from_attributes(struct dlist *attr, 
162                                                 const unsigned char * name);
163 extern void sysfs_close_directory(struct sysfs_directory *sysdir);
164 extern struct sysfs_directory *sysfs_open_directory(const unsigned char *path);
165 extern int sysfs_read_directory(struct sysfs_directory *sysdir);
166 extern int sysfs_read_all_subdirs(struct sysfs_directory *sysdir);
167 extern struct sysfs_directory *sysfs_get_subdirectory
168                         (struct sysfs_directory *dir, unsigned char *subname);
169 extern void sysfs_close_link(struct sysfs_link *ln);
170 extern struct sysfs_link *sysfs_open_link(const unsigned char *lnpath);
171 extern struct sysfs_link *sysfs_get_directory_link(struct sysfs_directory *dir,
172                                                 unsigned char *linkname);
173 extern struct sysfs_link *sysfs_get_subdirectory_link
174                         (struct sysfs_directory *dir, unsigned char *linkname);
175 extern struct sysfs_attribute *sysfs_get_directory_attribute
176                         (struct sysfs_directory *dir, unsigned char *attrname);
177
178 /* sysfs driver access */
179 extern void sysfs_close_driver(struct sysfs_driver *driver);
180 extern struct sysfs_driver *sysfs_open_driver(const unsigned char *path);
181 extern struct sysfs_attribute *sysfs_get_driver_attr
182                 (struct sysfs_driver *drv, const unsigned char *name);
183 extern struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver);
184 extern struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver);
185 extern void sysfs_close_driver_by_name(struct sysfs_driver *driver);
186 extern struct sysfs_driver *sysfs_open_driver_by_name
187         (const unsigned char *drv_name, const unsigned char *bus, size_t bsize);
188 extern int sysfs_write_driver_attr(unsigned char *drv, unsigned char *attrib,
189                                 unsigned char *value, size_t len);
190 extern int sysfs_read_driver_attr(unsigned char *drv, unsigned char *attrib,
191                                 unsigned char *value, size_t len);
192
193 /* generic sysfs device access */
194 extern void sysfs_close_root_device(struct sysfs_root_device *root);
195 extern struct sysfs_root_device *sysfs_open_root_device
196                                                 (const unsigned char *name);
197 extern void sysfs_close_device(struct sysfs_device *dev);
198 extern struct sysfs_device *sysfs_open_device(const unsigned char *path);
199 extern struct sysfs_attribute *sysfs_get_device_attr
200                         (struct sysfs_device *dev, const unsigned char *name);
201 extern struct dlist *sysfs_get_device_attributes(struct sysfs_device *device);
202 extern struct sysfs_device *sysfs_open_device_by_id
203         (const unsigned char *bus_id, const unsigned char *bus, size_t bsize);
204 extern int sysfs_write_device_attr(unsigned char *dev, unsigned char *attrib,
205                                 unsigned char *value, size_t len);
206 extern int sysfs_read_device_attr(unsigned char *dev, unsigned char *attrib,
207                                 unsigned char *value, size_t len);
208
209 /* generic sysfs bus access */
210 extern void sysfs_close_bus(struct sysfs_bus *bus);
211 extern struct sysfs_bus *sysfs_open_bus(const unsigned char *name);
212 extern struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus,
213                                                 unsigned char *id);
214 extern struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus,
215                                                 unsigned char *drvname);
216 extern struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus);
217 extern struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus,
218                                                 unsigned char *attrname);
219 extern struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, 
220                                                         unsigned char *dev_id);
221 extern int sysfs_find_device_bus(const unsigned char *dev_id, 
222                                         unsigned char *busname, size_t bsize);
223 extern int sysfs_find_driver_bus(const unsigned char *driver, 
224                                         unsigned char *busname, size_t bsize);
225
226 /* generic sysfs class access */
227 extern void sysfs_close_class_device(struct sysfs_class_device *dev);
228 extern struct sysfs_class_device *sysfs_open_class_device
229                                         (const unsigned char *path);
230 extern void sysfs_close_class(struct sysfs_class *cls);
231 extern struct sysfs_class *sysfs_open_class(const unsigned char *name);
232 extern struct sysfs_class_device *sysfs_get_class_device
233         (struct sysfs_class *class, unsigned char *name);
234 extern struct sysfs_class_device *sysfs_open_class_device_by_name
235         (const unsigned char *class, unsigned char *name);
236 extern struct dlist *sysfs_get_classdev_attributes
237         (struct sysfs_class_device *cdev);
238 extern int sysfs_find_device_class(const unsigned char *bus_id, 
239                 unsigned char *classname, size_t bsize);
240 extern struct sysfs_attribute *sysfs_get_classdev_attr
241         (struct sysfs_class_device *clsdev, const unsigned char *name);
242 extern int sysfs_write_classdev_attr(unsigned char *dev, unsigned char *attrib, 
243                 unsigned char *value, size_t len);
244 extern int sysfs_read_classdev_attr(unsigned char *dev, unsigned char *attrib, 
245                 unsigned char *value, size_t len);
246
247 #ifdef __cplusplus
248 }
249 #endif
250
251 #endif /* _LIBSYSFS_H_ */