1 | /* $Id: VBoxVMInfo.cpp 13243 2008-10-14 09:55:40Z 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 | gCtx.cUsers = INT32_MAX; /* value which isn't reached in real life. */
|
---|
83 |
|
---|
84 | int rc = VbglR3GuestPropConnect(&gCtx.iInfoSvcClientID);
|
---|
85 | if (!RT_SUCCESS(rc))
|
---|
86 | LogRel(("vboxVMInfoThread: Failed to connect to the guest property service! Error: %Rrc\n", rc));
|
---|
87 | else
|
---|
88 | {
|
---|
89 | Log(("vboxVMInfoThread: GuestProp ClientID = %d\n", gCtx.iInfoSvcClientID));
|
---|
90 |
|
---|
91 | /* Loading dynamic APIs. */
|
---|
92 | HMODULE hKernel32 = LoadLibrary(_T("kernel32"));
|
---|
93 | if (NULL != hKernel32)
|
---|
94 | {
|
---|
95 | gCtx.pfnWTSGetActiveConsoleSessionId = NULL;
|
---|
96 | gCtx.pfnWTSGetActiveConsoleSessionId = (fnWTSGetActiveConsoleSessionId)GetProcAddress(hKernel32, "WTSGetActiveConsoleSessionId");
|
---|
97 |
|
---|
98 | FreeLibrary(hKernel32);
|
---|
99 | }
|
---|
100 |
|
---|
101 | *pfStartThread = true;
|
---|
102 | *ppInstance = &gCtx;
|
---|
103 | }
|
---|
104 |
|
---|
105 | return 0;
|
---|
106 | }
|
---|
107 |
|
---|
108 | void vboxVMInfoDestroy(const VBOXSERVICEENV *pEnv, void *pInstance)
|
---|
109 | {
|
---|
110 | Assert(pEnv);
|
---|
111 |
|
---|
112 | VBOXINFORMATIONCONTEXT *pCtx = (VBOXINFORMATIONCONTEXT *)pInstance;
|
---|
113 | Assert(pCtx);
|
---|
114 |
|
---|
115 | /** @todo Temporary solution: Zap all values which are not valid anymore when VM goes down (reboot/shutdown).
|
---|
116 | * Needs to be replaced with "temporary properties" later. */
|
---|
117 |
|
---|
118 | vboxVMInfoWriteProp(pCtx, "GuestInfo/OS/LoggedInUsersList", NULL);
|
---|
119 | vboxVMInfoWritePropInt(pCtx, "GuestInfo/OS/LoggedInUsers", 0);
|
---|
120 | if (pCtx->cUsers != 0)
|
---|
121 | vboxVMInfoWriteProp(pCtx, "GuestInfo/OS/NoLoggedInUsers", "true");
|
---|
122 |
|
---|
123 | const char *apszPat[1] = { "/VirtualBox/GuestInfo/Net/*" };
|
---|
124 | VbglR3GuestPropDelSet(pCtx->iInfoSvcClientID, &apszPat[0], RT_ELEMENTS(apszPat));
|
---|
125 | vboxVMInfoWritePropInt(pCtx, "GuestInfo/Net/Count", 0);
|
---|
126 |
|
---|
127 | /* Disconnect from guest properties API. */
|
---|
128 | int rc = VbglR3GuestPropDisconnect(pCtx->iInfoSvcClientID);
|
---|
129 | if (!RT_SUCCESS(rc))
|
---|
130 | LogRel(("vboxVMInfoThread: Failed to disconnect from guest property service! Error: %Rrc\n", rc));
|
---|
131 |
|
---|
132 | Log(("vboxVMInfoThread: Destroyed.\n"));
|
---|
133 | return;
|
---|
134 | }
|
---|
135 |
|
---|
136 | unsigned __stdcall vboxVMInfoThread(void *pInstance)
|
---|
137 | {
|
---|
138 | Assert(pInstance);
|
---|
139 |
|
---|
140 | VBOXINFORMATIONCONTEXT *pCtx = (VBOXINFORMATIONCONTEXT *)pInstance;
|
---|
141 | bool fTerminate = false;
|
---|
142 |
|
---|
143 | Log(("vboxVMInfoThread: Started.\n"));
|
---|
144 |
|
---|
145 | if (NULL == pCtx) {
|
---|
146 | Log(("vboxVMInfoThread: Invalid context!\n"));
|
---|
147 | return -1;
|
---|
148 | }
|
---|
149 |
|
---|
150 | WSADATA wsaData;
|
---|
151 | DWORD cbReturned = 0;
|
---|
152 | DWORD dwCnt = 5;
|
---|
153 |
|
---|
154 | /* Required for network information. */
|
---|
155 | if (WSAStartup(0x0101, &wsaData)) {
|
---|
156 | Log(("vboxVMInfoThread: WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError())));
|
---|
157 | return -1;
|
---|
158 | }
|
---|
159 |
|
---|
160 | do
|
---|
161 | {
|
---|
162 | if (dwCnt++ < 5)
|
---|
163 | {
|
---|
164 | /* Sleep a bit to not eat too much CPU. */
|
---|
165 | if (NULL == pCtx->pEnv->hStopEvent)
|
---|
166 | Log(("vboxVMInfoThread: Invalid stop event!\n"));
|
---|
167 |
|
---|
168 | if (WaitForSingleObject (pCtx->pEnv->hStopEvent, 1000) == WAIT_OBJECT_0)
|
---|
169 | {
|
---|
170 | Log(("vboxVMInfoThread: Got stop event, terminating ...\n"));
|
---|
171 | fTerminate = true;
|
---|
172 | break;
|
---|
173 | }
|
---|
174 |
|
---|
175 | continue;
|
---|
176 | }
|
---|
177 |
|
---|
178 | dwCnt = 0;
|
---|
179 |
|
---|
180 | vboxVMInfoOS(pCtx);
|
---|
181 | vboxVMInfoAdditions(pCtx);
|
---|
182 | vboxVMInfoNet(pCtx);
|
---|
183 | vboxVMInfoUser(pCtx);
|
---|
184 |
|
---|
185 | if (pCtx->fFirstRun)
|
---|
186 | pCtx->fFirstRun = FALSE;
|
---|
187 | }
|
---|
188 | while (!fTerminate);
|
---|
189 |
|
---|
190 | WSACleanup();
|
---|
191 | return 0;
|
---|
192 | }
|
---|
193 |
|
---|