OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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. | |
4 | |
5 #include "net/base/dns_reloader.h" | |
6 | |
7 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | |
8 | |
9 #include <resolv.h> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/lazy_instance.h" | |
13 #include "base/logging.h" | |
14 #include "base/message_loop.h" | |
15 #include "base/synchronization/lock.h" | |
16 #include "base/threading/thread_local_storage.h" | |
17 #include "net/base/network_change_notifier.h" | |
18 | |
19 namespace { | |
20 | |
21 // On Linux/BSD, changes to /etc/resolv.conf can go unnoticed thus resulting | |
22 // in DNS queries failing either because nameservers are unknown on startup | |
23 // or because nameserver info has changed as a result of e.g. connecting to | |
24 // a new network. Some distributions patch glibc to stat /etc/resolv.conf | |
25 // to try to automatically detect such changes but these patches are not | |
26 // universal and even patched systems such as Jaunty appear to need calls | |
27 // to res_ninit to reload the nameserver information in different threads. | |
28 // | |
29 // To fix this, on systems with FilePathWatcher support, we use | |
30 // NetworkChangeNotifier::DNSObserver to monitor /etc/resolv.conf to | |
31 // enable us to respond to DNS changes and reload the resolver state. | |
32 // | |
33 // OpenBSD does not have thread-safe res_ninit/res_nclose so we can't do | |
34 // the same trick there and most *BSD's don't yet have support for | |
35 // FilePathWatcher (but perhaps the new kqueue mac code just needs to be | |
36 // ported to *BSD to support that). | |
37 | |
38 class DnsReloader : public net::NetworkChangeNotifier::DNSObserver { | |
39 public: | |
40 struct ReloadState { | |
41 int resolver_generation; | |
42 }; | |
43 | |
44 // NetworkChangeNotifier::OnDNSChanged methods: | |
45 virtual void OnDNSChanged() { | |
eroman
2011/08/11 01:57:55
nit: Could you add the OVERRIDE macro here?
Craig
2011/08/15 17:10:56
Done.
| |
46 DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_IO); | |
eroman
2011/08/11 01:57:55
nit: DCHECK_EQ
Craig
2011/08/15 17:10:56
Done.
| |
47 base::AutoLock l(lock_); | |
48 resolver_generation_++; | |
49 } | |
50 | |
51 void MaybeReload() { | |
52 ReloadState* reload_state = static_cast<ReloadState*>(tls_index_.Get()); | |
53 base::AutoLock l(lock_); | |
54 | |
55 if (!reload_state) { | |
56 reload_state = new ReloadState(); | |
57 reload_state->resolver_generation = resolver_generation_; | |
58 res_ninit(&_res); | |
59 tls_index_.Set(reload_state); | |
60 } else if (reload_state->resolver_generation != resolver_generation_) { | |
61 reload_state->resolver_generation = resolver_generation_; | |
62 // It is safe to call res_nclose here since we know res_ninit will have | |
63 // been called above. | |
64 res_nclose(&_res); | |
65 res_ninit(&_res); | |
66 } | |
67 } | |
68 | |
69 // Free the allocated state. | |
70 static void SlotReturnFunction(void* data) { | |
71 ReloadState* reload_state = static_cast<ReloadState*>(data); | |
72 if (reload_state) | |
73 res_nclose(&_res); | |
74 delete reload_state; | |
75 } | |
76 | |
77 private: | |
78 DnsReloader() : resolver_generation_(0) { | |
79 tls_index_.Initialize(SlotReturnFunction); | |
80 net::NetworkChangeNotifier::AddDNSObserver(this); | |
81 } | |
82 | |
83 ~DnsReloader() { | |
84 NOTREACHED(); // LeakyLazyInstance is not destructed. | |
85 net::NetworkChangeNotifier::RemoveDNSObserver(this); | |
eroman
2011/08/11 01:57:55
Please remove this (if it is notreached).
Plus it
Craig
2011/08/15 17:10:56
Done.
| |
86 } | |
87 | |
88 base::Lock lock_; // Protects resolver_generation_. | |
89 int resolver_generation_; | |
90 friend struct base::DefaultLazyInstanceTraits<DnsReloader>; | |
91 | |
92 // We use thread local storage to identify which ReloadState to interact with. | |
93 static base::ThreadLocalStorage::Slot tls_index_ ; | |
94 | |
95 DISALLOW_COPY_AND_ASSIGN(DnsReloader); | |
96 }; | |
97 | |
98 // A TLS slot to the ReloadState for the current thread. | |
99 // static | |
100 base::ThreadLocalStorage::Slot DnsReloader::tls_index_( | |
101 base::LINKER_INITIALIZED); | |
102 | |
103 base::LazyInstance<DnsReloader, | |
104 base::LeakyLazyInstanceTraits<DnsReloader> > | |
105 g_dns_reloader(base::LINKER_INITIALIZED); | |
106 | |
107 } // namespace | |
108 | |
109 namespace net { | |
110 | |
111 void EnsureDnsReloaderInit() { | |
112 DnsReloader* t ALLOW_UNUSED = g_dns_reloader.Pointer(); | |
113 } | |
114 | |
115 void DnsReloaderMaybeReload() { | |
116 // This routine can be called by any of the DNS worker threads. | |
117 DnsReloader* dns_reloader = g_dns_reloader.Pointer(); | |
118 dns_reloader->MaybeReload(); | |
119 } | |
120 | |
121 } // namespace net | |
122 | |
123 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | |
OLD | NEW |