VBoxManageGuestCtrl.cpp revision a15799ec68e8dc3bb9141ad4567500d41b1df527
/* $Id$ */
/** @file
* VBoxManage - Implementation of guestcontrol command.
*/
/*
* 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 "VBoxManage.h"
#ifndef VBOX_ONLY_DOCS
#ifdef USE_XPCOM_QUEUE
# include <errno.h>
#endif
#include <signal.h>
#ifdef RT_OS_DARWIN
# include <CoreFoundation/CFRunLoop.h>
#endif
using namespace com;
/**
* IVirtualBoxCallback implementation for handling the GuestControlCallback in
* relation to the "guestcontrol * wait" command.
*/
/** @todo */
/** Set by the signal handler. */
static volatile bool g_fExecCanceled = false;
#endif /* VBOX_ONLY_DOCS */
{
"VBoxManage guestcontrol execute <vmname>|<uuid>\n"
" <path to program>\n"
" --username <name> --password <password>\n"
" [--arguments \"<arguments>\"]\n"
" [--environment \"<NAME>=<VALUE> [<NAME>=<VALUE>]\"]\n"
" [--flags <flags>] [--timeout <msec>]\n"
" [--verbose] [--wait-for exit,stdout,stderr||]\n"
"\n");
}
#ifndef VBOX_ONLY_DOCS
/**
* Signal handler that sets g_fCanceled.
*
* This can be executed on any thread in the process, on Windows it may even be
* a thread dedicated to delivering this signal. Do not doing anything
* unnecessary here.
*/
static void execProcessSignalHandler(int iSignal)
{
ASMAtomicWriteBool(&g_fExecCanceled, true);
}
{
switch (uStatus)
{
case guestControl::PROC_STS_STARTED:
return "started";
case guestControl::PROC_STS_TEN:
return "successfully terminated";
case guestControl::PROC_STS_TES:
return "terminated by signal";
case guestControl::PROC_STS_TEA:
return "abnormally aborted";
case guestControl::PROC_STS_TOK:
return "timed out";
case guestControl::PROC_STS_TOA:
return "timed out, hanging";
case guestControl::PROC_STS_DWN:
return "killed";
case guestControl::PROC_STS_ERROR:
return "error";
default:
return "unknown";
}
}
static int handleExecProgram(HandlerArg *a)
{
/*
* Check the syntax. We can deduce the correct syntax from the number of
* arguments.
*/
uint32_t u32TimeoutMS = 0;
bool fWaitForExit = false;
bool fWaitForStdOut = false;
bool fWaitForStdErr = false;
bool fVerbose = false;
bool fTimeout = false;
/* Always use the actual command line as argv[0]. */
/* Iterate through all possible commands (if available). */
bool usageOK = true;
{
{
if (i + 1 >= a->argc)
usageOK = false;
else
{
char **papszArg;
int cArgs;
if (RT_SUCCESS(vrc))
{
for (int j = 0; j < cArgs; j++)
}
++i;
}
}
{
if (i + 1 >= a->argc)
usageOK = false;
else
{
char **papszArg;
int cArgs;
if (RT_SUCCESS(vrc))
{
for (int j = 0; j < cArgs; j++)
}
++i;
}
}
{
if ( i + 1 >= a->argc
usageOK = false;
else
++i;
}
{
if (i + 1 >= a->argc)
usageOK = false;
else
{
++i;
}
}
{
if (i + 1 >= a->argc)
usageOK = false;
else
{
++i;
}
}
{
if ( i + 1 >= a->argc
|| u32TimeoutMS == 0)
{
usageOK = false;
}
else
{
fTimeout = true;
++i;
}
}
{
if (i + 1 >= a->argc)
usageOK = false;
else
{
fWaitForExit = true;
{
fWaitForExit = true;
fWaitForStdOut = true;
}
{
fWaitForExit = true;
fWaitForStdErr = true;
}
else
usageOK = false;
++i;
}
}
fVerbose = true;
/** @todo Add fancy piping stuff here. */
else
return errorSyntax(USAGE_GUESTCONTROL,
}
if (!usageOK)
if (Utf8UserName.isEmpty())
return errorSyntax(USAGE_GUESTCONTROL,
"No user name specified!");
/* lookup VM. */
/* assume it's an UUID */
machine.asOutParam());
{
/* must be a name */
machine.asOutParam()));
}
if (machine)
{
do
{
/* open an existing session for VM */
// @todo r=dj assert that it's an existing session
/* get the mutable session machine */
/* get the associated console */
if (fVerbose)
{
if (u32TimeoutMS == 0)
RTPrintf("Waiting for guest to start process ...\n");
else
}
/* Get current time stamp to later calculate rest of timeout left. */
/* Execute the process. */
{
/* If we got a VBOX_E_IPRT error we handle the error in a more gentle way
* because it contains more accurate info about what went wrong. */
if (info.isFullAvailable())
{
if (rc == VBOX_E_IPRT_ERROR)
else
}
break;
}
if (fVerbose)
if (fWaitForExit)
{
if (fTimeout)
{
/* Calculate timeout value left after process has been started. */
/* Is timeout still bigger than current difference? */
if (u32TimeoutMS > u64Elapsed)
{
if (fVerbose)
}
else
{
if (fVerbose)
RTPrintf("No time left to wait for process!\n");
}
}
else if (fVerbose)
RTPrintf("Waiting for process to exit ...\n");
/* setup signal handling if cancelable */
bool fCanceledAlready = false;
fCancelable = FALSE;
if (fCancelable)
{
#ifdef SIGBREAK
#endif
}
/* Wait for process to exit ... */
{
ULONG cbOutputData = 0;
/*
* Some data left to output?
*/
if ( fWaitForStdOut
|| fWaitForStdErr)
{
{
/* If we got a VBOX_E_IPRT error we handle the error in a more gentle way
* because it contains more accurate info about what went wrong. */
if (info.isFullAvailable())
{
if (rc == VBOX_E_IPRT_ERROR)
else
}
cbOutputData = 0;
}
else
{
if (cbOutputData > 0)
{
/* aOutputData has a platform dependent line ending, standardize on
s++, d++)
{
if (*s == '\r')
{
/* skip over CR, adjust destination */
d--;
}
else if (s != d)
*d = *s;
}
}
}
}
if (cbOutputData <= 0) /* No more output data left? */
{
if (fCompleted)
break;
if ( fTimeout
{
break;
}
}
/* Process async cancelation */
if (g_fExecCanceled && !fCanceledAlready)
{
else
g_fExecCanceled = false;
}
/* Progress canceled by Main API? */
&& fCanceled)
{
break;
}
}
/* Undo signal handling */
if (fCancelable)
{
#ifdef SIGBREAK
#endif
}
if (fCanceled)
{
if (fVerbose)
RTPrintf("Process execution canceled!\n");
}
else if ( fCompleted
{
{
{
/* If we got a VBOX_E_IPRT error we handle the error in a more gentle way
* because it contains more accurate info about what went wrong. */
else
{
RTMsgError("Process error details:");
}
}
else
AssertMsgFailed(("Full error description is missing!\n"));
}
else if (fVerbose)
{
RTPrintf("Exit code=%u (Status=%u [%s], Flags=%u)\n", uRetExitCode, uRetStatus, getStatus(uRetStatus), uRetFlags);
}
}
else
{
if (fVerbose)
RTPrintf("Process execution aborted!\n");
}
}
a->session->UnlockMachine();
} while (0);
}
}
/**
* Access the guest control store.
*
* @returns 0 on success, 1 on failure
* @note see the command line API description for parameters
*/
int handleGuestControl(HandlerArg *a)
{
HandlerArg arg = *a;
if (a->argc == 0)
/* switch (cmd) */
return handleExecProgram(&arg);
/* default: */
}
#endif /* !VBOX_ONLY_DOCS */