GuestSessionImplTasks.cpp revision c89333d3e41e439ed9e74768000edc399d3e72e6
/* $Id$ */
/** @file
* VirtualBox Main - XXX.
*/
/*
* Copyright (C) 2012 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 "GuestImpl.h"
#include "GuestSessionImpl.h"
#include "GuestCtrlImplPrivate.h"
#include "Global.h"
#include "AutoCaller.h"
#include "ConsoleImpl.h"
#include "MachineImpl.h"
#include "ProgressImpl.h"
#include <memory> /* For auto_ptr. */
#ifdef LOG_GROUP
#endif
#define LOG_GROUP LOG_GROUP_GUEST_CONTROL
/*******************************************************************************
* Defines *
*******************************************************************************/
/**
* Update file flags.
*/
#define UPDATEFILE_FLAG_NONE RT_BIT(0)
/** Copy over the file from host to the
* guest. */
/** Execute file on the guest after it has
* been successfully transfered. */
/** File is optional, does not have to be
* existent on the .ISO. */
// session task classes
/////////////////////////////////////////////////////////////////////////////
{
}
GuestSessionTask::~GuestSessionTask(void)
{
}
{
{
return VINF_SUCCESS;
}
return VERR_NOT_FOUND;
}
{
return VINF_SUCCESS;
&& fCanceled)
return VERR_CANCELLED;
&& fCompleted)
{
AssertMsgFailed(("Setting value of an already completed progress\n"));
return VINF_SUCCESS;
}
return VERR_COM_UNEXPECTED;
return VINF_SUCCESS;
}
int GuestSessionTask::setProgressSuccess(void)
{
return VINF_SUCCESS;
&& !fCanceled
&& !fCompleted)
{
return VERR_COM_UNEXPECTED; /** @todo Find a better rc. */
}
return VINF_SUCCESS;
}
{
return hr; /* Return original rc. */
&& !fCanceled
&& !fCompleted)
{
return hr2;
}
return hr; /* Return original rc. */
}
mSourceOffset(0),
mSourceSize(0),
{
}
/** @todo Merge this and the above call and let the above call do the open/close file handling so that the
* inner code only has to deal with file handles. No time now ... */
{
}
SessionTaskCopyTo::~SessionTaskCopyTo(void)
{
}
int SessionTaskCopyTo::Run(void)
{
if (mCopyFileFlags)
{
return VERR_INVALID_PARAMETER;
}
int rc;
if (!mSourceFile)
{
/* Does our source file exist? */
{
}
else
{
if (RT_FAILURE(rc))
{
}
else
{
if (RT_FAILURE(rc))
{
}
}
}
}
else
{
pFile = mSourceFile;
/* Size + offset are optional. */
}
procInfo.mName = Utf8StrFmt(GuestSession::tr("Copying file \"%s\" to the guest to \"%s\" (%RU64 bytes)"),
/* Set arguments.*/
procInfo.mArguments.push_back(Utf8StrFmt("--output=%s", mDest.c_str())); /** @todo Do we need path conversion? */
/* Startup process. */
if (RT_SUCCESS(rc))
if (RT_FAILURE(rc))
{
}
else
{
uint64_t cbWrittenTotal = 0;
for (;;)
{
if ( RT_FAILURE(rc)
{
break;
}
/* If the guest does not support waiting for stdin, we now yield in
* order to reduce the CPU load due to busy waiting. */
RTThreadYield(); /* Optional, don't check rc. */
if (mSourceSize) /* If we have nothing to write, take a shortcut. */
{
/** @todo Not very efficient, but works for now. */
if (RT_SUCCESS(rc))
{
/*
* Some other error occured? There might be a chance that RTFileRead
* print a generic error.
*/
if (RT_FAILURE(rc))
{
break;
}
}
else
{
break;
}
}
/* Did we reach the end of the content we want to transfer (last chunk)? */
/* Did we reach the last block which is exactly _64K? */
/* ... or does the user want to cancel? */
&& fCanceled)
)
{
}
if (RT_FAILURE(rc))
{
break;
}
LogFlowThisFunc(("cbWritten=%RU32, cbToRead=%RU64, cbWrittenTotal=%RU64, cbFileSize=%RU64\n",
/* Only subtract bytes reported written by the guest. */
/* Update total bytes written to the guest. */
/* Did the user cancel the operation above? */
if (fCanceled)
break;
/* Update the progress.
* Watch out for division by zero. */
mSourceSize > 0
if (RT_FAILURE(rc))
break;
/* End of file reached? */
if (!cbToRead)
break;
} /* for */
if ( !fCanceled
|| RT_SUCCESS(rc))
{
/*
* Even if we succeeded until here make sure to check whether we really transfered
* everything.
*/
if ( mSourceSize > 0
&& cbWrittenTotal == 0)
{
/* If nothing was transfered but the file size was > 0 then "vbox_cat" wasn't able to write
* to the destination -> access denied. */
}
else if (cbWrittenTotal < mSourceSize)
{
/* If we did not copy all let the user know. */
}
else
{
if ( RT_FAILURE(rc)
{
if (RT_FAILURE(rc))
else
Utf8StrFmt(GuestSession::tr("Waiting on termination for copying file \"%s\" failed with wait result %ld"),
}
if (RT_SUCCESS(rc))
{
&& exitCode != 0)
)
{
}
}
if (RT_SUCCESS(rc))
rc = setProgressSuccess();
}
}
} /* processCreateExInteral */
if (!mSourceFile) /* Only close locally opened files. */
RTFileClose(*pFile);
return rc;
}
{
LogFlowThisFunc(("strDesc=%s, strSource=%s, strDest=%s, mCopyFileFlags=%x\n",
"gctlCpyTo");
return rc;
}
/* static */
{
}
{
}
{
}
int SessionTaskCopyFrom::Run(void)
{
/*
* Note: There will be races between querying file size + reading the guest file's
* content because we currently *do not* lock down the guest file when doing the
* actual operations.
** @todo Implement guest file locking!
*/
if (RT_FAILURE(rc))
{
}
{
}
if (RT_SUCCESS(rc))
{
RTFILE_O_WRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_WRITE); /** @todo Use the correct open modes! */
if (RT_FAILURE(rc))
{
}
else
{
procInfo.mName = Utf8StrFmt(GuestSession::tr("Copying file \"%s\" from guest to the host to \"%s\" (%RI64 bytes)"),
/* Set arguments.*/
/* Startup process. */
if (RT_SUCCESS(rc))
if (RT_FAILURE(rc))
{
Utf8StrFmt(GuestSession::tr("Unable to start guest process for copying data from guest to host: %Rrc"), rc));
}
else
{
uint64_t cbWrittenTotal = 0;
for (;;)
{
{
/* If the guest does not support waiting for stdin, we now yield in
* order to reduce the CPU load due to busy waiting. */
RTThreadYield(); /* Optional, don't check rc. */
&cbRead);
if (RT_FAILURE(rc))
{
break;
}
if (cbRead)
{
if (RT_FAILURE(rc))
{
break;
}
/* Only subtract bytes reported written by the guest. */
/* Update total bytes written to the guest. */
cbWrittenTotal += cbRead;
/* Did the user cancel the operation above? */
&& fCanceled)
break;
if (RT_FAILURE(rc))
break;
}
}
else if ( RT_FAILURE(rc)
{
break;
}
} /* for */
LogFlowThisFunc(("rc=%Rrc, cbWrittenTotal=%RU64, cbSize=%RI64, cbToRead=%RU64\n",
if ( !fCanceled
|| RT_SUCCESS(rc))
{
/*
* Even if we succeeded until here make sure to check whether we really transfered
* everything.
*/
if ( objData.mObjectSize > 0
&& cbWrittenTotal == 0)
{
/* If nothing was transfered but the file size was > 0 then "vbox_cat" wasn't able to write
* to the destination -> access denied. */
}
{
/* If we did not copy all let the user know. */
}
else
{
&& exitCode != 0)
)
{
}
else /* Yay, success! */
rc = setProgressSuccess();
}
}
}
}
}
return rc;
}
{
LogFlowThisFunc(("strDesc=%s, strSource=%s, strDest=%s, uFlags=%x\n",
"gctlCpyFrom");
return rc;
}
/* static */
{
}
{
}
{
}
{
/* pcbSize is optional. */
if (RT_FAILURE(rc))
{
if (fOptional)
return VINF_SUCCESS;
return rc;
}
/* Copy over the Guest Additions file to the guest. */
if (RT_SUCCESS(rc))
{
LogFlowThisFunc(("Copying Guest Additions installer file \"%s\" to \"%s\" on guest ...\n",
if (RT_SUCCESS(rc))
{
rc = pSession->startTaskAsync(Utf8StrFmt(GuestSession::tr("Copying Guest Additions installer file \"%s\" to \"%s\" on guest"),
if (RT_SUCCESS(rc))
{
&& fCanceled)
{
}
{
}
}
}
}
/** @todo Note: Since there is no file locking involved at the moment, there can be modifications
* between finished copying, the verification and the actual execution. */
/* Determine where the installer image ended up and if it has the correct size. */
if (RT_SUCCESS(rc))
{
LogFlowThisFunc(("Verifying Guest Additions installer file \"%s\" ...\n",
strFileDest.c_str()));
if ( RT_SUCCESS(rc)
{
LogFlowThisFunc(("Guest Additions installer file \"%s\" successfully verified\n",
strFileDest.c_str()));
}
else
{
{
LogFlowThisFunc(("Size of Guest Additions installer file \"%s\" does not match: %RI64bytes copied, %RU64bytes expected\n",
}
else
LogFlowThisFunc(("Error copying Guest Additions installer file \"%s\": %Rrc\n",
}
if (RT_SUCCESS(rc))
{
if (pcbSize)
*pcbSize = cbSizeOnGuest;
}
}
return rc;
}
int SessionTaskUpdateAdditions::runFileOnGuest(GuestSession *pSession, GuestProcessStartupInfo &procInfo)
{
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
{
&& exitCode != 0)
)
{
}
else /* Yay, success! */
{
}
}
else
{
if (RT_FAILURE(rc))
else
{
}
}
}
return rc;
}
int SessionTaskUpdateAdditions::Run(void)
{
if (RT_FAILURE(rc))
return rc;
/*
* Wait for the guest being ready within 30 seconds.
*/
{
{
rc = VERR_TIMEOUT;
break;
}
}
if (rc == VERR_TIMEOUT)
if (RT_SUCCESS(rc))
{
/*
* Determine if we are able to update automatically. This only works
* if there are recent Guest Additions installed already.
*/
if ( RT_SUCCESS(rc)
{
Utf8StrFmt(GuestSession::tr("Guest has too old Guest Additions (%s) installed for automatic updating, please update manually"),
strAddsVer.c_str()));
}
/*
* Determine guest OS type and the required installer image.
*/
if (RT_SUCCESS(rc))
{
{
}
{
}
else /* Everything else hopefully means Linux :-). */
#if 1 /* Only Windows is supported (and tested) at the moment. */
if (eOSType != eOSType_Windows)
{
Utf8StrFmt(GuestSession::tr("Detected guest OS (%s) does not support automatic Guest Additions updating, please update manually"),
}
#endif
}
}
if (RT_SUCCESS(rc))
{
/*
* Try to open the .ISO file to extract all needed files.
*/
if (RT_FAILURE(rc))
{
}
else
{
/* Set default installation directories. */
if (eOSType == eOSType_Windows)
strUpdateDir = "C:\\Temp\\";
/* Try looking up the Guest Additions installation directory. */
if (RT_SUCCESS(rc))
{
/* Try getting the installed Guest Additions version to know whether we
* can install our temporary Guest Addition data into the original installation
* directory.
*
* Because versions prior to 4.2 had bugs wrt spaces in paths we have to choose
* a different location then.
*/
bool fUseInstallDir = false;
if ( RT_SUCCESS(rc)
{
fUseInstallDir = true;
}
if (fUseInstallDir)
{
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
if (eOSType == eOSType_Windows)
{
}
else
}
}
}
if (RT_SUCCESS(rc))
LogRel(("Guest Additions update directory is: %s\n",
strUpdateDir.c_str()));
/* Create the installation directory. */
if (RT_FAILURE(rc))
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
/* Prepare the file(s) we want to copy over to the guest and
* (maybe) want to run. */
switch (eOSType)
{
case eOSType_Windows:
{
/* Do we need to install our certificates? We do this for W2K and up. */
bool fInstallCert = false;
if ( RT_SUCCESS(rc)
{
fInstallCert = true;
LogRel(("Certificates for auto updating WHQL drivers will be installed\n"));
}
else if (RT_FAILURE(rc))
{
/* Unknown (or unhandled) Windows OS. */
fInstallCert = true;
LogRel(("Unknown guest Windows version detected (%s), installing certificates for WHQL drivers\n",
}
else
LogRel(("Skipping installation of certificates for WHQL drivers\n"));
if (fInstallCert)
{
/* Our certificate. */
strUpdateDir + "oracle-vbox.cer",
/* Our certificate installation utility. */
/* First pass: Copy over the file + execute it to remove any existing
* VBox certificates. */
strUpdateDir + "VBoxCertUtil.exe",
/* Second pass: Only execute (but don't copy) again, this time installng the
* recent certificates just copied over. */
strUpdateDir + "VBoxCertUtil.exe",
}
/* The installers in different flavors, as we don't know (and can't assume)
* the guest's bitness. */
strUpdateDir + "VBoxWindowsAdditions-x86.exe",
strUpdateDir + "VBoxWindowsAdditions-amd64.exe",
/* The stub loader which decides which flavor to run. */
/* Don't quit VBoxService during upgrade because it still is used for this
* piece of code we're in right now (that is, here!) ... */
/* Tell the installer to report its current installation status
* using a running VBoxTray instance via balloon messages in the
* Windows taskbar. */
/* If the caller does not want to wait for out guest update process to end,
* complete the progress object now so that the caller can do other work. */
strUpdateDir + "VBoxWindowsAdditions.exe",
break;
}
case eOSType_Linux:
/** @todo Add Linux support. */
break;
case eOSType_Solaris:
/** @todo Add Solaris support. */
break;
default:
break;
}
}
if (RT_SUCCESS(rc))
{
/* We want to spend 40% total for all copying operations. So roughly
* calculate the specific percentage step of each copied file. */
LogRel(("Copying over Guest Additions update files to the guest ...\n"));
{
{
bool fOptional = false;
fOptional = true;
if (RT_FAILURE(rc))
{
break;
}
}
if (RT_FAILURE(rc))
break;
itFiles++;
}
}
/* Done copying, close .ISO file. */
RTIsoFsClose(&iso);
if (RT_SUCCESS(rc))
{
/* We want to spend 35% total for all copying operations. So roughly
* calculate the specific percentage step of each copied file. */
LogRel(("Executing Guest Additions update files ...\n"));
{
{
if (RT_FAILURE(rc))
{
break;
}
}
if (RT_FAILURE(rc))
break;
itFiles++;
}
}
if (RT_SUCCESS(rc))
{
LogRel(("Automatic update of Guest Additions succeeded\n"));
rc = setProgressSuccess();
}
else if (rc == VERR_CANCELLED)
{
LogRel(("Automatic update of Guest Additions canceled\n"));
}
else
{
{
{
&& strVal.isNotEmpty())
}
}
LogRel(("Please install Guest Additions manually\n"));
}
}
}
return rc;
}
{
LogFlowThisFunc(("strDesc=%s, strSource=%s, uFlags=%x\n",
"gctlUpGA");
return rc;
}
/* static */
{
}