VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceVMInfo.cpp@ 43791

Last change on this file since 43791 was 43791, checked in by vboxsync, 12 years ago

VBoxService/vminfo: Added support for ConsoleKit session detection via D-Bus.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 43.4 KB
Line 
1/* $Id: VBoxServiceVMInfo.cpp 43791 2012-11-01 13:25:57Z 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). */
78static uint32_t g_cMsVMInfoInterval = 0;
79/** The semaphore we're blocking on. */
80static RTSEMEVENTMULTI g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
81/** The guest property service client ID. */
82static uint32_t g_uVMInfoGuestPropSvcClientID = 0;
83/** Number of logged in users in OS. */
84static uint32_t g_cVMInfoLoggedInUsers = UINT32_MAX;
85/** The guest property cache. */
86static VBOXSERVICEVEPROPCACHE g_VMInfoPropCache;
87/** The VM session ID. Changes whenever the VM is restored or reset. */
88static 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 */
114int 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 */
126static DECLCALLBACK(int) VBoxServiceVMInfoPreInit(void)
127{
128 return VINF_SUCCESS;
129}
130
131
132/** @copydoc VBOXSERVICE::pfnOption */
133static 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 */
146static 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 */
206static 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 */
267static 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 */
284static 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 0
562 if (pszUserList && cUsersInList > 0)
563 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", "%s", pszUserList);
564 else
565 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", NULL);
566 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/LoggedInUsers", "%u", cUsersInList);
567 if (g_cVMInfoLoggedInUsers != cUsersInList)
568 {
569 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers",
570 cUsersInList == 0 ? "true" : "false");
571 g_cVMInfoLoggedInUsers = cUsersInList;
572 }
573#endif
574 if (RT_SUCCESS(rc) && pszUserList)
575 RTStrFree(pszUserList);
576 return rc;
577}
578
579
580/**
581 * Provide information about the guest network.
582 */
583static int vboxserviceVMInfoWriteNetwork(void)
584{
585 int rc = VINF_SUCCESS;
586 uint32_t cIfacesReport = 0;
587 char szPropPath[256];
588
589#ifdef RT_OS_WINDOWS
590 IP_ADAPTER_INFO *pAdpInfo = NULL;
591
592# ifndef TARGET_NT4
593 ULONG cbAdpInfo = sizeof(*pAdpInfo);
594 pAdpInfo = (IP_ADAPTER_INFO *)RTMemAlloc(cbAdpInfo);
595 if (!pAdpInfo)
596 {
597 VBoxServiceError("VMInfo/Network: Failed to allocate IP_ADAPTER_INFO\n");
598 return VERR_NO_MEMORY;
599 }
600 DWORD dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
601 if (dwRet == ERROR_BUFFER_OVERFLOW)
602 {
603 IP_ADAPTER_INFO *pAdpInfoNew = (IP_ADAPTER_INFO*)RTMemRealloc(pAdpInfo, cbAdpInfo);
604 if (pAdpInfoNew)
605 {
606 pAdpInfo = pAdpInfoNew;
607 dwRet = GetAdaptersInfo(pAdpInfo, &cbAdpInfo);
608 }
609 }
610 else if (dwRet == ERROR_NO_DATA)
611 {
612 VBoxServiceVerbose(3, "VMInfo/Network: No network adapters available\n");
613
614 /* If no network adapters available / present in the
615 * system we pretend success to not bail out too early. */
616 dwRet = ERROR_SUCCESS;
617 }
618
619 if (dwRet != ERROR_SUCCESS)
620 {
621 if (pAdpInfo)
622 RTMemFree(pAdpInfo);
623 VBoxServiceError("VMInfo/Network: Failed to get adapter info: Error %d\n", dwRet);
624 return RTErrConvertFromWin32(dwRet);
625 }
626# endif /* !TARGET_NT4 */
627
628 SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
629 if (sd == SOCKET_ERROR) /* Socket invalid. */
630 {
631 int wsaErr = WSAGetLastError();
632 /* Don't complain/bail out with an error if network stack is not up; can happen
633 * on NT4 due to start up when not connected shares dialogs pop up. */
634 if (WSAENETDOWN == wsaErr)
635 {
636 VBoxServiceVerbose(0, "VMInfo/Network: Network is not up yet.\n");
637 wsaErr = VINF_SUCCESS;
638 }
639 else
640 VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %d\n", wsaErr);
641 if (pAdpInfo)
642 RTMemFree(pAdpInfo);
643 return RTErrConvertFromWin32(wsaErr);
644 }
645
646 INTERFACE_INFO InterfaceList[20] = {0};
647 unsigned long nBytesReturned = 0;
648 if (WSAIoctl(sd,
649 SIO_GET_INTERFACE_LIST,
650 0,
651 0,
652 &InterfaceList,
653 sizeof(InterfaceList),
654 &nBytesReturned,
655 0,
656 0) == SOCKET_ERROR)
657 {
658 VBoxServiceError("VMInfo/Network: Failed to WSAIoctl() on socket: Error: %d\n", WSAGetLastError());
659 if (pAdpInfo)
660 RTMemFree(pAdpInfo);
661 return RTErrConvertFromWin32(WSAGetLastError());
662 }
663 int cIfacesSystem = nBytesReturned / sizeof(INTERFACE_INFO);
664
665 /** @todo Use GetAdaptersInfo() and GetAdapterAddresses (IPv4 + IPv6) for more information. */
666 for (int i = 0; i < cIfacesSystem; ++i)
667 {
668 sockaddr_in *pAddress;
669 u_long nFlags = 0;
670 if (InterfaceList[i].iiFlags & IFF_LOOPBACK) /* Skip loopback device. */
671 continue;
672 nFlags = InterfaceList[i].iiFlags;
673 pAddress = (sockaddr_in *)&(InterfaceList[i].iiAddress);
674 Assert(pAddress);
675 char szIp[32];
676 RTStrPrintf(szIp, sizeof(szIp), "%s", inet_ntoa(pAddress->sin_addr));
677 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/IP", cIfacesReport);
678 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szIp);
679
680 pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress);
681 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Broadcast", cIfacesReport);
682 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
683
684 pAddress = (sockaddr_in *)&(InterfaceList[i].iiNetmask);
685 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Netmask", cIfacesReport);
686 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
687
688 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/Status", cIfacesReport);
689 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, nFlags & IFF_UP ? "Up" : "Down");
690
691# ifndef TARGET_NT4
692 IP_ADAPTER_INFO *pAdp;
693 for (pAdp = pAdpInfo; pAdp; pAdp = pAdp->Next)
694 if (!strcmp(pAdp->IpAddressList.IpAddress.String, szIp))
695 break;
696
697 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/MAC", cIfacesReport);
698 if (pAdp)
699 {
700 char szMac[32];
701 RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
702 pAdp->Address[0], pAdp->Address[1], pAdp->Address[2],
703 pAdp->Address[3], pAdp->Address[4], pAdp->Address[5]);
704 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
705 }
706 else
707 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, NULL);
708# endif /* !TARGET_NT4 */
709
710 cIfacesReport++;
711 }
712 if (pAdpInfo)
713 RTMemFree(pAdpInfo);
714 if (sd >= 0)
715 closesocket(sd);
716
717#elif defined(RT_OS_HAIKU)
718 /** @todo Haiku: implement network info. retreival */
719 return VERR_NOT_IMPLEMENTED;
720
721#elif defined(RT_OS_FREEBSD)
722 struct ifaddrs *pIfHead = NULL;
723
724 /* Get all available interfaces */
725 rc = getifaddrs(&pIfHead);
726 if (rc < 0)
727 {
728 rc = RTErrConvertFromErrno(errno);
729 VBoxServiceError("VMInfo/Network: Failed to get all interfaces: Error %Rrc\n");
730 return rc;
731 }
732
733 /* Loop through all interfaces and set the data. */
734 for (struct ifaddrs *pIfCurr = pIfHead; pIfCurr; pIfCurr = pIfCurr->ifa_next)
735 {
736 /*
737 * Only AF_INET and no loopback interfaces
738 * @todo: IPv6 interfaces
739 */
740 if ( pIfCurr->ifa_addr->sa_family == AF_INET
741 && !(pIfCurr->ifa_flags & IFF_LOOPBACK))
742 {
743 char szInetAddr[NI_MAXHOST];
744
745 memset(szInetAddr, 0, NI_MAXHOST);
746 getnameinfo(pIfCurr->ifa_addr, sizeof(struct sockaddr_in),
747 szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
748 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/IP", cIfacesReport);
749 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
750
751 memset(szInetAddr, 0, NI_MAXHOST);
752 getnameinfo(pIfCurr->ifa_broadaddr, sizeof(struct sockaddr_in),
753 szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
754 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Broadcast", cIfacesReport);
755 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
756
757 memset(szInetAddr, 0, NI_MAXHOST);
758 getnameinfo(pIfCurr->ifa_netmask, sizeof(struct sockaddr_in),
759 szInetAddr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
760 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Netmask", cIfacesReport);
761 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szInetAddr);
762
763 /* Search for the AF_LINK interface of the current AF_INET one and get the mac. */
764 for (struct ifaddrs *pIfLinkCurr = pIfHead; pIfLinkCurr; pIfLinkCurr = pIfLinkCurr->ifa_next)
765 {
766 if ( pIfLinkCurr->ifa_addr->sa_family == AF_LINK
767 && !strcmp(pIfCurr->ifa_name, pIfLinkCurr->ifa_name))
768 {
769 char szMac[32];
770 uint8_t *pu8Mac = NULL;
771 struct sockaddr_dl *pLinkAddress = (struct sockaddr_dl *)pIfLinkCurr->ifa_addr;
772
773 AssertPtr(pLinkAddress);
774 pu8Mac = (uint8_t *)LLADDR(pLinkAddress);
775 RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
776 pu8Mac[0], pu8Mac[1], pu8Mac[2], pu8Mac[3], pu8Mac[4], pu8Mac[5]);
777 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/MAC", cIfacesReport);
778 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
779 break;
780 }
781 }
782
783 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/Status", cIfacesReport);
784 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, pIfCurr->ifa_flags & IFF_UP ? "Up" : "Down");
785
786 cIfacesReport++;
787 }
788 }
789
790 /* Free allocated resources. */
791 freeifaddrs(pIfHead);
792
793#else /* !RT_OS_WINDOWS && !RT_OS_FREEBSD */
794 int sd = socket(AF_INET, SOCK_DGRAM, 0);
795 if (sd < 0)
796 {
797 rc = RTErrConvertFromErrno(errno);
798 VBoxServiceError("VMInfo/Network: Failed to get a socket: Error %Rrc\n", rc);
799 return rc;
800 }
801
802 ifconf ifcfg;
803 char buffer[1024] = {0};
804 ifcfg.ifc_len = sizeof(buffer);
805 ifcfg.ifc_buf = buffer;
806 if (ioctl(sd, SIOCGIFCONF, &ifcfg) < 0)
807 {
808 close(sd);
809 rc = RTErrConvertFromErrno(errno);
810 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFCONF) on socket: Error %Rrc\n", rc);
811 return rc;
812 }
813
814 ifreq* ifrequest = ifcfg.ifc_req;
815 int cIfacesSystem = ifcfg.ifc_len / sizeof(ifreq);
816
817 for (int i = 0; i < cIfacesSystem; ++i)
818 {
819 sockaddr_in *pAddress;
820 if (ioctl(sd, SIOCGIFFLAGS, &ifrequest[i]) < 0)
821 {
822 rc = RTErrConvertFromErrno(errno);
823 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFFLAGS) on socket: Error %Rrc\n", rc);
824 break;
825 }
826 if (ifrequest[i].ifr_flags & IFF_LOOPBACK) /* Skip the loopback device. */
827 continue;
828
829 bool fIfUp = !!(ifrequest[i].ifr_flags & IFF_UP);
830 pAddress = ((sockaddr_in *)&ifrequest[i].ifr_addr);
831 Assert(pAddress);
832 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/IP", cIfacesReport);
833 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
834
835 if (ioctl(sd, SIOCGIFBRDADDR, &ifrequest[i]) < 0)
836 {
837 rc = RTErrConvertFromErrno(errno);
838 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %Rrc\n", rc);
839 break;
840 }
841 pAddress = (sockaddr_in *)&ifrequest[i].ifr_broadaddr;
842 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Broadcast", cIfacesReport);
843 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
844
845 if (ioctl(sd, SIOCGIFNETMASK, &ifrequest[i]) < 0)
846 {
847 rc = RTErrConvertFromErrno(errno);
848 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFNETMASK) on socket: Error %Rrc\n", rc);
849 break;
850 }
851# if defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
852 pAddress = (sockaddr_in *)&ifrequest[i].ifr_addr;
853# else
854 pAddress = (sockaddr_in *)&ifrequest[i].ifr_netmask;
855# endif
856
857 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/V4/Netmask", cIfacesReport);
858 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
859
860# if defined(RT_OS_SOLARIS)
861 /*
862 * "ifreq" is obsolete on Solaris. We use the recommended "lifreq".
863 * We might fail if the interface has not been assigned an IP address.
864 * That doesn't matter; as long as it's plumbed we can pick it up.
865 * But, if it has not acquired an IP address we cannot obtain it's MAC
866 * address this way, so we just use all zeros there.
867 */
868 RTMAC IfMac;
869 RT_ZERO(IfMac);
870 struct lifreq IfReq;
871 RT_ZERO(IfReq);
872 AssertCompile(sizeof(IfReq.lifr_name) >= sizeof(ifrequest[i].ifr_name));
873 strncpy(IfReq.lifr_name, ifrequest[i].ifr_name, sizeof(ifrequest[i].ifr_name));
874 if (ioctl(sd, SIOCGLIFADDR, &IfReq) >= 0)
875 {
876 struct arpreq ArpReq;
877 RT_ZERO(ArpReq);
878 memcpy(&ArpReq.arp_pa, &IfReq.lifr_addr, sizeof(struct sockaddr_in));
879
880 if (ioctl(sd, SIOCGARP, &ArpReq) >= 0)
881 memcpy(&IfMac, ArpReq.arp_ha.sa_data, sizeof(IfMac));
882 else
883 {
884 rc = RTErrConvertFromErrno(errno);
885 VBoxServiceError("VMInfo/Network: failed to ioctl(SIOCGARP) on socket: Error %Rrc\n", rc);
886 break;
887 }
888 }
889 else
890 {
891 VBoxServiceVerbose(2, "VMInfo/Network: Interface %d has no assigned IP address, skipping ...\n", i);
892 continue;
893 }
894# else
895# ifndef RT_OS_OS2 /** @todo port this to OS/2 */
896 if (ioctl(sd, SIOCGIFHWADDR, &ifrequest[i]) < 0)
897 {
898 rc = RTErrConvertFromErrno(errno);
899 VBoxServiceError("VMInfo/Network: Failed to ioctl(SIOCGIFHWADDR) on socket: Error %Rrc\n", rc);
900 break;
901 }
902# endif
903# endif
904
905# ifndef RT_OS_OS2 /** @todo port this to OS/2 */
906 char szMac[32];
907# if defined(RT_OS_SOLARIS)
908 uint8_t *pu8Mac = IfMac.au8;
909# else
910 uint8_t *pu8Mac = (uint8_t*)&ifrequest[i].ifr_hwaddr.sa_data[0]; /* @todo see above */
911# endif
912 RTStrPrintf(szMac, sizeof(szMac), "%02X%02X%02X%02X%02X%02X",
913 pu8Mac[0], pu8Mac[1], pu8Mac[2], pu8Mac[3], pu8Mac[4], pu8Mac[5]);
914 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/MAC", cIfacesReport);
915 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, "%s", szMac);
916# endif /* !OS/2*/
917
918 RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%u/Status", cIfacesReport);
919 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, szPropPath, fIfUp ? "Up" : "Down");
920 cIfacesReport++;
921 } /* For all interfaces */
922
923 close(sd);
924 if (RT_FAILURE(rc))
925 VBoxServiceError("VMInfo/Network: Network enumeration for interface %u failed with error %Rrc\n", cIfacesReport, rc);
926
927#endif /* !RT_OS_WINDOWS */
928
929#if 0 /* Zapping not enabled yet, needs more testing first. */
930 /*
931 * Zap all stale network interface data if the former (saved) network ifaces count
932 * is bigger than the current one.
933 */
934
935 /* Get former count. */
936 uint32_t cIfacesReportOld;
937 rc = VBoxServiceReadPropUInt32(g_uVMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/Net/Count", &cIfacesReportOld,
938 0 /* Min */, UINT32_MAX /* Max */);
939 if ( RT_SUCCESS(rc)
940 && cIfacesReportOld > cIfacesReport) /* Are some ifaces not around anymore? */
941 {
942 VBoxServiceVerbose(3, "VMInfo/Network: Stale interface data detected (%u old vs. %u current)\n",
943 cIfacesReportOld, cIfacesReport);
944
945 uint32_t uIfaceDeleteIdx = cIfacesReport;
946 do
947 {
948 VBoxServiceVerbose(3, "VMInfo/Network: Deleting stale data of interface %d ...\n", uIfaceDeleteIdx);
949 rc = VBoxServicePropCacheUpdateByPath(&g_VMInfoPropCache, NULL /* Value, delete */, 0 /* Flags */, "/VirtualBox/GuestInfo/Net/%u", uIfaceDeleteIdx++);
950 } while (RT_SUCCESS(rc));
951 }
952 else if ( RT_FAILURE(rc)
953 && rc != VERR_NOT_FOUND)
954 {
955 VBoxServiceError("VMInfo/Network: Failed retrieving old network interfaces count with error %Rrc\n", rc);
956 }
957#endif
958
959 /*
960 * This property is a beacon which is _always_ written, even if the network configuration
961 * does not change. If this property is missing, the host assumes that all other GuestInfo
962 * properties are no longer valid.
963 */
964 VBoxServicePropCacheUpdate(&g_VMInfoPropCache, "/VirtualBox/GuestInfo/Net/Count", "%d",
965 cIfacesReport);
966
967 /* Don't fail here; just report everything we got. */
968 return VINF_SUCCESS;
969}
970
971
972/** @copydoc VBOXSERVICE::pfnWorker */
973DECLCALLBACK(int) VBoxServiceVMInfoWorker(bool volatile *pfShutdown)
974{
975 int rc;
976
977 /*
978 * Tell the control thread that it can continue
979 * spawning services.
980 */
981 RTThreadUserSignal(RTThreadSelf());
982
983#ifdef RT_OS_WINDOWS
984 /* Required for network information (must be called per thread). */
985 WSADATA wsaData;
986 if (WSAStartup(MAKEWORD(2, 2), &wsaData))
987 VBoxServiceError("VMInfo/Network: WSAStartup failed! Error: %Rrc\n", RTErrConvertFromWin32(WSAGetLastError()));
988#endif /* RT_OS_WINDOWS */
989
990 int rc2;
991#ifdef VBOX_WITH_DBUS
992 rc2 = RTDBusLoadLib();
993 if (RT_FAILURE(rc2))
994 VBoxServiceVerbose(0, "VMInfo: D-Bus seems not to be installed; no ConsoleKit session handling available\n");
995#endif /* VBOX_WITH_DBUS */
996
997 /*
998 * Write the fixed properties first.
999 */
1000 vboxserviceVMInfoWriteFixedProperties();
1001
1002 /*
1003 * Now enter the loop retrieving runtime data continuously.
1004 */
1005 for (;;)
1006 {
1007 rc = vboxserviceVMInfoWriteUsers();
1008 if (RT_FAILURE(rc))
1009 break;
1010
1011 rc = vboxserviceVMInfoWriteNetwork();
1012 if (RT_FAILURE(rc))
1013 break;
1014
1015 /*
1016 * Flush all properties if we were restored.
1017 */
1018 uint64_t idNewSession = g_idVMInfoSession;
1019 VbglR3GetSessionId(&idNewSession);
1020 if (idNewSession != g_idVMInfoSession)
1021 {
1022 VBoxServiceVerbose(3, "VMInfo: The VM session ID changed, flushing all properties\n");
1023 vboxserviceVMInfoWriteFixedProperties();
1024 VBoxServicePropCacheFlush(&g_VMInfoPropCache);
1025 g_idVMInfoSession = idNewSession;
1026 }
1027
1028 /*
1029 * Block for a while.
1030 *
1031 * The event semaphore takes care of ignoring interruptions and it
1032 * allows us to implement service wakeup later.
1033 */
1034 if (*pfShutdown)
1035 break;
1036 rc2 = RTSemEventMultiWait(g_hVMInfoEvent, g_cMsVMInfoInterval);
1037 if (*pfShutdown)
1038 break;
1039 if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
1040 {
1041 VBoxServiceError("VMInfo: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
1042 rc = rc2;
1043 break;
1044 }
1045 else if (RT_LIKELY(RT_SUCCESS(rc2)))
1046 {
1047 /* Reset event semaphore if it got triggered. */
1048 rc2 = RTSemEventMultiReset(g_hVMInfoEvent);
1049 if (RT_FAILURE(rc2))
1050 rc2 = VBoxServiceError("VMInfo: RTSemEventMultiReset failed; rc2=%Rrc\n", rc2);
1051 }
1052 }
1053
1054#ifdef RT_OS_WINDOWS
1055 WSACleanup();
1056#endif
1057
1058 return rc;
1059}
1060
1061
1062/** @copydoc VBOXSERVICE::pfnStop */
1063static DECLCALLBACK(void) VBoxServiceVMInfoStop(void)
1064{
1065 RTSemEventMultiSignal(g_hVMInfoEvent);
1066}
1067
1068
1069/** @copydoc VBOXSERVICE::pfnTerm */
1070static DECLCALLBACK(void) VBoxServiceVMInfoTerm(void)
1071{
1072 if (g_hVMInfoEvent != NIL_RTSEMEVENTMULTI)
1073 {
1074 /** @todo temporary solution: Zap all values which are not valid
1075 * anymore when VM goes down (reboot/shutdown ). Needs to
1076 * be replaced with "temporary properties" later.
1077 *
1078 * One idea is to introduce a (HGCM-)session guest property
1079 * flag meaning that a guest property is only valid as long
1080 * as the HGCM session isn't closed (e.g. guest application
1081 * terminates). [don't remove till implemented]
1082 */
1083 /** @todo r=bird: Drop the VbglR3GuestPropDelSet call here and use the cache
1084 * since it remembers what we've written. */
1085 /* Delete the "../Net" branch. */
1086 const char *apszPat[1] = { "/VirtualBox/GuestInfo/Net/*" };
1087 int rc = VbglR3GuestPropDelSet(g_uVMInfoGuestPropSvcClientID, &apszPat[0], RT_ELEMENTS(apszPat));
1088
1089 /* Destroy property cache. */
1090 VBoxServicePropCacheDestroy(&g_VMInfoPropCache);
1091
1092 /* Disconnect from guest properties service. */
1093 rc = VbglR3GuestPropDisconnect(g_uVMInfoGuestPropSvcClientID);
1094 if (RT_FAILURE(rc))
1095 VBoxServiceError("VMInfo: Failed to disconnect from guest property service! Error: %Rrc\n", rc);
1096 g_uVMInfoGuestPropSvcClientID = 0;
1097
1098 RTSemEventMultiDestroy(g_hVMInfoEvent);
1099 g_hVMInfoEvent = NIL_RTSEMEVENTMULTI;
1100 }
1101}
1102
1103
1104/**
1105 * The 'vminfo' service description.
1106 */
1107VBOXSERVICE g_VMInfo =
1108{
1109 /* pszName. */
1110 "vminfo",
1111 /* pszDescription. */
1112 "Virtual Machine Information",
1113 /* pszUsage. */
1114 " [--vminfo-interval <ms>]"
1115 ,
1116 /* pszOptions. */
1117 " --vminfo-interval Specifies the interval at which to retrieve the\n"
1118 " VM information. The default is 10000 ms.\n"
1119 ,
1120 /* methods */
1121 VBoxServiceVMInfoPreInit,
1122 VBoxServiceVMInfoOption,
1123 VBoxServiceVMInfoInit,
1124 VBoxServiceVMInfoWorker,
1125 VBoxServiceVMInfoStop,
1126 VBoxServiceVMInfoTerm
1127};
1128
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette