VBoxGuestR3Lib.cpp revision 11a862be79fe123488bccca60c06e92cdbfec6e8
/** $Id$ */
/** @file
* VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions.
*/
/*
* Copyright (C) 2007 innotek GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifdef RT_OS_OS2
# define INCL_BASE
# define INCL_ERRORS
# include <os2.h>
#elif defined(RT_OS_LINUX)
# include <fcntl.h>
# include <stdlib.h>
# include <unistd.h>
# include <sys/resource.h>
#elif defined(RT_OS_SOLARIS)
# include <errno.h>
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <signal.h>
# include <fcntl.h>
#endif
#include <VBox/VBoxGuest.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The VBoxGuest device handle. */
VBGLR3DECL(int) VbglR3Init(void)
{
if (g_File != NIL_RTFILE)
return VINF_SUCCESS;
#if defined(RT_OS_OS2)
/*
* We might wish to compile this with Watcom, so stick to
* the OS/2 APIs all the way. And in any case we have to use
* DosDevIOCtl for the requests, why not use Dos* for everything.
*/
NULL);
if (rc)
return RTErrConvertFromOS2(rc);
if (hf < 16)
{
unsigned i;
for (i = 0; i < RT_ELEMENTS(ahfs); i++)
{
ahfs[i] = 0xffffffff;
if (rc)
break;
}
if (i-- > 1)
{
if (!rc)
{
fulState &= OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT; /* Turn off non-participating bits. */
}
if (!rc)
{
}
else
i++;
while (i-- > 0)
}
}
/* PORTME */
#else
/* the default implemenation. (linux, solaris) */
int rc = RTFileOpen(&File, VBOXGUEST_DEVICE_NAME, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
if (RT_FAILURE(rc))
return rc;
#endif
return VINF_SUCCESS;
}
VBGLR3DECL(void) VbglR3Term(void)
{
g_File = NIL_RTFILE;
if (File == NIL_RTFILE)
return;
#if defined(RT_OS_OS2)
#else
#endif
}
/**
* Internal wrapper around various OS specific ioctl implemenations.
*
* @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
* an failure returned by the OS specific ioctl APIs.
*
* @param iFunction The requested function.
* @param pvData The input and output data buffer.
* @param cbData The size of the buffer.
*
* @remark Exactly how the VBoxGuestCommonIOCtl is ferried back
* here is OS specific. On BSD and Darwin we can use errno,
* while on OS/2 we use the 2nd buffer of the IOCtl.
*/
{
#ifdef RT_OS_OS2
return vrc;
return RTErrConvertFromOS2(rc);
#elif defined(RT_OS_SOLARIS)
if (rc == -1)
return rc;
#else
/* Default implementation (linux, solaris). */
int rc2 = VERR_INTERNAL_ERROR;
if (RT_SUCCESS(rc))
return rc;
#endif
}
VBGLR3DECL(int) VbglR3GRAlloc(VMMDevRequestHeader **ppReq, uint32_t cb, VMMDevRequestType enmReqType)
{
AssertMsgReturn(cb >= sizeof(VMMDevRequestHeader), ("%#x vs %#zx\n", cb, sizeof(VMMDevRequestHeader)),
if (RT_UNLIKELY(!pReq))
return VERR_NO_MEMORY;
return VINF_SUCCESS;
}
{
}
{
}
{
if (RT_SUCCESS(rc))
return rc;
}
{
Req.mouseFeatures = 0;
Req.pointerXPos = 0;
Req.pointerYPos = 0;
if (RT_SUCCESS(rc))
{
if (pfFeatures)
if (px)
if (py)
}
return rc;
}
{
Req.pointerXPos = 0;
Req.pointerYPos = 0;
}
/**
* Cause any pending WaitEvent calls (VBOXGUEST_IOCTL_WAITEVENT) to return
* with a VERR_INTERRUPTED status.
*
* Can be used in combination with a termination flag variable for interrupting
* event loops. Avoiding race conditions is the responsibility of the caller.
*
* @returns IPRT status code
*/
VBGLR3DECL(int) VbglR3InterruptEventWaits(void)
{
return vbglR3DoIOCtl(VBOXGUEST_IOCTL_WAITEVENT_INTERRUPT_ALL, 0, 0);
}
/**
* Write to the backdoor logger from ring 3 guest code.
*
* @returns IPRT status code
*
* @remarks This currently does not accept more than 255 bytes of data at
* one time. It should probably be rewritten to use pass a pointer
* in the IOCtl.
*/
{
/*
* Solaris does not accept more than 255 bytes of data per ioctl request,
* so split large string into 128 byte chunks to prevent truncation.
*/
#define STEP 128 /** @todo increase to 512 when solaris ioctl code is fixed. (darwin limits us to 1024 IIRC) */
int rc = VINF_SUCCESS;
{
}
return rc;
}
/**
* Change the IRQ filter mask.
*
* @returns IPRT status code
* @param fOr The OR mask.
* @param fNo The NOT mask.
*/
{
}
/**
* Daemonize the process for running in the background.
*
* @returns 0 on success
*
* @param nochdir Pass 0 to change working directory to root.
*/
{
#if defined(RT_OS_LINUX)
/** rlimit structure for finding out how many open files we may have. */
/* To make sure that we are not currently a session leader, we must first fork and let
the parent process exit, as a newly created child is never session leader. This will
allow us to call setsid() later. */
if (fork() != 0)
{
exit(0);
}
/* Find the maximum number of files we can have open and close them all. */
{
/* For some reason the call failed. In that case we will just close the three
standard files and hope. */
}
{
close(i);
}
/* Change to the root directory to avoid keeping the one we were started in open. */
chdir("/");
/* Set our umask to zero. */
umask(0);
dup(1);
/* Detach from the controlling terminal by creating our own session, to avoid receiving
signals from the old session. */
setsid();
/* And fork again, letting the parent exit, to make us a child of init and avoid zombies. */
if (fork() != 0)
{
exit(0);
}
return 0;
/* Get the full path to the executable. */
char szExe[CCHMAXPATH];
if (rc)
{
return -1;
}
/* calc the length of the command line. */
char *pchArgs;
{
while (*pch);
}
else
{
if (cch1)
else
}
/* spawn a detach process */
char szObj[128];
RESULTCODES ResCodes = { 0, 0 };
szObj[0] = '\0';
rc = DosExecPgm(szObj, sizeof(szObj), EXEC_BACKGROUND, (PCSZ)pchArgs, NULL, &ResCodes, (PCSZ)szExe);
if (rc)
{
/* VBoxServiceError("DosExecPgm failed with rc=%d and szObj='%s'\n", rc, szObj); */
return -1;
}
DosExit(EXIT_PROCESS, 0);
return -1;
#elif defined(RT_OS_SOLARIS)
return -1;
if (pid < 0) /* The fork() failed. Bad. */
return -1;
if (pid > 0) /* Quit parent process */
exit(0);
/*
* The orphaned child becomes a daemon after attaching to init. We need to get
* rid of signals, file descriptors & other stuff we inherited from the parent.
*/
if (newpgid < 0) /* Failed to create new sesion */
return -1;
/* BSD daemon style. */
if (!noclose)
{
if (fd > 2)
}
/* Switch our current directory to root */
if (!nochdir)
/* Set file permission to something secure, as we need to run as root on Solaris */
umask(027);
return 0;
#endif
}