SUPLib-win.cpp revision 304e26e9a9c1422dbc299ed139eceacca271a9dd
/* $Id$ */
/** @file
* VirtualBox Support Library - Windows NT specific parts.
*/
/*
* Copyright (C) 2006-2012 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.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_SUP
#ifdef IN_SUP_HARDENED_R3
# define LOG_DISABLED
/** @todo RTLOGREL_DISABLED */
#endif
#define USE_NT_DEVICE_IO_CONTROL_FILE
#else
# include <Windows.h>
#endif
#include "../SUPLibInternal.h"
#include "../SUPDrvIOC.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The support service name. */
#define SERVICE_NAME "VBoxDrv"
/** Win32 Device name - system. */
#define DEVICE_NAME_SYS "\\\\.\\VBoxDrv"
/** Win32 Device name - user. */
#define DEVICE_NAME_USR "\\\\.\\VBoxDrvU"
/** NT Device name. */
#define DEVICE_NAME_NT L"\\Device\\VBoxDrv"
/** Win32 Symlink name. */
#define DEVICE_NAME_DOS L"\\DosDevices\\VBoxDrv"
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int suplibOsCreateService(void);
//unused: static int suplibOsUpdateService(void);
static int suplibOsDeleteService(void);
static int suplibOsStartService(void);
static int suplibOsStopService(void);
#else
static int suplibConvertWin32Err(int);
#endif
{
/*
* Nothing to do if pre-inited.
*/
if (fPreInited)
return VINF_SUCCESS;
/*
* Try open the device.
*/
NULL,
NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
#ifndef IN_SUP_HARDENED_R3
/*
* Try start the service and retry opening it.
*/
NULL,
NULL);
if (hDevice == INVALID_HANDLE_VALUE)
#endif /* !IN_SUP_HARDENED_R3 */
{
int rc = GetLastError();
switch (rc)
{
/** @todo someone must test what is actually returned. */
case ERROR_DEV_NOT_EXIST:
case ERROR_BAD_DEVICE:
case ERROR_DEVICE_REMOVED:
return VERR_VM_DRIVER_LOAD_ERROR;
case ERROR_PATH_NOT_FOUND:
case ERROR_FILE_NOT_FOUND:
return VERR_VM_DRIVER_NOT_INSTALLED;
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
return VERR_VM_DRIVER_NOT_ACCESSIBLE;
default:
return VERR_VM_DRIVER_OPEN_ERROR;
}
return -1 /** @todo define proper error codes for suplibOsInit failure. */;
}
}
/*
* We're done.
*/
return VINF_SUCCESS;
}
#ifndef IN_SUP_HARDENED_R3
int suplibOsInstall(void)
{
return suplibOsCreateService();
}
int suplibOsUninstall(void)
{
int rc = suplibOsStopService();
if (!rc)
rc = suplibOsDeleteService();
return rc;
}
/**
* Creates the service.
*
* @returns 0 on success.
* @returns -1 on failure.
*/
static int suplibOsCreateService(void)
{
/*
* Assume it didn't exist, so we'll create the service.
*/
if (hSMgrCreate)
{
char szDriver[RTPATH_MAX];
if (RT_SUCCESS(rc))
{
"VBox Support Driver",
return hService ? 0 : -1;
}
return rc;
}
return -1;
}
/**
* Stops a possibly running service.
*
* @returns 0 on success.
* @returns -1 on failure.
*/
static int suplibOsStopService(void)
{
/*
* Assume it didn't exist, so we'll create the service.
*/
int rc = -1;
if (hSMgr)
{
if (hService)
{
/*
* Stop the service.
*/
rc = 0;
{
int iWait = 100;
{
Sleep(100);
}
rc = 0;
else
}
else
{
AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", LastError, Status.dwCurrentState));
}
}
else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
rc = 0;
else
{
}
}
return rc;
}
/**
* Deletes the service.
*
* @returns 0 on success.
* @returns -1 on failure.
*/
int suplibOsDeleteService(void)
{
/*
* Assume it didn't exist, so we'll create the service.
*/
int rc = -1;
if (hSMgr)
{
if (hService)
{
/*
* Delete the service.
*/
if (DeleteService(hService))
rc = 0;
else
{
}
}
else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
rc = 0;
else
{
}
}
return rc;
}
#if 0
/**
* Creates the service.
*
* @returns 0 on success.
* @returns -1 on failure.
*/
static int suplibOsUpdateService(void)
{
/*
* Assume it didn't exist, so we'll create the service.
*/
if (hSMgr)
{
if (hService)
{
char szDriver[RTPATH_MAX];
if (RT_SUCCESS(rc))
{
{
return 0;
}
else
{
}
}
}
else
{
}
}
return -1;
}
#endif
/**
* Attempts to start the service, creating it if necessary.
*
* @returns 0 on success.
* @returns -1 on failure.
* @param fRetry Indicates retry call.
*/
static int suplibOsStartService(void)
{
/*
* Check if the driver service is there.
*/
{
AssertMsgFailed(("couldn't open service manager in SERVICE_QUERY_CONFIG | SERVICE_QUERY_STATUS mode!\n"));
return -1;
}
/*
* Try open our service to check it's status.
*/
if (!hService)
{
/*
* Create the service.
*/
int rc = suplibOsCreateService();
if (rc)
return rc;
/*
* Try open the service.
*/
}
/*
* Check if open and on demand create succeeded.
*/
int rc = -1;
if (hService)
{
/*
* Query service status to see if we need to start it or not.
*/
{
/*
* Start it.
*/
}
/*
* Wait for the service to finish starting.
* We'll wait for 10 seconds then we'll give up.
*/
{
int iWait;
{
Sleep(100);
}
("Failed to start. LastError=%Rwa iWait=%d status=%d\n",
}
rc = 0;
/*
* Close open handles.
*/
}
else
{
}
if (!CloseServiceHandle(hSMgr))
AssertFailed();
return rc;
}
{
/*
* Check if we're inited at all.
*/
{
AssertFailed();
}
return VINF_SUCCESS;
}
{
/*
* Issue the device I/O control.
*/
# ifdef USE_NT_DEVICE_IO_CONTROL_FILE
Ios.Information = 0;
NTSTATUS rcNt = NtDeviceIoControlFile((HANDLE)pThis->hDevice, NULL /*hEvent*/, NULL /*pfnApc*/, NULL /*pvApcCtx*/, &Ios,
if (NT_SUCCESS(rcNt))
{
return VINF_SUCCESS;
}
return suplibConvertNtStatus(rcNt);
# else
if (DeviceIoControl((HANDLE)pThis->hDevice, uFunction, pvReq, pHdr->cbIn, pvReq, cbReturned, &cbReturned, NULL))
return 0;
return suplibConvertWin32Err(GetLastError());
# endif
}
{
/*
* Issue device I/O control.
*/
# ifdef USE_NT_DEVICE_IO_CONTROL_FILE
Ios.Information = 0;
NTSTATUS rcNt = NtDeviceIoControlFile((HANDLE)pThis->hDevice, NULL /*hEvent*/, NULL /*pfnApc*/, NULL /*pvApcCtx*/, &Ios,
if (NT_SUCCESS(rcNt))
{
return VINF_SUCCESS;
}
return suplibConvertNtStatus(rcNt);
# else
DWORD cbReturned = 0;
if (DeviceIoControl((HANDLE)pThis->hDevice, uFunction, NULL, 0, (LPVOID)idCpu, 0, &cbReturned, NULL))
return VINF_SUCCESS;
return suplibConvertWin32Err(GetLastError());
# endif
}
{
if (*ppvPages)
return VINF_SUCCESS;
return RTErrConvertFromWin32(GetLastError());
}
{
return VINF_SUCCESS;
return RTErrConvertFromWin32(GetLastError());
}
# ifndef USE_NT_DEVICE_IO_CONTROL_FILE
/**
* Converts a supdrv win32 error code to an IPRT status code.
*
* @returns corresponding IPRT error code.
* @param rc Win32 error code.
*/
static int suplibConvertWin32Err(int rc)
{
/* Conversion program (link with ntdll.lib from ddk):
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <ntstatus.h>
#include <winternl.h>
#include <stdio.h>
int main()
{
#define CONVERT(a) printf(#a " %#x -> %d\n", a, RtlNtStatusToDosError((a)))
CONVERT(STATUS_SUCCESS);
CONVERT(STATUS_NOT_SUPPORTED);
CONVERT(STATUS_INVALID_PARAMETER);
CONVERT(STATUS_UNKNOWN_REVISION);
CONVERT(STATUS_INVALID_HANDLE);
CONVERT(STATUS_INVALID_ADDRESS);
CONVERT(STATUS_NOT_LOCKED);
CONVERT(STATUS_IMAGE_ALREADY_LOADED);
CONVERT(STATUS_ACCESS_DENIED);
CONVERT(STATUS_REVISION_MISMATCH);
return 0;
}
*/
switch (rc)
{
//case 0: return STATUS_SUCCESS;
case 0: return VINF_SUCCESS;
case ERROR_NOT_SUPPORTED: return VERR_GENERAL_FAILURE;
case ERROR_INVALID_PARAMETER: return VERR_INVALID_PARAMETER;
case ERROR_UNKNOWN_REVISION: return VERR_INVALID_MAGIC;
case ERROR_INVALID_HANDLE: return VERR_INVALID_HANDLE;
case ERROR_UNEXP_NET_ERR: return VERR_INVALID_POINTER;
case ERROR_NOT_LOCKED: return VERR_LOCK_FAILED;
case ERROR_SERVICE_ALREADY_RUNNING: return VERR_ALREADY_LOADED;
case ERROR_ACCESS_DENIED: return VERR_PERMISSION_DENIED;
case ERROR_REVISION_MISMATCH: return VERR_VERSION_MISMATCH;
}
/* fall back on the default conversion. */
return RTErrConvertFromWin32(rc);
}
# else
/**
* Reverse of VBoxDrvNtErr2NtStatus
* returns VBox status code.
* @param rcNt NT status code.
*/
{
switch (rcNt)
{
case STATUS_SUCCESS: return VINF_SUCCESS;
case STATUS_NOT_SUPPORTED: return VERR_GENERAL_FAILURE;
case STATUS_INVALID_PARAMETER: return VERR_INVALID_PARAMETER;
case STATUS_UNKNOWN_REVISION: return VERR_INVALID_MAGIC;
case STATUS_INVALID_HANDLE: return VERR_INVALID_HANDLE;
case STATUS_INVALID_ADDRESS: return VERR_INVALID_POINTER;
case STATUS_NOT_LOCKED: return VERR_LOCK_FAILED;
case STATUS_IMAGE_ALREADY_LOADED: return VERR_ALREADY_LOADED;
case STATUS_ACCESS_DENIED: return VERR_PERMISSION_DENIED;
case STATUS_REVISION_MISMATCH: return VERR_VERSION_MISMATCH;
}
/* Fall back on IPRT for the rest. */
return RTErrConvertFromNtStatus(rcNt);
}
# endif
#endif /* !IN_SUP_HARDENED_R3 */