VBoxGuestR3Lib.cpp revision 4cf22962b49e1056d0424cbe6736fee615e296ef
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Core.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * Copyright (C) 2007 Sun Microsystems, Inc.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * available from http://www.virtualbox.org. This file is free software;
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * General Public License (GPL) as published by the Free Software
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * additional information or have any questions.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync/*******************************************************************************
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync* Header Files *
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync*******************************************************************************/
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync/* Rather than try to resolve all the header file conflicts, I will just
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync prototype what we need here. */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsynctypedef void *pointer;
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsyncextern "C" int xf86ioctl(int, unsigned long, pointer);
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync/*******************************************************************************
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync* Global Variables *
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync*******************************************************************************/
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync/** The VBoxGuest device handle. */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync/** User counter.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * A counter of the number of times the library has been initialised, for use with
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * X.org drivers, where the library may be shared by multiple independant modules
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * inside a single process space.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * This will fail if two (or more) threads race each other calling VbglR3Init.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * However it will work fine for single threaded or otherwise serialized
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * processed calling us more than once.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * Have to use CreateFile here as we want to specify FILE_FLAG_OVERLAPPED
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * and possible some other bits not availble thru iprt/file.h.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * We might wish to compile this with Watcom, so stick to
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * the OS/2 APIs all the way. And in any case we have to use
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * DosDevIOCtl for the requests, why not use Dos* for everything.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync APIRET rc = DosOpen((PCSZ)VBOXGUEST_DEVICE_NAME, &hf, &ulAction, 0, FILE_NORMAL,
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync unsigned i;
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync if (i-- > 1)
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync fulState &= OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT; /* Turn off non-participating bits. */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync while (i-- > 0)
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * Try open the BSD device. The device cloning makes this a bit of work.
e6ad2e18e663b076aeabfec994947514566a7accvboxsync RTStrPrintf(szDevice, sizeof(szDevice), VBOXGUEST_DEVICE_NAME "%d", iUnit);
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync rc = RTFileOpen(&File, szDevice, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync#elif defined(VBOX_VBGLR3_XFREE86) && !defined(RT_OS_FREEBSD)
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync int File = xf86open(VBOXGUEST_DEVICE_NAME, XF86_O_RDWR);
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync /* The default implemenation. (linux, solaris) */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync int rc = RTFileOpen(&File, VBOXGUEST_DEVICE_NAME, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * Create release logger
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync int rc2 = RTLogCreate(&pReleaseLogger, 0, "all", "VBOX_RELEASE_LOG",
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync /* This may legitimately fail if we are using the mini-runtime. */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * Decrement the reference count and see if we're the last one out.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync# else /* The IPRT case. */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync#else /* VBOX_VBGLR3_XFREE86 */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync#endif /* VBOX_VBGLR3_XFREE86 */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * Internal wrapper around various OS specific ioctl implemenations.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * an failure returned by the OS specific ioctl APIs.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * @param iFunction The requested function.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * @param pvData The input and output data buffer.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * @param cbData The size of the buffer.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * @remark Exactly how the VBoxGuestCommonIOCtl is ferried back
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * here is OS specific. On BSD and Darwin we can use errno,
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * while on OS/2 we use the 2nd buffer of the IOCtl.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsyncint vbglR3DoIOCtl(unsigned iFunction, void *pvData, size_t cbData)
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync if (!DeviceIoControl(g_hFile, iFunction, pvData, cbData, pvData, cbData, &cbReturned, NULL))
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync/** @todo The passing of error codes needs to be tested and fixed (as does *all* the other hosts except for
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * OS/2). The idea is that the VBox status codes in ring-0 should be transfered without loss down to
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * ring-3. However, it's not vitally important right now (obviously, since the other guys has been
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * ignoring it for 1+ years now). On Linux and Solaris the transfer is done, but it is currently not
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * lossless, so still needs fixing. */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync APIRET rc = DosDevIOCtl(g_File, VBOXGUEST_IOCTL_CATEGORY, iFunction,
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync/** @todo test status code passing! */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync /* PORTME - This is preferred over the RTFileIOCtl variant below, just be careful with the (int). */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync/** @todo test status code passing! */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync return VERR_FILE_IO_ERROR; /* This is purely legacy stuff, it has to work and no more. */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync /* Default implementation - PORTME: Do not use this without testings that passing errors works! */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync/** @todo test status code passing! */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync int rc = RTFileIoCtl(g_File, (int)iFunction, pvData, cbData, &rc2);