1 | /* $Id: VBoxServiceVMInfo.cpp 43230 2012-09-06 15:34:30Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Virtual Machine Information for the Host.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. 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 | /*******************************************************************************
|
---|
21 | * Header Files *
|
---|
22 | *******************************************************************************/
|
---|
23 | #ifdef RT_OS_WINDOWS
|
---|
24 | # ifdef TARGET_NT4 /* HACK ALERT! PMIB_IPSTATS undefined if 0x0400 with newer SDKs. */
|
---|
25 | # undef _WIN32_WINNT
|
---|
26 | # define _WIN32_WINNT 0x0500
|
---|
27 | # endif
|
---|
28 | # include <winsock2.h>
|
---|
29 | # include <iphlpapi.h>
|
---|
30 | # include <ws2tcpip.h>
|
---|
31 | # include <windows.h>
|
---|
32 | # include <Ntsecapi.h>
|
---|
33 | #else
|
---|
34 | # define __STDC_LIMIT_MACROS
|
---|
35 | # include <arpa/inet.h>
|
---|
36 | # include <errno.h>
|
---|
37 | # include <netinet/in.h>
|
---|
38 | # include <sys/ioctl.h>
|
---|
39 | # include <sys/socket.h>
|
---|
40 | # include <net/if.h>
|
---|
41 | # include <unistd.h>
|
---|
42 | # ifndef RT_OS_OS2
|
---|
43 | # ifndef RT_OS_FREEBSD
|
---|
44 | # include <utmpx.h> /* @todo FreeBSD 9 should have this. */
|
---|
45 | # endif
|
---|
46 | # endif
|
---|
47 | # ifdef RT_OS_SOLARIS
|
---|
48 | # include <sys/sockio.h>
|
---|
49 | # include <net/if_arp.h>
|
---|
50 | # endif
|
---|
51 | # ifdef RT_OS_FREEBSD
|
---|
52 | # include <ifaddrs.h> /* getifaddrs, freeifaddrs */
|
---|
53 | # include <net/if_dl.h> /* LLADDR */
|
---|
54 | # include <netdb.h> /* getnameinfo */
|
---|
55 | # endif
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | #include <iprt/mem.h>
|
---|
59 | #include <iprt/thread.h>
|
---|
60 | #include <iprt/string.h>
|
---|
61 | #include <iprt/semaphore.h>
|
---|
62 | #include <iprt/system.h>
|
---|
63 | #include <iprt/time.h>
|
---|
64 | #include <iprt/assert.h>
|
---|
65 | #include <VBox/version.h>
|
---|
66 | #include <VBox/VBoxGuestLib.h>
|
---|
67 | #include "VBoxServiceInternal.h"
|
---|
68 | #include "VBoxServiceUtils.h"
|
---|
69 | #include "VBoxServicePropCache.h"
|
---|
70 |
|
---|
71 |
|
---|
72 | /*******************************************************************************
|
---|
73 | * Global Variables *
|
---|
74 | *******************************************************************************/
|
---|
75 | /** The vminfo interval (milliseconds). */
|
---|
76 | static uint32_t g_cMsVMInfoInterval = 0;
|
---|
77 | /** The semaphore we're blocking on. */
|
---|
78 | static RTSEMEVENTMULTI g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
79 | /** The guest property service client ID. */
|
---|
80 | static uint32_t g_uVMInfoGuestPropSvcClientID = 0;
|
---|
81 | /** Number of logged in users in OS. */
|
---|
82 | static uint32_t g_cVMInfoLoggedInUsers = UINT32_MAX;
|
---|
83 | /** The guest property cache. */
|
---|
84 | static VBOXSERVICEVEPROPCACHE g_VMInfoPropCache;
|
---|
85 | /** The VM session ID. Changes whenever the VM is restored or reset. */
|
---|
86 | static uint64_t g_idVMInfoSession;
|
---|
87 |
|
---|
88 |
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Signals the event so that a re-enumeration of VM-specific
|
---|
92 | * information (like logged in users) can happen.
|
---|
93 | *
|
---|
94 | * @return IPRT status code.
|
---|
95 | */
|
---|
96 | int VBoxServiceVMInfoSignal(void)
|
---|
97 | {
|
---|
98 | /* Trigger a re-enumeration of all logged-in users by unblocking
|
---|
99 | * the multi event semaphore of the VMInfo thread. */
|
---|
100 | if (g_hVMInfoEvent)
|
---|
101 | return RTSemEventMultiSignal(g_hVMInfoEvent);
|
---|
102 |
|
---|
103 | return VINF_SUCCESS;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
108 | static DECLCALLBACK(int) VBoxServiceVMInfoPreInit(void)
|
---|
109 | {
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
115 | static DECLCALLBACK(int) VBoxServiceVMInfoOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
116 | {
|
---|
117 | int rc = -1;
|
---|
118 | if (ppszShort)
|
---|
119 | /* no short options */;
|
---|
120 | else if (!strcmp(argv[*pi], "--vminfo-interval"))
|
---|
121 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
122 | &g_cMsVMInfoInterval, 1, UINT32_MAX - 1);
|
---|
123 | return rc;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
128 | static DECLCALLBACK(int) VBoxServiceVMInfoInit(void)
|
---|
129 | {
|
---|
130 | /*
|
---|
131 | * If not specified, find the right interval default.
|
---|
132 | * Then create the event sem to block on.
|
---|
133 | */
|
---|
134 | if (!g_cMsVMInfoInterval)
|
---|
135 | g_cMsVMInfoInterval = g_DefaultInterval * 1000;
|
---|
136 | if (!g_cMsVMInfoInterval)
|
---|
137 | g_cMsVMInfoInterval = 10 * 1000;
|
---|
138 |
|
---|
139 | int rc = RTSemEventMultiCreate(&g_hVMInfoEvent);
|
---|
140 | AssertRCReturn(rc, rc);
|
---|
141 |
|
---|
142 | VbglR3GetSessionId(&g_idVMInfoSession);
|
---|
143 | /* The status code is ignored as this information is not available with VBox < 3.2.10. */
|
---|
144 |
|
---|
145 | rc = VbglR3GuestPropConnect(&g_uVMInfoGuestPropSvcClientID);
|
---|
146 | if (RT_SUCCESS(rc))
|
---|
147 | VBoxServiceVerbose(3, "VMInfo: Property Service Client ID: %#x\n", g_uVMInfoGuestPropSvcClientID);
|
---|
148 | else
|
---|
149 | {
|
---|
150 | /* If the service was not found, we disable this service without
|
---|
151 | causing VBoxService to fail. */
|
---|
152 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
|
---|
153 | {
|
---|
154 | VBoxServiceVerbose(0, "VMInfo: Guest property service is not available, disabling the service\n");
|
---|
155 | rc = VERR_SERVICE_DISABLED;
|
---|
156 | }
|
---|
157 | else
|
---|
158 | VBoxServiceError("VMInfo: Failed to connect to the guest property service! Error: %Rrc\n", rc);
|
---|
159 | RTSemEventMultiDestroy(g_hVMInfoEvent);
|
---|
160 | g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
161 | }
|
---|
162 |
|
---|
163 | if (RT_SUCCESS(rc))
|
---|
164 | {
|
---|
165 | VBoxServicePropCacheCreate(&g_VMInfoPropCache, g_uVMInfoGuestPropSvcClientID);
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * Declare some guest properties with flags and reset values.
|
---|
169 | */
|
---|
170 | VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList",
|
---|
171 | VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_TRANSIENT, NULL /* Delete on exit */);
|
---|
172 | VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsers",
|
---|
173 | VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_TRANSIENT, "0");
|
---|
174 | VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers",
|
---|
175 | VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_TRANSIENT, "true");
|
---|
176 | VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/Net/Count",
|
---|
177 | VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_ALWAYS_UPDATE, NULL /* Delete on exit */);
|
---|
178 | }
|
---|
179 | return rc;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Writes the properties that won't change while the service is running.
|
---|
185 | *
|
---|
186 | * Errors are ignored.
|
---|
187 | */
|
---|
188 | static void vboxserviceVMInfoWriteFixedProperties(void)
|
---|
189 | {
|
---|
190 | /*
|
---|
191 | * First get OS information that won't change.
|
---|
192 | */
|
---|
193 | char szInfo[256];
|
---|
194 | int rc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szInfo, sizeof(szInfo));
|
---|
195 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Product",
|
---|
196 | "%s", RT_FAILURE(rc) ? "" : szInfo);
|
---|
197 |
|
---|
198 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szInfo, sizeof(szInfo));
|
---|
199 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Release",
|
---|
200 | "%s", RT_FAILURE(rc) ? "" : szInfo);
|
---|
201 |
|
---|
202 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szInfo, sizeof(szInfo));
|
---|
203 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Version",
|
---|
204 | "%s", RT_FAILURE(rc) ? "" : szInfo);
|
---|
205 |
|
---|
206 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szInfo, sizeof(szInfo));
|
---|
207 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/ServicePack",
|
---|
208 | "%s", RT_FAILURE(rc) ? "" : szInfo);
|
---|
209 |
|
---|
210 | /*
|
---|
211 | * Retrieve version information about Guest Additions and installed files (components).
|
---|
212 | */
|
---|
213 | char *pszAddVer;
|
---|
214 | char *pszAddVerExt;
|
---|
215 | char *pszAddRev;
|
---|
216 | rc = VbglR3GetAdditionsVersion(&pszAddVer, &pszAddVerExt, &pszAddRev);
|
---|
217 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Version",
|
---|
218 | "%s", RT_FAILURE(rc) ? "" : pszAddVer);
|
---|
219 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/VersionExt",
|
---|
220 | "%s", RT_FAILURE(rc) ? "" : pszAddVerExt);
|
---|
221 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Revision",
|
---|
222 | "%s", RT_FAILURE(rc) ? "" : pszAddRev);
|
---|
223 | if (RT_SUCCESS(rc))
|
---|
224 | {
|
---|
225 | RTStrFree(pszAddVer);
|
---|
226 | RTStrFree(pszAddVerExt);
|
---|
227 | RTStrFree(pszAddRev);
|
---|
228 | }
|
---|
229 |
|
---|
230 | #ifdef RT_OS_WINDOWS
|
---|
231 | /*
|
---|
232 | * Do windows specific properties.
|
---|
233 | */
|
---|
234 | char *pszInstDir;
|
---|
235 | rc = VbglR3GetAdditionsInstallationPath(&pszInstDir);
|
---|
236 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/InstallDir",
|
---|
237 | "%s", RT_FAILURE(rc) ? "" : pszInstDir);
|
---|
238 | if (RT_SUCCESS(rc))
|
---|
239 | RTStrFree(pszInstDir);
|
---|
240 |
|
---|
241 | VBoxServiceWinGetComponentVersions(g_uVMInfoGuestPropSvcClientID);
|
---|
242 | #endif
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Provide information about active users.
|
---|
248 | */
|
---|
249 | static int vboxserviceVMInfoWriteUsers(void)
|
---|
250 | {
|
---|
251 | int rc = VINF_SUCCESS;
|
---|
252 | char *pszUserList = NULL;
|
---|
253 | uint32_t cUsersInList = 0;
|
---|
254 |
|
---|
255 | #ifdef RT_OS_WINDOWS
|
---|
256 | # ifndef TARGET_NT4
|
---|
257 | rc = VBoxServiceVMInfoWinWriteUsers(&pszUserList, &cUsersInList);
|
---|
258 | # else
|
---|
259 | rc = VERR_NOT_IMPLEMENTED;
|
---|
260 | # endif
|
---|
261 |
|
---|
262 | #elif defined(RT_OS_FREEBSD)
|
---|
263 | /** @todo FreeBSD: Port logged on user info retrieval.
|
---|
264 | * However, FreeBSD 9 supports utmpx, so we could use the code
|
---|
265 | * block below (?). */
|
---|
266 | rc = VERR_NOT_IMPLEMENTED;
|
---|
267 |
|
---|
268 | #elif defined(RT_OS_OS2)
|
---|
269 | /** @todo OS/2: Port logged on (LAN/local/whatever) user info retrieval. */
|
---|
270 | rc = VERR_NOT_IMPLEMENTED;
|
---|
271 |
|
---|
272 | #else
|
---|
273 | setutxent();
|
---|
274 | utmpx *ut_user;
|
---|
275 | uint32_t cListSize = 32;
|
---|
276 |
|
---|
277 | /* Allocate a first array to hold 32 users max. */
|
---|
278 | char **papszUsers = (char **)RTMemAllocZ(cListSize * sizeof(char *));
|
---|
279 | if (papszUsers == NULL)
|
---|
280 | rc = VERR_NO_MEMORY;
|
---|
281 |
|
---|
282 | /* Process all entries in the utmp file. */
|
---|
283 | while ( (ut_user = getutxent())
|
---|
284 | && RT_SUCCESS(rc))
|
---|
285 | {
|
---|
286 | VBoxServiceVerbose(4, "Found logged in user \"%s\" (type: %d)\n",
|
---|
287 | ut_user->ut_user, ut_user->ut_type);
|
---|
288 | if (cUsersInList > cListSize)
|
---|
289 | {
|
---|
290 | cListSize += 32;
|
---|
291 | void *pvNew = RTMemRealloc(papszUsers, cListSize * sizeof(char*));
|
---|
292 | AssertPtrBreakStmt(pvNew, cListSize -= 32);
|
---|
293 | papszUsers = (char **)pvNew;
|
---|
294 | }
|
---|
295 |
|
---|
296 | /* Make sure we don't add user names which are not
|
---|
297 | * part of type USER_PROCESS. */
|
---|
298 | if (ut_user->ut_type == USER_PROCESS)
|
---|
299 | {
|
---|
300 | bool fFound = false;
|
---|
301 | for (uint32_t i = 0; i < cUsersInList && !fFound; i++)
|
---|
302 | fFound = strcmp(papszUsers[i], ut_user->ut_user) == 0;
|
---|
303 |
|
---|
304 | if (!fFound)
|
---|
305 | {
|
---|
306 | VBoxServiceVerbose(4, "Adding user \"%s\" (type: %d) to list\n",
|
---|
307 | ut_user->ut_user, ut_user->ut_type);
|
---|
308 |
|
---|
309 | rc = RTStrDupEx(&papszUsers[cUsersInList], (const char *)ut_user->ut_user);
|
---|
310 | if (RT_FAILURE(rc))
|
---|
311 | break;
|
---|
312 | cUsersInList++;
|
---|
313 | }
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | /* Calc the string length. */
|
---|
318 | size_t cchUserList = 0;
|
---|
319 | for (uint32_t i = 0; i < cUsersInList; i++)
|
---|
320 | cchUserList += (i != 0) + strlen(papszUsers[i]);
|
---|
321 |
|
---|
322 | /* Build the user list. */
|
---|
323 | rc = RTStrAllocEx(&pszUserList, cchUserList + 1);
|
---|
324 | if (RT_SUCCESS(rc))
|
---|
325 | {
|
---|
326 | char *psz = pszUserList;
|
---|
327 | for (uint32_t i = 0; i < cUsersInList; i++)
|
---|
328 | {
|
---|
329 | if (i != 0)
|
---|
330 | *psz++ = ',';
|
---|
331 | size_t cch = strlen(papszUsers[i]);
|
---|
332 | memcpy(psz, papszUsers[i], cch);
|
---|
333 | psz += cch;
|
---|
334 | }
|
---|
335 | *psz = '\0';
|
---|
336 | }
|
---|
337 |
|
---|
338 | /* Cleanup. */
|
---|
339 | for (uint32_t i = 0; i < cUsersInList; i++)
|
---|
340 | RTStrFree(papszUsers[i]);
|
---|
341 | RTMemFree(papszUsers);
|
---|
342 |
|
---|
343 | endutxent(); /* Close utmpx file. */
|
---|
344 | #endif
|
---|
345 | Assert(RT_FAILURE(rc) || cUsersInList == 0 || (pszUserList && *pszUserList));
|
---|
346 |
|
---|
347 | /* If the user enumeration above failed, reset the user count to 0 except
|
---|
348 | * we didn't have enough memory anymore. In that case we want to preserve
|
---|
349 | * the previous user count in order to not confuse third party tools which
|
---|
350 | * rely on that count. */
|
---|
351 | if (RT_FAILURE(rc))
|
---|
352 | {
|
---|
353 | if (rc == VERR_NO_MEMORY)
|
---|
354 | {
|
---|
355 | static int s_iVMInfoBitchedOOM = 0;
|
---|
356 | if (s_iVMInfoBitchedOOM++ < 3)
|
---|
357 | VBoxServiceVerbose(0, "Warning: Not enough memory available to enumerate users! Keeping old value (%u)\n",
|
---|
358 | g_cVMInfoLoggedInUsers);
|
---|
359 | cUsersInList = g_cVMInfoLoggedInUsers;
|
---|
360 | }
|
---|
361 | else
|
---|
362 | cUsersInList = 0;
|
---|
363 | }
|
---|
364 |
|
---|
365 | VBoxServiceVerbose(4, "cUsersInList=%RU32, pszUserList=%s, rc=%Rrc\n",
|
---|
366 | cUsersInList, pszUserList ? pszUserList : "<NULL>", rc);
|
---|
367 |
|
---|
368 | if (pszUserList && cUsersInList > 0)
|
---|
369 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", "%s", pszUserList);
|
---|
370 | else
|
---|
371 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", NULL);
|
---|
372 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsers", "%u", cUsersInList);
|
---|
373 | if (g_cVMInfoLoggedInUsers != cUsersInList)
|
---|
374 | {
|
---|
375 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers",
|
---|
376 | cUsersInList == 0 ? "true" : "false");
|
---|
377 | g_cVMInfoLoggedInUsers = cUsersInList;
|
---|
378 | }
|
---|
379 | if (RT_SUCCESS(rc) && pszUserList)
|
---|
380 | RTStrFree(pszUserList);
|
---|
381 | return rc;
|
---|
382 | }
|
---|
383 |
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Provide information about the guest network.
|
---|
387 | */
|
---|
388 | static int vboxserviceVMInfoWriteNetwork(void)
|
---|
389 | {
|
---|
390 | int rc = VINF_SUCCESS;
|
---|
391 | uint32_t cIfacesReport = 0;
|
---|
392 | char szPropPath[256];
|
---|
393 |
|
---|
394 | #ifdef RT_OS_WINDOWS
|
---|
395 | IP_ADAPTER_INFO *pAdpInfo = NULL;
|
---|
396 |
|
---|
397 | # ifndef TARGET_NT4
|
---|
398 | ULONG cbAdpInfo = sizeof(*pAdpInfo);
|
---|
399 | pAdpInfo = (IP_ADAPTER_INFO *)RTMemAlloc(cbAdpInfo);
|
---|
400 | if (!pAdpInfo)
|
---|
401 | {
|
---|
402 | VBoxServiceError("VMInfo/Network: Failed to allocate IP_ADAPTER_INFO\n");
|
---|
403 | return VERR_NO_MEMORY;
|
---|
404 | }
|
---|
405 | DWORD dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
|
---|
406 | if (dwRet == ERROR_BUFFER_OVERFLOW)
|
---|
407 | {
|
---|
408 | IP_ADAPTER_INFO *pAdpInfoNew = (IP_ADAPTER_INFO*)RTMemRealloc(pAdpInfo, cbAdpInfo);
|
---|
409 | if (pAdpInfoNew)
|
---|
410 | {
|
---|
411 | pAdpInfo = pAdpInfoNew;
|
---|
412 | dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
|
---|
413 | }
|
---|
414 | }
|
---|
415 | else if (dwRet == ERROR_NO_DATA)
|
---|
416 | {
|
---|
417 | VBoxServiceVerbose(3, "VMInfo/Network: No network adapters available\n");
|
---|
418 |
|
---|
419 | /* If no network adapters available / present in the
|
---|
420 | * system we pretend success to not bail out too early. */
|
---|
421 | dwRet = ERROR_SUCCESS;
|
---|
422 | }
|
---|
423 |
|
---|
424 | if (dwRet != ERROR_SUCCESS)
|
---|
425 | {
|
---|
426 | if (pAdpInfo)
|
---|
427 | RTMemFree(pAdpInfo);
|
---|
428 | VBoxServiceError("VMInfo/Network: Failed to get adapter info: Error %d\n", dwRet);
|
---|
429 | return RTErrConvertFromWin32(dwRet);
|
---|
430 | }
|
---|
431 | # endif /* !TARGET_NT4 */
|
---|
432 |
|
---|
433 | SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
|
---|
434 | if (sd == SOCKET_ERROR) /* Socket invalid. */
|
---|
435 | {
|
---|
436 | int wsaErr = WSAGetLastError();
|
---|
437 | /* Don't complain/bail out with an error if network stack is not up; can happen
|
---|
438 | * on NT4 due to start up when not connected shares dialogs pop up. */
|
---|
439 | if (WSAENETDOWN == wsaErr)
|
---|
440 | {
|
---|
441 | VBoxServiceVerbose(0, "VMInfo/Network: Network is not up yet.\n");
|
---|
442 | wsaErr = VINF_SUCCESS;
|
---|
443 | }
|
---|
444 | else
|
---|
445 | VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %d\n", wsaErr);
|
---|
446 | if (pAdpInfo)
|
---|
447 | RTMemFree(pAdpInfo);
|
---|
448 | return RTErrConvertFromWin32(wsaErr);
|
---|
449 | }
|
---|
450 |
|
---|
451 | INTERFACE_INFO InterfaceList[20] = {0};
|
---|
452 | unsigned long nBytesReturned = 0;
|
---|
453 | if (WSAIoctl(sd,
|
---|
454 | SIO_GET_INTERFACE_LIST,
|
---|
455 | 0,
|
---|
456 | 0,
|
---|
457 | &InterfaceList,
|
---|
458 | sizeof(InterfaceList),
|
---|
459 | &nBytesReturned,
|
---|
460 | 0,
|
---|
461 | 0) == SOCKET_ERROR)
|
---|
462 | {
|
---|
463 | VBoxServiceError("VMInfo/Network: Failed to WSAIoctl() on socket: Error: %d\n", WSAGetLastError());
|
---|
464 | if (pAdpInfo)
|
---|
465 | RTMemFree(pAdpInfo);
|
---|
466 | return RTErrConvertFromWin32(WSAGetLastError());
|
---|
467 | }
|
---|
468 | int cIfacesSystem = nBytesReturned / sizeof(INTERFACE_INFO);
|
---|
469 |
|
---|
470 | /** @todo Use GetAdaptersInfo() and GetAdapterAddresses (IPv4 + IPv6) for more information. */
|
---|
471 | for (int i = 0; i < cIfacesSystem; ++i)
|
---|
472 | {
|
---|
473 | sockaddr_in *pAddress;
|
---|
474 | u_long nFlags = 0;
|
---|
475 | if (InterfaceList[i].iiFlags & IFF_LOOPBACK) /* Skip loopback device. */
|
---|
476 | continue;
|
---|
477 | nFlags = InterfaceList[i].iiFlags;
|
---|
478 | pAddress = (sockaddr_in *)&(InterfaceList[i].iiAddress);
|
---|
479 | Assert(pAddress);
|
---|
480 | char szIp[32];
|
---|
481 | RTStrPrintf(szIp, sizeof(szIp), "%s", inet_ntoa(pAddress->sin_addr));
|
---|
482 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/IP", cIfacesReport);
|
---|
483 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szIp);
|
---|
484 |
|
---|
485 | pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress);
|
---|
486 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Broadcast", cIfacesReport);
|
---|
487 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
488 |
|
---|
489 | pAddress = (sockaddr_in *)&(InterfaceList[i].iiNetmask);
|
---|
490 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Netmask", cIfacesReport);
|
---|
491 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
492 |
|
---|
493 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/Status", cIfacesReport);
|
---|
494 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, nFlags & IFF_UP ? "Up" : "Down");
|
---|
495 |
|
---|
496 | # ifndef TARGET_NT4
|
---|
497 | IP_ADAPTER_INFO *pAdp;
|
---|
498 | for (pAdp = pAdpInfo; pAdp; pAdp = pAdp->Next)
|
---|
499 | if (!strcmp(pAdp->IpAddressList.IpAddress.String, szIp))
|
---|
500 | break;
|
---|
501 |
|
---|
502 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/MAC", cIfacesReport);
|
---|
503 | if (pAdp)
|
---|
504 | {
|
---|
505 | char szMac[32];
|
---|
506 | RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
|
---|
507 | pAdp->Address[0], pAdp->Address[1], pAdp->Address[2],
|
---|
508 | pAdp->Address[3], pAdp->Address[4], pAdp->Address[5]);
|
---|
509 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
|
---|
510 | }
|
---|
511 | else
|
---|
512 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, NULL);
|
---|
513 | # endif /* !TARGET_NT4 */
|
---|
514 |
|
---|
515 | cIfacesReport++;
|
---|
516 | }
|
---|
517 | if (pAdpInfo)
|
---|
518 | RTMemFree(pAdpInfo);
|
---|
519 | if (sd >= 0)
|
---|
520 | closesocket(sd);
|
---|
521 |
|
---|
522 | #elif defined(RT_OS_FREEBSD)
|
---|
523 | struct ifaddrs *pIfHead = NULL;
|
---|
524 |
|
---|
525 | /* Get all available interfaces */
|
---|
526 | rc = getifaddrs(&pIfHead);
|
---|
527 | if (rc < 0)
|
---|
528 | {
|
---|
529 | rc = RTErrConvertFromErrno(errno);
|
---|
530 | VBoxServiceError("VMInfo/Network: Failed to get all interfaces: Error %Rrc\n");
|
---|
531 | return rc;
|
---|
532 | }
|
---|
533 |
|
---|
534 | /* Loop through all interfaces and set the data. */
|
---|
535 | for (struct ifaddrs *pIfCurr = pIfHead; pIfCurr; pIfCurr = pIfCurr->ifa_next)
|
---|
536 | {
|
---|
537 | /*
|
---|
538 | * Only AF_INET and no loopback interfaces
|
---|
539 | * @todo: IPv6 interfaces
|
---|
540 | */
|
---|
541 | if ( pIfCurr->ifa_addr->sa_family == AF_INET
|
---|
542 | && !(pIfCurr->ifa_flags & IFF_LOOPBACK))
|
---|
543 | {
|
---|
544 | char szInetAddr[NI_MAXHOST];
|
---|
545 |
|
---|
546 | memset(szInetAddr, 0, NI_MAXHOST);
|
---|
547 | getnameinfo(pIfCurr->ifa_addr, sizeof(struct sockaddr_in),
|
---|
548 | szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
---|
549 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/IP", cIfacesReport);
|
---|
550 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
|
---|
551 |
|
---|
552 | memset(szInetAddr, 0, NI_MAXHOST);
|
---|
553 | getnameinfo(pIfCurr->ifa_broadaddr, sizeof(struct sockaddr_in),
|
---|
554 | szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
---|
555 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Broadcast", cIfacesReport);
|
---|
556 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
|
---|
557 |
|
---|
558 | memset(szInetAddr, 0, NI_MAXHOST);
|
---|
559 | getnameinfo(pIfCurr->ifa_netmask, sizeof(struct sockaddr_in),
|
---|
560 | szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
---|
561 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Netmask", cIfacesReport);
|
---|
562 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
|
---|
563 |
|
---|
564 | /* Search for the AF_LINK interface of the current AF_INET one and get the mac. */
|
---|
565 | for (struct ifaddrs *pIfLinkCurr = pIfHead; pIfLinkCurr; pIfLinkCurr = pIfLinkCurr->ifa_next)
|
---|
566 | {
|
---|
567 | if ( pIfLinkCurr->ifa_addr->sa_family == AF_LINK
|
---|
568 | && !strcmp(pIfCurr->ifa_name, pIfLinkCurr->ifa_name))
|
---|
569 | {
|
---|
570 | char szMac[32];
|
---|
571 | uint8_t *pu8Mac = NULL;
|
---|
572 | struct sockaddr_dl *pLinkAddress = (struct sockaddr_dl *)pIfLinkCurr->ifa_addr;
|
---|
573 |
|
---|
574 | AssertPtr(pLinkAddress);
|
---|
575 | pu8Mac = (uint8_t *)LLADDR(pLinkAddress);
|
---|
576 | RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
|
---|
577 | pu8Mac[0], pu8Mac[1], pu8Mac[2], pu8Mac[3], pu8Mac[4], pu8Mac[5]);
|
---|
578 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/MAC", cIfacesReport);
|
---|
579 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
|
---|
580 | break;
|
---|
581 | }
|
---|
582 | }
|
---|
583 |
|
---|
584 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/Status", cIfacesReport);
|
---|
585 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, pIfCurr->ifa_flags & IFF_UP ? "Up" : "Down");
|
---|
586 |
|
---|
587 | cIfacesReport++;
|
---|
588 | }
|
---|
589 | }
|
---|
590 |
|
---|
591 | /* Free allocated resources. */
|
---|
592 | freeifaddrs(pIfHead);
|
---|
593 |
|
---|
594 | #else /* !RT_OS_WINDOWS && !RT_OS_FREEBSD */
|
---|
595 | int sd = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
596 | if (sd < 0)
|
---|
597 | {
|
---|
598 | rc = RTErrConvertFromErrno(errno);
|
---|
599 | VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %Rrc\n", rc);
|
---|
600 | return rc;
|
---|
601 | }
|
---|
602 |
|
---|
603 | ifconf ifcfg;
|
---|
604 | char buffer[1024] = {0};
|
---|
605 | ifcfg.ifc_len = sizeof(buffer);
|
---|
606 | ifcfg.ifc_buf = buffer;
|
---|
607 | if (ioctl(sd, SIOCGIFCONF, &ifcfg) < 0)
|
---|
608 | {
|
---|
609 | close(sd);
|
---|
610 | rc = RTErrConvertFromErrno(errno);
|
---|
611 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFCONF) on socket: Error %Rrc\n", rc);
|
---|
612 | return rc;
|
---|
613 | }
|
---|
614 |
|
---|
615 | ifreq* ifrequest = ifcfg.ifc_req;
|
---|
616 | int cIfacesSystem = ifcfg.ifc_len / sizeof(ifreq);
|
---|
617 |
|
---|
618 | for (int i = 0; i < cIfacesSystem; ++i)
|
---|
619 | {
|
---|
620 | sockaddr_in *pAddress;
|
---|
621 | if (ioctl(sd, SIOCGIFFLAGS, &ifrequest[i]) < 0)
|
---|
622 | {
|
---|
623 | rc = RTErrConvertFromErrno(errno);
|
---|
624 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFFLAGS) on socket: Error %Rrc\n", rc);
|
---|
625 | break;
|
---|
626 | }
|
---|
627 | if (ifrequest[i].ifr_flags & IFF_LOOPBACK) /* Skip the loopback device. */
|
---|
628 | continue;
|
---|
629 |
|
---|
630 | bool fIfUp = !!(ifrequest[i].ifr_flags & IFF_UP);
|
---|
631 | pAddress = ((sockaddr_in *)&ifrequest[i].ifr_addr);
|
---|
632 | Assert(pAddress);
|
---|
633 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/IP", cIfacesReport);
|
---|
634 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
635 |
|
---|
636 | if (ioctl(sd, SIOCGIFBRDADDR, &ifrequest[i]) < 0)
|
---|
637 | {
|
---|
638 | rc = RTErrConvertFromErrno(errno);
|
---|
639 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %Rrc\n", rc);
|
---|
640 | break;
|
---|
641 | }
|
---|
642 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_broadaddr;
|
---|
643 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Broadcast", cIfacesReport);
|
---|
644 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
645 |
|
---|
646 | if (ioctl(sd, SIOCGIFNETMASK, &ifrequest[i]) < 0)
|
---|
647 | {
|
---|
648 | rc = RTErrConvertFromErrno(errno);
|
---|
649 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFNETMASK) on socket: Error %Rrc\n", rc);
|
---|
650 | break;
|
---|
651 | }
|
---|
652 | # if defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
|
---|
653 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_addr;
|
---|
654 | # else
|
---|
655 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_netmask;
|
---|
656 | # endif
|
---|
657 |
|
---|
658 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Netmask", cIfacesReport);
|
---|
659 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
660 |
|
---|
661 | # if defined(RT_OS_SOLARIS)
|
---|
662 | /*
|
---|
663 | * "ifreq" is obsolete on Solaris. We use the recommended "lifreq".
|
---|
664 | * We might fail if the interface has not been assigned an IP address.
|
---|
665 | * That doesn't matter; as long as it's plumbed we can pick it up.
|
---|
666 | * But, if it has not acquired an IP address we cannot obtain it's MAC
|
---|
667 | * address this way, so we just use all zeros there.
|
---|
668 | */
|
---|
669 | RTMAC IfMac;
|
---|
670 | RT_ZERO(IfMac);
|
---|
671 | struct lifreq IfReq;
|
---|
672 | RT_ZERO(IfReq);
|
---|
673 | AssertCompile(sizeof(IfReq.lifr_name) >= sizeof(ifrequest[i].ifr_name));
|
---|
674 | strncpy(IfReq.lifr_name, ifrequest[i].ifr_name, sizeof(ifrequest[i].ifr_name));
|
---|
675 | if (ioctl(sd, SIOCGLIFADDR, &IfReq) >= 0)
|
---|
676 | {
|
---|
677 | struct arpreq ArpReq;
|
---|
678 | RT_ZERO(ArpReq);
|
---|
679 | memcpy(&ArpReq.arp_pa, &IfReq.lifr_addr, sizeof(struct sockaddr_in));
|
---|
680 |
|
---|
681 | if (ioctl(sd, SIOCGARP, &ArpReq) >= 0)
|
---|
682 | memcpy(&IfMac, ArpReq.arp_ha.sa_data, sizeof(IfMac));
|
---|
683 | else
|
---|
684 | {
|
---|
685 | rc = RTErrConvertFromErrno(errno);
|
---|
686 | VBoxServiceError("VMInfo/Network: failed to ioctl(SIOCGARP) on socket: Error %Rrc\n", rc);
|
---|
687 | break;
|
---|
688 | }
|
---|
689 | }
|
---|
690 | else
|
---|
691 | {
|
---|
692 | VBoxServiceVerbose(2, "VMInfo/Network: Interface %d has no assigned IP address, skipping ...\n", i);
|
---|
693 | continue;
|
---|
694 | }
|
---|
695 | # else
|
---|
696 | # ifndef RT_OS_OS2 /** @todo port this to OS/2 */
|
---|
697 | if (ioctl(sd, SIOCGIFHWADDR, &ifrequest[i]) < 0)
|
---|
698 | {
|
---|
699 | rc = RTErrConvertFromErrno(errno);
|
---|
700 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFHWADDR) on socket: Error %Rrc\n", rc);
|
---|
701 | break;
|
---|
702 | }
|
---|
703 | # endif
|
---|
704 | # endif
|
---|
705 |
|
---|
706 | # ifndef RT_OS_OS2 /** @todo port this to OS/2 */
|
---|
707 | char szMac[32];
|
---|
708 | # if defined(RT_OS_SOLARIS)
|
---|
709 | uint8_t *pu8Mac = IfMac.au8;
|
---|
710 | # else
|
---|
711 | uint8_t *pu8Mac = (uint8_t*)&ifrequest[i].ifr_hwaddr.sa_data[0]; /* @todo see above */
|
---|
712 | # endif
|
---|
713 | RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
|
---|
714 | pu8Mac[0], pu8Mac[1], pu8Mac[2], pu8Mac[3], pu8Mac[4], pu8Mac[5]);
|
---|
715 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/MAC", cIfacesReport);
|
---|
716 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
|
---|
717 | # endif /* !OS/2*/
|
---|
718 |
|
---|
719 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/Status", cIfacesReport);
|
---|
720 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, fIfUp ? "Up" : "Down");
|
---|
721 | cIfacesReport++;
|
---|
722 | } /* For all interfaces */
|
---|
723 |
|
---|
724 | close(sd);
|
---|
725 | if (RT_FAILURE(rc))
|
---|
726 | VBoxServiceError("VMInfo/Network: Network enumeration for interface %u failed with error %Rrc\n", cIfacesReport, rc);
|
---|
727 |
|
---|
728 | #endif /* !RT_OS_WINDOWS */
|
---|
729 |
|
---|
730 | #if 0 /* Zapping not enabled yet, needs more testing first. */
|
---|
731 | /*
|
---|
732 | * Zap all stale network interface data if the former (saved) network ifaces count
|
---|
733 | * is bigger than the current one.
|
---|
734 | */
|
---|
735 |
|
---|
736 | /* Get former count. */
|
---|
737 | uint32_t cIfacesReportOld;
|
---|
738 | rc = VBoxServiceReadPropUInt32(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/Net/Count", &cIfacesReportOld,
|
---|
739 | 0 /* Min */, UINT32_MAX /* Max */);
|
---|
740 | if ( RT_SUCCESS(rc)
|
---|
741 | && cIfacesReportOld > cIfacesReport) /* Are some ifaces not around anymore? */
|
---|
742 | {
|
---|
743 | VBoxServiceVerbose(3, "VMInfo/Network: Stale interface data detected (%u old vs. %u current)\n",
|
---|
744 | cIfacesReportOld, cIfacesReport);
|
---|
745 |
|
---|
746 | uint32_t uIfaceDeleteIdx = cIfacesReport;
|
---|
747 | do
|
---|
748 | {
|
---|
749 | VBoxServiceVerbose(3, "VMInfo/Network: Deleting stale data of interface %d ...\n", uIfaceDeleteIdx);
|
---|
750 | rc = VBoxServicePropCacheUpdateByPath(&g_VMInfoPropCache, NULL /* Value, delete */, 0 /* Flags */, "/VirtualBox/GuestInfo/Net/%u", uIfaceDeleteIdx++);
|
---|
751 | } while (RT_SUCCESS(rc));
|
---|
752 | }
|
---|
753 | else if ( RT_FAILURE(rc)
|
---|
754 | && rc != VERR_NOT_FOUND)
|
---|
755 | {
|
---|
756 | VBoxServiceError("VMInfo/Network: Failed retrieving old network interfaces count with error %Rrc\n", rc);
|
---|
757 | }
|
---|
758 | #endif
|
---|
759 |
|
---|
760 | /*
|
---|
761 | * This property is a beacon which is _always_ written, even if the network configuration
|
---|
762 | * does not change. If this property is missing, the host assumes that all other GuestInfo
|
---|
763 | * properties are no longer valid.
|
---|
764 | */
|
---|
765 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/Net/Count", "%d",
|
---|
766 | cIfacesReport);
|
---|
767 |
|
---|
768 | /* Don't fail here; just report everything we got. */
|
---|
769 | return VINF_SUCCESS;
|
---|
770 | }
|
---|
771 |
|
---|
772 |
|
---|
773 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
774 | DECLCALLBACK(int) VBoxServiceVMInfoWorker(bool volatile *pfShutdown)
|
---|
775 | {
|
---|
776 | int rc;
|
---|
777 |
|
---|
778 | /*
|
---|
779 | * Tell the control thread that it can continue
|
---|
780 | * spawning services.
|
---|
781 | */
|
---|
782 | RTThreadUserSignal(RTThreadSelf());
|
---|
783 |
|
---|
784 | #ifdef RT_OS_WINDOWS
|
---|
785 | /* Required for network information (must be called per thread). */
|
---|
786 | WSADATA wsaData;
|
---|
787 | if (WSAStartup(MAKEWORD(2, 2), &wsaData))
|
---|
788 | VBoxServiceError("VMInfo/Network: WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError()));
|
---|
789 | #endif /* RT_OS_WINDOWS */
|
---|
790 |
|
---|
791 | /*
|
---|
792 | * Write the fixed properties first.
|
---|
793 | */
|
---|
794 | vboxserviceVMInfoWriteFixedProperties();
|
---|
795 |
|
---|
796 | /*
|
---|
797 | * Now enter the loop retrieving runtime data continuously.
|
---|
798 | */
|
---|
799 | for (;;)
|
---|
800 | {
|
---|
801 | rc = vboxserviceVMInfoWriteUsers();
|
---|
802 | if (RT_FAILURE(rc))
|
---|
803 | break;
|
---|
804 |
|
---|
805 | rc = vboxserviceVMInfoWriteNetwork();
|
---|
806 | if (RT_FAILURE(rc))
|
---|
807 | break;
|
---|
808 |
|
---|
809 | /*
|
---|
810 | * Flush all properties if we were restored.
|
---|
811 | */
|
---|
812 | uint64_t idNewSession = g_idVMInfoSession;
|
---|
813 | VbglR3GetSessionId(&idNewSession);
|
---|
814 | if (idNewSession != g_idVMInfoSession)
|
---|
815 | {
|
---|
816 | VBoxServiceVerbose(3, "VMInfo: The VM session ID changed, flushing all properties\n");
|
---|
817 | vboxserviceVMInfoWriteFixedProperties();
|
---|
818 | VBoxServicePropCacheFlush(&g_VMInfoPropCache);
|
---|
819 | g_idVMInfoSession = idNewSession;
|
---|
820 | }
|
---|
821 |
|
---|
822 | /*
|
---|
823 | * Block for a while.
|
---|
824 | *
|
---|
825 | * The event semaphore takes care of ignoring interruptions and it
|
---|
826 | * allows us to implement service wakeup later.
|
---|
827 | */
|
---|
828 | if (*pfShutdown)
|
---|
829 | break;
|
---|
830 | int rc2 = RTSemEventMultiWait(g_hVMInfoEvent, g_cMsVMInfoInterval);
|
---|
831 | if (*pfShutdown)
|
---|
832 | break;
|
---|
833 | if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
|
---|
834 | {
|
---|
835 | VBoxServiceError("VMInfo: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
|
---|
836 | rc = rc2;
|
---|
837 | break;
|
---|
838 | }
|
---|
839 | else if (RT_LIKELY(RT_SUCCESS(rc2)))
|
---|
840 | {
|
---|
841 | /* Reset event semaphore if it got triggered. */
|
---|
842 | rc2 = RTSemEventMultiReset(g_hVMInfoEvent);
|
---|
843 | if (RT_FAILURE(rc2))
|
---|
844 | rc2 = VBoxServiceError("VMInfo: RTSemEventMultiReset failed; rc2=%Rrc\n", rc2);
|
---|
845 | }
|
---|
846 | }
|
---|
847 |
|
---|
848 | #ifdef RT_OS_WINDOWS
|
---|
849 | WSACleanup();
|
---|
850 | #endif
|
---|
851 |
|
---|
852 | return rc;
|
---|
853 | }
|
---|
854 |
|
---|
855 |
|
---|
856 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
857 | static DECLCALLBACK(void) VBoxServiceVMInfoStop(void)
|
---|
858 | {
|
---|
859 | RTSemEventMultiSignal(g_hVMInfoEvent);
|
---|
860 | }
|
---|
861 |
|
---|
862 |
|
---|
863 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
864 | static DECLCALLBACK(void) VBoxServiceVMInfoTerm(void)
|
---|
865 | {
|
---|
866 | if (g_hVMInfoEvent != NIL_RTSEMEVENTMULTI)
|
---|
867 | {
|
---|
868 | /** @todo temporary solution: Zap all values which are not valid
|
---|
869 | * anymore when VM goes down (reboot/shutdown ). Needs to
|
---|
870 | * be replaced with "temporary properties" later.
|
---|
871 | *
|
---|
872 | * One idea is to introduce a (HGCM-)session guest property
|
---|
873 | * flag meaning that a guest property is only valid as long
|
---|
874 | * as the HGCM session isn't closed (e.g. guest application
|
---|
875 | * terminates). [don't remove till implemented]
|
---|
876 | */
|
---|
877 | /** @todo r=bird: Drop the VbglR3GuestPropDelSet call here and use the cache
|
---|
878 | * since it remembers what we've written. */
|
---|
879 | /* Delete the "../Net" branch. */
|
---|
880 | const char *apszPat[1] = { "/VirtualBox/GuestInfo/Net/*" };
|
---|
881 | int rc = VbglR3GuestPropDelSet(g_uVMInfoGuestPropSvcClientID, &apszPat[0], RT_ELEMENTS(apszPat));
|
---|
882 |
|
---|
883 | /* Destroy property cache. */
|
---|
884 | VBoxServicePropCacheDestroy(&g_VMInfoPropCache);
|
---|
885 |
|
---|
886 | /* Disconnect from guest properties service. */
|
---|
887 | rc = VbglR3GuestPropDisconnect(g_uVMInfoGuestPropSvcClientID);
|
---|
888 | if (RT_FAILURE(rc))
|
---|
889 | VBoxServiceError("VMInfo: Failed to disconnect from guest property service! Error: %Rrc\n", rc);
|
---|
890 | g_uVMInfoGuestPropSvcClientID = 0;
|
---|
891 |
|
---|
892 | RTSemEventMultiDestroy(g_hVMInfoEvent);
|
---|
893 | g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
894 | }
|
---|
895 | }
|
---|
896 |
|
---|
897 |
|
---|
898 | /**
|
---|
899 | * The 'vminfo' service description.
|
---|
900 | */
|
---|
901 | VBOXSERVICE g_VMInfo =
|
---|
902 | {
|
---|
903 | /* pszName. */
|
---|
904 | "vminfo",
|
---|
905 | /* pszDescription. */
|
---|
906 | "Virtual Machine Information",
|
---|
907 | /* pszUsage. */
|
---|
908 | " [--vminfo-interval <ms>]"
|
---|
909 | ,
|
---|
910 | /* pszOptions. */
|
---|
911 | " --vminfo-interval Specifies the interval at which to retrieve the\n"
|
---|
912 | " VM information. The default is 10000 ms.\n"
|
---|
913 | ,
|
---|
914 | /* methods */
|
---|
915 | VBoxServiceVMInfoPreInit,
|
---|
916 | VBoxServiceVMInfoOption,
|
---|
917 | VBoxServiceVMInfoInit,
|
---|
918 | VBoxServiceVMInfoWorker,
|
---|
919 | VBoxServiceVMInfoStop,
|
---|
920 | VBoxServiceVMInfoTerm
|
---|
921 | };
|
---|
922 |
|
---|