VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceVMInfo.cpp@ 31799

Last change on this file since 31799 was 31037, checked in by vboxsync, 15 years ago

VBoxService/VMInfo: Bugs fixed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.1 KB
Line 
1/* $Id: VBoxServiceVMInfo.cpp 31037 2010-07-23 08:30:33Z 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# include <winsock2.h>
25# include <iphlpapi.h>
26# include <ws2tcpip.h>
27# include <windows.h>
28# include <Ntsecapi.h>
29#else
30# define __STDC_LIMIT_MACROS
31# include <arpa/inet.h>
32# include <errno.h>
33# include <netinet/in.h>
34# include <sys/ioctl.h>
35# include <sys/socket.h>
36# include <net/if.h>
37# include <unistd.h>
38# ifndef RT_OS_FREEBSD /* The header does not exist anymore since FreeBSD 9-current */
39# include <utmp.h>
40# endif
41# ifdef RT_OS_SOLARIS
42# include <sys/sockio.h>
43# endif
44# ifdef RT_OS_FREEBSD
45# include <ifaddrs.h> /* getifaddrs, freeifaddrs */
46# include <net/if_dl.h> /* LLADDR */
47# include <netdb.h> /* getnameinfo */
48# endif
49#endif
50
51#include <iprt/mem.h>
52#include <iprt/thread.h>
53#include <iprt/string.h>
54#include <iprt/semaphore.h>
55#include <iprt/system.h>
56#include <iprt/time.h>
57#include <iprt/assert.h>
58#include <VBox/version.h>
59#include <VBox/VBoxGuestLib.h>
60#include "VBoxServiceInternal.h"
61#include "VBoxServiceUtils.h"
62#include "VBoxServicePropCache.h"
63
64
65/*******************************************************************************
66* Global Variables *
67*******************************************************************************/
68/** The vminfo interval (millseconds). */
69static uint32_t g_cMsVMInfoInterval = 0;
70/** The semaphore we're blocking on. */
71static RTSEMEVENTMULTI g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
72/** The guest property service client ID. */
73static uint32_t g_uVMInfoGuestPropSvcClientID = 0;
74/** Number of logged in users in OS. */
75static uint32_t g_cVMInfoLoggedInUsers = UINT32_MAX;
76/** The guest property cache. */
77static VBOXSERVICEVEPROPCACHE g_VMInfoPropCache;
78
79
80/** @copydoc VBOXSERVICE::pfnPreInit */
81static DECLCALLBACK(int) VBoxServiceVMInfoPreInit(void)
82{
83 return VINF_SUCCESS;
84}
85
86
87/** @copydoc VBOXSERVICE::pfnOption */
88static DECLCALLBACK(int) VBoxServiceVMInfoOption(const char **ppszShort, int argc, char **argv, int *pi)
89{
90 int rc = -1;
91 if (ppszShort)
92 /* no short options */;
93 else if (!strcmp(argv[*pi], "--vminfo-interval"))
94 rc = VBoxServiceArgUInt32(argc, argv, "", pi,
95 &g_cMsVMInfoInterval, 1, UINT32_MAX - 1);
96 return rc;
97}
98
99
100/** @copydoc VBOXSERVICE::pfnInit */
101static DECLCALLBACK(int) VBoxServiceVMInfoInit(void)
102{
103 /*
104 * If not specified, find the right interval default.
105 * Then create the event sem to block on.
106 */
107 if (!g_cMsVMInfoInterval)
108 g_cMsVMInfoInterval = g_DefaultInterval * 1000;
109 if (!g_cMsVMInfoInterval)
110 g_cMsVMInfoInterval = 10 * 1000;
111
112 int rc = RTSemEventMultiCreate(&g_hVMInfoEvent);
113 AssertRCReturn(rc, rc);
114
115 rc = VbglR3GuestPropConnect(&g_uVMInfoGuestPropSvcClientID);
116 if (RT_SUCCESS(rc))
117 VBoxServiceVerbose(3, "VMInfo: Property Service Client ID: %#x\n", g_uVMInfoGuestPropSvcClientID);
118 else
119 {
120 /* If the service was not found, we disable this service without
121 causing VBoxService to fail. */
122 if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
123 {
124 VBoxServiceVerbose(0, "VMInfo: Guest property service is not available, disabling the service\n");
125 rc = VERR_SERVICE_DISABLED;
126 }
127 else
128 VBoxServiceError("VMInfo: Failed to connect to the guest property service! Error: %Rrc\n", rc);
129 RTSemEventMultiDestroy(g_hVMInfoEvent);
130 g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
131 }
132
133 if (RT_SUCCESS(rc))
134 {
135 VBoxServicePropCacheCreate(&g_VMInfoPropCache, g_uVMInfoGuestPropSvcClientID);
136
137 /*
138 * Declare some guest properties with flags and reset values.
139 */
140 VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList",
141 VBOXSERVICEPROPCACHEFLAG_TEMPORARY, NULL /* Delete on exit */);
142 VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsers",
143 VBOXSERVICEPROPCACHEFLAG_TEMPORARY, "0");
144 VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers",
145 VBOXSERVICEPROPCACHEFLAG_TEMPORARY, "true");
146 VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/Net/Count",
147 VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_ALWAYS_UPDATE, NULL /* Delete on exit */);
148 }
149 return rc;
150}
151
152
153/**
154 * Writes the properties that won't change while the service is running.
155 *
156 * Errors are ignored.
157 */
158static void vboxserviceVMInfoWriteFixedProperties(void)
159{
160 /*
161 * First get OS information that won't change.
162 */
163 char szInfo[256];
164 int rc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szInfo, sizeof(szInfo));
165 VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Product",
166 "%s", RT_FAILURE(rc) ? "" : szInfo);
167
168 rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szInfo, sizeof(szInfo));
169 VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Release",
170 "%s", RT_FAILURE(rc) ? "" : szInfo);
171
172 rc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szInfo, sizeof(szInfo));
173 VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Version",
174 "%s", RT_FAILURE(rc) ? "" : szInfo);
175
176 rc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szInfo, sizeof(szInfo));
177 VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/ServicePack",
178 "%s", RT_FAILURE(rc) ? "" : szInfo);
179
180 /*
181 * Retrieve version information about Guest Additions and installed files (components).
182 */
183 char *pszAddVer;
184 char *pszAddRev;
185 rc = VbglR3GetAdditionsVersion(&pszAddVer, &pszAddRev);
186 VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Version",
187 "%s", RT_FAILURE(rc) ? "" : pszAddVer);
188 VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Revision",
189 "%s", RT_FAILURE(rc) ? "" : pszAddRev);
190 if (RT_SUCCESS(rc))
191 {
192 RTStrFree(pszAddVer);
193 RTStrFree(pszAddRev);
194 }
195
196#ifdef RT_OS_WINDOWS
197 /*
198 * Do windows specific properties.
199 */
200 char *pszInstDir;
201 rc = VbglR3GetAdditionsInstallationPath(&pszInstDir);
202 VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/InstallDir",
203 "%s", RT_FAILURE(rc) ? "" : pszInstDir);
204 if (RT_SUCCESS(rc))
205 RTStrFree(pszInstDir);
206
207 VBoxServiceWinGetComponentVersions(g_uVMInfoGuestPropSvcClientID);
208#endif
209}
210
211
212/**
213 * Provide information about active users.
214 */
215static int vboxserviceVMInfoWriteUsers(void)
216{
217 int rc;
218 char *pszUserList = NULL;
219 uint32_t cUsersInList = 0;
220
221#ifdef RT_OS_WINDOWS
222# ifndef TARGET_NT4
223 rc = VBoxServiceVMInfoWinWriteUsers(&pszUserList, &cUsersInList);
224# else
225 rc = VERR_NOT_IMPLEMENTED;
226# endif
227
228#elif defined(RT_OS_FREEBSD)
229 /** @todo FreeBSD: Port logged on user info retrival. */
230 rc = VERR_NOT_IMPLEMENTED;
231
232#elif defined(RT_OS_OS2)
233 /** @todo OS/2: Port logged on (LAN/local/whatever) user info retrival. */
234 rc = VERR_NOT_IMPLEMENTED;
235
236#else
237 /** @todo Add utmpx support? */
238 rc = utmpname(UTMP_FILE);
239# ifdef RT_OS_SOLARIS
240 if (rc != 1)
241# else
242 if (rc != 0)
243# endif
244 {
245 VBoxServiceError("VMInfo/Users: Could not set UTMP file! Error: %ld\n", errno);
246 rc = VERR_GENERAL_FAILURE;
247 }
248 else
249 rc = VINF_SUCCESS;
250
251 setutent();
252 utmp *ut_user;
253 uint32_t cListSize = 32;
254
255 /* Allocate a first array to hold 32 users max. */
256 char **papszUsers = (char **)RTMemAllocZ(cListSize * sizeof(char *));
257 if (papszUsers == NULL)
258 rc = VERR_NO_MEMORY;
259
260 /* Process all entries in the utmp file. */
261 while ( (ut_user = getutent())
262 && RT_SUCCESS(rc))
263 {
264 VBoxServiceVerbose(4, "VMInfo: Found logged in user \"%s\"\n", ut_user->ut_user);
265
266 if (cUsersInList > cListSize)
267 {
268 cListSize += 32;
269 void *pvNew = RTMemRealloc(papszUsers, cListSize * sizeof(char*));
270 AssertPtrBreakStmt(pvNew, cListSize -= 32);
271 papszUsers = (char **)pvNew;
272 }
273
274 /* Make sure we don't add user names which are not
275 * part of type USER_PROCESS. */
276 if (ut_user->ut_type == USER_PROCESS)
277 {
278 bool fFound = false;
279 for (uint32_t i = 0; i < cUsersInList && !fFound; i++)
280 fFound = strcmp(papszUsers[i], ut_user->ut_user) == 0;
281
282 if (!fFound)
283 {
284 rc = RTStrDupEx(&papszUsers[cUsersInList], (const char *)ut_user->ut_user);
285 if (RT_FAILURE(rc))
286 break;
287 cUsersInList++;
288 }
289 }
290 }
291
292 /* Calc the string length. */
293 size_t cchUserList = 0;
294 for (uint32_t i = 0; i < cUsersInList; i++)
295 cchUserList += (i != 0) + strlen(papszUsers[i]);
296
297 /* Build the user list. */
298 rc = RTStrAllocEx(&pszUserList, cchUserList + 1);
299 if (RT_SUCCESS(rc))
300 {
301 char *psz = pszUserList;
302 for (uint32_t i = 0; i < cUsersInList; i++)
303 {
304 if (i != 0)
305 *psz++ = ',';
306 size_t cch = strlen(papszUsers[i]);
307 memcpy(psz, papszUsers[i], cch);
308 psz += cch;
309 }
310 *psz = '\0';
311 }
312
313 /* Cleanup. */
314 for (uint32_t i = 0; i < cUsersInList; i++)
315 RTStrFree(papszUsers[i]);
316 RTMemFree(papszUsers);
317
318 endutent(); /* Close utmp file. */
319#endif
320 Assert(RT_FAILURE(rc) || cUsersInList == 0 || (pszUserList && *pszUserList));
321 if (RT_FAILURE(rc))
322 cUsersInList = 0;
323
324 if (pszUserList && cUsersInList > 0)
325 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", "%s", pszUserList);
326 else
327 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", NULL);
328 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsers", "%u", cUsersInList);
329 if (g_cVMInfoLoggedInUsers != cUsersInList)
330 {
331 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers",
332 cUsersInList == 0 ? "true" : "false");
333 g_cVMInfoLoggedInUsers = cUsersInList;
334 }
335 if (RT_SUCCESS(rc) && pszUserList)
336 RTStrFree(pszUserList);
337 return VINF_SUCCESS;
338}
339
340
341/**
342 * Provide information about the guest network.
343 */
344static int vboxserviceVMInfoWriteNetwork(void)
345{
346 int cIfacesReport = 0;
347 char szPropPath[256];
348
349#ifdef RT_OS_WINDOWS
350 IP_ADAPTER_INFO *pAdpInfo = NULL;
351
352# ifndef TARGET_NT4
353 ULONG cbAdpInfo = sizeof(*pAdpInfo);
354 pAdpInfo = (IP_ADAPTER_INFO *)RTMemAlloc(cbAdpInfo);
355 if (!pAdpInfo)
356 {
357 VBoxServiceError("VMInfo/Network: Failed to allocate IP_ADAPTER_INFO\n");
358 return VERR_NO_MEMORY;
359 }
360 DWORD dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
361 if (dwRet == ERROR_BUFFER_OVERFLOW)
362 {
363 IP_ADAPTER_INFO *pAdpInfoNew = (IP_ADAPTER_INFO*)RTMemRealloc(pAdpInfo, cbAdpInfo);
364 if (pAdpInfoNew)
365 {
366 pAdpInfo = pAdpInfoNew;
367 dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
368 }
369 }
370 if (dwRet != ERROR_SUCCESS)
371 {
372 if (pAdpInfo)
373 RTMemFree(pAdpInfo);
374 VBoxServiceError("VMInfo/Network: Failed to get adapter info: Error %d\n", dwRet);
375 return RTErrConvertFromWin32(dwRet);
376 }
377# endif
378
379 SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
380 if (sd == SOCKET_ERROR) /* Socket invalid. */
381 {
382 VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %d\n", WSAGetLastError());
383 if (pAdpInfo)
384 RTMemFree(pAdpInfo);
385 return RTErrConvertFromWin32(WSAGetLastError());
386 }
387
388 INTERFACE_INFO InterfaceList[20] = {0};
389 unsigned long nBytesReturned = 0;
390 if (WSAIoctl(sd,
391 SIO_GET_INTERFACE_LIST,
392 0,
393 0,
394 &InterfaceList,
395 sizeof(InterfaceList),
396 &nBytesReturned,
397 0,
398 0) == SOCKET_ERROR)
399 {
400 VBoxServiceError("VMInfo/Network: Failed to WSAIoctl() on socket: Error: %d\n", WSAGetLastError());
401 if (pAdpInfo)
402 RTMemFree(pAdpInfo);
403 return RTErrConvertFromWin32(WSAGetLastError());
404 }
405 int cIfacesSystem = nBytesReturned / sizeof(INTERFACE_INFO);
406
407 /** @todo Use GetAdaptersInfo() and GetAdapterAddresses (IPv4 + IPv6) for more information. */
408 for (int i = 0; i < cIfacesSystem; ++i)
409 {
410 sockaddr_in *pAddress;
411 u_long nFlags = 0;
412 if (InterfaceList[i].iiFlags & IFF_LOOPBACK) /* Skip loopback device. */
413 continue;
414 nFlags = InterfaceList[i].iiFlags;
415 pAddress = (sockaddr_in *)&(InterfaceList[i].iiAddress);
416 Assert(pAddress);
417 char szIp[32];
418 RTStrPrintf(szIp, sizeof(szIp), "%s", inet_ntoa(pAddress->sin_addr));
419 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/IP", cIfacesReport);
420 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szIp);
421
422 pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress);
423 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Broadcast", cIfacesReport);
424 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
425
426 pAddress = (sockaddr_in *)&(InterfaceList[i].iiNetmask);
427 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Netmask", cIfacesReport);
428 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
429
430 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/Status", cIfacesReport);
431 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, nFlags & IFF_UP ? "Up" : "Down");
432
433# ifndef TARGET_NT4
434 IP_ADAPTER_INFO *pAdp;
435 for (pAdp = pAdpInfo; pAdp; pAdp = pAdp->Next)
436 if (!strcmp(pAdp->IpAddressList.IpAddress.String, szIp))
437 break;
438
439 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/MAC", cIfacesReport);
440 if (pAdp)
441 {
442 char szMac[32];
443 RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
444 pAdp->Address[0], pAdp->Address[1], pAdp->Address[2],
445 pAdp->Address[3], pAdp->Address[4], pAdp->Address[5]);
446 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
447 }
448 else
449 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, NULL);
450# endif
451
452 cIfacesReport++;
453 }
454 if (pAdpInfo)
455 RTMemFree(pAdpInfo);
456 if (sd >= 0)
457 closesocket(sd);
458
459#elif defined(RT_OS_FREEBSD)
460 int rc = 0;
461 struct ifaddrs *pIfHead = NULL;
462
463 /* Get all available interfaces */
464 rc = getifaddrs(&pIfHead);
465 if (rc < 0)
466 {
467 VBoxServiceError("VMInfo/Network: Failed to get all interfaces: Error %d\n", errno);
468 return RTErrConvertFromErrno(errno);
469 }
470
471 /* Loop through all interfaces and set the data. */
472 for (struct ifaddrs *pIfCurr = pIfHead; pIfCurr; pIfCurr = pIfCurr->ifa_next)
473 {
474 /*
475 * Only AF_INET and no loopback interfaces
476 * @todo: IPv6 interfaces
477 */
478 if ( pIfCurr->ifa_addr->sa_family == AF_INET
479 && !(pIfCurr->ifa_flags & IFF_LOOPBACK))
480 {
481 char szInetAddr[NI_MAXHOST];
482
483 memset(szInetAddr, 0, NI_MAXHOST);
484 getnameinfo(pIfCurr->ifa_addr, sizeof(struct sockaddr_in),
485 szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
486 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/IP", cIfacesReport);
487 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
488
489 memset(szInetAddr, 0, NI_MAXHOST);
490 getnameinfo(pIfCurr->ifa_broadaddr, sizeof(struct sockaddr_in),
491 szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
492 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Broadcast", cIfacesReport);
493 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
494
495 memset(szInetAddr, 0, NI_MAXHOST);
496 getnameinfo(pIfCurr->ifa_netmask, sizeof(struct sockaddr_in),
497 szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
498 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Netmask", cIfacesReport);
499 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
500
501 /* Search for the AF_LINK interface of the current AF_INET one and get the mac. */
502 for (struct ifaddrs *pIfLinkCurr = pIfHead; pIfLinkCurr; pIfLinkCurr = pIfLinkCurr->ifa_next)
503 {
504 if ( pIfLinkCurr->ifa_addr->sa_family == AF_LINK
505 && !strcmp(pIfCurr->ifa_name, pIfLinkCurr->ifa_name))
506 {
507 char szMac[32];
508 uint8_t *pu8Mac = NULL;
509 struct sockaddr_dl *pLinkAddress = (struct sockaddr_dl *)pIfLinkCurr->ifa_addr;
510
511 AssertPtr(pLinkAddress);
512 pu8Mac = (uint8_t *)LLADDR(pLinkAddress);
513 RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
514 pu8Mac[0], pu8Mac[1], pu8Mac[2], pu8Mac[3], pu8Mac[4], pu8Mac[5]);
515 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/MAC", cIfacesReport);
516 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
517 break;
518 }
519 }
520
521 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/Status", cIfacesReport);
522 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, pIfCurr->ifa_flags & IFF_UP ? "Up" : "Down");
523
524 cIfacesReport++;
525 }
526 }
527
528 /* Free allocated resources. */
529 freeifaddrs(pIfHead);
530
531#else /* !RT_OS_WINDOWS && !RT_OS_FREEBSD */
532 int sd = socket(AF_INET, SOCK_DGRAM, 0);
533 if (sd < 0)
534 {
535 VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %d\n", errno);
536 return RTErrConvertFromErrno(errno);
537 }
538
539 ifconf ifcfg;
540 char buffer[1024] = {0};
541 ifcfg.ifc_len = sizeof(buffer);
542 ifcfg.ifc_buf = buffer;
543 if (ioctl(sd, SIOCGIFCONF, &ifcfg) < 0)
544 {
545 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFCONF) on socket: Error %d\n", errno);
546 return RTErrConvertFromErrno(errno);
547 }
548
549 ifreq* ifrequest = ifcfg.ifc_req;
550 int cIfacesSystem = ifcfg.ifc_len / sizeof(ifreq);
551
552 for (int i = 0; i < cIfacesSystem; ++i)
553 {
554 sockaddr_in *pAddress;
555 if (ioctl(sd, SIOCGIFFLAGS, &ifrequest[i]) < 0)
556 {
557 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFFLAGS) on socket: Error %d\n", errno);
558 close(sd);
559 return RTErrConvertFromErrno(errno);
560 }
561 if (ifrequest[i].ifr_flags & IFF_LOOPBACK) /* Skip the loopback device. */
562 continue;
563
564 bool fIfUp = !!(ifrequest[i].ifr_flags & IFF_UP);
565 pAddress = ((sockaddr_in *)&ifrequest[i].ifr_addr);
566 Assert(pAddress);
567 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/IP", cIfacesReport);
568 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
569
570 if (ioctl(sd, SIOCGIFBRDADDR, &ifrequest[i]) < 0)
571 {
572 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno);
573 close(sd);
574 return RTErrConvertFromErrno(errno);
575 }
576 pAddress = (sockaddr_in *)&ifrequest[i].ifr_broadaddr;
577 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Broadcast", cIfacesReport);
578 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
579
580 if (ioctl(sd, SIOCGIFNETMASK, &ifrequest[i]) < 0)
581 {
582 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno);
583 close(sd);
584 return RTErrConvertFromErrno(errno);
585 }
586# if defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
587 pAddress = (sockaddr_in *)&ifrequest[i].ifr_addr;
588# else
589 pAddress = (sockaddr_in *)&ifrequest[i].ifr_netmask;
590# endif
591
592 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Netmask", cIfacesReport);
593 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
594
595# if defined(RT_OS_SOLARIS)
596 if (ioctl(sd, SIOCGENADDR, &ifrequest[i]) < 0)
597# else
598 if (ioctl(sd, SIOCGIFHWADDR, &ifrequest[i]) < 0)
599# endif
600 {
601 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFHWADDR) on socket: Error %d\n", errno);
602 close(sd);
603 return RTErrConvertFromErrno(errno);
604 }
605
606 char szMac[32];
607# if defined(RT_OS_SOLARIS)
608 uint8_t *pu8Mac = (uint8_t*)&ifrequest[i].ifr_enaddr[0];
609# else
610 uint8_t *pu8Mac = (uint8_t*)&ifrequest[i].ifr_hwaddr.sa_data[0];
611# endif
612 RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
613 pu8Mac[0], pu8Mac[1], pu8Mac[2], pu8Mac[3], pu8Mac[4], pu8Mac[5]);
614 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/MAC", cIfacesReport);
615 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
616
617 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/Status", cIfacesReport);
618 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, fIfUp ? "Up" : "Down");
619 cIfacesReport++;
620 }
621
622 close(sd);
623
624#endif /* !RT_OS_WINDOWS */
625
626 /*
627 * This property is a beacon which is _always_ written, even if the network configuration
628 * does not change. If this property is missing, the host assumes that all other GuestInfo
629 * properties are no longer valid.
630 */
631 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/Net/Count", "%d",
632 cIfacesReport);
633
634 /** @todo r=bird: if cIfacesReport decreased compared to the previous run, zap
635 * the stale data. This can probably be encorporated into the cache. */
636
637 return VINF_SUCCESS;
638}
639
640
641/** @copydoc VBOXSERVICE::pfnWorker */
642DECLCALLBACK(int) VBoxServiceVMInfoWorker(bool volatile *pfShutdown)
643{
644 int rc;
645
646 /*
647 * Tell the control thread that it can continue
648 * spawning services.
649 */
650 RTThreadUserSignal(RTThreadSelf());
651
652#ifdef RT_OS_WINDOWS
653 /* Required for network information (must be called per thread). */
654 WSADATA wsaData;
655 if (WSAStartup(MAKEWORD(2, 2), &wsaData))
656 VBoxServiceError("VMInfo/Network: WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError()));
657#endif /* RT_OS_WINDOWS */
658
659 /*
660 * Write the fixed properties first.
661 */
662 vboxserviceVMInfoWriteFixedProperties();
663
664 /*
665 * Now enter the loop retrieving runtime data continuously.
666 */
667 for (;;)
668 {
669 rc = vboxserviceVMInfoWriteUsers();
670 if (RT_FAILURE(rc))
671 break;
672
673 rc = vboxserviceVMInfoWriteNetwork();
674 if (RT_FAILURE(rc))
675 break;
676
677 /*
678 * Block for a while.
679 *
680 * The event semaphore takes care of ignoring interruptions and it
681 * allows us to implement service wakeup later.
682 */
683 if (*pfShutdown)
684 break;
685 int rc2 = RTSemEventMultiWait(g_hVMInfoEvent, g_cMsVMInfoInterval);
686 if (*pfShutdown)
687 break;
688 if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
689 {
690 VBoxServiceError("VMInfo: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
691 rc = rc2;
692 break;
693 }
694 }
695
696#ifdef RT_OS_WINDOWS
697 WSACleanup();
698#endif
699
700 return rc;
701}
702
703
704/** @copydoc VBOXSERVICE::pfnStop */
705static DECLCALLBACK(void) VBoxServiceVMInfoStop(void)
706{
707 RTSemEventMultiSignal(g_hVMInfoEvent);
708}
709
710
711/** @copydoc VBOXSERVICE::pfnTerm */
712static DECLCALLBACK(void) VBoxServiceVMInfoTerm(void)
713{
714 int rc;
715
716 if (g_hVMInfoEvent != NIL_RTSEMEVENTMULTI)
717 {
718 /** @todo temporary solution: Zap all values which are not valid
719 * anymore when VM goes down (reboot/shutdown ). Needs to
720 * be replaced with "temporary properties" later.
721 *
722 * One idea is to introduce a (HGCM-)session guest property
723 * flag meaning that a guest property is only valid as long
724 * as the HGCM session isn't closed (e.g. guest application
725 * terminates). [don't remove till implemented]
726 */
727 /** @todo r=bird: Drop the VbglR3GuestPropDelSet call here and use the cache
728 * since it remembers what we've written. */
729 /* Delete the "../Net" branch. */
730 const char *apszPat[1] = { "/VirtualBox/GuestInfo/Net/*" };
731 rc = VbglR3GuestPropDelSet(g_uVMInfoGuestPropSvcClientID, &apszPat[0], RT_ELEMENTS(apszPat));
732
733 /* Destroy property cache. */
734 VBoxServicePropCacheDestroy(&g_VMInfoPropCache);
735
736 /* Disconnect from guest properties service. */
737 rc = VbglR3GuestPropDisconnect(g_uVMInfoGuestPropSvcClientID);
738 if (RT_FAILURE(rc))
739 VBoxServiceError("VMInfo: Failed to disconnect from guest property service! Error: %Rrc\n", rc);
740 g_uVMInfoGuestPropSvcClientID = 0;
741
742 RTSemEventMultiDestroy(g_hVMInfoEvent);
743 g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
744 }
745}
746
747
748/**
749 * The 'vminfo' service description.
750 */
751VBOXSERVICE g_VMInfo =
752{
753 /* pszName. */
754 "vminfo",
755 /* pszDescription. */
756 "Virtual Machine Information",
757 /* pszUsage. */
758 " [--vminfo-interval <ms>]"
759 ,
760 /* pszOptions. */
761 " --vminfo-interval Specifies the interval at which to retrieve the\n"
762 " VM information. The default is 10000 ms.\n"
763 ,
764 /* methods */
765 VBoxServiceVMInfoPreInit,
766 VBoxServiceVMInfoOption,
767 VBoxServiceVMInfoInit,
768 VBoxServiceVMInfoWorker,
769 VBoxServiceVMInfoStop,
770 VBoxServiceVMInfoTerm
771};
772
Note: See TracBrowser for help on using the repository browser.

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