VirtualBox

source: vbox/trunk/src/VBox/Main/SessionImpl.cpp@ 4615

Last change on this file since 4615 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.1 KB
Line 
1/** @file
2 *
3 * VBox Client Session COM Class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#if defined(RT_OS_WINDOWS)
19#elif defined(RT_OS_LINUX)
20#endif
21
22#ifdef VBOX_WITH_SYS_V_IPC_SESSION_WATCHER
23# include <errno.h>
24# include <sys/types.h>
25# include <sys/stat.h>
26# include <sys/ipc.h>
27# include <sys/sem.h>
28#endif
29
30#include "SessionImpl.h"
31#include "ConsoleImpl.h"
32
33#include "Logging.h"
34
35#include <VBox/err.h>
36#include <iprt/process.h>
37
38#if defined(RT_OS_WINDOWS) || defined (RT_OS_OS2)
39/** VM IPC mutex holder thread */
40static DECLCALLBACK(int) IPCMutexHolderThread (RTTHREAD Thread, void *pvUser);
41#endif
42
43/**
44 * Local macro to check whether the session is open and return an error if not.
45 * @note Don't forget to do |Auto[Reader]Lock alock (this);| before using this
46 * macro.
47 */
48#define CHECK_OPEN() \
49 do { \
50 if (mState != SessionState_SessionOpen) \
51 return setError (E_UNEXPECTED, \
52 tr ("The session is not open")); \
53 } while (0)
54
55// constructor / destructor
56/////////////////////////////////////////////////////////////////////////////
57
58HRESULT Session::FinalConstruct()
59{
60 LogFlowThisFunc (("\n"));
61
62 return init();
63}
64
65void Session::FinalRelease()
66{
67 LogFlowThisFunc (("\n"));
68
69 uninit (true /* aFinalRelease */);
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_UNEXPECTED);
83
84 LogFlowThisFuncEnter();
85
86 mState = SessionState_SessionClosed;
87 mType = SessionType_InvalidSessionType;
88
89#if defined(RT_OS_WINDOWS)
90 mIPCSem = NULL;
91 mIPCThreadSem = NULL;
92#elif defined(RT_OS_OS2)
93 mIPCThread = NIL_RTTHREAD;
94 mIPCThreadSem = NIL_RTSEMEVENT;
95#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
96 mIPCSem = -1;
97#else
98# error "Port me!"
99#endif
100
101 /* Confirm a successful initialization when it's the case */
102 autoInitSpan.setSucceeded();
103
104 LogFlowThisFuncLeave();
105
106 return S_OK;
107}
108
109/**
110 * Uninitializes the Session object.
111 *
112 * @note Locks this object for writing.
113 */
114void Session::uninit (bool aFinalRelease)
115{
116 LogFlowThisFuncEnter();
117 LogFlowThisFunc (("aFinalRelease=%d\n", aFinalRelease));
118
119 /* Enclose the state transition Ready->InUninit->NotReady */
120 AutoUninitSpan autoUninitSpan (this);
121 if (autoUninitSpan.uninitDone())
122 {
123 LogFlowThisFunc (("Already uninitialized.\n"));
124 LogFlowThisFuncLeave();
125 return;
126 }
127
128 AutoLock alock (this);
129
130 if (mState != SessionState_SessionClosed)
131 {
132 Assert (mState == SessionState_SessionOpen ||
133 mState == SessionState_SessionSpawning);
134
135 HRESULT rc = close (aFinalRelease, false /* aFromServer */);
136 AssertComRC (rc);
137 }
138
139 LogFlowThisFuncLeave();
140}
141
142// ISession properties
143/////////////////////////////////////////////////////////////////////////////
144
145STDMETHODIMP Session::COMGETTER(State) (SessionState_T *aState)
146{
147 if (!aState)
148 return E_POINTER;
149
150 AutoCaller autoCaller (this);
151 CheckComRCReturnRC (autoCaller.rc());
152
153 AutoReaderLock alock (this);
154
155 *aState = mState;
156
157 return S_OK;
158}
159
160STDMETHODIMP Session::COMGETTER(Type) (SessionType_T *aType)
161{
162 if (!aType)
163 return E_POINTER;
164
165 AutoCaller autoCaller (this);
166 CheckComRCReturnRC (autoCaller.rc());
167
168 AutoReaderLock alock (this);
169
170 CHECK_OPEN();
171
172 *aType = mType;
173 return S_OK;
174}
175
176STDMETHODIMP Session::COMGETTER(Machine) (IMachine **aMachine)
177{
178 if (!aMachine)
179 return E_POINTER;
180
181 AutoCaller autoCaller (this);
182 CheckComRCReturnRC (autoCaller.rc());
183
184 AutoReaderLock alock (this);
185
186 CHECK_OPEN();
187
188 HRESULT rc = E_FAIL;
189
190 if (mConsole)
191 rc = mConsole->machine().queryInterfaceTo (aMachine);
192 else
193 rc = mRemoteMachine.queryInterfaceTo (aMachine);
194 ComAssertComRC (rc);
195
196 return rc;
197}
198
199STDMETHODIMP Session::COMGETTER(Console) (IConsole **aConsole)
200{
201 if (!aConsole)
202 return E_POINTER;
203
204 AutoCaller autoCaller (this);
205 CheckComRCReturnRC (autoCaller.rc());
206
207 AutoReaderLock alock (this);
208
209 CHECK_OPEN();
210
211 HRESULT rc = E_FAIL;
212
213 if (mConsole)
214 rc = mConsole.queryInterfaceTo (aConsole);
215 else
216 rc = mRemoteConsole.queryInterfaceTo (aConsole);
217 ComAssertComRC (rc);
218
219 return rc;
220}
221
222// ISession methods
223/////////////////////////////////////////////////////////////////////////////
224
225STDMETHODIMP Session::Close()
226{
227 LogFlowThisFunc (("mState=%d, mType=%d\n", mState, mType));
228
229 AutoCaller autoCaller (this);
230 CheckComRCReturnRC (autoCaller.rc());
231
232 /* close() needs write lock */
233 AutoLock alock (this);
234
235 CHECK_OPEN();
236
237 return close (false /* aFinalRelease */, false /* aFromServer */);
238}
239
240// IInternalSessionControl methods
241/////////////////////////////////////////////////////////////////////////////
242
243STDMETHODIMP Session::GetPID (ULONG *aPid)
244{
245 AssertReturn (aPid, E_POINTER);
246
247 AutoCaller autoCaller (this);
248 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
249
250 AutoReaderLock alock (this);
251
252 *aPid = (ULONG) RTProcSelf();
253 AssertCompile (sizeof (*aPid) == sizeof (RTPROCESS));
254
255 return S_OK;
256}
257
258STDMETHODIMP Session::GetRemoteConsole (IConsole **aConsole)
259{
260 AssertReturn (aConsole, E_POINTER);
261
262 AutoCaller autoCaller (this);
263 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
264
265 AutoReaderLock alock (this);
266
267 AssertReturn (mState == SessionState_SessionOpen, E_FAIL);
268
269 AssertMsgReturn (mType == SessionType_DirectSession && !!mConsole,
270 ("This is not a direct session!\n"), E_FAIL);
271
272 mConsole.queryInterfaceTo (aConsole);
273
274 return S_OK;
275}
276
277STDMETHODIMP Session::AssignMachine (IMachine *aMachine)
278{
279 LogFlowThisFuncEnter();
280 LogFlowThisFunc (("aMachine=%p\n", aMachine));
281
282 AutoCaller autoCaller (this);
283 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
284
285 AutoLock alock (this);
286
287 AssertReturn (mState == SessionState_SessionClosed, E_FAIL);
288
289 if (!aMachine)
290 {
291 /*
292 * A special case: the server informs us that this session has been
293 * passed to IVirtualBox::OpenRemoteSession() so this session will
294 * become remote (but not existing) when AssignRemoteMachine() is
295 * called.
296 */
297
298 AssertReturn (mType == SessionType_InvalidSessionType, E_FAIL);
299 mType = SessionType_RemoteSession;
300 mState = SessionState_SessionSpawning;
301
302 LogFlowThisFuncLeave();
303 return S_OK;
304 }
305
306 HRESULT rc = E_FAIL;
307
308 /* query IInternalMachineControl interface */
309 mControl = aMachine;
310 AssertReturn (!!mControl, E_FAIL);
311
312 rc = mConsole.createObject();
313 AssertComRCReturn (rc, rc);
314
315 rc = mConsole->init (aMachine, mControl);
316 AssertComRCReturn (rc, rc);
317
318 rc = grabIPCSemaphore();
319
320 /*
321 * Reference the VirtualBox object to ensure the server is up
322 * until the session is closed
323 */
324 if (SUCCEEDED (rc))
325 rc = aMachine->COMGETTER(Parent) (mVirtualBox.asOutParam());
326
327 if (SUCCEEDED (rc))
328 {
329 mType = SessionType_DirectSession;
330 mState = SessionState_SessionOpen;
331 }
332 else
333 {
334 /* some cleanup */
335 mControl.setNull();
336 mConsole->uninit();
337 mConsole.setNull();
338 }
339
340 LogFlowThisFunc (("rc=%08X\n", rc));
341 LogFlowThisFuncLeave();
342
343 return rc;
344}
345
346STDMETHODIMP Session::AssignRemoteMachine (IMachine *aMachine, IConsole *aConsole)
347{
348 LogFlowThisFuncEnter();
349 LogFlowThisFunc (("aMachine=%p, aConsole=%p\n", aMachine, aConsole));
350
351 AssertReturn (aMachine && aConsole, E_INVALIDARG);
352
353 AutoCaller autoCaller (this);
354 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
355
356 AutoLock alock (this);
357
358 AssertReturn (mState == SessionState_SessionClosed ||
359 mState == SessionState_SessionSpawning, E_FAIL);
360
361 HRESULT rc = E_FAIL;
362
363 /* query IInternalMachineControl interface */
364 mControl = aMachine;
365 AssertReturn (!!mControl, E_FAIL);
366
367 /// @todo (dmik)
368 // currently, the remote session returns the same machine and
369 // console objects as the direct session, thus giving the
370 // (remote) client full control over the direct session. For the
371 // console, it is the desired behavior (the ability to control
372 // VM execution is a must for the remote session). What about
373 // the machine object, we may want to prevent the remote client
374 // from modifying machine data. In this case, we must:
375 // 1) assign the Machine object (instead of the SessionMachine
376 // object that is passed to this method) to mRemoteMachine;
377 // 2) remove GetMachine() property from the IConsole interface
378 // because it always returns the SessionMachine object
379 // (alternatively, we can supply a separate IConsole
380 // implementation that will return the Machine object in
381 // response to GetMachine()).
382
383 mRemoteMachine = aMachine;
384 mRemoteConsole = aConsole;
385
386 /*
387 * Reference the VirtualBox object to ensure the server is up
388 * until the session is closed
389 */
390 rc = aMachine->COMGETTER(Parent) (mVirtualBox.asOutParam());
391
392 if (SUCCEEDED (rc))
393 {
394 /*
395 * RemoteSession type can be already set by AssignMachine() when its
396 * argument is NULL (a special case)
397 */
398 if (mType != SessionType_RemoteSession)
399 mType = SessionType_ExistingSession;
400 else
401 Assert (mState == SessionState_SessionSpawning);
402
403 mState = SessionState_SessionOpen;
404 }
405 else
406 {
407 /* some cleanup */
408 mControl.setNull();
409 mRemoteMachine.setNull();
410 mRemoteConsole.setNull();
411 }
412
413 LogFlowThisFunc (("rc=%08X\n", rc));
414 LogFlowThisFuncLeave();
415
416 return rc;
417}
418
419STDMETHODIMP Session::UpdateMachineState (MachineState_T aMachineState)
420{
421 AutoCaller autoCaller (this);
422
423 if (autoCaller.state() != Ready)
424 {
425 /*
426 * We might have already entered Session::uninit() at this point, so
427 * return silently (not interested in the state change during uninit)
428 */
429 LogFlowThisFunc (("Already uninitialized.\n"));
430 return S_OK;
431 }
432
433 AutoReaderLock alock (this);
434
435 if (mState == SessionState_SessionClosing)
436 {
437 LogFlowThisFunc (("Already being closed.\n"));
438 return S_OK;
439 }
440
441 AssertReturn (mState == SessionState_SessionOpen &&
442 mType == SessionType_DirectSession, E_FAIL);
443
444 AssertReturn (!mControl.isNull(), E_FAIL);
445 AssertReturn (!mConsole.isNull(), E_FAIL);
446
447 return mConsole->updateMachineState (aMachineState);
448}
449
450STDMETHODIMP Session::Uninitialize()
451{
452 LogFlowThisFuncEnter();
453
454 AutoCaller autoCaller (this);
455
456 HRESULT rc = S_OK;
457
458 if (autoCaller.state() == Ready)
459 {
460 AutoReaderLock alock (this);
461
462 LogFlowThisFunc (("mState=%d, mType=%d\n", mState, mType));
463
464 if (mState == SessionState_SessionClosing)
465 {
466 LogFlowThisFunc (("Already being closed.\n"));
467 return S_OK;
468 }
469
470 AssertReturn (mState == SessionState_SessionOpen, E_FAIL);
471
472 /* close ourselves */
473 rc = close (false /* aFinalRelease */, true /* aFromServer */);
474 }
475 else if (autoCaller.state() == InUninit)
476 {
477 /*
478 * We might have already entered Session::uninit() at this point,
479 * return silently
480 */
481 LogFlowThisFunc (("Already uninitialized.\n"));
482 }
483 else
484 {
485 LogWarningThisFunc (("UNEXPECTED uninitialization!\n"));
486 rc = autoCaller.rc();
487 }
488
489 LogFlowThisFunc (("rc=%08X\n", rc));
490 LogFlowThisFuncLeave();
491
492 return rc;
493}
494
495STDMETHODIMP Session::OnDVDDriveChange()
496{
497 LogFlowThisFunc (("\n"));
498
499 AutoCaller autoCaller (this);
500 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
501
502 AutoReaderLock alock (this);
503 AssertReturn (mState == SessionState_SessionOpen &&
504 mType == SessionType_DirectSession, E_FAIL);
505
506 return mConsole->onDVDDriveChange();
507}
508
509STDMETHODIMP Session::OnFloppyDriveChange()
510{
511 LogFlowThisFunc (("\n"));
512
513 AutoCaller autoCaller (this);
514 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
515
516 AutoReaderLock alock (this);
517 AssertReturn (mState == SessionState_SessionOpen &&
518 mType == SessionType_DirectSession, E_FAIL);
519
520 return mConsole->onFloppyDriveChange();
521}
522
523STDMETHODIMP Session::OnNetworkAdapterChange(INetworkAdapter *networkAdapter)
524{
525 LogFlowThisFunc (("\n"));
526
527 AutoCaller autoCaller (this);
528 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
529
530 AutoReaderLock alock (this);
531 AssertReturn (mState == SessionState_SessionOpen &&
532 mType == SessionType_DirectSession, E_FAIL);
533
534 return mConsole->onNetworkAdapterChange(networkAdapter);
535}
536
537STDMETHODIMP Session::OnSerialPortChange(ISerialPort *serialPort)
538{
539 LogFlowThisFunc (("\n"));
540
541 AutoCaller autoCaller (this);
542 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
543
544 AutoReaderLock alock (this);
545 AssertReturn (mState == SessionState_SessionOpen &&
546 mType == SessionType_DirectSession, E_FAIL);
547
548 return mConsole->onSerialPortChange(serialPort);
549}
550
551STDMETHODIMP Session::OnParallelPortChange(IParallelPort *parallelPort)
552{
553 LogFlowThisFunc (("\n"));
554
555 AutoCaller autoCaller (this);
556 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
557
558 AutoReaderLock alock (this);
559 AssertReturn (mState == SessionState_SessionOpen &&
560 mType == SessionType_DirectSession, E_FAIL);
561
562 return mConsole->onParallelPortChange(parallelPort);
563}
564
565STDMETHODIMP Session::OnVRDPServerChange()
566{
567 LogFlowThisFunc (("\n"));
568
569 AutoCaller autoCaller (this);
570 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
571
572 AutoReaderLock alock (this);
573 AssertReturn (mState == SessionState_SessionOpen &&
574 mType == SessionType_DirectSession, E_FAIL);
575
576 return mConsole->onVRDPServerChange();
577}
578
579STDMETHODIMP Session::OnUSBControllerChange()
580{
581 LogFlowThisFunc (("\n"));
582
583 AutoCaller autoCaller (this);
584 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
585
586 AutoReaderLock alock (this);
587 AssertReturn (mState == SessionState_SessionOpen &&
588 mType == SessionType_DirectSession, E_FAIL);
589
590 return mConsole->onUSBControllerChange();
591}
592
593STDMETHODIMP Session::OnSharedFolderChange (BOOL aGlobal)
594{
595 LogFlowThisFunc (("\n"));
596
597 AutoCaller autoCaller (this);
598 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
599
600 AutoReaderLock alock (this);
601 AssertReturn (mState == SessionState_SessionOpen &&
602 mType == SessionType_DirectSession, E_FAIL);
603
604 return mConsole->onSharedFolderChange (aGlobal);
605}
606
607STDMETHODIMP Session::OnUSBDeviceAttach (IUSBDevice *aDevice,
608 IVirtualBoxErrorInfo *aError)
609{
610 LogFlowThisFunc (("\n"));
611
612 AutoCaller autoCaller (this);
613 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
614
615 AutoReaderLock alock (this);
616 AssertReturn (mState == SessionState_SessionOpen &&
617 mType == SessionType_DirectSession, E_FAIL);
618
619 return mConsole->onUSBDeviceAttach (aDevice, aError);
620}
621
622STDMETHODIMP Session::OnUSBDeviceDetach (INPTR GUIDPARAM aId,
623 IVirtualBoxErrorInfo *aError)
624{
625 LogFlowThisFunc (("\n"));
626
627 AutoCaller autoCaller (this);
628 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
629
630 AutoReaderLock alock (this);
631 AssertReturn (mState == SessionState_SessionOpen &&
632 mType == SessionType_DirectSession, E_FAIL);
633
634 return mConsole->onUSBDeviceDetach (aId, aError);
635}
636
637STDMETHODIMP Session::OnShowWindow (BOOL aCheck, BOOL *aCanShow, ULONG64 *aWinId)
638{
639 AutoCaller autoCaller (this);
640 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
641
642 AutoReaderLock alock (this);
643 AssertReturn (mState == SessionState_SessionOpen &&
644 mType == SessionType_DirectSession, E_FAIL);
645
646 return mConsole->onShowWindow (aCheck, aCanShow, aWinId);
647}
648
649// private methods
650///////////////////////////////////////////////////////////////////////////////
651
652/**
653 * Closes the current session.
654 *
655 * @param aFinalRelease called as a result of FinalRelease()
656 * @param aFromServer called as a result of Uninitialize()
657 *
658 * @note To be called only from #uninit(), #Close() or #Uninitialize().
659 * @note Locks this object for writing.
660 */
661HRESULT Session::close (bool aFinalRelease, bool aFromServer)
662{
663 LogFlowThisFuncEnter();
664 LogFlowThisFunc (("aFinalRelease=%d, isFromServer=%d\n",
665 aFinalRelease, aFromServer));
666
667 AutoCaller autoCaller (this);
668 AssertComRCReturnRC (autoCaller.rc());
669
670 AutoLock alock (this);
671
672 LogFlowThisFunc (("mState=%d, mType=%d\n", mState, mType));
673
674 if (mState != SessionState_SessionOpen)
675 {
676 Assert (mState == SessionState_SessionSpawning);
677
678 /* The session object is going to be uninitialized by the client before
679 * it has been assigned a direct console of the machine the client
680 * requested to open a remote session to using IVirtualBox::
681 * openRemoteSession(). Theoretically it should not happen because
682 * openRemoteSession() doesn't return control to the client until the
683 * procedure is fully complete, so assert here. */
684 AssertFailed();
685
686 mState = SessionState_SessionClosed;
687 mType = SessionType_InvalidSessionType;
688#if defined(RT_OS_WINDOWS)
689 Assert (!mIPCSem && !mIPCThreadSem);
690#elif defined(RT_OS_OS2)
691 Assert (mIPCThread == NIL_RTTHREAD &&
692 mIPCThreadSem == NIL_RTSEMEVENT);
693#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
694 Assert (mIPCSem == -1);
695#else
696# error "Port me!"
697#endif
698 LogFlowThisFuncLeave();
699 return S_OK;
700 }
701
702 /* go to the closing state */
703 mState = SessionState_SessionClosing;
704
705 if (mType == SessionType_DirectSession)
706 {
707 mConsole->uninit();
708 mConsole.setNull();
709 }
710 else
711 {
712 mRemoteMachine.setNull();
713 mRemoteConsole.setNull();
714 }
715
716 ComPtr <IProgress> progress;
717
718 if (!aFinalRelease && !aFromServer)
719 {
720 /*
721 * We trigger OnSessionEnd() only when the session closes itself using
722 * Close(). Note that if isFinalRelease = TRUE here, this means that
723 * the client process has already initialized the termination procedure
724 * without issuing Close() and the IPC channel is no more operational --
725 * so we cannot call the server's method (it will definitely fail). The
726 * server will instead simply detect the abnormal client death (since
727 * OnSessionEnd() is not called) and reset the machine state to Aborted.
728 */
729
730 /*
731 * while waiting for OnSessionEnd() to complete one of our methods
732 * can be called by the server (for example, Uninitialize(), if the
733 * direct session has initiated a closure just a bit before us) so
734 * we need to release the lock to avoid deadlocks. The state is already
735 * SessionState_SessionClosing here, so it's safe.
736 */
737 alock.leave();
738
739 LogFlowThisFunc (("Calling mControl->OnSessionEnd()...\n"));
740 HRESULT rc = mControl->OnSessionEnd (this, progress.asOutParam());
741 LogFlowThisFunc (("mControl->OnSessionEnd()=%08X\n", rc));
742
743 alock.enter();
744
745 /*
746 * If we get E_UNEXPECTED this means that the direct session has already
747 * been closed, we're just too late with our notification and nothing more
748 */
749 if (mType != SessionType_DirectSession && rc == E_UNEXPECTED)
750 rc = S_OK;
751
752 AssertComRC (rc);
753 }
754
755 mControl.setNull();
756
757 if (mType == SessionType_DirectSession)
758 {
759 releaseIPCSemaphore();
760 if (!aFinalRelease && !aFromServer)
761 {
762 /*
763 * Wait for the server to grab the semaphore and destroy the session
764 * machine (allowing us to open a new session with the same machine
765 * once this method returns)
766 */
767 Assert (!!progress);
768 if (progress)
769 progress->WaitForCompletion (-1);
770 }
771 }
772
773 mState = SessionState_SessionClosed;
774 mType = SessionType_InvalidSessionType;
775
776 /* release the VirtualBox instance as the very last step */
777 mVirtualBox.setNull();
778
779 LogFlowThisFuncLeave();
780 return S_OK;
781}
782
783/** @note To be called only from #AssignMachine() */
784HRESULT Session::grabIPCSemaphore()
785{
786 HRESULT rc = E_FAIL;
787
788 /* open the IPC semaphore based on the sessionId and try to grab it */
789 Bstr ipcId;
790 rc = mControl->GetIPCId (ipcId.asOutParam());
791 AssertComRCReturnRC (rc);
792
793 LogFlowThisFunc (("ipcId='%ls'\n", ipcId.raw()));
794
795#if defined(RT_OS_WINDOWS)
796
797 /*
798 * Since Session is an MTA object, this method can be executed on
799 * any thread, and this thread will not necessarily match the thread on
800 * which close() will be called later. Therefore, we need a separate
801 * thread to hold the IPC mutex and then release it in close().
802 */
803
804 mIPCThreadSem = ::CreateEvent (NULL, FALSE, FALSE, NULL);
805 AssertMsgReturn (mIPCThreadSem,
806 ("Cannot create an event sem, err=%d", ::GetLastError()),
807 E_FAIL);
808
809 void *data [3];
810 data [0] = (void *) (BSTR) ipcId;
811 data [1] = (void *) mIPCThreadSem;
812 data [2] = 0; /* will get an output from the thread */
813
814 /* create a thread to hold the IPC mutex until signalled to release it */
815 RTTHREAD tid;
816 int vrc = RTThreadCreate (&tid, IPCMutexHolderThread, (void *) data,
817 0, RTTHREADTYPE_MAIN_WORKER, 0, "IPCHolder");
818 AssertRCReturn (vrc, E_FAIL);
819
820 /* wait until thread init is completed */
821 DWORD wrc = ::WaitForSingleObject (mIPCThreadSem, INFINITE);
822 AssertMsg (wrc == WAIT_OBJECT_0, ("Wait failed, err=%d\n", ::GetLastError()));
823 Assert (data [2]);
824
825 if (wrc == WAIT_OBJECT_0 && data [2])
826 {
827 /* memorize the event sem we should signal in close() */
828 mIPCSem = (HANDLE) data [2];
829 rc = S_OK;
830 }
831 else
832 {
833 ::CloseHandle (mIPCThreadSem);
834 mIPCThreadSem = NULL;
835 rc = E_FAIL;
836 }
837
838#elif defined(RT_OS_OS2)
839
840 /* We use XPCOM where any message (including close()) can arrive on any
841 * worker thread (which will not necessarily match this thread that opens
842 * the mutex). Therefore, we need a separate thread to hold the IPC mutex
843 * and then release it in close(). */
844
845 int vrc = RTSemEventCreate (&mIPCThreadSem);
846 AssertRCReturn (vrc, E_FAIL);
847
848 void *data [3];
849 data [0] = (void *) ipcId.raw();
850 data [1] = (void *) mIPCThreadSem;
851 data [2] = (void *) false; /* will get the thread result here */
852
853 /* create a thread to hold the IPC mutex until signalled to release it */
854 vrc = RTThreadCreate (&mIPCThread, IPCMutexHolderThread, (void *) data,
855 0, RTTHREADTYPE_MAIN_WORKER, 0, "IPCHolder");
856 AssertRCReturn (vrc, E_FAIL);
857
858 /* wait until thread init is completed */
859 vrc = RTThreadUserWait (mIPCThread, RT_INDEFINITE_WAIT);
860 AssertReturn (VBOX_SUCCESS (vrc) || vrc == VERR_INTERRUPTED, E_FAIL);
861
862 /* the thread must succeed */
863 AssertReturn ((bool) data [2], E_FAIL);
864
865#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
866
867 Utf8Str semName = ipcId;
868 char *pszSemName = NULL;
869 RTStrUtf8ToCurrentCP (&pszSemName, semName);
870 key_t key = ::ftok (pszSemName, 0);
871 RTStrFree (pszSemName);
872
873 mIPCSem = ::semget (key, 0, 0);
874 AssertMsgReturn (mIPCSem >= 0,
875 ("Cannot open IPC semaphore, errno=%d", errno),
876 E_FAIL);
877
878 /* grab the semaphore */
879 ::sembuf sop = { 0, -1, SEM_UNDO };
880 int rv = ::semop (mIPCSem, &sop, 1);
881 AssertMsgReturn (rv == 0,
882 ("Cannot grab IPC semaphore, errno=%d", errno),
883 E_FAIL);
884
885#else
886# error "Port me!"
887#endif
888
889 return rc;
890}
891
892/** @note To be called only from #close() */
893void Session::releaseIPCSemaphore()
894{
895 /* release the IPC semaphore */
896#if defined(RT_OS_WINDOWS)
897
898 if (mIPCSem && mIPCThreadSem)
899 {
900 /*
901 * tell the thread holding the IPC mutex to release it;
902 * it will close mIPCSem handle
903 */
904 ::SetEvent (mIPCSem);
905 /* wait for the thread to finish */
906 ::WaitForSingleObject (mIPCThreadSem, INFINITE);
907 ::CloseHandle (mIPCThreadSem);
908 }
909
910#elif defined(RT_OS_OS2)
911
912 if (mIPCThread != NIL_RTTHREAD)
913 {
914 Assert (mIPCThreadSem != NIL_RTSEMEVENT);
915
916 /* tell the thread holding the IPC mutex to release it */
917 int vrc = RTSemEventSignal (mIPCThreadSem);
918 AssertRC (vrc == NO_ERROR);
919
920 /* wait for the thread to finish */
921 vrc = RTThreadUserWait (mIPCThread, RT_INDEFINITE_WAIT);
922 Assert (VBOX_SUCCESS (vrc) || vrc == VERR_INTERRUPTED);
923
924 mIPCThread = NIL_RTTHREAD;
925 }
926
927 if (mIPCThreadSem != NIL_RTSEMEVENT)
928 {
929 RTSemEventDestroy (mIPCThreadSem);
930 mIPCThreadSem = NIL_RTSEMEVENT;
931 }
932
933#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
934
935 if (mIPCSem >= 0)
936 {
937 ::sembuf sop = { 0, 1, SEM_UNDO };
938 ::semop (mIPCSem, &sop, 1);
939 }
940
941#else
942# error "Port me!"
943#endif
944}
945
946#if defined(RT_OS_WINDOWS)
947/** VM IPC mutex holder thread */
948DECLCALLBACK(int) IPCMutexHolderThread (RTTHREAD Thread, void *pvUser)
949{
950 LogFlowFuncEnter();
951
952 Assert (pvUser);
953 void **data = (void **) pvUser;
954
955 BSTR sessionId = (BSTR) data [0];
956 HANDLE initDoneSem = (HANDLE) data [1];
957
958 HANDLE ipcMutex = ::OpenMutex (MUTEX_ALL_ACCESS, FALSE, sessionId);
959 AssertMsg (ipcMutex, ("cannot open IPC mutex, err=%d\n", ::GetLastError()));
960
961 if (ipcMutex)
962 {
963 /* grab the mutex */
964 DWORD wrc = ::WaitForSingleObject (ipcMutex, 0);
965 AssertMsg (wrc == WAIT_OBJECT_0, ("cannot grab IPC mutex, err=%d\n", wrc));
966 if (wrc == WAIT_OBJECT_0)
967 {
968 HANDLE finishSem = ::CreateEvent (NULL, FALSE, FALSE, NULL);
969 AssertMsg (finishSem, ("cannot create event sem, err=%d\n", ::GetLastError()));
970 if (finishSem)
971 {
972 data [2] = (void *) finishSem;
973 /* signal we're done with init */
974 ::SetEvent (initDoneSem);
975 /* wait until we're signaled to release the IPC mutex */
976 ::WaitForSingleObject (finishSem, INFINITE);
977 /* release the IPC mutex */
978 LogFlow (("IPCMutexHolderThread(): releasing IPC mutex...\n"));
979 BOOL success = ::ReleaseMutex (ipcMutex);
980 AssertMsg (success, ("cannot release mutex, err=%d\n", ::GetLastError()));
981 ::CloseHandle (ipcMutex);
982 ::CloseHandle (finishSem);
983 }
984 }
985 }
986
987 /* signal we're done */
988 ::SetEvent (initDoneSem);
989
990 LogFlowFuncLeave();
991
992 return 0;
993}
994#endif
995
996#if defined(RT_OS_OS2)
997/** VM IPC mutex holder thread */
998DECLCALLBACK(int) IPCMutexHolderThread (RTTHREAD Thread, void *pvUser)
999{
1000 LogFlowFuncEnter();
1001
1002 Assert (pvUser);
1003 void **data = (void **) pvUser;
1004
1005 Utf8Str ipcId = (BSTR) data [0];
1006 RTSEMEVENT finishSem = (RTSEMEVENT) data [1];
1007
1008 LogFlowFunc (("ipcId='%s', finishSem=%p\n", ipcId.raw(), finishSem));
1009
1010 HMTX ipcMutex = NULLHANDLE;
1011 APIRET arc = ::DosOpenMutexSem ((PSZ) ipcId.raw(), &ipcMutex);
1012 AssertMsg (arc == NO_ERROR, ("cannot open IPC mutex, arc=%ld\n", arc));
1013
1014 if (arc == NO_ERROR)
1015 {
1016 /* grab the mutex */
1017 LogFlowFunc (("grabbing IPC mutex...\n"));
1018 arc = ::DosRequestMutexSem (ipcMutex, SEM_IMMEDIATE_RETURN);
1019 AssertMsg (arc == NO_ERROR, ("cannot grab IPC mutex, arc=%ld\n", arc));
1020 if (arc == NO_ERROR)
1021 {
1022 /* store the answer */
1023 data [2] = (void *) true;
1024 /* signal we're done */
1025 int vrc = RTThreadUserSignal (Thread);
1026 AssertRC (vrc);
1027
1028 /* wait until we're signaled to release the IPC mutex */
1029 LogFlowFunc (("waiting for termination signal..\n"));
1030 vrc = RTSemEventWait (finishSem, RT_INDEFINITE_WAIT);
1031 Assert (arc == ERROR_INTERRUPT || ERROR_TIMEOUT);
1032
1033 /* release the IPC mutex */
1034 LogFlowFunc (("releasing IPC mutex...\n"));
1035 arc = ::DosReleaseMutexSem (ipcMutex);
1036 AssertMsg (arc == NO_ERROR, ("cannot release mutex, arc=%ld\n", arc));
1037 }
1038
1039 ::DosCloseMutexSem (ipcMutex);
1040 }
1041
1042 /* store the answer */
1043 data [1] = (void *) false;
1044 /* signal we're done */
1045 int vrc = RTThreadUserSignal (Thread);
1046 AssertRC (vrc);
1047
1048 LogFlowFuncLeave();
1049
1050 return 0;
1051}
1052#endif
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