VBoxGuestR3LibAdditions.cpp revision ae913ac9ffc89ee2a83992937b57f30e983185aa
0N/A/* $Id$ */
1879N/A/** @file
0N/A * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Additions Info.
0N/A */
0N/A
0N/A/*
0N/A * Copyright (C) 2007-2011 Oracle Corporation
0N/A *
0N/A * This file is part of VirtualBox Open Source Edition (OSE), as
0N/A * available from http://www.virtualbox.org. This file is free software;
0N/A * you can redistribute it and/or modify it under the terms of the GNU
0N/A * General Public License (GPL) as published by the Free Software
0N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
0N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
0N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
0N/A *
0N/A * The contents of this file may alternatively be used under the terms
0N/A * of the Common Development and Distribution License Version 1.0
1472N/A * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
1472N/A * VirtualBox OSE distribution, in which case the provisions of the
1472N/A * CDDL are applicable instead of those of the GPL.
0N/A *
0N/A * You may elect to license modified versions of this file under the
0N/A * terms and conditions of either the GPL or the CDDL or both.
1879N/A */
1879N/A
1879N/A
1879N/A/*******************************************************************************
1879N/A* Header Files *
1879N/A*******************************************************************************/
1879N/A#include <iprt/mem.h>
1879N/A#include <iprt/string.h>
0N/A#include <VBox/log.h>
0N/A#include <VBox/version.h>
0N/A#include "VBGLR3Internal.h"
0N/A
0N/A
0N/A/**
0N/A * Fallback for VbglR3GetAdditionsVersion.
0N/A */
0N/Astatic int vbglR3GetAdditionsCompileTimeVersion(char **ppszVer, char **ppszVerEx, char **ppszRev)
0N/A{
0N/A int rc = VINF_SUCCESS;
0N/A if (ppszVer)
0N/A rc = RTStrDupEx(ppszVer, VBOX_VERSION_STRING_RAW);
0N/A if (RT_SUCCESS(rc))
0N/A {
0N/A if (ppszVerEx)
0N/A rc = RTStrDupEx(ppszVerEx, VBOX_VERSION_STRING);
0N/A if (RT_SUCCESS(rc))
0N/A {
0N/A if (ppszRev)
0N/A {
0N/A#if 0
0N/A char szRev[64];
0N/A RTStrPrintf(szRev, sizeof(szRev), "%d", VBOX_SVN_REV);
0N/A rc = RTStrDupEx(ppszRev, szRev);
1100N/A#else
0N/A rc = RTStrDupEx(ppszRev, RT_XSTR(VBOX_SVN_REV));
0N/A#endif
0N/A }
0N/A if (RT_SUCCESS(rc))
0N/A return VINF_SUCCESS;
0N/A
168N/A /* bail out: */
168N/A }
0N/A if (ppszVerEx)
0N/A {
0N/A RTStrFree(*ppszVerEx);
0N/A *ppszVerEx = NULL;
0N/A }
0N/A }
0N/A if (ppszVer)
0N/A {
0N/A RTStrFree(*ppszVer);
0N/A *ppszVer = NULL;
0N/A }
0N/A return rc;
0N/A}
0N/A
0N/A#ifdef RT_OS_WINDOWS
0N/A
0N/A/**
1915N/A * Looks up the storage path handle (registry).
1915N/A *
0N/A * @returns IPRT status value
0N/A * @param hKey Receives pointer of allocated version string. NULL is
0N/A * accepted. The returned pointer must be closed by
0N/A * vbglR3CloseAdditionsWinStoragePath().
0N/A */
0N/Astatic int vbglR3QueryAdditionsWinStoragePath(PHKEY phKey)
0N/A{
0N/A /*
0N/A * Try get the *installed* version first.
0N/A */
0N/A LONG r;
0N/A
0N/A /* Check the built in vendor path first. */
0N/A char szPath[255];
0N/A RTStrPrintf(szPath, sizeof(szPath), "SOFTWARE\\%s\\VirtualBox Guest Additions", VBOX_VENDOR_SHORT);
0N/A r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szPath, 0, KEY_READ, phKey);
0N/A# ifdef RT_ARCH_AMD64
0N/A if (r != ERROR_SUCCESS)
0N/A {
0N/A /* Check Wow6432Node. */
0N/A RTStrPrintf(szPath, sizeof(szPath), "SOFTWARE\\Wow6432Node\\%s\\VirtualBox Guest Additions", VBOX_VENDOR_SHORT);
33N/A r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szPath, 0, KEY_READ, phKey);
0N/A }
0N/A# endif
0N/A
0N/A /* Check the "Sun" path first. */
0N/A if (r != ERROR_SUCCESS)
0N/A {
0N/A r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
0N/A# ifdef RT_ARCH_AMD64
0N/A if (r != ERROR_SUCCESS)
0N/A {
0N/A /* Check Wow6432Node (for new entries). */
0N/A r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
0N/A }
0N/A# endif
63N/A }
0N/A
0N/A /* Still no luck? Then try the old "Sun xVM" paths ... */
0N/A if (r != ERROR_SUCCESS)
0N/A {
0N/A r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
0N/A# ifdef RT_ARCH_AMD64
0N/A if (r != ERROR_SUCCESS)
0N/A {
0N/A /* Check Wow6432Node (for new entries). */
0N/A r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
0N/A }
0N/A# endif
0N/A }
0N/A return RTErrConvertFromWin32(r);
0N/A}
0N/A
0N/A
0N/A/**
0N/A * Closes the storage path handle (registry).
0N/A *
0N/A * @returns IPRT status value
0N/A * @param hKey Handle to close, retrieved by
0N/A * vbglR3QueryAdditionsWinStoragePath().
0N/A */
0N/Astatic int vbglR3CloseAdditionsWinStoragePath(HKEY hKey)
0N/A{
0N/A return RTErrConvertFromWin32(RegCloseKey(hKey));
0N/A}
0N/A
0N/A#endif /* RT_OS_WINDOWS */
0N/A
0N/A/**
0N/A * Reports the Guest Additions status of a certain facility to the host.
0N/A *
0N/A * @returns IPRT status value
0N/A * @param enmFacility The facility to report the status on.
0N/A * @param enmStatus The new status of the facility.
0N/A * @param fReserved Reserved for future use (what?).
0N/A */
0N/AVBGLR3DECL(int) VbglR3ReportAdditionsStatus(VBoxGuestFacilityType enmFacility,
0N/A VBoxGuestFacilityStatus enmStatusCurrent,
0N/A uint32_t fReserved)
0N/A{
0N/A VMMDevReportGuestStatus Report;
0N/A RT_ZERO(Report);
0N/A int rc = vmmdevInitRequest((VMMDevRequestHeader*)&Report, VMMDevReq_ReportGuestStatus);
0N/A if (RT_SUCCESS(rc))
0N/A {
0N/A Report.guestStatus.facility = enmFacility;
0N/A Report.guestStatus.status = enmStatusCurrent;
0N/A Report.guestStatus.flags = fReserved;
0N/A
0N/A rc = vbglR3GRPerform(&Report.header);
0N/A }
0N/A return rc;
0N/A}
0N/A
0N/A
0N/Aint vbglR3QueryRegistryString(HKEY hKey, const char *pszValName, char *pszBuffer, size_t cchBuffer)
0N/A{
0N/A AssertReturn(pszValName, VERR_INVALID_PARAMETER);
0N/A AssertReturn(pszBuffer, VERR_INVALID_POINTER);
0N/A AssertReturn(cchBuffer, VERR_INVALID_PARAMETER);
0N/A
0N/A int rc = VINF_SUCCESS;
0N/A
0N/A DWORD dwType;
0N/A DWORD dwSize = (DWORD)cchBuffer;
0N/A LONG lRet = RegQueryValueEx(hKey, pszValName, NULL, &dwType, (BYTE*)(LPCTSTR)pszBuffer, &dwSize);
0N/A if (lRet == ERROR_SUCCESS)
0N/A {
0N/A rc = dwType == REG_SZ
0N/A ? VINF_SUCCESS : VERR_INVALID_PARAMETER;
0N/A }
0N/A else
0N/A rc = RTErrConvertFromWin32(lRet);
0N/A return rc;
0N/A}
0N/A
0N/A
0N/A/**
0N/A * Retrieves the installed Guest Additions version and/or revision.
0N/A *
0N/A * @returns IPRT status value
0N/A * @param ppszVer Receives pointer of allocated raw version string
0N/A * (major.minor.build). NULL is accepted. The returned
0N/A * pointer must be freed using RTStrFree().*
0N/A * @param ppszVerExt Receives pointer of allocated full version string
0N/A * (raw version + vendor suffix(es)). NULL is
0N/A * accepted. The returned pointer must be freed using
0N/A * RTStrFree().
0N/A * @param ppszRev Receives pointer of allocated revision string. NULL is
0N/A * accepted. The returned pointer must be freed using
0N/A * RTStrFree().
0N/A */
0N/AVBGLR3DECL(int) VbglR3GetAdditionsVersion(char **ppszVer, char **ppszVerExt, char **ppszRev)
0N/A{
0N/A /*
0N/A * Zap the return value up front.
0N/A */
0N/A if (ppszVer)
0N/A *ppszVer = NULL;
0N/A if (ppszVerExt)
0N/A *ppszVerExt = NULL;
0N/A if (ppszRev)
0N/A *ppszRev = NULL;
0N/A
0N/A
0N/A#ifdef RT_OS_WINDOWS
0N/A HKEY hKey;
0N/A int rc = vbglR3QueryAdditionsWinStoragePath(&hKey);
0N/A if (RT_SUCCESS(rc))
0N/A {
0N/A /*
0N/A * Version.
0N/A */
0N/A char szTemp[32];
0N/A if (ppszVer)
0N/A {
0N/A rc = vbglR3QueryRegistryString(hKey, "Version", szTemp, sizeof(szTemp));
0N/A if (RT_SUCCESS(rc))
0N/A rc = RTStrDupEx(ppszVer, szTemp);
0N/A }
0N/A
0N/A if ( RT_SUCCESS(rc)
0N/A && ppszVerExt)
0N/A {
0N/A rc = vbglR3QueryRegistryString(hKey, "VersionExt", szTemp, sizeof(szTemp));
0N/A if (RT_SUCCESS(rc))
0N/A rc = RTStrDupEx(ppszVerExt, szTemp);
0N/A }
0N/A
0N/A /*
0N/A * Revision.
0N/A */
0N/A if ( RT_SUCCESS(rc)
0N/A && ppszRev)
0N/A {
0N/A rc = vbglR3QueryRegistryString(hKey, "Revision", szTemp, sizeof(szTemp));
0N/A if (RT_SUCCESS(rc))
0N/A rc = RTStrDupEx(ppszRev, szTemp);
0N/A }
0N/A
0N/A int rc2 = vbglR3CloseAdditionsWinStoragePath(hKey);
0N/A if (RT_SUCCESS(rc))
0N/A rc = rc2;
0N/A else
0N/A {
605N/A /* Clean up allocated strings on error. */
0N/A if (*ppszVer)
0N/A RTStrFree(*ppszVer);
0N/A if (*ppszVerExt)
0N/A RTStrFree(*ppszVerExt);
0N/A if (*ppszRev)
0N/A RTStrFree(*ppszRev);
0N/A }
0N/A }
0N/A else
0N/A {
0N/A /*
0N/A * No registry entries found, return the version string compiled
0N/A * into this binary.
0N/A */
0N/A rc = vbglR3GetAdditionsCompileTimeVersion(ppszVer, ppszVerExt, ppszRev);
0N/A }
0N/A return rc;
0N/A
0N/A#else /* !RT_OS_WINDOWS */
0N/A /*
0N/A * On non-Windows platforms just return the compile-time version string.
0N/A */
0N/A return vbglR3GetAdditionsCompileTimeVersion(ppszVer, ppszVerExt, ppszRev);
0N/A#endif /* !RT_OS_WINDOWS */
0N/A}
0N/A
0N/A
0N/A/**
0N/A * Retrieves the installation path of Guest Additions.
0N/A *
0N/A * @returns IPRT status value
0N/A * @param ppszPath Receives pointer of allocated installation path string.
0N/A * The returned pointer must be freed using
0N/A * RTStrFree().
0N/A */
0N/AVBGLR3DECL(int) VbglR3GetAdditionsInstallationPath(char **ppszPath)
0N/A{
0N/A int rc;
0N/A#ifdef RT_OS_WINDOWS
0N/A HKEY hKey;
0N/A rc = vbglR3QueryAdditionsWinStoragePath(&hKey);
0N/A if (RT_SUCCESS(rc))
0N/A {
0N/A /* Installation directory. */
0N/A DWORD dwType;
0N/A DWORD dwSize = _MAX_PATH * sizeof(char);
0N/A char *pszTmp = (char*)RTMemAlloc(dwSize + 1);
0N/A if (pszTmp)
0N/A {
0N/A LONG l = RegQueryValueEx(hKey, "InstallDir", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
0N/A if ((l != ERROR_SUCCESS) && (l != ERROR_FILE_NOT_FOUND))
0N/A {
0N/A rc = RTErrConvertFromNtStatus(l);
0N/A }
0N/A else
0N/A {
0N/A if (dwType == REG_SZ)
0N/A rc = RTStrDupEx(ppszPath, pszTmp);
0N/A else
0N/A rc = VERR_INVALID_PARAMETER;
0N/A if (RT_SUCCESS(rc))
0N/A {
0N/A /* Flip slashes. */
0N/A for (char *pszTmp2 = ppszPath[0]; *pszTmp2; ++pszTmp2)
0N/A if (*pszTmp2 == '\\')
0N/A *pszTmp2 = '/';
0N/A }
0N/A }
0N/A RTMemFree(pszTmp);
0N/A }
0N/A else
0N/A rc = VERR_NO_MEMORY;
0N/A rc = vbglR3CloseAdditionsWinStoragePath(hKey);
0N/A }
0N/A#else
0N/A /** @todo implement me */
0N/A rc = VERR_NOT_IMPLEMENTED;
0N/A#endif
0N/A return rc;
0N/A}
0N/A
0N/A