VirtualBox

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

Last change on this file since 30671 was 30048, checked in by vboxsync, 14 years ago

VBoxServiceVMInfo.cpp: r=bird: Consitent error handling in vboxserviceVMInfoWriteUsers. Don't use constants like FILENAME_MAX (or _MAX_PATH) for sizing up buffers that hasn't got anything to do with the local file system. Use static functions where possible.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.8 KB
Line 
1/* $Id: VBoxServiceVMInfo.cpp 30048 2010-06-07 06:35:37Z 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#elif defined(RT_OS_FREEBSD)
228 /** @todo FreeBSD: Port logged on user info retrival. */
229 rc = VERR_NOT_IMPLEMENTED;
230#elif defined(RT_OS_OS2)
231 /** @todo OS/2: Port logged on (LAN/local/whatever) user info retrival. */
232 rc = VERR_NOT_IMPLEMENTED;
233#else
234 rc = utmpname(UTMP_FILE);
235# ifdef RT_OS_SOLARIS
236 if (rc != 1)
237# else
238 if (rc != 0)
239# endif
240 {
241 VBoxServiceError("VMInfo/Users: Could not set UTMP file! Error: %ld\n", errno);
242 rc = VERR_GENERAL_FAILURE;
243 }
244 else
245 rc = VINF_SUCCESS;
246 setutent();
247 utmp *ut_user;
248 while ((ut_user = getutent()))
249 {
250 /* Make sure we don't add user names which are not
251 * part of type USER_PROCESS. */
252
253 /** @todo Do we want to filter out user names? What if a user is logged in twice? */
254 if (ut_user->ut_type == USER_PROCESS)
255 {
256 if (cUsersInList > 0)
257 {
258 rc = RTStrAAppend(&pszUserList, ",");
259 AssertRCBreakStmt(rc, RTStrFree(pszUserList));
260 }
261 rc = RTStrAAppend(&pszUserList, ut_user->ut_user);
262 AssertRCBreakStmt(rc, RTStrFree(pszUserList));
263 cUsersInList++;
264 }
265 }
266 endutent();
267#endif
268 Assert(RT_FAILURE(rc) || cUsersInList == 0 || (pszUserList && *pszUserList));
269 if (RT_FAILURE(rc))
270 cUsersInList = 0;
271
272 if (pszUserList && cUsersInList > 0)
273 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", "%s", pszUserList);
274 else
275 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", NULL);
276 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsers", "%u", cUsersInList);
277 if (g_cVMInfoLoggedInUsers != cUsersInList)
278 {
279 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers",
280 cUsersInList == 0 ? "true" : "false");
281 g_cVMInfoLoggedInUsers = cUsersInList;
282 }
283 if (RT_SUCCESS(rc) && pszUserList)
284 RTStrFree(pszUserList);
285 return VINF_SUCCESS;
286}
287
288
289/**
290 * Provide information about the guest network.
291 */
292static int vboxserviceVMInfoWriteNetwork(void)
293{
294 int cIfacesReport = 0;
295 char szPropPath[256];
296
297#ifdef RT_OS_WINDOWS
298 IP_ADAPTER_INFO *pAdpInfo = NULL;
299
300# ifndef TARGET_NT4
301 ULONG cbAdpInfo = sizeof(*pAdpInfo);
302 pAdpInfo = (IP_ADAPTER_INFO *)RTMemAlloc(cbAdpInfo);
303 if (!pAdpInfo)
304 {
305 VBoxServiceError("VMInfo/Network: Failed to allocate IP_ADAPTER_INFO\n");
306 return VERR_NO_MEMORY;
307 }
308 DWORD dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
309 if (dwRet == ERROR_BUFFER_OVERFLOW)
310 {
311 IP_ADAPTER_INFO *pAdpInfoNew = (IP_ADAPTER_INFO*)RTMemRealloc(pAdpInfo, cbAdpInfo);
312 if (pAdpInfoNew)
313 {
314 pAdpInfo = pAdpInfoNew;
315 dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
316 }
317 }
318 if (dwRet != ERROR_SUCCESS)
319 {
320 if (pAdpInfo)
321 RTMemFree(pAdpInfo);
322 VBoxServiceError("VMInfo/Network: Failed to get adapter info: Error %d\n", dwRet);
323 return RTErrConvertFromWin32(dwRet);
324 }
325# endif
326
327 SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
328 if (sd == SOCKET_ERROR) /* Socket invalid. */
329 {
330 VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %d\n", WSAGetLastError());
331 if (pAdpInfo)
332 RTMemFree(pAdpInfo);
333 return RTErrConvertFromWin32(WSAGetLastError());
334 }
335
336 INTERFACE_INFO InterfaceList[20] = {0};
337 unsigned long nBytesReturned = 0;
338 if (WSAIoctl(sd,
339 SIO_GET_INTERFACE_LIST,
340 0,
341 0,
342 &InterfaceList,
343 sizeof(InterfaceList),
344 &nBytesReturned,
345 0,
346 0) == SOCKET_ERROR)
347 {
348 VBoxServiceError("VMInfo/Network: Failed to WSAIoctl() on socket: Error: %d\n", WSAGetLastError());
349 if (pAdpInfo)
350 RTMemFree(pAdpInfo);
351 return RTErrConvertFromWin32(WSAGetLastError());
352 }
353 int cIfacesSystem = nBytesReturned / sizeof(INTERFACE_INFO);
354
355 /** @todo Use GetAdaptersInfo() and GetAdapterAddresses (IPv4 + IPv6) for more information. */
356 for (int i = 0; i < cIfacesSystem; ++i)
357 {
358 sockaddr_in *pAddress;
359 u_long nFlags = 0;
360 if (InterfaceList[i].iiFlags & IFF_LOOPBACK) /* Skip loopback device. */
361 continue;
362 nFlags = InterfaceList[i].iiFlags;
363 pAddress = (sockaddr_in *)&(InterfaceList[i].iiAddress);
364 Assert(pAddress);
365 char szIp[32];
366 RTStrPrintf(szIp, sizeof(szIp), "%s", inet_ntoa(pAddress->sin_addr));
367 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/IP", cIfacesReport);
368 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szIp);
369
370 pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress);
371 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Broadcast", cIfacesReport);
372 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
373
374 pAddress = (sockaddr_in *)&(InterfaceList[i].iiNetmask);
375 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Netmask", cIfacesReport);
376 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
377
378 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/Status", cIfacesReport);
379 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, nFlags & IFF_UP ? "Up" : "Down");
380
381# ifndef TARGET_NT4
382 IP_ADAPTER_INFO *pAdp;
383 for (pAdp = pAdpInfo; pAdp; pAdp = pAdp->Next)
384 if (!strcmp(pAdp->IpAddressList.IpAddress.String, szIp))
385 break;
386
387 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/MAC", cIfacesReport);
388 if (pAdp)
389 {
390 char szMac[32];
391 RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
392 pAdp->Address[0], pAdp->Address[1], pAdp->Address[2],
393 pAdp->Address[3], pAdp->Address[4], pAdp->Address[5]);
394 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
395 }
396 else
397 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, NULL);
398# endif
399
400 cIfacesReport++;
401 }
402 if (pAdpInfo)
403 RTMemFree(pAdpInfo);
404 if (sd >= 0)
405 closesocket(sd);
406
407#elif defined(RT_OS_FREEBSD)
408 int rc = 0;
409 struct ifaddrs *pIfHead = NULL;
410
411 /* Get all available interfaces */
412 rc = getifaddrs(&pIfHead);
413 if (rc < 0)
414 {
415 VBoxServiceError("VMInfo/Network: Failed to get all interfaces: Error %d\n", errno);
416 return RTErrConvertFromErrno(errno);
417 }
418
419 /* Loop through all interfaces and set the data. */
420 for (struct ifaddrs *pIfCurr = pIfHead; pIfCurr; pIfCurr = pIfCurr->ifa_next)
421 {
422 /*
423 * Only AF_INET and no loopback interfaces
424 * @todo: IPv6 interfaces
425 */
426 if ( pIfCurr->ifa_addr->sa_family == AF_INET
427 && !(pIfCurr->ifa_flags & IFF_LOOPBACK))
428 {
429 char szInetAddr[NI_MAXHOST];
430
431 memset(szInetAddr, 0, NI_MAXHOST);
432 getnameinfo(pIfCurr->ifa_addr, sizeof(struct sockaddr_in),
433 szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
434 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/IP", cIfacesReport);
435 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
436
437 memset(szInetAddr, 0, NI_MAXHOST);
438 getnameinfo(pIfCurr->ifa_broadaddr, sizeof(struct sockaddr_in),
439 szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
440 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Broadcast", cIfacesReport);
441 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
442
443 memset(szInetAddr, 0, NI_MAXHOST);
444 getnameinfo(pIfCurr->ifa_netmask, sizeof(struct sockaddr_in),
445 szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
446 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Netmask", cIfacesReport);
447 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
448
449 /* Search for the AF_LINK interface of the current AF_INET one and get the mac. */
450 for (struct ifaddrs *pIfLinkCurr = pIfHead; pIfLinkCurr; pIfLinkCurr = pIfLinkCurr->ifa_next)
451 {
452 if ( pIfLinkCurr->ifa_addr->sa_family == AF_LINK
453 && !strcmp(pIfCurr->ifa_name, pIfLinkCurr->ifa_name))
454 {
455 char szMac[32];
456 uint8_t *pu8Mac = NULL;
457 struct sockaddr_dl *pLinkAddress = (struct sockaddr_dl *)pIfLinkCurr->ifa_addr;
458
459 AssertPtr(pLinkAddress);
460 pu8Mac = (uint8_t *)LLADDR(pLinkAddress);
461 RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
462 pu8Mac[0], pu8Mac[1], pu8Mac[2], pu8Mac[3], pu8Mac[4], pu8Mac[5]);
463 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/MAC", cIfacesReport);
464 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
465 break;
466 }
467 }
468
469 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/Status", cIfacesReport);
470 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, pIfCurr->ifa_flags & IFF_UP ? "Up" : "Down");
471
472 cIfacesReport++;
473 }
474 }
475
476 /* Free allocated resources. */
477 freeifaddrs(pIfHead);
478
479#else /* !RT_OS_WINDOWS && !RT_OS_FREEBSD */
480 int sd = socket(AF_INET, SOCK_DGRAM, 0);
481 if (sd < 0)
482 {
483 VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %d\n", errno);
484 return RTErrConvertFromErrno(errno);
485 }
486
487 ifconf ifcfg;
488 char buffer[1024] = {0};
489 ifcfg.ifc_len = sizeof(buffer);
490 ifcfg.ifc_buf = buffer;
491 if (ioctl(sd, SIOCGIFCONF, &ifcfg) < 0)
492 {
493 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFCONF) on socket: Error %d\n", errno);
494 return RTErrConvertFromErrno(errno);
495 }
496
497 ifreq* ifrequest = ifcfg.ifc_req;
498 int cIfacesSystem = ifcfg.ifc_len / sizeof(ifreq);
499
500 for (int i = 0; i < cIfacesSystem; ++i)
501 {
502 sockaddr_in *pAddress;
503 if (ioctl(sd, SIOCGIFFLAGS, &ifrequest[i]) < 0)
504 {
505 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFFLAGS) on socket: Error %d\n", errno);
506 close(sd);
507 return RTErrConvertFromErrno(errno);
508 }
509 if (ifrequest[i].ifr_flags & IFF_LOOPBACK) /* Skip the loopback device. */
510 continue;
511
512 bool fIfUp = !!(ifrequest[i].ifr_flags & IFF_UP);
513 pAddress = ((sockaddr_in *)&ifrequest[i].ifr_addr);
514 Assert(pAddress);
515 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/IP", cIfacesReport);
516 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
517
518 if (ioctl(sd, SIOCGIFBRDADDR, &ifrequest[i]) < 0)
519 {
520 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno);
521 close(sd);
522 return RTErrConvertFromErrno(errno);
523 }
524 pAddress = (sockaddr_in *)&ifrequest[i].ifr_broadaddr;
525 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Broadcast", cIfacesReport);
526 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
527
528 if (ioctl(sd, SIOCGIFNETMASK, &ifrequest[i]) < 0)
529 {
530 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno);
531 close(sd);
532 return RTErrConvertFromErrno(errno);
533 }
534# if defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
535 pAddress = (sockaddr_in *)&ifrequest[i].ifr_addr;
536# else
537 pAddress = (sockaddr_in *)&ifrequest[i].ifr_netmask;
538# endif
539
540 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Netmask", cIfacesReport);
541 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
542
543# if defined(RT_OS_SOLARIS)
544 if (ioctl(sd, SIOCGENADDR, &ifrequest[i]) < 0)
545# else
546 if (ioctl(sd, SIOCGIFHWADDR, &ifrequest[i]) < 0)
547# endif
548 {
549 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFHWADDR) on socket: Error %d\n", errno);
550 close(sd);
551 return RTErrConvertFromErrno(errno);
552 }
553
554 char szMac[32];
555# if defined(RT_OS_SOLARIS)
556 uint8_t *pu8Mac = (uint8_t*)&ifrequest[i].ifr_enaddr[0];
557# else
558 uint8_t *pu8Mac = (uint8_t*)&ifrequest[i].ifr_hwaddr.sa_data[0];
559# endif
560 RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
561 pu8Mac[0], pu8Mac[1], pu8Mac[2], pu8Mac[3], pu8Mac[4], pu8Mac[5]);
562 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/MAC", cIfacesReport);
563 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
564
565 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/Status", cIfacesReport);
566 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, fIfUp ? "Up" : "Down");
567 cIfacesReport++;
568 }
569
570 close(sd);
571
572#endif /* !RT_OS_WINDOWS */
573
574 /*
575 * This property is a beacon which is _always_ written, even if the network configuration
576 * does not change. If this property is missing, the host assumes that all other GuestInfo
577 * properties are no longer valid.
578 */
579 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/Net/Count", "%d",
580 cIfacesReport);
581
582 /** @todo r=bird: if cIfacesReport decreased compared to the previous run, zap
583 * the stale data. This can probably be encorporated into the cache. */
584
585 return VINF_SUCCESS;
586}
587
588
589/** @copydoc VBOXSERVICE::pfnWorker */
590DECLCALLBACK(int) VBoxServiceVMInfoWorker(bool volatile *pfShutdown)
591{
592 int rc;
593
594 /*
595 * Tell the control thread that it can continue
596 * spawning services.
597 */
598 RTThreadUserSignal(RTThreadSelf());
599
600#ifdef RT_OS_WINDOWS
601 /* Required for network information (must be called per thread). */
602 WSADATA wsaData;
603 if (WSAStartup(MAKEWORD(2, 2), &wsaData))
604 VBoxServiceError("VMInfo/Network: WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError()));
605#endif /* RT_OS_WINDOWS */
606
607 /*
608 * Write the fixed properties first.
609 */
610 vboxserviceVMInfoWriteFixedProperties();
611
612 /*
613 * Now enter the loop retrieving runtime data continuously.
614 */
615 for (;;)
616 {
617 rc = vboxserviceVMInfoWriteUsers();
618 if (RT_FAILURE(rc))
619 break;
620
621 rc = vboxserviceVMInfoWriteNetwork();
622 if (RT_FAILURE(rc))
623 break;
624
625 /*
626 * Block for a while.
627 *
628 * The event semaphore takes care of ignoring interruptions and it
629 * allows us to implement service wakeup later.
630 */
631 if (*pfShutdown)
632 break;
633 int rc2 = RTSemEventMultiWait(g_hVMInfoEvent, g_cMsVMInfoInterval);
634 if (*pfShutdown)
635 break;
636 if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
637 {
638 VBoxServiceError("VMInfo: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
639 rc = rc2;
640 break;
641 }
642 }
643
644#ifdef RT_OS_WINDOWS
645 WSACleanup();
646#endif
647
648 return rc;
649}
650
651
652/** @copydoc VBOXSERVICE::pfnStop */
653static DECLCALLBACK(void) VBoxServiceVMInfoStop(void)
654{
655 RTSemEventMultiSignal(g_hVMInfoEvent);
656}
657
658
659/** @copydoc VBOXSERVICE::pfnTerm */
660static DECLCALLBACK(void) VBoxServiceVMInfoTerm(void)
661{
662 int rc;
663
664 if (g_hVMInfoEvent != NIL_RTSEMEVENTMULTI)
665 {
666 /** @todo temporary solution: Zap all values which are not valid
667 * anymore when VM goes down (reboot/shutdown ). Needs to
668 * be replaced with "temporary properties" later.
669 *
670 * One idea is to introduce a (HGCM-)session guest property
671 * flag meaning that a guest property is only valid as long
672 * as the HGCM session isn't closed (e.g. guest application
673 * terminates). [don't remove till implemented]
674 */
675 /** @todo r=bird: Drop the VbglR3GuestPropDelSet call here and use the cache
676 * since it remembers what we've written. */
677 /* Delete the "../Net" branch. */
678 const char *apszPat[1] = { "/VirtualBox/GuestInfo/Net/*" };
679 rc = VbglR3GuestPropDelSet(g_uVMInfoGuestPropSvcClientID, &apszPat[0], RT_ELEMENTS(apszPat));
680
681 /* Destroy property cache. */
682 VBoxServicePropCacheDestroy(&g_VMInfoPropCache);
683
684 /* Disconnect from guest properties service. */
685 rc = VbglR3GuestPropDisconnect(g_uVMInfoGuestPropSvcClientID);
686 if (RT_FAILURE(rc))
687 VBoxServiceError("VMInfo: Failed to disconnect from guest property service! Error: %Rrc\n", rc);
688 g_uVMInfoGuestPropSvcClientID = 0;
689
690 RTSemEventMultiDestroy(g_hVMInfoEvent);
691 g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
692 }
693}
694
695
696/**
697 * The 'vminfo' service description.
698 */
699VBOXSERVICE g_VMInfo =
700{
701 /* pszName. */
702 "vminfo",
703 /* pszDescription. */
704 "Virtual Machine Information",
705 /* pszUsage. */
706 " [--vminfo-interval <ms>]"
707 ,
708 /* pszOptions. */
709 " --vminfo-interval Specifies the interval at which to retrieve the\n"
710 " VM information. The default is 10000 ms.\n"
711 ,
712 /* methods */
713 VBoxServiceVMInfoPreInit,
714 VBoxServiceVMInfoOption,
715 VBoxServiceVMInfoInit,
716 VBoxServiceVMInfoWorker,
717 VBoxServiceVMInfoStop,
718 VBoxServiceVMInfoTerm
719};
720
Note: See TracBrowser for help on using the repository browser.

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