VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/VirtualBoxClientImpl.cpp@ 59341

Last change on this file since 59341 was 52442, checked in by vboxsync, 10 years ago

Main: convert VirtualBoxClient and VRDEServerInfo to use wrappers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
1/* $Id: VirtualBoxClientImpl.cpp 52442 2014-08-21 16:03:15Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2010-2014 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#include "VirtualBoxClientImpl.h"
19
20#include "AutoCaller.h"
21#include "VBoxEvents.h"
22#include "Logging.h"
23#include "VBox/com/ErrorInfo.h"
24
25#include <iprt/asm.h>
26#include <iprt/thread.h>
27#include <iprt/critsect.h>
28#include <iprt/semaphore.h>
29#include <iprt/cpp/utils.h>
30
31
32/** Waiting time between probing whether VBoxSVC is alive. */
33#define VBOXCLIENT_DEFAULT_INTERVAL 30000
34
35
36/** Initialize instance counter class variable */
37uint32_t VirtualBoxClient::g_cInstances = 0;
38
39
40// constructor / destructor
41/////////////////////////////////////////////////////////////////////////////
42
43HRESULT VirtualBoxClient::FinalConstruct()
44{
45 HRESULT rc = init();
46 BaseFinalConstruct();
47 return rc;
48}
49
50void VirtualBoxClient::FinalRelease()
51{
52 uninit();
53 BaseFinalRelease();
54}
55
56// public initializer/uninitializer for internal purposes only
57/////////////////////////////////////////////////////////////////////////////
58
59/**
60 * Initializes the VirtualBoxClient object.
61 *
62 * @returns COM result indicator
63 */
64HRESULT VirtualBoxClient::init()
65{
66 LogFlowThisFunc(("\n"));
67
68 HRESULT rc;
69 /* Enclose the state transition NotReady->InInit->Ready */
70 AutoInitSpan autoInitSpan(this);
71 AssertReturn(autoInitSpan.isOk(), E_FAIL);
72
73 mData.m_ThreadWatcher = NIL_RTTHREAD;
74 mData.m_SemEvWatcher = NIL_RTSEMEVENT;
75
76 if (ASMAtomicIncU32(&g_cInstances) != 1)
77 AssertFailedReturn(E_FAIL);
78
79 rc = mData.m_pVirtualBox.createLocalObject(CLSID_VirtualBox);
80 AssertComRCReturnRC(rc);
81
82 rc = unconst(mData.m_pEventSource).createObject();
83 AssertComRCReturnRC(rc);
84 rc = mData.m_pEventSource->init();
85 AssertComRCReturnRC(rc);
86
87 /* Setting up the VBoxSVC watcher thread. If anything goes wrong here it
88 * is not considered important enough to cause any sort of visible
89 * failure. The monitoring will not be done, but that's all. */
90 int vrc = RTSemEventCreate(&mData.m_SemEvWatcher);
91 AssertRC(vrc);
92 if (RT_SUCCESS(vrc))
93 {
94 vrc = RTThreadCreate(&mData.m_ThreadWatcher, SVCWatcherThread,
95 this, 0, RTTHREADTYPE_INFREQUENT_POLLER,
96 RTTHREADFLAGS_WAITABLE, "VBoxSVCWatcher");
97 AssertRC(vrc);
98 }
99 else
100 {
101 RTSemEventDestroy(mData.m_SemEvWatcher);
102 mData.m_SemEvWatcher = NIL_RTSEMEVENT;
103 }
104
105 /* Confirm a successful initialization */
106 autoInitSpan.setSucceeded();
107
108 return rc;
109}
110
111/**
112 * Uninitializes the instance and sets the ready flag to FALSE.
113 * Called either from FinalRelease() or by the parent when it gets destroyed.
114 */
115void VirtualBoxClient::uninit()
116{
117 LogFlowThisFunc(("\n"));
118
119 /* Enclose the state transition Ready->InUninit->NotReady */
120 AutoUninitSpan autoUninitSpan(this);
121 if (autoUninitSpan.uninitDone())
122 return;
123
124 if (mData.m_ThreadWatcher != NIL_RTTHREAD)
125 {
126 /* Signal the event semaphore and wait for the thread to terminate.
127 * if it hangs for some reason exit anyway, this can cause a crash
128 * though as the object will no longer be available. */
129 RTSemEventSignal(mData.m_SemEvWatcher);
130 RTThreadWait(mData.m_ThreadWatcher, 30000, NULL);
131 mData.m_ThreadWatcher = NIL_RTTHREAD;
132 RTSemEventDestroy(mData.m_SemEvWatcher);
133 mData.m_SemEvWatcher = NIL_RTSEMEVENT;
134 }
135
136 mData.m_pVirtualBox.setNull();
137
138 ASMAtomicDecU32(&g_cInstances);
139}
140
141// IVirtualBoxClient properties
142/////////////////////////////////////////////////////////////////////////////
143
144/**
145 * Returns a reference to the VirtualBox object.
146 *
147 * @returns COM status code
148 * @param aVirtualBox Address of result variable.
149 */
150HRESULT VirtualBoxClient::getVirtualBox(ComPtr<IVirtualBox> &aVirtualBox)
151{
152 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
153 aVirtualBox = mData.m_pVirtualBox;
154 return S_OK;
155}
156
157/**
158 * Create a new Session object and return a reference to it.
159 *
160 * @returns COM status code
161 * @param aSession Address of result variable.
162 */
163HRESULT VirtualBoxClient::getSession(ComPtr<ISession> &aSession)
164{
165 /* this is not stored in this object, no need to lock */
166 ComPtr<ISession> pSession;
167 HRESULT rc = pSession.createInprocObject(CLSID_Session);
168 if (SUCCEEDED(rc))
169 aSession = pSession;
170 return rc;
171}
172
173/**
174 * Return reference to the EventSource associated with this object.
175 *
176 * @returns COM status code
177 * @param aEventSource Address of result variable.
178 */
179HRESULT VirtualBoxClient::getEventSource(ComPtr<IEventSource> &aEventSource)
180{
181 /* this is const, no need to lock */
182 aEventSource = mData.m_pEventSource;
183 return aEventSource.isNull() ? E_FAIL : S_OK;
184}
185
186// IVirtualBoxClient methods
187/////////////////////////////////////////////////////////////////////////////
188
189/**
190 * Checks a Machine object for any pending errors.
191 *
192 * @returns COM status code
193 * @param aMachine Machine object to check.
194 */
195HRESULT VirtualBoxClient::checkMachineError(const ComPtr<IMachine> &aMachine)
196{
197 BOOL fAccessible = FALSE;
198 HRESULT rc = aMachine->COMGETTER(Accessible)(&fAccessible);
199 if (FAILED(rc))
200 return setError(rc, tr("Could not check the accessibility status of the VM"));
201 else if (!fAccessible)
202 {
203 ComPtr<IVirtualBoxErrorInfo> pAccessError;
204 rc = aMachine->COMGETTER(AccessError)(pAccessError.asOutParam());
205 if (FAILED(rc))
206 return setError(rc, tr("Could not get the access error message of the VM"));
207 else
208 {
209 ErrorInfo info(pAccessError);
210 ErrorInfoKeeper eik(info);
211 return info.getResultCode();
212 }
213 }
214 return S_OK;
215}
216
217// private methods
218/////////////////////////////////////////////////////////////////////////////
219
220/*static*/
221DECLCALLBACK(int) VirtualBoxClient::SVCWatcherThread(RTTHREAD ThreadSelf,
222 void *pvUser)
223{
224 NOREF(ThreadSelf);
225 Assert(pvUser);
226 VirtualBoxClient *pThis = (VirtualBoxClient *)pvUser;
227 RTSEMEVENT sem = pThis->mData.m_SemEvWatcher;
228 RTMSINTERVAL cMillies = VBOXCLIENT_DEFAULT_INTERVAL;
229 int vrc;
230
231 /* The likelihood of early crashes are high, so start with a short wait. */
232 vrc = RTSemEventWait(sem, cMillies / 2);
233
234 /* As long as the waiting times out keep retrying the wait. */
235 while (RT_FAILURE(vrc))
236 {
237 {
238 HRESULT rc = S_OK;
239 ComPtr<IVirtualBox> pV;
240 {
241 AutoReadLock alock(pThis COMMA_LOCKVAL_SRC_POS);
242 pV = pThis->mData.m_pVirtualBox;
243 }
244 if (!pV.isNull())
245 {
246 ULONG rev;
247 rc = pV->COMGETTER(Revision)(&rev);
248 if (FAILED_DEAD_INTERFACE(rc))
249 {
250 LogRel(("VirtualBoxClient: detected unresponsive VBoxSVC (rc=%Rhrc)\n", rc));
251 {
252 AutoWriteLock alock(pThis COMMA_LOCKVAL_SRC_POS);
253 /* Throw away the VirtualBox reference, it's no longer
254 * usable as VBoxSVC terminated in the mean time. */
255 pThis->mData.m_pVirtualBox.setNull();
256 }
257 fireVBoxSVCAvailabilityChangedEvent(pThis->mData.m_pEventSource, FALSE);
258 }
259 }
260 else
261 {
262 /* Try to get a new VirtualBox reference straight away, and if
263 * this fails use an increased waiting time as very frequent
264 * restart attempts in some wedged config can cause high CPU
265 * and disk load. */
266 ComPtr<IVirtualBox> pVirtualBox;
267 rc = pVirtualBox.createLocalObject(CLSID_VirtualBox);
268 if (FAILED(rc))
269 cMillies = 3 * VBOXCLIENT_DEFAULT_INTERVAL;
270 else
271 {
272 LogRel(("VirtualBoxClient: detected working VBoxSVC (rc=%Rhrc)\n", rc));
273 {
274 AutoWriteLock alock(pThis COMMA_LOCKVAL_SRC_POS);
275 /* Update the VirtualBox reference, there's a working
276 * VBoxSVC again from now on. */
277 pThis->mData.m_pVirtualBox = pVirtualBox;
278 }
279 fireVBoxSVCAvailabilityChangedEvent(pThis->mData.m_pEventSource, TRUE);
280 cMillies = VBOXCLIENT_DEFAULT_INTERVAL;
281 }
282 }
283 }
284 vrc = RTSemEventWait(sem, cMillies);
285 }
286 return 0;
287}
288
289/* 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