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