VirtualBox

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

最後變更 在這個檔案從46578是 46465,由 vboxsync 提交於 12 年 前

Main/VPX: introduced onVideoCaptureChange event

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 42.2 KB
 
1/* $Id: SessionImpl.cpp 46465 2013-06-10 14:11:26Z vboxsync $ */
2/** @file
3 * VBox Client Session COM Class implementation in VBoxC.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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#ifdef VBOX_WITH_SYS_V_IPC_SESSION_WATCHER
19# include <errno.h>
20# include <sys/types.h>
21# include <sys/stat.h>
22# include <sys/ipc.h>
23# include <sys/sem.h>
24#endif
25
26#include "SessionImpl.h"
27#include "ConsoleImpl.h"
28#include "Global.h"
29
30#include "AutoCaller.h"
31#include "Logging.h"
32
33#include <VBox/err.h>
34#include <iprt/process.h>
35
36#if defined(RT_OS_WINDOWS) || defined (RT_OS_OS2)
37/** VM IPC mutex holder thread */
38static DECLCALLBACK(int) IPCMutexHolderThread(RTTHREAD Thread, void *pvUser);
39#endif
40
41/**
42 * Local macro to check whether the session is open and return an error if not.
43 * @note Don't forget to do |Auto[Reader]Lock alock (this);| before using this
44 * macro.
45 */
46#define CHECK_OPEN() \
47 do { \
48 if (mState != SessionState_Locked) \
49 return setError(E_UNEXPECTED, tr ("The session is not locked (session state: %s)"), Global::stringifySessionState(mState)); \
50 } while (0)
51
52// constructor / destructor
53/////////////////////////////////////////////////////////////////////////////
54
55HRESULT Session::FinalConstruct()
56{
57 LogFlowThisFunc(("\n"));
58
59 HRESULT rc = init();
60
61 BaseFinalConstruct();
62
63 return rc;
64}
65
66void Session::FinalRelease()
67{
68 LogFlowThisFunc(("\n"));
69
70 uninit();
71
72 BaseFinalRelease();
73}
74
75// public initializer/uninitializer for internal purposes only
76/////////////////////////////////////////////////////////////////////////////
77
78/**
79 * Initializes the Session object.
80 */
81HRESULT Session::init()
82{
83 /* Enclose the state transition NotReady->InInit->Ready */
84 AutoInitSpan autoInitSpan(this);
85 AssertReturn(autoInitSpan.isOk(), E_FAIL);
86
87 LogFlowThisFuncEnter();
88
89 mState = SessionState_Unlocked;
90 mType = SessionType_Null;
91
92#if defined(RT_OS_WINDOWS)
93 mIPCSem = NULL;
94 mIPCThreadSem = NULL;
95#elif defined(RT_OS_OS2)
96 mIPCThread = NIL_RTTHREAD;
97 mIPCThreadSem = NIL_RTSEMEVENT;
98#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
99 mIPCSem = -1;
100#else
101# error "Port me!"
102#endif
103
104 /* Confirm a successful initialization when it's the case */
105 autoInitSpan.setSucceeded();
106
107 LogFlowThisFuncLeave();
108
109 return S_OK;
110}
111
112/**
113 * Uninitializes the Session object.
114 *
115 * @note Locks this object for writing.
116 */
117void Session::uninit()
118{
119 LogFlowThisFuncEnter();
120
121 /* Enclose the state transition Ready->InUninit->NotReady */
122 AutoUninitSpan autoUninitSpan(this);
123 if (autoUninitSpan.uninitDone())
124 {
125 LogFlowThisFunc(("Already uninitialized.\n"));
126 LogFlowThisFuncLeave();
127 return;
128 }
129
130 /* close() needs write lock */
131 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
132
133 if (mState != SessionState_Unlocked)
134 {
135 Assert(mState == SessionState_Locked ||
136 mState == SessionState_Spawning);
137
138 HRESULT rc = unlockMachine(true /* aFinalRelease */, false /* aFromServer */);
139 AssertComRC(rc);
140 }
141
142 LogFlowThisFuncLeave();
143}
144
145// ISession properties
146/////////////////////////////////////////////////////////////////////////////
147
148STDMETHODIMP Session::COMGETTER(State)(SessionState_T *aState)
149{
150 CheckComArgOutPointerValid(aState);
151
152 AutoCaller autoCaller(this);
153 if (FAILED(autoCaller.rc())) return autoCaller.rc();
154
155 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
156
157 *aState = mState;
158
159 return S_OK;
160}
161
162STDMETHODIMP Session::COMGETTER(Type)(SessionType_T *aType)
163{
164 CheckComArgOutPointerValid(aType);
165
166 AutoCaller autoCaller(this);
167 if (FAILED(autoCaller.rc())) return autoCaller.rc();
168
169 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
170
171 CHECK_OPEN();
172
173 *aType = mType;
174 return S_OK;
175}
176
177STDMETHODIMP Session::COMGETTER(Machine)(IMachine **aMachine)
178{
179 CheckComArgOutPointerValid(aMachine);
180
181 AutoCaller autoCaller(this);
182 if (FAILED(autoCaller.rc())) return autoCaller.rc();
183
184 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
185
186 CHECK_OPEN();
187
188 HRESULT rc;
189 if (mConsole)
190 rc = mConsole->machine().queryInterfaceTo(aMachine);
191 else
192 rc = mRemoteMachine.queryInterfaceTo(aMachine);
193 if (FAILED(rc))
194 {
195 /** @todo VBox 3.3: replace E_FAIL with rc here. */
196 if (mConsole)
197 setError(E_FAIL, tr("Failed to query the session machine (%Rhrc)"), rc);
198 else if (FAILED_DEAD_INTERFACE(rc))
199 setError(E_FAIL, tr("Peer process crashed"));
200 else
201 setError(E_FAIL, tr("Failed to query the remote session machine (%Rhrc)"), rc);
202 }
203
204 return rc;
205}
206
207STDMETHODIMP Session::COMGETTER(Console)(IConsole **aConsole)
208{
209 CheckComArgOutPointerValid(aConsole);
210
211 AutoCaller autoCaller(this);
212 if (FAILED(autoCaller.rc())) return autoCaller.rc();
213
214 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
215
216 CHECK_OPEN();
217
218 HRESULT rc;
219 if (mConsole)
220 rc = mConsole.queryInterfaceTo(aConsole);
221 else
222 rc = mRemoteConsole.queryInterfaceTo(aConsole);
223
224 if (FAILED(rc))
225 {
226 /** @todo VBox 3.3: replace E_FAIL with rc here. */
227 if (mConsole)
228 setError(E_FAIL, tr("Failed to query the console (%Rhrc)"), rc);
229 else if (FAILED_DEAD_INTERFACE(rc))
230 setError(E_FAIL, tr("Peer process crashed"));
231 else
232 setError(E_FAIL, tr("Failed to query the remote console (%Rhrc)"), rc);
233 }
234
235 return rc;
236}
237
238// ISession methods
239/////////////////////////////////////////////////////////////////////////////
240
241STDMETHODIMP Session::UnlockMachine()
242{
243 LogFlowThisFunc(("mState=%d, mType=%d\n", mState, mType));
244
245 AutoCaller autoCaller(this);
246 if (FAILED(autoCaller.rc())) return autoCaller.rc();
247
248 /* close() needs write lock */
249 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
250
251 CHECK_OPEN();
252
253 return unlockMachine(false /* aFinalRelease */, false /* aFromServer */);
254}
255
256// IInternalSessionControl methods
257/////////////////////////////////////////////////////////////////////////////
258
259STDMETHODIMP Session::GetPID(ULONG *aPid)
260{
261 AssertReturn(aPid, E_POINTER);
262
263 AutoCaller autoCaller(this);
264 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
265
266 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
267
268 *aPid = (ULONG)RTProcSelf();
269 AssertCompile(sizeof(*aPid) == sizeof(RTPROCESS));
270
271 return S_OK;
272}
273
274STDMETHODIMP Session::GetRemoteConsole(IConsole **aConsole)
275{
276 LogFlowThisFuncEnter();
277 AssertReturn(aConsole, E_POINTER);
278
279 AutoCaller autoCaller(this);
280 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
281
282 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
283
284 AssertReturn(mState != SessionState_Unlocked, VBOX_E_INVALID_VM_STATE);
285
286 AssertMsgReturn(mType == SessionType_WriteLock && !!mConsole,
287 ("This is not a direct session!\n"),
288 VBOX_E_INVALID_OBJECT_STATE);
289
290 /* return a failure if the session already transitioned to Closing
291 * but the server hasn't processed Machine::OnSessionEnd() yet. */
292 if (mState != SessionState_Locked)
293 return VBOX_E_INVALID_VM_STATE;
294
295 mConsole.queryInterfaceTo(aConsole);
296
297 LogFlowThisFuncLeave();
298
299 return S_OK;
300}
301
302STDMETHODIMP Session::AssignMachine(IMachine *aMachine, LockType_T aLockType)
303{
304 LogFlowThisFuncEnter();
305 LogFlowThisFunc(("aMachine=%p\n", aMachine));
306
307 AutoCaller autoCaller(this);
308 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
309
310 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
311
312 AssertReturn(mState == SessionState_Unlocked, VBOX_E_INVALID_VM_STATE);
313
314 if (!aMachine)
315 {
316 /*
317 * A special case: the server informs us that this session has been
318 * passed to IMachine::launchVMProcess() so this session will become
319 * remote (but not existing) when AssignRemoteMachine() is called.
320 */
321
322 AssertReturn(mType == SessionType_Null, VBOX_E_INVALID_OBJECT_STATE);
323 mType = SessionType_Remote;
324 mState = SessionState_Spawning;
325
326 LogFlowThisFuncLeave();
327 return S_OK;
328 }
329
330 HRESULT rc = E_FAIL;
331
332 /* query IInternalMachineControl interface */
333 mControl = aMachine;
334 AssertReturn(!!mControl, E_FAIL);
335
336 rc = mConsole.createObject();
337 AssertComRCReturn(rc, rc);
338
339 rc = mConsole->init(aMachine, mControl, aLockType);
340 AssertComRCReturn(rc, rc);
341
342 rc = grabIPCSemaphore();
343
344 /*
345 * Reference the VirtualBox object to ensure the server is up
346 * until the session is closed
347 */
348 if (SUCCEEDED(rc))
349 rc = aMachine->COMGETTER(Parent)(mVirtualBox.asOutParam());
350
351 if (SUCCEEDED(rc))
352 {
353 mType = SessionType_WriteLock;
354 mState = SessionState_Locked;
355 }
356 else
357 {
358 /* some cleanup */
359 mControl.setNull();
360 if (!mConsole.isNull())
361 {
362 mConsole->uninit();
363 mConsole.setNull();
364 }
365 }
366
367 LogFlowThisFunc(("rc=%08X\n", rc));
368 LogFlowThisFuncLeave();
369
370 return rc;
371}
372
373STDMETHODIMP Session::AssignRemoteMachine(IMachine *aMachine, IConsole *aConsole)
374{
375 LogFlowThisFuncEnter();
376 LogFlowThisFunc(("aMachine=%p, aConsole=%p\n", aMachine, aConsole));
377
378 AssertReturn(aMachine && aConsole, E_INVALIDARG);
379
380 AutoCaller autoCaller(this);
381 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
382
383 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
384
385 AssertReturn(mState == SessionState_Unlocked ||
386 mState == SessionState_Spawning, VBOX_E_INVALID_VM_STATE);
387
388 HRESULT rc = E_FAIL;
389
390 /* query IInternalMachineControl interface */
391 mControl = aMachine;
392 AssertReturn(!!mControl, E_FAIL);
393
394 /// @todo (dmik)
395 // currently, the remote session returns the same machine and
396 // console objects as the direct session, thus giving the
397 // (remote) client full control over the direct session. For the
398 // console, it is the desired behavior (the ability to control
399 // VM execution is a must for the remote session). What about
400 // the machine object, we may want to prevent the remote client
401 // from modifying machine data. In this case, we must:
402 // 1) assign the Machine object (instead of the SessionMachine
403 // object that is passed to this method) to mRemoteMachine;
404 // 2) remove GetMachine() property from the IConsole interface
405 // because it always returns the SessionMachine object
406 // (alternatively, we can supply a separate IConsole
407 // implementation that will return the Machine object in
408 // response to GetMachine()).
409
410 mRemoteMachine = aMachine;
411 mRemoteConsole = aConsole;
412
413 /*
414 * Reference the VirtualBox object to ensure the server is up
415 * until the session is closed
416 */
417 rc = aMachine->COMGETTER(Parent)(mVirtualBox.asOutParam());
418
419 if (SUCCEEDED(rc))
420 {
421 /*
422 * RemoteSession type can be already set by AssignMachine() when its
423 * argument is NULL (a special case)
424 */
425 if (mType != SessionType_Remote)
426 mType = SessionType_Shared;
427 else
428 Assert(mState == SessionState_Spawning);
429
430 mState = SessionState_Locked;
431 }
432 else
433 {
434 /* some cleanup */
435 mControl.setNull();
436 mRemoteMachine.setNull();
437 mRemoteConsole.setNull();
438 }
439
440 LogFlowThisFunc(("rc=%08X\n", rc));
441 LogFlowThisFuncLeave();
442
443 return rc;
444}
445
446STDMETHODIMP Session::UpdateMachineState(MachineState_T aMachineState)
447{
448 AutoCaller autoCaller(this);
449
450 if (autoCaller.state() != Ready)
451 {
452 /*
453 * We might have already entered Session::uninit() at this point, so
454 * return silently (not interested in the state change during uninit)
455 */
456 LogFlowThisFunc(("Already uninitialized.\n"));
457 return S_OK;
458 }
459
460 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
461
462 if (mState == SessionState_Unlocking)
463 {
464 LogFlowThisFunc(("Already being unlocked.\n"));
465 return S_OK;
466 }
467
468 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
469 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
470
471 AssertReturn(!mControl.isNull(), E_FAIL);
472 AssertReturn(!mConsole.isNull(), E_FAIL);
473
474 return mConsole->updateMachineState(aMachineState);
475}
476
477STDMETHODIMP Session::Uninitialize()
478{
479 LogFlowThisFuncEnter();
480
481 AutoCaller autoCaller(this);
482
483 HRESULT rc = S_OK;
484
485 if (autoCaller.state() == Ready)
486 {
487 /* close() needs write lock */
488 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
489
490 LogFlowThisFunc(("mState=%s, mType=%d\n", Global::stringifySessionState(mState), mType));
491
492 if (mState == SessionState_Unlocking)
493 {
494 LogFlowThisFunc(("Already being unlocked.\n"));
495 return S_OK;
496 }
497
498#ifndef DEBUG_andy /* Don't bug me -- now time to fix this at the moment. */
499 AssertReturn(mState == SessionState_Locked ||
500 mState == SessionState_Spawning, VBOX_E_INVALID_VM_STATE);
501#endif
502
503 /* close ourselves */
504 rc = unlockMachine(false /* aFinalRelease */, true /* aFromServer */);
505 }
506 else if (autoCaller.state() == InUninit)
507 {
508 /*
509 * We might have already entered Session::uninit() at this point,
510 * return silently
511 */
512 LogFlowThisFunc(("Already uninitialized.\n"));
513 }
514 else
515 {
516 LogWarningThisFunc(("UNEXPECTED uninitialization!\n"));
517 rc = autoCaller.rc();
518 }
519
520 LogFlowThisFunc(("rc=%08X\n", rc));
521 LogFlowThisFuncLeave();
522
523 return rc;
524}
525
526STDMETHODIMP Session::OnNetworkAdapterChange(INetworkAdapter *networkAdapter, BOOL changeAdapter)
527{
528 LogFlowThisFunc(("\n"));
529
530 AutoCaller autoCaller(this);
531 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
532
533 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
534 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
535 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
536 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
537
538 return mConsole->onNetworkAdapterChange(networkAdapter, changeAdapter);
539}
540
541STDMETHODIMP Session::OnSerialPortChange(ISerialPort *serialPort)
542{
543 LogFlowThisFunc(("\n"));
544
545 AutoCaller autoCaller(this);
546 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
547
548 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
549 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
550 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
551 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
552
553 return mConsole->onSerialPortChange(serialPort);
554}
555
556STDMETHODIMP Session::OnParallelPortChange(IParallelPort *parallelPort)
557{
558 LogFlowThisFunc(("\n"));
559
560 AutoCaller autoCaller(this);
561 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
562
563 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
564 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
565 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
566 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
567
568 return mConsole->onParallelPortChange(parallelPort);
569}
570
571STDMETHODIMP Session::OnStorageControllerChange()
572{
573 LogFlowThisFunc(("\n"));
574
575 AutoCaller autoCaller(this);
576 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
577
578 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
579 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
580 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
581 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
582
583 return mConsole->onStorageControllerChange();
584}
585
586STDMETHODIMP Session::OnMediumChange(IMediumAttachment *aMediumAttachment, BOOL aForce)
587{
588 LogFlowThisFunc(("\n"));
589
590 AutoCaller autoCaller(this);
591 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
592
593 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
594 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
595 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
596 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
597
598 return mConsole->onMediumChange(aMediumAttachment, aForce);
599}
600
601STDMETHODIMP Session::OnCPUChange(ULONG aCPU, BOOL aRemove)
602{
603 LogFlowThisFunc(("\n"));
604
605 AutoCaller autoCaller(this);
606 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
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 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
612
613 return mConsole->onCPUChange(aCPU, aRemove);
614}
615
616STDMETHODIMP Session::OnCPUExecutionCapChange(ULONG aExecutionCap)
617{
618 LogFlowThisFunc(("\n"));
619
620 AutoCaller autoCaller(this);
621 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
622
623 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
624 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
625 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
626 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
627
628 return mConsole->onCPUExecutionCapChange(aExecutionCap);
629}
630
631STDMETHODIMP Session::OnVRDEServerChange(BOOL aRestart)
632{
633 LogFlowThisFunc(("\n"));
634
635 AutoCaller autoCaller(this);
636 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
637
638 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
639 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
640 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
641 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
642
643 return mConsole->onVRDEServerChange(aRestart);
644}
645
646STDMETHODIMP Session::OnVideoCaptureChange()
647{
648 LogFlowThisFunc(("\n"));
649
650 AutoCaller autoCaller(this);
651 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
652
653 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
654 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
655 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
656 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
657
658 return mConsole->onVideoCaptureChange();
659}
660
661STDMETHODIMP Session::OnUSBControllerChange()
662{
663 LogFlowThisFunc(("\n"));
664
665 AutoCaller autoCaller(this);
666 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
667
668 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
669 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
670 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
671 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
672
673 return mConsole->onUSBControllerChange();
674}
675
676STDMETHODIMP Session::OnSharedFolderChange(BOOL aGlobal)
677{
678 LogFlowThisFunc(("\n"));
679
680 AutoCaller autoCaller(this);
681 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
682
683 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
684 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
685 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
686 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
687
688 return mConsole->onSharedFolderChange(aGlobal);
689}
690
691STDMETHODIMP Session::OnClipboardModeChange(ClipboardMode_T aClipboardMode)
692{
693 LogFlowThisFunc(("\n"));
694
695 AutoCaller autoCaller(this);
696 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
697
698 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
699 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
700 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
701 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
702
703 return mConsole->onClipboardModeChange(aClipboardMode);
704}
705
706STDMETHODIMP Session::OnDragAndDropModeChange(DragAndDropMode_T aDragAndDropMode)
707{
708 LogFlowThisFunc(("\n"));
709
710 AutoCaller autoCaller(this);
711 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
712
713 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
714 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
715 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
716
717 return mConsole->onDragAndDropModeChange(aDragAndDropMode);
718}
719
720STDMETHODIMP Session::OnUSBDeviceAttach(IUSBDevice *aDevice,
721 IVirtualBoxErrorInfo *aError,
722 ULONG aMaskedIfs)
723{
724 LogFlowThisFunc(("\n"));
725
726 AutoCaller autoCaller(this);
727 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
728
729 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
730 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
731 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
732 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
733
734 return mConsole->onUSBDeviceAttach(aDevice, aError, aMaskedIfs);
735}
736
737STDMETHODIMP Session::OnUSBDeviceDetach(IN_BSTR aId,
738 IVirtualBoxErrorInfo *aError)
739{
740 LogFlowThisFunc(("\n"));
741
742 AutoCaller autoCaller(this);
743 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
744
745 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
746 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
747 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
748 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
749
750 return mConsole->onUSBDeviceDetach(aId, aError);
751}
752
753STDMETHODIMP Session::OnShowWindow(BOOL aCheck, BOOL *aCanShow, LONG64 *aWinId)
754{
755 AutoCaller autoCaller(this);
756 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
757
758 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
759
760 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
761 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
762
763 if (mState != SessionState_Locked)
764 {
765 /* the call from Machine issued when the session is open can arrive
766 * after the session starts closing or gets closed. Note that when
767 * aCheck is false, we return E_FAIL to indicate that aWinId we return
768 * is not valid */
769 *aCanShow = FALSE;
770 *aWinId = 0;
771 return aCheck ? S_OK : E_FAIL;
772 }
773
774 return mConsole->onShowWindow(aCheck, aCanShow, aWinId);
775}
776
777STDMETHODIMP Session::OnBandwidthGroupChange(IBandwidthGroup *aBandwidthGroup)
778{
779 LogFlowThisFunc(("\n"));
780
781 AutoCaller autoCaller(this);
782 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
783
784 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
785 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
786 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
787 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
788
789 return mConsole->onBandwidthGroupChange(aBandwidthGroup);
790}
791
792STDMETHODIMP Session::OnStorageDeviceChange(IMediumAttachment *aMediumAttachment, BOOL aRemove, BOOL aSilent)
793{
794 LogFlowThisFunc(("\n"));
795
796 AutoCaller autoCaller(this);
797 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
798
799 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
800 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
801 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
802 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
803
804 return mConsole->onStorageDeviceChange(aMediumAttachment, aRemove, aSilent);
805}
806
807STDMETHODIMP Session::AccessGuestProperty(IN_BSTR aName, IN_BSTR aValue, IN_BSTR aFlags,
808 BOOL aIsSetter, BSTR *aRetValue, LONG64 *aRetTimestamp, BSTR *aRetFlags)
809{
810#ifdef VBOX_WITH_GUEST_PROPS
811 AutoCaller autoCaller(this);
812 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
813
814 if (mState != SessionState_Locked)
815 return setError(VBOX_E_INVALID_VM_STATE,
816 tr("Machine is not locked by session (session state: %s)."),
817 Global::stringifySessionState(mState));
818 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
819 CheckComArgStrNotEmptyOrNull(aName);
820 if (!aIsSetter && !VALID_PTR(aRetValue))
821 return E_POINTER;
822 if (!aIsSetter && !VALID_PTR(aRetTimestamp))
823 return E_POINTER;
824 if (!aIsSetter && !VALID_PTR(aRetFlags))
825 return E_POINTER;
826 /* aValue can be NULL for a setter call if the property is to be deleted. */
827 if (aIsSetter && (aValue != NULL) && !VALID_PTR(aValue))
828 return setError(E_INVALIDARG, tr("Invalid value pointer"));
829 /* aFlags can be null if it is to be left as is */
830 if (aIsSetter && (aFlags != NULL) && !VALID_PTR(aFlags))
831 return setError(E_INVALIDARG, tr("Invalid flags pointer"));
832
833 /* If this session is not in a VM process fend off the call. The caller
834 * handles this correctly, by doing the operation in VBoxSVC. */
835 if (!mConsole)
836 return E_ACCESSDENIED;
837
838 if (!aIsSetter)
839 return mConsole->getGuestProperty(aName, aRetValue, aRetTimestamp, aRetFlags);
840 else
841 return mConsole->setGuestProperty(aName, aValue, aFlags);
842#else /* VBOX_WITH_GUEST_PROPS not defined */
843 ReturnComNotImplemented();
844#endif /* VBOX_WITH_GUEST_PROPS not defined */
845}
846
847STDMETHODIMP Session::EnumerateGuestProperties(IN_BSTR aPatterns,
848 ComSafeArrayOut(BSTR, aNames),
849 ComSafeArrayOut(BSTR, aValues),
850 ComSafeArrayOut(LONG64, aTimestamps),
851 ComSafeArrayOut(BSTR, aFlags))
852{
853#ifdef VBOX_WITH_GUEST_PROPS
854 AutoCaller autoCaller(this);
855 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
856
857 if (mState != SessionState_Locked)
858 return setError(VBOX_E_INVALID_VM_STATE,
859 tr("Machine is not locked by session (session state: %s)."),
860 Global::stringifySessionState(mState));
861 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
862 if (!VALID_PTR(aPatterns) && (aPatterns != NULL))
863 return E_POINTER;
864 if (ComSafeArrayOutIsNull(aNames))
865 return E_POINTER;
866 if (ComSafeArrayOutIsNull(aValues))
867 return E_POINTER;
868 if (ComSafeArrayOutIsNull(aTimestamps))
869 return E_POINTER;
870 if (ComSafeArrayOutIsNull(aFlags))
871 return E_POINTER;
872
873 /* If this session is not in a VM process fend off the call. The caller
874 * handles this correctly, by doing the operation in VBoxSVC. */
875 if (!mConsole)
876 return E_ACCESSDENIED;
877
878 return mConsole->enumerateGuestProperties(aPatterns,
879 ComSafeArrayOutArg(aNames),
880 ComSafeArrayOutArg(aValues),
881 ComSafeArrayOutArg(aTimestamps),
882 ComSafeArrayOutArg(aFlags));
883#else /* VBOX_WITH_GUEST_PROPS not defined */
884 ReturnComNotImplemented();
885#endif /* VBOX_WITH_GUEST_PROPS not defined */
886}
887
888STDMETHODIMP Session::OnlineMergeMedium(IMediumAttachment *aMediumAttachment,
889 ULONG aSourceIdx, ULONG aTargetIdx,
890 IMedium *aSource, IMedium *aTarget,
891 BOOL aMergeForward,
892 IMedium *aParentForTarget,
893 ComSafeArrayIn(IMedium *, aChildrenToReparent),
894 IProgress *aProgress)
895{
896 AutoCaller autoCaller(this);
897 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
898
899 if (mState != SessionState_Locked)
900 return setError(VBOX_E_INVALID_VM_STATE,
901 tr("Machine is not locked by session (session state: %s)."),
902 Global::stringifySessionState(mState));
903 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
904 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
905 CheckComArgNotNull(aMediumAttachment);
906 CheckComArgSafeArrayNotNull(aChildrenToReparent);
907
908 return mConsole->onlineMergeMedium(aMediumAttachment, aSourceIdx,
909 aTargetIdx, aSource, aTarget,
910 aMergeForward, aParentForTarget,
911 ComSafeArrayInArg(aChildrenToReparent),
912 aProgress);
913}
914
915STDMETHODIMP Session::EnableVMMStatistics(BOOL aEnable)
916{
917 AutoCaller autoCaller(this);
918 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
919
920 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
921 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
922 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
923 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
924
925 mConsole->enableVMMStatistics(aEnable);
926
927 return S_OK;
928}
929
930// private methods
931///////////////////////////////////////////////////////////////////////////////
932
933/**
934 * Unlocks a machine associated with the current session.
935 *
936 * @param aFinalRelease called as a result of FinalRelease()
937 * @param aFromServer called as a result of Uninitialize()
938 *
939 * @note To be called only from #uninit(), #UnlockMachine() or #Uninitialize().
940 * @note Locks this object for writing.
941 */
942HRESULT Session::unlockMachine(bool aFinalRelease, bool aFromServer)
943{
944 LogFlowThisFuncEnter();
945 LogFlowThisFunc(("aFinalRelease=%d, isFromServer=%d\n",
946 aFinalRelease, aFromServer));
947
948 AutoCaller autoCaller(this);
949 AssertComRCReturnRC(autoCaller.rc());
950
951 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
952
953 LogFlowThisFunc(("mState=%s, mType=%d\n", Global::stringifySessionState(mState), mType));
954
955 if (mState != SessionState_Locked)
956 {
957 Assert(mState == SessionState_Spawning);
958
959 /* The session object is going to be uninitialized before it has been
960 * assigned a direct console of the machine the client requested to open
961 * a remote session to using IVirtualBox:: openRemoteSession(). It is OK
962 * only if this close request comes from the server (for example, it
963 * detected that the VM process it started terminated before opening a
964 * direct session). Otherwise, it means that the client is too fast and
965 * trying to close the session before waiting for the progress object it
966 * got from IVirtualBox:: openRemoteSession() to complete, so assert. */
967 Assert(aFromServer);
968
969 mState = SessionState_Unlocked;
970 mType = SessionType_Null;
971#if defined(RT_OS_WINDOWS)
972 Assert(!mIPCSem && !mIPCThreadSem);
973#elif defined(RT_OS_OS2)
974 Assert(mIPCThread == NIL_RTTHREAD &&
975 mIPCThreadSem == NIL_RTSEMEVENT);
976#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
977 Assert(mIPCSem == -1);
978#else
979# error "Port me!"
980#endif
981 LogFlowThisFuncLeave();
982 return S_OK;
983 }
984
985 /* go to the closing state */
986 mState = SessionState_Unlocking;
987
988 if (mType == SessionType_WriteLock)
989 {
990 if (!mConsole.isNull())
991 {
992 mConsole->uninit();
993 mConsole.setNull();
994 }
995 }
996 else
997 {
998 mRemoteMachine.setNull();
999 mRemoteConsole.setNull();
1000 }
1001
1002 ComPtr<IProgress> progress;
1003
1004 if (!aFinalRelease && !aFromServer)
1005 {
1006 /*
1007 * We trigger OnSessionEnd() only when the session closes itself using
1008 * Close(). Note that if isFinalRelease = TRUE here, this means that
1009 * the client process has already initialized the termination procedure
1010 * without issuing Close() and the IPC channel is no more operational --
1011 * so we cannot call the server's method (it will definitely fail). The
1012 * server will instead simply detect the abnormal client death (since
1013 * OnSessionEnd() is not called) and reset the machine state to Aborted.
1014 */
1015
1016 /*
1017 * while waiting for OnSessionEnd() to complete one of our methods
1018 * can be called by the server (for example, Uninitialize(), if the
1019 * direct session has initiated a closure just a bit before us) so
1020 * we need to release the lock to avoid deadlocks. The state is already
1021 * SessionState_Closing here, so it's safe.
1022 */
1023 alock.release();
1024
1025 LogFlowThisFunc(("Calling mControl->OnSessionEnd()...\n"));
1026 HRESULT rc = mControl->OnSessionEnd(this, progress.asOutParam());
1027 LogFlowThisFunc(("mControl->OnSessionEnd()=%08X\n", rc));
1028
1029 alock.acquire();
1030
1031 /*
1032 * If we get E_UNEXPECTED this means that the direct session has already
1033 * been closed, we're just too late with our notification and nothing more
1034 *
1035 * bird: Seems E_ACCESSDENIED is what gets returned these days; see
1036 * VirtualBoxBase::addCaller.
1037 */
1038 if (mType != SessionType_WriteLock && (rc == E_UNEXPECTED || rc == E_ACCESSDENIED))
1039 rc = S_OK;
1040
1041#ifndef DEBUG_bird /* I don't want clients crashing on me just because VBoxSVC went belly up. */
1042 AssertComRC(rc);
1043#endif
1044 }
1045
1046 mControl.setNull();
1047
1048 if (mType == SessionType_WriteLock)
1049 {
1050 releaseIPCSemaphore();
1051 if (!aFinalRelease && !aFromServer)
1052 {
1053 /*
1054 * Wait for the server to grab the semaphore and destroy the session
1055 * machine (allowing us to open a new session with the same machine
1056 * once this method returns)
1057 */
1058 Assert(!!progress);
1059 if (progress)
1060 progress->WaitForCompletion(-1);
1061 }
1062 }
1063
1064 mState = SessionState_Unlocked;
1065 mType = SessionType_Null;
1066
1067 /* release the VirtualBox instance as the very last step */
1068 mVirtualBox.setNull();
1069
1070 LogFlowThisFuncLeave();
1071 return S_OK;
1072}
1073
1074/** @note To be called only from #AssignMachine() */
1075HRESULT Session::grabIPCSemaphore()
1076{
1077 HRESULT rc = E_FAIL;
1078
1079 /* open the IPC semaphore based on the sessionId and try to grab it */
1080 Bstr ipcId;
1081 rc = mControl->GetIPCId(ipcId.asOutParam());
1082 AssertComRCReturnRC(rc);
1083
1084 LogFlowThisFunc(("ipcId='%ls'\n", ipcId.raw()));
1085
1086#if defined(RT_OS_WINDOWS)
1087
1088 /*
1089 * Since Session is an MTA object, this method can be executed on
1090 * any thread, and this thread will not necessarily match the thread on
1091 * which close() will be called later. Therefore, we need a separate
1092 * thread to hold the IPC mutex and then release it in close().
1093 */
1094
1095 mIPCThreadSem = ::CreateEvent(NULL, FALSE, FALSE, NULL);
1096 AssertMsgReturn(mIPCThreadSem,
1097 ("Cannot create an event sem, err=%d", ::GetLastError()),
1098 E_FAIL);
1099
1100 void *data[3];
1101 data[0] = (void*)(BSTR)ipcId.raw();
1102 data[1] = (void*)mIPCThreadSem;
1103 data[2] = 0; /* will get an output from the thread */
1104
1105 /* create a thread to hold the IPC mutex until signalled to release it */
1106 RTTHREAD tid;
1107 int vrc = RTThreadCreate(&tid, IPCMutexHolderThread, (void*)data, 0, RTTHREADTYPE_MAIN_WORKER, 0, "IPCHolder");
1108 AssertRCReturn(vrc, E_FAIL);
1109
1110 /* wait until thread init is completed */
1111 DWORD wrc = ::WaitForSingleObject(mIPCThreadSem, INFINITE);
1112 AssertMsg(wrc == WAIT_OBJECT_0, ("Wait failed, err=%d\n", ::GetLastError()));
1113 Assert(data[2]);
1114
1115 if (wrc == WAIT_OBJECT_0 && data[2])
1116 {
1117 /* memorize the event sem we should signal in close() */
1118 mIPCSem = (HANDLE)data[2];
1119 rc = S_OK;
1120 }
1121 else
1122 {
1123 ::CloseHandle(mIPCThreadSem);
1124 mIPCThreadSem = NULL;
1125 rc = E_FAIL;
1126 }
1127
1128#elif defined(RT_OS_OS2)
1129
1130 /* We use XPCOM where any message (including close()) can arrive on any
1131 * worker thread (which will not necessarily match this thread that opens
1132 * the mutex). Therefore, we need a separate thread to hold the IPC mutex
1133 * and then release it in close(). */
1134
1135 int vrc = RTSemEventCreate(&mIPCThreadSem);
1136 AssertRCReturn(vrc, E_FAIL);
1137
1138 void *data[3];
1139 data[0] = (void*)ipcId.raw();
1140 data[1] = (void*)mIPCThreadSem;
1141 data[2] = (void*)false; /* will get the thread result here */
1142
1143 /* create a thread to hold the IPC mutex until signalled to release it */
1144 vrc = RTThreadCreate(&mIPCThread, IPCMutexHolderThread, (void *) data,
1145 0, RTTHREADTYPE_MAIN_WORKER, 0, "IPCHolder");
1146 AssertRCReturn(vrc, E_FAIL);
1147
1148 /* wait until thread init is completed */
1149 vrc = RTThreadUserWait (mIPCThread, RT_INDEFINITE_WAIT);
1150 AssertReturn(RT_SUCCESS(vrc) || vrc == VERR_INTERRUPTED, E_FAIL);
1151
1152 /* the thread must succeed */
1153 AssertReturn((bool)data[2], E_FAIL);
1154
1155#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
1156
1157# ifdef VBOX_WITH_NEW_SYS_V_KEYGEN
1158 Utf8Str ipcKey = ipcId;
1159 key_t key = RTStrToUInt32(ipcKey.c_str());
1160 AssertMsgReturn (key != 0,
1161 ("Key value of 0 is not valid for IPC semaphore"),
1162 E_FAIL);
1163# else /* !VBOX_WITH_NEW_SYS_V_KEYGEN */
1164 Utf8Str semName = ipcId;
1165 char *pszSemName = NULL;
1166 RTStrUtf8ToCurrentCP (&pszSemName, semName);
1167 key_t key = ::ftok (pszSemName, 'V');
1168 RTStrFree (pszSemName);
1169# endif /* !VBOX_WITH_NEW_SYS_V_KEYGEN */
1170
1171 mIPCSem = ::semget (key, 0, 0);
1172 AssertMsgReturn (mIPCSem >= 0,
1173 ("Cannot open IPC semaphore, errno=%d", errno),
1174 E_FAIL);
1175
1176 /* grab the semaphore */
1177 ::sembuf sop = { 0, -1, SEM_UNDO };
1178 int rv = ::semop (mIPCSem, &sop, 1);
1179 AssertMsgReturn (rv == 0,
1180 ("Cannot grab IPC semaphore, errno=%d", errno),
1181 E_FAIL);
1182
1183#else
1184# error "Port me!"
1185#endif
1186
1187 return rc;
1188}
1189
1190/** @note To be called only from #close() */
1191void Session::releaseIPCSemaphore()
1192{
1193 /* release the IPC semaphore */
1194#if defined(RT_OS_WINDOWS)
1195
1196 if (mIPCSem && mIPCThreadSem)
1197 {
1198 /*
1199 * tell the thread holding the IPC mutex to release it;
1200 * it will close mIPCSem handle
1201 */
1202 ::SetEvent (mIPCSem);
1203 /* wait for the thread to finish */
1204 ::WaitForSingleObject (mIPCThreadSem, INFINITE);
1205 ::CloseHandle (mIPCThreadSem);
1206
1207 mIPCThreadSem = NULL;
1208 mIPCSem = NULL;
1209 }
1210
1211#elif defined(RT_OS_OS2)
1212
1213 if (mIPCThread != NIL_RTTHREAD)
1214 {
1215 Assert (mIPCThreadSem != NIL_RTSEMEVENT);
1216
1217 /* tell the thread holding the IPC mutex to release it */
1218 int vrc = RTSemEventSignal (mIPCThreadSem);
1219 AssertRC(vrc == NO_ERROR);
1220
1221 /* wait for the thread to finish */
1222 vrc = RTThreadUserWait (mIPCThread, RT_INDEFINITE_WAIT);
1223 Assert (RT_SUCCESS(vrc) || vrc == VERR_INTERRUPTED);
1224
1225 mIPCThread = NIL_RTTHREAD;
1226 }
1227
1228 if (mIPCThreadSem != NIL_RTSEMEVENT)
1229 {
1230 RTSemEventDestroy (mIPCThreadSem);
1231 mIPCThreadSem = NIL_RTSEMEVENT;
1232 }
1233
1234#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
1235
1236 if (mIPCSem >= 0)
1237 {
1238 ::sembuf sop = { 0, 1, SEM_UNDO };
1239 ::semop (mIPCSem, &sop, 1);
1240
1241 mIPCSem = -1;
1242 }
1243
1244#else
1245# error "Port me!"
1246#endif
1247}
1248
1249#if defined(RT_OS_WINDOWS)
1250/** VM IPC mutex holder thread */
1251DECLCALLBACK(int) IPCMutexHolderThread (RTTHREAD Thread, void *pvUser)
1252{
1253 LogFlowFuncEnter();
1254
1255 Assert (pvUser);
1256 void **data = (void **) pvUser;
1257
1258 BSTR sessionId = (BSTR)data[0];
1259 HANDLE initDoneSem = (HANDLE)data[1];
1260
1261 HANDLE ipcMutex = ::OpenMutex (MUTEX_ALL_ACCESS, FALSE, sessionId);
1262 AssertMsg (ipcMutex, ("cannot open IPC mutex, err=%d\n", ::GetLastError()));
1263
1264 if (ipcMutex)
1265 {
1266 /* grab the mutex */
1267 DWORD wrc = ::WaitForSingleObject (ipcMutex, 0);
1268 AssertMsg (wrc == WAIT_OBJECT_0, ("cannot grab IPC mutex, err=%d\n", wrc));
1269 if (wrc == WAIT_OBJECT_0)
1270 {
1271 HANDLE finishSem = ::CreateEvent (NULL, FALSE, FALSE, NULL);
1272 AssertMsg (finishSem, ("cannot create event sem, err=%d\n", ::GetLastError()));
1273 if (finishSem)
1274 {
1275 data[2] = (void*)finishSem;
1276 /* signal we're done with init */
1277 ::SetEvent (initDoneSem);
1278 /* wait until we're signaled to release the IPC mutex */
1279 ::WaitForSingleObject (finishSem, INFINITE);
1280 /* release the IPC mutex */
1281 LogFlow (("IPCMutexHolderThread(): releasing IPC mutex...\n"));
1282 BOOL success = ::ReleaseMutex (ipcMutex);
1283 AssertMsg (success, ("cannot release mutex, err=%d\n", ::GetLastError()));
1284 ::CloseHandle (ipcMutex);
1285 ::CloseHandle (finishSem);
1286 }
1287 }
1288 }
1289
1290 /* signal we're done */
1291 ::SetEvent (initDoneSem);
1292
1293 LogFlowFuncLeave();
1294
1295 return 0;
1296}
1297#endif
1298
1299#if defined(RT_OS_OS2)
1300/** VM IPC mutex holder thread */
1301DECLCALLBACK(int) IPCMutexHolderThread (RTTHREAD Thread, void *pvUser)
1302{
1303 LogFlowFuncEnter();
1304
1305 Assert (pvUser);
1306 void **data = (void **) pvUser;
1307
1308 Utf8Str ipcId = (BSTR)data[0];
1309 RTSEMEVENT finishSem = (RTSEMEVENT)data[1];
1310
1311 LogFlowFunc (("ipcId='%s', finishSem=%p\n", ipcId.raw(), finishSem));
1312
1313 HMTX ipcMutex = NULLHANDLE;
1314 APIRET arc = ::DosOpenMutexSem ((PSZ) ipcId.raw(), &ipcMutex);
1315 AssertMsg (arc == NO_ERROR, ("cannot open IPC mutex, arc=%ld\n", arc));
1316
1317 if (arc == NO_ERROR)
1318 {
1319 /* grab the mutex */
1320 LogFlowFunc (("grabbing IPC mutex...\n"));
1321 arc = ::DosRequestMutexSem (ipcMutex, SEM_IMMEDIATE_RETURN);
1322 AssertMsg (arc == NO_ERROR, ("cannot grab IPC mutex, arc=%ld\n", arc));
1323 if (arc == NO_ERROR)
1324 {
1325 /* store the answer */
1326 data[2] = (void*)true;
1327 /* signal we're done */
1328 int vrc = RTThreadUserSignal (Thread);
1329 AssertRC(vrc);
1330
1331 /* wait until we're signaled to release the IPC mutex */
1332 LogFlowFunc (("waiting for termination signal..\n"));
1333 vrc = RTSemEventWait (finishSem, RT_INDEFINITE_WAIT);
1334 Assert (arc == ERROR_INTERRUPT || ERROR_TIMEOUT);
1335
1336 /* release the IPC mutex */
1337 LogFlowFunc (("releasing IPC mutex...\n"));
1338 arc = ::DosReleaseMutexSem (ipcMutex);
1339 AssertMsg (arc == NO_ERROR, ("cannot release mutex, arc=%ld\n", arc));
1340 }
1341
1342 ::DosCloseMutexSem (ipcMutex);
1343 }
1344
1345 /* store the answer */
1346 data[1] = (void*)false;
1347 /* signal we're done */
1348 int vrc = RTThreadUserSignal (Thread);
1349 AssertRC(vrc);
1350
1351 LogFlowFuncLeave();
1352
1353 return 0;
1354}
1355#endif
1356/* vi: set tabstop=4 shiftwidth=4 expandtab: */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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