VirtualBox

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

Last change on this file since 22314 was 21980, checked in by vboxsync, 16 years ago

VBoxServiceVMInfo.cpp: VBOX_SVN_REV is not a long but an int. No need to initialize buffers all over the place.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.4 KB
Line 
1/* $Id: VBoxServiceVMInfo.cpp 21980 2009-08-05 11:19:55Z vboxsync $ */
2/** @file
3 * VBoxVMInfo - Virtual machine (guest) information for the host.
4 */
5
6/*
7 * Copyright (C) 2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#ifdef RT_OS_WINDOWS
28#include <winsock2.h>
29#include <ws2tcpip.h>
30#include <windows.h>
31#include <Ntsecapi.h>
32#else
33# define __STDC_LIMIT_MACROS
34# include <arpa/inet.h>
35# include <errno.h>
36# include <net/if.h>
37# include <netinet/in.h>
38# include <sys/ioctl.h>
39# include <sys/socket.h>
40# include <unistd.h>
41# include <utmp.h>
42# ifdef RT_OS_SOLARIS
43# include <sys/sockio.h>
44# endif
45#endif
46
47#include <iprt/mem.h>
48#include <iprt/thread.h>
49#include <iprt/string.h>
50#include <iprt/semaphore.h>
51#include <iprt/system.h>
52#include <iprt/time.h>
53#include <iprt/assert.h>
54#include <VBox/version.h>
55#include <VBox/VBoxGuestLib.h>
56#include "VBoxServiceInternal.h"
57#include "VBoxServiceUtils.h"
58
59
60/*******************************************************************************
61* Global Variables *
62*******************************************************************************/
63/** The vminfo interval (millseconds). */
64uint32_t g_VMInfoInterval = 0;
65/** The semaphore we're blocking on. */
66static RTSEMEVENTMULTI g_VMInfoEvent = NIL_RTSEMEVENTMULTI;
67/** The guest property service client ID. */
68static uint32_t g_VMInfoGuestPropSvcClientID = 0;
69/** Number of logged in users in OS. */
70static uint32_t g_VMInfoLoggedInUsers = UINT32_MAX;
71#ifdef RT_OS_WINDOWS
72/** Function prototypes for dynamic loading. */
73fnWTSGetActiveConsoleSessionId g_pfnWTSGetActiveConsoleSessionId = NULL;
74/** External functions. */
75extern int VboxServiceWinGetAddsVersion(uint32_t uiClientID);
76extern int VboxServiceWinGetComponentVersions(uint32_t uiClientID);
77#endif
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_VMInfoInterval, 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_VMInfoInterval)
108 g_VMInfoInterval = g_DefaultInterval * 1000;
109 if (!g_VMInfoInterval)
110 g_VMInfoInterval = 10 * 1000;
111
112 int rc = RTSemEventMultiCreate(&g_VMInfoEvent);
113 AssertRCReturn(rc, rc);
114
115#ifdef RT_OS_WINDOWS
116 /* Get function pointers. */
117 HMODULE hKernel32 = LoadLibrary(_T("kernel32"));
118 if (NULL != hKernel32)
119 {
120 g_pfnWTSGetActiveConsoleSessionId = (fnWTSGetActiveConsoleSessionId)GetProcAddress(hKernel32, "WTSGetActiveConsoleSessionId");
121 FreeLibrary(hKernel32);
122 }
123#endif
124
125 rc = VbglR3GuestPropConnect(&g_VMInfoGuestPropSvcClientID);
126 if (RT_SUCCESS(rc))
127 VBoxServiceVerbose(3, "Property Service Client ID: %#x\n", g_VMInfoGuestPropSvcClientID);
128 else
129 {
130 VBoxServiceError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
131 RTSemEventMultiDestroy(g_VMInfoEvent);
132 g_VMInfoEvent = NIL_RTSEMEVENTMULTI;
133 }
134
135 return rc;
136}
137
138
139/** @copydoc VBOXSERVICE::pfnWorker */
140DECLCALLBACK(int) VBoxServiceVMInfoWorker(bool volatile *pfShutdown)
141{
142 int rc = VINF_SUCCESS;
143
144 /*
145 * Tell the control thread that it can continue
146 * spawning services.
147 */
148 RTThreadUserSignal(RTThreadSelf());
149
150#ifdef RT_OS_WINDOWS
151 /* Required for network information (must be called per thread). */
152 WSADATA wsaData;
153 if (WSAStartup(MAKEWORD(2, 2), &wsaData)) {
154 VBoxServiceError("WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError()));
155 }
156#endif /* !RT_OS_WINDOWS */
157
158 /* First get information that won't change while the OS is running. */
159 char szInfo[256] = {0};
160 rc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szInfo, sizeof(szInfo));
161 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, "GuestInfo/OS/Product", szInfo);
162
163 rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szInfo, sizeof(szInfo));
164 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, "GuestInfo/OS/Release", szInfo);
165
166 rc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szInfo, sizeof(szInfo));
167 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, "GuestInfo/OS/Version", szInfo);
168
169 rc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szInfo, sizeof(szInfo));
170 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, "GuestInfo/OS/ServicePack", szInfo);
171
172 /* Retrieve version information about Guest Additions and installed files (components). */
173#ifdef RT_OS_WINDOWS
174 rc = VboxServiceWinGetAddsVersion(g_VMInfoGuestPropSvcClientID);
175 rc = VboxServiceWinGetComponentVersions(g_VMInfoGuestPropSvcClientID);
176#else
177 /* VboxServiceGetAddsVersion !RT_OS_WINDOWS */
178 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, "GuestAdd/Version", VBOX_VERSION_STRING);
179
180 char szRevision[32];
181 RTStrPrintf(szRevision, sizeof(szRevision), "%u", VBOX_SVN_REV);
182 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, "GuestAdd/Revision", szRevision);
183#endif
184
185 /* Now enter the loop retrieving runtime data continuously. */
186 unsigned cErrors = 0;
187 for (;;)
188 {
189 /* Enumerate logged in users. */
190 uint32_t uiUserCount = 0;
191 char szUserList[4096] = {0};
192
193#ifdef RT_OS_WINDOWS
194 #ifndef TARGET_NT4
195 PLUID pSessions = NULL;
196 ULONG ulCount = 0;
197 NTSTATUS r = 0;
198
199 char* pszTemp = NULL;
200
201 /* This function can report stale or orphaned interactive logon sessions of already logged
202 off users (especially in Windows 2000). */
203 r = ::LsaEnumerateLogonSessions(&ulCount, &pSessions);
204 VBoxServiceVerbose(3, "Users: Found %ld users.\n", ulCount);
205
206 if (r != STATUS_SUCCESS)
207 {
208 VBoxServiceError("LsaEnumerate failed %lu\n", LsaNtStatusToWinError(r));
209 return 1;
210 }
211
212 PLUID pLuid = NULL;
213 DWORD dwNumOfProcLUIDs = VboxServiceVMInfoWinGetLUIDsFromProcesses(&pLuid);
214
215 VBOXSERVICEVMINFOUSER userInfo;
216 ZeroMemory (&userInfo, sizeof(VBOXSERVICEVMINFOUSER));
217
218 for (int i = 0; i<(int)ulCount; i++)
219 {
220 if (VboxServiceVMInfoWinIsLoggedIn(&userInfo, &pSessions[i], pLuid, dwNumOfProcLUIDs))
221 {
222 if (uiUserCount > 0)
223 strcat (szUserList, ",");
224
225 uiUserCount++;
226
227 RTUtf16ToUtf8(userInfo.szUser, &pszTemp);
228 strcat(szUserList, pszTemp);
229 RTMemFree(pszTemp);
230 }
231 }
232
233 if (NULL != pLuid)
234 ::LocalFree (pLuid);
235
236 ::LsaFreeReturnBuffer(pSessions);
237 #endif /* TARGET_NT4 */
238#else
239 utmp* ut_user;
240 rc = utmpname(UTMP_FILE);
241 if (rc != 0)
242 {
243 VBoxServiceError("Could not set UTMP file! Error: %ld", errno);
244 }
245 setutent();
246 while ((ut_user=getutent()))
247 {
248 /* Make sure we don't add user names which are not
249 * part of type USER_PROCESS and don't add same users twice. */
250 if ( (ut_user->ut_type == USER_PROCESS)
251 && (strstr(szUserList, ut_user->ut_user) == NULL))
252 {
253 /** @todo Do we really want to filter out double user names? (Same user logged in twice) */
254 if (uiUserCount > 0)
255 strcat(szUserList, ",");
256 strcat(szUserList, ut_user->ut_user);
257 uiUserCount++;
258 }
259 }
260 endutent();
261#endif /* !RT_OS_WINDOWS */
262
263 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, "GuestInfo/OS/LoggedInUsersList", (uiUserCount > 0) ? szUserList : NULL);
264 VboxServiceWritePropInt(g_VMInfoGuestPropSvcClientID, "GuestInfo/OS/LoggedInUsers", uiUserCount);
265 if (g_VMInfoLoggedInUsers != uiUserCount || g_VMInfoLoggedInUsers == UINT32_MAX)
266 {
267 /* Update this property ONLY if there is a real change from no users to
268 * users or vice versa. The only exception is that the initialization
269 * forces an update, but only once. This ensures consistent property
270 * settings even if the VM aborted previously. */
271 if (uiUserCount == 0)
272 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, "GuestInfo/OS/NoLoggedInUsers", "true");
273 else if (g_VMInfoLoggedInUsers == 0)
274 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, "GuestInfo/OS/NoLoggedInUsers", "false");
275 }
276 g_VMInfoLoggedInUsers = uiUserCount;
277
278 /* Get network configuration. */
279 /** @todo Throw this code into a separate function/module? */
280 int nNumInterfaces = 0;
281#ifdef RT_OS_WINDOWS
282 SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
283 if (sd == SOCKET_ERROR) /* Socket invalid. */
284 {
285 VBoxServiceError("Failed to get a socket: Error %d\n", WSAGetLastError());
286 return -1;
287 }
288
289 INTERFACE_INFO InterfaceList[20] = {0};
290 unsigned long nBytesReturned = 0;
291 if (WSAIoctl(sd,
292 SIO_GET_INTERFACE_LIST,
293 0,
294 0,
295 &InterfaceList,
296 sizeof(InterfaceList),
297 &nBytesReturned,
298 0,
299 0) == SOCKET_ERROR)
300 {
301 VBoxServiceError("Failed to WSAIoctl() on socket: Error: %d\n", WSAGetLastError());
302 return -1;
303 }
304 nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO);
305#else
306 int sd = socket(AF_INET, SOCK_DGRAM, 0);
307 if (sd < 0) /* Socket invalid. */
308 {
309 VBoxServiceError("Failed to get a socket: Error %d\n", errno);
310 return -1;
311 }
312
313 ifconf ifcfg;
314 char buffer[1024] = {0};
315 ifcfg.ifc_len = sizeof(buffer);
316 ifcfg.ifc_buf = buffer;
317 if (ioctl(sd, SIOCGIFCONF, &ifcfg) < 0)
318 {
319 VBoxServiceError("Failed to ioctl(SIOCGIFCONF) on socket: Error %d\n", errno);
320 return -1;
321 }
322
323 ifreq* ifrequest = ifcfg.ifc_req;
324 ifreq *ifreqitem = NULL;
325 nNumInterfaces = ifcfg.ifc_len / sizeof(ifreq);
326#endif
327 char szPropPath [FILENAME_MAX] = {0};
328 char szTemp [FILENAME_MAX] = {0};
329 int iCurIface = 0;
330
331 RTStrPrintf(szPropPath, sizeof(szPropPath), "GuestInfo/Net/Count");
332 VboxServiceWritePropInt(g_VMInfoGuestPropSvcClientID, szPropPath, (nNumInterfaces > 1 ? nNumInterfaces-1 : 0));
333
334 /** @todo Use GetAdaptersInfo() and GetAdapterAddresses (IPv4 + IPv6) for more information. */
335 for (int i = 0; i < nNumInterfaces; ++i)
336 {
337 sockaddr_in *pAddress;
338 u_long nFlags = 0;
339#ifdef RT_OS_WINDOWS
340 if (InterfaceList[i].iiFlags & IFF_LOOPBACK) /* Skip loopback device. */
341 continue;
342 nFlags = InterfaceList[i].iiFlags;
343 pAddress = (sockaddr_in *)&(InterfaceList[i].iiAddress);
344#else
345 if (ioctl(sd, SIOCGIFFLAGS, &ifrequest[i]) < 0)
346 {
347 VBoxServiceError("Failed to ioctl(SIOCGIFFLAGS) on socket: Error %d\n", errno);
348 return -1;
349 }
350 if (ifrequest[i].ifr_flags & IFF_LOOPBACK) /* Skip loopback device. */
351 continue;
352 nFlags = ifrequest[i].ifr_flags;
353 pAddress = ((sockaddr_in *)&ifrequest[i].ifr_addr);
354#endif
355 Assert(pAddress);
356 RTStrPrintf(szPropPath, sizeof(szPropPath), "GuestInfo/Net/%d/V4/IP", iCurIface);
357 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, szPropPath, inet_ntoa(pAddress->sin_addr));
358
359#ifdef RT_OS_WINDOWS
360 pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress);
361#else
362 if (ioctl(sd, SIOCGIFBRDADDR, &ifrequest[i]) < 0)
363 {
364 VBoxServiceError("Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno);
365 return -1;
366 }
367 pAddress = (sockaddr_in *)&ifrequest[i].ifr_broadaddr;
368#endif
369 RTStrPrintf(szPropPath, sizeof(szPropPath), "GuestInfo/Net/%d/V4/Broadcast", iCurIface);
370 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, szPropPath, inet_ntoa(pAddress->sin_addr));
371
372#ifdef RT_OS_WINDOWS
373 pAddress = (sockaddr_in *)&(InterfaceList[i].iiNetmask);
374#else
375 if (ioctl(sd, SIOCGIFNETMASK, &ifrequest[i]) < 0)
376 {
377 VBoxServiceError("Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno);
378 return -1;
379 }
380 #ifdef RT_OS_SOLARIS
381 pAddress = (sockaddr_in *)&ifrequest[i].ifr_addr;
382 #else
383 pAddress = (sockaddr_in *)&ifrequest[i].ifr_netmask;
384 #endif
385
386#endif
387 RTStrPrintf(szPropPath, sizeof(szPropPath), "GuestInfo/Net/%d/V4/Netmask", iCurIface);
388 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, szPropPath, inet_ntoa(pAddress->sin_addr));
389
390 if (nFlags & IFF_UP)
391 RTStrPrintf(szTemp, sizeof(szTemp), "Up");
392 else
393 RTStrPrintf(szTemp, sizeof(szTemp), "Down");
394
395 RTStrPrintf(szPropPath, sizeof(szPropPath), "GuestInfo/Net/%d/Status", iCurIface);
396 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, szPropPath, szTemp);
397
398 iCurIface++;
399 }
400#ifdef RT_OS_WINDOWS
401 if (sd) closesocket(sd);
402#else
403 if (sd) close(sd);
404#endif /* !RT_OS_WINDOWS */
405
406 /*
407 * Block for a while.
408 *
409 * The event semaphore takes care of ignoring interruptions and it
410 * allows us to implement service wakeup later.
411 */
412 if (*pfShutdown)
413 break;
414 int rc2 = RTSemEventMultiWait(g_VMInfoEvent, g_VMInfoInterval);
415 if (*pfShutdown)
416 break;
417 if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
418 {
419 VBoxServiceError("RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
420 rc = rc2;
421 break;
422 }
423 }
424
425#ifdef RT_OS_WINDOWS
426 WSACleanup();
427#endif /* !RT_OS_WINDOWS */
428
429 RTSemEventMultiDestroy(g_VMInfoEvent);
430 g_VMInfoEvent = NIL_RTSEMEVENTMULTI;
431 return rc;
432}
433
434
435/** @copydoc VBOXSERVICE::pfnStop */
436static DECLCALLBACK(void) VBoxServiceVMInfoStop(void)
437{
438 RTSemEventMultiSignal(g_VMInfoEvent);
439}
440
441
442/** @copydoc VBOXSERVICE::pfnTerm */
443static DECLCALLBACK(void) VBoxServiceVMInfoTerm(void)
444{
445 int rc;
446
447 if (g_VMInfoEvent != NIL_RTSEMEVENTMULTI)
448 {
449 /** @todo temporary solution: Zap all values which are not valid
450 * anymore when VM goes down (reboot/shutdown ). Needs to
451 * be replaced with "temporary properties" later.
452 *
453 * @todo r=bird: This code isn't called on non-Windows systems. We need
454 * a more formal way of shutting down the service for that to work.
455 */
456 rc = VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, "GuestInfo/OS/LoggedInUsersList", NULL);
457 rc = VboxServiceWritePropInt(g_VMInfoGuestPropSvcClientID, "GuestInfo/OS/LoggedInUsers", 0);
458 if (g_VMInfoLoggedInUsers > 0)
459 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, "GuestInfo/OS/NoLoggedInUsers", "true");
460
461 const char *apszPat[1] = { "/VirtualBox/GuestInfo/Net/*" };
462 rc = VbglR3GuestPropDelSet(g_VMInfoGuestPropSvcClientID, &apszPat[0], RT_ELEMENTS(apszPat));
463 rc = VboxServiceWritePropInt(g_VMInfoGuestPropSvcClientID, "GuestInfo/Net/Count", 0);
464
465 /* Disconnect from guest properties service. */
466 rc = VbglR3GuestPropDisconnect(g_VMInfoGuestPropSvcClientID);
467 if (RT_FAILURE(rc))
468 VBoxServiceError("Failed to disconnect from guest property service! Error: %Rrc\n", rc);
469 g_VMInfoGuestPropSvcClientID = 0;
470
471
472 RTSemEventMultiDestroy(g_VMInfoEvent);
473 g_VMInfoEvent = NIL_RTSEMEVENTMULTI;
474 }
475}
476
477
478/**
479 * The 'vminfo' service description.
480 */
481VBOXSERVICE g_VMInfo =
482{
483 /* pszName. */
484 "vminfo",
485 /* pszDescription. */
486 "Virtual Machine Information",
487 /* pszUsage. */
488 "[--vminfo-interval <ms>]"
489 ,
490 /* pszOptions. */
491 " --vminfo-interval Specifies the interval at which to retrieve the\n"
492 " VM information. The default is 10000 ms.\n"
493 ,
494 /* methods */
495 VBoxServiceVMInfoPreInit,
496 VBoxServiceVMInfoOption,
497 VBoxServiceVMInfoInit,
498 VBoxServiceVMInfoWorker,
499 VBoxServiceVMInfoStop,
500 VBoxServiceVMInfoTerm
501};
502
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