1 | /* $Id: VBoxVMInfo.cpp 12810 2008-09-29 16:11:14Z 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 |
|
---|
20 | static VBOXINFORMATIONCONTEXT gCtx = {0};
|
---|
21 |
|
---|
22 | int 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 |
|
---|
56 | cleanup:
|
---|
57 |
|
---|
58 | RTStrFree(pszValue);
|
---|
59 | return rc;
|
---|
60 | }
|
---|
61 |
|
---|
62 | int 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 |
|
---|
73 | int 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 | Log(("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 |
|
---|
107 | void 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 |
|
---|
117 | vboxVMInfoWriteProp(pCtx, "GuestInfo/OS/LoggedInUsersList", "");
|
---|
118 | vboxVMInfoWritePropInt(pCtx, "GuestInfo/OS/LoggedInUsers", 0);
|
---|
119 |
|
---|
120 | const char *apszPat[1] = { "/VirtualBox/GuestInfo/Net/*" };
|
---|
121 | VbglR3GuestPropDelSet(pCtx->iInfoSvcClientID, &apszPat[0], RT_ELEMENTS(apszPat));
|
---|
122 | vboxVMInfoWritePropInt(pCtx, "GuestInfo/Net/Count", 0);
|
---|
123 |
|
---|
124 | /* Disconnect from guest properties API. */
|
---|
125 | int rc = VbglR3GuestPropDisconnect(pCtx->iInfoSvcClientID);
|
---|
126 | if (!RT_SUCCESS(rc))
|
---|
127 | LogRel(("vboxVMInfoThread: Failed to disconnect from guest property service! Error: %Rrc\n", rc));
|
---|
128 |
|
---|
129 | Log(("vboxVMInfoThread: Destroyed.\n"));
|
---|
130 | return;
|
---|
131 | }
|
---|
132 |
|
---|
133 | unsigned __stdcall vboxVMInfoThread(void *pInstance)
|
---|
134 | {
|
---|
135 | Assert(pInstance);
|
---|
136 |
|
---|
137 | VBOXINFORMATIONCONTEXT *pCtx = (VBOXINFORMATIONCONTEXT *)pInstance;
|
---|
138 | bool fTerminate = false;
|
---|
139 |
|
---|
140 | Log(("vboxVMInfoThread: Started.\n"));
|
---|
141 |
|
---|
142 | if (NULL == pCtx) {
|
---|
143 | Log(("vboxVMInfoThread: Invalid context!\n"));
|
---|
144 | return -1;
|
---|
145 | }
|
---|
146 |
|
---|
147 | WSADATA wsaData;
|
---|
148 | DWORD cbReturned = 0;
|
---|
149 | DWORD dwCnt = 5;
|
---|
150 |
|
---|
151 | /* Required for network information. */
|
---|
152 | if (WSAStartup(0x0101, &wsaData)) {
|
---|
153 | Log(("vboxVMInfoThread: WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError())));
|
---|
154 | return -1;
|
---|
155 | }
|
---|
156 |
|
---|
157 | do
|
---|
158 | {
|
---|
159 | if (dwCnt++ < 5)
|
---|
160 | {
|
---|
161 | /* Sleep a bit to not eat too much CPU. */
|
---|
162 | if (NULL == pCtx->pEnv->hStopEvent)
|
---|
163 | Log(("vboxVMInfoThread: Invalid stop event!\n"));
|
---|
164 |
|
---|
165 | if (WaitForSingleObject (pCtx->pEnv->hStopEvent, 1000) == WAIT_OBJECT_0)
|
---|
166 | {
|
---|
167 | Log(("vboxVMInfoThread: Got stop event, terminating ...\n"));
|
---|
168 | fTerminate = true;
|
---|
169 | break;
|
---|
170 | }
|
---|
171 |
|
---|
172 | continue;
|
---|
173 | }
|
---|
174 |
|
---|
175 | dwCnt = 0;
|
---|
176 |
|
---|
177 | vboxVMInfoOS(pCtx);
|
---|
178 | vboxVMInfoAdditions(pCtx);
|
---|
179 | vboxVMInfoNet(pCtx);
|
---|
180 | vboxVMInfoUser(pCtx);
|
---|
181 |
|
---|
182 | if (pCtx->fFirstRun)
|
---|
183 | pCtx->fFirstRun = FALSE;
|
---|
184 | }
|
---|
185 | while (!fTerminate);
|
---|
186 |
|
---|
187 | WSACleanup();
|
---|
188 | return 0;
|
---|
189 | }
|
---|
190 |
|
---|