VBoxGuestR3Lib.cpp revision 8998be4b936b8b2d62f2165039f464c71112bda4
083dd76e9fd7a829b1ed67ffc9003276643e7db1vboxsync/* $Id$ */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync/** @file
083dd76e9fd7a829b1ed67ffc9003276643e7db1vboxsync * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Core.
85c594c1140f082dd862abde9dc7825137a3d51avboxsync */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync/*
67c2e6149c2c3c1de582868b109cf5b07e0daa3fvboxsync * Copyright (C) 2007 Oracle Corporation
85c594c1140f082dd862abde9dc7825137a3d51avboxsync *
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 *
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 *
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
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync/*******************************************************************************
85c594c1140f082dd862abde9dc7825137a3d51avboxsync* Header Files *
85c594c1140f082dd862abde9dc7825137a3d51avboxsync*******************************************************************************/
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#if defined(RT_OS_WINDOWS)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync# include <Windows.h>
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#elif defined(RT_OS_OS2)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync# define INCL_BASE
85c594c1140f082dd862abde9dc7825137a3d51avboxsync# define INCL_ERRORS
85c594c1140f082dd862abde9dc7825137a3d51avboxsync# include <os2.h>
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync#elif defined(RT_OS_FREEBSD) \
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync || defined(RT_OS_LINUX) \
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync || defined(RT_OS_SOLARIS)
87b2fb3f8283472ba7010aedbf2b4dc12302155cvboxsync# include <sys/types.h>
87b2fb3f8283472ba7010aedbf2b4dc12302155cvboxsync# include <sys/stat.h>
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync# if defined(RT_OS_LINUX) /** @todo check this on solaris+freebsd as well. */
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync# include <sys/ioctl.h>
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync# endif
11a862be79fe123488bccca60c06e92cdbfec6e8vboxsync# include <errno.h>
87b2fb3f8283472ba7010aedbf2b4dc12302155cvboxsync# include <unistd.h>
e38719852d98638514dba23fbacf53ad11361d6avboxsync#endif
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
d6aa6429f99fb7648883eb612f8a52b9aaf3bff4vboxsync#include <iprt/assert.h>
85c594c1140f082dd862abde9dc7825137a3d51avboxsync#include <iprt/asm.h>
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync#include <iprt/file.h>
d6aa6429f99fb7648883eb612f8a52b9aaf3bff4vboxsync#include <iprt/time.h>
d6aa6429f99fb7648883eb612f8a52b9aaf3bff4vboxsync#include <iprt/string.h>
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync#include <iprt/thread.h>
3598f07e0e71a448a04d478320a9ca6314160ff6vboxsync#include <VBox/log.h>
083dd76e9fd7a829b1ed67ffc9003276643e7db1vboxsync#include "VBGLR3Internal.h"
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync#ifdef VBOX_VBGLR3_XFREE86
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync/* Rather than try to resolve all the header file conflicts, I will just
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync prototype what we need here. */
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync# define XF86_O_RDWR 0x0002
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsynctypedef void *pointer;
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsyncextern "C" int xf86open(const char *, int, ...);
8ab60c04baaf509b2aaae2a260adaf1281aaac03vboxsyncextern "C" int xf86close(int);
8ab60c04baaf509b2aaae2a260adaf1281aaac03vboxsyncextern "C" int xf86ioctl(int, unsigned long, pointer);
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync#endif
6edb4183bc898fddcd0987b6c5c3903b8246fe45vboxsync
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync/*******************************************************************************
85c594c1140f082dd862abde9dc7825137a3d51avboxsync* Global Variables *
85c594c1140f082dd862abde9dc7825137a3d51avboxsync*******************************************************************************/
85c594c1140f082dd862abde9dc7825137a3d51avboxsync/** The VBoxGuest device handle. */
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync#ifdef VBOX_VBGLR3_XFREE86
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsyncstatic int g_File = -1;
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync#elif defined(RT_OS_WINDOWS)
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsyncstatic HANDLE g_hFile = INVALID_HANDLE_VALUE;
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync#else
85c594c1140f082dd862abde9dc7825137a3d51avboxsyncstatic RTFILE g_File = NIL_RTFILE;
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync#endif
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.
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync */
507ebf9b3b77c84000a55645867db6617b5324bcvboxsyncstatic uint32_t volatile g_cInits = 0;
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync
4ad933b349952e43d0b6ce21b3f5e2833ca04e85vboxsync
83b8ea36fb307b6264bc47ae9f7f67b1d32e3388vboxsync
83b8ea36fb307b6264bc47ae9f7f67b1d32e3388vboxsync/**
83b8ea36fb307b6264bc47ae9f7f67b1d32e3388vboxsync * Implementation of VbglR3Init and VbglR3InitUser
83b8ea36fb307b6264bc47ae9f7f67b1d32e3388vboxsync */
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsyncstatic int vbglR3Init(const char *pszDeviceName)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync{
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync uint32_t cInits = ASMAtomicIncU32(&g_cInits);
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync Assert(cInits > 0);
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync if (cInits > 1)
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync {
98da2c2cca8786e117ad93a31c6b2c6c1c3cdc78vboxsync /*
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.
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync */
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#ifdef RT_OS_WINDOWS
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (g_hFile == INVALID_HANDLE_VALUE)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#elif !defined (VBOX_VBGLR3_XFREE86)
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync if (g_File == NIL_RTFILE)
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync#else
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (g_File == -1)
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync#endif
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync return VERR_INTERNAL_ERROR;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return VINF_SUCCESS;
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync }
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#if defined(RT_OS_WINDOWS)
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync if (g_hFile != INVALID_HANDLE_VALUE)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#elif !defined(VBOX_VBGLR3_XFREE86)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (g_File != NIL_RTFILE)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#else
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (g_File != -1)
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync#endif
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync return VERR_INTERNAL_ERROR;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#if defined(RT_OS_WINDOWS)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync /*
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.
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync */
83b8ea36fb307b6264bc47ae9f7f67b1d32e3388vboxsync HANDLE hFile = CreateFile(pszDeviceName,
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync GENERIC_READ | GENERIC_WRITE,
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync FILE_SHARE_READ | FILE_SHARE_WRITE,
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync NULL,
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync OPEN_EXISTING,
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync NULL);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (hFile == INVALID_HANDLE_VALUE)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync return VERR_OPEN_FAILED;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync g_hFile = hFile;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#elif defined(RT_OS_OS2)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync /*
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 */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync HFILE hf = NULLHANDLE;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync ULONG ulAction = 0;
83b8ea36fb307b6264bc47ae9f7f67b1d32e3388vboxsync APIRET rc = DosOpen((PCSZ)pszDeviceName, &hf, &ulAction, 0, FILE_NORMAL,
85c594c1140f082dd862abde9dc7825137a3d51avboxsync OPEN_ACTION_OPEN_IF_EXISTS,
85c594c1140f082dd862abde9dc7825137a3d51avboxsync OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
85c594c1140f082dd862abde9dc7825137a3d51avboxsync NULL);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (rc)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return RTErrConvertFromOS2(rc);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (hf < 16)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync {
85c594c1140f082dd862abde9dc7825137a3d51avboxsync HFILE ahfs[16];
85c594c1140f082dd862abde9dc7825137a3d51avboxsync unsigned i;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync for (i = 0; i < RT_ELEMENTS(ahfs); i++)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync {
85c594c1140f082dd862abde9dc7825137a3d51avboxsync ahfs[i] = 0xffffffff;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync rc = DosDupHandle(hf, &ahfs[i]);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (rc)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync break;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync }
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (i-- > 1)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync {
85c594c1140f082dd862abde9dc7825137a3d51avboxsync ULONG fulState = 0;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync rc = DosQueryFHState(ahfs[i], &fulState);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (!rc)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync {
85c594c1140f082dd862abde9dc7825137a3d51avboxsync fulState |= OPEN_FLAGS_NOINHERIT;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync fulState &= OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT; /* Turn off non-participating bits. */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync rc = DosSetFHState(ahfs[i], fulState);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync }
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (!rc)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync {
85c594c1140f082dd862abde9dc7825137a3d51avboxsync rc = DosClose(hf);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync AssertMsg(!rc, ("%ld\n", rc));
85c594c1140f082dd862abde9dc7825137a3d51avboxsync hf = ahfs[i];
85c594c1140f082dd862abde9dc7825137a3d51avboxsync }
85c594c1140f082dd862abde9dc7825137a3d51avboxsync else
85c594c1140f082dd862abde9dc7825137a3d51avboxsync i++;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync while (i-- > 0)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync DosClose(ahfs[i]);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync }
85c594c1140f082dd862abde9dc7825137a3d51avboxsync }
dc0a54940789f994c84390cb4a9f03da0b492285vboxsync g_File = (RTFILE)hf;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
4faf92053d9cec5599c0ab8577231df0f7d615b3vboxsync#elif defined(VBOX_VBGLR3_XFREE86)
83b8ea36fb307b6264bc47ae9f7f67b1d32e3388vboxsync int File = xf86open(pszDeviceName, XF86_O_RDWR);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (File == -1)
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync return VERR_OPEN_FAILED;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync g_File = File;
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync
6edb4183bc898fddcd0987b6c5c3903b8246fe45vboxsync#else
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync /* The default implementation. (linux, solaris, freebsd) */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync RTFILE File;
83b8ea36fb307b6264bc47ae9f7f67b1d32e3388vboxsync int rc = RTFileOpen(&File, pszDeviceName, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (RT_FAILURE(rc))
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return rc;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync g_File = File;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
e38719852d98638514dba23fbacf53ad11361d6avboxsync#endif
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
2077a9092ce42fb009a3ab89b095ec02af18213evboxsync#ifndef VBOX_VBGLR3_XFREE86
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync /*
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync * Create release logger
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync */
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync PRTLOGGER pReleaseLogger;
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. */
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync if (RT_SUCCESS(rc2))
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync RTLogRelSetDefaultInstance(pReleaseLogger);
2077a9092ce42fb009a3ab89b095ec02af18213evboxsync#endif
3598f07e0e71a448a04d478320a9ca6314160ff6vboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return VINF_SUCCESS;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync}
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync/**
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync * Open the VBox R3 Guest Library. This should be called by system daemons
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync * and processes.
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync */
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsyncVBGLR3DECL(int) VbglR3Init(void)
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync{
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync return vbglR3Init(VBOXGUEST_DEVICE_NAME);
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync}
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync/**
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync * Open the VBox R3 Guest Library. Equivalent to VbglR3Init, but for user
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync * session processes.
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync */
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsyncVBGLR3DECL(int) VbglR3InitUser(void)
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync{
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync return vbglR3Init(VBOXGUEST_USER_DEVICE_NAME);
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync}
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync
584a4e8a4f886566a96ba14cdaab6e94162545a8vboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsyncVBGLR3DECL(void) VbglR3Term(void)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync{
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync /*
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync * Decrement the reference count and see if we're the last one out.
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync */
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync uint32_t cInits = ASMAtomicDecU32(&g_cInits);
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync if (cInits > 0)
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync return;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#if !defined(VBOX_VBGLR3_XFREE86)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync AssertReturnVoid(!cInits);
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync# if defined(RT_OS_WINDOWS)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync HANDLE hFile = g_hFile;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync g_hFile = INVALID_HANDLE_VALUE;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync AssertReturnVoid(hFile != INVALID_HANDLE_VALUE);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync BOOL fRc = CloseHandle(hFile);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync Assert(fRc); NOREF(fRc);
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync# elif defined(RT_OS_OS2)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync RTFILE File = g_File;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync g_File = NIL_RTFILE;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync AssertReturnVoid(File != NIL_RTFILE);
dc0a54940789f994c84390cb4a9f03da0b492285vboxsync APIRET rc = DosClose((uintptr_t)File);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync AssertMsg(!rc, ("%ld\n", rc));
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync# else /* The IPRT case. */
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync RTFILE File = g_File;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync g_File = NIL_RTFILE;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync AssertReturnVoid(File != NIL_RTFILE);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync int rc = RTFileClose(File);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync AssertRC(rc);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync# endif
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#else /* VBOX_VBGLR3_XFREE86 */
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync int File = g_File;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync g_File = -1;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (File == -1)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync return;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync xf86close(File);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#endif /* VBOX_VBGLR3_XFREE86 */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync}
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync/**
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * Internal wrapper around various OS specific ioctl implementations.
e38719852d98638514dba23fbacf53ad11361d6avboxsync *
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * an failure returned by the OS specific ioctl APIs.
e38719852d98638514dba23fbacf53ad11361d6avboxsync *
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * @param iFunction The requested function.
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * @param pvData The input and output data buffer.
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * @param cbData The size of the buffer.
e38719852d98638514dba23fbacf53ad11361d6avboxsync *
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.
85c594c1140f082dd862abde9dc7825137a3d51avboxsync */
0c1bdc5adae416967cb64e09f8ec81a5b77fe31dvboxsyncint vbglR3DoIOCtl(unsigned iFunction, void *pvData, size_t cbData)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync{
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#if defined(RT_OS_WINDOWS)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync DWORD cbReturned = 0;
06bb0fc0d77854631add1701e7ccde0095c73796vboxsync if (!DeviceIoControl(g_hFile, iFunction, pvData, (DWORD)cbData, pvData, (DWORD)cbData, &cbReturned, NULL))
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync {
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. */
16a9c73495bd644adca2ffe9567c3b2a72d7b94dvboxsync DWORD LastErr = GetLastError();
16a9c73495bd644adca2ffe9567c3b2a72d7b94dvboxsync return RTErrConvertFromWin32(LastErr);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync }
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync return VINF_SUCCESS;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#elif defined(RT_OS_OS2)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync ULONG cbOS2Parm = cbData;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync int32_t vrc = VERR_INTERNAL_ERROR;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync ULONG cbOS2Data = sizeof(vrc);
dc0a54940789f994c84390cb4a9f03da0b492285vboxsync APIRET rc = DosDevIOCtl((uintptr_t)g_File, VBOXGUEST_IOCTL_CATEGORY, iFunction,
85c594c1140f082dd862abde9dc7825137a3d51avboxsync pvData, cbData, &cbOS2Parm,
85c594c1140f082dd862abde9dc7825137a3d51avboxsync &vrc, sizeof(vrc), &cbOS2Data);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (RT_LIKELY(!rc))
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return vrc;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return RTErrConvertFromOS2(rc);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
b29e03c9044019d9c77222336e8f8616052824f5vboxsync#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync VBGLBIGREQ Hdr;
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync Hdr.u32Magic = VBGLBIGREQ_MAGIC;
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync Hdr.cbData = cbData;
11a862be79fe123488bccca60c06e92cdbfec6e8vboxsync Hdr.pvDataR3 = pvData;
6e94cf9c6334b6d8e3cd33afe7084e0048fe7c8evboxsync# if HC_ARCH_BITS == 32
6e94cf9c6334b6d8e3cd33afe7084e0048fe7c8evboxsync Hdr.u32Padding = 0;
6e94cf9c6334b6d8e3cd33afe7084e0048fe7c8evboxsync# endif
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync
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). */
8998be4b936b8b2d62f2165039f464c71112bda4vboxsync#ifdef VBOX_VBGLR3_XFREE86
8998be4b936b8b2d62f2165039f464c71112bda4vboxsync int rc = xf86ioctl(g_File, iFunction, &Hdr);
8998be4b936b8b2d62f2165039f464c71112bda4vboxsync#else
dc0a54940789f994c84390cb4a9f03da0b492285vboxsync int rc = ioctl(RTFileToNative(g_File), iFunction, &Hdr);
8998be4b936b8b2d62f2165039f464c71112bda4vboxsync#endif
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync if (rc == -1)
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync {
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync rc = errno;
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync return RTErrConvertFromErrno(rc);
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync }
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync return VINF_SUCCESS;
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync#elif defined(RT_OS_LINUX)
80b2a38b5b76ea1ccf85fc51191685fff23ccfebvboxsync# ifdef VBOX_VBGLR3_XFREE86
80b2a38b5b76ea1ccf85fc51191685fff23ccfebvboxsync int rc = xf86ioctl((int)g_File, iFunction, pvData);
80b2a38b5b76ea1ccf85fc51191685fff23ccfebvboxsync# else
16234c5f18bf83208a8b5a3a724435dc65e5504cvboxsync if (g_File == NIL_RTFILE)
16234c5f18bf83208a8b5a3a724435dc65e5504cvboxsync return VERR_INVALID_HANDLE;
dc0a54940789f994c84390cb4a9f03da0b492285vboxsync int rc = ioctl(RTFileToNative(g_File), iFunction, pvData);
80b2a38b5b76ea1ccf85fc51191685fff23ccfebvboxsync# endif
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync if (RT_LIKELY(rc == 0))
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync return VINF_SUCCESS;
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync /* Positive values are negated VBox error status codes. */
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync if (rc > 0)
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync rc = -rc;
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync else
80b2a38b5b76ea1ccf85fc51191685fff23ccfebvboxsync# ifdef VBOX_VBGLR3_XFREE86
80b2a38b5b76ea1ccf85fc51191685fff23ccfebvboxsync rc = VERR_FILE_IO_ERROR;
80b2a38b5b76ea1ccf85fc51191685fff23ccfebvboxsync# else
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync rc = RTErrConvertFromErrno(errno);
80b2a38b5b76ea1ccf85fc51191685fff23ccfebvboxsync# endif
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync NOREF(cbData);
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync return rc;
73814339da1a372e4dce0281cb0cced14d9e5873vboxsync
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync#elif defined(VBOX_VBGLR3_XFREE86)
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync /* PORTME - This is preferred over the RTFileIOCtl variant below, just be careful with the (int). */
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync/** @todo test status code passing! */
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync int rc = xf86ioctl(g_File, iFunction, pvData);
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync if (rc == -1)
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync return VERR_FILE_IO_ERROR; /* This is purely legacy stuff, it has to work and no more. */
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync return VINF_SUCCESS;
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync#else
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync /* Default implementation - PORTME: Do not use this without testings that passing errors works! */
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync/** @todo test status code passing! */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync int rc2 = VERR_INTERNAL_ERROR;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync int rc = RTFileIoCtl(g_File, (int)iFunction, pvData, cbData, &rc2);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (RT_SUCCESS(rc))
85c594c1140f082dd862abde9dc7825137a3d51avboxsync rc = rc2;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return rc;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync#endif
85c594c1140f082dd862abde9dc7825137a3d51avboxsync}
85c594c1140f082dd862abde9dc7825137a3d51avboxsync