VirtualBox

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

Last change on this file since 42693 was 42382, checked in by vboxsync, 12 years ago

Main/Session+Console+Machine: minimal change to reduce the overhead of normal API client sessions, because they don't need a full Console object with all sub-objects associated with them. Much more work todo if we want to completely eliminate all VM machinery dependency since the methods of Console are a wild mix of VM related and unrelated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 41.6 KB
Line 
1/* $Id: SessionImpl.cpp 42382 2012-07-25 09:35:56Z 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.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#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::OnUSBControllerChange()
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->onUSBControllerChange();
659}
660
661STDMETHODIMP Session::OnSharedFolderChange(BOOL aGlobal)
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->onSharedFolderChange(aGlobal);
674}
675
676STDMETHODIMP Session::OnClipboardModeChange(ClipboardMode_T aClipboardMode)
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->onClipboardModeChange(aClipboardMode);
689}
690
691STDMETHODIMP Session::OnDragAndDropModeChange(DragAndDropMode_T aDragAndDropMode)
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
702 return mConsole->onDragAndDropModeChange(aDragAndDropMode);
703}
704
705STDMETHODIMP Session::OnUSBDeviceAttach(IUSBDevice *aDevice,
706 IVirtualBoxErrorInfo *aError,
707 ULONG aMaskedIfs)
708{
709 LogFlowThisFunc(("\n"));
710
711 AutoCaller autoCaller(this);
712 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
713
714 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
715 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
716 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
717 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
718
719 return mConsole->onUSBDeviceAttach(aDevice, aError, aMaskedIfs);
720}
721
722STDMETHODIMP Session::OnUSBDeviceDetach(IN_BSTR aId,
723 IVirtualBoxErrorInfo *aError)
724{
725 LogFlowThisFunc(("\n"));
726
727 AutoCaller autoCaller(this);
728 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
729
730 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
731 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
732 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
733 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
734
735 return mConsole->onUSBDeviceDetach(aId, aError);
736}
737
738STDMETHODIMP Session::OnShowWindow(BOOL aCheck, BOOL *aCanShow, LONG64 *aWinId)
739{
740 AutoCaller autoCaller(this);
741 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
742
743 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
744
745 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
746 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
747
748 if (mState != SessionState_Locked)
749 {
750 /* the call from Machine issued when the session is open can arrive
751 * after the session starts closing or gets closed. Note that when
752 * aCheck is false, we return E_FAIL to indicate that aWinId we return
753 * is not valid */
754 *aCanShow = FALSE;
755 *aWinId = 0;
756 return aCheck ? S_OK : E_FAIL;
757 }
758
759 return mConsole->onShowWindow(aCheck, aCanShow, aWinId);
760}
761
762STDMETHODIMP Session::OnBandwidthGroupChange(IBandwidthGroup *aBandwidthGroup)
763{
764 LogFlowThisFunc(("\n"));
765
766 AutoCaller autoCaller(this);
767 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
768
769 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
770 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
771 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
772 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
773
774 return mConsole->onBandwidthGroupChange(aBandwidthGroup);
775}
776
777STDMETHODIMP Session::OnStorageDeviceChange(IMediumAttachment *aMediumAttachment, BOOL aRemove)
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->onStorageDeviceChange(aMediumAttachment, aRemove);
790}
791
792STDMETHODIMP Session::AccessGuestProperty(IN_BSTR aName, IN_BSTR aValue, IN_BSTR aFlags,
793 BOOL aIsSetter, BSTR *aRetValue, LONG64 *aRetTimestamp, BSTR *aRetFlags)
794{
795#ifdef VBOX_WITH_GUEST_PROPS
796 AutoCaller autoCaller(this);
797 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
798
799 if (mState != SessionState_Locked)
800 return setError(VBOX_E_INVALID_VM_STATE,
801 tr("Machine is not locked by session (session state: %s)."),
802 Global::stringifySessionState(mState));
803 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
804 CheckComArgStrNotEmptyOrNull(aName);
805 if (!aIsSetter && !VALID_PTR(aRetValue))
806 return E_POINTER;
807 if (!aIsSetter && !VALID_PTR(aRetTimestamp))
808 return E_POINTER;
809 if (!aIsSetter && !VALID_PTR(aRetFlags))
810 return E_POINTER;
811 /* aValue can be NULL for a setter call if the property is to be deleted. */
812 if (aIsSetter && (aValue != NULL) && !VALID_PTR(aValue))
813 return E_INVALIDARG;
814 /* aFlags can be null if it is to be left as is */
815 if (aIsSetter && (aFlags != NULL) && !VALID_PTR(aFlags))
816 return E_INVALIDARG;
817
818 /* If this session is not in a VM process fend off the call. The caller
819 * handles this correctly, by doing the operation in VBoxSVC. */
820 if (!mConsole)
821 return E_ACCESSDENIED;
822
823 if (!aIsSetter)
824 return mConsole->getGuestProperty(aName, aRetValue, aRetTimestamp, aRetFlags);
825 else
826 return mConsole->setGuestProperty(aName, aValue, aFlags);
827#else /* VBOX_WITH_GUEST_PROPS not defined */
828 ReturnComNotImplemented();
829#endif /* VBOX_WITH_GUEST_PROPS not defined */
830}
831
832STDMETHODIMP Session::EnumerateGuestProperties(IN_BSTR aPatterns,
833 ComSafeArrayOut(BSTR, aNames),
834 ComSafeArrayOut(BSTR, aValues),
835 ComSafeArrayOut(LONG64, aTimestamps),
836 ComSafeArrayOut(BSTR, aFlags))
837{
838#ifdef VBOX_WITH_GUEST_PROPS
839 AutoCaller autoCaller(this);
840 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
841
842 if (mState != SessionState_Locked)
843 return setError(VBOX_E_INVALID_VM_STATE,
844 tr("Machine is not locked by session (session state: %s)."),
845 Global::stringifySessionState(mState));
846 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
847 if (!VALID_PTR(aPatterns) && (aPatterns != NULL))
848 return E_POINTER;
849 if (ComSafeArrayOutIsNull(aNames))
850 return E_POINTER;
851 if (ComSafeArrayOutIsNull(aValues))
852 return E_POINTER;
853 if (ComSafeArrayOutIsNull(aTimestamps))
854 return E_POINTER;
855 if (ComSafeArrayOutIsNull(aFlags))
856 return E_POINTER;
857
858 /* If this session is not in a VM process fend off the call. The caller
859 * handles this correctly, by doing the operation in VBoxSVC. */
860 if (!mConsole)
861 return E_ACCESSDENIED;
862
863 return mConsole->enumerateGuestProperties(aPatterns,
864 ComSafeArrayOutArg(aNames),
865 ComSafeArrayOutArg(aValues),
866 ComSafeArrayOutArg(aTimestamps),
867 ComSafeArrayOutArg(aFlags));
868#else /* VBOX_WITH_GUEST_PROPS not defined */
869 ReturnComNotImplemented();
870#endif /* VBOX_WITH_GUEST_PROPS not defined */
871}
872
873STDMETHODIMP Session::OnlineMergeMedium(IMediumAttachment *aMediumAttachment,
874 ULONG aSourceIdx, ULONG aTargetIdx,
875 IMedium *aSource, IMedium *aTarget,
876 BOOL aMergeForward,
877 IMedium *aParentForTarget,
878 ComSafeArrayIn(IMedium *, aChildrenToReparent),
879 IProgress *aProgress)
880{
881 AutoCaller autoCaller(this);
882 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
883
884 if (mState != SessionState_Locked)
885 return setError(VBOX_E_INVALID_VM_STATE,
886 tr("Machine is not locked by session (session state: %s)."),
887 Global::stringifySessionState(mState));
888 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
889 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
890 CheckComArgNotNull(aMediumAttachment);
891 CheckComArgSafeArrayNotNull(aChildrenToReparent);
892
893 return mConsole->onlineMergeMedium(aMediumAttachment, aSourceIdx,
894 aTargetIdx, aSource, aTarget,
895 aMergeForward, aParentForTarget,
896 ComSafeArrayInArg(aChildrenToReparent),
897 aProgress);
898}
899
900STDMETHODIMP Session::EnableVMMStatistics(BOOL aEnable)
901{
902 AutoCaller autoCaller(this);
903 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
904
905 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
906 AssertReturn(mState == SessionState_Locked, VBOX_E_INVALID_VM_STATE);
907 AssertReturn(mType == SessionType_WriteLock, VBOX_E_INVALID_OBJECT_STATE);
908 AssertReturn(mConsole, VBOX_E_INVALID_OBJECT_STATE);
909
910 mConsole->enableVMMStatistics(aEnable);
911
912 return S_OK;
913}
914
915// private methods
916///////////////////////////////////////////////////////////////////////////////
917
918/**
919 * Unlocks a machine associated with the current session.
920 *
921 * @param aFinalRelease called as a result of FinalRelease()
922 * @param aFromServer called as a result of Uninitialize()
923 *
924 * @note To be called only from #uninit(), #UnlockMachine() or #Uninitialize().
925 * @note Locks this object for writing.
926 */
927HRESULT Session::unlockMachine(bool aFinalRelease, bool aFromServer)
928{
929 LogFlowThisFuncEnter();
930 LogFlowThisFunc(("aFinalRelease=%d, isFromServer=%d\n",
931 aFinalRelease, aFromServer));
932
933 AutoCaller autoCaller(this);
934 AssertComRCReturnRC(autoCaller.rc());
935
936 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
937
938 LogFlowThisFunc(("mState=%s, mType=%d\n", Global::stringifySessionState(mState), mType));
939
940 if (mState != SessionState_Locked)
941 {
942 Assert(mState == SessionState_Spawning);
943
944 /* The session object is going to be uninitialized before it has been
945 * assigned a direct console of the machine the client requested to open
946 * a remote session to using IVirtualBox:: openRemoteSession(). It is OK
947 * only if this close request comes from the server (for example, it
948 * detected that the VM process it started terminated before opening a
949 * direct session). Otherwise, it means that the client is too fast and
950 * trying to close the session before waiting for the progress object it
951 * got from IVirtualBox:: openRemoteSession() to complete, so assert. */
952 Assert(aFromServer);
953
954 mState = SessionState_Unlocked;
955 mType = SessionType_Null;
956#if defined(RT_OS_WINDOWS)
957 Assert(!mIPCSem && !mIPCThreadSem);
958#elif defined(RT_OS_OS2)
959 Assert(mIPCThread == NIL_RTTHREAD &&
960 mIPCThreadSem == NIL_RTSEMEVENT);
961#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
962 Assert(mIPCSem == -1);
963#else
964# error "Port me!"
965#endif
966 LogFlowThisFuncLeave();
967 return S_OK;
968 }
969
970 /* go to the closing state */
971 mState = SessionState_Unlocking;
972
973 if (mType == SessionType_WriteLock)
974 {
975 if (!mConsole.isNull())
976 {
977 mConsole->uninit();
978 mConsole.setNull();
979 }
980 }
981 else
982 {
983 mRemoteMachine.setNull();
984 mRemoteConsole.setNull();
985 }
986
987 ComPtr<IProgress> progress;
988
989 if (!aFinalRelease && !aFromServer)
990 {
991 /*
992 * We trigger OnSessionEnd() only when the session closes itself using
993 * Close(). Note that if isFinalRelease = TRUE here, this means that
994 * the client process has already initialized the termination procedure
995 * without issuing Close() and the IPC channel is no more operational --
996 * so we cannot call the server's method (it will definitely fail). The
997 * server will instead simply detect the abnormal client death (since
998 * OnSessionEnd() is not called) and reset the machine state to Aborted.
999 */
1000
1001 /*
1002 * while waiting for OnSessionEnd() to complete one of our methods
1003 * can be called by the server (for example, Uninitialize(), if the
1004 * direct session has initiated a closure just a bit before us) so
1005 * we need to release the lock to avoid deadlocks. The state is already
1006 * SessionState_Closing here, so it's safe.
1007 */
1008 alock.release();
1009
1010 LogFlowThisFunc(("Calling mControl->OnSessionEnd()...\n"));
1011 HRESULT rc = mControl->OnSessionEnd(this, progress.asOutParam());
1012 LogFlowThisFunc(("mControl->OnSessionEnd()=%08X\n", rc));
1013
1014 alock.acquire();
1015
1016 /*
1017 * If we get E_UNEXPECTED this means that the direct session has already
1018 * been closed, we're just too late with our notification and nothing more
1019 *
1020 * bird: Seems E_ACCESSDENIED is what gets returned these days; see
1021 * VirtualBoxBase::addCaller.
1022 */
1023 if (mType != SessionType_WriteLock && (rc == E_UNEXPECTED || rc == E_ACCESSDENIED))
1024 rc = S_OK;
1025
1026#ifndef DEBUG_bird /* I don't want clients crashing on me just because VBoxSVC went belly up. */
1027 AssertComRC(rc);
1028#endif
1029 }
1030
1031 mControl.setNull();
1032
1033 if (mType == SessionType_WriteLock)
1034 {
1035 releaseIPCSemaphore();
1036 if (!aFinalRelease && !aFromServer)
1037 {
1038 /*
1039 * Wait for the server to grab the semaphore and destroy the session
1040 * machine (allowing us to open a new session with the same machine
1041 * once this method returns)
1042 */
1043 Assert(!!progress);
1044 if (progress)
1045 progress->WaitForCompletion(-1);
1046 }
1047 }
1048
1049 mState = SessionState_Unlocked;
1050 mType = SessionType_Null;
1051
1052 /* release the VirtualBox instance as the very last step */
1053 mVirtualBox.setNull();
1054
1055 LogFlowThisFuncLeave();
1056 return S_OK;
1057}
1058
1059/** @note To be called only from #AssignMachine() */
1060HRESULT Session::grabIPCSemaphore()
1061{
1062 HRESULT rc = E_FAIL;
1063
1064 /* open the IPC semaphore based on the sessionId and try to grab it */
1065 Bstr ipcId;
1066 rc = mControl->GetIPCId(ipcId.asOutParam());
1067 AssertComRCReturnRC(rc);
1068
1069 LogFlowThisFunc(("ipcId='%ls'\n", ipcId.raw()));
1070
1071#if defined(RT_OS_WINDOWS)
1072
1073 /*
1074 * Since Session is an MTA object, this method can be executed on
1075 * any thread, and this thread will not necessarily match the thread on
1076 * which close() will be called later. Therefore, we need a separate
1077 * thread to hold the IPC mutex and then release it in close().
1078 */
1079
1080 mIPCThreadSem = ::CreateEvent(NULL, FALSE, FALSE, NULL);
1081 AssertMsgReturn(mIPCThreadSem,
1082 ("Cannot create an event sem, err=%d", ::GetLastError()),
1083 E_FAIL);
1084
1085 void *data[3];
1086 data[0] = (void*)(BSTR)ipcId.raw();
1087 data[1] = (void*)mIPCThreadSem;
1088 data[2] = 0; /* will get an output from the thread */
1089
1090 /* create a thread to hold the IPC mutex until signalled to release it */
1091 RTTHREAD tid;
1092 int vrc = RTThreadCreate(&tid, IPCMutexHolderThread, (void*)data, 0, RTTHREADTYPE_MAIN_WORKER, 0, "IPCHolder");
1093 AssertRCReturn(vrc, E_FAIL);
1094
1095 /* wait until thread init is completed */
1096 DWORD wrc = ::WaitForSingleObject(mIPCThreadSem, INFINITE);
1097 AssertMsg(wrc == WAIT_OBJECT_0, ("Wait failed, err=%d\n", ::GetLastError()));
1098 Assert(data[2]);
1099
1100 if (wrc == WAIT_OBJECT_0 && data[2])
1101 {
1102 /* memorize the event sem we should signal in close() */
1103 mIPCSem = (HANDLE)data[2];
1104 rc = S_OK;
1105 }
1106 else
1107 {
1108 ::CloseHandle(mIPCThreadSem);
1109 mIPCThreadSem = NULL;
1110 rc = E_FAIL;
1111 }
1112
1113#elif defined(RT_OS_OS2)
1114
1115 /* We use XPCOM where any message (including close()) can arrive on any
1116 * worker thread (which will not necessarily match this thread that opens
1117 * the mutex). Therefore, we need a separate thread to hold the IPC mutex
1118 * and then release it in close(). */
1119
1120 int vrc = RTSemEventCreate(&mIPCThreadSem);
1121 AssertRCReturn(vrc, E_FAIL);
1122
1123 void *data[3];
1124 data[0] = (void*)ipcId.raw();
1125 data[1] = (void*)mIPCThreadSem;
1126 data[2] = (void*)false; /* will get the thread result here */
1127
1128 /* create a thread to hold the IPC mutex until signalled to release it */
1129 vrc = RTThreadCreate(&mIPCThread, IPCMutexHolderThread, (void *) data,
1130 0, RTTHREADTYPE_MAIN_WORKER, 0, "IPCHolder");
1131 AssertRCReturn(vrc, E_FAIL);
1132
1133 /* wait until thread init is completed */
1134 vrc = RTThreadUserWait (mIPCThread, RT_INDEFINITE_WAIT);
1135 AssertReturn(RT_SUCCESS(vrc) || vrc == VERR_INTERRUPTED, E_FAIL);
1136
1137 /* the thread must succeed */
1138 AssertReturn((bool)data[2], E_FAIL);
1139
1140#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
1141
1142# ifdef VBOX_WITH_NEW_SYS_V_KEYGEN
1143 Utf8Str ipcKey = ipcId;
1144 key_t key = RTStrToUInt32(ipcKey.c_str());
1145 AssertMsgReturn (key != 0,
1146 ("Key value of 0 is not valid for IPC semaphore"),
1147 E_FAIL);
1148# else /* !VBOX_WITH_NEW_SYS_V_KEYGEN */
1149 Utf8Str semName = ipcId;
1150 char *pszSemName = NULL;
1151 RTStrUtf8ToCurrentCP (&pszSemName, semName);
1152 key_t key = ::ftok (pszSemName, 'V');
1153 RTStrFree (pszSemName);
1154# endif /* !VBOX_WITH_NEW_SYS_V_KEYGEN */
1155
1156 mIPCSem = ::semget (key, 0, 0);
1157 AssertMsgReturn (mIPCSem >= 0,
1158 ("Cannot open IPC semaphore, errno=%d", errno),
1159 E_FAIL);
1160
1161 /* grab the semaphore */
1162 ::sembuf sop = { 0, -1, SEM_UNDO };
1163 int rv = ::semop (mIPCSem, &sop, 1);
1164 AssertMsgReturn (rv == 0,
1165 ("Cannot grab IPC semaphore, errno=%d", errno),
1166 E_FAIL);
1167
1168#else
1169# error "Port me!"
1170#endif
1171
1172 return rc;
1173}
1174
1175/** @note To be called only from #close() */
1176void Session::releaseIPCSemaphore()
1177{
1178 /* release the IPC semaphore */
1179#if defined(RT_OS_WINDOWS)
1180
1181 if (mIPCSem && mIPCThreadSem)
1182 {
1183 /*
1184 * tell the thread holding the IPC mutex to release it;
1185 * it will close mIPCSem handle
1186 */
1187 ::SetEvent (mIPCSem);
1188 /* wait for the thread to finish */
1189 ::WaitForSingleObject (mIPCThreadSem, INFINITE);
1190 ::CloseHandle (mIPCThreadSem);
1191
1192 mIPCThreadSem = NULL;
1193 mIPCSem = NULL;
1194 }
1195
1196#elif defined(RT_OS_OS2)
1197
1198 if (mIPCThread != NIL_RTTHREAD)
1199 {
1200 Assert (mIPCThreadSem != NIL_RTSEMEVENT);
1201
1202 /* tell the thread holding the IPC mutex to release it */
1203 int vrc = RTSemEventSignal (mIPCThreadSem);
1204 AssertRC(vrc == NO_ERROR);
1205
1206 /* wait for the thread to finish */
1207 vrc = RTThreadUserWait (mIPCThread, RT_INDEFINITE_WAIT);
1208 Assert (RT_SUCCESS(vrc) || vrc == VERR_INTERRUPTED);
1209
1210 mIPCThread = NIL_RTTHREAD;
1211 }
1212
1213 if (mIPCThreadSem != NIL_RTSEMEVENT)
1214 {
1215 RTSemEventDestroy (mIPCThreadSem);
1216 mIPCThreadSem = NIL_RTSEMEVENT;
1217 }
1218
1219#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
1220
1221 if (mIPCSem >= 0)
1222 {
1223 ::sembuf sop = { 0, 1, SEM_UNDO };
1224 ::semop (mIPCSem, &sop, 1);
1225
1226 mIPCSem = -1;
1227 }
1228
1229#else
1230# error "Port me!"
1231#endif
1232}
1233
1234#if defined(RT_OS_WINDOWS)
1235/** VM IPC mutex holder thread */
1236DECLCALLBACK(int) IPCMutexHolderThread (RTTHREAD Thread, void *pvUser)
1237{
1238 LogFlowFuncEnter();
1239
1240 Assert (pvUser);
1241 void **data = (void **) pvUser;
1242
1243 BSTR sessionId = (BSTR)data[0];
1244 HANDLE initDoneSem = (HANDLE)data[1];
1245
1246 HANDLE ipcMutex = ::OpenMutex (MUTEX_ALL_ACCESS, FALSE, sessionId);
1247 AssertMsg (ipcMutex, ("cannot open IPC mutex, err=%d\n", ::GetLastError()));
1248
1249 if (ipcMutex)
1250 {
1251 /* grab the mutex */
1252 DWORD wrc = ::WaitForSingleObject (ipcMutex, 0);
1253 AssertMsg (wrc == WAIT_OBJECT_0, ("cannot grab IPC mutex, err=%d\n", wrc));
1254 if (wrc == WAIT_OBJECT_0)
1255 {
1256 HANDLE finishSem = ::CreateEvent (NULL, FALSE, FALSE, NULL);
1257 AssertMsg (finishSem, ("cannot create event sem, err=%d\n", ::GetLastError()));
1258 if (finishSem)
1259 {
1260 data[2] = (void*)finishSem;
1261 /* signal we're done with init */
1262 ::SetEvent (initDoneSem);
1263 /* wait until we're signaled to release the IPC mutex */
1264 ::WaitForSingleObject (finishSem, INFINITE);
1265 /* release the IPC mutex */
1266 LogFlow (("IPCMutexHolderThread(): releasing IPC mutex...\n"));
1267 BOOL success = ::ReleaseMutex (ipcMutex);
1268 AssertMsg (success, ("cannot release mutex, err=%d\n", ::GetLastError()));
1269 ::CloseHandle (ipcMutex);
1270 ::CloseHandle (finishSem);
1271 }
1272 }
1273 }
1274
1275 /* signal we're done */
1276 ::SetEvent (initDoneSem);
1277
1278 LogFlowFuncLeave();
1279
1280 return 0;
1281}
1282#endif
1283
1284#if defined(RT_OS_OS2)
1285/** VM IPC mutex holder thread */
1286DECLCALLBACK(int) IPCMutexHolderThread (RTTHREAD Thread, void *pvUser)
1287{
1288 LogFlowFuncEnter();
1289
1290 Assert (pvUser);
1291 void **data = (void **) pvUser;
1292
1293 Utf8Str ipcId = (BSTR)data[0];
1294 RTSEMEVENT finishSem = (RTSEMEVENT)data[1];
1295
1296 LogFlowFunc (("ipcId='%s', finishSem=%p\n", ipcId.raw(), finishSem));
1297
1298 HMTX ipcMutex = NULLHANDLE;
1299 APIRET arc = ::DosOpenMutexSem ((PSZ) ipcId.raw(), &ipcMutex);
1300 AssertMsg (arc == NO_ERROR, ("cannot open IPC mutex, arc=%ld\n", arc));
1301
1302 if (arc == NO_ERROR)
1303 {
1304 /* grab the mutex */
1305 LogFlowFunc (("grabbing IPC mutex...\n"));
1306 arc = ::DosRequestMutexSem (ipcMutex, SEM_IMMEDIATE_RETURN);
1307 AssertMsg (arc == NO_ERROR, ("cannot grab IPC mutex, arc=%ld\n", arc));
1308 if (arc == NO_ERROR)
1309 {
1310 /* store the answer */
1311 data[2] = (void*)true;
1312 /* signal we're done */
1313 int vrc = RTThreadUserSignal (Thread);
1314 AssertRC(vrc);
1315
1316 /* wait until we're signaled to release the IPC mutex */
1317 LogFlowFunc (("waiting for termination signal..\n"));
1318 vrc = RTSemEventWait (finishSem, RT_INDEFINITE_WAIT);
1319 Assert (arc == ERROR_INTERRUPT || ERROR_TIMEOUT);
1320
1321 /* release the IPC mutex */
1322 LogFlowFunc (("releasing IPC mutex...\n"));
1323 arc = ::DosReleaseMutexSem (ipcMutex);
1324 AssertMsg (arc == NO_ERROR, ("cannot release mutex, arc=%ld\n", arc));
1325 }
1326
1327 ::DosCloseMutexSem (ipcMutex);
1328 }
1329
1330 /* store the answer */
1331 data[1] = (void*)false;
1332 /* signal we're done */
1333 int vrc = RTThreadUserSignal (Thread);
1334 AssertRC(vrc);
1335
1336 LogFlowFuncLeave();
1337
1338 return 0;
1339}
1340#endif
1341/* 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