VBoxGuestR3Lib.cpp revision f6b7aa2d424718f1bf8ca45a37b219b986f1024b
/* $Id$ */
/** @file
* VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Core.
*/
/*
* Copyright (C) 2007 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#if defined(RT_OS_WINDOWS)
# include <Windows.h>
# define INCL_BASE
# define INCL_ERRORS
# include <os2.h>
# include <errno.h>
# include <unistd.h>
#endif
#include <VBox/VBoxGuest.h>
#include "VBGLR3Internal.h"
#ifdef VBOX_VBGLR3_XFREE86
/* Rather than try to resolve all the header file conflicts, I will just
prototype what we need here. */
# define XF86_O_RDWR 0x0002
typedef void *pointer;
extern "C" int xf86open(const char*, int,...);
extern "C" int xf86close(int);
#endif
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The VBoxGuest device handle. */
#ifdef VBOX_VBGLR3_XFREE86
static int g_File = -1;
#elif defined(RT_OS_WINDOWS)
#else
#endif
/** User counter.
* A counter of the number of times the library has been initialised, for use with
* X.org drivers, where the library may be shared by multiple independant modules
* inside a single process space.
*/
VBGLR3DECL(int) VbglR3Init(void)
{
#ifndef VBOX_VBGLR3_XFREE86
#endif
if (cInits > 1)
{
/*
* This will fail if two (or more) threads race each other calling VbglR3Init.
* However it will work fine for single threaded or otherwise serialized
* processed calling us more than once.
*/
#ifdef RT_OS_WINDOWS
if (g_hFile == INVALID_HANDLE_VALUE)
#elif !defined (VBOX_VBGLR3_XFREE86)
if (g_File == NIL_RTFILE)
#else
if (g_File == -1)
#endif
return VERR_INTERNAL_ERROR;
return VINF_SUCCESS;
}
#if defined(RT_OS_WINDOWS)
if (g_hFile != INVALID_HANDLE_VALUE)
#elif !defined(VBOX_VBGLR3_XFREE86)
if (g_File != NIL_RTFILE)
#else
if (g_File != -1)
#endif
return VERR_INTERNAL_ERROR;
#if defined(RT_OS_WINDOWS)
/*
* Have to use CreateFile here as we want to specify FILE_FLAG_OVERLAPPED
*/
NULL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return VERR_OPEN_FAILED;
/*
* We might wish to compile this with Watcom, so stick to
* the OS/2 APIs all the way. And in any case we have to use
* DosDevIOCtl for the requests, why not use Dos* for everything.
*/
NULL);
if (rc)
return RTErrConvertFromOS2(rc);
if (hf < 16)
{
unsigned i;
for (i = 0; i < RT_ELEMENTS(ahfs); i++)
{
ahfs[i] = 0xffffffff;
if (rc)
break;
}
if (i-- > 1)
{
if (!rc)
{
fulState &= OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT; /* Turn off non-participating bits. */
}
if (!rc)
{
}
else
i++;
while (i-- > 0)
}
}
#elif defined(RT_OS_FREEBSD)
/*
* Try open the BSD device. The device cloning makes this a bit of work.
*/
# if defined(VBOX_VBGLR3_XFREE86)
int File = 0;
# else
# endif
int rc;
{
# if defined(VBOX_VBGLR3_XFREE86)
if (File >= 0)
break;
# else
if (RT_SUCCESS(rc))
break;
# endif
}
# if defined(VBOX_VBGLR3_XFREE86)
if (File == -1)
return VERR_OPEN_FAILED;
# else
if (RT_FAILURE(rc))
return rc;
# endif
if (File == -1)
return VERR_OPEN_FAILED;
#else
/* The default implemenation. (linux, solaris) */
int rc = RTFileOpen(&File, VBOXGUEST_DEVICE_NAME, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
if (RT_FAILURE(rc))
return rc;
#endif
/*
* Create release logger
*/
static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
/* This may legitimately fail if we are using the mini-runtime. */
if (RT_SUCCESS(rc2))
return VINF_SUCCESS;
}
VBGLR3DECL(void) VbglR3Term(void)
{
/*
* Decrement the reference count and see if we're the last one out.
*/
if (cInits > 0)
return;
#if !defined(VBOX_VBGLR3_XFREE86)
# if defined(RT_OS_WINDOWS)
g_File = NIL_RTFILE;
# else /* The IPRT case. */
g_File = NIL_RTFILE;
# endif
#else /* VBOX_VBGLR3_XFREE86 */
g_File = -1;
if (File == -1)
return;
#endif /* VBOX_VBGLR3_XFREE86 */
}
/**
* Internal wrapper around various OS specific ioctl implemenations.
*
* @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
* an failure returned by the OS specific ioctl APIs.
*
* @param iFunction The requested function.
* @param pvData The input and output data buffer.
* @param cbData The size of the buffer.
*
* @remark Exactly how the VBoxGuestCommonIOCtl is ferried back
* here is OS specific. On BSD and Darwin we can use errno,
* while on OS/2 we use the 2nd buffer of the IOCtl.
*/
{
#if defined(RT_OS_WINDOWS)
DWORD cbReturned = 0;
{
/** @todo The passing of error codes needs to be tested and fixed (as does *all* the other hosts except for
* OS, Michael and Ramshankar!). The idea is that the VBox status codes in ring-0 should be
* transfered without loss down to ring-3. However, it's not vitally important right now (obviously, since
* the other guys has been ignoring it for 1+ years now). */
return RTErrConvertFromWin32(LastErr);
}
return VINF_SUCCESS;
return vrc;
return RTErrConvertFromOS2(rc);
/** @todo test status code passing! */
if (rc == -1)
{
return RTErrConvertFromErrno(rc);
}
return VINF_SUCCESS;
#elif defined(VBOX_VBGLR3_XFREE86)
/* PORTME - This is preferred over the RTFileIOCtl variant below, just be careful with the (int). */
/** @todo test status code passing! */
if (rc == -1)
return VERR_FILE_IO_ERROR; /* This is purely legacy stuff, it has to work and no more. */
return VINF_SUCCESS;
#else
/* Default implementation - PORTME: Do not use this without testings that passing errors works! */
/** @todo test status code passing! */
int rc2 = VERR_INTERNAL_ERROR;
if (RT_SUCCESS(rc))
return rc;
#endif
}