VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/linux/krnlmod-linux.cpp@ 69536

Last change on this file since 69536 was 67286, checked in by vboxsync, 8 years ago

Runtime/RTKrnlMod: Started working on Linux implementation gathering the information from /sys/module

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
1/* $Id: krnlmod-linux.cpp 67286 2017-06-07 20:35:49Z vboxsync $ */
2/** @file
3 * IPRT - Kernel module, Linux.
4 */
5
6/*
7 * Copyright (C) 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
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_SYSTEM
32#include <iprt/krnlmod.h>
33#include <iprt/linux/sysfs.h>
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/dir.h>
37#include <iprt/err.h>
38#include <iprt/mem.h>
39#include <iprt/string.h>
40#include <iprt/types.h>
41
42
43/**
44 * Internal kernel information record state.
45 */
46typedef struct RTKRNLMODINFOINT
47{
48 /** Reference counter. */
49 volatile uint32_t cRefs;
50 /** Reference count for the kernel module. */
51 uint32_t cRefKrnlMod;
52 /** Load address of the kernel module. */
53 RTR0UINTPTR uLoadAddr;
54 /** Size of the kernel module. */
55 size_t cbKrnlMod;
56 /** Size of the name in characters including the zero terminator. */
57 size_t cchName;
58 /** Module name - variable in size. */
59 char achName[1];
60} RTKRNLMODINFOINT;
61/** Pointer to the internal kernel module information record. */
62typedef RTKRNLMODINFOINT *PRTKRNLMODINFOINT;
63/** Pointer to a const internal kernel module information record. */
64typedef const RTKRNLMODINFOINT *PCRTKRNLMODINFOINT;
65
66
67
68/**
69 * Destroy the given kernel module information record.
70 *
71 * @returns nothing.
72 * @param pThis The record to destroy.
73 */
74static void rtKrnlModInfoDestroy(PRTKRNLMODINFOINT pThis)
75{
76 RTMemFree(pThis);
77}
78
79
80static int rtKrnlModLinuxReadIntFileDef(unsigned uBase, int64_t *pi64, int64_t i64Def,
81 const char *pszName, const char *pszPath)
82{
83 int rc = RTLinuxSysFsReadIntFile(uBase, pi64, "module/%s/%s", pszName, pszPath);
84 if (rc == VERR_FILE_NOT_FOUND)
85 {
86 *pi64 = i64Def;
87 rc = VINF_SUCCESS;
88 }
89
90 return rc;
91}
92
93/**
94 * Creates a new kernel module information record for the given module.
95 *
96 * @returns IPRT status code.
97 * @param pszName The kernel module name.
98 * @param phKrnlModInfo Where to store the handle to the kernel module information record
99 * on success.
100 */
101static int rtKrnlModLinuxInfoCreate(const char *pszName, PRTKRNLMODINFO phKrnlModInfo)
102{
103 int rc = VINF_SUCCESS;
104 size_t cchName = strlen(pszName) + 1;
105 PRTKRNLMODINFOINT pThis = (PRTKRNLMODINFOINT)RTMemAllocZ(RT_OFFSETOF(RTKRNLMODINFOINT, achName[cchName]));
106 if (RT_LIKELY(pThis))
107 {
108 memcpy(&pThis->achName[0], pszName, cchName);
109 pThis->cchName = cchName;
110 pThis->cRefs = 1;
111
112 int64_t iTmp = 0;
113 rc = rtKrnlModLinuxReadIntFileDef(10, &iTmp, 0, pszName, "refcnt");
114 if (RT_SUCCESS(rc))
115 pThis->cRefKrnlMod = (uint32_t)iTmp;
116
117 rc = rtKrnlModLinuxReadIntFileDef(10, &iTmp, 0, pszName, "coresize");
118 if (RT_SUCCESS(rc))
119 pThis->cbKrnlMod = iTmp;
120
121 rc = rtKrnlModLinuxReadIntFileDef(16, &iTmp, 0, pszName, "sections/.text");
122 if (RT_SUCCESS(rc))
123 pThis->uLoadAddr = iTmp;
124
125 if (RT_SUCCESS(rc))
126 *phKrnlModInfo = pThis;
127 else
128 RTMemFree(pThis);
129 }
130 else
131 rc = VERR_NO_MEMORY;
132
133 return rc;
134}
135
136
137RTDECL(int) RTKrnlModQueryLoaded(const char *pszName, bool *pfLoaded)
138{
139 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
140 AssertPtrReturn(pfLoaded, VERR_INVALID_POINTER);
141
142 int rc = RTLinuxSysFsExists("module/%s", pszName);
143 if (rc == VINF_SUCCESS)
144 *pfLoaded = true;
145 else if (rc == VERR_FILE_NOT_FOUND)
146 {
147 *pfLoaded = false;
148 rc = VINF_SUCCESS;
149 }
150
151 return rc;
152}
153
154
155RTDECL(int) RTKrnlModLoadedQueryInfo(const char *pszName, PRTKRNLMODINFO phKrnlModInfo)
156{
157 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
158 AssertPtrReturn(phKrnlModInfo, VERR_INVALID_POINTER);
159
160 int rc = RTLinuxSysFsExists("module/%s", pszName);
161 if (rc == VINF_SUCCESS)
162 rc = rtKrnlModLinuxInfoCreate(pszName, phKrnlModInfo);
163 else if (rc == VERR_FILE_NOT_FOUND)
164 rc = VERR_NOT_FOUND;
165
166 return rc;
167}
168
169
170RTDECL(uint32_t) RTKrnlModLoadedGetCount(void)
171{
172 uint32_t cKmodsLoaded = 0;
173
174 PRTDIR pDir = NULL;
175 int rc = RTDirOpen(&pDir, "/sys/module");
176 if (RT_SUCCESS(rc))
177 {
178 RTDIRENTRY DirEnt;
179 rc = RTDirRead(pDir, &DirEnt, NULL);
180 while (RT_SUCCESS(rc))
181 {
182 if ( RTStrCmp(DirEnt.szName, ".")
183 && RTStrCmp(DirEnt.szName, ".."))
184 cKmodsLoaded++;
185 rc = RTDirRead(pDir, &DirEnt, NULL);
186 }
187
188 RTDirClose(pDir);
189 }
190
191
192 return cKmodsLoaded;
193}
194
195
196RTDECL(int) RTKrnlModLoadedQueryInfoAll(PRTKRNLMODINFO pahKrnlModInfo, uint32_t cEntriesMax,
197 uint32_t *pcEntries)
198{
199 AssertReturn(VALID_PTR(pahKrnlModInfo) || cEntriesMax == 0, VERR_INVALID_PARAMETER);
200
201 uint32_t cKmodsLoaded = RTKrnlModLoadedGetCount();
202 if (cEntriesMax < cKmodsLoaded)
203 {
204 if (*pcEntries)
205 *pcEntries = cKmodsLoaded;
206 return VERR_BUFFER_OVERFLOW;
207 }
208
209 PRTDIR pDir = NULL;
210 int rc = RTDirOpen(&pDir, "/sys/module");
211 if (RT_SUCCESS(rc))
212 {
213 unsigned idxKrnlModInfo = 0;
214 RTDIRENTRY DirEnt;
215
216 rc = RTDirRead(pDir, &DirEnt, NULL);
217 while (RT_SUCCESS(rc))
218 {
219 if ( RTStrCmp(DirEnt.szName, ".")
220 && RTStrCmp(DirEnt.szName, ".."))
221 {
222 rc = rtKrnlModLinuxInfoCreate(DirEnt.szName, &pahKrnlModInfo[idxKrnlModInfo]);
223 if (RT_SUCCESS(rc))
224 idxKrnlModInfo++;
225 }
226
227 if (RT_SUCCESS(rc))
228 rc = RTDirRead(pDir, &DirEnt, NULL);
229 }
230
231 if (rc == VERR_NO_MORE_FILES)
232 rc = VINF_SUCCESS;
233 else if (RT_FAILURE(rc))
234 {
235 /* Rollback */
236 while (idxKrnlModInfo-- > 0)
237 RTKrnlModInfoRelease(pahKrnlModInfo[idxKrnlModInfo]);
238 }
239
240 if (*pcEntries)
241 *pcEntries = cKmodsLoaded;
242
243 RTDirClose(pDir);
244 }
245
246 return rc;
247}
248
249
250RTDECL(uint32_t) RTKrnlModInfoRetain(RTKRNLMODINFO hKrnlModInfo)
251{
252 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
253 AssertPtrReturn(pThis, UINT32_MAX);
254
255 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
256 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
257 return cRefs;
258}
259
260
261RTDECL(uint32_t) RTKrnlModInfoRelease(RTKRNLMODINFO hKrnlModInfo)
262{
263 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
264 if (!pThis)
265 return 0;
266 AssertPtrReturn(pThis, UINT32_MAX);
267
268 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
269 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
270 if (cRefs == 0)
271 rtKrnlModInfoDestroy(pThis);
272 return cRefs;
273}
274
275
276RTDECL(uint32_t) RTKrnlModInfoGetRefCnt(RTKRNLMODINFO hKrnlModInfo)
277{
278 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
279 AssertPtrReturn(pThis, 0);
280
281 return pThis->cRefKrnlMod;
282}
283
284
285RTDECL(const char *) RTKrnlModInfoGetName(RTKRNLMODINFO hKrnlModInfo)
286{
287 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
288 AssertPtrReturn(pThis, NULL);
289
290 return &pThis->achName[0];
291}
292
293
294RTDECL(const char *) RTKrnlModInfoGetFilePath(RTKRNLMODINFO hKrnlModInfo)
295{
296 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
297 AssertPtrReturn(pThis, NULL);
298
299 return NULL;
300}
301
302
303RTDECL(size_t) RTKrnlModInfoGetSize(RTKRNLMODINFO hKrnlModInfo)
304{
305 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
306 AssertPtrReturn(pThis, 0);
307
308 return pThis->cbKrnlMod;
309}
310
311
312RTDECL(RTR0UINTPTR) RTKrnlModInfoGetLoadAddr(RTKRNLMODINFO hKrnlModInfo)
313{
314 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
315 AssertPtrReturn(pThis, 0);
316
317 return pThis->uLoadAddr;
318}
319
320
321RTDECL(int) RTKrnlModInfoQueryRefModInfo(RTKRNLMODINFO hKrnlModInfo, uint32_t idx,
322 PRTKRNLMODINFO phKrnlModInfoRef)
323{
324 RT_NOREF3(hKrnlModInfo, idx, phKrnlModInfoRef);
325 return VERR_NOT_IMPLEMENTED;
326}
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