1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/dns/dns_reloader.h"
6
7 #include "build/build_config.h"
8
9 // If we're not on a POSIX system, it's not even safe to try to include resolv.h
10 // - there's not guarantee it exists at all. :(
11 #if BUILDFLAG(IS_POSIX)
12
13 #include <resolv.h>
14
15 // This code only works on systems where the C library provides res_ninit(3) and
16 // res_nclose(3), which requires __RES >= 19991006 (most libcs at this point,
17 // but not all).
18 //
19 // This code is also not used on either macOS or iOS, even though both platforms
20 // have res_ninit(3). On iOS, /etc/hosts is immutable so there's no reason for
21 // us to watch it; on macOS, there is a system mechanism for listening to DNS
22 // changes which does not require use to do this kind of reloading. See
23 // //net/dns/dns_config_watcher_mac.cc.
24 //
25 // It *also* is not used on Android, because Android handles nameserver changes
26 // for us and has no /etc/resolv.conf. Despite that, Bionic does export these
27 // interfaces, so we need to not use them.
28 //
29 // It is also also not used on Fuchsia. Regrettably, Fuchsia's resolv.h has
30 // __RES set to 19991006, but does not actually provide res_ninit(3). This was
31 // an old musl bug that was fixed by musl c8fdcfe5, but Fuchsia's SDK doesn't
32 // have that change.
33 #if defined(__RES) && __RES >= 19991006 && !BUILDFLAG(IS_APPLE) && \
34 !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_FUCHSIA)
35 // We define this so we don't need to restate the complex condition here twice
36 // below - it would be easy for the copies below to get out of sync.
37 #define USE_RES_NINIT
38 #endif // defined(_RES) && ...
39 #endif // BUILDFLAG(IS_POSIX)
40
41 #if defined(USE_RES_NINIT)
42
43 #include "base/lazy_instance.h"
44 #include "base/notreached.h"
45 #include "base/synchronization/lock.h"
46 #include "base/task/current_thread.h"
47 #include "base/threading/thread_local.h"
48 #include "net/base/network_change_notifier.h"
49
50 namespace net {
51
52 namespace {
53
54 // On Linux/BSD, changes to /etc/resolv.conf can go unnoticed thus resulting
55 // in DNS queries failing either because nameservers are unknown on startup
56 // or because nameserver info has changed as a result of e.g. connecting to
57 // a new network. Some distributions patch glibc to stat /etc/resolv.conf
58 // to try to automatically detect such changes but these patches are not
59 // universal and even patched systems such as Jaunty appear to need calls
60 // to res_ninit to reload the nameserver information in different threads.
61 //
62 // To fix this, on systems with FilePathWatcher support, we use
63 // NetworkChangeNotifier::DNSObserver to monitor /etc/resolv.conf to
64 // enable us to respond to DNS changes and reload the resolver state.
65 //
66 // Android does not have /etc/resolv.conf. The system takes care of nameserver
67 // changes, so none of this is needed.
68 //
69 // TODO(crbug.com/971411): Convert to SystemDnsConfigChangeNotifier because this
70 // really only cares about system DNS config changes, not Chrome effective
71 // config changes.
72
73 class DnsReloader : public NetworkChangeNotifier::DNSObserver {
74 public:
75 DnsReloader(const DnsReloader&) = delete;
76 DnsReloader& operator=(const DnsReloader&) = delete;
77
78 // NetworkChangeNotifier::DNSObserver:
OnDNSChanged()79 void OnDNSChanged() override {
80 base::AutoLock lock(lock_);
81 resolver_generation_++;
82 }
83
MaybeReload()84 void MaybeReload() {
85 ReloadState* reload_state = tls_reload_state_.Get();
86 base::AutoLock lock(lock_);
87
88 if (!reload_state) {
89 auto new_reload_state = std::make_unique<ReloadState>();
90 new_reload_state->resolver_generation = resolver_generation_;
91 res_ninit(&_res);
92 tls_reload_state_.Set(std::move(new_reload_state));
93 } else if (reload_state->resolver_generation != resolver_generation_) {
94 reload_state->resolver_generation = resolver_generation_;
95 // It is safe to call res_nclose here since we know res_ninit will have
96 // been called above.
97 res_nclose(&_res);
98 res_ninit(&_res);
99 }
100 }
101
102 private:
103 struct ReloadState {
~ReloadStatenet::__anona71a6bf10111::DnsReloader::ReloadState104 ~ReloadState() { res_nclose(&_res); }
105
106 int resolver_generation;
107 };
108
DnsReloader()109 DnsReloader() { NetworkChangeNotifier::AddDNSObserver(this); }
110
~DnsReloader()111 ~DnsReloader() override {
112 NOTREACHED(); // LeakyLazyInstance is not destructed.
113 }
114
115 base::Lock lock_; // Protects resolver_generation_.
116 int resolver_generation_ = 0;
117 friend struct base::LazyInstanceTraitsBase<DnsReloader>;
118
119 // We use thread local storage to identify which ReloadState to interact with.
120 base::ThreadLocalOwnedPointer<ReloadState> tls_reload_state_;
121 };
122
123 base::LazyInstance<DnsReloader>::Leaky
124 g_dns_reloader = LAZY_INSTANCE_INITIALIZER;
125
126 } // namespace
127
EnsureDnsReloaderInit()128 void EnsureDnsReloaderInit() {
129 g_dns_reloader.Pointer();
130 }
131
DnsReloaderMaybeReload()132 void DnsReloaderMaybeReload() {
133 // This routine can be called by any of the DNS worker threads.
134 DnsReloader* dns_reloader = g_dns_reloader.Pointer();
135 dns_reloader->MaybeReload();
136 }
137
138 } // namespace net
139
140 #else // !USE_RES_NINIT
141
142 namespace net {
143
EnsureDnsReloaderInit()144 void EnsureDnsReloaderInit() {}
145
DnsReloaderMaybeReload()146 void DnsReloaderMaybeReload() {}
147
148 } // namespace net
149
150 #endif // defined(USE_RES_NINIT)
151