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