VirtualBox

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

Last change on this file since 69411 was 69105, checked in by vboxsync, 7 years ago

include/iprt/: (C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.1 KB
Line 
1/* $Id: sysfs.h 69105 2017-10-17 10:20:49Z vboxsync $ */
2/** @file
3 * IPRT - Linux sysfs access.
4 */
5
6/*
7 * Copyright (C) 2008-2017 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 * Expects to read the whole file, mind, and will return VERR_BUFFER_OVERFLOW if
135 * that is not possible with the given buffer size.
136 *
137 * @returns IPRT status code.
138 * @param hFile The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
139 * @param pszBuf Where to store the string.
140 * @param cchBuf The size of the buffer. Must be at least 2 bytes.
141 * @param pcchRead Where to store the amount of characters read on success - optional.
142 */
143RTDECL(int) RTLinuxSysFsReadStr(RTFILE hFile, char *pszBuf, size_t cchBuf, size_t *pcchRead);
144
145/**
146 * Writes a string to a file opened with RTLinuxSysFsOpenEx or RTLinuxSysFsOpenExV for writing.
147 *
148 * @returns IPRT status code.
149 * @param hFile The file descriptor returned by RTLinuxSysFsOpenEx or RTLinuxSysFsOpenExV.
150 * @param pszBuf The string to write.
151 * @param cchBuf The length of the string to write - if 0 is given
152 * the string length is determined before writing it including the zero terminator.
153 * @param pcchWritten Where to store the amount of characters written on success - optional.
154 */
155RTDECL(int) RTLinuxSysFsWriteStr(RTFILE hFile, const char *pszBuf, size_t cchBuf, size_t *pcchWritten);
156
157/**
158 * Reads the remainder of a file opened with RTLinuxSysFsOpen or
159 * RTLinuxSysFsOpenV.
160 *
161 * @returns IPRT status code.
162 * @param hFile The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
163 * @param pvBuf Where to store the bits from the file.
164 * @param cbBuf The size of the buffer.
165 * @param pcbRead Where to return the number of bytes read. Optional.
166 */
167RTDECL(int) RTLinuxSysFsReadFile(RTFILE hFile, void *pvBuf, size_t cbBuf, size_t *pcbRead);
168
169/**
170 * Writes the given buffer to a file opened with RTLinuxSysFsOpenEx or
171 * RTLinuxSysFsOpenExV.
172 *
173 * @returns IPRT status code.
174 * @param hFile The file descriptor returned by RTLinuxSysFsOpenEx or RTLinuxSysFsOpenExV.
175 * @param pvBuf The data to write.
176 * @param cbBuf The size of the buffer.
177 * @param pcbWritten Where to return the number of bytes read. Optional.
178 */
179RTDECL(int) RTLinuxSysFsWriteFile(RTFILE hFile, void *pvBuf, size_t cbBuf, size_t *pcbWritten);
180
181/**
182 * Reads a number from a sysfs file.
183 *
184 * @returns IPRT status code.
185 * @param uBase The number base, 0 for autodetect.
186 * @param pi64 Where to store the 64-bit signed on success.
187 * @param pszFormat The filename format, either absolute or relative to "/sys/".
188 * @param va Format args.
189 */
190RTDECL(int) RTLinuxSysFsReadIntFileV(unsigned uBase, int64_t *pi64, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
191
192/**
193 * Reads a number from a sysfs file.
194 *
195 * @returns IPRT status code.
196 * @param uBase The number base, 0 for autodetect.
197 * @param pi64 Where to store the 64-bit signed on success.
198 * @param pszFormat The filename format, either absolute or relative to "/sys/".
199 * @param ... Format args.
200 */
201RTDECL(int) RTLinuxSysFsReadIntFile(unsigned uBase, int64_t *pi64, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
202
203/**
204 * Writes an unsigned 8-bit number to a sysfs file.
205 *
206 * @returns IPRT status code.
207 * @param uBase The base format to write the number. Passing 16 here for
208 * example writes the number as a hexadecimal string with 0x prepended.
209 * @param u8 The number to write.
210 * @param pszFormat The filename format, either absolute or relative to "/sys/".
211 * @param va Format args.
212 */
213RTDECL(int) RTLinuxSysFsWriteU8FileV(unsigned uBase, uint8_t u8, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
214
215/**
216 * Writes an unsigned 8-bit number to a sysfs file.
217 *
218 * @returns IPRT status code.
219 * @param uBase The base format to write the number. Passing 16 here for
220 * example writes the number as a hexadecimal string with 0x prepended.
221 * @param u8 The number to write.
222 * @param pszFormat The filename format, either absolute or relative to "/sys/".
223 * @param ... Format args.
224 */
225RTDECL(int) RTLinuxSysFsWriteU8File(unsigned uBase, uint8_t u8, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
226
227/**
228 * Writes an unsigned 16-bit number to a sysfs file.
229 *
230 * @returns IPRT status code.
231 * @param uBase The base format to write the number. Passing 16 here for
232 * example writes the number as a hexadecimal string with 0x prepended.
233 * @param u16 The number to write.
234 * @param pszFormat The filename format, either absolute or relative to "/sys/".
235 * @param va Format args.
236 */
237RTDECL(int) RTLinuxSysFsWriteU16FileV(unsigned uBase, uint16_t u16, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
238
239/**
240 * Writes an unsigned 16-bit number to a sysfs file.
241 *
242 * @returns IPRT status code.
243 * @param uBase The base format to write the number. Passing 16 here for
244 * example writes the number as a hexadecimal string with 0x prepended.
245 * @param u16 The number to write.
246 * @param pszFormat The filename format, either absolute or relative to "/sys/".
247 * @param ... Format args.
248 */
249RTDECL(int) RTLinuxSysFsWriteU16File(unsigned uBase, uint16_t u16, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
250
251/**
252 * Writes an unsigned 32-bit number to a sysfs file.
253 *
254 * @returns IPRT status code.
255 * @param uBase The base format to write the number. Passing 16 here for
256 * example writes the number as a hexadecimal string with 0x prepended.
257 * @param u32 The number to write.
258 * @param pszFormat The filename format, either absolute or relative to "/sys/".
259 * @param va Format args.
260 */
261RTDECL(int) RTLinuxSysFsWriteU32FileV(unsigned uBase, uint32_t u32, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
262
263/**
264 * Writes an unsigned 8-bit number to a sysfs file.
265 *
266 * @returns IPRT status code.
267 * @param uBase The base format to write the number. Passing 16 here for
268 * example writes the number as a hexadecimal string with 0x prepended.
269 * @param u32 The number to write.
270 * @param pszFormat The filename format, either absolute or relative to "/sys/".
271 * @param ... Format args.
272 */
273RTDECL(int) RTLinuxSysFsWriteU32File(unsigned uBase, uint32_t u32, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
274
275/**
276 * Writes an unsigned 64-bit number to a sysfs file.
277 *
278 * @returns IPRT status code.
279 * @param uBase The base format to write the number. Passing 16 here for
280 * example writes the number as a hexadecimal string with 0x prepended.
281 * @param u64 The number to write.
282 * @param pszFormat The filename format, either absolute or relative to "/sys/".
283 * @param va Format args.
284 */
285RTDECL(int) RTLinuxSysFsWriteU64FileV(unsigned uBase, uint64_t u64, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
286
287/**
288 * Writes an unsigned 8-bit number to a sysfs file.
289 *
290 * @returns IPRT status code.
291 * @param uBase The base format to write the number. Passing 16 here for
292 * example writes the number as a hexadecimal string with 0x prepended.
293 * @param u64 The number to write.
294 * @param pszFormat The filename format, either absolute or relative to "/sys/".
295 * @param ... Format args.
296 */
297RTDECL(int) RTLinuxSysFsWriteU64File(unsigned uBase, uint32_t u64, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
298
299/**
300 * Reads a device number from a sysfs file.
301 *
302 * @returns IPRT status code.
303 * @param pDevNum Where to store the device number on success.
304 * @param pszFormat The filename format, either absolute or relative to "/sys/".
305 * @param va Format args.
306 */
307RTDECL(int) RTLinuxSysFsReadDevNumFileV(dev_t *pDevNum, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0);
308
309/**
310 * Reads a device number from a sysfs file.
311 *
312 * @returns IPRT status code.
313 * @param pDevNum Where to store the device number on success.
314 * @param pszFormat The filename format, either absolute or relative to "/sys/".
315 * @param ... Format args.
316 */
317RTDECL(int) RTLinuxSysFsReadDevNumFile(dev_t *pDevNum, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3);
318
319/**
320 * Reads a string from a sysfs file.
321 *
322 * If the file contains a newline, we only return the text up until there. This
323 * differs from the RTLinuxSysFsReadStr() behaviour.
324 *
325 * @returns IPRT status code.
326 * @param pszBuf Where to store the path element. Must be at least two
327 * characters, but a longer buffer would be advisable.
328 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
329 * @param pcchRead Where to store the amount of characters read on success - optional.
330 * @param pszFormat The filename format, either absolute or relative to "/sys/".
331 * @param va Format args.
332 */
333RTDECL(int) RTLinuxSysFsReadStrFileV(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(4, 0);
334
335/**
336 * Reads a string from a sysfs file. If the file contains a newline, we only
337 * return the text up until there.
338 *
339 * @returns IPRT status code.
340 * @param pszBuf Where to store the path element. Must be at least two
341 * characters, but a longer buffer would be advisable.
342 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
343 * @param pcchRead Where to store the amount of characters read on success - optional.
344 * @param pszFormat The filename format, either absolute or relative to "/sys/".
345 * @param ... Format args.
346 */
347RTDECL(int) RTLinuxSysFsReadStrFile(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(4, 5);
348
349/**
350 * Writes a string to a sysfs file.
351 *
352 * @returns IPRT status code.
353 * @param pszBuf The string to write.
354 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
355 * @param pcchWritten Where to store the amount of characters written on success - optional.
356 * @param pszFormat The filename format, either absolute or relative to "/sys/".
357 * @param va Format args.
358 */
359RTDECL(int) RTLinuxSysFsWriteStrFileV(const char *pszBuf, size_t cchBuf, size_t *pcchWritten, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(4, 0);
360
361/**
362 * Writes a string to a sysfs file.
363 *
364 * @returns IPRT status code.
365 * @param pszBuf The string to write.
366 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
367 * @param pcchWritten Where to store the amount of characters written on success - optional.
368 * @param pszFormat The filename format, either absolute or relative to "/sys/".
369 * @param ... Format args.
370 */
371RTDECL(int) RTLinuxSysFsWriteStrFile(const char *pszBuf, size_t cchBuf, size_t *pcchWritten, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(4, 5);
372
373/**
374 * Reads the last element of the path of the file pointed to by the symbolic
375 * link specified.
376 *
377 * This is needed at least to get the name of the driver associated with a
378 * device, where pszFormat should be the "driver" link in the devices sysfs
379 * directory.
380 *
381 * @returns IPRT status code.
382 * @param pszBuf Where to store the path element. Must be at least two
383 * characters, but a longer buffer would be advisable.
384 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
385 * @param pchBuf Where to store the length of the returned string on success - optional.
386 * @param pszFormat The filename format, either absolute or relative to "/sys/".
387 * @param va Format args.
388 */
389RTDECL(int) RTLinuxSysFsGetLinkDestV(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(4, 0);
390
391/**
392 * Reads the last element of the path of the file pointed to by the symbolic
393 * link specified.
394 *
395 * This is needed at least to get the name of the driver associated with a
396 * device, where pszFormat should be the "driver" link in the devices sysfs
397 * directory.
398 *
399 * @returns IPRT status code.
400 * @param pszBuf Where to store the path element. Must be at least two
401 * characters, but a longer buffer would be advisable.
402 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
403 * @param pchBuf Where to store the length of the returned string on success - optional.
404 * @param pszFormat The filename format, either absolute or relative to "/sys/".
405 * @param ... Format args.
406 */
407RTDECL(int) RTLinuxSysFsGetLinkDest(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(4, 5);
408
409/**
410 * Check the path of a device node under /dev, given the device number and a
411 * pattern and store the path into @a pszBuf.
412 *
413 * @returns IPRT status code.
414 * @retval VERR_FILE_NOT_FOUND if no matching device node could be found.
415 * @param DevNum The device number to search for.
416 * @param fMode The type of device - only RTFS_TYPE_DEV_CHAR and
417 * RTFS_TYPE_DEV_BLOCK are valid values.
418 * @param pszBuf Where to store the path.
419 * @param cchBuf The size of the buffer.
420 * @param pszPattern The expected path format of the device node, either
421 * absolute or relative to "/dev".
422 * @param va Format args.
423 */
424RTDECL(int) RTLinuxCheckDevicePathV(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf,
425 const char *pszPattern, va_list va) RT_IPRT_FORMAT_ATTR(5, 0);
426
427/**
428 * Check the path of a device node under /dev, given the device number and a
429 * pattern and store the path into @a pszBuf.
430 *
431 * @returns IPRT status code.
432 * @retval VERR_FILE_NOT_FOUND if no matching device node could be found.
433 * @param DevNum The device number to search for
434 * @param fMode The type of device - only RTFS_TYPE_DEV_CHAR and
435 * RTFS_TYPE_DEV_BLOCK are valid values
436 * @param pszBuf Where to store the path.
437 * @param cchBuf The size of the buffer.
438 * @param pszPattern The expected path format of the device node, either
439 * absolute or relative to "/dev".
440 * @param ... Format args.
441 */
442RTDECL(int) RTLinuxCheckDevicePath(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf,
443 const char *pszPattern, ...) RT_IPRT_FORMAT_ATTR(5, 6);
444
445/** @} */
446
447RT_C_DECLS_END
448
449#endif
450
Note: See TracBrowser for help on using the repository browser.

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