VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostDnsService.cpp@ 49312

Last change on this file since 49312 was 49248, checked in by vboxsync, 11 years ago

Main/HostDnsMonitor::getInfo() don't need to lock here. dns-monitor thread is wait on sending events, and can't write to info.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1/* $Id: HostDnsService.cpp 49248 2013-10-23 03:26:55Z vboxsync $ */
2/** @file
3 * Base class fo Host DNS & Co services.
4 */
5
6/*
7 * Copyright (C) 2013 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 <VBox/com/array.h>
19#include <VBox/com/ptr.h>
20#include <VBox/com/string.h>
21
22#include <iprt/cpp/utils.h>
23
24#include "VirtualBoxImpl.h"
25#include <iprt/thread.h>
26#include <iprt/semaphore.h>
27#include <iprt/critsect.h>
28
29#include <algorithm>
30#include <string>
31#include <vector>
32#include "HostDnsService.h"
33
34
35static HostDnsMonitor *g_monitor;
36
37
38/* Lockee */
39Lockee::Lockee(){ RTCritSectInit(&mLock);}
40
41
42Lockee::~Lockee(){RTCritSectDelete(&mLock);}
43
44
45const RTCRITSECT* Lockee::lock() const {return &mLock;}
46
47/* ALock */
48ALock::ALock(const Lockee *l):lck(l)
49{
50 RTCritSectEnter(const_cast<PRTCRITSECT>(lck->lock()));
51}
52
53ALock::~ALock()
54{
55 RTCritSectLeave(const_cast<PRTCRITSECT>(lck->lock()));
56}
57
58
59
60inline static void detachVectorOfString(const std::vector<std::string>& v,
61 ComSafeArrayOut(BSTR, aBstrArray))
62{
63 com::SafeArray<BSTR> aBstr(v.size());
64
65 std::vector<std::string>::const_iterator it;
66
67 int i = 0;
68 it = v.begin();
69 for (; it != v.end(); ++it, ++i)
70 Utf8Str(it->c_str()).cloneTo(&aBstr[i]);
71
72 aBstr.detachTo(ComSafeArrayOutArg(aBstrArray));
73}
74
75
76struct HostDnsMonitor::Data
77{
78 std::vector<HostDnsMonitorProxy> proxies;
79 HostDnsInformation info;
80};
81
82
83struct HostDnsMonitorProxy::Data
84{
85 Data(const HostDnsMonitor* mon, const VirtualBox* vbox):
86 info(NULL), virtualbox(vbox), monitor(mon), fModified(true){}
87
88 virtual ~Data() {if (info) delete info;}
89
90 HostDnsInformation *info;
91 const VirtualBox *virtualbox;
92 const HostDnsMonitor* monitor;
93 bool fModified;
94};
95
96
97HostDnsMonitor::HostDnsMonitor():m(NULL){}
98
99
100HostDnsMonitor::~HostDnsMonitor(){if (m) delete m;}
101
102
103const HostDnsMonitor* HostDnsMonitor::getHostDnsMonitor()
104{
105 /* XXX: Moved initialization from HostImpl.cpp */
106 if (!g_monitor)
107 {
108 g_monitor =
109# if defined (RT_OS_DARWIN)
110 new HostDnsServiceDarwin();
111# elif defined(RT_OS_WINDOWS)
112 new HostDnsServiceWin();
113# elif defined(RT_OS_LINUX)
114 new HostDnsServiceLinux();
115# elif defined(RT_OS_SOLARIS)
116 new HostDnsServiceSolaris();
117# elif defined(RT_OS_OS2)
118 new HostDnsServiceOs2();
119# else
120 new HostDnsService();
121# endif
122 g_monitor->init();
123 }
124 return g_monitor;
125}
126
127
128void HostDnsMonitor::addMonitorProxy(const HostDnsMonitorProxy& proxy) const
129{
130 ALock l(this);
131 m->proxies.push_back(proxy);
132
133 proxy.notify();
134}
135
136
137void HostDnsMonitor::releaseMonitorProxy(const HostDnsMonitorProxy& proxy) const
138{
139 ALock l(this);
140 std::vector<HostDnsMonitorProxy>::iterator it;
141 it = std::find(m->proxies.begin(), m->proxies.end(), proxy);
142
143 if (it == m->proxies.end())
144 return;
145
146 m->proxies.erase(it);
147}
148
149
150void HostDnsMonitor::shutdown()
151{
152 if (g_monitor)
153 {
154 delete g_monitor;
155 g_monitor = NULL;
156 }
157}
158
159
160const HostDnsInformation& HostDnsMonitor::getInfo() const
161{
162 return m->info;
163}
164
165
166void HostDnsMonitor::notifyAll() const
167{
168 ALock l(this);
169 std::vector<HostDnsMonitorProxy>::const_iterator it;
170 for (it = m->proxies.begin(); it != m->proxies.end(); ++it)
171 it->notify();
172}
173
174
175void HostDnsMonitor::setInfo(const HostDnsInformation& info)
176{
177 ALock l(this);
178 m->info = info;
179}
180
181
182HRESULT HostDnsMonitor::init()
183{
184 m = new HostDnsMonitor::Data();
185 return S_OK;
186}
187
188/* HostDnsMonitorProxy */
189HostDnsMonitorProxy::HostDnsMonitorProxy():m(NULL){}
190
191HostDnsMonitorProxy::~HostDnsMonitorProxy()
192{
193 if (m && m->monitor)
194 {
195 m->monitor->releaseMonitorProxy(*this);
196 delete m;
197 }
198}
199
200
201void HostDnsMonitorProxy::init(const HostDnsMonitor* mon, const VirtualBox* aParent)
202{
203 m = new HostDnsMonitorProxy::Data(mon, aParent);
204 m->monitor->addMonitorProxy(*this);
205 updateInfo();
206}
207
208
209void HostDnsMonitorProxy::notify() const
210{
211 m->fModified = true;
212 const_cast<VirtualBox *>(m->virtualbox)->onHostNameResolutionConfigurationChange();
213}
214
215
216STDMETHODIMP HostDnsMonitorProxy::COMGETTER(NameServers)(ComSafeArrayOut(BSTR, aNameServers))
217{
218 AssertReturn(m && m->info, E_FAIL);
219 ALock l(this);
220
221 if (m->fModified)
222 updateInfo();
223
224 detachVectorOfString(m->info->servers, ComSafeArrayOutArg(aNameServers));
225
226 return S_OK;
227}
228
229
230STDMETHODIMP HostDnsMonitorProxy::COMGETTER(DomainName)(BSTR *aDomainName)
231{
232 AssertReturn(m && m->info, E_FAIL);
233 ALock l(this);
234
235 if (m->fModified)
236 updateInfo();
237
238 Utf8Str(m->info->domain.c_str()).cloneTo(aDomainName);
239
240 return S_OK;
241}
242
243
244STDMETHODIMP HostDnsMonitorProxy::COMGETTER(SearchStrings)(ComSafeArrayOut(BSTR, aSearchStrings))
245{
246 AssertReturn(m && m->info, E_FAIL);
247 ALock l(this);
248
249 if (m->fModified)
250 updateInfo();
251
252 detachVectorOfString(m->info->searchList, ComSafeArrayOutArg(aSearchStrings));
253
254 return S_OK;
255}
256
257
258bool HostDnsMonitorProxy::operator==(const HostDnsMonitorProxy& rhs)
259{
260 if (!m || rhs.m) return false;
261
262 /**
263 * we've assigned to the same instance of VirtualBox.
264 */
265 return m->virtualbox == rhs.m->virtualbox;
266}
267
268void HostDnsMonitorProxy::updateInfo()
269{
270 HostDnsInformation *i = new HostDnsInformation(m->monitor->getInfo());
271 HostDnsInformation *old = m->info;
272
273 if (old)
274 {
275 m->info = i;
276 delete old;
277 }
278 else
279 m->info = i;
280
281 m->fModified = false;
282}
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