1 | /* $Id: sysfs.h 14711 2008-11-27 15:16:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * sysfs.h - convenience routines for getting values from sysfs on Linux.
|
---|
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 ___VBox_sysfs_h
|
---|
32 | #define ___VBox_sysfs_h
|
---|
33 |
|
---|
34 | #include <iprt/mem.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 |
|
---|
38 | __BEGIN_DECLS
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Read a string value from a sysfs file.
|
---|
42 | * @returns iprt status value.
|
---|
43 | * @returns VERR_BUFFER_OVERFLOW if @a cbBuffer was not sufficient to hold the
|
---|
44 | * value.
|
---|
45 | * @param pszSysfsPath Path to the sysfs directory to be read from.
|
---|
46 | * @param pszFile Name of the file in @a pszSysfsPath to read the value
|
---|
47 | * from.
|
---|
48 | * @param pszBuffer Where to write the data read.
|
---|
49 | * @param cbBuffer The size of the buffer in @pszbuffer.
|
---|
50 | * @param pcbBufActual On success, the size of the data written. On buffer
|
---|
51 | * overflow the size of the buffer needed. Optional.
|
---|
52 | */
|
---|
53 | inline int VBoxSysfsGetString(const char *pszSysfsPath, const char *pszFile,
|
---|
54 | char *pszBuffer, size_t cbBuffer,
|
---|
55 | size_t *pcbBufActual)
|
---|
56 | {
|
---|
57 | PRTSTREAM pStream;
|
---|
58 | size_t cbBufActual;
|
---|
59 | int rc = VINF_SUCCESS;
|
---|
60 |
|
---|
61 | char *pszFullName = NULL;
|
---|
62 | RTMemAutoPtr<char, RTStrFree> FullName;
|
---|
63 | RTStrAPrintf(&pszFullName, "%s/%s", pszSysfsPath, pszFile);
|
---|
64 | FullName = pszFullName;
|
---|
65 | if (!FullName)
|
---|
66 | rc = VERR_NO_MEMORY;
|
---|
67 | if (RT_SUCCESS(rc))
|
---|
68 | rc = RTStrmOpen(pszFullName, "r", &pStream);
|
---|
69 | if (RT_SUCCESS(rc))
|
---|
70 | rc = RTStrmReadEx(pStream, pszBuffer, cbBuffer, &cbBufActual);
|
---|
71 | /* Remove trailing newlines and zero-terminate */
|
---|
72 | for (size_t i = 0; RT_SUCCESS(rc) && i < cbBufActual; ++i)
|
---|
73 | if (pszBuffer[i] == '\n')
|
---|
74 | cbBufActual = i;
|
---|
75 | if (RT_SUCCESS(rc))
|
---|
76 | {
|
---|
77 | if (cbBufActual + 1 > cbBuffer) /* +1 for the '\0' */
|
---|
78 | rc = VERR_BUFFER_OVERFLOW;
|
---|
79 | else
|
---|
80 | pszBuffer[cbBufActual] = '\0';
|
---|
81 | }
|
---|
82 | if (pcbBufActual != NULL && (RT_SUCCESS(rc) || rc == VERR_BUFFER_OVERFLOW))
|
---|
83 | *pcbBufActual = cbBufActual + 1; /* +1 for the '\0' */
|
---|
84 | RTStrmClose(pStream);
|
---|
85 | return rc;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Read a uint32 value from a sysfs file.
|
---|
90 | * @returns iprt status value.
|
---|
91 | * See also return codes from RTStrToUInt32Full().
|
---|
92 | * @param pszSysfsPath Path to the sysfs directory to be read from.
|
---|
93 | * @param pszFile Name of the file in @a pszSysfsPath to read the value
|
---|
94 | * from.
|
---|
95 | * @paramm uBase The base of the representation used.
|
---|
96 | * If this is zero, the function will look for known
|
---|
97 | * prefixes before defaulting to 10.
|
---|
98 | * @param pu32Value Where to store the value read.
|
---|
99 | */
|
---|
100 | inline int VBoxSysfsGetUInt32(const char *pszSysfsPath, const char *pszFile,
|
---|
101 | unsigned uBase, uint32_t *pu32Value)
|
---|
102 | {
|
---|
103 | char szBuffer[256];
|
---|
104 | int rc = VBoxSysfsGetString(pszSysfsPath, pszFile, szBuffer,
|
---|
105 | sizeof(szBuffer), NULL);
|
---|
106 | if (RT_SUCCESS(rc))
|
---|
107 | rc = RTStrToUInt32Full(szBuffer, uBase, pu32Value);
|
---|
108 | return rc;
|
---|
109 | }
|
---|
110 |
|
---|
111 | __END_DECLS
|
---|
112 |
|
---|
113 | #endif
|
---|
114 |
|
---|