1 | /* $Id: VBoxServiceVMInfo.cpp 43793 2012-11-02 09:26:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Virtual Machine Information for the Host.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 |
|
---|
20 | /*******************************************************************************
|
---|
21 | * Header Files *
|
---|
22 | *******************************************************************************/
|
---|
23 | #ifdef RT_OS_WINDOWS
|
---|
24 | # 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 | # ifdef 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 | /*******************************************************************************
|
---|
75 | * Global Variables *
|
---|
76 | *******************************************************************************/
|
---|
77 | /** The vminfo interval (milliseconds). */
|
---|
78 | static uint32_t g_cMsVMInfoInterval = 0;
|
---|
79 | /** The semaphore we're blocking on. */
|
---|
80 | static RTSEMEVENTMULTI g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
81 | /** The guest property service client ID. */
|
---|
82 | static uint32_t g_uVMInfoGuestPropSvcClientID = 0;
|
---|
83 | /** Number of logged in users in OS. */
|
---|
84 | static uint32_t g_cVMInfoLoggedInUsers = UINT32_MAX;
|
---|
85 | /** The guest property cache. */
|
---|
86 | static VBOXSERVICEVEPROPCACHE g_VMInfoPropCache;
|
---|
87 | /** The VM session ID. Changes whenever the VM is restored or reset. */
|
---|
88 | static uint64_t g_idVMInfoSession;
|
---|
89 |
|
---|
90 |
|
---|
91 | /*******************************************************************************
|
---|
92 | * Defines *
|
---|
93 | *******************************************************************************/
|
---|
94 | #ifdef VBOX_WITH_DBUS
|
---|
95 | /** ConsoleKit defines (taken from 0.4.5). */
|
---|
96 | #define CK_NAME "org.freedesktop.ConsoleKit"
|
---|
97 | #define CK_PATH "/org/freedesktop/ConsoleKit"
|
---|
98 | #define CK_INTERFACE "org.freedesktop.ConsoleKit"
|
---|
99 |
|
---|
100 | #define CK_MANAGER_PATH "/org/freedesktop/ConsoleKit/Manager"
|
---|
101 | #define CK_MANAGER_INTERFACE "org.freedesktop.ConsoleKit.Manager"
|
---|
102 | #define CK_SEAT_INTERFACE "org.freedesktop.ConsoleKit.Seat"
|
---|
103 | #define CK_SESSION_INTERFACE "org.freedesktop.ConsoleKit.Session"
|
---|
104 | #endif
|
---|
105 |
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Signals the event so that a re-enumeration of VM-specific
|
---|
110 | * information (like logged in users) can happen.
|
---|
111 | *
|
---|
112 | * @return IPRT status code.
|
---|
113 | */
|
---|
114 | int VBoxServiceVMInfoSignal(void)
|
---|
115 | {
|
---|
116 | /* Trigger a re-enumeration of all logged-in users by unblocking
|
---|
117 | * the multi event semaphore of the VMInfo thread. */
|
---|
118 | if (g_hVMInfoEvent)
|
---|
119 | return RTSemEventMultiSignal(g_hVMInfoEvent);
|
---|
120 |
|
---|
121 | return VINF_SUCCESS;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
126 | static DECLCALLBACK(int) VBoxServiceVMInfoPreInit(void)
|
---|
127 | {
|
---|
128 | return VINF_SUCCESS;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
133 | static DECLCALLBACK(int) VBoxServiceVMInfoOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
134 | {
|
---|
135 | int rc = -1;
|
---|
136 | if (ppszShort)
|
---|
137 | /* no short options */;
|
---|
138 | else if (!strcmp(argv[*pi], "--vminfo-interval"))
|
---|
139 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
140 | &g_cMsVMInfoInterval, 1, UINT32_MAX - 1);
|
---|
141 | return rc;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
146 | static DECLCALLBACK(int) VBoxServiceVMInfoInit(void)
|
---|
147 | {
|
---|
148 | /*
|
---|
149 | * If not specified, find the right interval default.
|
---|
150 | * Then create the event sem to block on.
|
---|
151 | */
|
---|
152 | if (!g_cMsVMInfoInterval)
|
---|
153 | g_cMsVMInfoInterval = g_DefaultInterval * 1000;
|
---|
154 | if (!g_cMsVMInfoInterval)
|
---|
155 | g_cMsVMInfoInterval = 10 * 1000;
|
---|
156 |
|
---|
157 | int rc = RTSemEventMultiCreate(&g_hVMInfoEvent);
|
---|
158 | AssertRCReturn(rc, rc);
|
---|
159 |
|
---|
160 | VbglR3GetSessionId(&g_idVMInfoSession);
|
---|
161 | /* The status code is ignored as this information is not available with VBox < 3.2.10. */
|
---|
162 |
|
---|
163 | rc = VbglR3GuestPropConnect(&g_uVMInfoGuestPropSvcClientID);
|
---|
164 | if (RT_SUCCESS(rc))
|
---|
165 | VBoxServiceVerbose(3, "VMInfo: Property Service Client ID: %#x\n", g_uVMInfoGuestPropSvcClientID);
|
---|
166 | else
|
---|
167 | {
|
---|
168 | /* If the service was not found, we disable this service without
|
---|
169 | causing VBoxService to fail. */
|
---|
170 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
|
---|
171 | {
|
---|
172 | VBoxServiceVerbose(0, "VMInfo: Guest property service is not available, disabling the service\n");
|
---|
173 | rc = VERR_SERVICE_DISABLED;
|
---|
174 | }
|
---|
175 | else
|
---|
176 | VBoxServiceError("VMInfo: Failed to connect to the guest property service! Error: %Rrc\n", rc);
|
---|
177 | RTSemEventMultiDestroy(g_hVMInfoEvent);
|
---|
178 | g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if (RT_SUCCESS(rc))
|
---|
182 | {
|
---|
183 | VBoxServicePropCacheCreate(&g_VMInfoPropCache, g_uVMInfoGuestPropSvcClientID);
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * Declare some guest properties with flags and reset values.
|
---|
187 | */
|
---|
188 | VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList",
|
---|
189 | VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_TRANSIENT, NULL /* Delete on exit */);
|
---|
190 | VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsers",
|
---|
191 | VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_TRANSIENT, "0");
|
---|
192 | VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers",
|
---|
193 | VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_TRANSIENT, "true");
|
---|
194 | VBoxServicePropCacheUpdateEntry(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/Net/Count",
|
---|
195 | VBOXSERVICEPROPCACHEFLAG_TEMPORARY | VBOXSERVICEPROPCACHEFLAG_ALWAYS_UPDATE, NULL /* Delete on exit */);
|
---|
196 | }
|
---|
197 | return rc;
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Writes the properties that won't change while the service is running.
|
---|
203 | *
|
---|
204 | * Errors are ignored.
|
---|
205 | */
|
---|
206 | static void vboxserviceVMInfoWriteFixedProperties(void)
|
---|
207 | {
|
---|
208 | /*
|
---|
209 | * First get OS information that won't change.
|
---|
210 | */
|
---|
211 | char szInfo[256];
|
---|
212 | int rc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szInfo, sizeof(szInfo));
|
---|
213 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Product",
|
---|
214 | "%s", RT_FAILURE(rc) ? "" : szInfo);
|
---|
215 |
|
---|
216 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szInfo, sizeof(szInfo));
|
---|
217 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Release",
|
---|
218 | "%s", RT_FAILURE(rc) ? "" : szInfo);
|
---|
219 |
|
---|
220 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szInfo, sizeof(szInfo));
|
---|
221 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Version",
|
---|
222 | "%s", RT_FAILURE(rc) ? "" : szInfo);
|
---|
223 |
|
---|
224 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szInfo, sizeof(szInfo));
|
---|
225 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/ServicePack",
|
---|
226 | "%s", RT_FAILURE(rc) ? "" : szInfo);
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * Retrieve version information about Guest Additions and installed files (components).
|
---|
230 | */
|
---|
231 | char *pszAddVer;
|
---|
232 | char *pszAddVerExt;
|
---|
233 | char *pszAddRev;
|
---|
234 | rc = VbglR3GetAdditionsVersion(&pszAddVer, &pszAddVerExt, &pszAddRev);
|
---|
235 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Version",
|
---|
236 | "%s", RT_FAILURE(rc) ? "" : pszAddVer);
|
---|
237 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/VersionExt",
|
---|
238 | "%s", RT_FAILURE(rc) ? "" : pszAddVerExt);
|
---|
239 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Revision",
|
---|
240 | "%s", RT_FAILURE(rc) ? "" : pszAddRev);
|
---|
241 | if (RT_SUCCESS(rc))
|
---|
242 | {
|
---|
243 | RTStrFree(pszAddVer);
|
---|
244 | RTStrFree(pszAddVerExt);
|
---|
245 | RTStrFree(pszAddRev);
|
---|
246 | }
|
---|
247 |
|
---|
248 | #ifdef RT_OS_WINDOWS
|
---|
249 | /*
|
---|
250 | * Do windows specific properties.
|
---|
251 | */
|
---|
252 | char *pszInstDir;
|
---|
253 | rc = VbglR3GetAdditionsInstallationPath(&pszInstDir);
|
---|
254 | VBoxServiceWritePropF(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/InstallDir",
|
---|
255 | "%s", RT_FAILURE(rc) ? "" : pszInstDir);
|
---|
256 | if (RT_SUCCESS(rc))
|
---|
257 | RTStrFree(pszInstDir);
|
---|
258 |
|
---|
259 | VBoxServiceWinGetComponentVersions(g_uVMInfoGuestPropSvcClientID);
|
---|
260 | #endif
|
---|
261 | }
|
---|
262 |
|
---|
263 | #if defined(VBOX_WITH_DBUS) && defined(RT_OS_LINUX) /* Not yet for Solaris/FreeBSB. */
|
---|
264 | /*
|
---|
265 | * Simple wrapper to work around compiler-specific va_list madness.
|
---|
266 | */
|
---|
267 | static dbus_bool_t vboxService_dbus_message_get_args(DBusMessage *message,
|
---|
268 | DBusError *error,
|
---|
269 | int first_arg_type,
|
---|
270 | ...)
|
---|
271 | {
|
---|
272 | va_list va;
|
---|
273 | va_start(va, first_arg_type);
|
---|
274 | dbus_bool_t ret = dbus_message_get_args_valist(message, error,
|
---|
275 | first_arg_type, va);
|
---|
276 | va_end(va);
|
---|
277 | return ret;
|
---|
278 | }
|
---|
279 | #endif
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Provide information about active users.
|
---|
283 | */
|
---|
284 | static int vboxserviceVMInfoWriteUsers(void)
|
---|
285 | {
|
---|
286 | int rc = VINF_SUCCESS;
|
---|
287 | char *pszUserList = NULL;
|
---|
288 | uint32_t cUsersInList = 0;
|
---|
289 |
|
---|
290 | #ifdef RT_OS_WINDOWS
|
---|
291 | # ifndef TARGET_NT4
|
---|
292 | rc = VBoxServiceVMInfoWinWriteUsers(&pszUserList, &cUsersInList);
|
---|
293 | # else
|
---|
294 | rc = VERR_NOT_IMPLEMENTED;
|
---|
295 | # endif
|
---|
296 |
|
---|
297 | #elif defined(RT_OS_FREEBSD)
|
---|
298 | /** @todo FreeBSD: Port logged on user info retrieval.
|
---|
299 | * However, FreeBSD 9 supports utmpx, so we could use the code
|
---|
300 | * block below (?). */
|
---|
301 | rc = VERR_NOT_IMPLEMENTED;
|
---|
302 |
|
---|
303 | #elif defined(RT_OS_HAIKU)
|
---|
304 | /** @todo Haiku: Port logged on user info retrieval. */
|
---|
305 | rc = VERR_NOT_IMPLEMENTED;
|
---|
306 |
|
---|
307 | #elif defined(RT_OS_OS2)
|
---|
308 | /** @todo OS/2: Port logged on (LAN/local/whatever) user info retrieval. */
|
---|
309 | rc = VERR_NOT_IMPLEMENTED;
|
---|
310 |
|
---|
311 | #else
|
---|
312 | setutxent();
|
---|
313 | utmpx *ut_user;
|
---|
314 | uint32_t cListSize = 32;
|
---|
315 |
|
---|
316 | /* Allocate a first array to hold 32 users max. */
|
---|
317 | char **papszUsers = (char **)RTMemAllocZ(cListSize * sizeof(char *));
|
---|
318 | if (papszUsers == NULL)
|
---|
319 | rc = VERR_NO_MEMORY;
|
---|
320 |
|
---|
321 | /* Process all entries in the utmp file.
|
---|
322 | * Note: This only handles */
|
---|
323 | while ( (ut_user = getutxent())
|
---|
324 | && RT_SUCCESS(rc))
|
---|
325 | {
|
---|
326 | VBoxServiceVerbose(4, "Found entry \"%s\" (type: %d, PID: %RU32, session: %RU32)\n",
|
---|
327 | ut_user->ut_user, ut_user->ut_type, ut_user->ut_pid, ut_user->ut_session);
|
---|
328 | if (cUsersInList > cListSize)
|
---|
329 | {
|
---|
330 | cListSize += 32;
|
---|
331 | void *pvNew = RTMemRealloc(papszUsers, cListSize * sizeof(char*));
|
---|
332 | AssertPtrBreakStmt(pvNew, cListSize -= 32);
|
---|
333 | papszUsers = (char **)pvNew;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /* Make sure we don't add user names which are not
|
---|
337 | * part of type USER_PROCES. */
|
---|
338 | if (ut_user->ut_type == USER_PROCESS) /* Regular user process. */
|
---|
339 | {
|
---|
340 | bool fFound = false;
|
---|
341 | for (uint32_t i = 0; i < cUsersInList && !fFound; i++)
|
---|
342 | fFound = strcmp(papszUsers[i], ut_user->ut_user) == 0;
|
---|
343 |
|
---|
344 | if (!fFound)
|
---|
345 | {
|
---|
346 | VBoxServiceVerbose(4, "Adding user \"%s\" (type: %d) to list\n",
|
---|
347 | ut_user->ut_user, ut_user->ut_type);
|
---|
348 |
|
---|
349 | rc = RTStrDupEx(&papszUsers[cUsersInList], (const char *)ut_user->ut_user);
|
---|
350 | if (RT_FAILURE(rc))
|
---|
351 | break;
|
---|
352 | cUsersInList++;
|
---|
353 | }
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | #ifdef VBOX_WITH_DBUS
|
---|
358 | # if defined(RT_OS_LINUX) /* Not yet for Solaris/FreeBSB. */
|
---|
359 | DBusError dbErr;
|
---|
360 | DBusConnection *pConnection = NULL;
|
---|
361 | int rc2 = RTDBusLoadLib();
|
---|
362 | if (RT_SUCCESS(rc2))
|
---|
363 | {
|
---|
364 | /* Handle desktop sessions using ConsoleKit. */
|
---|
365 | VBoxServiceVerbose(4, "Checking ConsoleKit sessions ...\n");
|
---|
366 |
|
---|
367 | dbus_error_init(&dbErr);
|
---|
368 | pConnection = dbus_bus_get(DBUS_BUS_SYSTEM, &dbErr);
|
---|
369 | }
|
---|
370 |
|
---|
371 | if ( pConnection
|
---|
372 | && !dbus_error_is_set(&dbErr))
|
---|
373 | {
|
---|
374 | /* Get all available sessions. */
|
---|
375 | DBusMessage *pMsgSessions = dbus_message_new_method_call("org.freedesktop.ConsoleKit",
|
---|
376 | "/org/freedesktop/ConsoleKit/Manager",
|
---|
377 | "org.freedesktop.ConsoleKit.Manager",
|
---|
378 | "GetSessions");
|
---|
379 | if ( pMsgSessions
|
---|
380 | && (dbus_message_get_type(pMsgSessions) == DBUS_MESSAGE_TYPE_METHOD_CALL))
|
---|
381 | {
|
---|
382 | DBusMessage *pReplySessions = dbus_connection_send_with_reply_and_block(pConnection,
|
---|
383 | pMsgSessions, 30 * 1000 /* 30s timeout */,
|
---|
384 | &dbErr);
|
---|
385 | if ( pReplySessions
|
---|
386 | && !dbus_error_is_set(&dbErr))
|
---|
387 | {
|
---|
388 | char **ppszSessions; int cSessions;
|
---|
389 | if ( (dbus_message_get_type(pMsgSessions) == DBUS_MESSAGE_TYPE_METHOD_CALL)
|
---|
390 | && vboxService_dbus_message_get_args(pReplySessions, &dbErr, DBUS_TYPE_ARRAY,
|
---|
391 | DBUS_TYPE_OBJECT_PATH, &ppszSessions, &cSessions,
|
---|
392 | DBUS_TYPE_INVALID /* Termination */))
|
---|
393 | {
|
---|
394 | VBoxServiceVerbose(4, "ConsoleKit: retrieved %RU16 session(s)\n", cSessions);
|
---|
395 | AssertPtr(*ppszSessions);
|
---|
396 |
|
---|
397 | char **ppszCurSession = ppszSessions;
|
---|
398 | for (ppszCurSession; *ppszCurSession; ppszCurSession++)
|
---|
399 | {
|
---|
400 | VBoxServiceVerbose(4, "ConsoleKit: processing session '%s' ...\n", *ppszCurSession);
|
---|
401 |
|
---|
402 | /* *ppszCurSession now contains the object path
|
---|
403 | * (e.g. "/org/freedesktop/ConsoleKit/Session1"). */
|
---|
404 | DBusMessage *pMsgUnixUser = dbus_message_new_method_call("org.freedesktop.ConsoleKit",
|
---|
405 | *ppszCurSession,
|
---|
406 | "org.freedesktop.ConsoleKit.Session",
|
---|
407 | "GetUnixUser");
|
---|
408 | if ( pMsgUnixUser
|
---|
409 | && dbus_message_get_type(pMsgUnixUser) == DBUS_MESSAGE_TYPE_METHOD_CALL)
|
---|
410 | {
|
---|
411 | DBusMessage *pReplyUnixUser = dbus_connection_send_with_reply_and_block(pConnection,
|
---|
412 | pMsgUnixUser, 30 * 1000 /* 30s timeout */,
|
---|
413 | &dbErr);
|
---|
414 | if ( pReplyUnixUser
|
---|
415 | && !dbus_error_is_set(&dbErr))
|
---|
416 | {
|
---|
417 | DBusMessageIter itMsg;
|
---|
418 | if ( dbus_message_iter_init(pReplyUnixUser, &itMsg)
|
---|
419 | && dbus_message_iter_get_arg_type(&itMsg) == DBUS_TYPE_UINT32)
|
---|
420 | {
|
---|
421 | /* Get uid from message. */
|
---|
422 | uint32_t uid;
|
---|
423 | dbus_message_iter_get_basic(&itMsg, &uid);
|
---|
424 |
|
---|
425 | /* Look up user name (realname) from uid. */
|
---|
426 | setpwent();
|
---|
427 | struct passwd *ppwEntry = getpwuid(uid);
|
---|
428 | if ( ppwEntry
|
---|
429 | && ppwEntry->pw_name)
|
---|
430 | {
|
---|
431 | VBoxServiceVerbose(4, "ConsoleKit: session '%s' -> %s (uid: %RU32)\n",
|
---|
432 | *ppszCurSession, ppwEntry->pw_name, uid);
|
---|
433 |
|
---|
434 | bool fFound = false;
|
---|
435 | for (uint32_t i = 0; i < cUsersInList && !fFound; i++)
|
---|
436 | fFound = strcmp(papszUsers[i], ppwEntry->pw_name) == 0;
|
---|
437 |
|
---|
438 | if (!fFound)
|
---|
439 | {
|
---|
440 | VBoxServiceVerbose(4, "ConsoleKit: adding user \"%s\" to list\n",
|
---|
441 | ppwEntry->pw_name);
|
---|
442 |
|
---|
443 | rc = RTStrDupEx(&papszUsers[cUsersInList], (const char *)ppwEntry->pw_name);
|
---|
444 | if (RT_FAILURE(rc))
|
---|
445 | break;
|
---|
446 | cUsersInList++;
|
---|
447 | }
|
---|
448 | }
|
---|
449 | else
|
---|
450 | VBoxServiceError("ConsoleKit: unable to lookup user name for uid=%RU32\n", uid);
|
---|
451 | }
|
---|
452 | else
|
---|
453 | AssertMsgFailed(("ConsoleKit: GetUnixUser returned a wrong argument type\n"));
|
---|
454 | }
|
---|
455 |
|
---|
456 | if (pReplyUnixUser)
|
---|
457 | dbus_message_unref(pReplyUnixUser);
|
---|
458 | }
|
---|
459 | else
|
---|
460 | VBoxServiceError("ConsoleKit: unable to retrieve user for session '%s' (msg type=%d): %s",
|
---|
461 | *ppszCurSession, dbus_message_get_type(pMsgUnixUser),
|
---|
462 | dbus_error_is_set(&dbErr) ? dbErr.message : "No error information available\n");
|
---|
463 |
|
---|
464 | if (pMsgUnixUser)
|
---|
465 | dbus_message_unref(pMsgUnixUser);
|
---|
466 | }
|
---|
467 |
|
---|
468 | dbus_free_string_array(ppszSessions);
|
---|
469 | }
|
---|
470 | else
|
---|
471 | {
|
---|
472 | VBoxServiceError("ConsoleKit: unable to retrieve session parameters (msg type=%d): %s",
|
---|
473 | dbus_message_get_type(pMsgSessions),
|
---|
474 | dbus_error_is_set(&dbErr) ? dbErr.message : "No error information available\n");
|
---|
475 | }
|
---|
476 | dbus_message_unref(pReplySessions);
|
---|
477 | }
|
---|
478 |
|
---|
479 | if (pMsgSessions)
|
---|
480 | {
|
---|
481 | dbus_message_unref(pMsgSessions);
|
---|
482 | pMsgSessions = NULL;
|
---|
483 | }
|
---|
484 | }
|
---|
485 | else
|
---|
486 | {
|
---|
487 | static int s_iBitchedAboutConsoleKit = 0;
|
---|
488 | if (s_iBitchedAboutConsoleKit++ < 3)
|
---|
489 | VBoxServiceError("Unable to invoke ConsoleKit (%d/3) -- maybe not installed / used? Error: %s\n",
|
---|
490 | s_iBitchedAboutConsoleKit,
|
---|
491 | dbus_error_is_set(&dbErr) ? dbErr.message : "No error information available\n");
|
---|
492 | }
|
---|
493 |
|
---|
494 | if (pMsgSessions)
|
---|
495 | dbus_message_unref(pMsgSessions);
|
---|
496 | }
|
---|
497 | else
|
---|
498 | {
|
---|
499 | static int s_iBitchedAboutDBus = 0;
|
---|
500 | if (s_iBitchedAboutDBus++ < 3)
|
---|
501 | VBoxServiceError("Unable to connect to system D-Bus (%d/3): %s\n", s_iBitchedAboutDBus,
|
---|
502 | dbus_error_is_set(&dbErr) ? dbErr.message : "D-Bus not installed\n");
|
---|
503 | }
|
---|
504 |
|
---|
505 | if (dbus_error_is_set(&dbErr))
|
---|
506 | dbus_error_free(&dbErr);
|
---|
507 | # endif /* RT_OS_LINUX */
|
---|
508 | #endif /* VBOX_WITH_DBUS */
|
---|
509 |
|
---|
510 | /** @todo Fedora/others: Handle systemd-loginctl. */
|
---|
511 |
|
---|
512 | /* Calc the string length. */
|
---|
513 | size_t cchUserList = 0;
|
---|
514 | if (RT_SUCCESS(rc))
|
---|
515 | {
|
---|
516 | for (uint32_t i = 0; i < cUsersInList; i++)
|
---|
517 | cchUserList += (i != 0) + strlen(papszUsers[i]);
|
---|
518 | }
|
---|
519 |
|
---|
520 | /* Build the user list. */
|
---|
521 | if (RT_SUCCESS(rc))
|
---|
522 | rc = RTStrAllocEx(&pszUserList, cchUserList + 1);
|
---|
523 | if (RT_SUCCESS(rc))
|
---|
524 | {
|
---|
525 | char *psz = pszUserList;
|
---|
526 | for (uint32_t i = 0; i < cUsersInList; i++)
|
---|
527 | {
|
---|
528 | if (i != 0)
|
---|
529 | *psz++ = ',';
|
---|
530 | size_t cch = strlen(papszUsers[i]);
|
---|
531 | memcpy(psz, papszUsers[i], cch);
|
---|
532 | psz += cch;
|
---|
533 | }
|
---|
534 | *psz = '\0';
|
---|
535 | }
|
---|
536 |
|
---|
537 | /* Cleanup. */
|
---|
538 | for (uint32_t i = 0; i < cUsersInList; i++)
|
---|
539 | RTStrFree(papszUsers[i]);
|
---|
540 | RTMemFree(papszUsers);
|
---|
541 |
|
---|
542 | endutxent(); /* Close utmpx file. */
|
---|
543 | #endif
|
---|
544 | Assert(RT_FAILURE(rc) || cUsersInList == 0 || (pszUserList && *pszUserList));
|
---|
545 |
|
---|
546 | /* If the user enumeration above failed, reset the user count to 0 except
|
---|
547 | * we didn't have enough memory anymore. In that case we want to preserve
|
---|
548 | * the previous user count in order to not confuse third party tools which
|
---|
549 | * rely on that count. */
|
---|
550 | if (RT_FAILURE(rc))
|
---|
551 | {
|
---|
552 | if (rc == VERR_NO_MEMORY)
|
---|
553 | {
|
---|
554 | static int s_iVMInfoBitchedOOM = 0;
|
---|
555 | if (s_iVMInfoBitchedOOM++ < 3)
|
---|
556 | VBoxServiceVerbose(0, "Warning: Not enough memory available to enumerate users! Keeping old value (%u)\n",
|
---|
557 | g_cVMInfoLoggedInUsers);
|
---|
558 | cUsersInList = g_cVMInfoLoggedInUsers;
|
---|
559 | }
|
---|
560 | else
|
---|
561 | cUsersInList = 0;
|
---|
562 | }
|
---|
563 |
|
---|
564 | VBoxServiceVerbose(4, "cUsersInList=%RU32, pszUserList=%s, rc=%Rrc\n",
|
---|
565 | cUsersInList, pszUserList ? pszUserList : "<NULL>", rc);
|
---|
566 |
|
---|
567 | if (pszUserList && cUsersInList > 0)
|
---|
568 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", "%s", pszUserList);
|
---|
569 | else
|
---|
570 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", NULL);
|
---|
571 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsers", "%u", cUsersInList);
|
---|
572 | if (g_cVMInfoLoggedInUsers != cUsersInList)
|
---|
573 | {
|
---|
574 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers",
|
---|
575 | cUsersInList == 0 ? "true" : "false");
|
---|
576 | g_cVMInfoLoggedInUsers = cUsersInList;
|
---|
577 | }
|
---|
578 |
|
---|
579 | if (RT_SUCCESS(rc) && pszUserList)
|
---|
580 | RTStrFree(pszUserList);
|
---|
581 | return rc;
|
---|
582 | }
|
---|
583 |
|
---|
584 |
|
---|
585 | /**
|
---|
586 | * Provide information about the guest network.
|
---|
587 | */
|
---|
588 | static int vboxserviceVMInfoWriteNetwork(void)
|
---|
589 | {
|
---|
590 | int rc = VINF_SUCCESS;
|
---|
591 | uint32_t cIfacesReport = 0;
|
---|
592 | char szPropPath[256];
|
---|
593 |
|
---|
594 | #ifdef RT_OS_WINDOWS
|
---|
595 | IP_ADAPTER_INFO *pAdpInfo = NULL;
|
---|
596 |
|
---|
597 | # ifndef TARGET_NT4
|
---|
598 | ULONG cbAdpInfo = sizeof(*pAdpInfo);
|
---|
599 | pAdpInfo = (IP_ADAPTER_INFO *)RTMemAlloc(cbAdpInfo);
|
---|
600 | if (!pAdpInfo)
|
---|
601 | {
|
---|
602 | VBoxServiceError("VMInfo/Network: Failed to allocate IP_ADAPTER_INFO\n");
|
---|
603 | return VERR_NO_MEMORY;
|
---|
604 | }
|
---|
605 | DWORD dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
|
---|
606 | if (dwRet == ERROR_BUFFER_OVERFLOW)
|
---|
607 | {
|
---|
608 | IP_ADAPTER_INFO *pAdpInfoNew = (IP_ADAPTER_INFO*)RTMemRealloc(pAdpInfo, cbAdpInfo);
|
---|
609 | if (pAdpInfoNew)
|
---|
610 | {
|
---|
611 | pAdpInfo = pAdpInfoNew;
|
---|
612 | dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
|
---|
613 | }
|
---|
614 | }
|
---|
615 | else if (dwRet == ERROR_NO_DATA)
|
---|
616 | {
|
---|
617 | VBoxServiceVerbose(3, "VMInfo/Network: No network adapters available\n");
|
---|
618 |
|
---|
619 | /* If no network adapters available / present in the
|
---|
620 | * system we pretend success to not bail out too early. */
|
---|
621 | dwRet = ERROR_SUCCESS;
|
---|
622 | }
|
---|
623 |
|
---|
624 | if (dwRet != ERROR_SUCCESS)
|
---|
625 | {
|
---|
626 | if (pAdpInfo)
|
---|
627 | RTMemFree(pAdpInfo);
|
---|
628 | VBoxServiceError("VMInfo/Network: Failed to get adapter info: Error %d\n", dwRet);
|
---|
629 | return RTErrConvertFromWin32(dwRet);
|
---|
630 | }
|
---|
631 | # endif /* !TARGET_NT4 */
|
---|
632 |
|
---|
633 | SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
|
---|
634 | if (sd == SOCKET_ERROR) /* Socket invalid. */
|
---|
635 | {
|
---|
636 | int wsaErr = WSAGetLastError();
|
---|
637 | /* Don't complain/bail out with an error if network stack is not up; can happen
|
---|
638 | * on NT4 due to start up when not connected shares dialogs pop up. */
|
---|
639 | if (WSAENETDOWN == wsaErr)
|
---|
640 | {
|
---|
641 | VBoxServiceVerbose(0, "VMInfo/Network: Network is not up yet.\n");
|
---|
642 | wsaErr = VINF_SUCCESS;
|
---|
643 | }
|
---|
644 | else
|
---|
645 | VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %d\n", wsaErr);
|
---|
646 | if (pAdpInfo)
|
---|
647 | RTMemFree(pAdpInfo);
|
---|
648 | return RTErrConvertFromWin32(wsaErr);
|
---|
649 | }
|
---|
650 |
|
---|
651 | INTERFACE_INFO InterfaceList[20] = {0};
|
---|
652 | unsigned long nBytesReturned = 0;
|
---|
653 | if (WSAIoctl(sd,
|
---|
654 | SIO_GET_INTERFACE_LIST,
|
---|
655 | 0,
|
---|
656 | 0,
|
---|
657 | &InterfaceList,
|
---|
658 | sizeof(InterfaceList),
|
---|
659 | &nBytesReturned,
|
---|
660 | 0,
|
---|
661 | 0) == SOCKET_ERROR)
|
---|
662 | {
|
---|
663 | VBoxServiceError("VMInfo/Network: Failed to WSAIoctl() on socket: Error: %d\n", WSAGetLastError());
|
---|
664 | if (pAdpInfo)
|
---|
665 | RTMemFree(pAdpInfo);
|
---|
666 | return RTErrConvertFromWin32(WSAGetLastError());
|
---|
667 | }
|
---|
668 | int cIfacesSystem = nBytesReturned / sizeof(INTERFACE_INFO);
|
---|
669 |
|
---|
670 | /** @todo Use GetAdaptersInfo() and GetAdapterAddresses (IPv4 + IPv6) for more information. */
|
---|
671 | for (int i = 0; i < cIfacesSystem; ++i)
|
---|
672 | {
|
---|
673 | sockaddr_in *pAddress;
|
---|
674 | u_long nFlags = 0;
|
---|
675 | if (InterfaceList[i].iiFlags & IFF_LOOPBACK) /* Skip loopback device. */
|
---|
676 | continue;
|
---|
677 | nFlags = InterfaceList[i].iiFlags;
|
---|
678 | pAddress = (sockaddr_in *)&(InterfaceList[i].iiAddress);
|
---|
679 | Assert(pAddress);
|
---|
680 | char szIp[32];
|
---|
681 | RTStrPrintf(szIp, sizeof(szIp), "%s", inet_ntoa(pAddress->sin_addr));
|
---|
682 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/IP", cIfacesReport);
|
---|
683 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szIp);
|
---|
684 |
|
---|
685 | pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress);
|
---|
686 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Broadcast", cIfacesReport);
|
---|
687 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
688 |
|
---|
689 | pAddress = (sockaddr_in *)&(InterfaceList[i].iiNetmask);
|
---|
690 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Netmask", cIfacesReport);
|
---|
691 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
692 |
|
---|
693 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/Status", cIfacesReport);
|
---|
694 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, nFlags & IFF_UP ? "Up" : "Down");
|
---|
695 |
|
---|
696 | # ifndef TARGET_NT4
|
---|
697 | IP_ADAPTER_INFO *pAdp;
|
---|
698 | for (pAdp = pAdpInfo; pAdp; pAdp = pAdp->Next)
|
---|
699 | if (!strcmp(pAdp->IpAddressList.IpAddress.String, szIp))
|
---|
700 | break;
|
---|
701 |
|
---|
702 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/MAC", cIfacesReport);
|
---|
703 | if (pAdp)
|
---|
704 | {
|
---|
705 | char szMac[32];
|
---|
706 | RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
|
---|
707 | pAdp->Address[0], pAdp->Address[1], pAdp->Address[2],
|
---|
708 | pAdp->Address[3], pAdp->Address[4], pAdp->Address[5]);
|
---|
709 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
|
---|
710 | }
|
---|
711 | else
|
---|
712 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, NULL);
|
---|
713 | # endif /* !TARGET_NT4 */
|
---|
714 |
|
---|
715 | cIfacesReport++;
|
---|
716 | }
|
---|
717 | if (pAdpInfo)
|
---|
718 | RTMemFree(pAdpInfo);
|
---|
719 | if (sd >= 0)
|
---|
720 | closesocket(sd);
|
---|
721 |
|
---|
722 | #elif defined(RT_OS_HAIKU)
|
---|
723 | /** @todo Haiku: implement network info. retreival */
|
---|
724 | return VERR_NOT_IMPLEMENTED;
|
---|
725 |
|
---|
726 | #elif defined(RT_OS_FREEBSD)
|
---|
727 | struct ifaddrs *pIfHead = NULL;
|
---|
728 |
|
---|
729 | /* Get all available interfaces */
|
---|
730 | rc = getifaddrs(&pIfHead);
|
---|
731 | if (rc < 0)
|
---|
732 | {
|
---|
733 | rc = RTErrConvertFromErrno(errno);
|
---|
734 | VBoxServiceError("VMInfo/Network: Failed to get all interfaces: Error %Rrc\n");
|
---|
735 | return rc;
|
---|
736 | }
|
---|
737 |
|
---|
738 | /* Loop through all interfaces and set the data. */
|
---|
739 | for (struct ifaddrs *pIfCurr = pIfHead; pIfCurr; pIfCurr = pIfCurr->ifa_next)
|
---|
740 | {
|
---|
741 | /*
|
---|
742 | * Only AF_INET and no loopback interfaces
|
---|
743 | * @todo: IPv6 interfaces
|
---|
744 | */
|
---|
745 | if ( pIfCurr->ifa_addr->sa_family == AF_INET
|
---|
746 | && !(pIfCurr->ifa_flags & IFF_LOOPBACK))
|
---|
747 | {
|
---|
748 | char szInetAddr[NI_MAXHOST];
|
---|
749 |
|
---|
750 | memset(szInetAddr, 0, NI_MAXHOST);
|
---|
751 | getnameinfo(pIfCurr->ifa_addr, sizeof(struct sockaddr_in),
|
---|
752 | szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
---|
753 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/IP", cIfacesReport);
|
---|
754 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
|
---|
755 |
|
---|
756 | memset(szInetAddr, 0, NI_MAXHOST);
|
---|
757 | getnameinfo(pIfCurr->ifa_broadaddr, sizeof(struct sockaddr_in),
|
---|
758 | szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
---|
759 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Broadcast", cIfacesReport);
|
---|
760 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
|
---|
761 |
|
---|
762 | memset(szInetAddr, 0, NI_MAXHOST);
|
---|
763 | getnameinfo(pIfCurr->ifa_netmask, sizeof(struct sockaddr_in),
|
---|
764 | szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
---|
765 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Netmask", cIfacesReport);
|
---|
766 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
|
---|
767 |
|
---|
768 | /* Search for the AF_LINK interface of the current AF_INET one and get the mac. */
|
---|
769 | for (struct ifaddrs *pIfLinkCurr = pIfHead; pIfLinkCurr; pIfLinkCurr = pIfLinkCurr->ifa_next)
|
---|
770 | {
|
---|
771 | if ( pIfLinkCurr->ifa_addr->sa_family == AF_LINK
|
---|
772 | && !strcmp(pIfCurr->ifa_name, pIfLinkCurr->ifa_name))
|
---|
773 | {
|
---|
774 | char szMac[32];
|
---|
775 | uint8_t *pu8Mac = NULL;
|
---|
776 | struct sockaddr_dl *pLinkAddress = (struct sockaddr_dl *)pIfLinkCurr->ifa_addr;
|
---|
777 |
|
---|
778 | AssertPtr(pLinkAddress);
|
---|
779 | pu8Mac = (uint8_t *)LLADDR(pLinkAddress);
|
---|
780 | RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
|
---|
781 | pu8Mac[0], pu8Mac[1], pu8Mac[2], pu8Mac[3], pu8Mac[4], pu8Mac[5]);
|
---|
782 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/MAC", cIfacesReport);
|
---|
783 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
|
---|
784 | break;
|
---|
785 | }
|
---|
786 | }
|
---|
787 |
|
---|
788 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/Status", cIfacesReport);
|
---|
789 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, pIfCurr->ifa_flags & IFF_UP ? "Up" : "Down");
|
---|
790 |
|
---|
791 | cIfacesReport++;
|
---|
792 | }
|
---|
793 | }
|
---|
794 |
|
---|
795 | /* Free allocated resources. */
|
---|
796 | freeifaddrs(pIfHead);
|
---|
797 |
|
---|
798 | #else /* !RT_OS_WINDOWS && !RT_OS_FREEBSD */
|
---|
799 | int sd = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
800 | if (sd < 0)
|
---|
801 | {
|
---|
802 | rc = RTErrConvertFromErrno(errno);
|
---|
803 | VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %Rrc\n", rc);
|
---|
804 | return rc;
|
---|
805 | }
|
---|
806 |
|
---|
807 | ifconf ifcfg;
|
---|
808 | char buffer[1024] = {0};
|
---|
809 | ifcfg.ifc_len = sizeof(buffer);
|
---|
810 | ifcfg.ifc_buf = buffer;
|
---|
811 | if (ioctl(sd, SIOCGIFCONF, &ifcfg) < 0)
|
---|
812 | {
|
---|
813 | close(sd);
|
---|
814 | rc = RTErrConvertFromErrno(errno);
|
---|
815 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFCONF) on socket: Error %Rrc\n", rc);
|
---|
816 | return rc;
|
---|
817 | }
|
---|
818 |
|
---|
819 | ifreq* ifrequest = ifcfg.ifc_req;
|
---|
820 | int cIfacesSystem = ifcfg.ifc_len / sizeof(ifreq);
|
---|
821 |
|
---|
822 | for (int i = 0; i < cIfacesSystem; ++i)
|
---|
823 | {
|
---|
824 | sockaddr_in *pAddress;
|
---|
825 | if (ioctl(sd, SIOCGIFFLAGS, &ifrequest[i]) < 0)
|
---|
826 | {
|
---|
827 | rc = RTErrConvertFromErrno(errno);
|
---|
828 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFFLAGS) on socket: Error %Rrc\n", rc);
|
---|
829 | break;
|
---|
830 | }
|
---|
831 | if (ifrequest[i].ifr_flags & IFF_LOOPBACK) /* Skip the loopback device. */
|
---|
832 | continue;
|
---|
833 |
|
---|
834 | bool fIfUp = !!(ifrequest[i].ifr_flags & IFF_UP);
|
---|
835 | pAddress = ((sockaddr_in *)&ifrequest[i].ifr_addr);
|
---|
836 | Assert(pAddress);
|
---|
837 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/IP", cIfacesReport);
|
---|
838 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
839 |
|
---|
840 | if (ioctl(sd, SIOCGIFBRDADDR, &ifrequest[i]) < 0)
|
---|
841 | {
|
---|
842 | rc = RTErrConvertFromErrno(errno);
|
---|
843 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %Rrc\n", rc);
|
---|
844 | break;
|
---|
845 | }
|
---|
846 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_broadaddr;
|
---|
847 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Broadcast", cIfacesReport);
|
---|
848 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
849 |
|
---|
850 | if (ioctl(sd, SIOCGIFNETMASK, &ifrequest[i]) < 0)
|
---|
851 | {
|
---|
852 | rc = RTErrConvertFromErrno(errno);
|
---|
853 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFNETMASK) on socket: Error %Rrc\n", rc);
|
---|
854 | break;
|
---|
855 | }
|
---|
856 | # if defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
|
---|
857 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_addr;
|
---|
858 | # else
|
---|
859 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_netmask;
|
---|
860 | # endif
|
---|
861 |
|
---|
862 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Netmask", cIfacesReport);
|
---|
863 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
864 |
|
---|
865 | # if defined(RT_OS_SOLARIS)
|
---|
866 | /*
|
---|
867 | * "ifreq" is obsolete on Solaris. We use the recommended "lifreq".
|
---|
868 | * We might fail if the interface has not been assigned an IP address.
|
---|
869 | * That doesn't matter; as long as it's plumbed we can pick it up.
|
---|
870 | * But, if it has not acquired an IP address we cannot obtain it's MAC
|
---|
871 | * address this way, so we just use all zeros there.
|
---|
872 | */
|
---|
873 | RTMAC IfMac;
|
---|
874 | RT_ZERO(IfMac);
|
---|
875 | struct lifreq IfReq;
|
---|
876 | RT_ZERO(IfReq);
|
---|
877 | AssertCompile(sizeof(IfReq.lifr_name) >= sizeof(ifrequest[i].ifr_name));
|
---|
878 | strncpy(IfReq.lifr_name, ifrequest[i].ifr_name, sizeof(ifrequest[i].ifr_name));
|
---|
879 | if (ioctl(sd, SIOCGLIFADDR, &IfReq) >= 0)
|
---|
880 | {
|
---|
881 | struct arpreq ArpReq;
|
---|
882 | RT_ZERO(ArpReq);
|
---|
883 | memcpy(&ArpReq.arp_pa, &IfReq.lifr_addr, sizeof(struct sockaddr_in));
|
---|
884 |
|
---|
885 | if (ioctl(sd, SIOCGARP, &ArpReq) >= 0)
|
---|
886 | memcpy(&IfMac, ArpReq.arp_ha.sa_data, sizeof(IfMac));
|
---|
887 | else
|
---|
888 | {
|
---|
889 | rc = RTErrConvertFromErrno(errno);
|
---|
890 | VBoxServiceError("VMInfo/Network: failed to ioctl(SIOCGARP) on socket: Error %Rrc\n", rc);
|
---|
891 | break;
|
---|
892 | }
|
---|
893 | }
|
---|
894 | else
|
---|
895 | {
|
---|
896 | VBoxServiceVerbose(2, "VMInfo/Network: Interface %d has no assigned IP address, skipping ...\n", i);
|
---|
897 | continue;
|
---|
898 | }
|
---|
899 | # else
|
---|
900 | # ifndef RT_OS_OS2 /** @todo port this to OS/2 */
|
---|
901 | if (ioctl(sd, SIOCGIFHWADDR, &ifrequest[i]) < 0)
|
---|
902 | {
|
---|
903 | rc = RTErrConvertFromErrno(errno);
|
---|
904 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFHWADDR) on socket: Error %Rrc\n", rc);
|
---|
905 | break;
|
---|
906 | }
|
---|
907 | # endif
|
---|
908 | # endif
|
---|
909 |
|
---|
910 | # ifndef RT_OS_OS2 /** @todo port this to OS/2 */
|
---|
911 | char szMac[32];
|
---|
912 | # if defined(RT_OS_SOLARIS)
|
---|
913 | uint8_t *pu8Mac = IfMac.au8;
|
---|
914 | # else
|
---|
915 | uint8_t *pu8Mac = (uint8_t*)&ifrequest[i].ifr_hwaddr.sa_data[0]; /* @todo see above */
|
---|
916 | # endif
|
---|
917 | RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
|
---|
918 | pu8Mac[0], pu8Mac[1], pu8Mac[2], pu8Mac[3], pu8Mac[4], pu8Mac[5]);
|
---|
919 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/MAC", cIfacesReport);
|
---|
920 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
|
---|
921 | # endif /* !OS/2*/
|
---|
922 |
|
---|
923 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/Status", cIfacesReport);
|
---|
924 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, fIfUp ? "Up" : "Down");
|
---|
925 | cIfacesReport++;
|
---|
926 | } /* For all interfaces */
|
---|
927 |
|
---|
928 | close(sd);
|
---|
929 | if (RT_FAILURE(rc))
|
---|
930 | VBoxServiceError("VMInfo/Network: Network enumeration for interface %u failed with error %Rrc\n", cIfacesReport, rc);
|
---|
931 |
|
---|
932 | #endif /* !RT_OS_WINDOWS */
|
---|
933 |
|
---|
934 | #if 0 /* Zapping not enabled yet, needs more testing first. */
|
---|
935 | /*
|
---|
936 | * Zap all stale network interface data if the former (saved) network ifaces count
|
---|
937 | * is bigger than the current one.
|
---|
938 | */
|
---|
939 |
|
---|
940 | /* Get former count. */
|
---|
941 | uint32_t cIfacesReportOld;
|
---|
942 | rc = VBoxServiceReadPropUInt32(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/Net/Count", &cIfacesReportOld,
|
---|
943 | 0 /* Min */, UINT32_MAX /* Max */);
|
---|
944 | if ( RT_SUCCESS(rc)
|
---|
945 | && cIfacesReportOld > cIfacesReport) /* Are some ifaces not around anymore? */
|
---|
946 | {
|
---|
947 | VBoxServiceVerbose(3, "VMInfo/Network: Stale interface data detected (%u old vs. %u current)\n",
|
---|
948 | cIfacesReportOld, cIfacesReport);
|
---|
949 |
|
---|
950 | uint32_t uIfaceDeleteIdx = cIfacesReport;
|
---|
951 | do
|
---|
952 | {
|
---|
953 | VBoxServiceVerbose(3, "VMInfo/Network: Deleting stale data of interface %d ...\n", uIfaceDeleteIdx);
|
---|
954 | rc = VBoxServicePropCacheUpdateByPath(&g_VMInfoPropCache, NULL /* Value, delete */, 0 /* Flags */, "/VirtualBox/GuestInfo/Net/%u", uIfaceDeleteIdx++);
|
---|
955 | } while (RT_SUCCESS(rc));
|
---|
956 | }
|
---|
957 | else if ( RT_FAILURE(rc)
|
---|
958 | && rc != VERR_NOT_FOUND)
|
---|
959 | {
|
---|
960 | VBoxServiceError("VMInfo/Network: Failed retrieving old network interfaces count with error %Rrc\n", rc);
|
---|
961 | }
|
---|
962 | #endif
|
---|
963 |
|
---|
964 | /*
|
---|
965 | * This property is a beacon which is _always_ written, even if the network configuration
|
---|
966 | * does not change. If this property is missing, the host assumes that all other GuestInfo
|
---|
967 | * properties are no longer valid.
|
---|
968 | */
|
---|
969 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/Net/Count", "%d",
|
---|
970 | cIfacesReport);
|
---|
971 |
|
---|
972 | /* Don't fail here; just report everything we got. */
|
---|
973 | return VINF_SUCCESS;
|
---|
974 | }
|
---|
975 |
|
---|
976 |
|
---|
977 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
978 | DECLCALLBACK(int) VBoxServiceVMInfoWorker(bool volatile *pfShutdown)
|
---|
979 | {
|
---|
980 | int rc;
|
---|
981 |
|
---|
982 | /*
|
---|
983 | * Tell the control thread that it can continue
|
---|
984 | * spawning services.
|
---|
985 | */
|
---|
986 | RTThreadUserSignal(RTThreadSelf());
|
---|
987 |
|
---|
988 | #ifdef RT_OS_WINDOWS
|
---|
989 | /* Required for network information (must be called per thread). */
|
---|
990 | WSADATA wsaData;
|
---|
991 | if (WSAStartup(MAKEWORD(2, 2), &wsaData))
|
---|
992 | VBoxServiceError("VMInfo/Network: WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError()));
|
---|
993 | #endif /* RT_OS_WINDOWS */
|
---|
994 |
|
---|
995 | /*
|
---|
996 | * Write the fixed properties first.
|
---|
997 | */
|
---|
998 | vboxserviceVMInfoWriteFixedProperties();
|
---|
999 |
|
---|
1000 | /*
|
---|
1001 | * Now enter the loop retrieving runtime data continuously.
|
---|
1002 | */
|
---|
1003 | for (;;)
|
---|
1004 | {
|
---|
1005 | rc = vboxserviceVMInfoWriteUsers();
|
---|
1006 | if (RT_FAILURE(rc))
|
---|
1007 | break;
|
---|
1008 |
|
---|
1009 | rc = vboxserviceVMInfoWriteNetwork();
|
---|
1010 | if (RT_FAILURE(rc))
|
---|
1011 | break;
|
---|
1012 |
|
---|
1013 | /*
|
---|
1014 | * Flush all properties if we were restored.
|
---|
1015 | */
|
---|
1016 | uint64_t idNewSession = g_idVMInfoSession;
|
---|
1017 | VbglR3GetSessionId(&idNewSession);
|
---|
1018 | if (idNewSession != g_idVMInfoSession)
|
---|
1019 | {
|
---|
1020 | VBoxServiceVerbose(3, "VMInfo: The VM session ID changed, flushing all properties\n");
|
---|
1021 | vboxserviceVMInfoWriteFixedProperties();
|
---|
1022 | VBoxServicePropCacheFlush(&g_VMInfoPropCache);
|
---|
1023 | g_idVMInfoSession = idNewSession;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | /*
|
---|
1027 | * Block for a while.
|
---|
1028 | *
|
---|
1029 | * The event semaphore takes care of ignoring interruptions and it
|
---|
1030 | * allows us to implement service wakeup later.
|
---|
1031 | */
|
---|
1032 | if (*pfShutdown)
|
---|
1033 | break;
|
---|
1034 | int rc2 = RTSemEventMultiWait(g_hVMInfoEvent, g_cMsVMInfoInterval);
|
---|
1035 | if (*pfShutdown)
|
---|
1036 | break;
|
---|
1037 | if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
|
---|
1038 | {
|
---|
1039 | VBoxServiceError("VMInfo: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
|
---|
1040 | rc = rc2;
|
---|
1041 | break;
|
---|
1042 | }
|
---|
1043 | else if (RT_LIKELY(RT_SUCCESS(rc2)))
|
---|
1044 | {
|
---|
1045 | /* Reset event semaphore if it got triggered. */
|
---|
1046 | rc2 = RTSemEventMultiReset(g_hVMInfoEvent);
|
---|
1047 | if (RT_FAILURE(rc2))
|
---|
1048 | rc2 = VBoxServiceError("VMInfo: RTSemEventMultiReset failed; rc2=%Rrc\n", rc2);
|
---|
1049 | }
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | #ifdef RT_OS_WINDOWS
|
---|
1053 | WSACleanup();
|
---|
1054 | #endif
|
---|
1055 |
|
---|
1056 | return rc;
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 |
|
---|
1060 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
1061 | static DECLCALLBACK(void) VBoxServiceVMInfoStop(void)
|
---|
1062 | {
|
---|
1063 | RTSemEventMultiSignal(g_hVMInfoEvent);
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 |
|
---|
1067 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
1068 | static DECLCALLBACK(void) VBoxServiceVMInfoTerm(void)
|
---|
1069 | {
|
---|
1070 | if (g_hVMInfoEvent != NIL_RTSEMEVENTMULTI)
|
---|
1071 | {
|
---|
1072 | /** @todo temporary solution: Zap all values which are not valid
|
---|
1073 | * anymore when VM goes down (reboot/shutdown ). Needs to
|
---|
1074 | * be replaced with "temporary properties" later.
|
---|
1075 | *
|
---|
1076 | * One idea is to introduce a (HGCM-)session guest property
|
---|
1077 | * flag meaning that a guest property is only valid as long
|
---|
1078 | * as the HGCM session isn't closed (e.g. guest application
|
---|
1079 | * terminates). [don't remove till implemented]
|
---|
1080 | */
|
---|
1081 | /** @todo r=bird: Drop the VbglR3GuestPropDelSet call here and use the cache
|
---|
1082 | * since it remembers what we've written. */
|
---|
1083 | /* Delete the "../Net" branch. */
|
---|
1084 | const char *apszPat[1] = { "/VirtualBox/GuestInfo/Net/*" };
|
---|
1085 | int rc = VbglR3GuestPropDelSet(g_uVMInfoGuestPropSvcClientID, &apszPat[0], RT_ELEMENTS(apszPat));
|
---|
1086 |
|
---|
1087 | /* Destroy property cache. */
|
---|
1088 | VBoxServicePropCacheDestroy(&g_VMInfoPropCache);
|
---|
1089 |
|
---|
1090 | /* Disconnect from guest properties service. */
|
---|
1091 | rc = VbglR3GuestPropDisconnect(g_uVMInfoGuestPropSvcClientID);
|
---|
1092 | if (RT_FAILURE(rc))
|
---|
1093 | VBoxServiceError("VMInfo: Failed to disconnect from guest property service! Error: %Rrc\n", rc);
|
---|
1094 | g_uVMInfoGuestPropSvcClientID = 0;
|
---|
1095 |
|
---|
1096 | RTSemEventMultiDestroy(g_hVMInfoEvent);
|
---|
1097 | g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
1098 | }
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 |
|
---|
1102 | /**
|
---|
1103 | * The 'vminfo' service description.
|
---|
1104 | */
|
---|
1105 | VBOXSERVICE g_VMInfo =
|
---|
1106 | {
|
---|
1107 | /* pszName. */
|
---|
1108 | "vminfo",
|
---|
1109 | /* pszDescription. */
|
---|
1110 | "Virtual Machine Information",
|
---|
1111 | /* pszUsage. */
|
---|
1112 | " [--vminfo-interval <ms>]"
|
---|
1113 | ,
|
---|
1114 | /* pszOptions. */
|
---|
1115 | " --vminfo-interval Specifies the interval at which to retrieve the\n"
|
---|
1116 | " VM information. The default is 10000 ms.\n"
|
---|
1117 | ,
|
---|
1118 | /* methods */
|
---|
1119 | VBoxServiceVMInfoPreInit,
|
---|
1120 | VBoxServiceVMInfoOption,
|
---|
1121 | VBoxServiceVMInfoInit,
|
---|
1122 | VBoxServiceVMInfoWorker,
|
---|
1123 | VBoxServiceVMInfoStop,
|
---|
1124 | VBoxServiceVMInfoTerm
|
---|
1125 | };
|
---|
1126 |
|
---|