VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxService/VBoxVMInfo.cpp@ 12724

最後變更 在這個檔案從12724是 12724,由 vboxsync 提交於 17 年 前

VBoxService: Implemented temporary solution for getting rid of invalid guest properties when the VM goes down (reboot/shutdown).

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 5.4 KB
 
1/* $Id: VBoxVMInfo.cpp 12724 2008-09-25 12:13:12Z vboxsync $ */
2/** @file
3 * VBoxVMInfo - Virtual machine (guest) information for the host.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * Sun Microsystems, Inc. confidential
10 * All rights reserved
11 */
12
13#include "VBoxService.h"
14#include "VBoxVMInfo.h"
15#include "VBoxVMInfoAdditions.h"
16#include "VBoxVMInfoUser.h"
17#include "VBoxVMInfoNet.h"
18#include "VBoxVMInfoOS.h"
19
20static VBOXINFORMATIONCONTEXT gCtx = {0};
21
22int vboxVMInfoWriteProp(VBOXINFORMATIONCONTEXT* a_pCtx, char *a_pszKey, char *a_pszValue)
23{
24 int rc = VINF_SUCCESS;
25 Assert(a_pCtx);
26 Assert(a_pszKey);
27 /* Not checking for a valid a_pszValue is intentional. */
28
29 char szKeyTemp [_MAX_PATH] = {0};
30 char *pszValue = NULL;
31
32 /* Append base path. */
33 RTStrPrintf(szKeyTemp, sizeof(szKeyTemp), "/VirtualBox/%s", a_pszKey); /** @todo r=bird: Why didn't you hardcode this into the strings before calling this function? */
34
35 if (a_pszValue != NULL)
36 {
37 rc = RTStrCurrentCPToUtf8(&pszValue, a_pszValue);
38 if (!RT_SUCCESS(rc)) {
39 LogRel(("vboxVMInfoThread: Failed to convert the value name \"%s\" to Utf8! Error: %Rrc\n", a_pszValue, rc));
40 goto cleanup;
41 }
42 }
43
44 rc = VbglR3GuestPropWriteValue(a_pCtx->iInfoSvcClientID, szKeyTemp, (a_pszValue == NULL) ? NULL : pszValue);
45 if (!RT_SUCCESS(rc))
46 {
47 LogRel(("vboxVMInfoThread: Failed to store the property \"%s\"=\"%s\"! ClientID: %d, Error: %Rrc\n", szKeyTemp, pszValue, a_pCtx->iInfoSvcClientID, rc));
48 goto cleanup;
49 }
50
51 if (pszValue != NULL)
52 Log(("vboxVMInfoThread: Property written: %s = %s\n", szKeyTemp, pszValue));
53 else
54 Log(("vboxVMInfoThread: Property deleted: %s\n", szKeyTemp));
55
56cleanup:
57
58 RTStrFree(pszValue);
59 return rc;
60}
61
62int vboxVMInfoWritePropInt(VBOXINFORMATIONCONTEXT* a_pCtx, char *a_pszKey, int a_iValue)
63{
64 Assert(a_pCtx);
65 Assert(a_pszKey);
66
67 char szBuffer[_MAX_PATH] = {0}; /** @todo r=bird: this is a bit excessive (the size) */
68 itoa(a_iValue, szBuffer, 10);
69
70 return vboxVMInfoWriteProp(a_pCtx, a_pszKey, szBuffer);
71}
72
73int vboxVMInfoInit(const VBOXSERVICEENV *pEnv, void **ppInstance, bool *pfStartThread)
74{
75 Assert(pEnv);
76 Assert(ppInstance);
77
78 Log(("vboxVMInfoThread: Init.\n"));
79
80 gCtx.pEnv = pEnv;
81 gCtx.fFirstRun = TRUE;
82
83 int rc = VbglR3GuestPropConnect(&gCtx.iInfoSvcClientID);
84 if (!RT_SUCCESS(rc))
85 LogRel(("vboxVMInfoThread: Failed to connect to the guest property service! Error: %Rrc\n", rc));
86 else
87 {
88 LogRel(("vboxVMInfoThread: GuestProp ClientID = %d\n", gCtx.iInfoSvcClientID));
89
90 /* Loading dynamic APIs. */
91 HMODULE hKernel32 = LoadLibrary(_T("kernel32"));
92 if (NULL != hKernel32)
93 {
94 gCtx.pfnWTSGetActiveConsoleSessionId = NULL;
95 gCtx.pfnWTSGetActiveConsoleSessionId = (fnWTSGetActiveConsoleSessionId)GetProcAddress(hKernel32, "WTSGetActiveConsoleSessionId");
96
97 FreeLibrary(hKernel32);
98 }
99
100 *pfStartThread = true;
101 *ppInstance = &gCtx;
102 }
103
104 return 0;
105}
106
107void vboxVMInfoDestroy(const VBOXSERVICEENV *pEnv, void *pInstance)
108{
109 Assert(pEnv);
110
111 VBOXINFORMATIONCONTEXT *pCtx = (VBOXINFORMATIONCONTEXT *)pInstance;
112 Assert(pCtx);
113
114 /* @todo Temporary solution: Zap all values which are not valid anymore when VM goes down (reboot/shutdown).
115 * Needs to be replaced with "temporary properties" later. */
116 char szPropPath [_MAX_PATH+1] = {0};
117 char*pPtr = &szPropPath[0];
118
119 vboxVMInfoWriteProp(pCtx, "GuestInfo/OS/LoggedInUsersList", "");
120 vboxVMInfoWritePropInt(pCtx, "GuestInfo/OS/LoggedInUsers", 0);
121
122 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/*");
123 VbglR3GuestPropDelTree(pCtx->iInfoSvcClientID, &pPtr, 1);
124 vboxVMInfoWritePropInt(pCtx, "GuestInfo/Net/Count", 0);
125
126 /* Disconnect from guest properties API. */
127 int rc = VbglR3GuestPropDisconnect(pCtx->iInfoSvcClientID);
128 if (!RT_SUCCESS(rc))
129 LogRel(("vboxVMInfoThread: Failed to disconnect from guest property service! Error: %Rrc\n", rc));
130
131 Log(("vboxVMInfoThread: Destroyed.\n"));
132 return;
133}
134
135unsigned __stdcall vboxVMInfoThread(void *pInstance)
136{
137 Assert(pInstance);
138
139 VBOXINFORMATIONCONTEXT *pCtx = (VBOXINFORMATIONCONTEXT *)pInstance;
140 bool fTerminate = false;
141
142 Log(("vboxVMInfoThread: Started.\n"));
143
144 if (NULL == pCtx) {
145 Log(("vboxVMInfoThread: Invalid context!\n"));
146 return -1;
147 }
148
149 WSADATA wsaData;
150 DWORD cbReturned = 0;
151 DWORD dwCnt = 5;
152
153 /* Required for network information. */
154 if (WSAStartup(0x0101, &wsaData)) {
155 Log(("vboxVMInfoThread: WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError())));
156 return -1;
157 }
158
159 do
160 {
161 if (dwCnt++ < 5)
162 {
163 /* Sleep a bit to not eat too much CPU. */
164 if (NULL == pCtx->pEnv->hStopEvent)
165 Log(("vboxVMInfoThread: Invalid stop event!\n"));
166
167 if (WaitForSingleObject (pCtx->pEnv->hStopEvent, 1000) == WAIT_OBJECT_0)
168 {
169 Log(("vboxVMInfoThread: Got stop event, terminating ...\n"));
170 fTerminate = true;
171 break;
172 }
173
174 continue;
175 }
176
177 dwCnt = 0;
178
179 vboxVMInfoOS(pCtx);
180 vboxVMInfoAdditions(pCtx);
181 vboxVMInfoNet(pCtx);
182 vboxVMInfoUser(pCtx);
183
184 if (pCtx->fFirstRun)
185 pCtx->fFirstRun = FALSE;
186 }
187 while (!fTerminate);
188
189 WSACleanup();
190 return 0;
191}
192
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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