1 | /* $Id: VBoxVMInfo.cpp 11982 2008-09-02 13:09:44Z 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 | if((!a_pCtx) || (!a_pszKey))
|
---|
26 | return VERR_INVALID_PARAMETER;
|
---|
27 |
|
---|
28 | char szKeyTemp [_MAX_PATH] = {0};
|
---|
29 | char *pszValue = NULL;
|
---|
30 |
|
---|
31 | /* Append base path. */
|
---|
32 | RTStrPrintf(szKeyTemp, sizeof(szKeyTemp), "/VirtualBox/%s", a_pszKey); /** @todo r=bird: Why didn't you hardcode this into the strings before calling this function? */
|
---|
33 |
|
---|
34 | if (a_pszValue != NULL)
|
---|
35 | {
|
---|
36 | rc = RTStrCurrentCPToUtf8(&pszValue, a_pszValue);
|
---|
37 | if (!RT_SUCCESS(rc)) {
|
---|
38 | LogRel(("vboxVMInfoThread: Failed to convert the value name \"%s\" to Utf8! Error: %Rrc\n", a_pszValue, rc));
|
---|
39 | goto cleanup;
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | rc = VbglR3GuestPropWriteValue(a_pCtx->iInfoSvcClientID, szKeyTemp, (a_pszValue == NULL) ? NULL : pszValue);
|
---|
44 | if (!RT_SUCCESS(rc))
|
---|
45 | {
|
---|
46 | LogRel(("vboxVMInfoThread: Failed to store the property \"%s\"=\"%s\"! ClientID: %d, Error: %Rrc\n", szKeyTemp, pszValue, a_pCtx->iInfoSvcClientID, rc));
|
---|
47 | goto cleanup;
|
---|
48 | }
|
---|
49 |
|
---|
50 | if (pszValue != NULL)
|
---|
51 | Log(("vboxVMInfoThread: Property written: %s = %s\n", szKeyTemp, pszValue));
|
---|
52 | else
|
---|
53 | Log(("vboxVMInfoThread: Property deleted: %s\n", szKeyTemp));
|
---|
54 |
|
---|
55 | cleanup:
|
---|
56 |
|
---|
57 | RTStrFree(pszValue);
|
---|
58 | return rc;
|
---|
59 | }
|
---|
60 |
|
---|
61 | int vboxVMInfoWritePropInt(VBOXINFORMATIONCONTEXT* a_pCtx, char *a_pszKey, int a_iValue )
|
---|
62 | {
|
---|
63 | char szBuffer[_MAX_PATH] = {0}; /** @todo r=bird: this is a bit excessive (the size) */
|
---|
64 | itoa(a_iValue, szBuffer, 10);
|
---|
65 |
|
---|
66 | return vboxVMInfoWriteProp(a_pCtx, a_pszKey, szBuffer);
|
---|
67 | }
|
---|
68 |
|
---|
69 | int vboxVMInfoInit(const VBOXSERVICEENV *pEnv, void **ppInstance, bool *pfStartThread)
|
---|
70 | {
|
---|
71 | Log(("vboxVMInfoThread: Init.\n"));
|
---|
72 |
|
---|
73 | gCtx.pEnv = pEnv;
|
---|
74 | gCtx.fFirstRun = TRUE;
|
---|
75 |
|
---|
76 | int rc = VbglR3GuestPropConnect(&gCtx.iInfoSvcClientID);
|
---|
77 | if (!RT_SUCCESS(rc))
|
---|
78 | LogRel(("vboxVMInfoThread: Failed to connect to the guest property service! Error: %Rrc\n", rc));
|
---|
79 | else
|
---|
80 | {
|
---|
81 | LogRel(("vboxVMInfoThread: GuestProp ClientID = %d\n", gCtx.iInfoSvcClientID));
|
---|
82 |
|
---|
83 | /* Loading dynamic APIs. */
|
---|
84 | HMODULE hKernel32 = LoadLibrary(_T("kernel32"));
|
---|
85 | if (NULL != hKernel32)
|
---|
86 | {
|
---|
87 | gCtx.pfnWTSGetActiveConsoleSessionId = NULL;
|
---|
88 | gCtx.pfnWTSGetActiveConsoleSessionId = (fnWTSGetActiveConsoleSessionId)GetProcAddress(hKernel32, "WTSGetActiveConsoleSessionId");
|
---|
89 |
|
---|
90 | FreeLibrary(hKernel32);
|
---|
91 | }
|
---|
92 |
|
---|
93 | *pfStartThread = true;
|
---|
94 | *ppInstance = &gCtx;
|
---|
95 | }
|
---|
96 |
|
---|
97 | return 0;
|
---|
98 | }
|
---|
99 |
|
---|
100 | void vboxVMInfoDestroy(const VBOXSERVICEENV *pEnv, void *pInstance)
|
---|
101 | {
|
---|
102 | VBOXINFORMATIONCONTEXT *pCtx = (VBOXINFORMATIONCONTEXT *)pInstance;
|
---|
103 |
|
---|
104 | int rc = VbglR3GuestPropDisconnect(pCtx->iInfoSvcClientID);
|
---|
105 | if (!RT_SUCCESS(rc))
|
---|
106 | LogRel(("vboxVMInfoThread: Failed to disconnect from guest property service! Error: %Rrc\n", rc));
|
---|
107 |
|
---|
108 | Log(("vboxVMInfoThread: Destroyed.\n"));
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | unsigned __stdcall vboxVMInfoThread(void *pInstance)
|
---|
113 | {
|
---|
114 | VBOXINFORMATIONCONTEXT *pCtx = (VBOXINFORMATIONCONTEXT *)pInstance;
|
---|
115 | bool fTerminate = false;
|
---|
116 |
|
---|
117 | Log(("vboxVMInfoThread: Started.\n"));
|
---|
118 |
|
---|
119 | if (NULL == pCtx) {
|
---|
120 | Log(("vboxVMInfoThread: Invalid context!\n"));
|
---|
121 | return -1;
|
---|
122 | }
|
---|
123 |
|
---|
124 | WSADATA wsaData;
|
---|
125 | DWORD cbReturned = 0;
|
---|
126 | DWORD dwCnt = 5;
|
---|
127 |
|
---|
128 | /* Required for network information. */
|
---|
129 | if (WSAStartup(0x0101, &wsaData)) {
|
---|
130 | Log(("vboxVMInfoThread: WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError())));
|
---|
131 | return -1;
|
---|
132 | }
|
---|
133 |
|
---|
134 | do
|
---|
135 | {
|
---|
136 | if (dwCnt++ < 5)
|
---|
137 | {
|
---|
138 | /* Sleep a bit to not eat too much CPU. */
|
---|
139 | if (NULL == pCtx->pEnv->hStopEvent)
|
---|
140 | Log(("vboxVMInfoThread: Invalid stop event!\n"));
|
---|
141 |
|
---|
142 | if (WaitForSingleObject (pCtx->pEnv->hStopEvent, 1000) == WAIT_OBJECT_0)
|
---|
143 | {
|
---|
144 | Log(("vboxVMInfoThread: Got stop event, terminating ...\n"));
|
---|
145 | fTerminate = true;
|
---|
146 | break;
|
---|
147 | }
|
---|
148 |
|
---|
149 | continue;
|
---|
150 | }
|
---|
151 |
|
---|
152 | dwCnt = 0;
|
---|
153 |
|
---|
154 | vboxVMInfoOS(pCtx);
|
---|
155 | vboxVMInfoAdditions(pCtx);
|
---|
156 | vboxVMInfoNet(pCtx);
|
---|
157 | vboxVMInfoUser(pCtx);
|
---|
158 |
|
---|
159 | if (pCtx->fFirstRun)
|
---|
160 | pCtx->fFirstRun = FALSE;
|
---|
161 | }
|
---|
162 | while (!fTerminate);
|
---|
163 |
|
---|
164 | WSACleanup();
|
---|
165 | return 0;
|
---|
166 | }
|
---|
167 |
|
---|