blob: 5ccedb4c0a29f99a22ef75e2223c0f9b5c92fbd6 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2021 The Chromium Authors
Eric Orthf79bc2e2021-04-02 22:23:062// 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/dns/dns_config_service_linux.h"
6
Eric Orthc30a1b72021-04-07 22:32:377#include <netdb.h>
Eric Orthf79bc2e2021-04-02 22:23:068#include <netinet/in.h>
9#include <resolv.h>
10#include <sys/socket.h>
11#include <sys/types.h>
12
Eric Orth0704a01d2021-04-08 17:03:1913#include <map>
Eric Orthf79bc2e2021-04-02 22:23:0614#include <memory>
15#include <string>
16#include <type_traits>
17#include <utility>
Eric Orthc30a1b72021-04-07 22:32:3718#include <vector>
Eric Orthf79bc2e2021-04-02 22:23:0619
20#include "base/bind.h"
21#include "base/callback.h"
22#include "base/check.h"
Tsuyoshi Horoea5c57b2022-08-10 02:15:0123#include "base/containers/contains.h"
Eric Orthf79bc2e2021-04-02 22:23:0624#include "base/files/file_path.h"
25#include "base/files/file_path_watcher.h"
26#include "base/location.h"
27#include "base/logging.h"
Keishi Hattorif28f4f82022-06-21 11:32:1528#include "base/memory/raw_ptr.h"
Tsuyoshi Horoea5c57b2022-08-10 02:15:0129#include "base/metrics/histogram_functions.h"
Eric Ortha4b7ca02021-04-09 22:28:5830#include "base/metrics/histogram_macros.h"
Eric Orthf79bc2e2021-04-02 22:23:0631#include "base/sequence_checker.h"
32#include "base/threading/scoped_blocking_call.h"
33#include "base/time/time.h"
34#include "net/base/ip_endpoint.h"
35#include "net/dns/dns_config.h"
Eric Orthc30a1b72021-04-07 22:32:3736#include "net/dns/nsswitch_reader.h"
Lina Ismail32c5ad42021-08-14 03:59:3437#include "net/dns/public/resolv_reader.h"
Eric Orthf79bc2e2021-04-02 22:23:0638#include "net/dns/serial_worker.h"
Anton Bikineev068d2912021-05-15 20:43:5239#include "third_party/abseil-cpp/absl/types/optional.h"
Eric Orthf79bc2e2021-04-02 22:23:0640
41namespace net {
42
43namespace internal {
44
45namespace {
46
47const base::FilePath::CharType kFilePathHosts[] =
48 FILE_PATH_LITERAL("/etc/hosts");
49
50#ifndef _PATH_RESCONF // Normally defined in <resolv.h>
Eric Orthc30a1b72021-04-07 22:32:3751#define _PATH_RESCONF FILE_PATH_LITERAL("/etc/resolv.conf")
Eric Orthf79bc2e2021-04-02 22:23:0652#endif
53
Eric Orthc30a1b72021-04-07 22:32:3754constexpr base::FilePath::CharType kFilePathResolv[] = _PATH_RESCONF;
Eric Orthf79bc2e2021-04-02 22:23:0655
Eric Orthc30a1b72021-04-07 22:32:3756#ifndef _PATH_NSSWITCH_CONF // Normally defined in <netdb.h>
57#define _PATH_NSSWITCH_CONF FILE_PATH_LITERAL("/etc/nsswitch.conf")
58#endif
Eric Orthf79bc2e2021-04-02 22:23:0659
Eric Orthc30a1b72021-04-07 22:32:3760constexpr base::FilePath::CharType kFilePathNsswitch[] = _PATH_NSSWITCH_CONF;
Eric Orthf79bc2e2021-04-02 22:23:0661
Anton Bikineev068d2912021-05-15 20:43:5262absl::optional<DnsConfig> ConvertResStateToDnsConfig(
Eric Orthf79bc2e2021-04-02 22:23:0663 const struct __res_state& res) {
Lina Ismail32c5ad42021-08-14 03:59:3464 absl::optional<std::vector<net::IPEndPoint>> nameservers =
65 GetNameservers(res);
Eric Orthf79bc2e2021-04-02 22:23:0666 DnsConfig dns_config;
67 dns_config.unhandled_options = false;
68
Lina Ismail32c5ad42021-08-14 03:59:3469 if (!nameservers.has_value())
Anton Bikineev068d2912021-05-15 20:43:5270 return absl::nullopt;
Eric Orthf79bc2e2021-04-02 22:23:0671
Lina Ismail32c5ad42021-08-14 03:59:3472 // Expected to be validated by GetNameservers()
73 DCHECK(res.options & RES_INIT);
Eric Orthf79bc2e2021-04-02 22:23:0674
Lina Ismail32c5ad42021-08-14 03:59:3475 dns_config.nameservers = std::move(nameservers.value());
Eric Orthf79bc2e2021-04-02 22:23:0676 dns_config.search.clear();
77 for (int i = 0; (i < MAXDNSRCH) && res.dnsrch[i]; ++i) {
78 dns_config.search.emplace_back(res.dnsrch[i]);
79 }
80
81 dns_config.ndots = res.ndots;
Peter Kastinge5a38ed2021-10-02 03:06:3582 dns_config.fallback_period = base::Seconds(res.retrans);
Eric Orthf79bc2e2021-04-02 22:23:0683 dns_config.attempts = res.retry;
84#if defined(RES_ROTATE)
85 dns_config.rotate = res.options & RES_ROTATE;
86#endif
87#if !defined(RES_USE_DNSSEC)
88 // Some versions of libresolv don't have support for the DO bit. In this
89 // case, we proceed without it.
90 static const int RES_USE_DNSSEC = 0;
91#endif
92
93 // The current implementation assumes these options are set. They normally
94 // cannot be overwritten by /etc/resolv.conf
95 const unsigned kRequiredOptions = RES_RECURSE | RES_DEFNAMES | RES_DNSRCH;
96 if ((res.options & kRequiredOptions) != kRequiredOptions) {
97 dns_config.unhandled_options = true;
98 return dns_config;
99 }
100
101 const unsigned kUnhandledOptions = RES_USEVC | RES_IGNTC | RES_USE_DNSSEC;
102 if (res.options & kUnhandledOptions) {
103 dns_config.unhandled_options = true;
104 return dns_config;
105 }
106
107 if (dns_config.nameservers.empty())
Anton Bikineev068d2912021-05-15 20:43:52108 return absl::nullopt;
Eric Orthf79bc2e2021-04-02 22:23:06109
110 // If any name server is 0.0.0.0, assume the configuration is invalid.
111 for (const IPEndPoint& nameserver : dns_config.nameservers) {
112 if (nameserver.address().IsZero())
Anton Bikineev068d2912021-05-15 20:43:52113 return absl::nullopt;
Eric Orthf79bc2e2021-04-02 22:23:06114 }
115 return dns_config;
116}
117
Eric Orth0704a01d2021-04-08 17:03:19118// Helper to add the effective result of `action` to `in_out_parsed_behavior`.
119// Returns false if `action` results in inconsistent behavior (setting an action
120// for a status that already has a different action).
121bool SetActionBehavior(const NsswitchReader::ServiceAction& action,
122 std::map<NsswitchReader::Status, NsswitchReader::Action>&
123 in_out_parsed_behavior) {
124 if (action.negated) {
125 for (NsswitchReader::Status status :
126 {NsswitchReader::Status::kSuccess, NsswitchReader::Status::kNotFound,
127 NsswitchReader::Status::kUnavailable,
128 NsswitchReader::Status::kTryAgain}) {
129 if (status != action.status) {
130 NsswitchReader::ServiceAction effective_action = {
131 /*negated=*/false, status, action.action};
132 if (!SetActionBehavior(effective_action, in_out_parsed_behavior))
133 return false;
134 }
135 }
136 } else {
137 if (in_out_parsed_behavior.count(action.status) >= 1 &&
138 in_out_parsed_behavior[action.status] != action.action) {
139 return false;
140 }
141 in_out_parsed_behavior[action.status] = action.action;
142 }
143
144 return true;
145}
146
147// Helper to determine if `actions` match `expected_actions`, meaning `actions`
148// contains no unknown statuses or actions and for every expectation set in
149// `expected_actions`, the expected action matches the effective result from
150// `actions`.
151bool AreActionsCompatible(
152 const std::vector<NsswitchReader::ServiceAction>& actions,
153 const std::map<NsswitchReader::Status, NsswitchReader::Action>
154 expected_actions) {
155 std::map<NsswitchReader::Status, NsswitchReader::Action> parsed_behavior;
156
157 for (const NsswitchReader::ServiceAction& action : actions) {
158 if (action.status == NsswitchReader::Status::kUnknown ||
159 action.action == NsswitchReader::Action::kUnknown) {
160 return false;
161 }
162
163 if (!SetActionBehavior(action, parsed_behavior))
164 return false;
165 }
166
167 // Default behavior if not configured.
168 if (parsed_behavior.count(NsswitchReader::Status::kSuccess) == 0)
169 parsed_behavior[NsswitchReader::Status::kSuccess] =
170 NsswitchReader::Action::kReturn;
171 if (parsed_behavior.count(NsswitchReader::Status::kNotFound) == 0)
172 parsed_behavior[NsswitchReader::Status::kNotFound] =
173 NsswitchReader::Action::kContinue;
174 if (parsed_behavior.count(NsswitchReader::Status::kUnavailable) == 0)
175 parsed_behavior[NsswitchReader::Status::kUnavailable] =
176 NsswitchReader::Action::kContinue;
177 if (parsed_behavior.count(NsswitchReader::Status::kTryAgain) == 0)
178 parsed_behavior[NsswitchReader::Status::kTryAgain] =
179 NsswitchReader::Action::kContinue;
180
181 for (const std::pair<const NsswitchReader::Status, NsswitchReader::Action>&
182 expected : expected_actions) {
183 if (parsed_behavior[expected.first] != expected.second)
184 return false;
185 }
186
187 return true;
188}
189
Eric Ortha4b7ca02021-04-09 22:28:58190// These values are emitted in metrics. Entries should not be renumbered and
191// numeric values should never be reused. (See NsswitchIncompatibleReason in
192// tools/metrics/histograms/enums.xml.)
Eric Orth0704a01d2021-04-08 17:03:19193enum class IncompatibleNsswitchReason {
194 kFilesMissing = 0,
Eric Ortha4b7ca02021-04-09 22:28:58195 kMultipleFiles = 1,
196 kBadFilesActions = 2,
197 kDnsMissing = 3,
198 kBadDnsActions = 4,
199 kBadMdnsMinimalActions = 5,
200 kBadOtherServiceActions = 6,
201 kUnknownService = 7,
202 kIncompatibleService = 8,
203 kMaxValue = kIncompatibleService
Eric Orth0704a01d2021-04-08 17:03:19204};
205
Eric Ortha4b7ca02021-04-09 22:28:58206void RecordIncompatibleNsswitchReason(
207 IncompatibleNsswitchReason reason,
Anton Bikineev068d2912021-05-15 20:43:52208 absl::optional<NsswitchReader::Service> service_token) {
Eric Ortha4b7ca02021-04-09 22:28:58209 if (service_token) {
Tsuyoshi Horoea5c57b2022-08-10 02:15:01210 base::UmaHistogramEnumeration(
211 "Net.DNS.DnsConfig.Nsswitch.IncompatibleService",
212 service_token.value());
Eric Ortha4b7ca02021-04-09 22:28:58213 }
Eric Orth0704a01d2021-04-08 17:03:19214}
215
Eric Orthc30a1b72021-04-07 22:32:37216bool IsNsswitchConfigCompatible(
217 const std::vector<NsswitchReader::ServiceSpecification>& nsswitch_hosts) {
Eric Orth0704a01d2021-04-08 17:03:19218 bool files_found = false;
219 for (const NsswitchReader::ServiceSpecification& specification :
220 nsswitch_hosts) {
221 switch (specification.service) {
222 case NsswitchReader::Service::kUnknown:
223 RecordIncompatibleNsswitchReason(
Eric Ortha4b7ca02021-04-09 22:28:58224 IncompatibleNsswitchReason::kUnknownService, specification.service);
Eric Orth0704a01d2021-04-08 17:03:19225 return false;
226
227 case NsswitchReader::Service::kFiles:
228 if (files_found) {
229 RecordIncompatibleNsswitchReason(
Eric Ortha4b7ca02021-04-09 22:28:58230 IncompatibleNsswitchReason::kMultipleFiles,
231 specification.service);
Eric Orth0704a01d2021-04-08 17:03:19232 return false;
233 }
234 files_found = true;
235 // Chrome will use the result on HOSTS hit and otherwise continue to
236 // DNS. `kFiles` entries must match that behavior to be compatible.
237 if (!AreActionsCompatible(specification.actions,
238 {{NsswitchReader::Status::kSuccess,
239 NsswitchReader::Action::kReturn},
240 {NsswitchReader::Status::kNotFound,
241 NsswitchReader::Action::kContinue},
242 {NsswitchReader::Status::kUnavailable,
243 NsswitchReader::Action::kContinue},
244 {NsswitchReader::Status::kTryAgain,
245 NsswitchReader::Action::kContinue}})) {
246 RecordIncompatibleNsswitchReason(
Eric Ortha4b7ca02021-04-09 22:28:58247 IncompatibleNsswitchReason::kBadFilesActions,
248 specification.service);
Eric Orth0704a01d2021-04-08 17:03:19249 return false;
250 }
251 break;
252
253 case NsswitchReader::Service::kDns:
254 if (!files_found) {
255 RecordIncompatibleNsswitchReason(
Eric Ortha4b7ca02021-04-09 22:28:58256 IncompatibleNsswitchReason::kFilesMissing,
Anton Bikineev068d2912021-05-15 20:43:52257 /*service_token=*/absl::nullopt);
Eric Orth0704a01d2021-04-08 17:03:19258 return false;
259 }
260 // Chrome will always stop if DNS finds a result or will otherwise
261 // fallback to the system resolver (and get whatever behavior is
262 // configured in nsswitch.conf), so the only compatibility requirement
263 // is that `kDns` entries are configured to return on success.
264 if (!AreActionsCompatible(specification.actions,
265 {{NsswitchReader::Status::kSuccess,
266 NsswitchReader::Action::kReturn}})) {
267 RecordIncompatibleNsswitchReason(
Eric Ortha4b7ca02021-04-09 22:28:58268 IncompatibleNsswitchReason::kBadDnsActions,
269 specification.service);
Eric Orth0704a01d2021-04-08 17:03:19270 return false;
271 }
272
273 // Ignore any entries after `kDns` because Chrome will fallback to the
274 // system resolver if a result was not found in DNS.
275 return true;
276
277 case NsswitchReader::Service::kMdns:
278 case NsswitchReader::Service::kMdns4:
279 case NsswitchReader::Service::kMdns6:
280 case NsswitchReader::Service::kResolve:
281 RecordIncompatibleNsswitchReason(
Eric Ortha4b7ca02021-04-09 22:28:58282 IncompatibleNsswitchReason::kIncompatibleService,
283 specification.service);
Eric Orth0704a01d2021-04-08 17:03:19284 return false;
285
286 case NsswitchReader::Service::kMdnsMinimal:
287 case NsswitchReader::Service::kMdns4Minimal:
288 case NsswitchReader::Service::kMdns6Minimal:
289 // Always compatible as long as `kUnavailable` is `kContinue` because
290 // the service is expected to always result in `kUnavailable` for any
291 // names Chrome would attempt to resolve (non-*.local names because
292 // Chrome always delegates *.local names to the system resolver).
293 if (!AreActionsCompatible(specification.actions,
294 {{NsswitchReader::Status::kUnavailable,
295 NsswitchReader::Action::kContinue}})) {
296 RecordIncompatibleNsswitchReason(
Eric Ortha4b7ca02021-04-09 22:28:58297 IncompatibleNsswitchReason::kBadMdnsMinimalActions,
298 specification.service);
Eric Orth0704a01d2021-04-08 17:03:19299 return false;
300 }
301 break;
302
303 case NsswitchReader::Service::kMyHostname:
304 case NsswitchReader::Service::kNis:
305 // Similar enough to Chrome behavior (or unlikely to matter for Chrome
306 // resolutions) to be considered compatible unless the actions do
307 // something very weird to skip remaining services without a result.
308 if (!AreActionsCompatible(specification.actions,
309 {{NsswitchReader::Status::kNotFound,
310 NsswitchReader::Action::kContinue},
311 {NsswitchReader::Status::kUnavailable,
312 NsswitchReader::Action::kContinue},
313 {NsswitchReader::Status::kTryAgain,
314 NsswitchReader::Action::kContinue}})) {
315 RecordIncompatibleNsswitchReason(
Eric Ortha4b7ca02021-04-09 22:28:58316 IncompatibleNsswitchReason::kBadOtherServiceActions,
317 specification.service);
Eric Orth0704a01d2021-04-08 17:03:19318 return false;
319 }
320 break;
321 }
322 }
323
Eric Ortha4b7ca02021-04-09 22:28:58324 RecordIncompatibleNsswitchReason(IncompatibleNsswitchReason::kDnsMissing,
Anton Bikineev068d2912021-05-15 20:43:52325 /*service_token=*/absl::nullopt);
Eric Orth0704a01d2021-04-08 17:03:19326 return false;
Eric Orthc30a1b72021-04-07 22:32:37327}
328
329} // namespace
330
331class DnsConfigServiceLinux::Watcher : public DnsConfigService::Watcher {
332 public:
333 explicit Watcher(DnsConfigServiceLinux& service)
334 : DnsConfigService::Watcher(service) {}
335 ~Watcher() override = default;
336
337 Watcher(const Watcher&) = delete;
338 Watcher& operator=(const Watcher&) = delete;
339
340 bool Watch() override {
341 CheckOnCorrectSequence();
342
343 bool success = true;
344 if (!resolv_watcher_.Watch(
345 base::FilePath(kFilePathResolv),
346 base::FilePathWatcher::Type::kNonRecursive,
Eric Ortha4b7ca02021-04-09 22:28:58347 base::BindRepeating(&Watcher::OnResolvFilePathWatcherChange,
Eric Orthc30a1b72021-04-07 22:32:37348 base::Unretained(this)))) {
349 LOG(ERROR) << "DNS config (resolv.conf) watch failed to start.";
350 success = false;
351 }
352
353 if (!nsswitch_watcher_.Watch(
354 base::FilePath(kFilePathNsswitch),
355 base::FilePathWatcher::Type::kNonRecursive,
Eric Ortha4b7ca02021-04-09 22:28:58356 base::BindRepeating(&Watcher::OnNsswitchFilePathWatcherChange,
Eric Orthc30a1b72021-04-07 22:32:37357 base::Unretained(this)))) {
358 LOG(ERROR) << "DNS nsswitch.conf watch failed to start.";
359 success = false;
360 }
361
362 if (!hosts_watcher_.Watch(
363 base::FilePath(kFilePathHosts),
364 base::FilePathWatcher::Type::kNonRecursive,
365 base::BindRepeating(&Watcher::OnHostsFilePathWatcherChange,
366 base::Unretained(this)))) {
367 LOG(ERROR) << "DNS hosts watch failed to start.";
368 success = false;
369 }
370 return success;
371 }
372
373 private:
Eric Ortha4b7ca02021-04-09 22:28:58374 void OnResolvFilePathWatcherChange(const base::FilePath& path, bool error) {
Tsuyoshi Horoea5c57b2022-08-10 02:15:01375 base::UmaHistogramBoolean("Net.DNS.DnsConfig.Resolv.FileChange", true);
Eric Ortha4b7ca02021-04-09 22:28:58376 OnConfigChanged(!error);
377 }
378
379 void OnNsswitchFilePathWatcherChange(const base::FilePath& path, bool error) {
Tsuyoshi Horoea5c57b2022-08-10 02:15:01380 base::UmaHistogramBoolean("Net.DNS.DnsConfig.Nsswitch.FileChange", true);
Eric Orthc30a1b72021-04-07 22:32:37381 OnConfigChanged(!error);
382 }
383
384 void OnHostsFilePathWatcherChange(const base::FilePath& path, bool error) {
385 OnHostsChanged(!error);
386 }
387
388 base::FilePathWatcher resolv_watcher_;
389 base::FilePathWatcher nsswitch_watcher_;
390 base::FilePathWatcher hosts_watcher_;
391};
392
393// A SerialWorker that uses libresolv to initialize res_state and converts
394// it to DnsConfig.
395class DnsConfigServiceLinux::ConfigReader : public SerialWorker {
396 public:
397 explicit ConfigReader(DnsConfigServiceLinux& service,
398 std::unique_ptr<ResolvReader> resolv_reader,
399 std::unique_ptr<NsswitchReader> nsswitch_reader)
400 : service_(&service),
Eric Orth8ec1b8e2021-10-11 19:33:47401 work_item_(std::make_unique<WorkItem>(std::move(resolv_reader),
402 std::move(nsswitch_reader))) {
Eric Orthc30a1b72021-04-07 22:32:37403 // Allow execution on another thread; nothing thread-specific about
404 // constructor.
405 DETACH_FROM_SEQUENCE(sequence_checker_);
Eric Orthc30a1b72021-04-07 22:32:37406 }
407
Eric Orth8ec1b8e2021-10-11 19:33:47408 ~ConfigReader() override = default;
409
Eric Orthc30a1b72021-04-07 22:32:37410 ConfigReader(const ConfigReader&) = delete;
411 ConfigReader& operator=(const ConfigReader&) = delete;
412
Eric Orth8ec1b8e2021-10-11 19:33:47413 std::unique_ptr<SerialWorker::WorkItem> CreateWorkItem() override {
414 // Reuse same `WorkItem` to allow reuse of contained reader objects.
415 DCHECK(work_item_);
416 return std::move(work_item_);
Eric Orthc30a1b72021-04-07 22:32:37417 }
418
Erik Andersond328a542022-03-04 17:35:30419 bool OnWorkFinished(std::unique_ptr<SerialWorker::WorkItem>
Eric Orth8ec1b8e2021-10-11 19:33:47420 serial_worker_work_item) override {
421 DCHECK(serial_worker_work_item);
422 DCHECK(!work_item_);
Eric Orthc30a1b72021-04-07 22:32:37423 DCHECK(!IsCancelled());
Eric Orth8ec1b8e2021-10-11 19:33:47424
425 work_item_.reset(static_cast<WorkItem*>(serial_worker_work_item.release()));
426 if (work_item_->dns_config_.has_value()) {
427 service_->OnConfigRead(std::move(work_item_->dns_config_).value());
Erik Andersond328a542022-03-04 17:35:30428 return true;
Eric Orthc30a1b72021-04-07 22:32:37429 } else {
430 LOG(WARNING) << "Failed to read DnsConfig.";
Erik Andersond328a542022-03-04 17:35:30431 return false;
Eric Orthc30a1b72021-04-07 22:32:37432 }
433 }
434
435 private:
Eric Orth8ec1b8e2021-10-11 19:33:47436 class WorkItem : public SerialWorker::WorkItem {
437 public:
438 WorkItem(std::unique_ptr<ResolvReader> resolv_reader,
439 std::unique_ptr<NsswitchReader> nsswitch_reader)
440 : resolv_reader_(std::move(resolv_reader)),
441 nsswitch_reader_(std::move(nsswitch_reader)) {
442 DCHECK(resolv_reader_);
443 DCHECK(nsswitch_reader_);
444 }
Eric Orthc30a1b72021-04-07 22:32:37445
Eric Orth8ec1b8e2021-10-11 19:33:47446 void DoWork() override {
447 base::ScopedBlockingCall scoped_blocking_call(
448 FROM_HERE, base::BlockingType::MAY_BLOCK);
449
Dominique Fauteux-Chapleaue08899a2021-12-15 21:03:40450 {
451 std::unique_ptr<ScopedResState> res = resolv_reader_->GetResState();
452 if (res) {
453 dns_config_ = ConvertResStateToDnsConfig(res->state());
454 }
Eric Orth8ec1b8e2021-10-11 19:33:47455 }
456
Tsuyoshi Horoea5c57b2022-08-10 02:15:01457 base::UmaHistogramBoolean("Net.DNS.DnsConfig.Resolv.Read",
458 dns_config_.has_value());
Eric Orth8ec1b8e2021-10-11 19:33:47459 if (!dns_config_.has_value())
460 return;
Tsuyoshi Horoea5c57b2022-08-10 02:15:01461 base::UmaHistogramBoolean("Net.DNS.DnsConfig.Resolv.Valid",
462 dns_config_->IsValid());
463 base::UmaHistogramBoolean("Net.DNS.DnsConfig.Resolv.Compatible",
464 !dns_config_->unhandled_options);
Eric Orth8ec1b8e2021-10-11 19:33:47465
466 // Override `fallback_period` value to match default setting on
467 // Windows.
468 dns_config_->fallback_period = kDnsDefaultFallbackPeriod;
469
470 if (dns_config_ && !dns_config_->unhandled_options) {
471 std::vector<NsswitchReader::ServiceSpecification> nsswitch_hosts =
472 nsswitch_reader_->ReadAndParseHosts();
Eric Orth8ec1b8e2021-10-11 19:33:47473 dns_config_->unhandled_options =
474 !IsNsswitchConfigCompatible(nsswitch_hosts);
Tsuyoshi Horoea5c57b2022-08-10 02:15:01475 base::UmaHistogramBoolean("Net.DNS.DnsConfig.Nsswitch.Compatible",
476 !dns_config_->unhandled_options);
477 base::UmaHistogramBoolean(
478 "Net.DNS.DnsConfig.Nsswitch.NisServiceInHosts",
479 base::Contains(nsswitch_hosts, NsswitchReader::Service::kNis,
480 &NsswitchReader::ServiceSpecification::service));
Eric Orth8ec1b8e2021-10-11 19:33:47481 }
482 }
483
484 private:
485 friend class ConfigReader;
486 absl::optional<DnsConfig> dns_config_;
487 std::unique_ptr<ResolvReader> resolv_reader_;
488 std::unique_ptr<NsswitchReader> nsswitch_reader_;
489 };
490
491 // Raw pointer to owning DnsConfigService.
Keishi Hattorif28f4f82022-06-21 11:32:15492 const raw_ptr<DnsConfigServiceLinux> service_;
Eric Orth8ec1b8e2021-10-11 19:33:47493
494 // Null while the `WorkItem` is running on the `ThreadPool`.
495 std::unique_ptr<WorkItem> work_item_;
Eric Orthc30a1b72021-04-07 22:32:37496};
497
Eric Orthc30a1b72021-04-07 22:32:37498DnsConfigServiceLinux::DnsConfigServiceLinux()
499 : DnsConfigService(kFilePathHosts) {
500 // Allow constructing on one thread and living on another.
501 DETACH_FROM_SEQUENCE(sequence_checker_);
502}
503
504DnsConfigServiceLinux::~DnsConfigServiceLinux() {
505 if (config_reader_)
506 config_reader_->Cancel();
507}
508
509void DnsConfigServiceLinux::ReadConfigNow() {
510 if (!config_reader_)
511 CreateReader();
512 config_reader_->WorkNow();
513}
514
515bool DnsConfigServiceLinux::StartWatching() {
516 CreateReader();
517 watcher_ = std::make_unique<Watcher>(*this);
518 return watcher_->Watch();
519}
520
521void DnsConfigServiceLinux::CreateReader() {
522 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
523 DCHECK(!config_reader_);
524 DCHECK(resolv_reader_);
525 DCHECK(nsswitch_reader_);
Eric Orth8ec1b8e2021-10-11 19:33:47526 config_reader_ = std::make_unique<ConfigReader>(
Eric Orthc30a1b72021-04-07 22:32:37527 *this, std::move(resolv_reader_), std::move(nsswitch_reader_));
528}
529
Eric Orthf79bc2e2021-04-02 22:23:06530} // namespace internal
531
532// static
533std::unique_ptr<DnsConfigService> DnsConfigService::CreateSystemService() {
534 return std::make_unique<internal::DnsConfigServiceLinux>();
535}
536
537} // namespace net