SessionImpl.cpp revision afcb8bcffe77eed97b80054d17d877cb467e6344
/* $Id$ */
/** @file
* VBox Client Session COM Class implementation in VBoxC.
*/
/*
* Copyright (C) 2006-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.
*/
#include "SessionImpl.h"
#include "ConsoleImpl.h"
#include "Global.h"
#include "ClientTokenHolder.h"
#include "AutoCaller.h"
#include "Logging.h"
/**
* Local macro to check whether the session is open and return an error if not.
* @note Don't forget to do |Auto[Reader]Lock alock (this);| before using this
* macro.
*/
#define CHECK_OPEN() \
do { \
if (mState != SessionState_Locked) \
return setError(E_UNEXPECTED, tr ("The session is not locked (session state: %s)"), Global::stringifySessionState(mState)); \
} while (0)
// constructor / destructor
/////////////////////////////////////////////////////////////////////////////
{
LogFlowThisFunc(("\n"));
return rc;
}
void Session::FinalRelease()
{
LogFlowThisFunc(("\n"));
uninit();
}
// public initializer/uninitializer for internal purposes only
/////////////////////////////////////////////////////////////////////////////
/**
* Initializes the Session object.
*/
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan(this);
/* Confirm a successful initialization when it's the case */
return S_OK;
}
/**
* Uninitializes the Session object.
*
* @note Locks this object for writing.
*/
{
/* Enclose the state transition Ready->InUninit->NotReady */
AutoUninitSpan autoUninitSpan(this);
if (autoUninitSpan.uninitDone())
{
LogFlowThisFunc(("Already uninitialized.\n"));
return;
}
/* close() needs write lock */
if (mState != SessionState_Unlocked)
{
}
}
// ISession properties
/////////////////////////////////////////////////////////////////////////////
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
CHECK_OPEN();
return S_OK;
}
{
AutoCaller autoCaller(this);
CHECK_OPEN();
#ifndef VBOX_COM_INPROC_API_CLIENT
if (mConsole)
else
#endif
{
/** @todo VBox 3.3: replace E_FAIL with rc here. */
#ifndef VBOX_COM_INPROC_API_CLIENT
if (mConsole)
else
#endif
if (FAILED_DEAD_INTERFACE(rc))
else
}
return rc;
}
{
AutoCaller autoCaller(this);
CHECK_OPEN();
#ifndef VBOX_COM_INPROC_API_CLIENT
if (mConsole)
else
#endif
{
/** @todo VBox 3.3: replace E_FAIL with rc here. */
#ifndef VBOX_COM_INPROC_API_CLIENT
if (mConsole)
else
#endif
if (FAILED_DEAD_INTERFACE(rc))
else
}
return rc;
}
// ISession methods
/////////////////////////////////////////////////////////////////////////////
{
AutoCaller autoCaller(this);
/* close() needs write lock */
CHECK_OPEN();
}
// IInternalSessionControl methods
/////////////////////////////////////////////////////////////////////////////
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
("This is not a direct session!\n"),
/* return a failure if the session already transitioned to Closing
* but the server hasn't processed Machine::OnSessionEnd() yet. */
if (mState != SessionState_Locked)
return VBOX_E_INVALID_VM_STATE;
return S_OK;
#else /* VBOX_COM_INPROC_API_CLIENT */
AssertFailed();
return VBOX_E_INVALID_OBJECT_STATE;
#endif /* VBOX_COM_INPROC_API_CLIENT */
}
#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
{
AutoCaller autoCaller(this);
if (!aMachine)
{
/*
* A special case: the server informs us that this session has been
* passed to IMachine::launchVMProcess() so this session will become
* remote (but not existing) when AssignRemoteMachine() is called.
*/
return S_OK;
}
/* query IInternalMachineControl interface */
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
#endif
#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
/* create the machine client token */
try
{
#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
if (!mClientTokenHolder->isReady())
{
delete mClientTokenHolder;
}
}
{
rc = E_OUTOFMEMORY;
}
/*
* Reference the VirtualBox object to ensure the server is up
* until the session is closed
*/
{
}
else
{
/* some cleanup */
#ifndef VBOX_COM_INPROC_API_CLIENT
{
}
#endif
}
return rc;
}
{
AutoCaller autoCaller(this);
/* query IInternalMachineControl interface */
/// @todo (dmik)
// currently, the remote session returns the same machine and
// console objects as the direct session, thus giving the
// (remote) client full control over the direct session. For the
// console, it is the desired behavior (the ability to control
// VM execution is a must for the remote session). What about
// the machine object, we may want to prevent the remote client
// from modifying machine data. In this case, we must:
// 1) assign the Machine object (instead of the SessionMachine
// object that is passed to this method) to mRemoteMachine;
// 2) remove GetMachine() property from the IConsole interface
// because it always returns the SessionMachine object
// (alternatively, we can supply a separate IConsole
// implementation that will return the Machine object in
// response to GetMachine()).
/*
* Reference the VirtualBox object to ensure the server is up
* until the session is closed
*/
{
/*
* RemoteSession type can be already set by AssignMachine() when its
* argument is NULL (a special case)
*/
if (mType != SessionType_Remote)
else
}
else
{
/* some cleanup */
}
return rc;
}
{
AutoCaller autoCaller(this);
{
/*
* We might have already entered Session::uninit() at this point, so
* return silently (not interested in the state change during uninit)
*/
LogFlowThisFunc(("Already uninitialized.\n"));
return S_OK;
}
if (mState == SessionState_Unlocking)
{
LogFlowThisFunc(("Already being unlocked.\n"));
return S_OK;
}
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
AutoCaller autoCaller(this);
{
/* close() needs write lock */
if (mState == SessionState_Unlocking)
{
LogFlowThisFunc(("Already being unlocked.\n"));
return S_OK;
}
|| mState == SessionState_Spawning,
("Session is in wrong state (%ld), expected locked (%ld) or spawning (%ld)\n",
/* close ourselves */
}
{
/*
* We might have already entered Session::uninit() at this point,
* return silently
*/
LogFlowThisFunc(("Already uninitialized.\n"));
}
else
{
LogWarningThisFunc(("UNEXPECTED uninitialization!\n"));
}
return rc;
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
return mConsole->onStorageControllerChange();
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
return mConsole->onVideoCaptureChange();
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
return mConsole->onUSBControllerChange();
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#endif
if (mState != SessionState_Locked)
{
/* the call from Machine issued when the session is open can arrive
* after the session starts closing or gets closed. Note that when
* aCheck is false, we return E_FAIL to indicate that aWinId we return
* is not valid */
*aWinId = 0;
}
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
STDMETHODIMP Session::OnStorageDeviceChange(IMediumAttachment *aMediumAttachment, BOOL aRemove, BOOL aSilent)
{
LogFlowThisFunc(("\n"));
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
return S_OK;
#endif
}
{
#ifdef VBOX_WITH_GUEST_PROPS
# ifndef VBOX_COM_INPROC_API_CLIENT
AutoCaller autoCaller(this);
if (mState != SessionState_Locked)
return setError(VBOX_E_INVALID_VM_STATE,
tr("Machine is not locked by session (session state: %s)."),
return E_POINTER;
return E_POINTER;
return E_POINTER;
/* aValue can be NULL for a setter call if the property is to be deleted. */
/* aFlags can be null if it is to be left as is */
/* If this session is not in a VM process fend off the call. The caller
* handles this correctly, by doing the operation in VBoxSVC. */
if (!mConsole)
return E_ACCESSDENIED;
if (!aIsSetter)
else
# else /* VBOX_COM_INPROC_API_CLIENT */
/** @todo This is nonsense, non-VM API users shouldn't need to deal with this
* method call, VBoxSVC should be clever enough to see that the
* session doesn't have a console! */
return E_ACCESSDENIED;
# endif /* VBOX_COM_INPROC_API_CLIENT */
#else /* VBOX_WITH_GUEST_PROPS */
#endif /* VBOX_WITH_GUEST_PROPS */
}
{
#if defined(VBOX_WITH_GUEST_PROPS) && !defined(VBOX_COM_INPROC_API_CLIENT)
AutoCaller autoCaller(this);
if (mState != SessionState_Locked)
return setError(VBOX_E_INVALID_VM_STATE,
tr("Machine is not locked by session (session state: %s)."),
return E_POINTER;
if (ComSafeArrayOutIsNull(aNames))
return E_POINTER;
if (ComSafeArrayOutIsNull(aValues))
return E_POINTER;
return E_POINTER;
if (ComSafeArrayOutIsNull(aFlags))
return E_POINTER;
/* If this session is not in a VM process fend off the call. The caller
* handles this correctly, by doing the operation in VBoxSVC. */
if (!mConsole)
return E_ACCESSDENIED;
#else /* VBOX_WITH_GUEST_PROPS not defined */
#endif /* VBOX_WITH_GUEST_PROPS not defined */
}
{
AutoCaller autoCaller(this);
if (mState != SessionState_Locked)
return setError(VBOX_E_INVALID_VM_STATE,
tr("Machine is not locked by session (session state: %s)."),
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
AssertFailed();
return E_NOTIMPL;
#endif
}
{
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
return S_OK;
#else
AssertFailed();
return E_NOTIMPL;
#endif
}
{
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
AssertFailed();
return E_NOTIMPL;
#endif
}
{
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
AssertFailed();
return E_NOTIMPL;
#endif
}
{
AutoCaller autoCaller(this);
#ifndef VBOX_COM_INPROC_API_CLIENT
#else
AssertFailed();
return E_NOTIMPL;
#endif
}
// private methods
///////////////////////////////////////////////////////////////////////////////
/**
* Unlocks a machine associated with the current session.
*
* @param aFinalRelease called as a result of FinalRelease()
* @param aFromServer called as a result of Uninitialize()
*
* @note To be called only from #uninit(), #UnlockMachine() or #Uninitialize().
* @note Locks this object for writing.
*/
{
LogFlowThisFunc(("aFinalRelease=%d, isFromServer=%d\n",
AutoCaller autoCaller(this);
if (mState != SessionState_Locked)
{
/* The session object is going to be uninitialized before it has been
* assigned a direct console of the machine the client requested to open
* a remote session to using IVirtualBox:: openRemoteSession(). It is OK
* only if this close request comes from the server (for example, it
* detected that the VM process it started terminated before opening a
* direct session). Otherwise, it means that the client is too fast and
* trying to close the session before waiting for the progress object it
* got from IVirtualBox:: openRemoteSession() to complete, so assert. */
return S_OK;
}
/* go to the closing state */
if (mType == SessionType_WriteLock)
{
#ifndef VBOX_COM_INPROC_API_CLIENT
{
}
#else
#endif
}
else
{
}
if (!aFinalRelease && !aFromServer)
{
/*
* We trigger OnSessionEnd() only when the session closes itself using
* Close(). Note that if isFinalRelease = TRUE here, this means that
* the client process has already initialized the termination procedure
* without issuing Close() and the IPC channel is no more operational --
* so we cannot call the server's method (it will definitely fail). The
* server will instead simply detect the abnormal client death (since
* OnSessionEnd() is not called) and reset the machine state to Aborted.
*/
/*
* while waiting for OnSessionEnd() to complete one of our methods
* can be called by the server (for example, Uninitialize(), if the
* direct session has initiated a closure just a bit before us) so
* we need to release the lock to avoid deadlocks. The state is already
* SessionState_Closing here, so it's safe.
*/
LogFlowThisFunc(("Calling mControl->OnSessionEnd()...\n"));
/*
* If we get E_UNEXPECTED this means that the direct session has already
* been closed, we're just too late with our notification and nothing more
*
* bird: Seems E_ACCESSDENIED is what gets returned these days; see
* VirtualBoxBase::addCaller.
*/
#ifndef DEBUG_bird /* I don't want clients crashing on me just because VBoxSVC went belly up. */
#endif
}
if (mType == SessionType_WriteLock)
{
if (mClientTokenHolder)
{
delete mClientTokenHolder;
}
if (!aFinalRelease && !aFromServer)
{
/*
* Wait for the server to grab the semaphore and destroy the session
* machine (allowing us to open a new session with the same machine
* once this method returns)
*/
if (progress)
}
}
/* release the VirtualBox instance as the very last step */
return S_OK;
}
/* vi: set tabstop=4 shiftwidth=4 expandtab: */