1 | /* $Id: sysfs.h 26163 2010-02-02 18:58:33Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Linux sysfs access.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #ifndef ___iprt_linux_sysfs_h
|
---|
32 | #define ___iprt_linux_sysfs_h
|
---|
33 |
|
---|
34 | #include <iprt/cdefs.h>
|
---|
35 | #include <iprt/types.h>
|
---|
36 | #include <iprt/stdarg.h>
|
---|
37 |
|
---|
38 | #include <sys/types.h> /* for dev_t */
|
---|
39 |
|
---|
40 | RT_C_DECLS_BEGIN
|
---|
41 |
|
---|
42 | /** @defgroup grp_rt_linux_sysfs RTLinuxSysfs - Linux sysfs
|
---|
43 | * @ingroup grp_rt
|
---|
44 | * @{
|
---|
45 | */
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Checks if a sysfs file (or directory, device, symlink, whatever) exists.
|
---|
49 | *
|
---|
50 | * @returns true / false, errno is preserved.
|
---|
51 | * @param pszFormat The name format, either absolute or relative to "/sys/".
|
---|
52 | * @param va The format args.
|
---|
53 | */
|
---|
54 | RTDECL(bool) RTLinuxSysFsExistsV(const char *pszFormat, va_list va);
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Checks if a sysfs file (or directory, device, symlink, whatever) exists.
|
---|
58 | *
|
---|
59 | * @returns true / false, errno is preserved.
|
---|
60 | * @param pszFormat The name format, either absolute or relative to "/sys/".
|
---|
61 | * @param ... The format args.
|
---|
62 | */
|
---|
63 | RTDECL(bool) RTLinuxSysFsExists(const char *pszFormat, ...);
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Opens a sysfs file.
|
---|
67 | *
|
---|
68 | * @returns The file descriptor. -1 and errno on failure.
|
---|
69 | * @param pszFormat The name format, either absolute or relative to "/sys/".
|
---|
70 | * @param va The format args.
|
---|
71 | */
|
---|
72 | RTDECL(int) RTLinuxSysFsOpenV(const char *pszFormat, va_list va);
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Opens a sysfs file.
|
---|
76 | *
|
---|
77 | * @returns The file descriptor. -1 and errno on failure.
|
---|
78 | * @param pszFormat The name format, either absolute or relative to "/sys/".
|
---|
79 | * @param ... The format args.
|
---|
80 | */
|
---|
81 | RTDECL(int) RTLinuxSysFsOpen(const char *pszFormat, ...);
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Closes a file opened with RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
|
---|
85 | *
|
---|
86 | * @param fd File descriptor returned by RTLinuxSysFsOpen or
|
---|
87 | * RTLinuxSysFsOpenV.
|
---|
88 | */
|
---|
89 | RTDECL(void) RTLinuxSysFsClose(int fd);
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Reads a string from a file opened with RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
|
---|
93 | *
|
---|
94 | * @returns The number of bytes read. -1 and errno on failure.
|
---|
95 | * @param fd The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
|
---|
96 | * @param pszBuf Where to store the string.
|
---|
97 | * @param cchBuf The size of the buffer. Must be at least 2 bytes.
|
---|
98 | */
|
---|
99 | RTDECL(ssize_t) RTLinuxSysFsReadStr(int fd, char *pszBuf, size_t cchBuf);
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Reads a number from a sysfs file.
|
---|
103 | *
|
---|
104 | * @returns 64-bit signed value on success, -1 and errno on failure.
|
---|
105 | * @param uBase The number base, 0 for autodetect.
|
---|
106 | * @param pszFormat The filename format, either absolute or relative to "/sys/".
|
---|
107 | * @param va Format args.
|
---|
108 | */
|
---|
109 | RTDECL(int64_t) RTLinuxSysFsReadIntFileV(unsigned uBase, const char *pszFormat, va_list va);
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Reads a number from a sysfs file.
|
---|
113 | *
|
---|
114 | * @returns 64-bit signed value on success, -1 and errno on failure.
|
---|
115 | * @param uBase The number base, 0 for autodetect.
|
---|
116 | * @param pszFormat The filename format, either absolute or relative to "/sys/".
|
---|
117 | * @param ... Format args.
|
---|
118 | */
|
---|
119 | RTDECL(int64_t) RTLinuxSysFsReadIntFile(unsigned uBase, const char *pszFormat, ...);
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Reads a device number from a sysfs file.
|
---|
123 | *
|
---|
124 | * @returns device number on success, 0 and errno on failure.
|
---|
125 | * @param pszFormat The filename format, either absolute or relative to "/sys/".
|
---|
126 | * @param va Format args.
|
---|
127 | */
|
---|
128 | RTDECL(dev_t) RTLinuxSysFsReadDevNumFileV(const char *pszFormat, va_list va);
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Reads a device number from a sysfs file.
|
---|
132 | *
|
---|
133 | * @returns device number on success, 0 and errno on failure.
|
---|
134 | * @param pszFormat The filename format, either absolute or relative to "/sys/".
|
---|
135 | * @param ... Format args.
|
---|
136 | */
|
---|
137 | RTDECL(dev_t) RTLinuxSysFsReadDevNumFile(const char *pszFormat, ...);
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Reads a string from a sysfs file. If the file contains a newline, we only
|
---|
141 | * return the text up until there.
|
---|
142 | *
|
---|
143 | * @returns number of characters read on success, -1 and errno on failure.
|
---|
144 | * @param pszBuf Where to store the path element. Must be at least two
|
---|
145 | * characters, but a longer buffer would be advisable.
|
---|
146 | * @param cchBuf The size of the buffer pointed to by @a pszBuf.
|
---|
147 | * @param pszFormat The filename format, either absolute or relative to "/sys/".
|
---|
148 | * @param va Format args.
|
---|
149 | */
|
---|
150 | RTDECL(ssize_t) RTLinuxSysFsReadStrFileV(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va);
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Reads a string from a sysfs file. If the file contains a newline, we only
|
---|
154 | * return the text up until there.
|
---|
155 | *
|
---|
156 | * @returns number of characters read on success, -1 and errno on failure.
|
---|
157 | * @param pszBuf Where to store the path element. Must be at least two
|
---|
158 | * characters, but a longer buffer would be advisable.
|
---|
159 | * @param cchBuf The size of the buffer pointed to by @a pszBuf.
|
---|
160 | * @param pszFormat The filename format, either absolute or relative to "/sys/".
|
---|
161 | * @param ... Format args.
|
---|
162 | */
|
---|
163 | RTDECL(ssize_t) RTLinuxSysFsReadStrFile(char *pszBuf, size_t cchBuf, const char *pszFormat, ...);
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Reads the last element of the path of the file pointed to by the symbolic
|
---|
167 | * link specified.
|
---|
168 | *
|
---|
169 | * This is needed at least to get the name of the driver associated with a
|
---|
170 | * device, where pszFormat should be the "driver" link in the devices sysfs
|
---|
171 | * directory.
|
---|
172 | *
|
---|
173 | * @returns The length of the returned string on success, -1 and errno on
|
---|
174 | * failure.
|
---|
175 | * @param pszBuf Where to store the path element. Must be at least two
|
---|
176 | * characters, but a longer buffer would be advisable.
|
---|
177 | * @param cchBuf The size of the buffer pointed to by @a pszBuf.
|
---|
178 | * @param pszFormat The filename format, either absolute or relative to "/sys/".
|
---|
179 | * @param va Format args.
|
---|
180 | */
|
---|
181 | RTDECL(ssize_t) RTLinuxSysFsGetLinkDestV(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va);
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Reads the last element of the path of the file pointed to by the symbolic
|
---|
185 | * link specified.
|
---|
186 | *
|
---|
187 | * This is needed at least to get the name of the driver associated with a
|
---|
188 | * device, where pszFormat should be the "driver" link in the devices sysfs
|
---|
189 | * directory.
|
---|
190 | *
|
---|
191 | * @returns The length of the returned string on success, -1 and errno on
|
---|
192 | * failure.
|
---|
193 | * @param pszBuf Where to store the path element. Must be at least two
|
---|
194 | * characters, but a longer buffer would be advisable.
|
---|
195 | * @param cchBuf The size of the buffer pointed to by @a pszBuf.
|
---|
196 | * @param pszFormat The filename format, either absolute or relative to "/sys/".
|
---|
197 | * @param ... Format args.
|
---|
198 | */
|
---|
199 | RTDECL(ssize_t) RTLinuxSysFsGetLinkDest(char *pszBuf, size_t cchBuf, const char *pszFormat, ...);
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Find the path of a device node under /dev, given then device number.
|
---|
203 | *
|
---|
204 | * This function will recursively search under /dev until it finds a device node
|
---|
205 | * matching @a devnum, and store the path into @a pszBuf. The caller may
|
---|
206 | * provide an expected path in pszSuggestion, which will be tried before
|
---|
207 | * searching, but due to the variance in Linux systems it can be hard to always
|
---|
208 | * correctly predict the path.
|
---|
209 | *
|
---|
210 | * @returns The length of the returned string on success, -1 and errno on
|
---|
211 | * failure.
|
---|
212 | * @returns -1 and ENOENT if no matching device node could be found.
|
---|
213 | * @param DevNum The device number to search for.
|
---|
214 | * @param fMode The type of device - only RTFS_TYPE_DEV_CHAR and
|
---|
215 | * RTFS_TYPE_DEV_BLOCK are valid values.
|
---|
216 | * @param pszBuf Where to store the path.
|
---|
217 | * @param cchBuf The size of the buffer.
|
---|
218 | * @param pszSuggestion The expected path format of the device node, either
|
---|
219 | * absolute or relative to "/dev". (Optional)
|
---|
220 | * @param va Format args.
|
---|
221 | */
|
---|
222 | RTDECL(ssize_t) RTLinuxFindDevicePathV(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf,
|
---|
223 | const char *pszSuggestion, va_list va);
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Find the path of a device node under /dev, given the device number.
|
---|
227 | *
|
---|
228 | * This function will recursively search under /dev until it finds a device node
|
---|
229 | * matching @a devnum, and store the path into @a pszBuf. The caller may
|
---|
230 | * provide an expected path in pszSuggestion, which will be tried before
|
---|
231 | * searching, but due to the variance in Linux systems it can be hard to always
|
---|
232 | * correctly predict the path.
|
---|
233 | *
|
---|
234 | * @returns The length of the returned string on success, -1 and errno on
|
---|
235 | * failure.
|
---|
236 | * @returns -1 and ENOENT if no matching device node could be found.
|
---|
237 | * @param DevNum The device number to search for
|
---|
238 | * @param fMode The type of device - only RTFS_TYPE_DEV_CHAR and
|
---|
239 | * RTFS_TYPE_DEV_BLOCK are valid values
|
---|
240 | * @param pszBuf Where to store the path.
|
---|
241 | * @param cchBuf The size of the buffer.
|
---|
242 | * @param pszSuggestion The expected path format of the device node, either
|
---|
243 | * absolute or relative to "/dev". (Optional)
|
---|
244 | * @param ... Format args.
|
---|
245 | */
|
---|
246 | RTDECL(ssize_t) RTLinuxFindDevicePath(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf,
|
---|
247 | const char *pszSuggestion, ...);
|
---|
248 |
|
---|
249 | /** @} */
|
---|
250 |
|
---|
251 | RT_C_DECLS_END
|
---|
252 |
|
---|
253 | #endif
|
---|
254 |
|
---|