chiark / gitweb /
12e7cc5f997832da9ac5f883e11c00c7f8de68f7
[elogind.git] / libsysfs / sysfs / libsysfs.h
1 /*
2  * libsysfs.h
3  *
4  * Header Definitions for libsysfs
5  *
6  * Copyright (C) IBM Corp. 2004-2005
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 #define SYSFS_FSTYPE_NAME       "sysfs"
31 #define SYSFS_PROC_MNTS         "/proc/mounts"
32 #define SYSFS_BUS_NAME          "bus"
33 #define SYSFS_CLASS_NAME        "class"
34 #define SYSFS_BLOCK_NAME        "block"
35 #define SYSFS_DEVICES_NAME      "devices"
36 #define SYSFS_DRIVERS_NAME      "drivers"
37 #define SYSFS_MODULE_NAME       "module"
38 #define SYSFS_NAME_ATTRIBUTE    "name"
39 #define SYSFS_UNKNOWN           "unknown"
40 #define SYSFS_PATH_ENV          "SYSFS_PATH"
41
42 #define SYSFS_PATH_MAX          256
43 #define SYSFS_NAME_LEN          64
44 #define SYSFS_BUS_ID_SIZE       32
45
46 /* mount path for sysfs, can be overridden by exporting SYSFS_PATH */
47 #define SYSFS_MNT_PATH          "/sys"
48
49 enum sysfs_attribute_method {
50         SYSFS_METHOD_SHOW =     0x01,   /* attr can be read by user */
51         SYSFS_METHOD_STORE =    0x02,   /* attr can be changed by user */
52 };
53
54 /*
55  * NOTE: 
56  * 1. We have the statically allocated "name" as the first element of all 
57  * the structures. This feature is used in the "sorter" function for dlists
58  * 2. As is the case with attrlist
59  * 3. As is the case with path
60  */
61 struct sysfs_attribute {
62         char name[SYSFS_NAME_LEN];
63         char path[SYSFS_PATH_MAX];
64         char *value;
65         unsigned short len;                     /* value length */
66         enum sysfs_attribute_method method;     /* show and store */
67 };
68
69 struct sysfs_driver {
70         char name[SYSFS_NAME_LEN];
71         char path[SYSFS_PATH_MAX];
72         struct dlist *attrlist;
73         char bus[SYSFS_NAME_LEN];
74
75         /* Private: for internal use only */
76         struct dlist *devices;
77 };
78
79 struct sysfs_device {
80         char name[SYSFS_NAME_LEN];
81         char path[SYSFS_PATH_MAX];
82         struct dlist *attrlist;
83         char bus_id[SYSFS_NAME_LEN];
84         char bus[SYSFS_NAME_LEN];
85         char driver_name[SYSFS_NAME_LEN];
86
87         /* Private: for internal use only */
88         struct sysfs_device *parent;            
89         /* NOTE - we still don't populate this */
90         struct dlist *children; 
91 };
92
93 /* NOTE: not used as of now */
94 struct sysfs_bus {
95         char name[SYSFS_NAME_LEN];
96         char path[SYSFS_PATH_MAX];
97         struct dlist *attrlist;
98
99         /* Private: for internal use only */
100         struct dlist *drivers;
101         struct dlist *devices;
102 };
103
104 struct sysfs_class_device {
105         char name[SYSFS_NAME_LEN];
106         char path[SYSFS_PATH_MAX];
107         struct dlist *attrlist;
108         char classname[SYSFS_NAME_LEN];
109
110         /* Private: for internal use only */
111         struct sysfs_class_device *parent;      
112         struct sysfs_device *sysdevice;         /* NULL if virtual */
113 };
114
115 /* NOTE: not used as of now */
116 struct sysfs_class {
117         char name[SYSFS_NAME_LEN];
118         char path[SYSFS_PATH_MAX];
119         struct dlist *attrlist;
120
121         /* Private: for internal use only */
122         struct dlist *devices;
123 };
124
125 #ifdef __cplusplus
126 extern "C" {
127 #endif
128
129 /*
130  * Function Prototypes
131  */
132 extern int sysfs_get_mnt_path(char *mnt_path, size_t len);
133 extern int sysfs_remove_trailing_slash(char *path);
134 extern int sysfs_get_name_from_path(const char *path, char *name, size_t len);
135 extern int sysfs_path_is_dir(const char *path);
136 extern int sysfs_path_is_link(const char *path);
137 extern int sysfs_path_is_file(const char *path);
138 extern int sysfs_get_link(const char *path, char *target, size_t len);
139 extern struct dlist *sysfs_open_directory_list(const char *path);
140 extern void sysfs_close_list(struct dlist *list);
141
142 /* sysfs directory and file access */
143 extern void sysfs_close_attribute(struct sysfs_attribute *sysattr);
144 extern struct sysfs_attribute *sysfs_open_attribute(const char *path);
145 extern int sysfs_read_attribute(struct sysfs_attribute *sysattr);
146 extern int sysfs_write_attribute(struct sysfs_attribute *sysattr,
147                 const char *new_value, size_t len);
148
149 /* sysfs driver access */
150 extern void sysfs_close_driver(struct sysfs_driver *driver);
151 extern struct sysfs_driver *sysfs_open_driver
152         (const char *bus_name, const char *drv_name);
153 extern struct sysfs_driver *sysfs_open_driver_path(const char *path);
154 extern struct sysfs_attribute *sysfs_get_driver_attr
155         (struct sysfs_driver *drv, const char *name);
156 extern struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver);
157 extern struct dlist *sysfs_get_driver_devices(struct sysfs_driver *driver);
158
159 /* generic sysfs device access */
160 extern void sysfs_close_device_tree(struct sysfs_device *device);
161 extern struct sysfs_device *sysfs_open_device_tree(const char *path);
162 extern void sysfs_close_device(struct sysfs_device *dev);
163 extern struct sysfs_device *sysfs_open_device
164         (const char *bus, const char *bus_id);
165 extern struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev);
166 extern struct sysfs_device *sysfs_open_device_path(const char *path);
167 extern int sysfs_get_device_bus(struct sysfs_device *dev);
168 extern struct sysfs_attribute *sysfs_get_device_attr
169         (struct sysfs_device *dev, const char *name);
170 extern struct dlist *sysfs_get_device_attributes
171         (struct sysfs_device *dev);
172
173 /* generic sysfs class access */
174 extern void sysfs_close_class_device(struct sysfs_class_device *dev);
175 extern struct sysfs_class_device *sysfs_open_class_device_path
176         (const char *path);
177 extern struct sysfs_class_device *sysfs_open_class_device
178         (const char *classname, const char *name);
179 extern struct sysfs_class_device *sysfs_get_classdev_parent
180         (struct sysfs_class_device *clsdev);
181 extern struct sysfs_attribute *sysfs_get_classdev_attr
182         (struct sysfs_class_device *clsdev, const char *name);
183 extern struct dlist *sysfs_get_classdev_attributes
184         (struct sysfs_class_device *clsdev);
185 extern struct sysfs_device *sysfs_get_classdev_device
186         (struct sysfs_class_device *clsdev);
187 extern void sysfs_close_class(struct sysfs_class *cls);
188 extern struct sysfs_class *sysfs_open_class(const char *name);
189 extern struct sysfs_class_device *sysfs_get_class_device
190         (struct sysfs_class *cls, const char *name);
191 extern struct dlist *sysfs_get_class_devices(struct sysfs_class *cls);
192
193 /* generic sysfs bus access */
194 extern void sysfs_close_bus(struct sysfs_bus *bus);
195 extern struct sysfs_bus *sysfs_open_bus(const char *name);
196 extern struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus);
197 extern struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus);
198 extern struct sysfs_device *sysfs_get_bus_device
199         (struct sysfs_bus *bus, const char *id);
200 extern struct sysfs_driver *sysfs_get_bus_driver
201         (struct sysfs_bus *bus, const char *drvname);
202
203 /**
204  * sort_list: sorter function to keep list elements sorted in alphabetical 
205  *      order. Just does a strncmp as you can see :)
206  *      
207  * Returns 1 if less than 0 otherwise
208  *
209  * NOTE: We take care to have a statically allocated "name" as the first 
210  *      lement of all libsysfs structures. Hence, this function will work 
211  *      AS IS for _ALL_ the lists that have to be sorted.
212  */
213 static inline int sort_list(void *new_elem, void *old_elem)
214 {
215         return ((strncmp(((struct sysfs_attribute *)new_elem)->name,
216                 ((struct sysfs_attribute *)old_elem)->name,
217                 strlen(((struct sysfs_attribute *)new_elem)->name))) < 0 ? 1 : 0);
218 }
219
220
221 #ifdef __cplusplus
222 }
223 #endif
224
225 #endif /* _LIBSYSFS_H_ */