GuestSessionImplTasks.cpp revision 11b175175a0ed424b8e8354acda681ad0adde0f8
/* $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 "ProgressImpl.h"
#include <memory> /* For auto_ptr. */
#ifdef LOG_GROUP
#endif
#define LOG_GROUP LOG_GROUP_GUEST_CONTROL
/*
* If the following define is enabled the Guest Additions update code also
* checks for locations which were not supposed to happen due to not resolving
* environment variables in VBoxService toolbox arguments. So a file copied
* to "%TEMP%\foo.exe" became "C:\Windows\EMPfoo.exe".
*/
#define VBOX_SERVICE_ENVARG_BUG
// session task classes
/////////////////////////////////////////////////////////////////////////////
{
}
GuestSessionTask::~GuestSessionTask(void)
{
}
{
return VINF_SUCCESS;
&& fCanceled)
return VERR_CANCELLED;
&& !fCompleted)
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))
{
LogRel(("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))
{
#ifdef VBOX_SERVICE_ENVARG_BUG
if (RT_FAILURE(rc))
{
/* Ugly hack: Because older Guest Additions have problems with environment variable
expansion in parameters we have to check an alternative location on Windows.
So check for "%TEMP%\" being "C:\\Windows\\system32\\EMP" actually. */
{
Utf8Str strFileDestBug = "C:\\Windows\\system32\\EMP" + strFileDest.substr(sizeof("%TEMP%\\") - sizeof(char));
}
}
#endif
if ( RT_SUCCESS(rc)
{
LogRel(("Guest Additions installer file \"%s\" successfully verified\n",
strFileDest.c_str()));
}
else
{
}
if (RT_SUCCESS(rc))
{
if (pcbSize)
*pcbSize = cbSizeOnGuest;
}
}
return rc;
}
{
#ifdef VBOX_SERVICE_ENVARG_BUG
if (RT_FAILURE(rc))
procInfo.mCommand = "C:\\Windows\\system32\\EMP" + procInfo.mCommand.substr(sizeof("%TEMP%\\") - sizeof(char));
#endif
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;
/*
* Determine guest OS type and the required installer image.
* At the moment only Windows guests are supported.
*/
{
{
strInstallerImage = "VBOXWINDOWSADDITIONS_AMD64.EXE";
else
strInstallerImage = "VBOXWINDOWSADDITIONS_X86.EXE";
/* Since the installers are located in the root directory,
* no further path processing needs to be done (yet). */
}
else /* Everything else is not supported (yet). */
{
Utf8StrFmt(GuestSession::tr("Detected guest OS (%s) does not support automatic Guest Additions updating, please update manually"),
osTypeIdUtf8.c_str()));
}
}
else
{
}
if (RT_SUCCESS(rc))
{
/*
* Try to open the .ISO file and locate the specified installer.
*/
if (RT_FAILURE(rc))
{
}
else
{
/** @todo Add support for non-Windows as well! */
bool fInstallCertificates = false;
if (RT_SUCCESS(rc))
{
/*
* Copy over main installer to the guest.
*/
if (RT_SUCCESS(rc))
/*
* Install needed certificates for the WHQL crap.
** @todo Only for Windows!
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
fInstallCertificates = true;
}
else
Utf8StrFmt(GuestSession::tr("Error while copying certificate installation tool to the guest: %Rrc"), rc));
}
}
else
}
}
/*
* Run VBoxCertUtil.exe to install the Oracle certificate.
*/
if ( RT_SUCCESS(rc)
{
LogRel(("Installing certificates on the guest ...\n"));
/* Construct arguments. */
/** @todo Remove hardcoded paths. */
/* Ugly hack: Because older Guest Additions have problems with environment variable
expansion in parameters we have to check an alternative location on Windows.
So check for "%TEMP%\VBoxWindowsAdditions.exe" in a screwed up way. */
#ifdef VBOX_SERVICE_ENVARG_BUG
if (RT_SUCCESS(rc))
#endif
#ifdef VBOX_SERVICE_ENVARG_BUG
else
#endif
/* Overwrite rc in any case. */
}
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
LogRel(("Updating Guest Additions ...\n"));
/* 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. */
/* Construct arguments. */
/* 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 (RT_SUCCESS(rc))
hr = setProgressSuccess();
}
RTIsoFsClose(&iso);
}
}
return rc;
}
{
LogFlowThisFunc(("strDesc=%s, strSource=%s, uFlags=%x\n",
"gctlUpGA");
return rc;
}
/* static */
{
}