1 | /* $Id: VBoxServiceVMInfo.cpp 27552 2010-03-20 14:59:18Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Virtual Machine Information for the Host.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2010 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 <netinet/in.h>
|
---|
37 | # include <sys/ioctl.h>
|
---|
38 | # include <sys/socket.h>
|
---|
39 | # include <net/if.h>
|
---|
40 | # include <unistd.h>
|
---|
41 | # ifndef RT_OS_FREEBSD /* The header does not exist anymore since FreeBSD 9-current */
|
---|
42 | # include <utmp.h>
|
---|
43 | # endif
|
---|
44 | # ifdef RT_OS_SOLARIS
|
---|
45 | # include <sys/sockio.h>
|
---|
46 | # endif
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | #include <iprt/mem.h>
|
---|
50 | #include <iprt/thread.h>
|
---|
51 | #include <iprt/string.h>
|
---|
52 | #include <iprt/semaphore.h>
|
---|
53 | #include <iprt/system.h>
|
---|
54 | #include <iprt/time.h>
|
---|
55 | #include <iprt/assert.h>
|
---|
56 | #include <VBox/version.h>
|
---|
57 | #include <VBox/VBoxGuestLib.h>
|
---|
58 | #include "VBoxServiceInternal.h"
|
---|
59 | #include "VBoxServiceUtils.h"
|
---|
60 |
|
---|
61 |
|
---|
62 | /*******************************************************************************
|
---|
63 | * Global Variables *
|
---|
64 | *******************************************************************************/
|
---|
65 | /** The vminfo interval (millseconds). */
|
---|
66 | uint32_t g_VMInfoInterval = 0;
|
---|
67 | /** The semaphore we're blocking on. */
|
---|
68 | static RTSEMEVENTMULTI g_VMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
69 | /** The guest property service client ID. */
|
---|
70 | static uint32_t g_VMInfoGuestPropSvcClientID = 0;
|
---|
71 | /** Number of logged in users in OS. */
|
---|
72 | static uint32_t g_cVMInfoLoggedInUsers = UINT32_MAX;
|
---|
73 |
|
---|
74 |
|
---|
75 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
76 | static DECLCALLBACK(int) VBoxServiceVMInfoPreInit(void)
|
---|
77 | {
|
---|
78 | return VINF_SUCCESS;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
83 | static DECLCALLBACK(int) VBoxServiceVMInfoOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
84 | {
|
---|
85 | int rc = -1;
|
---|
86 | if (ppszShort)
|
---|
87 | /* no short options */;
|
---|
88 | else if (!strcmp(argv[*pi], "--vminfo-interval"))
|
---|
89 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
90 | &g_VMInfoInterval, 1, UINT32_MAX - 1);
|
---|
91 | return rc;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
96 | static DECLCALLBACK(int) VBoxServiceVMInfoInit(void)
|
---|
97 | {
|
---|
98 | /*
|
---|
99 | * If not specified, find the right interval default.
|
---|
100 | * Then create the event sem to block on.
|
---|
101 | */
|
---|
102 | if (!g_VMInfoInterval)
|
---|
103 | g_VMInfoInterval = g_DefaultInterval * 1000;
|
---|
104 | if (!g_VMInfoInterval)
|
---|
105 | g_VMInfoInterval = 10 * 1000;
|
---|
106 |
|
---|
107 | int rc = RTSemEventMultiCreate(&g_VMInfoEvent);
|
---|
108 | AssertRCReturn(rc, rc);
|
---|
109 |
|
---|
110 | #ifdef RT_OS_WINDOWS
|
---|
111 | /* Get function pointers. */
|
---|
112 | HMODULE hKernel32 = LoadLibrary("kernel32");
|
---|
113 | if (hKernel32 != NULL)
|
---|
114 | {
|
---|
115 | g_pfnWTSGetActiveConsoleSessionId = (PFNWTSGETACTIVECONSOLESESSIONID)GetProcAddress(hKernel32, "WTSGetActiveConsoleSessionId");
|
---|
116 | FreeLibrary(hKernel32);
|
---|
117 | }
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | rc = VbglR3GuestPropConnect(&g_VMInfoGuestPropSvcClientID);
|
---|
121 | if (RT_SUCCESS(rc))
|
---|
122 | VBoxServiceVerbose(3, "Property Service Client ID: %#x\n", g_VMInfoGuestPropSvcClientID);
|
---|
123 | else
|
---|
124 | {
|
---|
125 | VBoxServiceError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
|
---|
126 | RTSemEventMultiDestroy(g_VMInfoEvent);
|
---|
127 | g_VMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
128 | }
|
---|
129 |
|
---|
130 | return rc;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Writes the properties that won't change while the service is running.
|
---|
136 | *
|
---|
137 | * Errors are ignored.
|
---|
138 | */
|
---|
139 | static void VBoxServiceVMInfoWriteFixedProperties(void)
|
---|
140 | {
|
---|
141 | /*
|
---|
142 | * First get OS information that won't change.
|
---|
143 | */
|
---|
144 | char szInfo[256];
|
---|
145 | int rc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szInfo, sizeof(szInfo));
|
---|
146 | if (RT_SUCCESS(rc))
|
---|
147 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Product", "%s", szInfo);
|
---|
148 | else
|
---|
149 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Product", "");
|
---|
150 |
|
---|
151 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szInfo, sizeof(szInfo));
|
---|
152 | if (RT_SUCCESS(rc))
|
---|
153 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Release", "%s", szInfo);
|
---|
154 | else
|
---|
155 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Release", "");
|
---|
156 |
|
---|
157 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szInfo, sizeof(szInfo));
|
---|
158 | if (RT_SUCCESS(rc))
|
---|
159 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Version", "%s", szInfo);
|
---|
160 | else
|
---|
161 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Version", "");
|
---|
162 |
|
---|
163 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szInfo, sizeof(szInfo));
|
---|
164 | if (RT_SUCCESS(rc))
|
---|
165 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/ServicePack", "%s", szInfo);
|
---|
166 | else
|
---|
167 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/ServicePack", "");
|
---|
168 |
|
---|
169 | /*
|
---|
170 | * Retrieve version information about Guest Additions and installed files (components).
|
---|
171 | */
|
---|
172 | char *pszAddVer;
|
---|
173 | char *pszAddRev;
|
---|
174 | rc = VbglR3GetAdditionsVersion(&pszAddVer, &pszAddRev);
|
---|
175 | if (RT_SUCCESS(rc))
|
---|
176 | {
|
---|
177 | /* Write information to host. */
|
---|
178 | rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Version", "%s", pszAddVer);
|
---|
179 | rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Revision", "%s", pszAddRev);
|
---|
180 | RTStrFree(pszAddVer);
|
---|
181 | RTStrFree(pszAddRev);
|
---|
182 | }
|
---|
183 |
|
---|
184 | #ifdef RT_OS_WINDOWS
|
---|
185 | /*
|
---|
186 | * Do windows specific properties.
|
---|
187 | */
|
---|
188 | char *pszInstDir;
|
---|
189 | rc = VbglR3GetAdditionsInstallationPath(&pszInstDir);
|
---|
190 | if (RT_SUCCESS(rc))
|
---|
191 | {
|
---|
192 | rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/InstallDir", "%s", pszInstDir);
|
---|
193 | RTStrFree(pszInstDir);
|
---|
194 | }
|
---|
195 | VBoxServiceWinGetComponentVersions(g_VMInfoGuestPropSvcClientID);
|
---|
196 | #endif
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
201 | DECLCALLBACK(int) VBoxServiceVMInfoWorker(bool volatile *pfShutdown)
|
---|
202 | {
|
---|
203 | int rc = VINF_SUCCESS;
|
---|
204 |
|
---|
205 | /*
|
---|
206 | * Tell the control thread that it can continue
|
---|
207 | * spawning services.
|
---|
208 | */
|
---|
209 | RTThreadUserSignal(RTThreadSelf());
|
---|
210 |
|
---|
211 | #ifdef RT_OS_WINDOWS
|
---|
212 | /* Required for network information (must be called per thread). */
|
---|
213 | WSADATA wsaData;
|
---|
214 | if (WSAStartup(MAKEWORD(2, 2), &wsaData))
|
---|
215 | VBoxServiceError("WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError()));
|
---|
216 | #endif /* RT_OS_WINDOWS */
|
---|
217 |
|
---|
218 | /*
|
---|
219 | * Write the fixed properties first.
|
---|
220 | */
|
---|
221 | VBoxServiceVMInfoWriteFixedProperties();
|
---|
222 |
|
---|
223 | /*
|
---|
224 | * Now enter the loop retrieving runtime data continuously.
|
---|
225 | */
|
---|
226 | unsigned cErrors = 0;
|
---|
227 | for (;;)
|
---|
228 | {
|
---|
229 | /** @todo r=bird: split this code up into functions!! */
|
---|
230 | /* Enumerate logged in users. */
|
---|
231 | uint32_t cUsersInList = 0;
|
---|
232 | char szUserList[4096] = {0};
|
---|
233 |
|
---|
234 | #ifdef RT_OS_WINDOWS
|
---|
235 | # ifndef TARGET_NT4
|
---|
236 | PLUID paSessions = NULL;
|
---|
237 | ULONG cSession = 0;
|
---|
238 | NTSTATUS r = 0;
|
---|
239 |
|
---|
240 | /* This function can report stale or orphaned interactive logon sessions
|
---|
241 | of already logged off users (especially in Windows 2000). */
|
---|
242 | r = ::LsaEnumerateLogonSessions(&cSession, &paSessions);
|
---|
243 | VBoxServiceVerbose(3, "Users: Found %ld users.\n", cSession);
|
---|
244 | if (r != STATUS_SUCCESS)
|
---|
245 | {
|
---|
246 | VBoxServiceError("LsaEnumerate failed %lu\n", LsaNtStatusToWinError(r));
|
---|
247 | return 1;
|
---|
248 | }
|
---|
249 |
|
---|
250 | PVBOXSERVICEVMINFOPROC paProcs;
|
---|
251 | DWORD cProcs;
|
---|
252 | rc = VBoxServiceVMInfoWinProcessesEnumerate(&paProcs, &cProcs);
|
---|
253 | if (RT_SUCCESS(rc))
|
---|
254 | {
|
---|
255 | for (ULONG i = 0; i < cSession; i++)
|
---|
256 | {
|
---|
257 | VBOXSERVICEVMINFOUSER UserInfo;
|
---|
258 | if ( VBoxServiceVMInfoWinIsLoggedIn(&UserInfo, &paSessions[i])
|
---|
259 | && VBoxServiceVMInfoWinSessionHasProcesses(&paSessions[i], paProcs, cProcs))
|
---|
260 | {
|
---|
261 | if (cUsersInList > 0)
|
---|
262 | strcat(szUserList, ",");
|
---|
263 |
|
---|
264 | cUsersInList++;
|
---|
265 |
|
---|
266 | char *pszTemp;
|
---|
267 | int rc2 = RTUtf16ToUtf8(UserInfo.wszUser, &pszTemp);
|
---|
268 | if (RT_SUCCESS(rc2))
|
---|
269 | {
|
---|
270 | strcat(szUserList, pszTemp);
|
---|
271 | RTMemFree(pszTemp);
|
---|
272 | }
|
---|
273 | else
|
---|
274 | strcat(szUserList, "<string-convertion-error>");
|
---|
275 | }
|
---|
276 | }
|
---|
277 | VBoxServiceVMInfoWinProcessesFree(paProcs);
|
---|
278 | }
|
---|
279 |
|
---|
280 | ::LsaFreeReturnBuffer(paSessions);
|
---|
281 | # endif /* TARGET_NT4 */
|
---|
282 | #elif defined(RT_OS_FREEBSD)
|
---|
283 | /** @todo FreeBSD: Port logged on user info retrival. */
|
---|
284 | #elif defined(RT_OS_OS2)
|
---|
285 | /** @todo OS/2: Port logged on (LAN/local/whatever) user info retrival. */
|
---|
286 | #else
|
---|
287 | rc = utmpname(UTMP_FILE);
|
---|
288 | # ifdef RT_OS_SOLARIS
|
---|
289 | if (rc != 1)
|
---|
290 | # else
|
---|
291 | if (rc != 0)
|
---|
292 | # endif
|
---|
293 | {
|
---|
294 | VBoxServiceError("Could not set UTMP file! Error: %ld\n", errno);
|
---|
295 | }
|
---|
296 | setutent();
|
---|
297 | utmp *ut_user;
|
---|
298 | while ((ut_user = getutent()))
|
---|
299 | {
|
---|
300 | /* Make sure we don't add user names which are not
|
---|
301 | * part of type USER_PROCESS and don't add same users twice. */
|
---|
302 | if ( ut_user->ut_type == USER_PROCESS
|
---|
303 | && strstr(szUserList, ut_user->ut_user) == NULL)
|
---|
304 | {
|
---|
305 | /** @todo Do we really want to filter out double user names? (Same user logged in twice)
|
---|
306 | * bird: If we do, then we must add checks for buffer overflows here! */
|
---|
307 | /** @todo r=bird: strstr will filtering out users with similar names. For
|
---|
308 | * example: smith, smithson, joesmith and bobsmith */
|
---|
309 | if (cUsersInList > 0)
|
---|
310 | strcat(szUserList, ",");
|
---|
311 | strcat(szUserList, ut_user->ut_user);
|
---|
312 | cUsersInList++;
|
---|
313 | }
|
---|
314 | }
|
---|
315 | endutent();
|
---|
316 | #endif /* !RT_OS_WINDOWS */
|
---|
317 |
|
---|
318 | if (cUsersInList > 0)
|
---|
319 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", "%s", szUserList);
|
---|
320 | else
|
---|
321 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", NULL);
|
---|
322 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/LoggedInUsers", "%u", cUsersInList);
|
---|
323 | if ( g_cVMInfoLoggedInUsers != cUsersInList
|
---|
324 | || g_cVMInfoLoggedInUsers == UINT32_MAX)
|
---|
325 | {
|
---|
326 | /* Update this property ONLY if there is a real change from no users to
|
---|
327 | * users or vice versa. The only exception is that the initialization
|
---|
328 | * forces an update, but only once. This ensures consistent property
|
---|
329 | * settings even if the VM aborted previously. */
|
---|
330 | if (cUsersInList == 0)
|
---|
331 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers", "true");
|
---|
332 | else if (g_cVMInfoLoggedInUsers == 0)
|
---|
333 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers", "false");
|
---|
334 | }
|
---|
335 | g_cVMInfoLoggedInUsers = cUsersInList;
|
---|
336 |
|
---|
337 | /*
|
---|
338 | * Get network configuration.
|
---|
339 | */
|
---|
340 | /** @todo Throw this code into a separate function/module? */
|
---|
341 | int nNumInterfaces = 0;
|
---|
342 | #ifdef RT_OS_WINDOWS
|
---|
343 | SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
|
---|
344 | if (sd == SOCKET_ERROR) /* Socket invalid. */
|
---|
345 | {
|
---|
346 | VBoxServiceError("Failed to get a socket: Error %d\n", WSAGetLastError());
|
---|
347 | return -1;
|
---|
348 | }
|
---|
349 |
|
---|
350 | INTERFACE_INFO InterfaceList[20] = {0};
|
---|
351 | unsigned long nBytesReturned = 0;
|
---|
352 | if (WSAIoctl(sd,
|
---|
353 | SIO_GET_INTERFACE_LIST,
|
---|
354 | 0,
|
---|
355 | 0,
|
---|
356 | &InterfaceList,
|
---|
357 | sizeof(InterfaceList),
|
---|
358 | &nBytesReturned,
|
---|
359 | 0,
|
---|
360 | 0) == SOCKET_ERROR)
|
---|
361 | {
|
---|
362 | VBoxServiceError("Failed to WSAIoctl() on socket: Error: %d\n", WSAGetLastError());
|
---|
363 | return -1;
|
---|
364 | }
|
---|
365 | nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO);
|
---|
366 | #else
|
---|
367 | int sd = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
368 | if (sd < 0) /* Socket invalid. */
|
---|
369 | {
|
---|
370 | VBoxServiceError("Failed to get a socket: Error %d\n", errno);
|
---|
371 | return -1;
|
---|
372 | }
|
---|
373 |
|
---|
374 | ifconf ifcfg;
|
---|
375 | char buffer[1024] = {0};
|
---|
376 | ifcfg.ifc_len = sizeof(buffer);
|
---|
377 | ifcfg.ifc_buf = buffer;
|
---|
378 | if (ioctl(sd, SIOCGIFCONF, &ifcfg) < 0)
|
---|
379 | {
|
---|
380 | VBoxServiceError("Failed to ioctl(SIOCGIFCONF) on socket: Error %d\n", errno);
|
---|
381 | return -1;
|
---|
382 | }
|
---|
383 |
|
---|
384 | ifreq* ifrequest = ifcfg.ifc_req;
|
---|
385 | ifreq *ifreqitem = NULL;
|
---|
386 | nNumInterfaces = ifcfg.ifc_len / sizeof(ifreq);
|
---|
387 | #endif
|
---|
388 | char szPropPath [FILENAME_MAX];
|
---|
389 | int iCurIface = 0;
|
---|
390 |
|
---|
391 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/Net/Count", "%d",
|
---|
392 | nNumInterfaces > 1 ? nNumInterfaces-1 : 0);
|
---|
393 |
|
---|
394 | /** @todo Use GetAdaptersInfo() and GetAdapterAddresses (IPv4 + IPv6) for more information. */
|
---|
395 | for (int i = 0; i < nNumInterfaces; ++i)
|
---|
396 | {
|
---|
397 | sockaddr_in *pAddress;
|
---|
398 | u_long nFlags = 0;
|
---|
399 | #ifdef RT_OS_WINDOWS
|
---|
400 | if (InterfaceList[i].iiFlags & IFF_LOOPBACK) /* Skip loopback device. */
|
---|
401 | continue;
|
---|
402 | nFlags = InterfaceList[i].iiFlags;
|
---|
403 | pAddress = (sockaddr_in *)&(InterfaceList[i].iiAddress);
|
---|
404 | #else
|
---|
405 | if (ioctl(sd, SIOCGIFFLAGS, &ifrequest[i]) < 0)
|
---|
406 | {
|
---|
407 | VBoxServiceError("Failed to ioctl(SIOCGIFFLAGS) on socket: Error %d\n", errno);
|
---|
408 | return -1;
|
---|
409 | }
|
---|
410 | if (ifrequest[i].ifr_flags & IFF_LOOPBACK) /* Skip loopback device. */
|
---|
411 | continue;
|
---|
412 | nFlags = ifrequest[i].ifr_flags;
|
---|
413 | pAddress = ((sockaddr_in *)&ifrequest[i].ifr_addr);
|
---|
414 | #endif
|
---|
415 | Assert(pAddress);
|
---|
416 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/IP", iCurIface);
|
---|
417 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
418 |
|
---|
419 | #ifdef RT_OS_WINDOWS
|
---|
420 | pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress);
|
---|
421 | #else
|
---|
422 | if (ioctl(sd, SIOCGIFBRDADDR, &ifrequest[i]) < 0)
|
---|
423 | {
|
---|
424 | VBoxServiceError("Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno);
|
---|
425 | return -1;
|
---|
426 | }
|
---|
427 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_broadaddr;
|
---|
428 | #endif
|
---|
429 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Broadcast", iCurIface);
|
---|
430 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
431 |
|
---|
432 | #ifdef RT_OS_WINDOWS
|
---|
433 | pAddress = (sockaddr_in *)&(InterfaceList[i].iiNetmask);
|
---|
434 | #else
|
---|
435 | if (ioctl(sd, SIOCGIFNETMASK, &ifrequest[i]) < 0)
|
---|
436 | {
|
---|
437 | VBoxServiceError("Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno);
|
---|
438 | return -1;
|
---|
439 | }
|
---|
440 | #if defined(RT_OS_FREEBSD) || defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
|
---|
441 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_addr;
|
---|
442 | #else
|
---|
443 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_netmask;
|
---|
444 | #endif
|
---|
445 |
|
---|
446 | #endif
|
---|
447 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Netmask", iCurIface);
|
---|
448 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
449 |
|
---|
450 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/Status", iCurIface);
|
---|
451 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, szPropPath,
|
---|
452 | nFlags & IFF_UP ? "Up" : "Down");
|
---|
453 |
|
---|
454 | iCurIface++;
|
---|
455 | }
|
---|
456 | if (sd >= 0)
|
---|
457 | #ifdef RT_OS_WINDOWS
|
---|
458 | closesocket(sd);
|
---|
459 | #else
|
---|
460 | close(sd);
|
---|
461 | #endif
|
---|
462 |
|
---|
463 | /*
|
---|
464 | * Block for a while.
|
---|
465 | *
|
---|
466 | * The event semaphore takes care of ignoring interruptions and it
|
---|
467 | * allows us to implement service wakeup later.
|
---|
468 | */
|
---|
469 | if (*pfShutdown)
|
---|
470 | break;
|
---|
471 | int rc2 = RTSemEventMultiWait(g_VMInfoEvent, g_VMInfoInterval);
|
---|
472 | if (*pfShutdown)
|
---|
473 | break;
|
---|
474 | if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
|
---|
475 | {
|
---|
476 | VBoxServiceError("RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
|
---|
477 | rc = rc2;
|
---|
478 | break;
|
---|
479 | }
|
---|
480 | }
|
---|
481 |
|
---|
482 | #ifdef RT_OS_WINDOWS
|
---|
483 | WSACleanup();
|
---|
484 | #endif
|
---|
485 |
|
---|
486 | RTSemEventMultiDestroy(g_VMInfoEvent);
|
---|
487 | g_VMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
488 | return rc;
|
---|
489 | }
|
---|
490 |
|
---|
491 |
|
---|
492 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
493 | static DECLCALLBACK(void) VBoxServiceVMInfoStop(void)
|
---|
494 | {
|
---|
495 | RTSemEventMultiSignal(g_VMInfoEvent);
|
---|
496 | }
|
---|
497 |
|
---|
498 |
|
---|
499 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
500 | static DECLCALLBACK(void) VBoxServiceVMInfoTerm(void)
|
---|
501 | {
|
---|
502 | int rc;
|
---|
503 |
|
---|
504 | if (g_VMInfoEvent != NIL_RTSEMEVENTMULTI)
|
---|
505 | {
|
---|
506 | /** @todo temporary solution: Zap all values which are not valid
|
---|
507 | * anymore when VM goes down (reboot/shutdown ). Needs to
|
---|
508 | * be replaced with "temporary properties" later.
|
---|
509 | *
|
---|
510 | * @todo r=bird: This code isn't called on non-Windows systems. We need
|
---|
511 | * a more formal way of shutting down the service for that to work.
|
---|
512 | */
|
---|
513 | rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", NULL);
|
---|
514 | rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/LoggedInUsers", "%d", 0);
|
---|
515 | if (g_cVMInfoLoggedInUsers > 0)
|
---|
516 | VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers", "true");
|
---|
517 |
|
---|
518 | const char *apszPat[1] = { "/VirtualBox/GuestInfo/Net/*" };
|
---|
519 | rc = VbglR3GuestPropDelSet(g_VMInfoGuestPropSvcClientID, &apszPat[0], RT_ELEMENTS(apszPat));
|
---|
520 | rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/Net/Count", "%d", 0);
|
---|
521 |
|
---|
522 | /* Disconnect from guest properties service. */
|
---|
523 | rc = VbglR3GuestPropDisconnect(g_VMInfoGuestPropSvcClientID);
|
---|
524 | if (RT_FAILURE(rc))
|
---|
525 | VBoxServiceError("Failed to disconnect from guest property service! Error: %Rrc\n", rc);
|
---|
526 | g_VMInfoGuestPropSvcClientID = 0;
|
---|
527 |
|
---|
528 |
|
---|
529 | RTSemEventMultiDestroy(g_VMInfoEvent);
|
---|
530 | g_VMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
531 | }
|
---|
532 | }
|
---|
533 |
|
---|
534 |
|
---|
535 | /**
|
---|
536 | * The 'vminfo' service description.
|
---|
537 | */
|
---|
538 | VBOXSERVICE g_VMInfo =
|
---|
539 | {
|
---|
540 | /* pszName. */
|
---|
541 | "vminfo",
|
---|
542 | /* pszDescription. */
|
---|
543 | "Virtual Machine Information",
|
---|
544 | /* pszUsage. */
|
---|
545 | "[--vminfo-interval <ms>]"
|
---|
546 | ,
|
---|
547 | /* pszOptions. */
|
---|
548 | " --vminfo-interval Specifies the interval at which to retrieve the\n"
|
---|
549 | " VM information. The default is 10000 ms.\n"
|
---|
550 | ,
|
---|
551 | /* methods */
|
---|
552 | VBoxServiceVMInfoPreInit,
|
---|
553 | VBoxServiceVMInfoOption,
|
---|
554 | VBoxServiceVMInfoInit,
|
---|
555 | VBoxServiceVMInfoWorker,
|
---|
556 | VBoxServiceVMInfoStop,
|
---|
557 | VBoxServiceVMInfoTerm
|
---|
558 | };
|
---|
559 |
|
---|