VBoxServiceVMInfo.cpp revision 16897bbb9d5fd853e9f847abd86c5266a927b0d1
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * VBoxService - Virtual Machine Information for the Host.
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * Copyright (C) 2009-2010 Sun Microsystems, Inc.
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * available from http://www.virtualbox.org. This file is free software;
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * you can redistribute it and/or modify it under the terms of the GNU
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * General Public License (GPL) as published by the Free Software
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * additional information or have any questions.
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync/*******************************************************************************
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync* Header Files *
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync*******************************************************************************/
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync# ifndef RT_OS_FREEBSD /* The header does not exist anymore since FreeBSD 9-current */
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync/*******************************************************************************
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync* Global Variables *
87f68d052aecda193e89e8f41ec147606c7f4e0bvboxsync*******************************************************************************/
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync/** The vminfo interval (millseconds). */
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync/** The semaphore we're blocking on. */
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsyncstatic RTSEMEVENTMULTI g_VMInfoEvent = NIL_RTSEMEVENTMULTI;
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync/** The guest property service client ID. */
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync/** Number of logged in users in OS. */
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsyncstatic uint32_t g_cVMInfoLoggedInUsers = UINT32_MAX;
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync/** @copydoc VBOXSERVICE::pfnPreInit */
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsyncstatic DECLCALLBACK(int) VBoxServiceVMInfoPreInit(void)
87f68d052aecda193e89e8f41ec147606c7f4e0bvboxsync/** @copydoc VBOXSERVICE::pfnOption */
87f68d052aecda193e89e8f41ec147606c7f4e0bvboxsyncstatic DECLCALLBACK(int) VBoxServiceVMInfoOption(const char **ppszShort, int argc, char **argv, int *pi)
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync /* no short options */;
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync/** @copydoc VBOXSERVICE::pfnInit */
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsyncstatic DECLCALLBACK(int) VBoxServiceVMInfoInit(void)
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * If not specified, find the right interval default.
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * Then create the event sem to block on.
87f68d052aecda193e89e8f41ec147606c7f4e0bvboxsync /* Get function pointers. */
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync g_pfnWTSGetActiveConsoleSessionId = (PFNWTSGETACTIVECONSOLESESSIONID)GetProcAddress(hKernel32, "WTSGetActiveConsoleSessionId");
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync rc = VbglR3GuestPropConnect(&g_VMInfoGuestPropSvcClientID);
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync VBoxServiceVerbose(3, "Property Service Client ID: %#x\n", g_VMInfoGuestPropSvcClientID);
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync VBoxServiceError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * Writes the properties that won't change while the service is running.
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * Errors are ignored.
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsyncstatic void VBoxServiceVMInfoWriteFixedProperties(void)
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * First get OS information that won't change.
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync int rc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szInfo, sizeof(szInfo));
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Product", "%s", szInfo);
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Product", "");
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szInfo, sizeof(szInfo));
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Release", "%s", szInfo);
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Release", "");
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync rc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szInfo, sizeof(szInfo));
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Version", "%s", szInfo);
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/Version", "");
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync rc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szInfo, sizeof(szInfo));
e961f5bfe1727c6816d3dad3805ebe21b6ba1c64vboxsync VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/ServicePack", "%s", szInfo);
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/ServicePack", "");
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * Retrieve version information about Guest Additions and installed files (components).
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync rc = VbglR3GetAdditionsVersion(&pszAddVer, &pszAddRev);
87f68d052aecda193e89e8f41ec147606c7f4e0bvboxsync /* Write information to host. */
87f68d052aecda193e89e8f41ec147606c7f4e0bvboxsync rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Version", "%s", pszAddVer);
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Revision", "%s", pszAddRev);
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync else /* If not found delete stale entries. */
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Version", NULL);
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/Revision", NULL);
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * Do windows specific properties.
87f68d052aecda193e89e8f41ec147606c7f4e0bvboxsync rc = VbglR3GetAdditionsInstallationPath(&pszInstDir);
ecd3f4c6194d87e9a53684b17e02ca96c7a7d1a7vboxsync rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/InstallDir", "%s", pszInstDir);
87f68d052aecda193e89e8f41ec147606c7f4e0bvboxsync else /* If not found delete stale entry. */
87f68d052aecda193e89e8f41ec147606c7f4e0bvboxsync rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestAdd/InstallDir", NULL);
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync VBoxServiceWinGetComponentVersions(g_VMInfoGuestPropSvcClientID);
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync /* return rc; */
87f68d052aecda193e89e8f41ec147606c7f4e0bvboxsync/** @copydoc VBOXSERVICE::pfnWorker */
87f68d052aecda193e89e8f41ec147606c7f4e0bvboxsyncDECLCALLBACK(int) VBoxServiceVMInfoWorker(bool volatile *pfShutdown)
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * Tell the control thread that it can continue
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync * spawning services.
5c2e23084fe3d3163d8f441b99cfd9d2f76b6b2avboxsync /* Required for network information (must be called per thread). */
unsigned cErrors = 0;
#ifdef RT_OS_WINDOWS
# ifndef TARGET_NT4
NTSTATUS r = 0;
if (r != STATUS_SUCCESS)
if (cUsersInList > 0)
cUsersInList++;
char *pszTemp;
# ifdef RT_OS_SOLARIS
if (rc != 0)
setutent();
if (cUsersInList > 0)
cUsersInList++;
endutent();
if (cUsersInList > 0)
VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", "%s", szUserList);
VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", NULL);
VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/LoggedInUsers", "%u", cUsersInList);
if (cUsersInList == 0)
VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers", "true");
else if (g_cVMInfoLoggedInUsers == 0)
VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers", "false");
int nNumInterfaces = 0;
#ifdef RT_OS_WINDOWS
unsigned long nBytesReturned = 0;
sizeof(InterfaceList),
0) == SOCKET_ERROR)
int iCurIface = 0;
for (int i = 0; i < nNumInterfaces; ++i)
#ifdef RT_OS_WINDOWS
VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
#ifdef RT_OS_WINDOWS
RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestInfo/Net/%d/V4/Broadcast", iCurIface);
VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
#ifdef RT_OS_WINDOWS
VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, szPropPath, "%s", inet_ntoa(pAddress->sin_addr));
iCurIface++;
if (sd >= 0)
#ifdef RT_OS_WINDOWS
if (*pfShutdown)
if (*pfShutdown)
#ifdef RT_OS_WINDOWS
WSACleanup();
return rc;
int rc;
rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/LoggedInUsersList", NULL);
rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/LoggedInUsers", "%d", 0);
if (g_cVMInfoLoggedInUsers > 0)
VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/OS/NoLoggedInUsers", "true");
rc = VBoxServiceWritePropF(g_VMInfoGuestPropSvcClientID, "/VirtualBox/GuestInfo/Net/Count", "%d", 0);