VBoxGuestR3Lib.cpp revision 208aaecb51db539a2f7f3d25f38dd2efd0f014a3
77b1a2d8b5dbe2c0b5200794914239fee3c8ee5dvboxsync * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Core.
be41e59f051a3abb5cd05e7fe270ab2fea791f5avboxsync * Copyright (C) 2007 Sun Microsystems, Inc.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * available from http://www.virtualbox.org. This file is free software;
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * The contents of this file may alternatively be used under the terms
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * of the Common Development and Distribution License Version 1.0
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * VirtualBox OSE distribution, in which case the provisions of the
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * CDDL are applicable instead of those of the GPL.
cd6f71bc352f550074f1ba2c830a2cf2f0b3dd46vboxsync * You may elect to license modified versions of this file under the
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * terms and conditions of either the GPL or the CDDL or both.
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
16a9adc14900ca18e6909679a579f6833425e030vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * additional information or have any questions.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync/*******************************************************************************
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync* Header Files *
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync*******************************************************************************/
30c39307775310ba50de9d9b74f1ea9e12524102vboxsync || defined(RT_OS_LINUX) \
30c39307775310ba50de9d9b74f1ea9e12524102vboxsync# if defined(RT_OS_LINUX) /** @todo check this on solaris+freebsd as well. */
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync/* Rather than try to resolve all the header file conflicts, I will just
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync prototype what we need here. */
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsynctypedef void *pointer;
9496f2d398b49813176939d7a339ae513d5175efvboxsyncextern "C" int xf86ioctl(int, unsigned long, pointer);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync/*******************************************************************************
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync* Global Variables *
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync*******************************************************************************/
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync/** The VBoxGuest device handle. */
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync/** User counter.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * A counter of the number of times the library has been initialised, for use with
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * X.org drivers, where the library may be shared by multiple independant modules
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * inside a single process space.
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync * Implementation of VbglR3Init and VbglR3InitUser
7766bf675357fd940d8c49e69a5d72dc6eaa6be4vboxsync * This will fail if two (or more) threads race each other calling VbglR3Init.
7766bf675357fd940d8c49e69a5d72dc6eaa6be4vboxsync * However it will work fine for single threaded or otherwise serialized
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync * processed calling us more than once.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Have to use CreateFile here as we want to specify FILE_FLAG_OVERLAPPED
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * and possible some other bits not availble thru iprt/file.h.
9496f2d398b49813176939d7a339ae513d5175efvboxsync * We might wish to compile this with Watcom, so stick to
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync * the OS/2 APIs all the way. And in any case we have to use
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync * DosDevIOCtl for the requests, why not use Dos* for everything.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync APIRET rc = DosOpen((PCSZ)pszDeviceName, &hf, &ulAction, 0, FILE_NORMAL,
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
63c12491acc2b8b95c8ac454f1c48b98eec8f7d8vboxsync unsigned i;
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync if (i-- > 1)
61b5982fad4660d0fe3dd6ceba9eda85eb32f7e8vboxsync fulState &= OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT; /* Turn off non-participating bits. */
1741b757f7a570a7e6d48db46c3f4cd50f2ee296vboxsync while (i-- > 0)
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync /* The default implemenation. (linux, solaris, freebsd) */
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync int rc = RTFileOpen(&File, pszDeviceName, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync * Create release logger
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync int rc2 = RTLogCreate(&pReleaseLogger, 0, "all", "VBOX_RELEASE_LOG",
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync /* This may legitimately fail if we are using the mini-runtime. */
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync * Open the VBox R3 Guest Library. This should be called by system daemons
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync * and processes.
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync * Open the VBox R3 Guest Library. Equivalent to VbglR3Init, but for user
16a9adc14900ca18e6909679a579f6833425e030vboxsync * session processes.
16a9adc14900ca18e6909679a579f6833425e030vboxsync * Decrement the reference count and see if we're the last one out.
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync# else /* The IPRT case. */
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync#else /* VBOX_VBGLR3_XFREE86 */
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync#endif /* VBOX_VBGLR3_XFREE86 */
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync * Internal wrapper around various OS specific ioctl implemenations.
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync * an failure returned by the OS specific ioctl APIs.
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync * @param iFunction The requested function.
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync * @param pvData The input and output data buffer.
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync * @param cbData The size of the buffer.
a9d49c8f2b28a72e6a4db86eee91e4569290157bvboxsync * @remark Exactly how the VBoxGuestCommonIOCtl is ferried back
a9d49c8f2b28a72e6a4db86eee91e4569290157bvboxsync * here is OS specific. On BSD and Darwin we can use errno,
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync * while on OS/2 we use the 2nd buffer of the IOCtl.
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsyncint vbglR3DoIOCtl(unsigned iFunction, void *pvData, size_t cbData)
9496f2d398b49813176939d7a339ae513d5175efvboxsync if (!DeviceIoControl(g_hFile, iFunction, pvData, (DWORD)cbData, pvData, (DWORD)cbData, &cbReturned, NULL))
59d7f5939d42ad9d344fbad8401e00a900db92c5vboxsync/** @todo The passing of error codes needs to be tested and fixed (as does *all* the other hosts except for
59d7f5939d42ad9d344fbad8401e00a900db92c5vboxsync * OS/2). The idea is that the VBox status codes in ring-0 should be transfered without loss down to
22ec733a5e041fcdfe02fce2eafc9faf8b0077ddvboxsync * ring-3. However, it's not vitally important right now (obviously, since the other guys has been
9496f2d398b49813176939d7a339ae513d5175efvboxsync * ignoring it for 1+ years now). On Linux and Solaris the transfer is done, but it is currently not
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync * lossless, so still needs fixing. */
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync APIRET rc = DosDevIOCtl(g_File, VBOXGUEST_IOCTL_CATEGORY, iFunction,
cc15c3fa4bb2d3fb91e4d0cd15a73133963f86b0vboxsync#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
468c2bcb36eb9a032f5dd0fcb34db10bd58e9996vboxsync/** @todo test status code passing! Check that the kernel doesn't do any
bbede9c189def47a9880f0ffb03c0c230c774185vboxsync * error checks using specific errno values, and just pass an VBox
9496f2d398b49813176939d7a339ae513d5175efvboxsync * error instead of an errno.h one. Alternatively, extend/redefine the
16a9adc14900ca18e6909679a579f6833425e030vboxsync * header with an error code return field (much better alternative
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync * actually). */
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync int rc = xf86ioctl((int)g_File, iFunction, pvData);
a0a5ab4e085a7ee5b95bdfae04cec7de95792c3cvboxsync /* Positive values are negated VBox error status codes. */
b5ad839a3757b305d4e98d7264da2b53c9cd27f0vboxsync /* PORTME - This is preferred over the RTFileIOCtl variant below, just be careful with the (int). */
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync/** @todo test status code passing! */
b5ad839a3757b305d4e98d7264da2b53c9cd27f0vboxsync return VERR_FILE_IO_ERROR; /* This is purely legacy stuff, it has to work and no more. */
a0a5ab4e085a7ee5b95bdfae04cec7de95792c3cvboxsync /* Default implementation - PORTME: Do not use this without testings that passing errors works! */
a0a5ab4e085a7ee5b95bdfae04cec7de95792c3cvboxsync/** @todo test status code passing! */
b5ad839a3757b305d4e98d7264da2b53c9cd27f0vboxsync int rc = RTFileIoCtl(g_File, (int)iFunction, pvData, cbData, &rc2);