VBoxGuestR3Lib.cpp revision 8998be4b936b8b2d62f2165039f464c71112bda4
083dd76e9fd7a829b1ed67ffc9003276643e7db1vboxsync * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Core.
67c2e6149c2c3c1de582868b109cf5b07e0daa3fvboxsync * Copyright (C) 2007 Oracle Corporation
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.
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync * The contents of this file may alternatively be used under the terms
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync * of the Common Development and Distribution License Version 1.0
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync * VirtualBox OSE distribution, in which case the provisions of the
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync * CDDL are applicable instead of those of the GPL.
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync * You may elect to license modified versions of this file under the
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync * terms and conditions of either the GPL or the CDDL or both.
85c594c1140f082dd862abde9dc7825137a3d51avboxsync/*******************************************************************************
85c594c1140f082dd862abde9dc7825137a3d51avboxsync* Header Files *
85c594c1140f082dd862abde9dc7825137a3d51avboxsync*******************************************************************************/
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync || defined(RT_OS_LINUX) \
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync# if defined(RT_OS_LINUX) /** @todo check this on solaris+freebsd as well. */
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
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * X.org drivers, where the library may be shared by multiple independent modules
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync * inside a single process space.
83b8ea36fb307b6264bc47ae9f7f67b1d32e3388vboxsync * Implementation of VbglR3Init and VbglR3InitUser
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
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * and possible some other bits not available 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.
83b8ea36fb307b6264bc47ae9f7f67b1d32e3388vboxsync APIRET rc = DosOpen((PCSZ)pszDeviceName, &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)
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync /* The default implementation. (linux, solaris, freebsd) */
83b8ea36fb307b6264bc47ae9f7f67b1d32e3388vboxsync int rc = RTFileOpen(&File, pszDeviceName, 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",
e66c95fa9950a4d9c567db63a4a495bf086cc4bfvboxsync RT_ELEMENTS(s_apszGroups), &s_apszGroups[0], RTLOGDEST_USER, NULL);
3598f07e0e71a448a04d478320a9ca6314160ff6vboxsync /* This may legitimately fail if we are using the mini-runtime. */
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync * Open the VBox R3 Guest Library. This should be called by system daemons
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync * and processes.
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync * Open the VBox R3 Guest Library. Equivalent to VbglR3Init, but for user
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync * session processes.
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 */
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * Internal wrapper around various OS specific ioctl implementations.
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)
06bb0fc0d77854631add1701e7ccde0095c73796vboxsync if (!DeviceIoControl(g_hFile, iFunction, pvData, (DWORD)cbData, pvData, (DWORD)cbData, &cbReturned, NULL))
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync/** @todo The passing of error codes needs to be tested and fixed (as does *all* the other hosts except for
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * OS/2). The idea is that the VBox status codes in ring-0 should be transferred 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. */
dc0a54940789f994c84390cb4a9f03da0b492285vboxsync APIRET rc = DosDevIOCtl((uintptr_t)g_File, VBOXGUEST_IOCTL_CATEGORY, iFunction,
b29e03c9044019d9c77222336e8f8616052824f5vboxsync#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
b9b55af9562405a52639a014db7fcc08040066advboxsync/** @todo test status code passing! Check that the kernel doesn't do any
b9b55af9562405a52639a014db7fcc08040066advboxsync * error checks using specific errno values, and just pass an VBox
b9b55af9562405a52639a014db7fcc08040066advboxsync * error instead of an errno.h one. Alternatively, extend/redefine the
b9b55af9562405a52639a014db7fcc08040066advboxsync * header with an error code return field (much better alternative
b9b55af9562405a52639a014db7fcc08040066advboxsync * actually). */
dc0a54940789f994c84390cb4a9f03da0b492285vboxsync int rc = ioctl(RTFileToNative(g_File), iFunction, &Hdr);
80b2a38b5b76ea1ccf85fc51191685fff23ccfebvboxsync int rc = xf86ioctl((int)g_File, iFunction, pvData);
dc0a54940789f994c84390cb4a9f03da0b492285vboxsync int rc = ioctl(RTFileToNative(g_File), iFunction, pvData);
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync /* Positive values are negated VBox error status codes. */
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);