Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
|
agl
2011/07/13 22:17:48
wrong year.
Craig
2011/07/18 16:18:12
Done.
| |
| 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/threading/thread_local_storage.h" | |
| 14 #include "net/base/network_change_notifier.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // On Linux/BSD, changes to /etc/resolv.conf can go unnoticed thus resulting | |
| 19 // in DNS queries failing either because nameservers are unknown on startup | |
| 20 // or because nameserver info has changed as a result of e.g. connecting to | |
| 21 // a new network. Some distributions patch glibc to stat /etc/resolv.conf | |
| 22 // to try to automatically detect such changes but these patches are not | |
| 23 // universal and even patched systems such as Jaunty appear to need calls | |
| 24 // to res_ninit to reload the nameserver information in different threads. | |
| 25 // | |
| 26 // To fix this, on systems with FilePathWatcher support, we use | |
| 27 // NetworkChangeNotifier::DNSObserver to monitor /etc/resolv.conf to | |
| 28 // enable us to respond to DNS changes and reload the resolver state. | |
| 29 // | |
| 30 // OpenBSD does not have thread-safe res_ninit/res_nclose so we can't do | |
| 31 // the same trick there and most *BSD's don't yet have support for | |
| 32 // FilePathWatcher (but perhaps the new kqueue mac code just needs to be | |
| 33 // ported to *BSD to support that). | |
| 34 | |
| 35 class DnsReloader : public net::NetworkChangeNotifier::DNSObserver { | |
| 36 public: | |
| 37 struct ReloadState { | |
| 38 int resolver_generation; | |
| 39 }; | |
| 40 | |
| 41 // NetworkChangeNotifier::OnDNSChanged methods: | |
| 42 virtual void OnDNSChanged() { | |
|
agl
2011/07/13 22:17:48
needs a comment to say which thread it runs on. (A
Craig
2011/07/18 16:18:12
Done with a DCHECK but it's a touch ugly.
| |
| 43 resolver_generation_++; | |
| 44 } | |
| 45 | |
| 46 void MaybeReload() { | |
| 47 ReloadState* reload_state = static_cast<ReloadState*>(tls_index_.Get()); | |
| 48 | |
| 49 if (!reload_state) { | |
| 50 reload_state = new ReloadState(); | |
| 51 reload_state->resolver_generation = resolver_generation_; | |
| 52 res_ninit(&_res); | |
| 53 tls_index_.Set(reload_state); | |
| 54 } else if (reload_state->resolver_generation != resolver_generation_) { | |
| 55 reload_state->resolver_generation = resolver_generation_; | |
| 56 // It is safe to call res_nclose here since we know res_ninit will have | |
| 57 // been called above. | |
| 58 res_nclose(&_res); | |
| 59 res_ninit(&_res); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 // Free the allocated state. | |
| 64 static void SlotReturnFunction(void* data) { | |
| 65 ReloadState* reload_state = static_cast<ReloadState*>(data); | |
| 66 if (reload_state) | |
| 67 res_nclose(&_res); | |
| 68 delete reload_state; | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 int resolver_generation_; | |
|
agl
2011/07/13 22:17:48
it appears that this is read/written across differ
Craig
2011/07/14 17:23:30
I assumed that it was thread-safe based on the ass
Craig
2011/07/18 16:18:12
Done.
| |
| 73 friend struct base::DefaultLazyInstanceTraits<DnsReloader>; | |
| 74 | |
| 75 DnsReloader() : resolver_generation_(0) { | |
| 76 // During testing the DnsReloader Singleton may be created and destroyed | |
| 77 // multiple times. Initialize the ThreadLocalStorage slot only once. | |
|
willchan no longer on Chromium
2011/07/14 15:42:32
Really? Which test does this?
Craig
2011/07/14 17:23:30
I copied the code blindly from dns_reload_timer :(
Craig
2011/07/18 16:18:13
Done.
| |
| 78 if (!tls_index_.initialized()) | |
| 79 tls_index_.Initialize(SlotReturnFunction); | |
| 80 net::NetworkChangeNotifier::AddDNSObserver(this); | |
| 81 } | |
| 82 | |
| 83 ~DnsReloader() { | |
| 84 net::NetworkChangeNotifier::RemoveDNSObserver(this); | |
| 85 } | |
| 86 | |
| 87 // We use thread local storage to identify which ReloadState to interact with. | |
| 88 static base::ThreadLocalStorage::Slot tls_index_ ; | |
|
willchan no longer on Chromium
2011/07/14 15:42:32
We generally use ThreadLocalPointer instead of Thr
Craig
2011/07/18 16:18:13
With ThreadLocalStorage, I can hook into the Slot
| |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(DnsReloader); | |
| 91 }; | |
| 92 | |
| 93 // A TLS slot to the ReloadState for the current thread. | |
| 94 // static | |
| 95 base::ThreadLocalStorage::Slot DnsReloader::tls_index_( | |
| 96 base::LINKER_INITIALIZED); | |
| 97 | |
| 98 base::LazyInstance<DnsReloader, | |
| 99 base::LeakyLazyInstanceTraits<DnsReloader> > | |
| 100 g_dns_reloader(base::LINKER_INITIALIZED); | |
| 101 | |
| 102 } // namespace | |
| 103 | |
| 104 namespace net { | |
| 105 | |
| 106 void DnsReloaderInit() { | |
| 107 DnsReloader* t ALLOW_UNUSED = g_dns_reloader.Pointer(); | |
| 108 } | |
| 109 | |
| 110 void DnsReloaderMaybeReload() { | |
|
agl
2011/07/13 22:17:48
Needs a comment to say that it runs on any DNS wor
Craig
2011/07/18 16:18:13
Done.
| |
| 111 DnsReloader* dns_reloader = g_dns_reloader.Pointer(); | |
| 112 dns_reloader->MaybeReload(); | |
| 113 } | |
| 114 | |
| 115 } // namespace net | |
| 116 | |
| 117 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | |
| OLD | NEW |