Chromium Code Reviews| 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 | |
|
eroman
2011/07/25 20:13:03
Actually, it looks like since these calls are prot
Craig
2011/07/26 15:15:32
Ah, interesting!
I'm not that clued up on *BSD so
eroman
2011/07/26 16:53:13
Agreed. I definitely am not suggesting you enable
| |
| 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() { | |
| 46 DCHECK(MessageLoop::current()->thread_name() == "Chrome_IOThread"); | |
|
eroman
2011/07/25 20:13:03
Please avoid referencing chrome thread names (sinc
Craig
2011/08/03 19:30:58
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 base::Lock lock_; // Protects resolver_generation_. | |
| 79 int resolver_generation_; | |
| 80 friend struct base::DefaultLazyInstanceTraits<DnsReloader>; | |
| 81 | |
| 82 DnsReloader() : resolver_generation_(0) { | |
|
eroman
2011/07/25 20:13:03
style-nit: please declare methods above data membe
Craig
2011/08/03 19:30:58
Done.
| |
| 83 tls_index_.Initialize(SlotReturnFunction); | |
| 84 net::NetworkChangeNotifier::AddDNSObserver(this); | |
| 85 } | |
| 86 | |
| 87 ~DnsReloader() { | |
| 88 NOTREACHED(); // LeakyLazyInstance is not destructed. | |
| 89 net::NetworkChangeNotifier::RemoveDNSObserver(this); | |
| 90 } | |
| 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 |