VBoxServiceVMInfo-win.cpp revision fba0644e8c6d87f22abcc10570391a89a9633aba
/* $Id$ */
/** @file
* VBoxService - Virtual Machine Information for the Host, Windows specifics.
*/
/*
* Copyright (C) 2009-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#endif
#include <Windows.h>
#include <wtsapi32.h> /* For WTS* calls. */
#include <psapi.h> /* EnumProcesses. */
#include <Ntsecapi.h> /* Needed for process security information. */
#include <iprt/semaphore.h>
#include <VBox/VBoxGuestLib.h>
#include "VBoxServiceInternal.h"
#include "VBoxServiceUtils.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Structure for storing the looked up user information. */
typedef struct
{
/** Structure for the file information lookup. */
typedef struct
{
char *pszFilePath;
char *pszFileName;
/** Structure for process information lookup. */
typedef struct
{
/*******************************************************************************
* Prototypes
*******************************************************************************/
bool VBoxServiceVMInfoWinSessionHasProcesses(PLUID pSession, VBOXSERVICEVMINFOPROC const *paProcs, DWORD cProcs);
#ifndef TARGET_NT4
/**
* Fills in more data for a process.
*
* @returns VBox status code.
* @param pProc The process structure to fill data into.
* @param tkClass The kind of token information to get.
*/
{
if (h == NULL)
return RTErrConvertFromWin32(GetLastError());
int rc = VERR_NO_MEMORY;
{
void *pvTokenInfo = NULL;
switch (tkClass)
{
case TokenStatistics:
dwTokenInfoSize = sizeof(TOKEN_STATISTICS);
break;
/** @todo Implement more token classes here. */
default:
break;
}
if (pvTokenInfo)
{
{
switch (tkClass)
{
case TokenStatistics:
{
/** @todo Add more information of TOKEN_STATISTICS as needed. */
break;
}
default:
/* Should never get here! */
break;
}
rc = VINF_SUCCESS;
}
else
}
}
else
CloseHandle(h);
return rc;
}
/**
* Enumerate all the processes in the system and get the logon user IDs for
* them.
*
* @returns VBox status code.
* @param ppaProcs Where to return the process snapshot. This must be
* freed by calling VBoxServiceVMInfoWinProcessesFree.
*
* @param pcProcs Where to store the returned process count.
*/
{
/*
* Call EnumProcesses with an increasingly larger buffer until it all fits
* or we think something is screwed up.
*/
int rc = VINF_SUCCESS;
do
{
/* Allocate / grow the buffer first. */
cProcesses *= 2;
if (!pvNew)
{
rc = VERR_NO_MEMORY;
break;
}
/* Query the processes. Not the cbRet == buffer size means there could be more work to be done. */
{
break;
}
{
break;
}
} while (cProcesses <= 32768); /* Should be enough; see: http://blogs.technet.com/markrussinovich/archive/2009/07/08/3261309.aspx */
if (RT_SUCCESS(rc))
{
/*
* Allocate out process structures and fill data into them.
* We currently only try lookup their LUID's.
*/
if (paProcs)
{
for (DWORD i = 0; i < cProcesses; i++)
{
if (RT_FAILURE(rc))
{
Windows, we should not consider to be this an error here. */
rc = VINF_SUCCESS;
}
}
/* Save number of processes */
if (RT_SUCCESS(rc))
{
*pcProcs = cProcesses;
}
else
}
else
rc = VERR_NO_MEMORY;
}
return rc;
}
/**
* Frees the process structures returned by
* VBoxServiceVMInfoWinProcessesEnumerate() before.
*
* @param paProcs What
*/
{
}
/**
* Determins whether the specified session has processes on the system.
*
* @returns true if it has, false if it doesn't.
* @param pSession The session.
* @param paProcs The process snapshot.
* @param cProcs The number of processes in the snaphot.
*/
bool VBoxServiceVMInfoWinSessionHasProcesses(PLUID pSession, VBOXSERVICEVMINFOPROC const *paProcs, DWORD cProcs)
{
if (!cProcs) /* To be on the safe side. */
return false;
if (rcNt != STATUS_SUCCESS)
{
return false;
}
AssertPtrReturn(pSessionData, false);
/*
* session. So check if we have some processes bound to it by comparing the
* session <-> process LUIDs.
*/
{
/*VBoxServiceVerbose(3, "%ld:%ld <-> %ld:%ld\n",
paProcs[i].luid.HighPart, paProcs[i].luid.LowPart,
pSessionData->LogonId.HighPart, pSessionData->LogonId.LowPart);*/
{
return true;
}
}
return false;
}
/**
* Save and noisy string copy.
*
* @param pwszDst Destination buffer.
* @param cbDst Size in bytes - not WCHAR count!
* @param pSrc Source string.
* @param pszWhat What this is. For the log.
*/
static void VBoxServiceVMInfoWinSafeCopy(PWCHAR pwszDst, size_t cbDst, LSA_UNICODE_STRING const *pSrc, const char *pszWhat)
{
{
VBoxServiceVerbose(0, "%s is too long - %u bytes, buffer %u bytes! It will be truncated.\n",
}
if (cbCopy)
}
/**
* Detects whether a user is logged on.
*
* @returns true if logged in, false if not (or error).
* @param a_pUserInfo Where to return the user information.
* @param a_pSession The session to check.
*/
{
if (!a_pSession)
return false;
if (rcNt != STATUS_SUCCESS)
{
/* Skip session data which is not valid anymore because it may have been
* already terminated. */
if (ulError != ERROR_NO_SUCH_LOGON_SESSION)
if (pSessionData)
return false;
}
if (!pSessionData)
{
VBoxServiceError("VMInfo/Users: Invalid logon session data!\n");
return false;
}
/*
* Only handle users which can login interactively or logged in
* remotely over native RDP.
*/
bool fFoundUser = false;
{
/*
* Copy out relevant data.
*/
VBoxServiceVMInfoWinSafeCopy(a_pUserInfo->wszAuthenticationPackage, sizeof(a_pUserInfo->wszAuthenticationPackage),
if (!LookupAccountSid(NULL,
&enmOwnerType))
{
VBoxServiceError("VMInfo/Users: Failed looking up account info for user '%ls': %ld!\n",
}
else
{
{
VBoxServiceVerbose(3, "VMInfo/Users: Account User=%ls, Session=%ld, LUID=%ld,%ld, AuthPkg=%ls, Domain=%ls\n",
/* Detect RDP sessions as well. */
int iState = 0;
&pBuffer,
&cbRet))
{
if (cbRet)
{
/** @todo On Vista and W2K, always "old" user name are still
* there. Filter out the old one! */
fFoundUser = true;
}
if (pBuffer)
}
else
{
/*
* Terminal services don't run (for example in W2K,
* nothing to worry about ...). ... or is on the Vista
* fast user switching page!
*/
fFoundUser = true;
}
}
}
}
return fFoundUser;
}
/**
* Retrieves the currently logged in users and stores their names along with the
* user count.
*
* @returns VBox status code.
* @param ppszUserList Where to store the user list (separated by commas).
* Must be freed with RTStrFree().
* @param pcUsersInList Where to store the number of users in the list.
*/
{
/* This function can report stale or orphaned interactive logon sessions
of already logged off users (especially in Windows 2000). */
if (rcNt != STATUS_SUCCESS)
{
/* If we're about to shutdown when we were in the middle of enumerating the logon
sessions, skip the error to not confuse the user with an unnecessary log message. */
if (rcWin == ERROR_SHUTDOWN_IN_PROGRESS)
{
}
else
return RTErrConvertFromWin32(rcWin);
}
if (RT_SUCCESS(rc))
{
*pcUsersInList = 0;
{
{
if (*pcUsersInList > 0)
{
}
*pcUsersInList += 1;
char *pszTemp;
if (RT_SUCCESS(rc2))
{
}
else
}
}
}
return rc;
}
#endif /* TARGET_NT4 */
{
int rc;
/* ASSUME: szSysDir and szWinDir and derivatives are always ASCII compatible. */
#ifdef RT_ARCH_AMD64
#endif
/* The file information table. */
#ifndef TARGET_NT4
const VBOXSERVICEVMINFOFILE aVBoxFiles[] =
{
{ szSysDir, "VBoxControl.exe" },
{ szSysDir, "VBoxHook.dll" },
{ szSysDir, "VBoxDisp.dll" },
{ szSysDir, "VBoxMRXNP.dll" },
{ szSysDir, "VBoxService.exe" },
{ szSysDir, "VBoxTray.exe" },
{ szSysDir, "VBoxGINA.dll" },
{ szSysDir, "VBoxCredProv.dll" },
/* On 64-bit we don't yet have the OpenGL DLLs in native format.
So just enumerate the 32-bit files in the SYSWOW directory. */
# ifdef RT_ARCH_AMD64
{ szSysWowDir, "VBoxOGLarrayspu.dll" },
{ szSysWowDir, "VBoxOGLcrutil.dll" },
{ szSysWowDir, "VBoxOGLerrorspu.dll" },
{ szSysWowDir, "VBoxOGLpackspu.dll" },
{ szSysWowDir, "VBoxOGLpassthroughspu.dll" },
{ szSysWowDir, "VBoxOGLfeedbackspu.dll" },
{ szSysWowDir, "VBoxOGL.dll" },
# else /* !RT_ARCH_AMD64 */
{ szSysDir, "VBoxOGLarrayspu.dll" },
{ szSysDir, "VBoxOGLcrutil.dll" },
{ szSysDir, "VBoxOGLerrorspu.dll" },
{ szSysDir, "VBoxOGLpackspu.dll" },
{ szSysDir, "VBoxOGLpassthroughspu.dll" },
{ szSysDir, "VBoxOGLfeedbackspu.dll" },
{ szSysDir, "VBoxOGL.dll" },
# endif /* !RT_ARCH_AMD64 */
{ szDriversDir, "VBoxGuest.sys" },
{ szDriversDir, "VBoxMouse.sys" },
{ szDriversDir, "VBoxSF.sys" },
{ szDriversDir, "VBoxVideo.sys" },
};
#else /* TARGET_NT4 */
const VBOXSERVICEVMINFOFILE aVBoxFiles[] =
{
{ szSysDir, "VBoxControl.exe" },
{ szSysDir, "VBoxHook.dll" },
{ szSysDir, "VBoxDisp.dll" },
{ szSysDir, "VBoxServiceNT.exe" },
{ szSysDir, "VBoxTray.exe" },
{ szDriversDir, "VBoxGuestNT.sys" },
{ szDriversDir, "VBoxMouseNT.sys" },
{ szDriversDir, "VBoxVideo.sys" },
};
#endif /* TARGET_NT4 */
for (unsigned i = 0; i < RT_ELEMENTS(aVBoxFiles); i++)
{
char szVer[128];
VBoxServiceGetFileVersionString(aVBoxFiles[i].pszFilePath, aVBoxFiles[i].pszFileName, szVer, sizeof(szVer));
char szPropPath[256];
RTStrPrintf(szPropPath, sizeof(szPropPath), "/VirtualBox/GuestAdd/Components/%s", aVBoxFiles[i].pszFileName);
}
return VINF_SUCCESS;
}