1 | /* $Id: VBoxServiceVMInfo.cpp 50555 2014-02-24 12:30:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Virtual Machine Information for the Host.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2013 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 | # ifdef TARGET_NT4 /* HACK ALERT! PMIB_IPSTATS undefined if 0x0400 with newer SDKs. */
|
---|
25 | # undef _WIN32_WINNT
|
---|
26 | # define _WIN32_WINNT 0x0500
|
---|
27 | # endif
|
---|
28 | # include <winsock2.h>
|
---|
29 | # include <iphlpapi.h>
|
---|
30 | # include <ws2tcpip.h>
|
---|
31 | # include <windows.h>
|
---|
32 | # include <Ntsecapi.h>
|
---|
33 | #else
|
---|
34 | # define __STDC_LIMIT_MACROS
|
---|
35 | # include <arpa/inet.h>
|
---|
36 | # include <errno.h>
|
---|
37 | # include <netinet/in.h>
|
---|
38 | # include <sys/ioctl.h>
|
---|
39 | # include <sys/socket.h>
|
---|
40 | # include <net/if.h>
|
---|
41 | # include <pwd.h> /* getpwuid */
|
---|
42 | # include <unistd.h>
|
---|
43 | # if !defined(RT_OS_OS2) && !defined(RT_OS_FREEBSD) && !defined(RT_OS_HAIKU)
|
---|
44 | # include <utmpx.h> /* @todo FreeBSD 9 should have this. */
|
---|
45 | # endif
|
---|
46 | # ifdef RT_OS_SOLARIS
|
---|
47 | # include <sys/sockio.h>
|
---|
48 | # include <net/if_arp.h>
|
---|
49 | # endif
|
---|
50 | # if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
|
---|
51 | # include <ifaddrs.h> /* getifaddrs, freeifaddrs */
|
---|
52 | # include <net/if_dl.h> /* LLADDR */
|
---|
53 | # include <netdb.h> /* getnameinfo */
|
---|
54 | # endif
|
---|
55 | # ifdef VBOX_WITH_DBUS
|
---|
56 | # include <VBox/dbus.h>
|
---|
57 | # endif
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | #include <iprt/mem.h>
|
---|
61 | #include <iprt/thread.h>
|
---|
62 | #include <iprt/string.h>
|
---|
63 | #include <iprt/semaphore.h>
|
---|
64 | #include <iprt/system.h>
|
---|
65 | #include <iprt/time.h>
|
---|
66 | #include <iprt/assert.h>
|
---|
67 | #include <VBox/version.h>
|
---|
68 | #include <VBox/VBoxGuestLib.h>
|
---|
69 | #include "VBoxServiceInternal.h"
|
---|
70 | #include "VBoxServiceUtils.h"
|
---|
71 | #include "VBoxServicePropCache.h"
|
---|
72 |
|
---|
73 |
|
---|
74 | /** Structure containing information about a location awarness
|
---|
75 | * client provided by the host. */
|
---|
76 | /** @todo Move this (and functions) into VbglR3. */
|
---|
77 | typedef struct VBOXSERVICELACLIENTINFO
|
---|
78 | {
|
---|
79 | uint32_t uID;
|
---|
80 | char *pszName;
|
---|
81 | char *pszLocation;
|
---|
82 | char *pszDomain;
|
---|
83 | bool fAttached;
|
---|
84 | uint64_t uAttachedTS;
|
---|
85 | } VBOXSERVICELACLIENTINFO, *PVBOXSERVICELACLIENTINFO;
|
---|
86 |
|
---|
87 |
|
---|
88 | /*******************************************************************************
|
---|
89 | * Global Variables *
|
---|
90 | *******************************************************************************/
|
---|
91 | /** The vminfo interval (milliseconds). */
|
---|
92 | static uint32_t g_cMsVMInfoInterval = 0;
|
---|
93 | /** The semaphore we're blocking on. */
|
---|
94 | static RTSEMEVENTMULTI g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
95 | /** The guest property service client ID. */
|
---|
96 | static uint32_t g_uVMInfoGuestPropSvcClientID = 0;
|
---|
97 | /** Number of currently logged in users in OS. */
|
---|
98 | static uint32_t g_cVMInfoLoggedInUsers = 0;
|
---|
99 | /** The guest property cache. */
|
---|
100 | static VBOXSERVICEVEPROPCACHE g_VMInfoPropCache;
|
---|
101 | static const char *g_pszPropCacheValLoggedInUsersList = "/VirtualBox/GuestInfo/OS/LoggedInUsersList";
|
---|
102 | static const char *g_pszPropCacheValLoggedInUsers = "/VirtualBox/GuestInfo/OS/LoggedInUsers";
|
---|
103 | static const char *g_pszPropCacheValNoLoggedInUsers = "/VirtualBox/GuestInfo/OS/NoLoggedInUsers";
|
---|
104 | static const char *g_pszPropCacheValNetCount = "/VirtualBox/GuestInfo/Net/Count";
|
---|
105 | /** A guest user's guest property root key. */
|
---|
106 | static const char *g_pszPropCacheValUser = "/VirtualBox/GuestInfo/User/";
|
---|
107 | /** The VM session ID. Changes whenever the VM is restored or reset. */
|
---|
108 | static uint64_t g_idVMInfoSession;
|
---|
109 | /** The last attached locartion awareness (LA) client timestamp. */
|
---|
110 | static uint64_t g_LAClientAttachedTS = 0;
|
---|
111 | /** The current LA client info. */
|
---|
112 | static VBOXSERVICELACLIENTINFO g_LAClientInfo;
|
---|
113 | /** User idle threshold (in ms). This specifies the minimum time a user is considered
|
---|
114 | * as being idle and then will be reported to the host. Default is 5s. */
|
---|
115 | uint32_t g_uVMInfoUserIdleThresholdMS = 5 * 1000;
|
---|
116 |
|
---|
117 |
|
---|
118 | /*******************************************************************************
|
---|
119 | * Defines *
|
---|
120 | *******************************************************************************/
|
---|
121 | static const char *g_pszLAActiveClient = "/VirtualBox/HostInfo/VRDP/ActiveClient";
|
---|
122 |
|
---|
123 | #ifdef VBOX_WITH_DBUS
|
---|
124 | /** ConsoleKit defines (taken from 0.4.5). */
|
---|
125 | #define CK_NAME "org.freedesktop.ConsoleKit"
|
---|
126 | #define CK_PATH "/org/freedesktop/ConsoleKit"
|
---|
127 | #define CK_INTERFACE "org.freedesktop.ConsoleKit"
|
---|
128 |
|
---|
129 | #define CK_MANAGER_PATH "/org/freedesktop/ConsoleKit/Manager"
|
---|
130 | #define CK_MANAGER_INTERFACE "org.freedesktop.ConsoleKit.Manager"
|
---|
131 | #define CK_SEAT_INTERFACE "org.freedesktop.ConsoleKit.Seat"
|
---|
132 | #define CK_SESSION_INTERFACE "org.freedesktop.ConsoleKit.Session"
|
---|
133 | #endif
|
---|
134 |
|
---|
135 |
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Signals the event so that a re-enumeration of VM-specific
|
---|
139 | * information (like logged in users) can happen.
|
---|
140 | *
|
---|
141 | * @return IPRT status code.
|
---|
142 | */
|
---|
143 | int VBoxServiceVMInfoSignal(void)
|
---|
144 | {
|
---|
145 | /* Trigger a re-enumeration of all logged-in users by unblocking
|
---|
146 | * the multi event semaphore of the VMInfo thread. */
|
---|
147 | if (g_hVMInfoEvent)
|
---|
148 | return RTSemEventMultiSignal(g_hVMInfoEvent);
|
---|
149 |
|
---|
150 | return VINF_SUCCESS;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
155 | static DECLCALLBACK(int) VBoxServiceVMInfoPreInit(void)
|
---|
156 | {
|
---|
157 | return VINF_SUCCESS;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
162 | static DECLCALLBACK(int) VBoxServiceVMInfoOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
163 | {
|
---|
164 | /** @todo Use RTGetOpt here. */
|
---|
165 |
|
---|
166 | int rc = -1;
|
---|
167 | if (ppszShort)
|
---|
168 | /* no short options */;
|
---|
169 | else if (!strcmp(argv[*pi], "--vminfo-interval"))
|
---|
170 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
171 | &g_cMsVMInfoInterval, 1, UINT32_MAX - 1);
|
---|
172 | else if (!strcmp(argv[*pi], "--vminfo-user-idle-threshold"))
|
---|
173 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
174 | &g_uVMInfoUserIdleThresholdMS, 1, UINT32_MAX - 1);
|
---|
175 | return rc;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
180 | static DECLCALLBACK(int) VBoxServiceVMInfoInit(void)
|
---|
181 | {
|
---|
182 | /*
|
---|
183 | * If not specified, find the right interval default.
|
---|
184 | * Then create the event sem to block on.
|
---|
185 | */
|
---|
186 | if (!g_cMsVMInfoInterval)
|
---|
187 | g_cMsVMInfoInterval = g_DefaultInterval * 1000;
|
---|
188 | if (!g_cMsVMInfoInterval)
|
---|
189 | {
|
---|
190 | /* Set it to 5s by default for location awareness checks. */
|
---|
191 | g_cMsVMInfoInterval = 5 * 1000;
|
---|
192 | }
|
---|
193 |
|
---|
194 | int rc = RTSemEventMultiCreate(&g_hVMInfoEvent);
|
---|
195 | AssertRCReturn(rc, rc);
|
---|
196 |
|
---|
197 | VbglR3GetSessionId(&g_idVMInfoSession);
|
---|
198 | /* The status code is ignored as this information is not available with VBox < 3.2.10. */
|
---|
199 |
|
---|
200 | /* Initialize the LA client object. */
|
---|
201 | RT_ZERO(g_LAClientInfo);
|
---|
202 |
|
---|
203 | rc = VbglR3GuestPropConnect(&g_uVMInfoGuestPropSvcClientID);
|
---|
204 | if (RT_SUCCESS(rc))
|
---|
205 | VBoxServiceVerbose(3, "Property Service Client ID: %#x\n", g_uVMInfoGuestPropSvcClientID);
|
---|
206 | else
|
---|
207 | {
|
---|
208 | /* If the service was not found, we disable this service without
|
---|
209 | causing VBoxService to fail. */
|
---|
210 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
|
---|
211 | {
|
---|
212 | VBoxServiceVerbose(0, "Guest property service is not available, disabling the service\n");
|
---|
213 | rc = VERR_SERVICE_DISABLED;
|
---|
214 | }
|
---|
215 | else
|
---|
216 | VBoxServiceError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
|
---|
217 | RTSemEventMultiDestroy(g_hVMInfoEvent);
|
---|
218 | g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
219 | }
|
---|
220 |
|
---|
221 | if (RT_SUCCESS(rc))
|
---|
222 | {
|
---|
223 | VBoxServicePropCacheCreate(&g_VMInfoPropCache, g_uVMInfoGuestPropSvcClientID);
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * Declare some guest properties with flags and reset values.
|
---|
227 | */
|
---|
228 | int rc2 = VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, g_pszPropCacheValLoggedInUsersList,
|
---|
229 | VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_TRANSIENT, NULL /* Delete on exit */);
|
---|
230 | if (RT_FAILURE(rc2))
|
---|
231 | VBoxServiceError("Failed to init property cache value \"%s\", rc=%Rrc\n", g_pszPropCacheValLoggedInUsersList, rc2);
|
---|
232 |
|
---|
233 | rc2 = VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, g_pszPropCacheValLoggedInUsers,
|
---|
234 | VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_TRANSIENT, "0");
|
---|
235 | if (RT_FAILURE(rc2))
|
---|
236 | VBoxServiceError("Failed to init property cache value \"%s\", rc=%Rrc\n", g_pszPropCacheValLoggedInUsers, rc2);
|
---|
237 |
|
---|
238 | rc2 = VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, g_pszPropCacheValNoLoggedInUsers,
|
---|
239 | VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_TRANSIENT, "true");
|
---|
240 | if (RT_FAILURE(rc2))
|
---|
241 | VBoxServiceError("Failed to init property cache value \"%s\", rc=%Rrc\n", g_pszPropCacheValNoLoggedInUsers, rc2);
|
---|
242 |
|
---|
243 | rc2 = VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, g_pszPropCacheValNetCount,
|
---|
244 | VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_ALWAYS_UPDATE, NULL /* Delete on exit */);
|
---|
245 | if (RT_FAILURE(rc2))
|
---|
246 | VBoxServiceError("Failed to init property cache value \"%s\", rc=%Rrc\n", g_pszPropCacheValNetCount, rc2);
|
---|
247 |
|
---|
248 | /*
|
---|
249 | * Get configuration guest properties from the host.
|
---|
250 | * Note: All properties should have sensible defaults in case the lookup here fails.
|
---|
251 | */
|
---|
252 | char *pszValue;
|
---|
253 | rc2 = VBoxServiceReadHostProp(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--vminfo-user-idle-threshold", true /* Read only */,
|
---|
254 | &pszValue, NULL /* Flags */, NULL /* Timestamp */);
|
---|
255 | if (RT_SUCCESS(rc2))
|
---|
256 | {
|
---|
257 | AssertPtr(pszValue);
|
---|
258 | g_uVMInfoUserIdleThresholdMS = RT_CLAMP(RTStrToUInt32(pszValue), 1000, UINT32_MAX - 1);
|
---|
259 | RTStrFree(pszValue);
|
---|
260 | }
|
---|
261 | }
|
---|
262 | return rc;
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Retrieves a specifiy client LA property.
|
---|
268 | *
|
---|
269 | * @return IPRT status code.
|
---|
270 | * @param uClientID LA client ID to retrieve property for.
|
---|
271 | * @param pszProperty Property (without path) to retrieve.
|
---|
272 | * @param ppszValue Where to store value of property.
|
---|
273 | * @param puTimestamp Timestamp of property to retrieve. Optional.
|
---|
274 | */
|
---|
275 | static int vboxServiceGetLAClientValue(uint32_t uClientID, const char *pszProperty,
|
---|
276 | char **ppszValue, uint64_t *puTimestamp)
|
---|
277 | {
|
---|
278 | AssertReturn(uClientID, VERR_INVALID_PARAMETER);
|
---|
279 | AssertPtrReturn(pszProperty, VERR_INVALID_POINTER);
|
---|
280 |
|
---|
281 | int rc;
|
---|
282 |
|
---|
283 | char pszClientPath[255];
|
---|
284 | if (RTStrPrintf(pszClientPath, sizeof(pszClientPath),
|
---|
285 | "/VirtualBox/HostInfo/VRDP/Client/%RU32/%s", uClientID, pszProperty))
|
---|
286 | {
|
---|
287 | rc = VBoxServiceReadHostProp(g_uVMInfoGuestPropSvcClientID, pszClientPath, true /* Read only */,
|
---|
288 | ppszValue, NULL /* Flags */, puTimestamp);
|
---|
289 | }
|
---|
290 | else
|
---|
291 | rc = VERR_NO_MEMORY;
|
---|
292 |
|
---|
293 | return rc;
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Retrieves LA client information. On success the returned structure will have allocated
|
---|
299 | * objects which need to be free'd with vboxServiceFreeLAClientInfo.
|
---|
300 | *
|
---|
301 | * @return IPRT status code.
|
---|
302 | * @param uClientID Client ID to retrieve information for.
|
---|
303 | * @param pClient Pointer where to store the client information.
|
---|
304 | */
|
---|
305 | static int vboxServiceGetLAClientInfo(uint32_t uClientID, PVBOXSERVICELACLIENTINFO pClient)
|
---|
306 | {
|
---|
307 | AssertReturn(uClientID, VERR_INVALID_PARAMETER);
|
---|
308 | AssertPtrReturn(pClient, VERR_INVALID_POINTER);
|
---|
309 |
|
---|
310 | int rc = vboxServiceGetLAClientValue(uClientID, "Name", &pClient->pszName,
|
---|
311 | NULL /* Timestamp */);
|
---|
312 | if (RT_SUCCESS(rc))
|
---|
313 | {
|
---|
314 | char *pszAttach;
|
---|
315 | rc = vboxServiceGetLAClientValue(uClientID, "Attach", &pszAttach,
|
---|
316 | &pClient->uAttachedTS);
|
---|
317 | if (RT_SUCCESS(rc))
|
---|
318 | {
|
---|
319 | AssertPtr(pszAttach);
|
---|
320 | pClient->fAttached = !RTStrICmp(pszAttach, "1") ? true : false;
|
---|
321 |
|
---|
322 | RTStrFree(pszAttach);
|
---|
323 | }
|
---|
324 | }
|
---|
325 | if (RT_SUCCESS(rc))
|
---|
326 | rc = vboxServiceGetLAClientValue(uClientID, "Location", &pClient->pszLocation,
|
---|
327 | NULL /* Timestamp */);
|
---|
328 | if (RT_SUCCESS(rc))
|
---|
329 | rc = vboxServiceGetLAClientValue(uClientID, "Domain", &pClient->pszDomain,
|
---|
330 | NULL /* Timestamp */);
|
---|
331 | if (RT_SUCCESS(rc))
|
---|
332 | pClient->uID = uClientID;
|
---|
333 |
|
---|
334 | return rc;
|
---|
335 | }
|
---|
336 |
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Frees all allocated LA client information of a structure.
|
---|
340 | *
|
---|
341 | * @param pClient Pointer to client information structure to free.
|
---|
342 | */
|
---|
343 | static void vboxServiceFreeLAClientInfo(PVBOXSERVICELACLIENTINFO pClient)
|
---|
344 | {
|
---|
345 | if (pClient)
|
---|
346 | {
|
---|
347 | if (pClient->pszName)
|
---|
348 | {
|
---|
349 | RTStrFree(pClient->pszName);
|
---|
350 | pClient->pszName = NULL;
|
---|
351 | }
|
---|
352 | if (pClient->pszLocation)
|
---|
353 | {
|
---|
354 | RTStrFree(pClient->pszLocation);
|
---|
355 | pClient->pszLocation = NULL;
|
---|
356 | }
|
---|
357 | if (pClient->pszDomain)
|
---|
358 | {
|
---|
359 | RTStrFree(pClient->pszDomain);
|
---|
360 | pClient->pszDomain = NULL;
|
---|
361 | }
|
---|
362 | }
|
---|
363 | }
|
---|
364 |
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Updates a per-guest user guest property inside the given property cache.
|
---|
368 | *
|
---|
369 | * @return IPRT status code.
|
---|
370 | * @param pCache Pointer to guest property cache to update user in.
|
---|
371 | * @param pszUser Name of guest user to update.
|
---|
372 | * @param pszDomain Domain of guest user to update. Optional.
|
---|
373 | * @param pszKey Key name of guest property to update.
|
---|
374 | * @param pszValueFormat Guest property value to set. Pass NULL for deleting
|
---|
375 | * the property.
|
---|
376 | */
|
---|
377 | int vboxServiceUserUpdateF(PVBOXSERVICEVEPROPCACHE pCache, const char *pszUser, const char *pszDomain,
|
---|
378 | const char *pszKey, const char *pszValueFormat, ...)
|
---|
379 | {
|
---|
380 | AssertPtrReturn(pCache, VERR_INVALID_POINTER);
|
---|
381 | AssertPtrReturn(pszUser, VERR_INVALID_POINTER);
|
---|
382 | /* pszDomain is optional. */
|
---|
383 | AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
|
---|
384 | /* pszValueFormat is optional. */
|
---|
385 |
|
---|
386 | int rc = VINF_SUCCESS;
|
---|
387 |
|
---|
388 | char *pszName;
|
---|
389 | if (pszDomain)
|
---|
390 | {
|
---|
391 | if (!RTStrAPrintf(&pszName, "%s%s@%s/%s", g_pszPropCacheValUser, pszUser, pszDomain, pszKey))
|
---|
392 | rc = VERR_NO_MEMORY;
|
---|
393 | }
|
---|
394 | else
|
---|
395 | {
|
---|
396 | if (!RTStrAPrintf(&pszName, "%s%s/%s", g_pszPropCacheValUser, pszUser, pszKey))
|
---|
397 | rc = VERR_NO_MEMORY;
|
---|
398 | }
|
---|
399 |
|
---|
400 | char *pszValue = NULL;
|
---|
401 | if ( RT_SUCCESS(rc)
|
---|
402 | && pszValueFormat)
|
---|
403 | {
|
---|
404 | va_list va;
|
---|
405 | va_start(va, pszValueFormat);
|
---|
406 | if (RTStrAPrintfV(&pszValue, pszValueFormat, va) < 0)
|
---|
407 | rc = VERR_NO_MEMORY;
|
---|
408 | va_end(va);
|
---|
409 | if ( RT_SUCCESS(rc)
|
---|
410 | && !pszValue)
|
---|
411 | rc = VERR_NO_STR_MEMORY;
|
---|
412 | }
|
---|
413 |
|
---|
414 | if (RT_SUCCESS(rc))
|
---|
415 | rc = VBoxServicePropCacheUpdate(pCache, pszName, pszValue);
|
---|
416 | if (rc == VINF_SUCCESS) /* VBoxServicePropCacheUpdate will also return VINF_NO_CHANGE. */
|
---|
417 | {
|
---|
418 | /** @todo Combine updating flags w/ updating the actual value. */
|
---|
419 | rc = VBoxServicePropCacheUpdateEntry(pCache, pszName,
|
---|
420 | VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_TRANSIENT,
|
---|
421 | NULL /* Delete on exit */);
|
---|
422 | }
|
---|
423 |
|
---|
424 | RTStrFree(pszValue);
|
---|
425 | RTStrFree(pszName);
|
---|
426 | return rc;
|
---|
427 | }
|
---|
428 |
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * Writes the properties that won't change while the service is running.
|
---|
432 | *
|
---|
433 | * Errors are ignored.
|
---|
434 | */
|
---|
435 | static void vboxserviceVMInfoWriteFixedProperties(void)
|
---|
436 | {
|
---|
437 | /*
|
---|
438 | * First get OS information that won't change.
|
---|
439 | */
|
---|
440 | char szInfo[256];
|
---|
441 | int rc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szInfo, sizeof(szInfo));
|
---|
442 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Product",
|
---|
443 | "%s", RT_FAILURE(rc) ? "" : szInfo);
|
---|
444 |
|
---|
445 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szInfo, sizeof(szInfo));
|
---|
446 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Release",
|
---|
447 | "%s", RT_FAILURE(rc) ? "" : szInfo);
|
---|
448 |
|
---|
449 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szInfo, sizeof(szInfo));
|
---|
450 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Version",
|
---|
451 | "%s", RT_FAILURE(rc) ? "" : szInfo);
|
---|
452 |
|
---|
453 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szInfo, sizeof(szInfo));
|
---|
454 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/ServicePack",
|
---|
455 | "%s", RT_FAILURE(rc) ? "" : szInfo);
|
---|
456 |
|
---|
457 | /*
|
---|
458 | * Retrieve version information about Guest Additions and installed files (components).
|
---|
459 | */
|
---|
460 | char *pszAddVer;
|
---|
461 | char *pszAddVerExt;
|
---|
462 | char *pszAddRev;
|
---|
463 | rc = VbglR3GetAdditionsVersion(&pszAddVer, &pszAddVerExt, &pszAddRev);
|
---|
464 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Version",
|
---|
465 | "%s", RT_FAILURE(rc) ? "" : pszAddVer);
|
---|
466 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/VersionExt",
|
---|
467 | "%s", RT_FAILURE(rc) ? "" : pszAddVerExt);
|
---|
468 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Revision",
|
---|
469 | "%s", RT_FAILURE(rc) ? "" : pszAddRev);
|
---|
470 | if (RT_SUCCESS(rc))
|
---|
471 | {
|
---|
472 | RTStrFree(pszAddVer);
|
---|
473 | RTStrFree(pszAddVerExt);
|
---|
474 | RTStrFree(pszAddRev);
|
---|
475 | }
|
---|
476 |
|
---|
477 | #ifdef RT_OS_WINDOWS
|
---|
478 | /*
|
---|
479 | * Do windows specific properties.
|
---|
480 | */
|
---|
481 | char *pszInstDir;
|
---|
482 | rc = VbglR3GetAdditionsInstallationPath(&pszInstDir);
|
---|
483 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/InstallDir",
|
---|
484 | "%s", RT_FAILURE(rc) ? "" : pszInstDir);
|
---|
485 | if (RT_SUCCESS(rc))
|
---|
486 | RTStrFree(pszInstDir);
|
---|
487 |
|
---|
488 | VBoxServiceWinGetComponentVersions(g_uVMInfoGuestPropSvcClientID);
|
---|
489 | #endif
|
---|
490 | }
|
---|
491 |
|
---|
492 | #if defined(VBOX_WITH_DBUS) && defined(RT_OS_LINUX) /* Not yet for Solaris/FreeBSB. */
|
---|
493 | /*
|
---|
494 | * Simple wrapper to work around compiler-specific va_list madness.
|
---|
495 | */
|
---|
496 | static dbus_bool_t vboxService_dbus_message_get_args(DBusMessage *message,
|
---|
497 | DBusError *error,
|
---|
498 | int first_arg_type,
|
---|
499 | ...)
|
---|
500 | {
|
---|
501 | va_list va;
|
---|
502 | va_start(va, first_arg_type);
|
---|
503 | dbus_bool_t ret = dbus_message_get_args_valist(message, error,
|
---|
504 | first_arg_type, va);
|
---|
505 | va_end(va);
|
---|
506 | return ret;
|
---|
507 | }
|
---|
508 | #endif
|
---|
509 |
|
---|
510 | /**
|
---|
511 | * Provide information about active users.
|
---|
512 | */
|
---|
513 | static int vboxserviceVMInfoWriteUsers(void)
|
---|
514 | {
|
---|
515 | int rc = VINF_SUCCESS;
|
---|
516 | char *pszUserList = NULL;
|
---|
517 | uint32_t cUsersInList = 0;
|
---|
518 |
|
---|
519 | #ifdef RT_OS_WINDOWS
|
---|
520 | # ifndef TARGET_NT4
|
---|
521 | rc = VBoxServiceVMInfoWinWriteUsers(&g_VMInfoPropCache,
|
---|
522 | &pszUserList, &cUsersInList);
|
---|
523 | # else
|
---|
524 | rc = VERR_NOT_IMPLEMENTED;
|
---|
525 | # endif
|
---|
526 |
|
---|
527 | #elif defined(RT_OS_FREEBSD)
|
---|
528 | /** @todo FreeBSD: Port logged on user info retrieval.
|
---|
529 | * However, FreeBSD 9 supports utmpx, so we could use the code
|
---|
530 | * block below (?). */
|
---|
531 | rc = VERR_NOT_IMPLEMENTED;
|
---|
532 |
|
---|
533 | #elif defined(RT_OS_HAIKU)
|
---|
534 | /** @todo Haiku: Port logged on user info retrieval. */
|
---|
535 | rc = VERR_NOT_IMPLEMENTED;
|
---|
536 |
|
---|
537 | #elif defined(RT_OS_OS2)
|
---|
538 | /** @todo OS/2: Port logged on (LAN/local/whatever) user info retrieval. */
|
---|
539 | rc = VERR_NOT_IMPLEMENTED;
|
---|
540 |
|
---|
541 | #else
|
---|
542 | setutxent();
|
---|
543 | utmpx *ut_user;
|
---|
544 | uint32_t cListSize = 32;
|
---|
545 |
|
---|
546 | /* Allocate a first array to hold 32 users max. */
|
---|
547 | char **papszUsers = (char **)RTMemAllocZ(cListSize * sizeof(char *));
|
---|
548 | if (papszUsers == NULL)
|
---|
549 | rc = VERR_NO_MEMORY;
|
---|
550 |
|
---|
551 | /* Process all entries in the utmp file.
|
---|
552 | * Note: This only handles */
|
---|
553 | while ( (ut_user = getutxent())
|
---|
554 | && RT_SUCCESS(rc))
|
---|
555 | {
|
---|
556 | # ifdef RT_OS_DARWIN /* No ut_user->ut_session on Darwin */
|
---|
557 | VBoxServiceVerbose(4, "Found entry \"%s\" (type: %d, PID: %RU32)\n",
|
---|
558 | ut_user->ut_user, ut_user->ut_type, ut_user->ut_pid);
|
---|
559 | # else
|
---|
560 | VBoxServiceVerbose(4, "Found entry \"%s\" (type: %d, PID: %RU32, session: %RU32)\n",
|
---|
561 | ut_user->ut_user, ut_user->ut_type, ut_user->ut_pid, ut_user->ut_session);
|
---|
562 | # endif
|
---|
563 | if (cUsersInList > cListSize)
|
---|
564 | {
|
---|
565 | cListSize += 32;
|
---|
566 | void *pvNew = RTMemRealloc(papszUsers, cListSize * sizeof(char*));
|
---|
567 | AssertPtrBreakStmt(pvNew, cListSize -= 32);
|
---|
568 | papszUsers = (char **)pvNew;
|
---|
569 | }
|
---|
570 |
|
---|
571 | /* Make sure we don't add user names which are not
|
---|
572 | * part of type USER_PROCES. */
|
---|
573 | if (ut_user->ut_type == USER_PROCESS) /* Regular user process. */
|
---|
574 | {
|
---|
575 | bool fFound = false;
|
---|
576 | for (uint32_t i = 0; i < cUsersInList && !fFound; i++)
|
---|
577 | fFound = strcmp(papszUsers[i], ut_user->ut_user) == 0;
|
---|
578 |
|
---|
579 | if (!fFound)
|
---|
580 | {
|
---|
581 | VBoxServiceVerbose(4, "Adding user \"%s\" (type: %d) to list\n",
|
---|
582 | ut_user->ut_user, ut_user->ut_type);
|
---|
583 |
|
---|
584 | rc = RTStrDupEx(&papszUsers[cUsersInList], (const char *)ut_user->ut_user);
|
---|
585 | if (RT_FAILURE(rc))
|
---|
586 | break;
|
---|
587 | cUsersInList++;
|
---|
588 | }
|
---|
589 | }
|
---|
590 | }
|
---|
591 |
|
---|
592 | # ifdef VBOX_WITH_DBUS
|
---|
593 | # if defined(RT_OS_LINUX) /* Not yet for Solaris/FreeBSB. */
|
---|
594 | DBusError dbErr;
|
---|
595 | DBusConnection *pConnection = NULL;
|
---|
596 | int rc2 = RTDBusLoadLib();
|
---|
597 | if (RT_SUCCESS(rc2))
|
---|
598 | {
|
---|
599 | /* Handle desktop sessions using ConsoleKit. */
|
---|
600 | VBoxServiceVerbose(4, "Checking ConsoleKit sessions ...\n");
|
---|
601 |
|
---|
602 | dbus_error_init(&dbErr);
|
---|
603 | pConnection = dbus_bus_get(DBUS_BUS_SYSTEM, &dbErr);
|
---|
604 | }
|
---|
605 |
|
---|
606 | if ( pConnection
|
---|
607 | && !dbus_error_is_set(&dbErr))
|
---|
608 | {
|
---|
609 | /* Get all available sessions. */
|
---|
610 | DBusMessage *pMsgSessions = dbus_message_new_method_call("org.freedesktop.ConsoleKit",
|
---|
611 | "/org/freedesktop/ConsoleKit/Manager",
|
---|
612 | "org.freedesktop.ConsoleKit.Manager",
|
---|
613 | "GetSessions");
|
---|
614 | if ( pMsgSessions
|
---|
615 | && (dbus_message_get_type(pMsgSessions) == DBUS_MESSAGE_TYPE_METHOD_CALL))
|
---|
616 | {
|
---|
617 | DBusMessage *pReplySessions = dbus_connection_send_with_reply_and_block(pConnection,
|
---|
618 | pMsgSessions, 30 * 1000 /* 30s timeout */,
|
---|
619 | &dbErr);
|
---|
620 | if ( pReplySessions
|
---|
621 | && !dbus_error_is_set(&dbErr))
|
---|
622 | {
|
---|
623 | char **ppszSessions; int cSessions;
|
---|
624 | if ( (dbus_message_get_type(pMsgSessions) == DBUS_MESSAGE_TYPE_METHOD_CALL)
|
---|
625 | && vboxService_dbus_message_get_args(pReplySessions, &dbErr, DBUS_TYPE_ARRAY,
|
---|
626 | DBUS_TYPE_OBJECT_PATH, &ppszSessions, &cSessions,
|
---|
627 | DBUS_TYPE_INVALID /* Termination */))
|
---|
628 | {
|
---|
629 | VBoxServiceVerbose(4, "ConsoleKit: retrieved %RU16 session(s)\n", cSessions);
|
---|
630 |
|
---|
631 | char **ppszCurSession = ppszSessions;
|
---|
632 | for (ppszCurSession;
|
---|
633 | ppszCurSession && *ppszCurSession; ppszCurSession++)
|
---|
634 | {
|
---|
635 | VBoxServiceVerbose(4, "ConsoleKit: processing session '%s' ...\n", *ppszCurSession);
|
---|
636 |
|
---|
637 | /* Only respect active sessions .*/
|
---|
638 | bool fActive = false;
|
---|
639 | DBusMessage *pMsgSessionActive = dbus_message_new_method_call("org.freedesktop.ConsoleKit",
|
---|
640 | *ppszCurSession,
|
---|
641 | "org.freedesktop.ConsoleKit.Session",
|
---|
642 | "IsActive");
|
---|
643 | if ( pMsgSessionActive
|
---|
644 | && dbus_message_get_type(pMsgSessionActive) == DBUS_MESSAGE_TYPE_METHOD_CALL)
|
---|
645 | {
|
---|
646 | DBusMessage *pReplySessionActive = dbus_connection_send_with_reply_and_block(pConnection,
|
---|
647 | pMsgSessionActive, 30 * 1000 /* 30s timeout */,
|
---|
648 | &dbErr);
|
---|
649 | if ( pReplySessionActive
|
---|
650 | && !dbus_error_is_set(&dbErr))
|
---|
651 | {
|
---|
652 | DBusMessageIter itMsg;
|
---|
653 | if ( dbus_message_iter_init(pReplySessionActive, &itMsg)
|
---|
654 | && dbus_message_iter_get_arg_type(&itMsg) == DBUS_TYPE_BOOLEAN)
|
---|
655 | {
|
---|
656 | /* Get uid from message. */
|
---|
657 | int val;
|
---|
658 | dbus_message_iter_get_basic(&itMsg, &val);
|
---|
659 | fActive = val >= 1;
|
---|
660 | }
|
---|
661 |
|
---|
662 | if (pReplySessionActive)
|
---|
663 | dbus_message_unref(pReplySessionActive);
|
---|
664 | }
|
---|
665 |
|
---|
666 | if (pMsgSessionActive)
|
---|
667 | dbus_message_unref(pMsgSessionActive);
|
---|
668 | }
|
---|
669 |
|
---|
670 | VBoxServiceVerbose(4, "ConsoleKit: session '%s' is %s\n",
|
---|
671 | *ppszCurSession, fActive ? "active" : "not active");
|
---|
672 |
|
---|
673 | /* *ppszCurSession now contains the object path
|
---|
674 | * (e.g. "/org/freedesktop/ConsoleKit/Session1"). */
|
---|
675 | DBusMessage *pMsgUnixUser = dbus_message_new_method_call("org.freedesktop.ConsoleKit",
|
---|
676 | *ppszCurSession,
|
---|
677 | "org.freedesktop.ConsoleKit.Session",
|
---|
678 | "GetUnixUser");
|
---|
679 | if ( fActive
|
---|
680 | && pMsgUnixUser
|
---|
681 | && dbus_message_get_type(pMsgUnixUser) == DBUS_MESSAGE_TYPE_METHOD_CALL)
|
---|
682 | {
|
---|
683 | DBusMessage *pReplyUnixUser = dbus_connection_send_with_reply_and_block(pConnection,
|
---|
684 | pMsgUnixUser, 30 * 1000 /* 30s timeout */,
|
---|
685 | &dbErr);
|
---|
686 | if ( pReplyUnixUser
|
---|
687 | && !dbus_error_is_set(&dbErr))
|
---|
688 | {
|
---|
689 | DBusMessageIter itMsg;
|
---|
690 | if ( dbus_message_iter_init(pReplyUnixUser, &itMsg)
|
---|
691 | && dbus_message_iter_get_arg_type(&itMsg) == DBUS_TYPE_UINT32)
|
---|
692 | {
|
---|
693 | /* Get uid from message. */
|
---|
694 | uint32_t uid;
|
---|
695 | dbus_message_iter_get_basic(&itMsg, &uid);
|
---|
696 |
|
---|
697 | /** @todo Add support for getting UID_MIN (/etc/login.defs on
|
---|
698 | * Debian). */
|
---|
699 | uint32_t uid_min = 1000;
|
---|
700 |
|
---|
701 | /* Look up user name (realname) from uid. */
|
---|
702 | setpwent();
|
---|
703 | struct passwd *ppwEntry = getpwuid(uid);
|
---|
704 | if ( ppwEntry
|
---|
705 | && ppwEntry->pw_uid >= uid_min /* Only respect users, not daemons etc. */
|
---|
706 | && ppwEntry->pw_name)
|
---|
707 | {
|
---|
708 | VBoxServiceVerbose(4, "ConsoleKit: session '%s' -> %s (uid: %RU32)\n",
|
---|
709 | *ppszCurSession, ppwEntry->pw_name, uid);
|
---|
710 |
|
---|
711 | bool fFound = false;
|
---|
712 | for (uint32_t i = 0; i < cUsersInList && !fFound; i++)
|
---|
713 | fFound = strcmp(papszUsers[i], ppwEntry->pw_name) == 0;
|
---|
714 |
|
---|
715 | if (!fFound)
|
---|
716 | {
|
---|
717 | VBoxServiceVerbose(4, "ConsoleKit: adding user \"%s\" to list\n",
|
---|
718 | ppwEntry->pw_name);
|
---|
719 |
|
---|
720 | rc = RTStrDupEx(&papszUsers[cUsersInList], (const char *)ppwEntry->pw_name);
|
---|
721 | if (RT_FAILURE(rc))
|
---|
722 | break;
|
---|
723 | cUsersInList++;
|
---|
724 | }
|
---|
725 | }
|
---|
726 | else
|
---|
727 | VBoxServiceError("ConsoleKit: unable to lookup user name for uid=%RU32\n", uid);
|
---|
728 | }
|
---|
729 | else
|
---|
730 | AssertMsgFailed(("ConsoleKit: GetUnixUser returned a wrong argument type\n"));
|
---|
731 | }
|
---|
732 |
|
---|
733 | if (pReplyUnixUser)
|
---|
734 | dbus_message_unref(pReplyUnixUser);
|
---|
735 | }
|
---|
736 | else
|
---|
737 | VBoxServiceError("ConsoleKit: unable to retrieve user for session '%s' (msg type=%d): %s",
|
---|
738 | *ppszCurSession, dbus_message_get_type(pMsgUnixUser),
|
---|
739 | dbus_error_is_set(&dbErr) ? dbErr.message : "No error information available");
|
---|
740 |
|
---|
741 | if (pMsgUnixUser)
|
---|
742 | dbus_message_unref(pMsgUnixUser);
|
---|
743 | }
|
---|
744 |
|
---|
745 | dbus_free_string_array(ppszSessions);
|
---|
746 | }
|
---|
747 | else
|
---|
748 | {
|
---|
749 | VBoxServiceError("ConsoleKit: unable to retrieve session parameters (msg type=%d): %s",
|
---|
750 | dbus_message_get_type(pMsgSessions),
|
---|
751 | dbus_error_is_set(&dbErr) ? dbErr.message : "No error information available");
|
---|
752 | }
|
---|
753 | dbus_message_unref(pReplySessions);
|
---|
754 | }
|
---|
755 |
|
---|
756 | if (pMsgSessions)
|
---|
757 | {
|
---|
758 | dbus_message_unref(pMsgSessions);
|
---|
759 | pMsgSessions = NULL;
|
---|
760 | }
|
---|
761 | }
|
---|
762 | else
|
---|
763 | {
|
---|
764 | static int s_iBitchedAboutConsoleKit = 0;
|
---|
765 | if (s_iBitchedAboutConsoleKit++ < 3)
|
---|
766 | VBoxServiceError("Unable to invoke ConsoleKit (%d/3) -- maybe not installed / used? Error: %s\n",
|
---|
767 | s_iBitchedAboutConsoleKit,
|
---|
768 | dbus_error_is_set(&dbErr) ? dbErr.message : "No error information available");
|
---|
769 | }
|
---|
770 |
|
---|
771 | if (pMsgSessions)
|
---|
772 | dbus_message_unref(pMsgSessions);
|
---|
773 | }
|
---|
774 | else
|
---|
775 | {
|
---|
776 | static int s_iBitchedAboutDBus = 0;
|
---|
777 | if (s_iBitchedAboutDBus++ < 3)
|
---|
778 | VBoxServiceError("Unable to connect to system D-Bus (%d/3): %s\n", s_iBitchedAboutDBus,
|
---|
779 | pConnection && dbus_error_is_set(&dbErr) ? dbErr.message : "D-Bus not installed");
|
---|
780 | }
|
---|
781 |
|
---|
782 | if ( pConnection
|
---|
783 | && dbus_error_is_set(&dbErr))
|
---|
784 | dbus_error_free(&dbErr);
|
---|
785 | # endif /* RT_OS_LINUX */
|
---|
786 | # endif /* VBOX_WITH_DBUS */
|
---|
787 |
|
---|
788 | /** @todo Fedora/others: Handle systemd-loginctl. */
|
---|
789 |
|
---|
790 | /* Calc the string length. */
|
---|
791 | size_t cchUserList = 0;
|
---|
792 | if (RT_SUCCESS(rc))
|
---|
793 | {
|
---|
794 | for (uint32_t i = 0; i < cUsersInList; i++)
|
---|
795 | cchUserList += (i != 0) + strlen(papszUsers[i]);
|
---|
796 | }
|
---|
797 |
|
---|
798 | /* Build the user list. */
|
---|
799 | if (cchUserList > 0)
|
---|
800 | {
|
---|
801 | if (RT_SUCCESS(rc))
|
---|
802 | rc = RTStrAllocEx(&pszUserList, cchUserList + 1);
|
---|
803 | if (RT_SUCCESS(rc))
|
---|
804 | {
|
---|
805 | char *psz = pszUserList;
|
---|
806 | for (uint32_t i = 0; i < cUsersInList; i++)
|
---|
807 | {
|
---|
808 | if (i != 0)
|
---|
809 | *psz++ = ',';
|
---|
810 | size_t cch = strlen(papszUsers[i]);
|
---|
811 | memcpy(psz, papszUsers[i], cch);
|
---|
812 | psz += cch;
|
---|
813 | }
|
---|
814 | *psz = '\0';
|
---|
815 | }
|
---|
816 | }
|
---|
817 |
|
---|
818 | /* Cleanup. */
|
---|
819 | for (uint32_t i = 0; i < cUsersInList; i++)
|
---|
820 | RTStrFree(papszUsers[i]);
|
---|
821 | RTMemFree(papszUsers);
|
---|
822 |
|
---|
823 | endutxent(); /* Close utmpx file. */
|
---|
824 | #endif /* !RT_OS_WINDOWS && !RT_OS_FREEBSD && !RT_OS_HAIKU && !RT_OS_OS2 */
|
---|
825 |
|
---|
826 | Assert(RT_FAILURE(rc) || cUsersInList == 0 || (pszUserList && *pszUserList));
|
---|
827 |
|
---|
828 | /* If the user enumeration above failed, reset the user count to 0 except
|
---|
829 | * we didn't have enough memory anymore. In that case we want to preserve
|
---|
830 | * the previous user count in order to not confuse third party tools which
|
---|
831 | * rely on that count. */
|
---|
832 | if (RT_FAILURE(rc))
|
---|
833 | {
|
---|
834 | if (rc == VERR_NO_MEMORY)
|
---|
835 | {
|
---|
836 | static int s_iVMInfoBitchedOOM = 0;
|
---|
837 | if (s_iVMInfoBitchedOOM++ < 3)
|
---|
838 | VBoxServiceVerbose(0, "Warning: Not enough memory available to enumerate users! Keeping old value (%RU32)\n",
|
---|
839 | g_cVMInfoLoggedInUsers);
|
---|
840 | cUsersInList = g_cVMInfoLoggedInUsers;
|
---|
841 | }
|
---|
842 | else
|
---|
843 | cUsersInList = 0;
|
---|
844 | }
|
---|
845 | else /* Preserve logged in users count. */
|
---|
846 | g_cVMInfoLoggedInUsers = cUsersInList;
|
---|
847 |
|
---|
848 | VBoxServiceVerbose(4, "cUsersInList=%RU32, pszUserList=%s, rc=%Rrc\n",
|
---|
849 | cUsersInList, pszUserList ? pszUserList : "<NULL>", rc);
|
---|
850 |
|
---|
851 | if (pszUserList)
|
---|
852 | {
|
---|
853 | AssertMsg(cUsersInList, ("pszUserList contains users whereas cUsersInList is 0\n"));
|
---|
854 | rc = VBoxServicePropCacheUpdate(&g_VMInfoPropCache, g_pszPropCacheValLoggedInUsersList, "%s", pszUserList);
|
---|
855 | }
|
---|
856 | else
|
---|
857 | rc = VBoxServicePropCacheUpdate(&g_VMInfoPropCache, g_pszPropCacheValLoggedInUsersList, NULL);
|
---|
858 | if (RT_FAILURE(rc))
|
---|
859 | VBoxServiceError("Error writing logged in users list, rc=%Rrc\n", rc);
|
---|
860 |
|
---|
861 | rc = VBoxServicePropCacheUpdate(&g_VMInfoPropCache, g_pszPropCacheValLoggedInUsers, "%RU32", cUsersInList);
|
---|
862 | if (RT_FAILURE(rc))
|
---|
863 | VBoxServiceError("Error writing logged in users count, rc=%Rrc\n", rc);
|
---|
864 |
|
---|
865 | rc = VBoxServicePropCacheUpdate(&g_VMInfoPropCache, g_pszPropCacheValNoLoggedInUsers,
|
---|
866 | cUsersInList == 0 ? "true" : "false");
|
---|
867 | if (RT_FAILURE(rc))
|
---|
868 | VBoxServiceError("Error writing no logged in users beacon, rc=%Rrc\n", rc);
|
---|
869 |
|
---|
870 | if (pszUserList)
|
---|
871 | RTStrFree(pszUserList);
|
---|
872 |
|
---|
873 | VBoxServiceVerbose(4, "Writing users returned with rc=%Rrc\n", rc);
|
---|
874 | return rc;
|
---|
875 | }
|
---|
876 |
|
---|
877 |
|
---|
878 | /**
|
---|
879 | * Provide information about the guest network.
|
---|
880 | */
|
---|
881 | static int vboxserviceVMInfoWriteNetwork(void)
|
---|
882 | {
|
---|
883 | int rc = VINF_SUCCESS;
|
---|
884 | uint32_t cIfacesReport = 0;
|
---|
885 | char szPropPath[256];
|
---|
886 |
|
---|
887 | #ifdef RT_OS_WINDOWS
|
---|
888 | IP_ADAPTER_INFO *pAdpInfo = NULL;
|
---|
889 |
|
---|
890 | # ifndef TARGET_NT4
|
---|
891 | ULONG cbAdpInfo = sizeof(*pAdpInfo);
|
---|
892 | pAdpInfo = (IP_ADAPTER_INFO *)RTMemAlloc(cbAdpInfo);
|
---|
893 | if (!pAdpInfo)
|
---|
894 | {
|
---|
895 | VBoxServiceError("VMInfo/Network: Failed to allocate IP_ADAPTER_INFO\n");
|
---|
896 | return VERR_NO_MEMORY;
|
---|
897 | }
|
---|
898 | DWORD dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
|
---|
899 | if (dwRet == ERROR_BUFFER_OVERFLOW)
|
---|
900 | {
|
---|
901 | IP_ADAPTER_INFO *pAdpInfoNew = (IP_ADAPTER_INFO*)RTMemRealloc(pAdpInfo, cbAdpInfo);
|
---|
902 | if (pAdpInfoNew)
|
---|
903 | {
|
---|
904 | pAdpInfo = pAdpInfoNew;
|
---|
905 | dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
|
---|
906 | }
|
---|
907 | }
|
---|
908 | else if (dwRet == ERROR_NO_DATA)
|
---|
909 | {
|
---|
910 | VBoxServiceVerbose(3, "VMInfo/Network: No network adapters available\n");
|
---|
911 |
|
---|
912 | /* If no network adapters available / present in the
|
---|
913 | * system we pretend success to not bail out too early. */
|
---|
914 | dwRet = ERROR_SUCCESS;
|
---|
915 | }
|
---|
916 |
|
---|
917 | if (dwRet != ERROR_SUCCESS)
|
---|
918 | {
|
---|
919 | if (pAdpInfo)
|
---|
920 | RTMemFree(pAdpInfo);
|
---|
921 | VBoxServiceError("VMInfo/Network: Failed to get adapter info: Error %d\n", dwRet);
|
---|
922 | return RTErrConvertFromWin32(dwRet);
|
---|
923 | }
|
---|
924 | # endif /* !TARGET_NT4 */
|
---|
925 |
|
---|
926 | SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
|
---|
927 | if (sd == SOCKET_ERROR) /* Socket invalid. */
|
---|
928 | {
|
---|
929 | int wsaErr = WSAGetLastError();
|
---|
930 | /* Don't complain/bail out with an error if network stack is not up; can happen
|
---|
931 | * on NT4 due to start up when not connected shares dialogs pop up. */
|
---|
932 | if (WSAENETDOWN == wsaErr)
|
---|
933 | {
|
---|
934 | VBoxServiceVerbose(0, "VMInfo/Network: Network is not up yet.\n");
|
---|
935 | wsaErr = VINF_SUCCESS;
|
---|
936 | }
|
---|
937 | else
|
---|
938 | VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %d\n", wsaErr);
|
---|
939 | if (pAdpInfo)
|
---|
940 | RTMemFree(pAdpInfo);
|
---|
941 | return RTErrConvertFromWin32(wsaErr);
|
---|
942 | }
|
---|
943 |
|
---|
944 | INTERFACE_INFO InterfaceList[20] = {0};
|
---|
945 | unsigned long nBytesReturned = 0;
|
---|
946 | if (WSAIoctl(sd,
|
---|
947 | SIO_GET_INTERFACE_LIST,
|
---|
948 | 0,
|
---|
949 | 0,
|
---|
950 | &InterfaceList,
|
---|
951 | sizeof(InterfaceList),
|
---|
952 | &nBytesReturned,
|
---|
953 | 0,
|
---|
954 | 0) == SOCKET_ERROR)
|
---|
955 | {
|
---|
956 | VBoxServiceError("VMInfo/Network: Failed to WSAIoctl() on socket: Error: %d\n", WSAGetLastError());
|
---|
957 | if (pAdpInfo)
|
---|
958 | RTMemFree(pAdpInfo);
|
---|
959 | return RTErrConvertFromWin32(WSAGetLastError());
|
---|
960 | }
|
---|
961 | int cIfacesSystem = nBytesReturned / sizeof(INTERFACE_INFO);
|
---|
962 |
|
---|
963 | /** @todo Use GetAdaptersInfo() and GetAdapterAddresses (IPv4 + IPv6) for more information. */
|
---|
964 | for (int i = 0; i < cIfacesSystem; ++i)
|
---|
965 | {
|
---|
966 | sockaddr_in *pAddress;
|
---|
967 | u_long nFlags = 0;
|
---|
968 | if (InterfaceList[i].iiFlags & IFF_LOOPBACK) /* Skip loopback device. */
|
---|
969 | continue;
|
---|
970 | nFlags = InterfaceList[i].iiFlags;
|
---|
971 | pAddress = (sockaddr_in *)&(InterfaceList[i].iiAddress);
|
---|
972 | Assert(pAddress);
|
---|
973 | char szIp[32];
|
---|
974 | RTStrPrintf(szIp, sizeof(szIp), "%s", inet_ntoa(pAddress->sin_addr));
|
---|
975 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/V4/IP", cIfacesReport);
|
---|
976 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szIp);
|
---|
977 |
|
---|
978 | pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress);
|
---|
979 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/V4/Broadcast", cIfacesReport);
|
---|
980 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
981 |
|
---|
982 | pAddress = (sockaddr_in *)&(InterfaceList[i].iiNetmask);
|
---|
983 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/V4/Netmask", cIfacesReport);
|
---|
984 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
985 |
|
---|
986 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/Status", cIfacesReport);
|
---|
987 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, nFlags & IFF_UP ? "Up" : "Down");
|
---|
988 |
|
---|
989 | # ifndef TARGET_NT4
|
---|
990 | IP_ADAPTER_INFO *pAdp;
|
---|
991 | for (pAdp = pAdpInfo; pAdp; pAdp = pAdp->Next)
|
---|
992 | if (!strcmp(pAdp->IpAddressList.IpAddress.String, szIp))
|
---|
993 | break;
|
---|
994 |
|
---|
995 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/MAC", cIfacesReport);
|
---|
996 | if (pAdp)
|
---|
997 | {
|
---|
998 | char szMac[32];
|
---|
999 | RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
|
---|
1000 | pAdp->Address[0], pAdp->Address[1], pAdp->Address[2],
|
---|
1001 | pAdp->Address[3], pAdp->Address[4], pAdp->Address[5]);
|
---|
1002 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
|
---|
1003 | }
|
---|
1004 | else
|
---|
1005 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, NULL);
|
---|
1006 | # endif /* !TARGET_NT4 */
|
---|
1007 |
|
---|
1008 | cIfacesReport++;
|
---|
1009 | }
|
---|
1010 | if (pAdpInfo)
|
---|
1011 | RTMemFree(pAdpInfo);
|
---|
1012 | if (sd >= 0)
|
---|
1013 | closesocket(sd);
|
---|
1014 |
|
---|
1015 | #elif defined(RT_OS_HAIKU)
|
---|
1016 | /** @todo Haiku: implement network info. retreival */
|
---|
1017 | return VERR_NOT_IMPLEMENTED;
|
---|
1018 |
|
---|
1019 | #elif defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
|
---|
1020 | struct ifaddrs *pIfHead = NULL;
|
---|
1021 |
|
---|
1022 | /* Get all available interfaces */
|
---|
1023 | rc = getifaddrs(&pIfHead);
|
---|
1024 | if (rc < 0)
|
---|
1025 | {
|
---|
1026 | rc = RTErrConvertFromErrno(errno);
|
---|
1027 | VBoxServiceError("VMInfo/Network: Failed to get all interfaces: Error %Rrc\n");
|
---|
1028 | return rc;
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | /* Loop through all interfaces and set the data. */
|
---|
1032 | for (struct ifaddrs *pIfCurr = pIfHead; pIfCurr; pIfCurr = pIfCurr->ifa_next)
|
---|
1033 | {
|
---|
1034 | /*
|
---|
1035 | * Only AF_INET and no loopback interfaces
|
---|
1036 | */
|
---|
1037 | /** @todo: IPv6 interfaces */
|
---|
1038 | if ( pIfCurr->ifa_addr->sa_family == AF_INET
|
---|
1039 | && !(pIfCurr->ifa_flags & IFF_LOOPBACK))
|
---|
1040 | {
|
---|
1041 | char szInetAddr[NI_MAXHOST];
|
---|
1042 |
|
---|
1043 | memset(szInetAddr, 0, NI_MAXHOST);
|
---|
1044 | getnameinfo(pIfCurr->ifa_addr, sizeof(struct sockaddr_in),
|
---|
1045 | szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
---|
1046 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/V4/IP", cIfacesReport);
|
---|
1047 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
|
---|
1048 |
|
---|
1049 | memset(szInetAddr, 0, NI_MAXHOST);
|
---|
1050 | getnameinfo(pIfCurr->ifa_broadaddr, sizeof(struct sockaddr_in),
|
---|
1051 | szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
---|
1052 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/V4/Broadcast", cIfacesReport);
|
---|
1053 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
|
---|
1054 |
|
---|
1055 | memset(szInetAddr, 0, NI_MAXHOST);
|
---|
1056 | getnameinfo(pIfCurr->ifa_netmask, sizeof(struct sockaddr_in),
|
---|
1057 | szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
---|
1058 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/V4/Netmask", cIfacesReport);
|
---|
1059 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
|
---|
1060 |
|
---|
1061 | /* Search for the AF_LINK interface of the current AF_INET one and get the mac. */
|
---|
1062 | for (struct ifaddrs *pIfLinkCurr = pIfHead; pIfLinkCurr; pIfLinkCurr = pIfLinkCurr->ifa_next)
|
---|
1063 | {
|
---|
1064 | if ( pIfLinkCurr->ifa_addr->sa_family == AF_LINK
|
---|
1065 | && !strcmp(pIfCurr->ifa_name, pIfLinkCurr->ifa_name))
|
---|
1066 | {
|
---|
1067 | char szMac[32];
|
---|
1068 | uint8_t *pu8Mac = NULL;
|
---|
1069 | struct sockaddr_dl *pLinkAddress = (struct sockaddr_dl *)pIfLinkCurr->ifa_addr;
|
---|
1070 |
|
---|
1071 | AssertPtr(pLinkAddress);
|
---|
1072 | pu8Mac = (uint8_t *)LLADDR(pLinkAddress);
|
---|
1073 | RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
|
---|
1074 | pu8Mac[0], pu8Mac[1], pu8Mac[2], pu8Mac[3], pu8Mac[4], pu8Mac[5]);
|
---|
1075 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/MAC", cIfacesReport);
|
---|
1076 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
|
---|
1077 | break;
|
---|
1078 | }
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/Status", cIfacesReport);
|
---|
1082 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, pIfCurr->ifa_flags & IFF_UP ? "Up" : "Down");
|
---|
1083 |
|
---|
1084 | cIfacesReport++;
|
---|
1085 | }
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | /* Free allocated resources. */
|
---|
1089 | freeifaddrs(pIfHead);
|
---|
1090 |
|
---|
1091 | #else /* !RT_OS_WINDOWS && !RT_OS_FREEBSD */
|
---|
1092 | int sd = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
1093 | if (sd < 0)
|
---|
1094 | {
|
---|
1095 | rc = RTErrConvertFromErrno(errno);
|
---|
1096 | VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %Rrc\n", rc);
|
---|
1097 | return rc;
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | ifconf ifcfg;
|
---|
1101 | char buffer[1024] = {0};
|
---|
1102 | ifcfg.ifc_len = sizeof(buffer);
|
---|
1103 | ifcfg.ifc_buf = buffer;
|
---|
1104 | if (ioctl(sd, SIOCGIFCONF, &ifcfg) < 0)
|
---|
1105 | {
|
---|
1106 | close(sd);
|
---|
1107 | rc = RTErrConvertFromErrno(errno);
|
---|
1108 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFCONF) on socket: Error %Rrc\n", rc);
|
---|
1109 | return rc;
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | ifreq *ifrequest = ifcfg.ifc_req;
|
---|
1113 | int cIfacesSystem = ifcfg.ifc_len / sizeof(ifreq);
|
---|
1114 |
|
---|
1115 | # ifdef RT_OS_OS2 /* Get the physical addresses. */
|
---|
1116 | struct ifmib IfStats;
|
---|
1117 | if (ioctl(sd, SIOSTATIF, &IfStats) < 0)
|
---|
1118 | {
|
---|
1119 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOSTATIF): errno=%d\n", errno);
|
---|
1120 | RT_ZERO(IfStats);
|
---|
1121 | }
|
---|
1122 | # endif
|
---|
1123 |
|
---|
1124 | for (int i = 0; i < cIfacesSystem; ++i)
|
---|
1125 | {
|
---|
1126 | /* Get the interface flags - ASSUMES the address isn't really overwritten (ugly). */
|
---|
1127 | if (ioctl(sd, SIOCGIFFLAGS, &ifrequest[i]) < 0)
|
---|
1128 | {
|
---|
1129 | # ifdef RT_OS_OS2 /* Ignore errors when querying flags for non-existing interfaces. */
|
---|
1130 | if (errno == ENXIO)
|
---|
1131 | continue;
|
---|
1132 | # endif
|
---|
1133 | rc = RTErrConvertFromErrno(errno);
|
---|
1134 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFFLAGS,#%u) on socket: Error %Rrc\n", i, rc);
|
---|
1135 | break;
|
---|
1136 | }
|
---|
1137 | if (ifrequest[i].ifr_flags & IFF_LOOPBACK) /* Skip the loopback device. */
|
---|
1138 | continue;
|
---|
1139 |
|
---|
1140 | bool const fIfUp = !!(ifrequest[i].ifr_flags & IFF_UP);
|
---|
1141 |
|
---|
1142 | # ifdef RT_OS_OS2
|
---|
1143 | /* Don't know what OS/2 returns in SIOCGIFCONF, but it isn't the address. Get it. */
|
---|
1144 | if (ioctl(sd, SIOCGIFADDR, &ifrequest[i]) < 0)
|
---|
1145 | {
|
---|
1146 | rc = RTErrConvertFromErrno(errno);
|
---|
1147 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFADDR) on socket: Error %Rrc\n", rc);
|
---|
1148 | break;
|
---|
1149 | }
|
---|
1150 | # endif
|
---|
1151 | sockaddr_in *pAddress = ((sockaddr_in *)&ifrequest[i].ifr_addr);
|
---|
1152 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/V4/IP", cIfacesReport);
|
---|
1153 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
1154 |
|
---|
1155 | /* Get the broadcast address. */
|
---|
1156 | if (ioctl(sd, SIOCGIFBRDADDR, &ifrequest[i]) < 0)
|
---|
1157 | {
|
---|
1158 | rc = RTErrConvertFromErrno(errno);
|
---|
1159 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %Rrc\n", rc);
|
---|
1160 | break;
|
---|
1161 | }
|
---|
1162 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_broadaddr;
|
---|
1163 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/V4/Broadcast", cIfacesReport);
|
---|
1164 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
1165 |
|
---|
1166 | /* Get the net mask. */
|
---|
1167 | if (ioctl(sd, SIOCGIFNETMASK, &ifrequest[i]) < 0)
|
---|
1168 | {
|
---|
1169 | rc = RTErrConvertFromErrno(errno);
|
---|
1170 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFNETMASK) on socket: Error %Rrc\n", rc);
|
---|
1171 | break;
|
---|
1172 | }
|
---|
1173 | # if defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
|
---|
1174 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_addr;
|
---|
1175 | # else
|
---|
1176 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_netmask;
|
---|
1177 | # endif
|
---|
1178 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/V4/Netmask", cIfacesReport);
|
---|
1179 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
1180 |
|
---|
1181 | # if defined(RT_OS_SOLARIS)
|
---|
1182 | /*
|
---|
1183 | * "ifreq" is obsolete on Solaris. We use the recommended "lifreq".
|
---|
1184 | * We might fail if the interface has not been assigned an IP address.
|
---|
1185 | * That doesn't matter; as long as it's plumbed we can pick it up.
|
---|
1186 | * But, if it has not acquired an IP address we cannot obtain it's MAC
|
---|
1187 | * address this way, so we just use all zeros there.
|
---|
1188 | */
|
---|
1189 | RTMAC IfMac;
|
---|
1190 | RT_ZERO(IfMac);
|
---|
1191 | struct lifreq IfReq;
|
---|
1192 | RT_ZERO(IfReq);
|
---|
1193 | AssertCompile(sizeof(IfReq.lifr_name) >= sizeof(ifrequest[i].ifr_name));
|
---|
1194 | strncpy(IfReq.lifr_name, ifrequest[i].ifr_name, sizeof(ifrequest[i].ifr_name));
|
---|
1195 | if (ioctl(sd, SIOCGLIFADDR, &IfReq) >= 0)
|
---|
1196 | {
|
---|
1197 | struct arpreq ArpReq;
|
---|
1198 | RT_ZERO(ArpReq);
|
---|
1199 | memcpy(&ArpReq.arp_pa, &IfReq.lifr_addr, sizeof(struct sockaddr_in));
|
---|
1200 |
|
---|
1201 | if (ioctl(sd, SIOCGARP, &ArpReq) >= 0)
|
---|
1202 | memcpy(&IfMac, ArpReq.arp_ha.sa_data, sizeof(IfMac));
|
---|
1203 | else
|
---|
1204 | {
|
---|
1205 | rc = RTErrConvertFromErrno(errno);
|
---|
1206 | VBoxServiceError("VMInfo/Network: failed to ioctl(SIOCGARP) on socket: Error %Rrc\n", rc);
|
---|
1207 | break;
|
---|
1208 | }
|
---|
1209 | }
|
---|
1210 | else
|
---|
1211 | {
|
---|
1212 | VBoxServiceVerbose(2, "VMInfo/Network: Interface %d has no assigned IP address, skipping ...\n", i);
|
---|
1213 | continue;
|
---|
1214 | }
|
---|
1215 | # elif defined(RT_OS_OS2)
|
---|
1216 | RTMAC IfMac;
|
---|
1217 | uint32_t iIfStat = RT_MIN((unsigned)IfStats.ifNumber, IFMIB_ENTRIES); /* paranoia */
|
---|
1218 | while (iIfStat-- > 0) /** @todo test this code with more than one interface. */
|
---|
1219 | if (IfStats.iftable[iIfStat].iftIndex == i)
|
---|
1220 | memcpy(&IfMac, IfStats.iftable[iIfStat].iftPhysAddr, sizeof(IfMac));
|
---|
1221 | #else
|
---|
1222 | if (ioctl(sd, SIOCGIFHWADDR, &ifrequest[i]) < 0)
|
---|
1223 | {
|
---|
1224 | rc = RTErrConvertFromErrno(errno);
|
---|
1225 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFHWADDR) on socket: Error %Rrc\n", rc);
|
---|
1226 | break;
|
---|
1227 | }
|
---|
1228 | RTMAC IfMac = *(PRTMAC)&ifrequest[i].ifr_hwaddr.sa_data[0];
|
---|
1229 | # endif
|
---|
1230 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/MAC", cIfacesReport);
|
---|
1231 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%02X%02X%02X%02X%02X%02X",
|
---|
1232 | IfMac.au8[0], IfMac.au8[1], IfMac.au8[2], IfMac.au8[3], IfMac.au8[4], IfMac.au8[5]);
|
---|
1233 |
|
---|
1234 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%RU32/Status", cIfacesReport);
|
---|
1235 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, fIfUp ? "Up" : "Down");
|
---|
1236 | cIfacesReport++;
|
---|
1237 | } /* For all interfaces */
|
---|
1238 |
|
---|
1239 | close(sd);
|
---|
1240 | if (RT_FAILURE(rc))
|
---|
1241 | VBoxServiceError("VMInfo/Network: Network enumeration for interface %RU32 failed with error %Rrc\n", cIfacesReport, rc);
|
---|
1242 |
|
---|
1243 | #endif /* !RT_OS_WINDOWS */
|
---|
1244 |
|
---|
1245 | #if 0 /* Zapping not enabled yet, needs more testing first. */
|
---|
1246 | /*
|
---|
1247 | * Zap all stale network interface data if the former (saved) network ifaces count
|
---|
1248 | * is bigger than the current one.
|
---|
1249 | */
|
---|
1250 |
|
---|
1251 | /* Get former count. */
|
---|
1252 | uint32_t cIfacesReportOld;
|
---|
1253 | rc = VBoxServiceReadPropUInt32(g_uVMInfoGuestPropSvcClientID, g_pszPropCacheValNetCount, &cIfacesReportOld,
|
---|
1254 | 0 /* Min */, UINT32_MAX /* Max */);
|
---|
1255 | if ( RT_SUCCESS(rc)
|
---|
1256 | && cIfacesReportOld > cIfacesReport) /* Are some ifaces not around anymore? */
|
---|
1257 | {
|
---|
1258 | VBoxServiceVerbose(3, "VMInfo/Network: Stale interface data detected (%RU32 old vs. %RU32 current)\n",
|
---|
1259 | cIfacesReportOld, cIfacesReport);
|
---|
1260 |
|
---|
1261 | uint32_t uIfaceDeleteIdx = cIfacesReport;
|
---|
1262 | do
|
---|
1263 | {
|
---|
1264 | VBoxServiceVerbose(3, "VMInfo/Network: Deleting stale data of interface %d ...\n", uIfaceDeleteIdx);
|
---|
1265 | rc = VBoxServicePropCacheUpdateByPath(&g_VMInfoPropCache, NULL /* Value, delete */, 0 /* Flags */, "/VirtualBox/GuestInfo/Net/%RU32", uIfaceDeleteIdx++);
|
---|
1266 | } while (RT_SUCCESS(rc));
|
---|
1267 | }
|
---|
1268 | else if ( RT_FAILURE(rc)
|
---|
1269 | && rc != VERR_NOT_FOUND)
|
---|
1270 | {
|
---|
1271 | VBoxServiceError("VMInfo/Network: Failed retrieving old network interfaces count with error %Rrc\n", rc);
|
---|
1272 | }
|
---|
1273 | #endif
|
---|
1274 |
|
---|
1275 | /*
|
---|
1276 | * This property is a beacon which is _always_ written, even if the network configuration
|
---|
1277 | * does not change. If this property is missing, the host assumes that all other GuestInfo
|
---|
1278 | * properties are no longer valid.
|
---|
1279 | */
|
---|
1280 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, g_pszPropCacheValNetCount, "%RU32",
|
---|
1281 | cIfacesReport);
|
---|
1282 |
|
---|
1283 | /* Don't fail here; just report everything we got. */
|
---|
1284 | return VINF_SUCCESS;
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 |
|
---|
1288 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
1289 | DECLCALLBACK(int) VBoxServiceVMInfoWorker(bool volatile *pfShutdown)
|
---|
1290 | {
|
---|
1291 | int rc;
|
---|
1292 |
|
---|
1293 | /*
|
---|
1294 | * Tell the control thread that it can continue
|
---|
1295 | * spawning services.
|
---|
1296 | */
|
---|
1297 | RTThreadUserSignal(RTThreadSelf());
|
---|
1298 |
|
---|
1299 | #ifdef RT_OS_WINDOWS
|
---|
1300 | /* Required for network information (must be called per thread). */
|
---|
1301 | WSADATA wsaData;
|
---|
1302 | if (WSAStartup(MAKEWORD(2, 2), &wsaData))
|
---|
1303 | VBoxServiceError("VMInfo/Network: WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError()));
|
---|
1304 | #endif /* RT_OS_WINDOWS */
|
---|
1305 |
|
---|
1306 | /*
|
---|
1307 | * Write the fixed properties first.
|
---|
1308 | */
|
---|
1309 | vboxserviceVMInfoWriteFixedProperties();
|
---|
1310 |
|
---|
1311 | /*
|
---|
1312 | * Now enter the loop retrieving runtime data continuously.
|
---|
1313 | */
|
---|
1314 | for (;;)
|
---|
1315 | {
|
---|
1316 | rc = vboxserviceVMInfoWriteUsers();
|
---|
1317 | if (RT_FAILURE(rc))
|
---|
1318 | break;
|
---|
1319 |
|
---|
1320 | rc = vboxserviceVMInfoWriteNetwork();
|
---|
1321 | if (RT_FAILURE(rc))
|
---|
1322 | break;
|
---|
1323 |
|
---|
1324 | /* Whether to wait for event semaphore or not. */
|
---|
1325 | bool fWait = true;
|
---|
1326 |
|
---|
1327 | /* Check for location awareness. This most likely only
|
---|
1328 | * works with VBox (latest) 4.1 and up. */
|
---|
1329 |
|
---|
1330 | /* Check for new connection. */
|
---|
1331 | char *pszLAClientID = NULL;
|
---|
1332 | int rc2 = VBoxServiceReadHostProp(g_uVMInfoGuestPropSvcClientID, g_pszLAActiveClient, true /* Read only */,
|
---|
1333 | &pszLAClientID, NULL /* Flags */, NULL /* Timestamp */);
|
---|
1334 | if (RT_SUCCESS(rc2))
|
---|
1335 | {
|
---|
1336 | AssertPtr(pszLAClientID);
|
---|
1337 | if (RTStrICmp(pszLAClientID, "0")) /* Is a client connected? */
|
---|
1338 | {
|
---|
1339 | uint32_t uLAClientID = RTStrToInt32(pszLAClientID);
|
---|
1340 | uint64_t uLAClientAttachedTS;
|
---|
1341 |
|
---|
1342 | /* Peek at "Attach" value to figure out if hotdesking happened. */
|
---|
1343 | char *pszAttach = NULL;
|
---|
1344 | rc2 = vboxServiceGetLAClientValue(uLAClientID, "Attach", &pszAttach,
|
---|
1345 | &uLAClientAttachedTS);
|
---|
1346 |
|
---|
1347 | if ( RT_SUCCESS(rc2)
|
---|
1348 | && ( !g_LAClientAttachedTS
|
---|
1349 | || (g_LAClientAttachedTS != uLAClientAttachedTS)))
|
---|
1350 | {
|
---|
1351 | vboxServiceFreeLAClientInfo(&g_LAClientInfo);
|
---|
1352 |
|
---|
1353 | /* Note: There is a race between setting the guest properties by the host and getting them by
|
---|
1354 | * the guest. */
|
---|
1355 | rc2 = vboxServiceGetLAClientInfo(uLAClientID, &g_LAClientInfo);
|
---|
1356 | if (RT_SUCCESS(rc2))
|
---|
1357 | {
|
---|
1358 | VBoxServiceVerbose(1, "VRDP: Hotdesk client %s with ID=%RU32, Name=%s, Domain=%s\n",
|
---|
1359 | /* If g_LAClientAttachedTS is 0 this means there already was an active
|
---|
1360 | * hotdesk session when VBoxService started. */
|
---|
1361 | !g_LAClientAttachedTS ? "already active" : g_LAClientInfo.fAttached ? "connected" : "disconnected",
|
---|
1362 | uLAClientID, g_LAClientInfo.pszName, g_LAClientInfo.pszDomain);
|
---|
1363 |
|
---|
1364 | g_LAClientAttachedTS = g_LAClientInfo.uAttachedTS;
|
---|
1365 |
|
---|
1366 | /* Don't wait for event semaphore below anymore because we now know that the client
|
---|
1367 | * changed. This means we need to iterate all VM information again immediately. */
|
---|
1368 | fWait = false;
|
---|
1369 | }
|
---|
1370 | else
|
---|
1371 | {
|
---|
1372 | static int s_iBitchedAboutLAClientInfo = 0;
|
---|
1373 | if (s_iBitchedAboutLAClientInfo++ < 10)
|
---|
1374 | VBoxServiceError("Error getting active location awareness client info, rc=%Rrc\n", rc2);
|
---|
1375 | }
|
---|
1376 | }
|
---|
1377 | else if (RT_FAILURE(rc2))
|
---|
1378 | VBoxServiceError("Error getting attached value of location awareness client %RU32, rc=%Rrc\n",
|
---|
1379 | uLAClientID, rc2);
|
---|
1380 | if (pszAttach)
|
---|
1381 | RTStrFree(pszAttach);
|
---|
1382 | }
|
---|
1383 | else
|
---|
1384 | {
|
---|
1385 | VBoxServiceVerbose(1, "VRDP: UTTSC disconnected from VRDP server\n");
|
---|
1386 | vboxServiceFreeLAClientInfo(&g_LAClientInfo);
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 | RTStrFree(pszLAClientID);
|
---|
1390 | }
|
---|
1391 | else
|
---|
1392 | {
|
---|
1393 | static int s_iBitchedAboutLAClient = 0;
|
---|
1394 | if ( (rc2 != VERR_NOT_FOUND) /* No location awareness installed, skip. */
|
---|
1395 | && s_iBitchedAboutLAClient++ < 3)
|
---|
1396 | VBoxServiceError("VRDP: Querying connected location awareness client failed with rc=%Rrc\n", rc2);
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | VBoxServiceVerbose(3, "VRDP: Handling location awareness done\n");
|
---|
1400 |
|
---|
1401 | /*
|
---|
1402 | * Flush all properties if we were restored.
|
---|
1403 | */
|
---|
1404 | uint64_t idNewSession = g_idVMInfoSession;
|
---|
1405 | VbglR3GetSessionId(&idNewSession);
|
---|
1406 | if (idNewSession != g_idVMInfoSession)
|
---|
1407 | {
|
---|
1408 | VBoxServiceVerbose(3, "The VM session ID changed, flushing all properties\n");
|
---|
1409 | vboxserviceVMInfoWriteFixedProperties();
|
---|
1410 | VBoxServicePropCacheFlush(&g_VMInfoPropCache);
|
---|
1411 | g_idVMInfoSession = idNewSession;
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 | /*
|
---|
1415 | * Block for a while.
|
---|
1416 | *
|
---|
1417 | * The event semaphore takes care of ignoring interruptions and it
|
---|
1418 | * allows us to implement service wakeup later.
|
---|
1419 | */
|
---|
1420 | if (*pfShutdown)
|
---|
1421 | break;
|
---|
1422 | if (fWait)
|
---|
1423 | rc2 = RTSemEventMultiWait(g_hVMInfoEvent, g_cMsVMInfoInterval);
|
---|
1424 | if (*pfShutdown)
|
---|
1425 | break;
|
---|
1426 | if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
|
---|
1427 | {
|
---|
1428 | VBoxServiceError("RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
|
---|
1429 | rc = rc2;
|
---|
1430 | break;
|
---|
1431 | }
|
---|
1432 | else if (RT_LIKELY(RT_SUCCESS(rc2)))
|
---|
1433 | {
|
---|
1434 | /* Reset event semaphore if it got triggered. */
|
---|
1435 | rc2 = RTSemEventMultiReset(g_hVMInfoEvent);
|
---|
1436 | if (RT_FAILURE(rc2))
|
---|
1437 | rc2 = VBoxServiceError("RTSemEventMultiReset failed; rc2=%Rrc\n", rc2);
|
---|
1438 | }
|
---|
1439 | }
|
---|
1440 |
|
---|
1441 | #ifdef RT_OS_WINDOWS
|
---|
1442 | WSACleanup();
|
---|
1443 | #endif
|
---|
1444 |
|
---|
1445 | return rc;
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 |
|
---|
1449 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
1450 | static DECLCALLBACK(void) VBoxServiceVMInfoStop(void)
|
---|
1451 | {
|
---|
1452 | RTSemEventMultiSignal(g_hVMInfoEvent);
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 |
|
---|
1456 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
1457 | static DECLCALLBACK(void) VBoxServiceVMInfoTerm(void)
|
---|
1458 | {
|
---|
1459 | if (g_hVMInfoEvent != NIL_RTSEMEVENTMULTI)
|
---|
1460 | {
|
---|
1461 | /** @todo temporary solution: Zap all values which are not valid
|
---|
1462 | * anymore when VM goes down (reboot/shutdown ). Needs to
|
---|
1463 | * be replaced with "temporary properties" later.
|
---|
1464 | *
|
---|
1465 | * One idea is to introduce a (HGCM-)session guest property
|
---|
1466 | * flag meaning that a guest property is only valid as long
|
---|
1467 | * as the HGCM session isn't closed (e.g. guest application
|
---|
1468 | * terminates). [don't remove till implemented]
|
---|
1469 | */
|
---|
1470 | /** @todo r=bird: Drop the VbglR3GuestPropDelSet call here and use the cache
|
---|
1471 | * since it remembers what we've written. */
|
---|
1472 | /* Delete the "../Net" branch. */
|
---|
1473 | const char *apszPat[1] = { "/VirtualBox/GuestInfo/Net/*" };
|
---|
1474 | int rc = VbglR3GuestPropDelSet(g_uVMInfoGuestPropSvcClientID, &apszPat[0], RT_ELEMENTS(apszPat));
|
---|
1475 |
|
---|
1476 | /* Destroy LA client info. */
|
---|
1477 | vboxServiceFreeLAClientInfo(&g_LAClientInfo);
|
---|
1478 |
|
---|
1479 | /* Destroy property cache. */
|
---|
1480 | VBoxServicePropCacheDestroy(&g_VMInfoPropCache);
|
---|
1481 |
|
---|
1482 | /* Disconnect from guest properties service. */
|
---|
1483 | rc = VbglR3GuestPropDisconnect(g_uVMInfoGuestPropSvcClientID);
|
---|
1484 | if (RT_FAILURE(rc))
|
---|
1485 | VBoxServiceError("Failed to disconnect from guest property service! Error: %Rrc\n", rc);
|
---|
1486 | g_uVMInfoGuestPropSvcClientID = 0;
|
---|
1487 |
|
---|
1488 | RTSemEventMultiDestroy(g_hVMInfoEvent);
|
---|
1489 | g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
1490 | }
|
---|
1491 | }
|
---|
1492 |
|
---|
1493 |
|
---|
1494 | /**
|
---|
1495 | * The 'vminfo' service description.
|
---|
1496 | */
|
---|
1497 | VBOXSERVICE g_VMInfo =
|
---|
1498 | {
|
---|
1499 | /* pszName. */
|
---|
1500 | "vminfo",
|
---|
1501 | /* pszDescription. */
|
---|
1502 | "Virtual Machine Information",
|
---|
1503 | /* pszUsage. */
|
---|
1504 | " [--vminfo-interval <ms>] [--vminfo-user-idle-threshold <ms>]"
|
---|
1505 | ,
|
---|
1506 | /* pszOptions. */
|
---|
1507 | " --vminfo-interval Specifies the interval at which to retrieve the\n"
|
---|
1508 | " VM information. The default is 10000 ms.\n"
|
---|
1509 | " --vminfo-user-idle-threshold <ms>\n"
|
---|
1510 | " Specifies the user idle threshold (in ms) for\n"
|
---|
1511 | " considering a guest user as being idle. The default\n"
|
---|
1512 | " is 5000 (5 seconds).\n"
|
---|
1513 | ,
|
---|
1514 | /* methods */
|
---|
1515 | VBoxServiceVMInfoPreInit,
|
---|
1516 | VBoxServiceVMInfoOption,
|
---|
1517 | VBoxServiceVMInfoInit,
|
---|
1518 | VBoxServiceVMInfoWorker,
|
---|
1519 | VBoxServiceVMInfoStop,
|
---|
1520 | VBoxServiceVMInfoTerm
|
---|
1521 | };
|
---|
1522 |
|
---|