VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/SessionImpl.cpp@ 67414

Last change on this file since 67414 was 66217, checked in by vboxsync, 8 years ago

Main: a few doxygen fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 37.3 KB
Line 
1/* $Id: SessionImpl.cpp 66217 2017-03-23 09:33:56Z vboxsync $ */
2/** @file
3 * VBox Client Session COM Class implementation in VBoxC.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "SessionImpl.h"
19#include "ConsoleImpl.h"
20#include "Global.h"
21#include "ClientTokenHolder.h"
22
23#include "AutoCaller.h"
24#include "Logging.h"
25
26#include <VBox/err.h>
27#include <iprt/process.h>
28
29/**
30 * Local macro to check whether the session is open and return an error if not.
31 * @note Don't forget to do |Auto[Reader]Lock alock (this);| before using this
32 * macro.
33 */
34#define CHECK_OPEN() \
35 do { \
36 if (mState != SessionState_Locked) \
37 return setError(E_UNEXPECTED, tr ("The session is not locked (session state: %s)"), \
38 Global::stringifySessionState(mState)); \
39 } while (0)
40
41// constructor / destructor
42/////////////////////////////////////////////////////////////////////////////
43
44Session::Session()
45{
46}
47
48Session::~Session()
49{
50}
51
52HRESULT Session::FinalConstruct()
53{
54 LogFlowThisFunc(("\n"));
55
56 HRESULT rc = init();
57
58 BaseFinalConstruct();
59
60 return rc;
61}
62
63void Session::FinalRelease()
64{
65 LogFlowThisFunc(("\n"));
66
67 uninit();
68
69 BaseFinalRelease();
70}
71
72// public initializer/uninitializer for internal purposes only
73/////////////////////////////////////////////////////////////////////////////
74
75/**
76 * Initializes the Session object.
77 */
78HRESULT Session::init()
79{
80 /* Enclose the state transition NotReady->InInit->Ready */
81 AutoInitSpan autoInitSpan(this);
82 AssertReturn(autoInitSpan.isOk(), E_FAIL);
83
84 LogFlowThisFuncEnter();
85
86 mState = SessionState_Unlocked;
87 mType = SessionType_Null;
88
89 mClientTokenHolder = NULL;
90
91 /* Confirm a successful initialization when it's the case */
92 autoInitSpan.setSucceeded();
93
94 LogFlowThisFuncLeave();
95
96 return S_OK;
97}
98
99/**
100 * Uninitializes the Session object.
101 *
102 * @note Locks this object for writing.
103 */
104void Session::uninit()
105{
106 LogFlowThisFuncEnter();
107
108 /* Enclose the state transition Ready->InUninit->NotReady */
109 AutoUninitSpan autoUninitSpan(this);
110 if (autoUninitSpan.uninitDone())
111 {
112 LogFlowThisFunc(("Already uninitialized.\n"));
113 LogFlowThisFuncLeave();
114 return;
115 }
116
117 /* close() needs write lock */
118 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
119
120 if (mState != SessionState_Unlocked)
121 {
122 Assert(mState == SessionState_Locked ||
123 mState == SessionState_Spawning);
124
125 HRESULT rc = i_unlockMachine(true /* aFinalRelease */, false /* aFromServer */, alock);
126 AssertComRC(rc);
127 }
128
129 LogFlowThisFuncLeave();
130}
131
132// ISession properties
133/////////////////////////////////////////////////////////////////////////////
134
135HRESULT Session::getState(SessionState_T *aState)
136{
137 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
138
139 *aState = mState;
140
141 return S_OK;
142}
143
144HRESULT Session::getType(SessionType_T *aType)
145{
146 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
147
148 CHECK_OPEN();
149
150 *aType = mType;
151 return S_OK;
152}
153
154HRESULT Session::getName(com::Utf8Str &aName)
155{
156 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
157
158 aName = mName;
159 return S_OK;
160}
161
162HRESULT Session::setName(const com::Utf8Str &aName)
163{
164 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
165
166 if (mState != SessionState_Unlocked)
167 return setError(VBOX_E_INVALID_OBJECT_STATE, tr("Trying to set name for a session which is not in state \"unlocked\""));
168
169 mName = aName;
170 return S_OK;
171}
172
173HRESULT Session::getMachine(ComPtr<IMachine> &aMachine)
174{
175 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
176
177 CHECK_OPEN();
178
179 HRESULT rc;
180#ifndef VBOX_COM_INPROC_API_CLIENT
181 if (mConsole)
182 rc = mConsole->i_machine().queryInterfaceTo(aMachine.asOutParam());
183 else
184#endif
185 rc = mRemoteMachine.queryInterfaceTo(aMachine.asOutParam());
186 if (FAILED(rc))
187 {
188#ifndef VBOX_COM_INPROC_API_CLIENT
189 if (mConsole)
190 setError(rc, tr("Failed to query the session machine"));
191 else
192#endif
193 if (FAILED_DEAD_INTERFACE(rc))
194 setError(rc, tr("Peer process crashed"));
195 else
196 setError(rc, tr("Failed to query the remote session machine"));
197 }
198
199 return rc;
200}
201
202HRESULT Session::getConsole(ComPtr<IConsole> &aConsole)
203{
204 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
205
206 CHECK_OPEN();
207
208 HRESULT rc = S_OK;
209#ifndef VBOX_COM_INPROC_API_CLIENT
210 if (mConsole)
211 rc = mConsole.queryInterfaceTo(aConsole.asOutParam());
212 else
213#endif
214 rc = mRemoteConsole.queryInterfaceTo(aConsole.asOutParam());
215
216 if (FAILED(rc))
217 {
218#ifndef VBOX_COM_INPROC_API_CLIENT
219 if (mConsole)
220 setError(rc, tr("Failed to query the console"));
221 else
222#endif
223 if (FAILED_DEAD_INTERFACE(rc))
224 setError(rc, tr("Peer process crashed"));
225 else
226 setError(rc, tr("Failed to query the remote console"));
227 }
228
229 return rc;
230}
231
232// ISession methods
233/////////////////////////////////////////////////////////////////////////////
234HRESULT Session::unlockMachine()
235{
236 LogFlowThisFunc(("mState=%d, mType=%d\n", mState, mType));
237
238 /* close() needs write lock */
239 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
240
241 CHECK_OPEN();
242 return i_unlockMachine(false /* aFinalRelease */, false /* aFromServer */, alock);
243}
244
245// IInternalSessionControl methods
246/////////////////////////////////////////////////////////////////////////////
247HRESULT Session::getPID(ULONG *aPid)
248{
249 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
250
251 *aPid = (ULONG)RTProcSelf();
252 AssertCompile(sizeof(*aPid) == sizeof(RTPROCESS));
253
254 return S_OK;
255}
256
257HRESULT Session::getRemoteConsole(ComPtr<IConsole> &aConsole)
258{
259 LogFlowThisFuncEnter();
260
261 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
262
263#ifndef VBOX_COM_INPROC_API_CLIENT
264 AssertMsgReturn(mType == SessionType_WriteLock && !!mConsole,
265 ("This is not a direct session!\n"),
266 VBOX_E_INVALID_OBJECT_STATE);
267
268 /* return a failure if the session already transitioned to Closing
269 * but the server hasn't processed Machine::OnSessionEnd() yet. */
270 if (mState != SessionState_Locked)
271 return VBOX_E_INVALID_VM_STATE;
272
273 mConsole.queryInterfaceTo(aConsole.asOutParam());
274
275 LogFlowThisFuncLeave();
276
277 return S_OK;
278
279#else /* VBOX_COM_INPROC_API_CLIENT */
280 RT_NOREF(aConsole);
281 AssertFailed();
282 return VBOX_E_INVALID_OBJECT_STATE;
283#endif /* VBOX_COM_INPROC_API_CLIENT */
284}
285
286HRESULT Session::getNominalState(MachineState_T *aNominalState)
287{
288 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
289 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
290 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
291#ifndef VBOX_COM_INPROC_API_CLIENT
292 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
293
294 return mConsole->i_getNominalState(*aNominalState);
295#else
296 RT_NOREF(aNominalState);
297 AssertFailed();
298 return E_NOTIMPL;
299#endif
300}
301
302#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
303HRESULT Session::assignMachine(const ComPtr<IMachine> &aMachine,
304 LockType_T aLockType,
305 const com::Utf8Str &aTokenId)
306#else
307HRESULT Session::assignMachine(const ComPtr<IMachine> &aMachine,
308 LockType_T aLockType,
309 const ComPtr<IToken> &aToken)
310#endif /* !VBOX_WITH_GENERIC_SESSION_WATCHER */
311{
312 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
313
314 AssertReturn(mState == SessionState_Unlocked, VBOX_E_INVALID_VM_STATE);
315
316 if (!aMachine)
317 {
318 /*
319 * A special case: the server informs us that this session has been
320 * passed to IMachine::launchVMProcess() so this session will become
321 * remote (but not existing) when AssignRemoteMachine() is called.
322 */
323
324 AssertReturn(mType == SessionType_Null, VBOX_E_INVALID_OBJECT_STATE);
325 mType = SessionType_Remote;
326 mState = SessionState_Spawning;
327
328 return S_OK;
329 }
330
331 /* query IInternalMachineControl interface */
332 mControl = aMachine;
333 AssertReturn(!!mControl, E_FAIL);
334
335 HRESULT rc = S_OK;
336#ifndef VBOX_COM_INPROC_API_CLIENT
337 if (aLockType == LockType_VM)
338 {
339 /* This is what is special about VM processes: they have a Console
340 * object which is the root of all VM related activity. */
341 rc = mConsole.createObject();
342 AssertComRCReturn(rc, rc);
343
344 rc = mConsole->init(aMachine, mControl, aLockType);
345 AssertComRCReturn(rc, rc);
346 }
347 else
348 mRemoteMachine = aMachine;
349#else
350 RT_NOREF(aLockType);
351 mRemoteMachine = aMachine;
352#endif
353
354#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
355 Utf8Str strTokenId(aTokenId);
356 Assert(!strTokenId.isEmpty());
357#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
358 Assert(!aToken.isNull());
359#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
360 /* create the machine client token */
361 try
362 {
363#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
364 mClientTokenHolder = new ClientTokenHolder(strTokenId);
365#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
366 mClientTokenHolder = new ClientTokenHolder(aToken);
367#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
368 if (!mClientTokenHolder->isReady())
369 {
370 delete mClientTokenHolder;
371 mClientTokenHolder = NULL;
372 rc = E_FAIL;
373 }
374 }
375 catch (std::bad_alloc &)
376 {
377 rc = E_OUTOFMEMORY;
378 }
379
380 /*
381 * Reference the VirtualBox object to ensure the server is up
382 * until the session is closed
383 */
384 if (SUCCEEDED(rc))
385 rc = aMachine->COMGETTER(Parent)(mVirtualBox.asOutParam());
386
387 if (SUCCEEDED(rc))
388 {
389 mType = SessionType_WriteLock;
390 mState = SessionState_Locked;
391 }
392 else
393 {
394 /* some cleanup */
395 mControl.setNull();
396#ifndef VBOX_COM_INPROC_API_CLIENT
397 if (!mConsole.isNull())
398 {
399 mConsole->uninit();
400 mConsole.setNull();
401 }
402#endif
403 }
404
405 return rc;
406}
407
408HRESULT Session::assignRemoteMachine(const ComPtr<IMachine> &aMachine,
409 const ComPtr<IConsole> &aConsole)
410
411{
412 AssertReturn(aMachine, E_INVALIDARG);
413
414 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
415
416 AssertReturn(mState == SessionState_Unlocked ||
417 mState == SessionState_Spawning, VBOX_E_INVALID_VM_STATE);
418
419 HRESULT rc = E_FAIL;
420
421 /* query IInternalMachineControl interface */
422 mControl = aMachine;
423 AssertReturn(!!mControl, E_FAIL);
424
425 /// @todo (dmik)
426 // currently, the remote session returns the same machine and
427 // console objects as the direct session, thus giving the
428 // (remote) client full control over the direct session. For the
429 // console, it is the desired behavior (the ability to control
430 // VM execution is a must for the remote session). What about
431 // the machine object, we may want to prevent the remote client
432 // from modifying machine data. In this case, we must:
433 // 1) assign the Machine object (instead of the SessionMachine
434 // object that is passed to this method) to mRemoteMachine;
435 // 2) remove GetMachine() property from the IConsole interface
436 // because it always returns the SessionMachine object
437 // (alternatively, we can supply a separate IConsole
438 // implementation that will return the Machine object in
439 // response to GetMachine()).
440
441 mRemoteMachine = aMachine;
442 mRemoteConsole = aConsole;
443
444 /*
445 * Reference the VirtualBox object to ensure the server is up
446 * until the session is closed
447 */
448 rc = aMachine->COMGETTER(Parent)(mVirtualBox.asOutParam());
449
450 if (SUCCEEDED(rc))
451 {
452 /*
453 * RemoteSession type can be already set by AssignMachine() when its
454 * argument is NULL (a special case)
455 */
456 if (mType != SessionType_Remote)
457 mType = SessionType_Shared;
458 else
459 Assert(mState == SessionState_Spawning);
460
461 mState = SessionState_Locked;
462 }
463 else
464 {
465 /* some cleanup */
466 mControl.setNull();
467 mRemoteMachine.setNull();
468 mRemoteConsole.setNull();
469 }
470
471 LogFlowThisFunc(("rc=%08X\n", rc));
472 LogFlowThisFuncLeave();
473
474 return rc;
475}
476
477HRESULT Session::updateMachineState(MachineState_T aMachineState)
478{
479
480 if (getObjectState().getState() != ObjectState::Ready)
481 {
482 /*
483 * We might have already entered Session::uninit() at this point, so
484 * return silently (not interested in the state change during uninit)
485 */
486 LogFlowThisFunc(("Already uninitialized.\n"));
487 return S_OK;
488 }
489
490 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
491
492 if (mState == SessionState_Unlocking)
493 {
494 LogFlowThisFunc(("Already being unlocked.\n"));
495 return S_OK;
496 }
497
498 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
499 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
500
501 AssertReturn(!mControl.isNull(), E_FAIL);
502#ifndef VBOX_COM_INPROC_API_CLIENT
503 AssertReturn(!mConsole.isNull(), E_FAIL);
504
505 return mConsole->i_updateMachineState(aMachineState);
506#else
507 RT_NOREF(aMachineState);
508 return S_OK;
509#endif
510}
511
512HRESULT Session::uninitialize()
513{
514 LogFlowThisFuncEnter();
515
516 AutoCaller autoCaller(this);
517
518 HRESULT rc = S_OK;
519
520 if (getObjectState().getState() == ObjectState::Ready)
521 {
522 /* close() needs write lock */
523 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
524
525 LogFlowThisFunc(("mState=%s, mType=%d\n", Global::stringifySessionState(mState), mType));
526
527 if (mState == SessionState_Unlocking)
528 {
529 LogFlowThisFunc(("Already being unlocked.\n"));
530 return S_OK;
531 }
532
533 if ( mState == SessionState_Locked
534 || mState == SessionState_Spawning)
535 { /* likely */ }
536 else
537 {
538#ifndef DEBUG_bird /* bird: hitting this all the time running tdAddBaseic1.py. */
539 AssertMsgFailed(("Session is in wrong state (%d), expected locked (%d) or spawning (%d)\n",
540 mState, SessionState_Locked, SessionState_Spawning));
541#endif
542 return VBOX_E_INVALID_VM_STATE;
543 }
544
545 /* close ourselves */
546 rc = i_unlockMachine(false /* aFinalRelease */, true /* aFromServer */, alock);
547 }
548 else if (getObjectState().getState() == ObjectState::InUninit)
549 {
550 /*
551 * We might have already entered Session::uninit() at this point,
552 * return silently
553 */
554 LogFlowThisFunc(("Already uninitialized.\n"));
555 }
556 else
557 {
558 Log1WarningThisFunc(("UNEXPECTED uninitialization!\n"));
559 rc = autoCaller.rc();
560 }
561
562 LogFlowThisFunc(("rc=%08X\n", rc));
563 LogFlowThisFuncLeave();
564
565 return rc;
566}
567
568HRESULT Session::onNetworkAdapterChange(const ComPtr<INetworkAdapter> &aNetworkAdapter,
569 BOOL aChangeAdapter)
570
571{
572 LogFlowThisFunc(("\n"));
573
574 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
575 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
576 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
577#ifndef VBOX_COM_INPROC_API_CLIENT
578 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
579
580 return mConsole->i_onNetworkAdapterChange(aNetworkAdapter, aChangeAdapter);
581#else
582 RT_NOREF(aNetworkAdapter, aChangeAdapter);
583 return S_OK;
584#endif
585}
586
587HRESULT Session::onSerialPortChange(const ComPtr<ISerialPort> &aSerialPort)
588{
589 LogFlowThisFunc(("\n"));
590
591 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
592 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
593 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
594#ifndef VBOX_COM_INPROC_API_CLIENT
595 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
596
597 return mConsole->i_onSerialPortChange(aSerialPort);
598#else
599 RT_NOREF(aSerialPort);
600 return S_OK;
601#endif
602}
603
604HRESULT Session::onParallelPortChange(const ComPtr<IParallelPort> &aParallelPort)
605{
606 LogFlowThisFunc(("\n"));
607
608 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
609 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
610 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
611#ifndef VBOX_COM_INPROC_API_CLIENT
612 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
613
614 return mConsole->i_onParallelPortChange(aParallelPort);
615#else
616 RT_NOREF(aParallelPort);
617 return S_OK;
618#endif
619}
620
621HRESULT Session::onStorageControllerChange()
622{
623 LogFlowThisFunc(("\n"));
624
625 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
626 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
627 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
628#ifndef VBOX_COM_INPROC_API_CLIENT
629 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
630
631 return mConsole->i_onStorageControllerChange();
632#else
633 return S_OK;
634#endif
635}
636
637HRESULT Session::onMediumChange(const ComPtr<IMediumAttachment> &aMediumAttachment,
638 BOOL aForce)
639{
640 LogFlowThisFunc(("\n"));
641
642 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
643 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
644 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
645#ifndef VBOX_COM_INPROC_API_CLIENT
646 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
647
648 return mConsole->i_onMediumChange(aMediumAttachment, aForce);
649#else
650 RT_NOREF(aMediumAttachment, aForce);
651 return S_OK;
652#endif
653}
654
655HRESULT Session::onCPUChange(ULONG aCpu, BOOL aAdd)
656{
657 LogFlowThisFunc(("\n"));
658
659 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
660 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
661 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
662#ifndef VBOX_COM_INPROC_API_CLIENT
663 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
664
665 return mConsole->i_onCPUChange(aCpu, aAdd);
666#else
667 RT_NOREF(aCpu, aAdd);
668 return S_OK;
669#endif
670}
671
672HRESULT Session::onCPUExecutionCapChange(ULONG aExecutionCap)
673{
674 LogFlowThisFunc(("\n"));
675
676 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
677 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
678 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
679#ifndef VBOX_COM_INPROC_API_CLIENT
680 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
681
682 return mConsole->i_onCPUExecutionCapChange(aExecutionCap);
683#else
684 RT_NOREF(aExecutionCap);
685 return S_OK;
686#endif
687}
688
689HRESULT Session::onVRDEServerChange(BOOL aRestart)
690{
691 LogFlowThisFunc(("\n"));
692
693 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
694 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
695 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
696#ifndef VBOX_COM_INPROC_API_CLIENT
697 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
698
699 return mConsole->i_onVRDEServerChange(aRestart);
700#else
701 RT_NOREF(aRestart);
702 return S_OK;
703#endif
704}
705
706HRESULT Session::onVideoCaptureChange()
707{
708 LogFlowThisFunc(("\n"));
709
710 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
711 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
712 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
713#ifndef VBOX_COM_INPROC_API_CLIENT
714 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
715
716 return mConsole->i_onVideoCaptureChange();
717#else
718 return S_OK;
719#endif
720}
721
722HRESULT Session::onUSBControllerChange()
723{
724 LogFlowThisFunc(("\n"));
725
726 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
727 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
728 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
729#ifndef VBOX_COM_INPROC_API_CLIENT
730 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
731
732 return mConsole->i_onUSBControllerChange();
733#else
734 return S_OK;
735#endif
736}
737
738HRESULT Session::onSharedFolderChange(BOOL aGlobal)
739{
740 LogFlowThisFunc(("\n"));
741
742 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
743 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
744 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
745#ifndef VBOX_COM_INPROC_API_CLIENT
746 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
747
748 return mConsole->i_onSharedFolderChange(aGlobal);
749#else
750 RT_NOREF(aGlobal);
751 return S_OK;
752#endif
753}
754
755HRESULT Session::onClipboardModeChange(ClipboardMode_T aClipboardMode)
756{
757 LogFlowThisFunc(("\n"));
758
759 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
760 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
761 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
762#ifndef VBOX_COM_INPROC_API_CLIENT
763 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
764
765 return mConsole->i_onClipboardModeChange(aClipboardMode);
766#else
767 RT_NOREF(aClipboardMode);
768 return S_OK;
769#endif
770}
771
772HRESULT Session::onDnDModeChange(DnDMode_T aDndMode)
773{
774 LogFlowThisFunc(("\n"));
775
776 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
777 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
778#ifndef VBOX_COM_INPROC_API_CLIENT
779 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
780 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
781
782 return mConsole->i_onDnDModeChange(aDndMode);
783#else
784 RT_NOREF(aDndMode);
785 return S_OK;
786#endif
787}
788
789HRESULT Session::onUSBDeviceAttach(const ComPtr<IUSBDevice> &aDevice,
790 const ComPtr<IVirtualBoxErrorInfo> &aError,
791 ULONG aMaskedInterfaces,
792 const com::Utf8Str &aCaptureFilename)
793{
794 LogFlowThisFunc(("\n"));
795
796 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
797 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
798 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
799#ifndef VBOX_COM_INPROC_API_CLIENT
800 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
801
802 return mConsole->i_onUSBDeviceAttach(aDevice, aError, aMaskedInterfaces, aCaptureFilename);
803#else
804 RT_NOREF(aDevice, aError, aMaskedInterfaces, aCaptureFilename);
805 return S_OK;
806#endif
807}
808
809HRESULT Session::onUSBDeviceDetach(const com::Guid &aId,
810 const ComPtr<IVirtualBoxErrorInfo> &aError)
811{
812 LogFlowThisFunc(("\n"));
813
814 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
815 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
816 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
817#ifndef VBOX_COM_INPROC_API_CLIENT
818 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
819
820 return mConsole->i_onUSBDeviceDetach(aId.toUtf16().raw(), aError);
821#else
822 RT_NOREF(aId, aError);
823 return S_OK;
824#endif
825}
826
827HRESULT Session::onShowWindow(BOOL aCheck, BOOL *aCanShow, LONG64 *aWinId)
828{
829 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
830
831 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
832#ifndef VBOX_COM_INPROC_API_CLIENT
833 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
834#endif
835
836 if (mState != SessionState_Locked)
837 {
838 /* the call from Machine issued when the session is open can arrive
839 * after the session starts closing or gets closed. Note that when
840 * aCheck is false, we return E_FAIL to indicate that aWinId we return
841 * is not valid */
842 *aCanShow = FALSE;
843 *aWinId = 0;
844 return aCheck ? S_OK : E_FAIL;
845 }
846
847#ifndef VBOX_COM_INPROC_API_CLIENT
848 return mConsole->i_onShowWindow(aCheck, aCanShow, aWinId);
849#else
850 return S_OK;
851#endif
852}
853
854HRESULT Session::onBandwidthGroupChange(const ComPtr<IBandwidthGroup> &aBandwidthGroup)
855{
856 LogFlowThisFunc(("\n"));
857
858 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
859 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
860 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
861#ifndef VBOX_COM_INPROC_API_CLIENT
862 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
863
864 return mConsole->i_onBandwidthGroupChange(aBandwidthGroup);
865#else
866 RT_NOREF(aBandwidthGroup);
867 return S_OK;
868#endif
869}
870
871HRESULT Session::onStorageDeviceChange(const ComPtr<IMediumAttachment> &aMediumAttachment, BOOL aRemove, BOOL aSilent)
872{
873 LogFlowThisFunc(("\n"));
874
875 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
876 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
877 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
878#ifndef VBOX_COM_INPROC_API_CLIENT
879 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
880
881 return mConsole->i_onStorageDeviceChange(aMediumAttachment, aRemove, aSilent);
882#else
883 RT_NOREF(aMediumAttachment, aRemove, aSilent);
884 return S_OK;
885#endif
886}
887
888HRESULT Session::accessGuestProperty(const com::Utf8Str &aName, const com::Utf8Str &aValue, const com::Utf8Str &aFlags,
889 ULONG aAccessMode, com::Utf8Str &aRetValue, LONG64 *aRetTimestamp, com::Utf8Str &aRetFlags)
890{
891#ifdef VBOX_WITH_GUEST_PROPS
892# ifndef VBOX_COM_INPROC_API_CLIENT
893 if (mState != SessionState_Locked)
894 return setError(VBOX_E_INVALID_VM_STATE,
895 tr("Machine is not locked by session (session state: %s)."),
896 Global::stringifySessionState(mState));
897 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
898 if (aName.isEmpty())
899 return E_INVALIDARG;
900 if (aAccessMode == 0 && !RT_VALID_PTR(aRetTimestamp))
901 return E_POINTER;
902
903 /* If this session is not in a VM process fend off the call. The caller
904 * handles this correctly, by doing the operation in VBoxSVC. */
905 if (!mConsole)
906 return E_ACCESSDENIED;
907
908 HRESULT hr;
909 if (aAccessMode == 2)
910 hr = mConsole->i_deleteGuestProperty(aName);
911 else if (aAccessMode == 1)
912 hr = mConsole->i_setGuestProperty(aName, aValue, aFlags);
913 else if (aAccessMode == 0)
914 hr = mConsole->i_getGuestProperty(aName, &aRetValue, aRetTimestamp, &aRetFlags);
915 else
916 hr = E_INVALIDARG;
917
918 return hr;
919# else /* VBOX_COM_INPROC_API_CLIENT */
920 /** @todo This is nonsense, non-VM API users shouldn't need to deal with this
921 * method call, VBoxSVC should be clever enough to see that the
922 * session doesn't have a console! */
923 RT_NOREF(aName, aValue, aFlags, aAccessMode, aRetValue, aRetTimestamp, aRetFlags);
924 return E_ACCESSDENIED;
925# endif /* VBOX_COM_INPROC_API_CLIENT */
926
927#else /* VBOX_WITH_GUEST_PROPS */
928 ReturnComNotImplemented();
929#endif /* VBOX_WITH_GUEST_PROPS */
930}
931
932HRESULT Session::enumerateGuestProperties(const com::Utf8Str &aPatterns,
933 std::vector<com::Utf8Str> &aKeys,
934 std::vector<com::Utf8Str> &aValues,
935 std::vector<LONG64> &aTimestamps,
936 std::vector<com::Utf8Str> &aFlags)
937{
938#if defined(VBOX_WITH_GUEST_PROPS) && !defined(VBOX_COM_INPROC_API_CLIENT)
939 if (mState != SessionState_Locked)
940 return setError(VBOX_E_INVALID_VM_STATE,
941 tr("Machine is not locked by session (session state: %s)."),
942 Global::stringifySessionState(mState));
943 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
944
945 /* If this session is not in a VM process fend off the call. The caller
946 * handles this correctly, by doing the operation in VBoxSVC. */
947 if (!mConsole)
948 return E_ACCESSDENIED;
949
950 return mConsole->i_enumerateGuestProperties(aPatterns, aKeys, aValues, aTimestamps, aFlags);
951
952#else /* VBOX_WITH_GUEST_PROPS not defined */
953 RT_NOREF(aPatterns, aKeys, aValues, aTimestamps, aFlags);
954 ReturnComNotImplemented();
955#endif /* VBOX_WITH_GUEST_PROPS not defined */
956}
957
958HRESULT Session::onlineMergeMedium(const ComPtr<IMediumAttachment> &aMediumAttachment, ULONG aSourceIdx,
959 ULONG aTargetIdx, const ComPtr<IProgress> &aProgress)
960{
961 if (mState != SessionState_Locked)
962 return setError(VBOX_E_INVALID_VM_STATE,
963 tr("Machine is not locked by session (session state: %s)."),
964 Global::stringifySessionState(mState));
965#ifndef VBOX_COM_INPROC_API_CLIENT
966 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
967 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
968
969 return mConsole->i_onlineMergeMedium(aMediumAttachment,
970 aSourceIdx, aTargetIdx,
971 aProgress);
972#else
973 RT_NOREF(aMediumAttachment, aSourceIdx, aTargetIdx, aProgress);
974 AssertFailed();
975 return E_NOTIMPL;
976#endif
977}
978
979HRESULT Session::reconfigureMediumAttachments(const std::vector<ComPtr<IMediumAttachment> > &aAttachments)
980{
981 if (mState != SessionState_Locked)
982 return setError(VBOX_E_INVALID_VM_STATE,
983 tr("Machine is not locked by session (session state: %s)."),
984 Global::stringifySessionState(mState));
985#ifndef VBOX_COM_INPROC_API_CLIENT
986 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
987 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
988
989 return mConsole->i_reconfigureMediumAttachments(aAttachments);
990#else
991 RT_NOREF(aAttachments);
992 AssertFailed();
993 return E_NOTIMPL;
994#endif
995}
996
997HRESULT Session::enableVMMStatistics(BOOL aEnable)
998{
999 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1000 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
1001 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
1002#ifndef VBOX_COM_INPROC_API_CLIENT
1003 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
1004
1005 mConsole->i_enableVMMStatistics(aEnable);
1006
1007 return S_OK;
1008#else
1009 RT_NOREF(aEnable);
1010 AssertFailed();
1011 return E_NOTIMPL;
1012#endif
1013}
1014
1015HRESULT Session::pauseWithReason(Reason_T aReason)
1016{
1017 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1018 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
1019 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
1020#ifndef VBOX_COM_INPROC_API_CLIENT
1021 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
1022
1023 return mConsole->i_pause(aReason);
1024#else
1025 RT_NOREF(aReason);
1026 AssertFailed();
1027 return E_NOTIMPL;
1028#endif
1029}
1030
1031HRESULT Session::resumeWithReason(Reason_T aReason)
1032{
1033 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1034 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
1035 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
1036#ifndef VBOX_COM_INPROC_API_CLIENT
1037 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
1038
1039 AutoWriteLock dummyLock(mConsole COMMA_LOCKVAL_SRC_POS);
1040 return mConsole->i_resume(aReason, dummyLock);
1041#else
1042 RT_NOREF(aReason);
1043 AssertFailed();
1044 return E_NOTIMPL;
1045#endif
1046}
1047
1048HRESULT Session::saveStateWithReason(Reason_T aReason, const ComPtr<IProgress> &aProgress, const Utf8Str &aStateFilePath,
1049 BOOL aPauseVM, BOOL *aLeftPaused)
1050{
1051 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1052 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
1053 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
1054#ifndef VBOX_COM_INPROC_API_CLIENT
1055 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
1056
1057 bool fLeftPaused = false;
1058 HRESULT rc = mConsole->i_saveState(aReason, aProgress, aStateFilePath, !!aPauseVM, fLeftPaused);
1059 if (aLeftPaused)
1060 *aLeftPaused = fLeftPaused;
1061 return rc;
1062#else
1063 RT_NOREF(aReason, aProgress, aStateFilePath, aPauseVM, aLeftPaused);
1064 AssertFailed();
1065 return E_NOTIMPL;
1066#endif
1067}
1068
1069HRESULT Session::cancelSaveStateWithReason()
1070{
1071 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1072 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
1073 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
1074#ifndef VBOX_COM_INPROC_API_CLIENT
1075 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
1076
1077 return mConsole->i_cancelSaveState();
1078#else
1079 AssertFailed();
1080 return E_NOTIMPL;
1081#endif
1082}
1083
1084// private methods
1085///////////////////////////////////////////////////////////////////////////////
1086
1087/**
1088 * Unlocks a machine associated with the current session.
1089 *
1090 * @param aFinalRelease called as a result of FinalRelease()
1091 * @param aFromServer called as a result of Uninitialize()
1092 * @param aLockW The write lock this object is protected with.
1093 * Must be acquired already and will be released
1094 * and later reacquired during the unlocking.
1095 *
1096 * @note To be called only from #uninit(), ISession::UnlockMachine() or
1097 * ISession::Uninitialize().
1098 */
1099HRESULT Session::i_unlockMachine(bool aFinalRelease, bool aFromServer, AutoWriteLock &aLockW)
1100{
1101 LogFlowThisFuncEnter();
1102 LogFlowThisFunc(("aFinalRelease=%d, isFromServer=%d\n",
1103 aFinalRelease, aFromServer));
1104
1105 LogFlowThisFunc(("mState=%s, mType=%d\n", Global::stringifySessionState(mState), mType));
1106
1107 Assert(aLockW.isWriteLockOnCurrentThread());
1108
1109 if (mState != SessionState_Locked)
1110 {
1111 Assert(mState == SessionState_Spawning);
1112
1113 /* The session object is going to be uninitialized before it has been
1114 * assigned a direct console of the machine the client requested to open
1115 * a remote session to using IVirtualBox:: openRemoteSession(). It is OK
1116 * only if this close request comes from the server (for example, it
1117 * detected that the VM process it started terminated before opening a
1118 * direct session). Otherwise, it means that the client is too fast and
1119 * trying to close the session before waiting for the progress object it
1120 * got from IVirtualBox:: openRemoteSession() to complete, so assert. */
1121 Assert(aFromServer);
1122
1123 mState = SessionState_Unlocked;
1124 mType = SessionType_Null;
1125
1126 Assert(!mClientTokenHolder);
1127
1128 LogFlowThisFuncLeave();
1129 return S_OK;
1130 }
1131
1132 /* go to the closing state */
1133 mState = SessionState_Unlocking;
1134
1135 if (mType == SessionType_WriteLock)
1136 {
1137#ifndef VBOX_COM_INPROC_API_CLIENT
1138 if (!mConsole.isNull())
1139 {
1140 mConsole->uninit();
1141 mConsole.setNull();
1142 }
1143#else
1144 mRemoteMachine.setNull();
1145#endif
1146 }
1147 else
1148 {
1149 mRemoteMachine.setNull();
1150 mRemoteConsole.setNull();
1151 }
1152
1153 ComPtr<IProgress> progress;
1154
1155 if (!aFinalRelease && !aFromServer)
1156 {
1157 /*
1158 * We trigger OnSessionEnd() only when the session closes itself using
1159 * Close(). Note that if isFinalRelease = TRUE here, this means that
1160 * the client process has already initialized the termination procedure
1161 * without issuing Close() and the IPC channel is no more operational --
1162 * so we cannot call the server's method (it will definitely fail). The
1163 * server will instead simply detect the abnormal client death (since
1164 * OnSessionEnd() is not called) and reset the machine state to Aborted.
1165 */
1166
1167 /*
1168 * while waiting for OnSessionEnd() to complete one of our methods
1169 * can be called by the server (for example, Uninitialize(), if the
1170 * direct session has initiated a closure just a bit before us) so
1171 * we need to release the lock to avoid deadlocks. The state is already
1172 * SessionState_Closing here, so it's safe.
1173 */
1174 aLockW.release();
1175
1176 Assert(!aLockW.isWriteLockOnCurrentThread());
1177
1178 LogFlowThisFunc(("Calling mControl->OnSessionEnd()...\n"));
1179 HRESULT rc = mControl->OnSessionEnd(this, progress.asOutParam());
1180 LogFlowThisFunc(("mControl->OnSessionEnd()=%08X\n", rc));
1181
1182 aLockW.acquire();
1183
1184 /*
1185 * If we get E_UNEXPECTED this means that the direct session has already
1186 * been closed, we're just too late with our notification and nothing more
1187 *
1188 * bird: Seems E_ACCESSDENIED is what gets returned these days; see
1189 * ObjectState::addCaller.
1190 */
1191 if (mType != SessionType_WriteLock && (rc == E_UNEXPECTED || rc == E_ACCESSDENIED))
1192 rc = S_OK;
1193
1194#if !defined(DEBUG_bird) && !defined(DEBUG_andy) /* I don't want clients crashing on me just because VBoxSVC went belly up. */
1195 AssertComRC(rc);
1196#endif
1197 }
1198
1199 mControl.setNull();
1200
1201 if (mType == SessionType_WriteLock)
1202 {
1203 if (mClientTokenHolder)
1204 {
1205 delete mClientTokenHolder;
1206 mClientTokenHolder = NULL;
1207 }
1208
1209 if (!aFinalRelease && !aFromServer)
1210 {
1211 /*
1212 * Wait for the server to grab the semaphore and destroy the session
1213 * machine (allowing us to open a new session with the same machine
1214 * once this method returns)
1215 */
1216 Assert(!!progress);
1217 if (progress)
1218 progress->WaitForCompletion(-1);
1219 }
1220 }
1221
1222 mState = SessionState_Unlocked;
1223 mType = SessionType_Null;
1224
1225 /* release the VirtualBox instance as the very last step */
1226 mVirtualBox.setNull();
1227
1228 LogFlowThisFuncLeave();
1229 return S_OK;
1230}
1231
1232/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette