DrvNamedPipe.cpp revision c18940aaee473ca75acc33491c1dc77b3e8094e0
/** @file
*
* VBox stream devices:
* Named pipe stream
*/
/*
* Copyright (C) 2006-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 *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_NAMEDPIPE
#include <iprt/semaphore.h>
#include "Builtins.h"
#ifdef RT_OS_WINDOWS
#include <windows.h>
#else /* !RT_OS_WINDOWS */
#include <errno.h>
#include <unistd.h>
#endif /* !RT_OS_WINDOWS */
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Converts a pointer to DRVNAMEDPIPE::IMedia to a PDRVNAMEDPIPE. */
#define PDMISTREAM_2_DRVNAMEDPIPE(pInterface) ( (PDRVNAMEDPIPE)((uintptr_t)pInterface - RT_OFFSETOF(DRVNAMEDPIPE, IStream)) )
/** Converts a pointer to PDMDRVINS::IBase to a PPDMDRVINS. */
#define PDMIBASE_2_DRVINS(pInterface) ( (PPDMDRVINS)((uintptr_t)pInterface - RT_OFFSETOF(PDMDRVINS, IBase)) )
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Named pipe driver instance data.
*/
typedef struct DRVNAMEDPIPE
{
/** The stream interface. */
/** Pointer to the driver instance. */
/** Pointer to the named pipe file name. (Freed by MM) */
char *pszLocation;
/** Flag whether VirtualBox represents the server or client side. */
bool fIsServer;
#ifdef RT_OS_WINDOWS
/* File handle of the named pipe. */
/* Overlapped structure for writes. */
/* Overlapped structure for reads. */
/* Listen thread wakeup semaphore */
#else /* !RT_OS_WINDOWS */
/** Socket handle of the local socket for server. */
/** Socket handle of the local socket. */
#endif /* !RT_OS_WINDOWS */
/** Thread for listening for new connections. */
/** Flag to signal listening thread to shut down. */
bool fShutdown;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/** @copydoc PDMISTREAM::pfnRead */
{
int rc = VINF_SUCCESS;
#ifdef RT_OS_WINDOWS
{
{
if ( uError == ERROR_PIPE_LISTENING
|| uError == ERROR_PIPE_NOT_CONNECTED)
{
cbReallyRead = 0;
/* wait a bit or else we'll be called right back. */
RTThreadSleep(100);
}
else
{
if (uError == ERROR_IO_PENDING)
{
uError = 0;
/* Wait for incoming bytes. */
uError = GetLastError();
}
}
}
if (VBOX_FAILURE(rc))
{
|| rc == VERR_BROKEN_PIPE
)
)
{
{
}
/* pretend success */
rc = VINF_SUCCESS;
}
cbReallyRead = 0;
}
}
#else /* !RT_OS_WINDOWS */
{
if (cbReallyRead == 0)
{
}
else if (cbReallyRead == -1)
{
cbReallyRead = 0;
}
*cbRead = cbReallyRead;
}
#endif /* !RT_OS_WINDOWS */
else
{
RTThreadSleep(100);
*cbRead = 0;
}
return rc;
}
/** @copydoc PDMISTREAM::pfnWrite */
static DECLCALLBACK(int) drvNamedPipeWrite(PPDMISTREAM pInterface, const void *pvBuf, size_t *cbWrite)
{
int rc = VINF_SUCCESS;
#ifdef RT_OS_WINDOWS
{
unsigned cbWritten;
{
if ( uError == ERROR_PIPE_LISTENING
|| uError == ERROR_PIPE_NOT_CONNECTED)
{
}
else
if (uError != ERROR_IO_PENDING)
{
}
else
{
/* Wait for the write to complete. */
if (GetOverlappedResult(pData->NamedPipe, &pData->OverlappedWrite, (DWORD *)&cbWritten, TRUE) == FALSE)
uError = GetLastError();
}
}
else
if (VBOX_FAILURE(rc))
{
|| rc == VERR_BROKEN_PIPE)
{
{
}
/* pretend success */
rc = VINF_SUCCESS;
}
cbWritten = 0;
}
}
#else /* !RT_OS_WINDOWS */
{
if (cbWritten == 0)
{
}
else if (cbWritten == -1)
{
cbWritten = 0;
}
}
#endif /* !RT_OS_WINDOWS */
return rc;
}
/**
* Queries an interface to the driver.
*
* @returns Pointer to interface.
* @returns NULL if the interface was not supported by the driver.
* @param pInterface Pointer to this interface structure.
* @param enmInterface The requested interface identification.
* @thread Any thread.
*/
static DECLCALLBACK(void *) drvNamedPipeQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
{
switch (enmInterface)
{
case PDMINTERFACE_BASE:
case PDMINTERFACE_STREAM:
default:
return NULL;
}
}
/* -=-=-=-=- listen thread -=-=-=-=- */
/**
* Receive thread loop.
*
* @returns 0 on success.
* @param ThreadSelf Thread handle to this thread.
* @param pvUser User argument.
*/
{
int rc = VINF_SUCCESS;
#ifdef RT_OS_WINDOWS
#endif
{
#ifdef RT_OS_WINDOWS
if ( !fConnected
{
if (hrc == ERROR_IO_PENDING)
{
hrc = 0;
hrc = GetLastError();
}
break;
if (hrc == ERROR_PIPE_CONNECTED)
{
}
else if (hrc != ERROR_SUCCESS)
{
break;
}
}
#else /* !RT_OS_WINDOWS */
{
break;
}
if (s == -1)
{
break;
}
else
{
{
close(s);
}
else
pData->LocalSocket = s;
}
#endif /* !RT_OS_WINDOWS */
}
#ifdef RT_OS_WINDOWS
#endif
return VINF_SUCCESS;
}
/**
* Construct a named pipe stream driver instance.
*
* @returns VBox status.
* @param pDrvIns The driver instance data.
* If the registration structure is needed, pDrvIns->pDrvReg points to it.
* @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
* of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
* iInstance it's expected to be used a bit in this function.
*/
{
int rc;
char *pszLocation = NULL;
/*
* Init the static parts.
*/
#ifdef RT_OS_WINDOWS
#else /* !RT_OS_WINDOWS */
#endif /* !RT_OS_WINDOWS */
/* IBase */
/* IStream */
/*
* Read the configuration.
*/
{
goto out;
}
if (VBOX_FAILURE(rc))
{
goto out;
}
bool fIsServer;
if (VBOX_FAILURE(rc))
{
goto out;
}
#ifdef RT_OS_WINDOWS
if (fIsServer)
{
HANDLE hPipe = CreateNamedPipe(pData->pszLocation, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, 32, 32, 10000, NULL);
if (hPipe == INVALID_HANDLE_VALUE)
{
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NamedPipe#%d failed to create named pipe %s"), pDrvIns->iInstance, pszLocation);
}
rc = RTThreadCreate(&pData->ListenThread, drvNamedPipeListenLoop, (void *)pData, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SerPipe");
if VBOX_FAILURE(rc)
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NamedPipe#%d failed to create listening thread"), pDrvIns->iInstance);
}
else
{
/* Connect to the named pipe. */
HANDLE hPipe = CreateFile(pData->pszLocation, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (hPipe == INVALID_HANDLE_VALUE)
{
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NamedPipe#%d failed to connect to named pipe %s"), pDrvIns->iInstance, pszLocation);
}
}
#else /* !RT_OS_WINDOWS */
int s;
struct sockaddr_un addr;
return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS, N_("NamedPipe#%d failed to create local socket"), pDrvIns->iInstance);
if (fIsServer)
{
/* Bind address to the local socket. */
return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS, N_("NamedPipe#%d failed to bind to local socket %s"), pDrvIns->iInstance, pszLocation);
pData->LocalSocketServer = s;
rc = RTThreadCreate(&pData->ListenThread, drvNamedPipeListenLoop, (void *)pData, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SerPipe");
if VBOX_FAILURE(rc)
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NamedPipe#%d failed to create listening thread"), pDrvIns->iInstance);
}
else
{
/* Connect to the local socket. */
return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS, N_("NamedPipe#%d failed to connect to local socket %s"), pDrvIns->iInstance, pszLocation);
pData->LocalSocket = s;
}
#endif /* !RT_OS_WINDOWS */
out:
if (VBOX_FAILURE(rc))
{
if (pszLocation)
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NamedPipe#%d failed to initialize"), pDrvIns->iInstance);
}
return VINF_SUCCESS;
}
/**
* Destruct a named pipe stream driver instance.
*
* Most VM resources are freed by the VM. This callback is provided so that
* any non-VM resources can be freed correctly.
*
* @param pDrvIns The driver instance data.
*/
{
if (pData->ListenThread)
{
}
if (pData->pszLocation)
}
/**
* Power off a named pipe stream driver instance.
*
* This does most of the destruction work, to avoid ordering dependencies.
*
* @param pDrvIns The driver instance data.
*/
{
#ifdef RT_OS_WINDOWS
{
{
}
}
{
/* Wake up listen thread */
}
#else /* !RT_OS_WINDOWS */
{
if (pData->pszLocation)
}
else
{
}
#endif /* !RT_OS_WINDOWS */
}
/**
* Named pipe driver registration record.
*/
const PDMDRVREG g_DrvNamedPipe =
{
/* u32Version */
/* szDriverName */
"NamedPipe",
/* pszDescription */
"Named Pipe stream driver.",
/* fFlags */
/* fClass. */
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(DRVNAMEDPIPE),
/* pfnConstruct */
/* pfnDestruct */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnDetach */
NULL,
/* pfnPowerOff */
};