VirtualBox

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

Last change on this file since 45772 was 45757, checked in by vboxsync, 12 years ago

Main: more debugging

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