VirtualBox

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

Last change on this file since 77807 was 76585, checked in by vboxsync, 6 years ago

*: scm --fix-header-guard-endif

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