1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/base/dns_reloader.h"
7 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
12 #include "base/basictypes.h"
13 #include "base/lazy_instance.h"
14 #include "base/logging.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/synchronization/lock.h"
17 #include "base/threading/thread_local_storage.h"
18 #include "net/base/network_change_notifier.h"
24 // On Linux/BSD, changes to /etc/resolv.conf can go unnoticed thus resulting
25 // in DNS queries failing either because nameservers are unknown on startup
26 // or because nameserver info has changed as a result of e.g. connecting to
27 // a new network. Some distributions patch glibc to stat /etc/resolv.conf
28 // to try to automatically detect such changes but these patches are not
29 // universal and even patched systems such as Jaunty appear to need calls
30 // to res_ninit to reload the nameserver information in different threads.
32 // To fix this, on systems with FilePathWatcher support, we use
33 // NetworkChangeNotifier::DNSObserver to monitor /etc/resolv.conf to
34 // enable us to respond to DNS changes and reload the resolver state.
36 // OpenBSD does not have thread-safe res_ninit/res_nclose so we can't do
37 // the same trick there and most *BSD's don't yet have support for
38 // FilePathWatcher (but perhaps the new kqueue mac code just needs to be
39 // ported to *BSD to support that).
41 // Android does not have /etc/resolv.conf. The system takes care of nameserver
42 // changes, so none of this is needed.
44 class DnsReloader
: public NetworkChangeNotifier::DNSObserver
{
47 int resolver_generation
;
50 // NetworkChangeNotifier::DNSObserver:
51 void OnDNSChanged() override
{
52 DCHECK(base::MessageLoopForIO::IsCurrent());
53 base::AutoLock
l(lock_
);
54 resolver_generation_
++;
58 ReloadState
* reload_state
= static_cast<ReloadState
*>(tls_index_
.Get());
59 base::AutoLock
l(lock_
);
62 reload_state
= new ReloadState();
63 reload_state
->resolver_generation
= resolver_generation_
;
65 tls_index_
.Set(reload_state
);
66 } else if (reload_state
->resolver_generation
!= resolver_generation_
) {
67 reload_state
->resolver_generation
= resolver_generation_
;
68 // It is safe to call res_nclose here since we know res_ninit will have
75 // Free the allocated state.
76 static void SlotReturnFunction(void* data
) {
77 ReloadState
* reload_state
= static_cast<ReloadState
*>(data
);
84 DnsReloader() : resolver_generation_(0) {
85 tls_index_
.Initialize(SlotReturnFunction
);
86 NetworkChangeNotifier::AddDNSObserver(this);
89 ~DnsReloader() override
{
90 NOTREACHED(); // LeakyLazyInstance is not destructed.
93 base::Lock lock_
; // Protects resolver_generation_.
94 int resolver_generation_
;
95 friend struct base::DefaultLazyInstanceTraits
<DnsReloader
>;
97 // We use thread local storage to identify which ReloadState to interact with.
98 static base::ThreadLocalStorage::StaticSlot tls_index_
;
100 DISALLOW_COPY_AND_ASSIGN(DnsReloader
);
103 // A TLS slot to the ReloadState for the current thread.
105 base::ThreadLocalStorage::StaticSlot
DnsReloader::tls_index_
= TLS_INITIALIZER
;
107 base::LazyInstance
<DnsReloader
>::Leaky
108 g_dns_reloader
= LAZY_INSTANCE_INITIALIZER
;
112 void EnsureDnsReloaderInit() {
113 g_dns_reloader
.Pointer();
116 void DnsReloaderMaybeReload() {
117 // This routine can be called by any of the DNS worker threads.
118 DnsReloader
* dns_reloader
= g_dns_reloader
.Pointer();
119 dns_reloader
->MaybeReload();
124 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) &&
125 // !defined(OS_ANDROID)