Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(244)

Side by Side Diff: net/base/dns_reloader.cc

Issue 1392943003: net: Move dns_reloader.* into dns directory. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sort better Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/dns_reloader.h ('k') | net/dns/dns_reloader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 !defined(OS_ANDROID)
9
10 #include <resolv.h>
11
12 #include "base/basictypes.h"
13 #include "base/lazy_instance.h"
14 #include "base/logging.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/synchronization/lock.h"
17 #include "base/threading/thread_local_storage.h"
18 #include "net/base/network_change_notifier.h"
19
20 namespace net {
21
22 namespace {
23
24 // On Linux/BSD, changes to /etc/resolv.conf can go unnoticed thus resulting
25 // in DNS queries failing either because nameservers are unknown on startup
26 // or because nameserver info has changed as a result of e.g. connecting to
27 // a new network. Some distributions patch glibc to stat /etc/resolv.conf
28 // to try to automatically detect such changes but these patches are not
29 // universal and even patched systems such as Jaunty appear to need calls
30 // to res_ninit to reload the nameserver information in different threads.
31 //
32 // To fix this, on systems with FilePathWatcher support, we use
33 // NetworkChangeNotifier::DNSObserver to monitor /etc/resolv.conf to
34 // enable us to respond to DNS changes and reload the resolver state.
35 //
36 // OpenBSD does not have thread-safe res_ninit/res_nclose so we can't do
37 // the same trick there and most *BSD's don't yet have support for
38 // FilePathWatcher (but perhaps the new kqueue mac code just needs to be
39 // ported to *BSD to support that).
40 //
41 // Android does not have /etc/resolv.conf. The system takes care of nameserver
42 // changes, so none of this is needed.
43
44 class DnsReloader : public NetworkChangeNotifier::DNSObserver {
45 public:
46 struct ReloadState {
47 int resolver_generation;
48 };
49
50 // NetworkChangeNotifier::DNSObserver:
51 void OnDNSChanged() override {
52 DCHECK(base::MessageLoopForIO::IsCurrent());
53 base::AutoLock l(lock_);
54 resolver_generation_++;
55 }
56
57 void MaybeReload() {
58 ReloadState* reload_state = static_cast<ReloadState*>(tls_index_.Get());
59 base::AutoLock l(lock_);
60
61 if (!reload_state) {
62 reload_state = new ReloadState();
63 reload_state->resolver_generation = resolver_generation_;
64 res_ninit(&_res);
65 tls_index_.Set(reload_state);
66 } else if (reload_state->resolver_generation != resolver_generation_) {
67 reload_state->resolver_generation = resolver_generation_;
68 // It is safe to call res_nclose here since we know res_ninit will have
69 // been called above.
70 res_nclose(&_res);
71 res_ninit(&_res);
72 }
73 }
74
75 // Free the allocated state.
76 static void SlotReturnFunction(void* data) {
77 ReloadState* reload_state = static_cast<ReloadState*>(data);
78 if (reload_state)
79 res_nclose(&_res);
80 delete reload_state;
81 }
82
83 private:
84 DnsReloader() : resolver_generation_(0) {
85 tls_index_.Initialize(SlotReturnFunction);
86 NetworkChangeNotifier::AddDNSObserver(this);
87 }
88
89 ~DnsReloader() override {
90 NOTREACHED(); // LeakyLazyInstance is not destructed.
91 }
92
93 base::Lock lock_; // Protects resolver_generation_.
94 int resolver_generation_;
95 friend struct base::DefaultLazyInstanceTraits<DnsReloader>;
96
97 // We use thread local storage to identify which ReloadState to interact with.
98 static base::ThreadLocalStorage::StaticSlot tls_index_;
99
100 DISALLOW_COPY_AND_ASSIGN(DnsReloader);
101 };
102
103 // A TLS slot to the ReloadState for the current thread.
104 // static
105 base::ThreadLocalStorage::StaticSlot DnsReloader::tls_index_ = TLS_INITIALIZER;
106
107 base::LazyInstance<DnsReloader>::Leaky
108 g_dns_reloader = LAZY_INSTANCE_INITIALIZER;
109
110 } // namespace
111
112 void EnsureDnsReloaderInit() {
113 g_dns_reloader.Pointer();
114 }
115
116 void DnsReloaderMaybeReload() {
117 // This routine can be called by any of the DNS worker threads.
118 DnsReloader* dns_reloader = g_dns_reloader.Pointer();
119 dns_reloader->MaybeReload();
120 }
121
122 } // namespace net
123
124 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) &&
125 // !defined(OS_ANDROID)
OLDNEW
« no previous file with comments | « net/base/dns_reloader.h ('k') | net/dns/dns_reloader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698