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