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