VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostDnsService.h@ 108010

Last change on this file since 108010 was 108010, checked in by vboxsync, 4 months ago

Main/HostDnsService.cpp: Fixed lock order problem with HostDnsMonitorProxy::updateInfo / HostDnsMonitorProxy::pollGlobalExtraData / VirtualBox::getExtraData after r166866. This replaces the IPRT scope-based locking with the main-style locking for flexibility.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1/* $Id: HostDnsService.h 108010 2025-01-31 23:49:41Z vboxsync $ */
2/** @file
3 * Host DNS listener.
4 */
5
6/*
7 * Copyright (C) 2005-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.215389.xyz.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#ifndef MAIN_INCLUDED_SRC_src_server_HostDnsService_h
29#define MAIN_INCLUDED_SRC_src_server_HostDnsService_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33#include "VirtualBoxBase.h"
34
35#include <iprt/err.h> /* VERR_IGNORED */
36#include <iprt/cpp/lock.h>
37
38#include <list>
39#include <iprt/sanitized/string>
40#include <vector>
41
42typedef std::list<com::Utf8Str> Utf8StrList;
43typedef Utf8StrList::iterator Utf8StrListIterator;
44
45class HostDnsMonitorProxy;
46typedef const HostDnsMonitorProxy *PCHostDnsMonitorProxy;
47
48class HostDnsInformation
49{
50public:
51 static const uint32_t IGNORE_SERVER_ORDER = RT_BIT_32(0);
52 static const uint32_t IGNORE_SUFFIXES = RT_BIT_32(1);
53
54public:
55 /** @todo r=bird: Why on earth are we using std::string and not Utf8Str? */
56 std::vector<std::string> servers;
57 std::string domain;
58 std::vector<std::string> searchList;
59 bool equals(const HostDnsInformation &, uint32_t fLaxComparison = 0) const;
60};
61
62/**
63 * Base class for host DNS service implementations.
64 *
65 * This class supposed to be a real DNS monitor object as a singleton,
66 * so it lifecycle starts and ends together with VBoxSVC.
67 */
68class HostDnsServiceBase
69{
70 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(HostDnsServiceBase);
71
72public:
73
74 static HostDnsServiceBase *createHostDnsMonitor(void);
75
76public:
77
78 /* @note: method will wait till client call
79 HostDnsService::monitorThreadInitializationDone() */
80 virtual HRESULT init(HostDnsMonitorProxy *pProxy);
81 virtual void uninit(void);
82
83 virtual ~HostDnsServiceBase();
84
85protected:
86
87 explicit HostDnsServiceBase(bool fThreaded = false);
88
89 void setInfo(const HostDnsInformation &);
90
91 /* this function used only if HostDnsMonitor::HostDnsMonitor(true) */
92 void onMonitorThreadInitDone();
93
94public:
95
96 virtual int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs)
97 {
98 RT_NOREF(uTimeoutMs); AssertFailed(); return VERR_NOT_IMPLEMENTED;
99 }
100
101 virtual int monitorThreadProc(void) { AssertFailed(); return VERR_NOT_IMPLEMENTED; }
102
103private:
104
105 static DECLCALLBACK(int) threadMonitorProc(RTTHREAD, void *);
106
107protected:
108
109 mutable RTCLockMtx m_LockMtx;
110
111public: /** @todo r=andy Why is this public? */
112
113 struct Data;
114 Data *m;
115};
116
117/**
118 * This class supposed to be a proxy for events on changing Host Name Resolving configurations.
119 */
120class HostDnsMonitorProxy
121 : public Lockable
122{
123public:
124
125 HostDnsMonitorProxy();
126 virtual ~HostDnsMonitorProxy();
127
128public:
129
130 HRESULT init(VirtualBox *virtualbox);
131 void uninit(void);
132 void notify(const HostDnsInformation &info);
133
134 HRESULT GetNameServers(std::vector<com::Utf8Str> &aNameServers);
135 HRESULT GetDomainName(com::Utf8Str *pDomainName);
136 HRESULT GetSearchStrings(std::vector<com::Utf8Str> &aSearchStrings);
137
138 LockHandle *lockHandle() const RT_OVERRIDE;
139
140private:
141
142 uint32_t pollGlobalExtraData(AutoWriteLock &aLock);
143 bool updateInfo(const HostDnsInformation &info);
144
145private:
146 struct Data;
147 Data *m;
148 /** Object lock object. */
149 mutable util::RWLockHandle m_ObjectLock;
150};
151
152# if defined(RT_OS_DARWIN) || defined(DOXYGEN_RUNNING)
153class HostDnsServiceDarwin : public HostDnsServiceBase
154{
155public:
156
157 HostDnsServiceDarwin();
158 virtual ~HostDnsServiceDarwin();
159
160public:
161
162 HRESULT init(HostDnsMonitorProxy *pProxy);
163 void uninit(void);
164
165protected:
166
167 int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs) RT_OVERRIDE;
168 int monitorThreadProc(void) RT_OVERRIDE;
169
170private:
171
172 int updateInfo(void);
173 static void hostDnsServiceStoreCallback(void *store, void *arrayRef, void *info);
174 struct Data;
175 Data *m;
176};
177# endif
178# if defined(RT_OS_WINDOWS) || defined(DOXYGEN_RUNNING)
179class HostDnsServiceWin : public HostDnsServiceBase
180{
181public:
182 HostDnsServiceWin();
183 virtual ~HostDnsServiceWin();
184
185public:
186
187 HRESULT init(HostDnsMonitorProxy *pProxy);
188 void uninit(void);
189
190protected:
191
192 int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs) RT_OVERRIDE;
193 int monitorThreadProc(void) RT_OVERRIDE;
194
195private:
196
197 HRESULT updateInfo(void);
198
199private:
200
201 struct Data;
202 Data *m;
203};
204# endif
205# if defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) || defined(RT_OS_OS2) || defined(RT_OS_FREEBSD) \
206 || defined(DOXYGEN_RUNNING)
207class HostDnsServiceResolvConf: public HostDnsServiceBase
208{
209public:
210
211 explicit HostDnsServiceResolvConf(bool fThreaded = false) : HostDnsServiceBase(fThreaded), m(NULL) {}
212 virtual ~HostDnsServiceResolvConf();
213
214public:
215
216 HRESULT init(HostDnsMonitorProxy *pProxy, const char *aResolvConfFileName);
217 void uninit(void);
218
219 const std::string& getResolvConf(void) const;
220
221protected:
222
223 HRESULT readResolvConf(void);
224
225protected:
226
227 struct Data;
228 Data *m;
229};
230# if defined(RT_OS_SOLARIS) || defined(DOXYGEN_RUNNING)
231/**
232 * XXX: https://blogs.oracle.com/praks/entry/file_events_notification
233 */
234class HostDnsServiceSolaris : public HostDnsServiceResolvConf
235{
236public:
237
238 HostDnsServiceSolaris() {}
239 virtual ~HostDnsServiceSolaris() {}
240
241public:
242
243 virtual HRESULT init(HostDnsMonitorProxy *pProxy)
244 {
245 return HostDnsServiceResolvConf::init(pProxy, "/etc/resolv.conf");
246 }
247};
248
249# endif
250# if defined(RT_OS_LINUX) || defined(DOXYGEN_RUNNING)
251class HostDnsServiceLinux : public HostDnsServiceResolvConf
252{
253public:
254
255 HostDnsServiceLinux() : HostDnsServiceResolvConf(true), m_fdShutdown(-1) {}
256 virtual ~HostDnsServiceLinux();
257
258public:
259
260 HRESULT init(HostDnsMonitorProxy *pProxy);
261
262protected:
263
264 int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs) RT_OVERRIDE;
265 int monitorThreadProc(void) RT_OVERRIDE;
266
267 /** Socket end to write shutdown notification to, so the monitor thread will
268 * wake up and terminate. */
269 int m_fdShutdown;
270};
271
272# endif
273# if defined(RT_OS_FREEBSD) || defined(DOXYGEN_RUNNING)
274class HostDnsServiceFreebsd: public HostDnsServiceResolvConf
275{
276public:
277
278 HostDnsServiceFreebsd(){}
279 virtual ~HostDnsServiceFreebsd() {}
280
281public:
282
283 virtual HRESULT init(HostDnsMonitorProxy *pProxy)
284 {
285 return HostDnsServiceResolvConf::init(pProxy, "/etc/resolv.conf");
286 }
287};
288
289# endif
290# if defined(RT_OS_OS2) || defined(DOXYGEN_RUNNING)
291class HostDnsServiceOs2 : public HostDnsServiceResolvConf
292{
293public:
294
295 HostDnsServiceOs2() {}
296 virtual ~HostDnsServiceOs2() {}
297
298public:
299
300 /* XXX: \\MPTN\\ETC should be taken from environment variable ETC */
301 virtual HRESULT init(HostDnsMonitorProxy *pProxy)
302 {
303 return HostDnsServiceResolvConf::init(pProxy, "\\MPTN\\ETC\\RESOLV2");
304 }
305};
306
307# endif
308# endif
309
310#endif /* !MAIN_INCLUDED_SRC_src_server_HostDnsService_h */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette