VirtualBox

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

Last change on this file since 102342 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.5 KB
Line 
1/* $Id: sysfs.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Linux sysfs access.
4 */
5
6/*
7 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37#ifndef IPRT_INCLUDED_linux_sysfs_h
38#define IPRT_INCLUDED_linux_sysfs_h
39#ifndef RT_WITHOUT_PRAGMA_ONCE
40# pragma once
41#endif
42
43#include <iprt/cdefs.h>
44#include <iprt/types.h>
45#include <iprt/stdarg.h>
46
47#include <sys/types.h> /* for dev_t */
48
49RT_C_DECLS_BEGIN
50
51/** @defgroup grp_rt_linux_sysfs RTLinuxSysfs - Linux sysfs
52 * @ingroup grp_rt
53 * @{
54 */
55
56/**
57 * Checks if a sysfs file (or directory, device, symlink, whatever) exists.
58 *
59 * @returns true if the sysfs object exists.
60 * false otherwise or if an error occurred.
61 * @param pszFormat The name format, either absolute or relative to "/sys/".
62 * @param va The format args.
63 */
64RTDECL(bool) RTLinuxSysFsExistsV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
65
66/**
67 * Checks if a sysfs object (directory, device, symlink, whatever) exists.
68 *
69 * @returns IPRT status code.
70 * @retval VINF_SUCCESS if the sysfs object exists.
71 * @retval VERR_FILE_NOT_FOUND if the sysfs object does not exist.
72 * @param pszFormat The name format, either absolute or relative to "/sys/".
73 * @param va The format args.
74 */
75RTDECL(int) RTLinuxSysFsExistsExV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
76
77/**
78 * Checks if a sysfs file (or directory, device, symlink, whatever) exists.
79 *
80 * @returns true if the sysfs object exists.
81 * false otherwise or if an error occurred.
82 * @param pszFormat The name format, either absolute or relative to "/sys/".
83 * @param ... The format args.
84 */
85RTDECL(bool) RTLinuxSysFsExists(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
86
87/**
88 * Checks if a sysfs object (directory, device, symlink, whatever) exists.
89 *
90 * @returns IPRT status code.
91 * @retval VINF_SUCCESS if the sysfs object exists.
92 * @retval VERR_FILE_NOT_FOUND if the sysfs object does not exist.
93 * @param pszFormat The name format, either absolute or relative to "/sys/".
94 * @param ... The format args.
95 */
96RTDECL(int) RTLinuxSysFsExistsEx(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
97
98/**
99 * Opens a sysfs file for reading.
100 *
101 * @returns IPRT status code.
102 * @param phFile Where to store the file handle on success.
103 * @param pszFormat The name format, either absolute or relative to "/sys/".
104 * @param va The format args.
105 *
106 * @note Close the file using RTFileClose().
107 */
108RTDECL(int) RTLinuxSysFsOpenV(PRTFILE phFile, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0);
109
110/**
111 * Opens a sysfs file - extended version.
112 *
113 * @returns IPRT status code.
114 * @param phFile Where to store the file handle on success.
115 * @param fOpen Open flags, see RTFileOpen().
116 * @param pszFormat The name format, either absolute or relative to "/sys/".
117 * @param va The format args.
118 */
119RTDECL(int) RTLinuxSysFsOpenExV(PRTFILE phFile, uint64_t fOpen, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
120
121/**
122 * Opens a sysfs file.
123 *
124 * @returns IPRT status code.
125 * @param phFile Where to store the file handle on success.
126 * @param pszFormat The name format, either absolute or relative to "/sys/".
127 * @param ... The format args.
128 *
129 * @note Close the file using RTFileClose().
130 */
131RTDECL(int) RTLinuxSysFsOpen(PRTFILE phFile, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3);
132
133/**
134 * Opens a sysfs file - extended version.
135 *
136 * @returns IPRT status code.
137 * @param phFile Where to store the file handle on success.
138 * @param fOpen Open flags, see RTFileOpen().
139 * @param pszFormat The name format, either absolute or relative to "/sys/".
140 * @param ... The format args.
141 */
142RTDECL(int) RTLinuxSysFsOpenEx(PRTFILE phFile, uint64_t fOpen, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
143
144/**
145 * Reads a string from a file opened with RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
146 *
147 * Expects to read the whole file, mind, and will return VERR_BUFFER_OVERFLOW if
148 * that is not possible with the given buffer size.
149 *
150 * @returns IPRT status code.
151 * @param hFile The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
152 * @param pszBuf Where to store the string.
153 * @param cchBuf The size of the buffer. Must be at least 2 bytes.
154 * @param pcchRead Where to store the amount of characters read on success - optional.
155 */
156RTDECL(int) RTLinuxSysFsReadStr(RTFILE hFile, char *pszBuf, size_t cchBuf, size_t *pcchRead);
157
158/**
159 * Writes a string to a file opened with RTLinuxSysFsOpenEx or RTLinuxSysFsOpenExV for writing.
160 *
161 * @returns IPRT status code.
162 * @param hFile The file descriptor returned by RTLinuxSysFsOpenEx or RTLinuxSysFsOpenExV.
163 * @param pszBuf The string to write.
164 * @param cchBuf The length of the string to write - if 0 is given
165 * the string length is determined before writing it including the zero terminator.
166 * @param pcchWritten Where to store the amount of characters written on success - optional.
167 */
168RTDECL(int) RTLinuxSysFsWriteStr(RTFILE hFile, const char *pszBuf, size_t cchBuf, size_t *pcchWritten);
169
170/**
171 * Reads the remainder of a file opened with RTLinuxSysFsOpen or
172 * RTLinuxSysFsOpenV.
173 *
174 * @returns IPRT status code.
175 * @param hFile The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
176 * @param pvBuf Where to store the bits from the file.
177 * @param cbBuf The size of the buffer.
178 * @param pcbRead Where to return the number of bytes read. Optional.
179 */
180RTDECL(int) RTLinuxSysFsReadFile(RTFILE hFile, void *pvBuf, size_t cbBuf, size_t *pcbRead);
181
182/**
183 * Writes the given buffer to a file opened with RTLinuxSysFsOpenEx or
184 * RTLinuxSysFsOpenExV.
185 *
186 * @returns IPRT status code.
187 * @param hFile The file descriptor returned by RTLinuxSysFsOpenEx or RTLinuxSysFsOpenExV.
188 * @param pvBuf The data to write.
189 * @param cbBuf The size of the buffer.
190 * @param pcbWritten Where to return the number of bytes read. Optional.
191 */
192RTDECL(int) RTLinuxSysFsWriteFile(RTFILE hFile, void *pvBuf, size_t cbBuf, size_t *pcbWritten);
193
194/**
195 * Reads a number from a sysfs file.
196 *
197 * @returns IPRT status code.
198 * @param uBase The number base, 0 for autodetect.
199 * @param pi64 Where to store the 64-bit signed on success.
200 * @param pszFormat The filename format, either absolute or relative to "/sys/".
201 * @param va Format args.
202 */
203RTDECL(int) RTLinuxSysFsReadIntFileV(unsigned uBase, int64_t *pi64, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
204
205/**
206 * Reads a number from a sysfs file.
207 *
208 * @returns IPRT status code.
209 * @param uBase The number base, 0 for autodetect.
210 * @param pi64 Where to store the 64-bit signed on success.
211 * @param pszFormat The filename format, either absolute or relative to "/sys/".
212 * @param ... Format args.
213 */
214RTDECL(int) RTLinuxSysFsReadIntFile(unsigned uBase, int64_t *pi64, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
215
216/**
217 * Writes an unsigned 8-bit number to a sysfs file.
218 *
219 * @returns IPRT status code.
220 * @param uBase The base format to write the number. Passing 16 here for
221 * example writes the number as a hexadecimal string with 0x prepended.
222 * @param u8 The number to write.
223 * @param pszFormat The filename format, either absolute or relative to "/sys/".
224 * @param va Format args.
225 */
226RTDECL(int) RTLinuxSysFsWriteU8FileV(unsigned uBase, uint8_t u8, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
227
228/**
229 * Writes an unsigned 8-bit number to a sysfs file.
230 *
231 * @returns IPRT status code.
232 * @param uBase The base format to write the number. Passing 16 here for
233 * example writes the number as a hexadecimal string with 0x prepended.
234 * @param u8 The number to write.
235 * @param pszFormat The filename format, either absolute or relative to "/sys/".
236 * @param ... Format args.
237 */
238RTDECL(int) RTLinuxSysFsWriteU8File(unsigned uBase, uint8_t u8, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
239
240/**
241 * Writes an unsigned 16-bit number to a sysfs file.
242 *
243 * @returns IPRT status code.
244 * @param uBase The base format to write the number. Passing 16 here for
245 * example writes the number as a hexadecimal string with 0x prepended.
246 * @param u16 The number to write.
247 * @param pszFormat The filename format, either absolute or relative to "/sys/".
248 * @param va Format args.
249 */
250RTDECL(int) RTLinuxSysFsWriteU16FileV(unsigned uBase, uint16_t u16, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
251
252/**
253 * Writes an unsigned 16-bit number to a sysfs file.
254 *
255 * @returns IPRT status code.
256 * @param uBase The base format to write the number. Passing 16 here for
257 * example writes the number as a hexadecimal string with 0x prepended.
258 * @param u16 The number to write.
259 * @param pszFormat The filename format, either absolute or relative to "/sys/".
260 * @param ... Format args.
261 */
262RTDECL(int) RTLinuxSysFsWriteU16File(unsigned uBase, uint16_t u16, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
263
264/**
265 * Writes an unsigned 32-bit number to a sysfs file.
266 *
267 * @returns IPRT status code.
268 * @param uBase The base format to write the number. Passing 16 here for
269 * example writes the number as a hexadecimal string with 0x prepended.
270 * @param u32 The number to write.
271 * @param pszFormat The filename format, either absolute or relative to "/sys/".
272 * @param va Format args.
273 */
274RTDECL(int) RTLinuxSysFsWriteU32FileV(unsigned uBase, uint32_t u32, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
275
276/**
277 * Writes an unsigned 8-bit number to a sysfs file.
278 *
279 * @returns IPRT status code.
280 * @param uBase The base format to write the number. Passing 16 here for
281 * example writes the number as a hexadecimal string with 0x prepended.
282 * @param u32 The number to write.
283 * @param pszFormat The filename format, either absolute or relative to "/sys/".
284 * @param ... Format args.
285 */
286RTDECL(int) RTLinuxSysFsWriteU32File(unsigned uBase, uint32_t u32, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
287
288/**
289 * Writes an unsigned 64-bit number to a sysfs file.
290 *
291 * @returns IPRT status code.
292 * @param uBase The base format to write the number. Passing 16 here for
293 * example writes the number as a hexadecimal string with 0x prepended.
294 * @param u64 The number to write.
295 * @param pszFormat The filename format, either absolute or relative to "/sys/".
296 * @param va Format args.
297 */
298RTDECL(int) RTLinuxSysFsWriteU64FileV(unsigned uBase, uint64_t u64, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
299
300/**
301 * Writes an unsigned 8-bit number to a sysfs file.
302 *
303 * @returns IPRT status code.
304 * @param uBase The base format to write the number. Passing 16 here for
305 * example writes the number as a hexadecimal string with 0x prepended.
306 * @param u64 The number to write.
307 * @param pszFormat The filename format, either absolute or relative to "/sys/".
308 * @param ... Format args.
309 */
310RTDECL(int) RTLinuxSysFsWriteU64File(unsigned uBase, uint32_t u64, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
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 va Format args.
319 */
320RTDECL(int) RTLinuxSysFsReadDevNumFileV(dev_t *pDevNum, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0);
321
322/**
323 * Reads a device number from a sysfs file.
324 *
325 * @returns IPRT status code.
326 * @param pDevNum Where to store the device number on success.
327 * @param pszFormat The filename format, either absolute or relative to "/sys/".
328 * @param ... Format args.
329 */
330RTDECL(int) RTLinuxSysFsReadDevNumFile(dev_t *pDevNum, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3);
331
332/**
333 * Reads a string from a sysfs file.
334 *
335 * If the file contains a newline, we only return the text up until there. This
336 * differs from the RTLinuxSysFsReadStr() behaviour.
337 *
338 * @returns IPRT status code.
339 * @param pszBuf Where to store the path element. Must be at least two
340 * characters, but a longer buffer would be advisable.
341 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
342 * @param pcchRead Where to store the amount of characters read on success - optional.
343 * @param pszFormat The filename format, either absolute or relative to "/sys/".
344 * @param va Format args.
345 */
346RTDECL(int) RTLinuxSysFsReadStrFileV(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(4, 0);
347
348/**
349 * Reads a string from a sysfs file. If the file contains a newline, we only
350 * return the text up until there.
351 *
352 * @returns IPRT status code.
353 * @param pszBuf Where to store the path element. Must be at least two
354 * characters, but a longer buffer would be advisable.
355 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
356 * @param pcchRead Where to store the amount of characters read on success - optional.
357 * @param pszFormat The filename format, either absolute or relative to "/sys/".
358 * @param ... Format args.
359 */
360RTDECL(int) RTLinuxSysFsReadStrFile(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(4, 5);
361
362/**
363 * Writes a string to a sysfs file.
364 *
365 * @returns IPRT status code.
366 * @param pszBuf The string to write.
367 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
368 * @param pcchWritten Where to store the amount of characters written on success - optional.
369 * @param pszFormat The filename format, either absolute or relative to "/sys/".
370 * @param va Format args.
371 */
372RTDECL(int) RTLinuxSysFsWriteStrFileV(const char *pszBuf, size_t cchBuf, size_t *pcchWritten, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(4, 0);
373
374/**
375 * Writes a string to a sysfs file.
376 *
377 * @returns IPRT status code.
378 * @param pszBuf The string to write.
379 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
380 * @param pcchWritten Where to store the amount of characters written on success - optional.
381 * @param pszFormat The filename format, either absolute or relative to "/sys/".
382 * @param ... Format args.
383 */
384RTDECL(int) RTLinuxSysFsWriteStrFile(const char *pszBuf, size_t cchBuf, size_t *pcchWritten, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(4, 5);
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 va Format args.
401 */
402RTDECL(int) RTLinuxSysFsGetLinkDestV(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(4, 0);
403
404/**
405 * Reads the last element of the path of the file pointed to by the symbolic
406 * link specified.
407 *
408 * This is needed at least to get the name of the driver associated with a
409 * device, where pszFormat should be the "driver" link in the devices sysfs
410 * directory.
411 *
412 * @returns IPRT status code.
413 * @param pszBuf Where to store the path element. Must be at least two
414 * characters, but a longer buffer would be advisable.
415 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
416 * @param pchBuf Where to store the length of the returned string on success - optional.
417 * @param pszFormat The filename format, either absolute or relative to "/sys/".
418 * @param ... Format args.
419 */
420RTDECL(int) RTLinuxSysFsGetLinkDest(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(4, 5);
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 va Format args.
436 */
437RTDECL(int) RTLinuxCheckDevicePathV(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf,
438 const char *pszPattern, va_list va) RT_IPRT_FORMAT_ATTR(5, 0);
439
440/**
441 * Check the path of a device node under /dev, given the device number and a
442 * pattern and store the path into @a pszBuf.
443 *
444 * @returns IPRT status code.
445 * @retval VERR_FILE_NOT_FOUND if no matching device node could be found.
446 * @param DevNum The device number to search for
447 * @param fMode The type of device - only RTFS_TYPE_DEV_CHAR and
448 * RTFS_TYPE_DEV_BLOCK are valid values
449 * @param pszBuf Where to store the path.
450 * @param cchBuf The size of the buffer.
451 * @param pszPattern The expected path format of the device node, either
452 * absolute or relative to "/dev".
453 * @param ... Format args.
454 */
455RTDECL(int) RTLinuxCheckDevicePath(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf,
456 const char *pszPattern, ...) RT_IPRT_FORMAT_ATTR(5, 6);
457
458/**
459 * Constructs the path of a sysfs file from the format parameters passed,
460 * prepending "/sys/" if the path is relative.
461 *
462 * @returns IPRT status code.
463 * @param pszPath Where to write the path.
464 * @param cbPath The size of the buffer pointed to by @a pszPath.
465 * @param pszFormat The name format, either absolute or relative to "/sys/".
466 * @param va The format args.
467 */
468RTDECL(int) RTLinuxConstructPathV(char *pszPath, size_t cbPath, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
469
470/**
471 * Constructs the path of a sysfs file from the format parameters passed,
472 * prepending "/sys/" if the path is relative.
473 *
474 * @returns IPRT status code.
475 * @param pszPath Where to write the path.
476 * @param cbPath The size of the buffer pointed to by @a pszPath.
477 * @param pszFormat The name format, either absolute or relative to "/sys/".
478 * @param ... The format args.
479 */
480RTDECL(int) RTLinuxConstructPath(char *pszPath, size_t cbPath, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
481
482/** @} */
483
484RT_C_DECLS_END
485
486#endif /* !IPRT_INCLUDED_linux_sysfs_h */
487
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