GuestSessionImplTasks.cpp revision b730e944aef2382d2306995521834f0f13b6d7bc
/* $Id$ */
/** @file
* VirtualBox Main - Guest session tasks.
*/
/*
* Copyright (C) 2012-2013 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 0
/** Copy over the file from host to the
* guest. */
#define UPDATEFILE_FLAG_COPY_FROM_ISO RT_BIT(0)
/** 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;
}
{
LogFlowFunc(("hr=%Rhrc, strMsg=%s\n",
return hr; /* Return original rc. */
&& !fCanceled
&& !fCompleted)
{
return hr2;
}
return hr; /* Return original rc. */
}
{
}
SessionTaskOpen::~SessionTaskOpen(void)
{
}
{
/* Nothing to do here anymore. */
return vrc;
}
{
"gctlSesOpen");
return rc;
}
/* static */
{
}
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
{
rc = VINF_SUCCESS;
pFile = mSourceFile;
/* Size + offset are optional. */
}
/* 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_SUCCESS(rc))
{
&guestRc);
}
if (RT_FAILURE(rc))
{
switch (rc)
{
case VERR_GSTCTL_GUEST_ERROR:
break;
default:
Utf8StrFmt(GuestSession::tr("Error while creating guest process for copying file \"%s\" from guest to host: %Rrc"),
break;
}
}
if (RT_SUCCESS(rc))
{
uint64_t cbWrittenTotal = 0;
for (;;)
{
if ( RT_FAILURE(rc)
|| ( waitRes != ProcessWaitResult_StdIn
{
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))
{
switch (rc)
{
case VERR_GSTCTL_GUEST_ERROR:
break;
default:
break;
}
break;
}
/* Only subtract bytes reported written by the guest. */
/* Update total bytes written to the guest. */
LogFlowThisFunc(("rc=%Rrc, cbWritten=%RU32, cbToRead=%RU64, cbWrittenTotal=%RU64, cbFileSize=%RU64\n",
/* 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 */
LogFlowThisFunc(("Copy loop ended with rc=%Rrc, cbToRead=%RU64, cbWrittenTotal=%RU64, cbFileSize=%RU64\n",
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))
&guestRc);
if (RT_FAILURE(rc))
{
switch (rc)
{
case VERR_GSTCTL_GUEST_ERROR:
break;
default:
Utf8StrFmt(GuestSession::tr("Error while creating guest process for copying file \"%s\" from guest to host: %Rrc"),
break;
}
}
else
{
uint64_t cbWrittenTotal = 0;
for (;;)
{
if (RT_FAILURE(rc))
{
switch (rc)
{
case VERR_GSTCTL_GUEST_ERROR:
break;
default:
Utf8StrFmt(GuestSession::tr("Error while creating guest process for copying file \"%s\" from guest to host: %Rrc"),
break;
}
break;
}
if ( waitRes == ProcessWaitResult_StdOut
{
/* 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 (RT_FAILURE(rc))
{
switch (rc)
{
case VERR_GSTCTL_GUEST_ERROR:
break;
default:
break;
}
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
{
break;
}
} /* for */
LogFlowThisFunc(("rc=%Rrc, guestrc=%Rrc, waitRes=%ld, 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 */
{
}
const ProcessArguments &aArguments,
{
}
{
}
const ProcessArguments &aArgumentsSource)
{
int rc = VINF_SUCCESS;
try
{
/* Filter out arguments which already are in the destination to
* not end up having them specified twice. Not the fastest method on the
* planet but does the job. */
{
bool fFound = false;
{
{
fFound = true;
break;
}
itDest++;
}
if (!fFound)
itSource++;
}
}
{
return VERR_NO_MEMORY;
}
return rc;
}
{
/* 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))
{
LogRel(("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
{
{
LogRel(("Size of Guest Additions installer file \"%s\" does not match: %RI64 bytes copied, %RU64 bytes expected\n",
}
else
{
switch (rc)
{
case VERR_GSTCTL_GUEST_ERROR:
break;
default:
break;
}
}
}
if (RT_SUCCESS(rc))
{
if (pcbSize)
}
}
return rc;
}
int SessionTaskUpdateAdditions::runFileOnGuest(GuestSession *pSession, GuestProcessStartupInfo &procInfo)
{
if (RT_SUCCESS(vrc))
{
if (RT_SUCCESS(guestRc))
if (RT_SUCCESS(vrc))
}
if (RT_FAILURE(vrc))
{
switch (vrc)
{
case VERR_NOT_EQUAL: /** @todo Special guest control rc needed! */
break;
case VERR_GSTCTL_GUEST_ERROR:
break;
case VERR_INVALID_STATE: /** @todo Special guest control rc needed! */
break;
default:
break;
}
}
return vrc;
}
int SessionTaskUpdateAdditions::Run(void)
{
if (RT_FAILURE(rc))
return rc;
#if 0
/*
* Wait for the guest being ready within 30 seconds.
*/
{
{
rc = VERR_TIMEOUT;
break;
}
}
if (rc == VERR_TIMEOUT)
#else
/*
* For use with the GUI we don't want to wait, just return so that the manual .ISO mounting
* can continue.
*/
{
Utf8StrFmt(GuestSession::tr("Guest Additions are installed but not fully loaded yet, aborting automatic update")));
else
}
#endif
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()));
}
}
if (RT_SUCCESS(rc))
{
/*
* Determine guest OS type and the required installer image.
*/
if (RT_SUCCESS(rc))
{
{
/*
* Determine guest OS version.
*/
if (RT_FAILURE(rc))
{
}
/* Because Windows 2000 + XP and is bitching with WHQL popups even if we have signed drivers we
* can't do automated updates here. */
/* Windows XP 64-bit (5.2) is a Windows 2003 Server actually, so skip this here. */
if ( RT_SUCCESS(rc)
{
{
/* If we don't have AdditionsUpdateFlag_WaitForUpdateStartOnly set we can't continue
* because the Windows Guest Additions installer will fail because of WHQL popups. If the
* flag is set this update routine ends successfully as soon as the installer was started
* (and the user has to deal with it in the guest). */
{
Utf8StrFmt(GuestSession::tr("Windows 2000 and XP are not supported for automatic updating due to WHQL interaction, please update manually")));
}
}
}
else
{
Utf8StrFmt(GuestSession::tr("%s (%s) not supported for automatic updating, please update manually"),
}
}
{
}
else /* Everything else hopefully means Linux :-). */
#if 1 /* Only Windows is supported (and tested) at the moment. */
if ( RT_SUCCESS(rc)
&& osType != 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 (osType == 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 (osType == eOSType_Windows)
{
}
else
}
}
}
if (RT_SUCCESS(rc))
LogRel(("Guest Additions update directory is: %s\n",
strUpdateDir.c_str()));
/* Create the installation directory. */
int guestRc;
if (RT_FAILURE(rc))
{
switch (rc)
{
case VERR_GSTCTL_GUEST_ERROR:
break;
default:
break;
}
}
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 (osType)
{
case eOSType_Windows:
{
/* Do we need to install our certificates? We do this for W2K and up. */
bool fInstallCert = false;
/* Only Windows 2000 and up need certificates to be installed. */
{
fInstallCert = true;
LogRel(("Certificates for auto updating WHQL drivers will be installed\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. */
/* Set a running timeout of 5 minutes -- the Windows Guest Additions
* setup can take quite a while, so be on the safe side. */
/* 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. */
/* Add optional installer command line arguments from the API to the
* installer's startup info. */
/* 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();
}
}
}
if (RT_FAILURE(rc))
{
if (rc == VERR_CANCELLED)
{
LogRel(("Automatic update of Guest Additions was canceled\n"));
}
else
{
{
if ( errorInfo.isFullAvailable()
|| errorInfo.isBasicAvailable())
{
}
}
LogRel(("Automatic update of Guest Additions failed: %s (%Rhrc)\n",
}
LogRel(("Please install Guest Additions manually\n"));
}
/** @todo Clean up copied / left over installation files. */
return rc;
}
{
LogFlowThisFunc(("strDesc=%s, strSource=%s, uFlags=%x\n",
"gctlUpGA");
return rc;
}
/* static */
{
}