VirtualBox

source: vbox/trunk/include/iprt/linux/sysfs.h@ 60390

Last change on this file since 60390 was 60390, checked in by vboxsync, 9 years ago

Runtime/linux/sysfs: Correct format string attributes and introduce new methods to write to sysfs files

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.9 KB
Line 
1/* $Id: sysfs.h 60390 2016-04-08 09:17:34Z vboxsync $ */
2/** @file
3 * IPRT - Linux sysfs access.
4 */
5
6/*
7 * Copyright (C) 2008-2016 Oracle Corporation
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
27#ifndef ___iprt_linux_sysfs_h
28#define ___iprt_linux_sysfs_h
29
30#include <iprt/cdefs.h>
31#include <iprt/types.h>
32#include <iprt/stdarg.h>
33
34#include <sys/types.h> /* for dev_t */
35
36RT_C_DECLS_BEGIN
37
38/** @defgroup grp_rt_linux_sysfs RTLinuxSysfs - Linux sysfs
39 * @ingroup grp_rt
40 * @{
41 */
42
43/**
44 * Checks if a sysfs file (or directory, device, symlink, whatever) exists.
45 *
46 * @returns true if the sysfs object exists.
47 * false otherwise or if an error occurred.
48 * @param pszFormat The name format, either absolute or relative to "/sys/".
49 * @param va The format args.
50 */
51RTDECL(bool) RTLinuxSysFsExistsV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
52
53/**
54 * Checks if a sysfs object (directory, device, symlink, whatever) exists.
55 *
56 * @returns IPRT status code.
57 * @retval VINF_SUCCESS if the sysfs object exists.
58 * @retval VERR_FILE_NOT_FOUND if the sysfs object does not exist.
59 * @param pszFormat The name format, either absolute or relative to "/sys/".
60 * @param va The format args.
61 */
62RTDECL(int) RTLinuxSysFsExistsExV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
63
64/**
65 * Checks if a sysfs file (or directory, device, symlink, whatever) exists.
66 *
67 * @returns true if the sysfs object exists.
68 * false otherwise or if an error occurred.
69 * @param pszFormat The name format, either absolute or relative to "/sys/".
70 * @param ... The format args.
71 */
72RTDECL(bool) RTLinuxSysFsExists(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
73
74/**
75 * Checks if a sysfs object (directory, device, symlink, whatever) exists.
76 *
77 * @returns IPRT status code.
78 * @retval VINF_SUCCESS if the sysfs object exists.
79 * @retval VERR_FILE_NOT_FOUND if the sysfs object does not exist.
80 * @param pszFormat The name format, either absolute or relative to "/sys/".
81 * @param ... The format args.
82 */
83RTDECL(int) RTLinuxSysFsExistsEx(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
84
85/**
86 * Opens a sysfs file for reading.
87 *
88 * @returns IPRT status code.
89 * @param phFile Where to store the file handle on success.
90 * @param pszFormat The name format, either absolute or relative to "/sys/".
91 * @param va The format args.
92 *
93 * @note Close the file using RTFileClose().
94 */
95RTDECL(int) RTLinuxSysFsOpenV(PRTFILE phFile, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0);
96
97/**
98 * Opens a sysfs file - extended version.
99 *
100 * @returns IPRT status code.
101 * @param phFile Where to store the file handle on success.
102 * @param fOpen Open flags, see RTFileOpen().
103 * @param pszFormat The name format, either absolute or relative to "/sys/".
104 * @param va The format args.
105 */
106RTDECL(int) RTLinuxSysFsOpenExV(PRTFILE phFile, uint64_t fOpen, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
107
108/**
109 * Opens a sysfs file.
110 *
111 * @returns IPRT status code.
112 * @param phFile Where to store the file handle on success.
113 * @param pszFormat The name format, either absolute or relative to "/sys/".
114 * @param ... The format args.
115 *
116 * @note Close the file using RTFileClose().
117 */
118RTDECL(int) RTLinuxSysFsOpen(PRTFILE phFile, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3);
119
120/**
121 * Opens a sysfs file - extended version.
122 *
123 * @returns IPRT status code.
124 * @param phFile Where to store the file handle on success.
125 * @param fOpen Open flags, see RTFileOpen().
126 * @param pszFormat The name format, either absolute or relative to "/sys/".
127 * @param ... The format args.
128 */
129RTDECL(int) RTLinuxSysFsOpenEx(PRTFILE phFile, uint64_t fOpen, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
130
131/**
132 * Reads a string from a file opened with RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
133 *
134 * @returns IPRT status code.
135 * @param hFile The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
136 * @param pszBuf Where to store the string.
137 * @param cchBuf The size of the buffer. Must be at least 2 bytes.
138 * @param pcchRead Where to store the amount of characters read on success - optional.
139 */
140RTDECL(int) RTLinuxSysFsReadStr(RTFILE hFile, char *pszBuf, size_t cchBuf, size_t *pcchRead);
141
142/**
143 * Writes a string to a file opened with RTLinuxSysFsOpenEx or RTLinuxSysFsOpenExV for writing.
144 *
145 * @returns IPRT status code.
146 * @param hFile The file descriptor returned by RTLinuxSysFsOpenEx or RTLinuxSysFsOpenExV.
147 * @param pszBuf The string to write.
148 * @param cchBuf The string length without zero terminator - if 0 is given
149 * the string length is determined before writing.
150 * @param pcchWritten Where to store the amount of characters written on success - optional.
151 */
152RTDECL(int) RTLinuxSysFsWriteStr(RTFILE hFile, const char *pszBuf, size_t cchBuf, size_t *pcchWritten);
153
154/**
155 * Reads the remainder of a file opened with RTLinuxSysFsOpen or
156 * RTLinuxSysFsOpenV.
157 *
158 * @returns IPRT status code.
159 * @param hFile The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
160 * @param pvBuf Where to store the bits from the file.
161 * @param cbBuf The size of the buffer.
162 * @param pcbRead Where to return the number of bytes read. Optional.
163 */
164RTDECL(int) RTLinuxSysFsReadFile(RTFILE hFile, void *pvBuf, size_t cbBuf, size_t *pcbRead);
165
166/**
167 * Writes the given buffer to a file opened with RTLinuxSysFsOpenEx or
168 * RTLinuxSysFsOpenExV.
169 *
170 * @returns IPRT status code.
171 * @param hFile The file descriptor returned by RTLinuxSysFsOpenEx or RTLinuxSysFsOpenExV.
172 * @param pvBuf The data to write.
173 * @param cbBuf The size of the buffer.
174 * @param pcbWritten Where to return the number of bytes read. Optional.
175 */
176RTDECL(int) RTLinuxSysFsWriteFile(RTFILE hFile, void *pvBuf, size_t cbBuf, size_t *pcbWritten);
177
178/**
179 * Reads a number from a sysfs file.
180 *
181 * @returns IPRT status code.
182 * @param uBase The number base, 0 for autodetect.
183 * @param pi64 Where to store the 64-bit signed on success.
184 * @param pszFormat The filename format, either absolute or relative to "/sys/".
185 * @param va Format args.
186 */
187RTDECL(int) RTLinuxSysFsReadIntFileV(unsigned uBase, int64_t *pi64, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
188
189/**
190 * Reads a number from a sysfs file.
191 *
192 * @returns IPRT status code.
193 * @param uBase The number base, 0 for autodetect.
194 * @param pi64 Where to store the 64-bit signed on success.
195 * @param pszFormat The filename format, either absolute or relative to "/sys/".
196 * @param ... Format args.
197 */
198RTDECL(int) RTLinuxSysFsReadIntFile(unsigned uBase, int64_t *pi64, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
199
200/**
201 * Writes an unsigned 8-bit number to a sysfs file.
202 *
203 * @returns IPRT status code.
204 * @param uBase The base format to write the number. Passing 16 here for
205 * example writes the number as a hexadecimal string with 0x prepended.
206 * @param u8 The number to write.
207 * @param pszFormat The filename format, either absolute or relative to "/sys/".
208 * @param va Format args.
209 */
210RTDECL(int) RTLinuxSysFsWriteU8FileV(unsigned uBase, uint8_t u8, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
211
212/**
213 * Writes an unsigned 8-bit number to a sysfs file.
214 *
215 * @returns IPRT status code.
216 * @param uBase The base format to write the number. Passing 16 here for
217 * example writes the number as a hexadecimal string with 0x prepended.
218 * @param u8 The number to write.
219 * @param pszFormat The filename format, either absolute or relative to "/sys/".
220 * @param ... Format args.
221 */
222RTDECL(int) RTLinuxSysFsWriteU8File(unsigned uBase, uint8_t u8, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
223
224/**
225 * Writes an unsigned 16-bit number to a sysfs file.
226 *
227 * @returns IPRT status code.
228 * @param uBase The base format to write the number. Passing 16 here for
229 * example writes the number as a hexadecimal string with 0x prepended.
230 * @param u16 The number to write.
231 * @param pszFormat The filename format, either absolute or relative to "/sys/".
232 * @param va Format args.
233 */
234RTDECL(int) RTLinuxSysFsWriteU16FileV(unsigned uBase, uint16_t u16, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
235
236/**
237 * Writes an unsigned 16-bit number to a sysfs file.
238 *
239 * @returns IPRT status code.
240 * @param uBase The base format to write the number. Passing 16 here for
241 * example writes the number as a hexadecimal string with 0x prepended.
242 * @param u16 The number to write.
243 * @param pszFormat The filename format, either absolute or relative to "/sys/".
244 * @param ... Format args.
245 */
246RTDECL(int) RTLinuxSysFsWriteU16File(unsigned uBase, uint16_t u16, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
247
248/**
249 * Writes an unsigned 32-bit number to a sysfs file.
250 *
251 * @returns IPRT status code.
252 * @param uBase The base format to write the number. Passing 16 here for
253 * example writes the number as a hexadecimal string with 0x prepended.
254 * @param u32 The number to write.
255 * @param pszFormat The filename format, either absolute or relative to "/sys/".
256 * @param va Format args.
257 */
258RTDECL(int) RTLinuxSysFsWriteU32FileV(unsigned uBase, uint32_t u32, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
259
260/**
261 * Writes an unsigned 8-bit number to a sysfs file.
262 *
263 * @returns IPRT status code.
264 * @param uBase The base format to write the number. Passing 16 here for
265 * example writes the number as a hexadecimal string with 0x prepended.
266 * @param u32 The number to write.
267 * @param pszFormat The filename format, either absolute or relative to "/sys/".
268 * @param ... Format args.
269 */
270RTDECL(int) RTLinuxSysFsWriteU32File(unsigned uBase, uint32_t u32, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
271
272/**
273 * Writes an unsigned 64-bit number to a sysfs file.
274 *
275 * @returns IPRT status code.
276 * @param uBase The base format to write the number. Passing 16 here for
277 * example writes the number as a hexadecimal string with 0x prepended.
278 * @param u64 The number to write.
279 * @param pszFormat The filename format, either absolute or relative to "/sys/".
280 * @param va Format args.
281 */
282RTDECL(int) RTLinuxSysFsWriteU64FileV(unsigned uBase, uint64_t u64, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
283
284/**
285 * Writes an unsigned 8-bit number to a sysfs file.
286 *
287 * @returns IPRT status code.
288 * @param uBase The base format to write the number. Passing 16 here for
289 * example writes the number as a hexadecimal string with 0x prepended.
290 * @param u64 The number to write.
291 * @param pszFormat The filename format, either absolute or relative to "/sys/".
292 * @param ... Format args.
293 */
294RTDECL(int) RTLinuxSysFsWriteU64File(unsigned uBase, uint32_t u64, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
295
296/**
297 * Reads a device number from a sysfs file.
298 *
299 * @returns IPRT status code.
300 * @param pDevNum Where to store the device number on success.
301 * @param pszFormat The filename format, either absolute or relative to "/sys/".
302 * @param va Format args.
303 */
304RTDECL(int) RTLinuxSysFsReadDevNumFileV(dev_t *pDevNum, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0);
305
306/**
307 * Reads a device number from a sysfs file.
308 *
309 * @returns IPRT status code.
310 * @param pDevNum Where to store the device number on success.
311 * @param pszFormat The filename format, either absolute or relative to "/sys/".
312 * @param ... Format args.
313 */
314RTDECL(int) RTLinuxSysFsReadDevNumFile(dev_t *pDevNum, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3);
315
316/**
317 * Reads a string from a sysfs file. If the file contains a newline, we only
318 * return the text up until there.
319 *
320 * @returns IPRT status code.
321 * @param pszBuf Where to store the path element. Must be at least two
322 * characters, but a longer buffer would be advisable.
323 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
324 * @param pcchRead Where to store the amount of characters read on success - optional.
325 * @param pszFormat The filename format, either absolute or relative to "/sys/".
326 * @param va Format args.
327 */
328RTDECL(int) RTLinuxSysFsReadStrFileV(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(4, 0);
329
330/**
331 * Reads a string from a sysfs file. If the file contains a newline, we only
332 * return the text up until there.
333 *
334 * @returns IPRT status code.
335 * @param pszBuf Where to store the path element. Must be at least two
336 * characters, but a longer buffer would be advisable.
337 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
338 * @param pcchRead Where to store the amount of characters read on success - optional.
339 * @param pszFormat The filename format, either absolute or relative to "/sys/".
340 * @param ... Format args.
341 */
342RTDECL(int) RTLinuxSysFsReadStrFile(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(4, 5);
343
344/**
345 * Writes a string to a sysfs file.
346 *
347 * @returns IPRT status code.
348 * @param pszBuf The string to write.
349 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
350 * @param pcchWritten Where to store the amount of characters written on success - optional.
351 * @param pszFormat The filename format, either absolute or relative to "/sys/".
352 * @param va Format args.
353 */
354RTDECL(int) RTLinuxSysFsWriteStrFileV(const char *pszBuf, size_t cchBuf, size_t *pcchWritten, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(4, 0);
355
356/**
357 * Writes a string to a sysfs file.
358 *
359 * @returns IPRT status code.
360 * @param pszBuf The string to write.
361 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
362 * @param pcchWritten Where to store the amount of characters written on success - optional.
363 * @param pszFormat The filename format, either absolute or relative to "/sys/".
364 * @param ... Format args.
365 */
366RTDECL(int) RTLinuxSysFsReadStrFile(char *pszBuf, size_t cchBuf, size_t *pcchWritten, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(4, 5);
367
368/**
369 * Reads the last element of the path of the file pointed to by the symbolic
370 * link specified.
371 *
372 * This is needed at least to get the name of the driver associated with a
373 * device, where pszFormat should be the "driver" link in the devices sysfs
374 * directory.
375 *
376 * @returns IPRT status code.
377 * @param pszBuf Where to store the path element. Must be at least two
378 * characters, but a longer buffer would be advisable.
379 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
380 * @param pchBuf Where to store the length of the returned string on success - optional.
381 * @param pszFormat The filename format, either absolute or relative to "/sys/".
382 * @param va Format args.
383 */
384RTDECL(int) RTLinuxSysFsGetLinkDestV(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(4, 0);
385
386/**
387 * Reads the last element of the path of the file pointed to by the symbolic
388 * link specified.
389 *
390 * This is needed at least to get the name of the driver associated with a
391 * device, where pszFormat should be the "driver" link in the devices sysfs
392 * directory.
393 *
394 * @returns IPRT status code.
395 * @param pszBuf Where to store the path element. Must be at least two
396 * characters, but a longer buffer would be advisable.
397 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
398 * @param pchBuf Where to store the length of the returned string on success - optional.
399 * @param pszFormat The filename format, either absolute or relative to "/sys/".
400 * @param ... Format args.
401 */
402RTDECL(int) RTLinuxSysFsGetLinkDest(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(4, 5);
403
404/**
405 * Check the path of a device node under /dev, given the device number and a
406 * pattern and store the path into @a pszBuf.
407 *
408 * @returns IPRT status code.
409 * @retval VERR_FILE_NOT_FOUND if no matching device node could be found.
410 * @param DevNum The device number to search for.
411 * @param fMode The type of device - only RTFS_TYPE_DEV_CHAR and
412 * RTFS_TYPE_DEV_BLOCK are valid values.
413 * @param pszBuf Where to store the path.
414 * @param cchBuf The size of the buffer.
415 * @param pszPattern The expected path format of the device node, either
416 * absolute or relative to "/dev".
417 * @param va Format args.
418 */
419RTDECL(int) RTLinuxCheckDevicePathV(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf,
420 const char *pszPattern, va_list va) RT_IPRT_FORMAT_ATTR(5, 0);
421
422/**
423 * Check the path of a device node under /dev, given the device number and a
424 * pattern and store the path into @a pszBuf.
425 *
426 * @returns IPRT status code.
427 * @retval VERR_FILE_NOT_FOUND if no matching device node could be found.
428 * @param DevNum The device number to search for
429 * @param fMode The type of device - only RTFS_TYPE_DEV_CHAR and
430 * RTFS_TYPE_DEV_BLOCK are valid values
431 * @param pszBuf Where to store the path.
432 * @param cchBuf The size of the buffer.
433 * @param pszPattern The expected path format of the device node, either
434 * absolute or relative to "/dev".
435 * @param ... Format args.
436 */
437RTDECL(int) RTLinuxCheckDevicePath(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf,
438 const char *pszPattern, ...) RT_IPRT_FORMAT_ATTR(5, 6);
439
440/** @} */
441
442RT_C_DECLS_END
443
444#endif
445
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette