VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServicePageSharing.cpp@ 30646

Last change on this file since 30646 was 30646, checked in by vboxsync, 15 years ago

Reregister all shared modules after VM restore

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.7 KB
Line 
1/* $Id: VBoxServicePageSharing.cpp 30646 2010-07-06 08:00:31Z vboxsync $ */
2/** @file
3 * VBoxService - Guest page sharing.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <iprt/assert.h>
23#include <iprt/avl.h>
24#include <iprt/asm.h>
25#include <iprt/mem.h>
26#include <iprt/process.h>
27#include <iprt/env.h>
28#include <iprt/stream.h>
29#include <iprt/file.h>
30#include <iprt/string.h>
31#include <iprt/semaphore.h>
32#include <iprt/system.h>
33#include <iprt/thread.h>
34#include <iprt/time.h>
35#include <VBox/VBoxGuestLib.h>
36#include "VBoxServiceInternal.h"
37#include "VBoxServiceUtils.h"
38
39
40/*******************************************************************************
41* Global Variables *
42*******************************************************************************/
43
44/** The semaphore we're blocking on. */
45static RTSEMEVENTMULTI g_PageSharingEvent = NIL_RTSEMEVENTMULTI;
46
47#if defined(RT_OS_WINDOWS) && !defined(TARGET_NT4)
48#include <tlhelp32.h>
49#include <psapi.h>
50#include <winternl.h>
51
52typedef struct
53{
54 AVLPVNODECORE Core;
55 HMODULE hModule;
56 char szFileVersion[16];
57 MODULEENTRY32 Info;
58} KNOWN_MODULE, *PKNOWN_MODULE;
59
60#define SystemModuleInformation 11
61
62typedef struct _RTL_PROCESS_MODULE_INFORMATION
63{
64 ULONG Section;
65 PVOID MappedBase;
66 PVOID ImageBase;
67 ULONG ImageSize;
68 ULONG Flags;
69 USHORT LoadOrderIndex;
70 USHORT InitOrderIndex;
71 USHORT LoadCount;
72 USHORT OffsetToFileName;
73 CHAR FullPathName[256];
74} RTL_PROCESS_MODULE_INFORMATION, *PRTL_PROCESS_MODULE_INFORMATION;
75
76typedef struct _RTL_PROCESS_MODULES
77{
78 ULONG NumberOfModules;
79 RTL_PROCESS_MODULE_INFORMATION Modules[1];
80} RTL_PROCESS_MODULES, *PRTL_PROCESS_MODULES;
81
82typedef NTSTATUS (WINAPI *PFNZWQUERYSYSTEMINFORMATION)(ULONG, PVOID, ULONG, PULONG);
83static PFNZWQUERYSYSTEMINFORMATION ZwQuerySystemInformation = NULL;
84static HMODULE hNtdll = 0;
85
86
87static DECLCALLBACK(int) VBoxServicePageSharingEmptyTreeCallback(PAVLPVNODECORE pNode, void *pvUser);
88
89static PAVLPVNODECORE pKnownModuleTree = NULL;
90
91/**
92 * Registers a new module with the VMM
93 * @param pModule Module ptr
94 * @param fValidateMemory Validate/touch memory pages or not
95 */
96void VBoxServicePageSharingRegisterModule(PKNOWN_MODULE pModule, bool fValidateMemory)
97{
98 VMMDEVSHAREDREGIONDESC aRegions[VMMDEVSHAREDREGIONDESC_MAX];
99 DWORD dwModuleSize = pModule->Info.modBaseSize;
100 BYTE *pBaseAddress = pModule->Info.modBaseAddr;
101 DWORD cbVersionSize, dummy;
102 BYTE *pVersionInfo;
103
104 VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule\n");
105
106 cbVersionSize = GetFileVersionInfoSize(pModule->Info.szExePath, &dummy);
107 if (!cbVersionSize)
108 {
109 VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: GetFileVersionInfoSize failed with %d\n", GetLastError());
110 return;
111 }
112 pVersionInfo = (BYTE *)RTMemAlloc(cbVersionSize);
113 if (!pVersionInfo)
114 return;
115
116 if (!GetFileVersionInfo(pModule->Info.szExePath, 0, cbVersionSize, pVersionInfo))
117 {
118 VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: GetFileVersionInfo failed with %d\n", GetLastError());
119 goto end;
120 }
121
122 /* Fetch default code page. */
123 struct LANGANDCODEPAGE {
124 WORD wLanguage;
125 WORD wCodePage;
126 } *lpTranslate;
127
128 UINT cbTranslate;
129 BOOL ret = VerQueryValue(pVersionInfo, TEXT("\\VarFileInfo\\Translation"), (LPVOID *)&lpTranslate, &cbTranslate);
130 if ( !ret
131 || cbTranslate < 4)
132 {
133 VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: VerQueryValue failed with %d (cb=%d)\n", GetLastError(), cbTranslate);
134 goto end;
135 }
136
137 unsigned i;
138 UINT cbFileVersion;
139 char *lpszFileVersion;
140 unsigned cTranslationBlocks = cbTranslate/sizeof(struct LANGANDCODEPAGE);
141
142 for(i = 0; i < cTranslationBlocks; i++)
143 {
144 /* Fetch file version string. */
145 char szFileVersionLocation[256];
146
147 sprintf(szFileVersionLocation, TEXT("\\StringFileInfo\\%04x%04x\\FileVersion"), lpTranslate[i].wLanguage, lpTranslate[i].wCodePage);
148 ret = VerQueryValue(pVersionInfo, szFileVersionLocation, (LPVOID *)&lpszFileVersion, &cbFileVersion);
149 if (ret)
150 break;
151 }
152 if (i == cTranslationBlocks)
153 {
154 VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: no file version found!\n");
155 goto end;
156 }
157
158 _snprintf(pModule->szFileVersion, sizeof(pModule->szFileVersion), "%s", lpszFileVersion);
159 pModule->szFileVersion[RT_ELEMENTS(pModule->szFileVersion) - 1] = 0;
160
161 unsigned idxRegion = 0;
162
163 if (fValidateMemory)
164 {
165 do
166 {
167 MEMORY_BASIC_INFORMATION MemInfo;
168
169 SIZE_T ret = VirtualQuery(pBaseAddress, &MemInfo, sizeof(MemInfo));
170 Assert(ret);
171 if (!ret)
172 {
173 VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: VirtualQueryEx failed with %d\n", GetLastError());
174 break;
175 }
176
177 if ( MemInfo.State == MEM_COMMIT
178 && MemInfo.Type == MEM_IMAGE)
179 {
180 switch (MemInfo.Protect)
181 {
182 case PAGE_EXECUTE:
183 case PAGE_EXECUTE_READ:
184 case PAGE_READONLY:
185 {
186 char *pRegion = (char *)MemInfo.BaseAddress;
187
188 /* Skip the first region as it only contains the image file header. */
189 if (pRegion != (char *)pModule->Info.modBaseAddr)
190 {
191 /* Touch all pages. */
192 while (pRegion < (char *)MemInfo.BaseAddress + MemInfo.RegionSize)
193 {
194 /* Try to trick the optimizer to leave the page touching code in place. */
195 ASMProbeReadByte(pRegion);
196 pRegion += PAGE_SIZE;
197 }
198 }
199#ifdef RT_ARCH_X86
200 aRegions[idxRegion].GCRegionAddr = (RTGCPTR32)MemInfo.BaseAddress;
201#else
202 aRegions[idxRegion].GCRegionAddr = (RTGCPTR64)MemInfo.BaseAddress;
203#endif
204 aRegions[idxRegion].cbRegion = MemInfo.RegionSize;
205 idxRegion++;
206
207 break;
208 }
209
210 default:
211 break; /* ignore */
212 }
213 }
214
215 pBaseAddress = (BYTE *)MemInfo.BaseAddress + MemInfo.RegionSize;
216 if (dwModuleSize > MemInfo.RegionSize)
217 {
218 dwModuleSize -= MemInfo.RegionSize;
219 }
220 else
221 {
222 dwModuleSize = 0;
223 break;
224 }
225
226 if (idxRegion >= RT_ELEMENTS(aRegions))
227 break; /* out of room */
228 }
229 while (dwModuleSize);
230 }
231 else
232 {
233 /* We can't probe kernel memory ranges, so pretend it's one big region. */
234#ifdef RT_ARCH_X86
235 aRegions[idxRegion].GCRegionAddr = (RTGCPTR32)pBaseAddress;
236#else
237 aRegions[idxRegion].GCRegionAddr = (RTGCPTR64)pBaseAddress;
238#endif
239 aRegions[idxRegion].cbRegion = dwModuleSize;
240 idxRegion++;
241 }
242 VBoxServiceVerbose(3, "VbglR3RegisterSharedModule %s %s base=%p size=%x cregions=%d\n", pModule->Info.szModule, pModule->szFileVersion, pModule->Info.modBaseAddr, pModule->Info.modBaseSize, idxRegion);
243#ifdef RT_ARCH_X86
244 int rc = VbglR3RegisterSharedModule(pModule->Info.szModule, pModule->szFileVersion, (RTGCPTR32)pModule->Info.modBaseAddr,
245 pModule->Info.modBaseSize, idxRegion, aRegions);
246#else
247 int rc = VbglR3RegisterSharedModule(pModule->Info.szModule, pModule->szFileVersion, (RTGCPTR64)pModule->Info.modBaseAddr,
248 pModule->Info.modBaseSize, idxRegion, aRegions);
249#endif
250
251// AssertRC(rc);
252 if (RT_FAILURE(rc))
253 VBoxServiceVerbose(3, "VbglR3RegisterSharedModule failed with %d\n", rc);
254
255end:
256 RTMemFree(pVersionInfo);
257 return;
258}
259
260/**
261 * Inspect all loaded modules for the specified process
262 * @param dwProcessId Process id
263 */
264void VBoxServicePageSharingInspectModules(DWORD dwProcessId, PAVLPVNODECORE *ppNewTree)
265{
266 HANDLE hProcess, hSnapshot;
267
268 /* Get a list of all the modules in this process. */
269 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,
270 FALSE /* no child process handle inheritance */, dwProcessId);
271 if (hProcess == NULL)
272 {
273 VBoxServiceVerbose(3, "VBoxServicePageSharingInspectModules: OpenProcess %x failed with %d\n", dwProcessId, GetLastError());
274 return;
275 }
276
277 hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessId);
278 if (hSnapshot == INVALID_HANDLE_VALUE)
279 {
280 VBoxServiceVerbose(3, "VBoxServicePageSharingInspectModules: CreateToolhelp32Snapshot failed with %d\n", GetLastError());
281 CloseHandle(hProcess);
282 return;
283 }
284
285 VBoxServiceVerbose(3, "VBoxServicePageSharingInspectModules\n");
286
287 MODULEENTRY32 ModuleInfo;
288 BOOL bRet;
289
290 ModuleInfo.dwSize = sizeof(ModuleInfo);
291 bRet = Module32First(hSnapshot, &ModuleInfo);
292 do
293 {
294 /** todo when changing this make sure VBoxService.exe is excluded! */
295 char *pszDot = strrchr(ModuleInfo.szModule, '.');
296 if ( pszDot
297 && (pszDot[1] == 'e' || pszDot[1] == 'E'))
298 continue; /* ignore executables for now. */
299
300 /* Found it before? */
301 PAVLPVNODECORE pRec = RTAvlPVGet(ppNewTree, ModuleInfo.modBaseAddr);
302 if (!pRec)
303 {
304 pRec = RTAvlPVRemove(&pKnownModuleTree, ModuleInfo.modBaseAddr);
305 if (!pRec)
306 {
307 /* New module; register it. */
308 PKNOWN_MODULE pModule = (PKNOWN_MODULE)RTMemAllocZ(sizeof(*pModule));
309 Assert(pModule);
310 if (!pModule)
311 break;
312
313 pModule->Info = ModuleInfo;
314 pModule->Core.Key = ModuleInfo.modBaseAddr;
315 pModule->hModule = LoadLibraryEx(ModuleInfo.szExePath, 0, DONT_RESOLVE_DLL_REFERENCES);
316 if (pModule->hModule)
317 VBoxServicePageSharingRegisterModule(pModule, true /* validate pages */);
318
319 VBoxServiceVerbose(3, "\n\n MODULE NAME: %s", ModuleInfo.szModule );
320 VBoxServiceVerbose(3, "\n executable = %s", ModuleInfo.szExePath );
321 VBoxServiceVerbose(3, "\n process ID = 0x%08X", ModuleInfo.th32ProcessID );
322 VBoxServiceVerbose(3, "\n base address = 0x%08X", (DWORD) ModuleInfo.modBaseAddr );
323 VBoxServiceVerbose(3, "\n base size = %d", ModuleInfo.modBaseSize );
324
325 pRec = &pModule->Core;
326 }
327 bool ret = RTAvlPVInsert(ppNewTree, pRec);
328 Assert(ret); NOREF(ret);
329 }
330 }
331 while (Module32Next(hSnapshot, &ModuleInfo));
332
333 CloseHandle(hSnapshot);
334 CloseHandle(hProcess);
335}
336
337/**
338 * Inspect all running processes for executables and dlls that might be worth sharing
339 * with other VMs.
340 *
341 */
342void VBoxServicePageSharingInspectGuest()
343{
344 HANDLE hSnapshot;
345 PAVLPVNODECORE pNewTree = NULL;
346 DWORD dwProcessId = GetCurrentProcessId();
347
348 VBoxServiceVerbose(3, "VBoxServicePageSharingInspectGuest\n");
349
350 hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
351 if (hSnapshot == INVALID_HANDLE_VALUE)
352 {
353 VBoxServiceVerbose(3, "CreateToolhelp32Snapshot failed with %d\n", GetLastError());
354 return;
355 }
356
357 /* Check loaded modules for all running processes. */
358 PROCESSENTRY32 ProcessInfo;
359
360 ProcessInfo.dwSize = sizeof(ProcessInfo);
361 Process32First(hSnapshot, &ProcessInfo);
362
363 do
364 {
365 /* Skip our own process. */
366 if (ProcessInfo.th32ProcessID != dwProcessId)
367 VBoxServicePageSharingInspectModules(ProcessInfo.th32ProcessID, &pNewTree);
368 }
369 while (Process32Next(hSnapshot, &ProcessInfo));
370
371 CloseHandle(hSnapshot);
372
373 /* Check all loaded kernel modules. */
374 if (ZwQuerySystemInformation)
375 {
376 ULONG cbBuffer = 0;
377 PVOID pBuffer = NULL;
378 PRTL_PROCESS_MODULES pSystemModules;
379
380 NTSTATUS ret = ZwQuerySystemInformation(SystemModuleInformation, (PVOID)&cbBuffer, 0, &cbBuffer);
381 if (!cbBuffer)
382 {
383 VBoxServiceVerbose(1, "ZwQuerySystemInformation returned length 0\n");
384 goto skipkernelmodules;
385 }
386
387 pBuffer = RTMemAllocZ(cbBuffer);
388 if (!pBuffer)
389 goto skipkernelmodules;
390
391 ret = ZwQuerySystemInformation(SystemModuleInformation, pBuffer, cbBuffer, &cbBuffer);
392 if (ret != STATUS_SUCCESS)
393 {
394 VBoxServiceVerbose(1, "ZwQuerySystemInformation returned %x (1)\n", ret);
395 goto skipkernelmodules;
396 }
397
398 pSystemModules = (PRTL_PROCESS_MODULES)pBuffer;
399 for (unsigned i = 0; i < pSystemModules->NumberOfModules; i++)
400 {
401 VBoxServiceVerbose(4, "\n\n KERNEL MODULE NAME: %s", pSystemModules->Modules[i].FullPathName[pSystemModules->Modules[i].OffsetToFileName] );
402 VBoxServiceVerbose(4, "\n executable = %s", pSystemModules->Modules[i].FullPathName );
403 VBoxServiceVerbose(4, "\n flags = 0x%08X\n", pSystemModules->Modules[i].Flags);
404
405 /* User-mode modules seem to have no flags set; skip them as we detected them above. */
406 if (pSystemModules->Modules[i].Flags == 0)
407 continue;
408
409 /* Found it before? */
410 PAVLPVNODECORE pRec = RTAvlPVGet(&pNewTree, pSystemModules->Modules[i].ImageBase);
411 if (!pRec)
412 {
413 pRec = RTAvlPVRemove(&pKnownModuleTree, pSystemModules->Modules[i].ImageBase);
414 if (!pRec)
415 {
416 /* New module; register it. */
417 char szFullFilePath[512];
418 PKNOWN_MODULE pModule = (PKNOWN_MODULE)RTMemAllocZ(sizeof(*pModule));
419 Assert(pModule);
420 if (!pModule)
421 break;
422
423 strcpy(pModule->Info.szModule, &pSystemModules->Modules[i].FullPathName[pSystemModules->Modules[i].OffsetToFileName]);
424 GetSystemDirectoryA(szFullFilePath, sizeof(szFullFilePath));
425
426 /* skip \Systemroot\system32 */
427 char *lpPath = strchr(&pSystemModules->Modules[i].FullPathName[1], '\\');
428 if (!lpPath)
429 {
430 /* Seen just file names in XP; try to locate the file in the system32 and system32\drivers directories. */
431 strcat(szFullFilePath, "\\");
432 strcat(szFullFilePath, pSystemModules->Modules[i].FullPathName);
433 VBoxServiceVerbose(3, "Unexpected kernel module name try %s\n", szFullFilePath);
434 if (RTFileExists(szFullFilePath) == false)
435 {
436 GetSystemDirectoryA(szFullFilePath, sizeof(szFullFilePath));
437 strcat(szFullFilePath, "\\drivers\\");
438 strcat(szFullFilePath, pSystemModules->Modules[i].FullPathName);
439 VBoxServiceVerbose(3, "Unexpected kernel module name try %s\n", szFullFilePath);
440 if (RTFileExists(szFullFilePath) == false)
441 {
442 VBoxServiceVerbose(1, "Unexpected kernel module name %s\n", pSystemModules->Modules[i].FullPathName);
443 RTMemFree(pModule);
444 continue;
445 }
446 }
447 }
448 else
449 {
450 lpPath = strchr(lpPath+1, '\\');
451 if (!lpPath)
452 {
453 VBoxServiceVerbose(1, "Unexpected kernel module name %s (2)\n", pSystemModules->Modules[i].FullPathName);
454 RTMemFree(pModule);
455 continue;
456 }
457
458 strcat(szFullFilePath, lpPath);
459 }
460
461 strcpy(pModule->Info.szExePath, szFullFilePath);
462 pModule->Info.modBaseAddr = (BYTE *)pSystemModules->Modules[i].ImageBase;
463 pModule->Info.modBaseSize = pSystemModules->Modules[i].ImageSize;
464
465 pModule->Core.Key = pSystemModules->Modules[i].ImageBase;
466 VBoxServicePageSharingRegisterModule(pModule, false /* don't check memory pages */);
467
468 VBoxServiceVerbose(3, "\n\n KERNEL MODULE NAME: %s", pModule->Info.szModule );
469 VBoxServiceVerbose(3, "\n executable = %s", pModule->Info.szExePath );
470 VBoxServiceVerbose(3, "\n base address = 0x%08X", (DWORD) pModule->Info.modBaseAddr );
471 VBoxServiceVerbose(3, "\n flags = 0x%08X", pSystemModules->Modules[i].Flags);
472 VBoxServiceVerbose(3, "\n base size = %d", pModule->Info.modBaseSize );
473
474 pRec = &pModule->Core;
475 }
476 bool ret = RTAvlPVInsert(&pNewTree, pRec);
477 Assert(ret); NOREF(ret);
478 }
479 }
480skipkernelmodules:
481 if (pBuffer)
482 RTMemFree(pBuffer);
483 }
484
485 /* Delete leftover modules in the old tree. */
486 RTAvlPVDestroy(&pKnownModuleTree, VBoxServicePageSharingEmptyTreeCallback, NULL);
487
488 /* Check all registered modules. */
489 int rc = VbglR3CheckSharedModules();
490 if (rc == VINF_PGM_SHARED_MODULE_NONE_REGISTERED)
491 {
492 bool fUnregister = false;
493 /* The VM was restored, so reregister all modules the next time. */
494 RTAvlPVDestroy(&pKnownModuleTree, VBoxServicePageSharingEmptyTreeCallback, &fUnregister);
495 pKnownModuleTree = NULL;
496 }
497 else
498 {
499 /* Activate new module tree. */
500 pKnownModuleTree = pNewTree;
501 }
502}
503
504/**
505 * RTAvlPVDestroy callback.
506 */
507static DECLCALLBACK(int) VBoxServicePageSharingEmptyTreeCallback(PAVLPVNODECORE pNode, void *pvUser)
508{
509 PKNOWN_MODULE pModule = (PKNOWN_MODULE)pNode;
510 bool *pfUnregister = (bool *)pvUser;
511
512 VBoxServiceVerbose(3, "VBoxServicePageSharingEmptyTreeCallback %s %s\n", pModule->Info.szModule, pModule->szFileVersion);
513
514 /* Dereference module in the hypervisor. */
515 if ( !pfUnregister
516 || *pfUnregister == true)
517 {
518 #ifdef RT_ARCH_X86
519 int rc = VbglR3UnregisterSharedModule(pModule->Info.szModule, pModule->szFileVersion, (RTGCPTR32)pModule->Info.modBaseAddr, pModule->Info.modBaseSize);
520 #else
521 int rc = VbglR3UnregisterSharedModule(pModule->Info.szModule, pModule->szFileVersion, (RTGCPTR64)pModule->Info.modBaseAddr, pModule->Info.modBaseSize);
522 #endif
523 AssertRC(rc);
524 }
525
526 if (pModule->hModule)
527 FreeLibrary(pModule->hModule);
528 RTMemFree(pNode);
529 return 0;
530}
531
532
533#elif TARGET_NT4
534void VBoxServicePageSharingInspectGuest()
535{
536 /* not implemented */
537}
538#else
539void VBoxServicePageSharingInspectGuest()
540{
541 /* @todo other platforms */
542}
543#endif
544
545/** @copydoc VBOXSERVICE::pfnPreInit */
546static DECLCALLBACK(int) VBoxServicePageSharingPreInit(void)
547{
548 return VINF_SUCCESS;
549}
550
551
552/** @copydoc VBOXSERVICE::pfnOption */
553static DECLCALLBACK(int) VBoxServicePageSharingOption(const char **ppszShort, int argc, char **argv, int *pi)
554{
555 NOREF(ppszShort);
556 NOREF(argc);
557 NOREF(argv);
558 NOREF(pi);
559 return VINF_SUCCESS;
560}
561
562
563/** @copydoc VBOXSERVICE::pfnInit */
564static DECLCALLBACK(int) VBoxServicePageSharingInit(void)
565{
566 VBoxServiceVerbose(3, "VBoxServicePageSharingInit\n");
567
568 int rc = RTSemEventMultiCreate(&g_PageSharingEvent);
569 AssertRCReturn(rc, rc);
570
571#if defined(RT_OS_WINDOWS) && !defined(TARGET_NT4)
572 hNtdll = LoadLibrary("ntdll.dll");
573
574 if (hNtdll)
575 ZwQuerySystemInformation = (PFNZWQUERYSYSTEMINFORMATION)GetProcAddress(hNtdll, "ZwQuerySystemInformation");
576#endif
577
578 /* Never fail here. */
579 return VINF_SUCCESS;
580}
581
582/** @copydoc VBOXSERVICE::pfnWorker */
583DECLCALLBACK(int) VBoxServicePageSharingWorker(bool volatile *pfShutdown)
584{
585 /*
586 * Tell the control thread that it can continue
587 * spawning services.
588 */
589 RTThreadUserSignal(RTThreadSelf());
590
591 /*
592 * Now enter the loop retrieving runtime data continuously.
593 */
594 for (;;)
595 {
596 VBoxServiceVerbose(3, "VBoxServicePageSharingWorker: enabled=%d\n", VbglR3PageSharingIsEnabled());
597
598 if (VbglR3PageSharingIsEnabled())
599 VBoxServicePageSharingInspectGuest();
600
601 /*
602 * Block for a minute.
603 *
604 * The event semaphore takes care of ignoring interruptions and it
605 * allows us to implement service wakeup later.
606 */
607 if (*pfShutdown)
608 break;
609 int rc = RTSemEventMultiWait(g_PageSharingEvent, 60000);
610 if (*pfShutdown)
611 break;
612 if (rc != VERR_TIMEOUT && RT_FAILURE(rc))
613 {
614 VBoxServiceError("RTSemEventMultiWait failed; rc=%Rrc\n", rc);
615 break;
616 }
617 }
618
619 RTSemEventMultiDestroy(g_PageSharingEvent);
620 g_PageSharingEvent = NIL_RTSEMEVENTMULTI;
621
622 VBoxServiceVerbose(3, "VBoxServicePageSharingWorker: finished thread\n");
623 return 0;
624}
625
626#ifdef RT_OS_WINDOWS
627
628/**
629 * This gets control when VBoxService is launched with -pagefusionfork by
630 * VBoxServicePageSharingWorkerProcess().
631 *
632 * @returns RTEXITCODE_SUCCESS.
633 *
634 * @remarks It won't normally return since the parent drops the shutdown hint
635 * via RTProcTerminate().
636 */
637RTEXITCODE VBoxServicePageSharingInitFork(void)
638{
639 VBoxServiceVerbose(3, "VBoxServicePageSharingInitFork\n");
640
641 bool fShutdown = false;
642 VBoxServicePageSharingInit();
643 VBoxServicePageSharingWorker(&fShutdown);
644
645 return RTEXITCODE_SUCCESS;
646}
647
648/** @copydoc VBOXSERVICE::pfnWorker */
649DECLCALLBACK(int) VBoxServicePageSharingWorkerProcess(bool volatile *pfShutdown)
650{
651 RTPROCESS hProcess = NIL_RTPROCESS;
652 int rc;
653
654 /*
655 * Tell the control thread that it can continue
656 * spawning services.
657 */
658 RTThreadUserSignal(RTThreadSelf());
659
660 /*
661 * Now enter the loop retrieving runtime data continuously.
662 */
663 for (;;)
664 {
665 VBoxServiceVerbose(3, "VBoxServicePageSharingWorkerProcess: enabled=%d\n", VbglR3PageSharingIsEnabled());
666
667 if ( VbglR3PageSharingIsEnabled()
668 && hProcess == NIL_RTPROCESS)
669 {
670 char szExeName[256];
671 char *pszExeName;
672 char *pszArgs[3];
673
674 pszExeName = RTProcGetExecutableName(szExeName, sizeof(szExeName));
675
676 if (pszExeName)
677 {
678 pszArgs[0] = pszExeName;
679 pszArgs[1] = "-pagefusionfork";
680 pszArgs[2] = NULL;
681 /* Start a 2nd VBoxService process to deal with page fusion as we do not wish to dummy load
682 * dlls into this process. (first load with DONT_RESOLVE_DLL_REFERENCES, 2nd normal -> dll init routines not called!)
683 */
684 rc = RTProcCreate(pszExeName, pszArgs, RTENV_DEFAULT, 0 /* normal child */, &hProcess);
685 if (rc != VINF_SUCCESS)
686 VBoxServiceError("RTProcCreate %s failed; rc=%Rrc\n", pszExeName, rc);
687 }
688 }
689
690 /*
691 * Block for a minute.
692 *
693 * The event semaphore takes care of ignoring interruptions and it
694 * allows us to implement service wakeup later.
695 */
696 if (*pfShutdown)
697 break;
698 rc = RTSemEventMultiWait(g_PageSharingEvent, 60000);
699 if (*pfShutdown)
700 break;
701 if (rc != VERR_TIMEOUT && RT_FAILURE(rc))
702 {
703 VBoxServiceError("RTSemEventMultiWait failed; rc=%Rrc\n", rc);
704 break;
705 }
706 }
707
708 if (hProcess)
709 RTProcTerminate(hProcess);
710
711 RTSemEventMultiDestroy(g_PageSharingEvent);
712 g_PageSharingEvent = NIL_RTSEMEVENTMULTI;
713
714 VBoxServiceVerbose(3, "VBoxServicePageSharingWorkerProcess: finished thread\n");
715 return 0;
716}
717
718#endif /* RT_OS_WINDOWS */
719
720/** @copydoc VBOXSERVICE::pfnTerm */
721static DECLCALLBACK(void) VBoxServicePageSharingTerm(void)
722{
723 VBoxServiceVerbose(3, "VBoxServicePageSharingTerm\n");
724
725#if defined(RT_OS_WINDOWS) && !defined(TARGET_NT4)
726 if (hNtdll)
727 FreeLibrary(hNtdll);
728#endif
729 return;
730}
731
732
733/** @copydoc VBOXSERVICE::pfnStop */
734static DECLCALLBACK(void) VBoxServicePageSharingStop(void)
735{
736 RTSemEventMultiSignal(g_PageSharingEvent);
737}
738
739
740/**
741 * The 'pagesharing' service description.
742 */
743VBOXSERVICE g_PageSharing =
744{
745 /* pszName. */
746 "pagesharing",
747 /* pszDescription. */
748 "Page Sharing",
749 /* pszUsage. */
750 NULL,
751 /* pszOptions. */
752 NULL,
753 /* methods */
754 VBoxServicePageSharingPreInit,
755 VBoxServicePageSharingOption,
756 VBoxServicePageSharingInit,
757#ifdef RT_OS_WINDOWS
758 VBoxServicePageSharingWorkerProcess,
759#else
760 VBoxServicePageSharingWorker,
761#endif
762 VBoxServicePageSharingStop,
763 VBoxServicePageSharingTerm
764};
Note: See TracBrowser for help on using the repository browser.

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