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() OVERRIDE { |
| 46 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO); |
| 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 } |
| 86 |
| 87 base::Lock lock_; // Protects resolver_generation_. |
| 88 int resolver_generation_; |
| 89 friend struct base::DefaultLazyInstanceTraits<DnsReloader>; |
| 90 |
| 91 // We use thread local storage to identify which ReloadState to interact with. |
| 92 static base::ThreadLocalStorage::Slot tls_index_ ; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(DnsReloader); |
| 95 }; |
| 96 |
| 97 // A TLS slot to the ReloadState for the current thread. |
| 98 // static |
| 99 base::ThreadLocalStorage::Slot DnsReloader::tls_index_( |
| 100 base::LINKER_INITIALIZED); |
| 101 |
| 102 base::LazyInstance<DnsReloader, |
| 103 base::LeakyLazyInstanceTraits<DnsReloader> > |
| 104 g_dns_reloader(base::LINKER_INITIALIZED); |
| 105 |
| 106 } // namespace |
| 107 |
| 108 namespace net { |
| 109 |
| 110 void EnsureDnsReloaderInit() { |
| 111 DnsReloader* t ALLOW_UNUSED = g_dns_reloader.Pointer(); |
| 112 } |
| 113 |
| 114 void DnsReloaderMaybeReload() { |
| 115 // This routine can be called by any of the DNS worker threads. |
| 116 DnsReloader* dns_reloader = g_dns_reloader.Pointer(); |
| 117 dns_reloader->MaybeReload(); |
| 118 } |
| 119 |
| 120 } // namespace net |
| 121 |
| 122 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) |
OLD | NEW |