• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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_reload_timer.h"
6 
7 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD)
8 #include "base/lazy_instance.h"
9 #include "base/threading/thread_local_storage.h"
10 #include "base/time.h"
11 
12 namespace {
13 
14 // On Linux/BSD, changes to /etc/resolv.conf can go unnoticed thus resulting
15 // in DNS queries failing either because nameservers are unknown on startup
16 // or because nameserver info has changed as a result of e.g. connecting to
17 // a new network. Some distributions patch glibc to stat /etc/resolv.conf
18 // to try to automatically detect such changes but these patches are not
19 // universal and even patched systems such as Jaunty appear to need calls
20 // to res_ninit to reload the nameserver information in different threads.
21 //
22 // We adopt the Mozilla solution here which is to call res_ninit when
23 // lookups fail and to rate limit the reloading to once per second per
24 // thread.
25 //
26 // OpenBSD does not have thread-safe res_ninit/res_nclose so we can't do
27 // the same trick there.
28 
29 // Keep a timer per calling thread to rate limit the calling of res_ninit.
30 class DnsReloadTimer {
31  public:
32   // Check if the timer for the calling thread has expired. When no
33   // timer exists for the calling thread, create one.
Expired()34   bool Expired() {
35     const base::TimeDelta kRetryTime = base::TimeDelta::FromSeconds(1);
36     base::TimeTicks now = base::TimeTicks::Now();
37     base::TimeTicks* timer_ptr =
38       static_cast<base::TimeTicks*>(tls_index_.Get());
39 
40     if (!timer_ptr) {
41       timer_ptr = new base::TimeTicks();
42       *timer_ptr = base::TimeTicks::Now();
43       tls_index_.Set(timer_ptr);
44       // Return true to reload dns info on the first call for each thread.
45       return true;
46     } else if (now - *timer_ptr > kRetryTime) {
47       *timer_ptr = now;
48       return true;
49     } else {
50       return false;
51     }
52   }
53 
54   // Free the allocated timer.
SlotReturnFunction(void * data)55   static void SlotReturnFunction(void* data) {
56     base::TimeTicks* tls_data = static_cast<base::TimeTicks*>(data);
57     delete tls_data;
58   }
59 
60  private:
61   friend struct base::DefaultLazyInstanceTraits<DnsReloadTimer>;
62 
DnsReloadTimer()63   DnsReloadTimer() {
64     // During testing the DnsReloadTimer Singleton may be created and destroyed
65     // multiple times. Initialize the ThreadLocalStorage slot only once.
66     if (!tls_index_.initialized())
67       tls_index_.Initialize(SlotReturnFunction);
68   }
69 
~DnsReloadTimer()70   ~DnsReloadTimer() {
71   }
72 
73   // We use thread local storage to identify which base::TimeTicks to
74   // interact with.
75   static base::ThreadLocalStorage::Slot tls_index_ ;
76 
77   DISALLOW_COPY_AND_ASSIGN(DnsReloadTimer);
78 };
79 
80 // A TLS slot to the TimeTicks for the current thread.
81 // static
82 base::ThreadLocalStorage::Slot DnsReloadTimer::tls_index_(
83     base::LINKER_INITIALIZED);
84 
85 base::LazyInstance<DnsReloadTimer,
86                    base::LeakyLazyInstanceTraits<DnsReloadTimer> >
87     g_dns_reload_timer(base::LINKER_INITIALIZED);
88 
89 }  // namespace
90 
91 namespace net {
92 
DnsReloadTimerHasExpired()93 bool DnsReloadTimerHasExpired() {
94   DnsReloadTimer* dns_timer = g_dns_reload_timer.Pointer();
95   return dns_timer->Expired();
96 }
97 
98 }  // namespace net
99 
100 #endif  // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD)
101