1 | /* $Id: VBoxServiceVMInfo.cpp 43792 2012-11-01 13:28:44Z 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 | /* Handle desktop sessions using ConsoleKit. */
|
---|
360 | VBoxServiceVerbose(4, "Checking ConsoleKit sessions ...\n");
|
---|
361 |
|
---|
362 | DBusError dbErr;
|
---|
363 | dbus_error_init(&dbErr);
|
---|
364 |
|
---|
365 | DBusConnection *pConnection = dbus_bus_get(DBUS_BUS_SYSTEM, &dbErr);
|
---|
366 | if ( pConnection
|
---|
367 | && !dbus_error_is_set(&dbErr))
|
---|
368 | {
|
---|
369 | /* Get all available sessions. */
|
---|
370 | DBusMessage *pMsgSessions = dbus_message_new_method_call("org.freedesktop.ConsoleKit",
|
---|
371 | "/org/freedesktop/ConsoleKit/Manager",
|
---|
372 | "org.freedesktop.ConsoleKit.Manager",
|
---|
373 | "GetSessions");
|
---|
374 | if ( pMsgSessions
|
---|
375 | && (dbus_message_get_type(pMsgSessions) == DBUS_MESSAGE_TYPE_METHOD_CALL))
|
---|
376 | {
|
---|
377 | DBusMessage *pReplySessions = dbus_connection_send_with_reply_and_block(pConnection,
|
---|
378 | pMsgSessions, 30 * 1000 /* 30s timeout */,
|
---|
379 | &dbErr);
|
---|
380 | if ( pReplySessions
|
---|
381 | && !dbus_error_is_set(&dbErr))
|
---|
382 | {
|
---|
383 | char **ppszSessions; int cSessions;
|
---|
384 | if ( (dbus_message_get_type(pMsgSessions) == DBUS_MESSAGE_TYPE_METHOD_CALL)
|
---|
385 | && vboxService_dbus_message_get_args(pReplySessions, &dbErr, DBUS_TYPE_ARRAY,
|
---|
386 | DBUS_TYPE_OBJECT_PATH, &ppszSessions, &cSessions,
|
---|
387 | DBUS_TYPE_INVALID /* Termination */))
|
---|
388 | {
|
---|
389 | VBoxServiceVerbose(4, "ConsoleKit: retrieved %RU16 session(s)\n", cSessions);
|
---|
390 | AssertPtr(*ppszSessions);
|
---|
391 |
|
---|
392 | char **ppszCurSession = ppszSessions;
|
---|
393 | for (ppszCurSession; *ppszCurSession; ppszCurSession++)
|
---|
394 | {
|
---|
395 | VBoxServiceVerbose(4, "ConsoleKit: processing session '%s' ...\n", *ppszCurSession);
|
---|
396 |
|
---|
397 | /* *ppszCurSession now contains the object path
|
---|
398 | * (e.g. "/org/freedesktop/ConsoleKit/Session1"). */
|
---|
399 | DBusMessage *pMsgUnixUser = dbus_message_new_method_call("org.freedesktop.ConsoleKit",
|
---|
400 | *ppszCurSession,
|
---|
401 | "org.freedesktop.ConsoleKit.Session",
|
---|
402 | "GetUnixUser");
|
---|
403 | if ( pMsgUnixUser
|
---|
404 | && dbus_message_get_type(pMsgUnixUser) == DBUS_MESSAGE_TYPE_METHOD_CALL)
|
---|
405 | {
|
---|
406 | DBusMessage *pReplyUnixUser = dbus_connection_send_with_reply_and_block(pConnection,
|
---|
407 | pMsgUnixUser, 30 * 1000 /* 30s timeout */,
|
---|
408 | &dbErr);
|
---|
409 | if ( pReplyUnixUser
|
---|
410 | && !dbus_error_is_set(&dbErr))
|
---|
411 | {
|
---|
412 | DBusMessageIter itMsg;
|
---|
413 | if ( dbus_message_iter_init(pReplyUnixUser, &itMsg)
|
---|
414 | && dbus_message_iter_get_arg_type(&itMsg) == DBUS_TYPE_UINT32)
|
---|
415 | {
|
---|
416 | /* Get uid from message. */
|
---|
417 | uint32_t uid;
|
---|
418 | dbus_message_iter_get_basic(&itMsg, &uid);
|
---|
419 |
|
---|
420 | /* Look up user name (realname) from uid. */
|
---|
421 | setpwent();
|
---|
422 | struct passwd *ppwEntry = getpwuid(uid);
|
---|
423 | if ( ppwEntry
|
---|
424 | && ppwEntry->pw_name)
|
---|
425 | {
|
---|
426 | VBoxServiceVerbose(4, "ConsoleKit: session '%s' -> %s (uid: %RU32)\n",
|
---|
427 | *ppszCurSession, ppwEntry->pw_name, uid);
|
---|
428 |
|
---|
429 | bool fFound = false;
|
---|
430 | for (uint32_t i = 0; i < cUsersInList && !fFound; i++)
|
---|
431 | fFound = strcmp(papszUsers[i], ppwEntry->pw_name) == 0;
|
---|
432 |
|
---|
433 | if (!fFound)
|
---|
434 | {
|
---|
435 | VBoxServiceVerbose(4, "ConsoleKit: adding user \"%s\" to list\n",
|
---|
436 | ppwEntry->pw_name);
|
---|
437 |
|
---|
438 | rc = RTStrDupEx(&papszUsers[cUsersInList], (const char *)ppwEntry->pw_name);
|
---|
439 | if (RT_FAILURE(rc))
|
---|
440 | break;
|
---|
441 | cUsersInList++;
|
---|
442 | }
|
---|
443 | }
|
---|
444 | else
|
---|
445 | VBoxServiceError("ConsoleKit: unable to lookup user name for uid=%RU32\n", uid);
|
---|
446 | }
|
---|
447 | else
|
---|
448 | AssertMsgFailed(("ConsoleKit: GetUnixUser returned a wrong argument type\n"));
|
---|
449 | }
|
---|
450 |
|
---|
451 | if (pReplyUnixUser)
|
---|
452 | dbus_message_unref(pReplyUnixUser);
|
---|
453 | }
|
---|
454 | else
|
---|
455 | VBoxServiceError("ConsoleKit: unable to retrieve user for session '%s' (msg type=%d): %s",
|
---|
456 | *ppszCurSession, dbus_message_get_type(pMsgUnixUser),
|
---|
457 | dbus_error_is_set(&dbErr) ? dbErr.message : "No error information available\n");
|
---|
458 |
|
---|
459 | if (pMsgUnixUser)
|
---|
460 | dbus_message_unref(pMsgUnixUser);
|
---|
461 | }
|
---|
462 |
|
---|
463 | dbus_free_string_array(ppszSessions);
|
---|
464 | }
|
---|
465 | else
|
---|
466 | {
|
---|
467 | VBoxServiceError("ConsoleKit: unable to retrieve session parameters (msg type=%d): %s",
|
---|
468 | dbus_message_get_type(pMsgSessions),
|
---|
469 | dbus_error_is_set(&dbErr) ? dbErr.message : "No error information available\n");
|
---|
470 | }
|
---|
471 | dbus_message_unref(pReplySessions);
|
---|
472 | }
|
---|
473 |
|
---|
474 | if (pMsgSessions)
|
---|
475 | {
|
---|
476 | dbus_message_unref(pMsgSessions);
|
---|
477 | pMsgSessions = NULL;
|
---|
478 | }
|
---|
479 | }
|
---|
480 | else
|
---|
481 | {
|
---|
482 | static int s_iBitchedAboutConsoleKit = 0;
|
---|
483 | if (s_iBitchedAboutConsoleKit++ < 3)
|
---|
484 | VBoxServiceError("Unable to invoke ConsoleKit (%d/3) -- maybe not installed / used? Error: %s\n",
|
---|
485 | s_iBitchedAboutConsoleKit,
|
---|
486 | dbus_error_is_set(&dbErr) ? dbErr.message : "No error information available\n");
|
---|
487 | }
|
---|
488 |
|
---|
489 | if (pMsgSessions)
|
---|
490 | dbus_message_unref(pMsgSessions);
|
---|
491 | }
|
---|
492 | else
|
---|
493 | {
|
---|
494 | static int s_iBitchedAboutDBus = 0;
|
---|
495 | if (s_iBitchedAboutDBus++ < 3)
|
---|
496 | VBoxServiceError("Unable to connect to system D-Bus (%d/3)\n", s_iBitchedAboutDBus);
|
---|
497 | }
|
---|
498 |
|
---|
499 | if (dbus_error_is_set(&dbErr))
|
---|
500 | dbus_error_free(&dbErr);
|
---|
501 | # endif /* RT_OS_LINUX */
|
---|
502 | #endif /* VBOX_WITH_DBUS */
|
---|
503 |
|
---|
504 | /** @todo Fedora/others: Handle systemd-loginctl. */
|
---|
505 |
|
---|
506 | /* Calc the string length. */
|
---|
507 | size_t cchUserList = 0;
|
---|
508 | if (RT_SUCCESS(rc))
|
---|
509 | {
|
---|
510 | for (uint32_t i = 0; i < cUsersInList; i++)
|
---|
511 | cchUserList += (i != 0) + strlen(papszUsers[i]);
|
---|
512 | }
|
---|
513 |
|
---|
514 | /* Build the user list. */
|
---|
515 | if (RT_SUCCESS(rc))
|
---|
516 | rc = RTStrAllocEx(&pszUserList, cchUserList + 1);
|
---|
517 | if (RT_SUCCESS(rc))
|
---|
518 | {
|
---|
519 | char *psz = pszUserList;
|
---|
520 | for (uint32_t i = 0; i < cUsersInList; i++)
|
---|
521 | {
|
---|
522 | if (i != 0)
|
---|
523 | *psz++ = ',';
|
---|
524 | size_t cch = strlen(papszUsers[i]);
|
---|
525 | memcpy(psz, papszUsers[i], cch);
|
---|
526 | psz += cch;
|
---|
527 | }
|
---|
528 | *psz = '\0';
|
---|
529 | }
|
---|
530 |
|
---|
531 | /* Cleanup. */
|
---|
532 | for (uint32_t i = 0; i < cUsersInList; i++)
|
---|
533 | RTStrFree(papszUsers[i]);
|
---|
534 | RTMemFree(papszUsers);
|
---|
535 |
|
---|
536 | endutxent(); /* Close utmpx file. */
|
---|
537 | #endif
|
---|
538 | Assert(RT_FAILURE(rc) || cUsersInList == 0 || (pszUserList && *pszUserList));
|
---|
539 |
|
---|
540 | /* If the user enumeration above failed, reset the user count to 0 except
|
---|
541 | * we didn't have enough memory anymore. In that case we want to preserve
|
---|
542 | * the previous user count in order to not confuse third party tools which
|
---|
543 | * rely on that count. */
|
---|
544 | if (RT_FAILURE(rc))
|
---|
545 | {
|
---|
546 | if (rc == VERR_NO_MEMORY)
|
---|
547 | {
|
---|
548 | static int s_iVMInfoBitchedOOM = 0;
|
---|
549 | if (s_iVMInfoBitchedOOM++ < 3)
|
---|
550 | VBoxServiceVerbose(0, "Warning: Not enough memory available to enumerate users! Keeping old value (%u)\n",
|
---|
551 | g_cVMInfoLoggedInUsers);
|
---|
552 | cUsersInList = g_cVMInfoLoggedInUsers;
|
---|
553 | }
|
---|
554 | else
|
---|
555 | cUsersInList = 0;
|
---|
556 | }
|
---|
557 |
|
---|
558 | VBoxServiceVerbose(4, "cUsersInList=%RU32, pszUserList=%s, rc=%Rrc\n",
|
---|
559 | cUsersInList, pszUserList ? pszUserList : "<NULL>", rc);
|
---|
560 |
|
---|
561 | if (pszUserList && cUsersInList > 0)
|
---|
562 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", "%s", pszUserList);
|
---|
563 | else
|
---|
564 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", NULL);
|
---|
565 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsers", "%u", cUsersInList);
|
---|
566 | if (g_cVMInfoLoggedInUsers != cUsersInList)
|
---|
567 | {
|
---|
568 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers",
|
---|
569 | cUsersInList == 0 ? "true" : "false");
|
---|
570 | g_cVMInfoLoggedInUsers = cUsersInList;
|
---|
571 | }
|
---|
572 |
|
---|
573 | if (RT_SUCCESS(rc) && pszUserList)
|
---|
574 | RTStrFree(pszUserList);
|
---|
575 | return rc;
|
---|
576 | }
|
---|
577 |
|
---|
578 |
|
---|
579 | /**
|
---|
580 | * Provide information about the guest network.
|
---|
581 | */
|
---|
582 | static int vboxserviceVMInfoWriteNetwork(void)
|
---|
583 | {
|
---|
584 | int rc = VINF_SUCCESS;
|
---|
585 | uint32_t cIfacesReport = 0;
|
---|
586 | char szPropPath[256];
|
---|
587 |
|
---|
588 | #ifdef RT_OS_WINDOWS
|
---|
589 | IP_ADAPTER_INFO *pAdpInfo = NULL;
|
---|
590 |
|
---|
591 | # ifndef TARGET_NT4
|
---|
592 | ULONG cbAdpInfo = sizeof(*pAdpInfo);
|
---|
593 | pAdpInfo = (IP_ADAPTER_INFO *)RTMemAlloc(cbAdpInfo);
|
---|
594 | if (!pAdpInfo)
|
---|
595 | {
|
---|
596 | VBoxServiceError("VMInfo/Network: Failed to allocate IP_ADAPTER_INFO\n");
|
---|
597 | return VERR_NO_MEMORY;
|
---|
598 | }
|
---|
599 | DWORD dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
|
---|
600 | if (dwRet == ERROR_BUFFER_OVERFLOW)
|
---|
601 | {
|
---|
602 | IP_ADAPTER_INFO *pAdpInfoNew = (IP_ADAPTER_INFO*)RTMemRealloc(pAdpInfo, cbAdpInfo);
|
---|
603 | if (pAdpInfoNew)
|
---|
604 | {
|
---|
605 | pAdpInfo = pAdpInfoNew;
|
---|
606 | dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
|
---|
607 | }
|
---|
608 | }
|
---|
609 | else if (dwRet == ERROR_NO_DATA)
|
---|
610 | {
|
---|
611 | VBoxServiceVerbose(3, "VMInfo/Network: No network adapters available\n");
|
---|
612 |
|
---|
613 | /* If no network adapters available / present in the
|
---|
614 | * system we pretend success to not bail out too early. */
|
---|
615 | dwRet = ERROR_SUCCESS;
|
---|
616 | }
|
---|
617 |
|
---|
618 | if (dwRet != ERROR_SUCCESS)
|
---|
619 | {
|
---|
620 | if (pAdpInfo)
|
---|
621 | RTMemFree(pAdpInfo);
|
---|
622 | VBoxServiceError("VMInfo/Network: Failed to get adapter info: Error %d\n", dwRet);
|
---|
623 | return RTErrConvertFromWin32(dwRet);
|
---|
624 | }
|
---|
625 | # endif /* !TARGET_NT4 */
|
---|
626 |
|
---|
627 | SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
|
---|
628 | if (sd == SOCKET_ERROR) /* Socket invalid. */
|
---|
629 | {
|
---|
630 | int wsaErr = WSAGetLastError();
|
---|
631 | /* Don't complain/bail out with an error if network stack is not up; can happen
|
---|
632 | * on NT4 due to start up when not connected shares dialogs pop up. */
|
---|
633 | if (WSAENETDOWN == wsaErr)
|
---|
634 | {
|
---|
635 | VBoxServiceVerbose(0, "VMInfo/Network: Network is not up yet.\n");
|
---|
636 | wsaErr = VINF_SUCCESS;
|
---|
637 | }
|
---|
638 | else
|
---|
639 | VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %d\n", wsaErr);
|
---|
640 | if (pAdpInfo)
|
---|
641 | RTMemFree(pAdpInfo);
|
---|
642 | return RTErrConvertFromWin32(wsaErr);
|
---|
643 | }
|
---|
644 |
|
---|
645 | INTERFACE_INFO InterfaceList[20] = {0};
|
---|
646 | unsigned long nBytesReturned = 0;
|
---|
647 | if (WSAIoctl(sd,
|
---|
648 | SIO_GET_INTERFACE_LIST,
|
---|
649 | 0,
|
---|
650 | 0,
|
---|
651 | &InterfaceList,
|
---|
652 | sizeof(InterfaceList),
|
---|
653 | &nBytesReturned,
|
---|
654 | 0,
|
---|
655 | 0) == SOCKET_ERROR)
|
---|
656 | {
|
---|
657 | VBoxServiceError("VMInfo/Network: Failed to WSAIoctl() on socket: Error: %d\n", WSAGetLastError());
|
---|
658 | if (pAdpInfo)
|
---|
659 | RTMemFree(pAdpInfo);
|
---|
660 | return RTErrConvertFromWin32(WSAGetLastError());
|
---|
661 | }
|
---|
662 | int cIfacesSystem = nBytesReturned / sizeof(INTERFACE_INFO);
|
---|
663 |
|
---|
664 | /** @todo Use GetAdaptersInfo() and GetAdapterAddresses (IPv4 + IPv6) for more information. */
|
---|
665 | for (int i = 0; i < cIfacesSystem; ++i)
|
---|
666 | {
|
---|
667 | sockaddr_in *pAddress;
|
---|
668 | u_long nFlags = 0;
|
---|
669 | if (InterfaceList[i].iiFlags & IFF_LOOPBACK) /* Skip loopback device. */
|
---|
670 | continue;
|
---|
671 | nFlags = InterfaceList[i].iiFlags;
|
---|
672 | pAddress = (sockaddr_in *)&(InterfaceList[i].iiAddress);
|
---|
673 | Assert(pAddress);
|
---|
674 | char szIp[32];
|
---|
675 | RTStrPrintf(szIp, sizeof(szIp), "%s", inet_ntoa(pAddress->sin_addr));
|
---|
676 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/IP", cIfacesReport);
|
---|
677 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szIp);
|
---|
678 |
|
---|
679 | pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress);
|
---|
680 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Broadcast", cIfacesReport);
|
---|
681 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
682 |
|
---|
683 | pAddress = (sockaddr_in *)&(InterfaceList[i].iiNetmask);
|
---|
684 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Netmask", cIfacesReport);
|
---|
685 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
686 |
|
---|
687 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/Status", cIfacesReport);
|
---|
688 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, nFlags & IFF_UP ? "Up" : "Down");
|
---|
689 |
|
---|
690 | # ifndef TARGET_NT4
|
---|
691 | IP_ADAPTER_INFO *pAdp;
|
---|
692 | for (pAdp = pAdpInfo; pAdp; pAdp = pAdp->Next)
|
---|
693 | if (!strcmp(pAdp->IpAddressList.IpAddress.String, szIp))
|
---|
694 | break;
|
---|
695 |
|
---|
696 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/MAC", cIfacesReport);
|
---|
697 | if (pAdp)
|
---|
698 | {
|
---|
699 | char szMac[32];
|
---|
700 | RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
|
---|
701 | pAdp->Address[0], pAdp->Address[1], pAdp->Address[2],
|
---|
702 | pAdp->Address[3], pAdp->Address[4], pAdp->Address[5]);
|
---|
703 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
|
---|
704 | }
|
---|
705 | else
|
---|
706 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, NULL);
|
---|
707 | # endif /* !TARGET_NT4 */
|
---|
708 |
|
---|
709 | cIfacesReport++;
|
---|
710 | }
|
---|
711 | if (pAdpInfo)
|
---|
712 | RTMemFree(pAdpInfo);
|
---|
713 | if (sd >= 0)
|
---|
714 | closesocket(sd);
|
---|
715 |
|
---|
716 | #elif defined(RT_OS_HAIKU)
|
---|
717 | /** @todo Haiku: implement network info. retreival */
|
---|
718 | return VERR_NOT_IMPLEMENTED;
|
---|
719 |
|
---|
720 | #elif defined(RT_OS_FREEBSD)
|
---|
721 | struct ifaddrs *pIfHead = NULL;
|
---|
722 |
|
---|
723 | /* Get all available interfaces */
|
---|
724 | rc = getifaddrs(&pIfHead);
|
---|
725 | if (rc < 0)
|
---|
726 | {
|
---|
727 | rc = RTErrConvertFromErrno(errno);
|
---|
728 | VBoxServiceError("VMInfo/Network: Failed to get all interfaces: Error %Rrc\n");
|
---|
729 | return rc;
|
---|
730 | }
|
---|
731 |
|
---|
732 | /* Loop through all interfaces and set the data. */
|
---|
733 | for (struct ifaddrs *pIfCurr = pIfHead; pIfCurr; pIfCurr = pIfCurr->ifa_next)
|
---|
734 | {
|
---|
735 | /*
|
---|
736 | * Only AF_INET and no loopback interfaces
|
---|
737 | * @todo: IPv6 interfaces
|
---|
738 | */
|
---|
739 | if ( pIfCurr->ifa_addr->sa_family == AF_INET
|
---|
740 | && !(pIfCurr->ifa_flags & IFF_LOOPBACK))
|
---|
741 | {
|
---|
742 | char szInetAddr[NI_MAXHOST];
|
---|
743 |
|
---|
744 | memset(szInetAddr, 0, NI_MAXHOST);
|
---|
745 | getnameinfo(pIfCurr->ifa_addr, sizeof(struct sockaddr_in),
|
---|
746 | szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
---|
747 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/IP", cIfacesReport);
|
---|
748 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
|
---|
749 |
|
---|
750 | memset(szInetAddr, 0, NI_MAXHOST);
|
---|
751 | getnameinfo(pIfCurr->ifa_broadaddr, sizeof(struct sockaddr_in),
|
---|
752 | szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
---|
753 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Broadcast", cIfacesReport);
|
---|
754 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
|
---|
755 |
|
---|
756 | memset(szInetAddr, 0, NI_MAXHOST);
|
---|
757 | getnameinfo(pIfCurr->ifa_netmask, sizeof(struct sockaddr_in),
|
---|
758 | szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
---|
759 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Netmask", cIfacesReport);
|
---|
760 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
|
---|
761 |
|
---|
762 | /* Search for the AF_LINK interface of the current AF_INET one and get the mac. */
|
---|
763 | for (struct ifaddrs *pIfLinkCurr = pIfHead; pIfLinkCurr; pIfLinkCurr = pIfLinkCurr->ifa_next)
|
---|
764 | {
|
---|
765 | if ( pIfLinkCurr->ifa_addr->sa_family == AF_LINK
|
---|
766 | && !strcmp(pIfCurr->ifa_name, pIfLinkCurr->ifa_name))
|
---|
767 | {
|
---|
768 | char szMac[32];
|
---|
769 | uint8_t *pu8Mac = NULL;
|
---|
770 | struct sockaddr_dl *pLinkAddress = (struct sockaddr_dl *)pIfLinkCurr->ifa_addr;
|
---|
771 |
|
---|
772 | AssertPtr(pLinkAddress);
|
---|
773 | pu8Mac = (uint8_t *)LLADDR(pLinkAddress);
|
---|
774 | RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
|
---|
775 | pu8Mac[0], pu8Mac[1], pu8Mac[2], pu8Mac[3], pu8Mac[4], pu8Mac[5]);
|
---|
776 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/MAC", cIfacesReport);
|
---|
777 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
|
---|
778 | break;
|
---|
779 | }
|
---|
780 | }
|
---|
781 |
|
---|
782 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/Status", cIfacesReport);
|
---|
783 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, pIfCurr->ifa_flags & IFF_UP ? "Up" : "Down");
|
---|
784 |
|
---|
785 | cIfacesReport++;
|
---|
786 | }
|
---|
787 | }
|
---|
788 |
|
---|
789 | /* Free allocated resources. */
|
---|
790 | freeifaddrs(pIfHead);
|
---|
791 |
|
---|
792 | #else /* !RT_OS_WINDOWS && !RT_OS_FREEBSD */
|
---|
793 | int sd = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
794 | if (sd < 0)
|
---|
795 | {
|
---|
796 | rc = RTErrConvertFromErrno(errno);
|
---|
797 | VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %Rrc\n", rc);
|
---|
798 | return rc;
|
---|
799 | }
|
---|
800 |
|
---|
801 | ifconf ifcfg;
|
---|
802 | char buffer[1024] = {0};
|
---|
803 | ifcfg.ifc_len = sizeof(buffer);
|
---|
804 | ifcfg.ifc_buf = buffer;
|
---|
805 | if (ioctl(sd, SIOCGIFCONF, &ifcfg) < 0)
|
---|
806 | {
|
---|
807 | close(sd);
|
---|
808 | rc = RTErrConvertFromErrno(errno);
|
---|
809 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFCONF) on socket: Error %Rrc\n", rc);
|
---|
810 | return rc;
|
---|
811 | }
|
---|
812 |
|
---|
813 | ifreq* ifrequest = ifcfg.ifc_req;
|
---|
814 | int cIfacesSystem = ifcfg.ifc_len / sizeof(ifreq);
|
---|
815 |
|
---|
816 | for (int i = 0; i < cIfacesSystem; ++i)
|
---|
817 | {
|
---|
818 | sockaddr_in *pAddress;
|
---|
819 | if (ioctl(sd, SIOCGIFFLAGS, &ifrequest[i]) < 0)
|
---|
820 | {
|
---|
821 | rc = RTErrConvertFromErrno(errno);
|
---|
822 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFFLAGS) on socket: Error %Rrc\n", rc);
|
---|
823 | break;
|
---|
824 | }
|
---|
825 | if (ifrequest[i].ifr_flags & IFF_LOOPBACK) /* Skip the loopback device. */
|
---|
826 | continue;
|
---|
827 |
|
---|
828 | bool fIfUp = !!(ifrequest[i].ifr_flags & IFF_UP);
|
---|
829 | pAddress = ((sockaddr_in *)&ifrequest[i].ifr_addr);
|
---|
830 | Assert(pAddress);
|
---|
831 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/IP", cIfacesReport);
|
---|
832 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
833 |
|
---|
834 | if (ioctl(sd, SIOCGIFBRDADDR, &ifrequest[i]) < 0)
|
---|
835 | {
|
---|
836 | rc = RTErrConvertFromErrno(errno);
|
---|
837 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %Rrc\n", rc);
|
---|
838 | break;
|
---|
839 | }
|
---|
840 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_broadaddr;
|
---|
841 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Broadcast", cIfacesReport);
|
---|
842 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
843 |
|
---|
844 | if (ioctl(sd, SIOCGIFNETMASK, &ifrequest[i]) < 0)
|
---|
845 | {
|
---|
846 | rc = RTErrConvertFromErrno(errno);
|
---|
847 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFNETMASK) on socket: Error %Rrc\n", rc);
|
---|
848 | break;
|
---|
849 | }
|
---|
850 | # if defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
|
---|
851 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_addr;
|
---|
852 | # else
|
---|
853 | pAddress = (sockaddr_in *)&ifrequest[i].ifr_netmask;
|
---|
854 | # endif
|
---|
855 |
|
---|
856 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Netmask", cIfacesReport);
|
---|
857 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
|
---|
858 |
|
---|
859 | # if defined(RT_OS_SOLARIS)
|
---|
860 | /*
|
---|
861 | * "ifreq" is obsolete on Solaris. We use the recommended "lifreq".
|
---|
862 | * We might fail if the interface has not been assigned an IP address.
|
---|
863 | * That doesn't matter; as long as it's plumbed we can pick it up.
|
---|
864 | * But, if it has not acquired an IP address we cannot obtain it's MAC
|
---|
865 | * address this way, so we just use all zeros there.
|
---|
866 | */
|
---|
867 | RTMAC IfMac;
|
---|
868 | RT_ZERO(IfMac);
|
---|
869 | struct lifreq IfReq;
|
---|
870 | RT_ZERO(IfReq);
|
---|
871 | AssertCompile(sizeof(IfReq.lifr_name) >= sizeof(ifrequest[i].ifr_name));
|
---|
872 | strncpy(IfReq.lifr_name, ifrequest[i].ifr_name, sizeof(ifrequest[i].ifr_name));
|
---|
873 | if (ioctl(sd, SIOCGLIFADDR, &IfReq) >= 0)
|
---|
874 | {
|
---|
875 | struct arpreq ArpReq;
|
---|
876 | RT_ZERO(ArpReq);
|
---|
877 | memcpy(&ArpReq.arp_pa, &IfReq.lifr_addr, sizeof(struct sockaddr_in));
|
---|
878 |
|
---|
879 | if (ioctl(sd, SIOCGARP, &ArpReq) >= 0)
|
---|
880 | memcpy(&IfMac, ArpReq.arp_ha.sa_data, sizeof(IfMac));
|
---|
881 | else
|
---|
882 | {
|
---|
883 | rc = RTErrConvertFromErrno(errno);
|
---|
884 | VBoxServiceError("VMInfo/Network: failed to ioctl(SIOCGARP) on socket: Error %Rrc\n", rc);
|
---|
885 | break;
|
---|
886 | }
|
---|
887 | }
|
---|
888 | else
|
---|
889 | {
|
---|
890 | VBoxServiceVerbose(2, "VMInfo/Network: Interface %d has no assigned IP address, skipping ...\n", i);
|
---|
891 | continue;
|
---|
892 | }
|
---|
893 | # else
|
---|
894 | # ifndef RT_OS_OS2 /** @todo port this to OS/2 */
|
---|
895 | if (ioctl(sd, SIOCGIFHWADDR, &ifrequest[i]) < 0)
|
---|
896 | {
|
---|
897 | rc = RTErrConvertFromErrno(errno);
|
---|
898 | VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFHWADDR) on socket: Error %Rrc\n", rc);
|
---|
899 | break;
|
---|
900 | }
|
---|
901 | # endif
|
---|
902 | # endif
|
---|
903 |
|
---|
904 | # ifndef RT_OS_OS2 /** @todo port this to OS/2 */
|
---|
905 | char szMac[32];
|
---|
906 | # if defined(RT_OS_SOLARIS)
|
---|
907 | uint8_t *pu8Mac = IfMac.au8;
|
---|
908 | # else
|
---|
909 | uint8_t *pu8Mac = (uint8_t*)&ifrequest[i].ifr_hwaddr.sa_data[0]; /* @todo see above */
|
---|
910 | # endif
|
---|
911 | RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
|
---|
912 | pu8Mac[0], pu8Mac[1], pu8Mac[2], pu8Mac[3], pu8Mac[4], pu8Mac[5]);
|
---|
913 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/MAC", cIfacesReport);
|
---|
914 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
|
---|
915 | # endif /* !OS/2*/
|
---|
916 |
|
---|
917 | RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/Status", cIfacesReport);
|
---|
918 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, fIfUp ? "Up" : "Down");
|
---|
919 | cIfacesReport++;
|
---|
920 | } /* For all interfaces */
|
---|
921 |
|
---|
922 | close(sd);
|
---|
923 | if (RT_FAILURE(rc))
|
---|
924 | VBoxServiceError("VMInfo/Network: Network enumeration for interface %u failed with error %Rrc\n", cIfacesReport, rc);
|
---|
925 |
|
---|
926 | #endif /* !RT_OS_WINDOWS */
|
---|
927 |
|
---|
928 | #if 0 /* Zapping not enabled yet, needs more testing first. */
|
---|
929 | /*
|
---|
930 | * Zap all stale network interface data if the former (saved) network ifaces count
|
---|
931 | * is bigger than the current one.
|
---|
932 | */
|
---|
933 |
|
---|
934 | /* Get former count. */
|
---|
935 | uint32_t cIfacesReportOld;
|
---|
936 | rc = VBoxServiceReadPropUInt32(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/Net/Count", &cIfacesReportOld,
|
---|
937 | 0 /* Min */, UINT32_MAX /* Max */);
|
---|
938 | if ( RT_SUCCESS(rc)
|
---|
939 | && cIfacesReportOld > cIfacesReport) /* Are some ifaces not around anymore? */
|
---|
940 | {
|
---|
941 | VBoxServiceVerbose(3, "VMInfo/Network: Stale interface data detected (%u old vs. %u current)\n",
|
---|
942 | cIfacesReportOld, cIfacesReport);
|
---|
943 |
|
---|
944 | uint32_t uIfaceDeleteIdx = cIfacesReport;
|
---|
945 | do
|
---|
946 | {
|
---|
947 | VBoxServiceVerbose(3, "VMInfo/Network: Deleting stale data of interface %d ...\n", uIfaceDeleteIdx);
|
---|
948 | rc = VBoxServicePropCacheUpdateByPath(&g_VMInfoPropCache, NULL /* Value, delete */, 0 /* Flags */, "/VirtualBox/GuestInfo/Net/%u", uIfaceDeleteIdx++);
|
---|
949 | } while (RT_SUCCESS(rc));
|
---|
950 | }
|
---|
951 | else if ( RT_FAILURE(rc)
|
---|
952 | && rc != VERR_NOT_FOUND)
|
---|
953 | {
|
---|
954 | VBoxServiceError("VMInfo/Network: Failed retrieving old network interfaces count with error %Rrc\n", rc);
|
---|
955 | }
|
---|
956 | #endif
|
---|
957 |
|
---|
958 | /*
|
---|
959 | * This property is a beacon which is _always_ written, even if the network configuration
|
---|
960 | * does not change. If this property is missing, the host assumes that all other GuestInfo
|
---|
961 | * properties are no longer valid.
|
---|
962 | */
|
---|
963 | VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/Net/Count", "%d",
|
---|
964 | cIfacesReport);
|
---|
965 |
|
---|
966 | /* Don't fail here; just report everything we got. */
|
---|
967 | return VINF_SUCCESS;
|
---|
968 | }
|
---|
969 |
|
---|
970 |
|
---|
971 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
972 | DECLCALLBACK(int) VBoxServiceVMInfoWorker(bool volatile *pfShutdown)
|
---|
973 | {
|
---|
974 | int rc;
|
---|
975 |
|
---|
976 | /*
|
---|
977 | * Tell the control thread that it can continue
|
---|
978 | * spawning services.
|
---|
979 | */
|
---|
980 | RTThreadUserSignal(RTThreadSelf());
|
---|
981 |
|
---|
982 | #ifdef RT_OS_WINDOWS
|
---|
983 | /* Required for network information (must be called per thread). */
|
---|
984 | WSADATA wsaData;
|
---|
985 | if (WSAStartup(MAKEWORD(2, 2), &wsaData))
|
---|
986 | VBoxServiceError("VMInfo/Network: WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError()));
|
---|
987 | #endif /* RT_OS_WINDOWS */
|
---|
988 |
|
---|
989 | int rc2;
|
---|
990 | #ifdef VBOX_WITH_DBUS
|
---|
991 | rc2 = RTDBusLoadLib();
|
---|
992 | if (RT_FAILURE(rc2))
|
---|
993 | VBoxServiceVerbose(0, "VMInfo: D-Bus seems not to be installed; no ConsoleKit session handling available\n");
|
---|
994 | #endif /* VBOX_WITH_DBUS */
|
---|
995 |
|
---|
996 | /*
|
---|
997 | * Write the fixed properties first.
|
---|
998 | */
|
---|
999 | vboxserviceVMInfoWriteFixedProperties();
|
---|
1000 |
|
---|
1001 | /*
|
---|
1002 | * Now enter the loop retrieving runtime data continuously.
|
---|
1003 | */
|
---|
1004 | for (;;)
|
---|
1005 | {
|
---|
1006 | rc = vboxserviceVMInfoWriteUsers();
|
---|
1007 | if (RT_FAILURE(rc))
|
---|
1008 | break;
|
---|
1009 |
|
---|
1010 | rc = vboxserviceVMInfoWriteNetwork();
|
---|
1011 | if (RT_FAILURE(rc))
|
---|
1012 | break;
|
---|
1013 |
|
---|
1014 | /*
|
---|
1015 | * Flush all properties if we were restored.
|
---|
1016 | */
|
---|
1017 | uint64_t idNewSession = g_idVMInfoSession;
|
---|
1018 | VbglR3GetSessionId(&idNewSession);
|
---|
1019 | if (idNewSession != g_idVMInfoSession)
|
---|
1020 | {
|
---|
1021 | VBoxServiceVerbose(3, "VMInfo: The VM session ID changed, flushing all properties\n");
|
---|
1022 | vboxserviceVMInfoWriteFixedProperties();
|
---|
1023 | VBoxServicePropCacheFlush(&g_VMInfoPropCache);
|
---|
1024 | g_idVMInfoSession = idNewSession;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | /*
|
---|
1028 | * Block for a while.
|
---|
1029 | *
|
---|
1030 | * The event semaphore takes care of ignoring interruptions and it
|
---|
1031 | * allows us to implement service wakeup later.
|
---|
1032 | */
|
---|
1033 | if (*pfShutdown)
|
---|
1034 | break;
|
---|
1035 | rc2 = RTSemEventMultiWait(g_hVMInfoEvent, g_cMsVMInfoInterval);
|
---|
1036 | if (*pfShutdown)
|
---|
1037 | break;
|
---|
1038 | if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
|
---|
1039 | {
|
---|
1040 | VBoxServiceError("VMInfo: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
|
---|
1041 | rc = rc2;
|
---|
1042 | break;
|
---|
1043 | }
|
---|
1044 | else if (RT_LIKELY(RT_SUCCESS(rc2)))
|
---|
1045 | {
|
---|
1046 | /* Reset event semaphore if it got triggered. */
|
---|
1047 | rc2 = RTSemEventMultiReset(g_hVMInfoEvent);
|
---|
1048 | if (RT_FAILURE(rc2))
|
---|
1049 | rc2 = VBoxServiceError("VMInfo: RTSemEventMultiReset failed; rc2=%Rrc\n", rc2);
|
---|
1050 | }
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | #ifdef RT_OS_WINDOWS
|
---|
1054 | WSACleanup();
|
---|
1055 | #endif
|
---|
1056 |
|
---|
1057 | return rc;
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 |
|
---|
1061 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
1062 | static DECLCALLBACK(void) VBoxServiceVMInfoStop(void)
|
---|
1063 | {
|
---|
1064 | RTSemEventMultiSignal(g_hVMInfoEvent);
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 |
|
---|
1068 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
1069 | static DECLCALLBACK(void) VBoxServiceVMInfoTerm(void)
|
---|
1070 | {
|
---|
1071 | if (g_hVMInfoEvent != NIL_RTSEMEVENTMULTI)
|
---|
1072 | {
|
---|
1073 | /** @todo temporary solution: Zap all values which are not valid
|
---|
1074 | * anymore when VM goes down (reboot/shutdown ). Needs to
|
---|
1075 | * be replaced with "temporary properties" later.
|
---|
1076 | *
|
---|
1077 | * One idea is to introduce a (HGCM-)session guest property
|
---|
1078 | * flag meaning that a guest property is only valid as long
|
---|
1079 | * as the HGCM session isn't closed (e.g. guest application
|
---|
1080 | * terminates). [don't remove till implemented]
|
---|
1081 | */
|
---|
1082 | /** @todo r=bird: Drop the VbglR3GuestPropDelSet call here and use the cache
|
---|
1083 | * since it remembers what we've written. */
|
---|
1084 | /* Delete the "../Net" branch. */
|
---|
1085 | const char *apszPat[1] = { "/VirtualBox/GuestInfo/Net/*" };
|
---|
1086 | int rc = VbglR3GuestPropDelSet(g_uVMInfoGuestPropSvcClientID, &apszPat[0], RT_ELEMENTS(apszPat));
|
---|
1087 |
|
---|
1088 | /* Destroy property cache. */
|
---|
1089 | VBoxServicePropCacheDestroy(&g_VMInfoPropCache);
|
---|
1090 |
|
---|
1091 | /* Disconnect from guest properties service. */
|
---|
1092 | rc = VbglR3GuestPropDisconnect(g_uVMInfoGuestPropSvcClientID);
|
---|
1093 | if (RT_FAILURE(rc))
|
---|
1094 | VBoxServiceError("VMInfo: Failed to disconnect from guest property service! Error: %Rrc\n", rc);
|
---|
1095 | g_uVMInfoGuestPropSvcClientID = 0;
|
---|
1096 |
|
---|
1097 | RTSemEventMultiDestroy(g_hVMInfoEvent);
|
---|
1098 | g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
|
---|
1099 | }
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 |
|
---|
1103 | /**
|
---|
1104 | * The 'vminfo' service description.
|
---|
1105 | */
|
---|
1106 | VBOXSERVICE g_VMInfo =
|
---|
1107 | {
|
---|
1108 | /* pszName. */
|
---|
1109 | "vminfo",
|
---|
1110 | /* pszDescription. */
|
---|
1111 | "Virtual Machine Information",
|
---|
1112 | /* pszUsage. */
|
---|
1113 | " [--vminfo-interval <ms>]"
|
---|
1114 | ,
|
---|
1115 | /* pszOptions. */
|
---|
1116 | " --vminfo-interval Specifies the interval at which to retrieve the\n"
|
---|
1117 | " VM information. The default is 10000 ms.\n"
|
---|
1118 | ,
|
---|
1119 | /* methods */
|
---|
1120 | VBoxServiceVMInfoPreInit,
|
---|
1121 | VBoxServiceVMInfoOption,
|
---|
1122 | VBoxServiceVMInfoInit,
|
---|
1123 | VBoxServiceVMInfoWorker,
|
---|
1124 | VBoxServiceVMInfoStop,
|
---|
1125 | VBoxServiceVMInfoTerm
|
---|
1126 | };
|
---|
1127 |
|
---|