tstGuestControlSvc.cpp revision 4d98012b3229f47c9fb4eb767563359484284077
/* $Id$ */
/** @file
*
* Testcase for the guest control service.
*/
/*
* Copyright (C) 2010 Oracle Corporation
*
* 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 *
*******************************************************************************/
#include <iprt/initterm.h>
#include "../gctrl.h"
using namespace guestControl;
char g_szImageName[RTPATH_MAX];
/** Prototypes. */
/** Simple call handle structure for the guest call completion callback */
struct VBOXHGCMCALLHANDLE_TYPEDEF
{
/** Where to store the result code */
};
/**
* For buffering process input supplied by the client.
*/
typedef struct TXSEXECSTDINBUF
{
/** The mount of buffered data. */
/** The current data offset. */
/** The data buffer. */
char *pch;
/** The amount of allocated buffer space. */
/** Send further input into the bit bucket (stdin is dead). */
bool fBitBucket;
/** The CRC-32 for standard input (received part). */
/** Pointer to a standard input buffer. */
typedef TXSEXECSTDINBUF *PTXSEXECSTDINBUF;
/** Call completion callback for guest calls. */
{
}
/**
* Initialise the HGCM service table as much as we need to start the
* service
* @param pTable the table to initialise
*/
{
}
/**
* Sets up the redirection / pipe / nothing for one of the standard handles.
*
* @returns IPRT status code. No client replies made.
* @param pszHowTo How to set up this standard handle.
* @param fd Which standard handle it is (0 == stdin, 1 ==
* stdout, 2 == stderr).
* @param ph The generic handle that @a pph may be set
* pointing to. Always set.
* @param pph Pointer to the RTProcCreateExec argument.
* Always set.
* @param phPipe Where to return the end of the pipe that we
* should service. Always set.
*/
{
*phPipe = NIL_RTPIPE;
int rc;
/*
* to represent the "other" end to phPipe.
*/
if (fd == 0) /* stdin? */
{
/* Connect a wrtie pipe specified by phPipe to stdin. */
}
else /* stdout or stderr? */
{
/* Connect a read pipe specified by phPipe to stdout or stderr. */
}
if (RT_FAILURE(rc))
return rc;
return rc;
}
/**
* Handle an error event on standard input.
*
* @param hPollSet The polling set.
* @param fPollEvt The event mask returned by RTPollNoResume.
* @param phStdInW The standard input pipe handle.
* @param pStdInBuf The standard input buffer.
*/
static void guestExecProcHandleStdInErrorEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, PRTPIPE phStdInW,
{
int rc2;
{
}
*phStdInW = NIL_RTPIPE;
pStdInBuf->cbAllocated = 0;
pStdInBuf->fBitBucket = true;
}
/**
* Try write some more data to the standard input of the child.
*
* @returns IPRT status code.
* @param pStdInBuf The standard input buffer.
* @param hStdInW The standard input pipe.
*/
{
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Handle an event indicating we can write to the standard input pipe of the
* child process.
*
* @param hPollSet The polling set.
* @param fPollEvt The event mask returned by RTPollNoResume.
* @param phStdInW The standard input pipe.
* @param pStdInBuf The standard input buffer.
*/
static void guestExecProcHandleStdInWritableEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, PRTPIPE phStdInW,
{
int rc;
if (!(fPollEvt & RTPOLL_EVT_ERROR))
{
{
/** @todo do we need to do something about this error condition? */
}
{
}
}
else
}
/**
* Handle pending output data or error on standard out, standard error or the
* test pipe.
*
* @returns IPRT status code from client send.
* @param hPollSet The polling set.
* @param fPollEvt The event mask returned by RTPollNoResume.
* @param phPipeR The pipe handle.
* @param uHandleId The handle ID.
* @param pszOpcode The opcode for the data upload.
*
* @todo Put the last 4 parameters into a struct!
*/
{
/*
* Try drain the pipe before acting on any errors.
*/
int rc = VINF_SUCCESS;
{
#if 1
#endif
/**puCrc32 = RTCrc32Process(*puCrc32, abBuf, cbRead);
Log(("cbRead=%#x Crc32=%#x \n", cbRead, *puCrc32));
Pkt.uCrc32 = RTCrc32Finish(*puCrc32);*/
/* if (g_fDisplayOutput)
{
if (enmHndId == TXSEXECHNDID_STDOUT)
RTStrmPrintf(g_pStdErr, "%.*s", cbRead, Pkt.abBuf);
else if (enmHndId == TXSEXECHNDID_STDERR)
RTStrmPrintf(g_pStdErr, "%.*s", cbRead, Pkt.abBuf);
}
rc = txsReplyInternal(&Pkt.Hdr, pszOpcode, cbRead + sizeof(uint32_t));*/
/* Make sure we go another poll round in case there was too much data
for the buffer to hold. */
}
else if (RT_FAILURE(rc2))
{
}
/*
* If an error was raised signalled,
*/
if (fPollEvt & RTPOLL_EVT_ERROR)
{
*phPipeR = NIL_RTPIPE;
}
return rc;
}
/**
* Handle a transport event or successful pfnPollIn() call.
*
* @returns IPRT status code from client send.
* @retval VINF_EOF indicates ABORT command.
*
* @param hPollSet The polling set.
* @param fPollEvt The event mask returned by RTPollNoResume.
* @param idPollHnd The handle ID.
* @param hStdInW The standard input pipe.
* @param pStdInBuf The standard input buffer.
*/
static int guestExecProcHandleTransportEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, uint32_t idPollHnd,
{
int rc = RTPollSetAddPipe(hPollSet, *phStdInW, RTPOLL_EVT_WRITE, 4 /*TXSEXECHNDID_STDIN_WRITABLE*/);
return rc;
}
{
int rc;
int rc2;
bool fProcessAlive = true;
bool fProcessTimedOut = false;
|| hStdOutR != NIL_RTPIPE
|| hStdErrR != NIL_RTPIPE;
? 100 /* need to poll for input */
: 1000; /* need only poll for process exit and aborts */
RTMSINTERVAL cMsPollCur = 0;
/*
* Before entering the loop, tell the host that we've started the guest
* and that it's now OK to send input to the process.
*/
rc = VINF_SUCCESS;
/*
* Process input, output, the test pipe and client requests.
*/
while (RT_SUCCESS(rc))
{
/*
*/
cMsPollCur = 0; /* no rest until we've checked everything. */
if (RT_SUCCESS(rc2))
{
switch (idPollHnd)
{
case 0 /* TXSEXECHNDID_STDIN */:
break;
case 1 /* TXSEXECHNDID_STDOUT */:
rc = guestExecProcHandleOutputEvent(hPollSet, fPollEvt, &hStdOutR, &uStdOutCrc32, 1 /* TXSEXECHNDID_STDOUT */);
break;
case 2 /*TXSEXECHNDID_STDERR */:
rc = guestExecProcHandleOutputEvent(hPollSet, fPollEvt, &hStdErrR, &uStdErrCrc32, 2 /*TXSEXECHNDID_STDERR */);
break;
case 4 /* TXSEXECHNDID_STDIN_WRITABLE */:
break;
default:
break;
}
break; /* abort command, or client dead or something */
continue;
}
/*
* Check for incoming data.
*/
/*
* Check for process death.
*/
if (fProcessAlive)
{
if (RT_SUCCESS_NP(rc2))
{
fProcessAlive = false;
continue;
}
continue;
{
fProcessAlive = false;
AssertFailed();
}
else
}
/*
* If the process has terminated, we're should head out.
*/
if (!fProcessAlive)
break;
/*
* Check for timed out, killing the process.
*/
if (cMillies != RT_INDEFINITE_WAIT)
{
if (cMsElapsed >= cMillies)
{
fProcessTimedOut = true;
if ( MsProcessKilled == UINT64_MAX
{
break; /* give up after 20 mins */
continue;
}
cMilliesLeft = 10000;
}
else
}
/* Reset the polling interval since we've done all pending work. */
}
/*
* Try kill the process if it's still alive at this point.
*/
if (fProcessAlive)
{
if (MsProcessKilled == UINT64_MAX)
{
RTThreadSleep(500);
}
for (size_t i = 0; i < 10; i++)
{
if (RT_SUCCESS(rc2))
{
fProcessAlive = false;
break;
}
if (i >= 5)
}
}
/*
* If we don't have a client problem (RT_FAILURE(rc) we'll reply to the
* clients exec packet now.
*/
if (RT_SUCCESS(rc))
{
{
}
{
}
/*else if (g_fTerminate && (fProcessAlive || MsProcessKilled != UINT64_MAX))
{
}*/
else if (fProcessAlive)
{
}
else if (MsProcessKilled != UINT64_MAX)
{
}
&& ProcessStatus.iStatus == 0)
{
}
{
}
{
}
{
}
else
{
}
}
return rc;
}
{
#if 0
/* Print some info */
RTPrintf("Flags: %u\n"
"# Args: %u\n"
"# Env: %u\n"
"StdIn: %s, StdOut: %s, StdErr: %s\n"
"User: %s, Timelimit: %u\n",
#endif
/*
* Create the environment.
*/
if (RT_SUCCESS(rc))
{
size_t i;
for (i = 0; i < cEnvVars; i++)
{
if (RT_FAILURE(rc))
break;
}
if (RT_SUCCESS(rc))
{
/*
* Setup the redirection of the standard stuff.
*/
/** @todo consider supporting: gcc stuff.c >file 2>&1. */
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/*
* Create a poll set for the pipes and let the
* transport layer add stuff to it as well.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
rc = RTPollSetAddPipe(hPollSet, hStdOutR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, 1 /* TXSEXECHNDID_STDOUT */);
if (RT_SUCCESS(rc))
rc = RTPollSetAddPipe(hPollSet, hStdErrR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, 2 /* TXSEXECHNDID_TESTPIPE */);
if (RT_SUCCESS(rc))
{
&hProcess);
if (RT_SUCCESS(rc))
{
/*
* Close the child ends of any pipes and redirected files.
*/
/*
* The handles that are no longer in the set have
* been closed by the above call in order to prevent
* the guest from getting stuck accessing them.
* So, NIL the handles to avoid closing them again.
*/
}
}
}
}
}
}
}
}
return rc;
}
int guestExecHandleCmdExecute(VBOXHGCMSVCFNTABLE *pTable, PVBOXHGCMSVCPARM paParms, uint32_t cParms)
{
char *pcBuf;
if(cParms < 13)
return VERR_INVALID_PARAMETER;
/* flags */
if (RT_SUCCESS(rc))
{
/* cmd */
/* arguments */
if ( pszExecName
&& RT_SUCCESS(rc))
{
/* argc */
/* argv */
char *pcData;
char **ppaArg;
int iArgs;
/* environment */
if (RT_SUCCESS(rc))
{
/* envc */
/* envv */
if (uEnvc)
{
uint32_t i = 0;
{
{
rc = VERR_NO_MEMORY;
break;
}
}
}
if (RT_SUCCESS(rc))
{
/* stdin */
if ( pszStdIn
&& RT_SUCCESS(rc))
{
/* stdout */
if ( pszStdOut
&& RT_SUCCESS(rc))
{
/* stderr */
if ( pszStdErr
&& RT_SUCCESS(rc))
{
/* user */
if ( pszUser
&& RT_SUCCESS(rc))
{
/* password */
if (RT_SUCCESS(rc))
{
/* time to wait (0 = max, no time limit) */
if (RT_SUCCESS(rc))
{
}
}
}
}
}
}
}
}
}
}
return rc;
}
/** Sends the current stdout (stderr later?) data to the host. */
{
int command = GUEST_EXEC_SEND_STDOUT;
|| cbStdOut == 0)
{
return VERR_INVALID_PARAMETER;
}
3, paParms);
int rc = VINF_SUCCESS;
{
}
return rc;
}
/** Gets a new message (command) from the host and does the appropriate action. */
{
int command = GUEST_GET_HOST_MSG;
/* Work around silly constant issues - we ought to allow passing
* constant strings in the hgcm parameters. */
13, paParms);
int rc;
{
}
else
{
/*
* Parameter 0 *always* specifies the actual command the host wants
* to execute.
*/
if (RT_SUCCESS(rc))
{
switch(uCmd)
{
case GETHOSTMSG_EXEC_CMD:
break;
case GETHOSTMSG_EXEC_STDIN:
//rc = guestExecHandleCmdStdIn(pTable, paParms, 12);
break;
default:
break;
}
}
}
return rc;
}
/** This represents the execution service thread in VBoxService. */
{
if(RT_FAILURE(rc))
return rc;
/* We don't remove the current request from the host queue yet,
* so don't try to get a new host message more than once atm. */
for(;;) /* Run forever atm. */
{
RTThreadSleep(1);
/** @tdo Add graceful shutdown here. */
}
return rc;
}
const char *pszCmd,
const void *pvArgs,
const void *pvEnv,
const char *pszStdin,
const char *pszStdOut,
const char *pszStdErr,
const char *pszUser,
const char *pszPassword,
{
int command = HOST_EXEC_CMD;
/** @todo Put some assert macros here! */
13, paParms);
if (RT_FAILURE(rc))
{
}
return rc;
}
{
int command = HOST_EXEC_GET_STATUS;
/** @todo Put some assert macros here! */
return 0;
}
/**
* Test the EXECUTE function.
* @returns iprt status value to indicate whether the test went as expected.
* @note prints its own diagnostic information to stdout.
*/
{
RTPrintf("Testing the EXECUTE call.\n");
{
RTPrintf("Invalid pfnHostCall() pointer\n");
return VERR_INVALID_POINTER;
}
int rc = VINF_SUCCESS;
/*
* The host code (= later Main?) will run in this thread,
* while the client (guest) code will run in another one (= VBoxService in guest).
*/
/* This is the host code. */
if(RT_SUCCESS(rc))
{
void *pvArgs;
/* Test stdout*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
456123,
cEnv,
"|",
"|",
"|",
"vbox",
"password",
}
}
if (RT_SUCCESS(rc))
{
for(;;)
{
RTThreadSleep(1);
}
}
/* Wait for guest thread to finish. */
int rc2;
if (RT_FAILURE(rc))
else if (RT_FAILURE(rc2))
}
return rc;
}
{
/*for (int i=0; i<10; i++)
{
RTStrmPrintf(g_pStdOut, "This is the i=%d to StdOut! Waiting ...\n", i+1);
RTStrmPrintf(g_pStdErr, "This is the i=%d to StdErr!\n", i+1);
RTThreadSleep(1000);
}*/
RTThreadSleep(5000);
return VINF_SUCCESS;
}
{
int rc = VINF_SUCCESS;
#if 0
/* Dump arguments and environment. */
if (RT_SUCCESS(rc))
{
}
#endif
/* Do the test(s) based on the handle number(s). */
switch (h)
{
case 0: /* stdin */
break;
case 1: /* stdout */
break;
case 2: /* stderr */
break;
case 3: /* all std* */
break;
}
return RTEXITCODE_SUCCESS;
}
/**
* Parses the arguments.
*
* @returns Exit code. Exit if this is non-zero or @a *pfExit is set.
* @param argc The number of arguments.
* @param argv The argument vector.
* @param pfExit For indicating exit when the exit code is zero.
*/
{
#if 1
for (int i=0; i<argc; i++)
#endif
if (argc == 2)
{
*pfExit = true;
RTPrintf("Unknown test! Exit.\n");
return RTEXITCODE_FAILURE;
}
return RTEXITCODE_SUCCESS;
}
{
if (RT_FAILURE(rc))
return RTMsgInitFailure(rc);
/* Save image name for later use. */
bool fExit = false;
return rc;
/* The function is inside the service, not HGCM. */
{
RTPrintf("Failed to start the HGCM service.\n");
return 1;
}
return 1;
RTPrintf("tstGuestControlSvc: SUCCEEDED.\n");
return 0;
}