-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathflush_linux.go
More file actions
64 lines (58 loc) · 2.09 KB
/
flush_linux.go
File metadata and controls
64 lines (58 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//go:build linux
package txeh
import (
"errors"
"os/exec"
"runtime"
)
// ErrNoResolver is returned when no supported DNS resolver is found on the system.
// If your system does not cache DNS locally (e.g., no systemd-resolved, dnsmasq, or
// unbound), hosts file changes take effect immediately without flushing.
var ErrNoResolver = errors.New(
"systemd-resolved not detected (tried resolvectl, systemd-resolve). " +
"If your system does not cache DNS locally, hosts file changes take effect immediately without flushing",
)
// flushDNSCachePlatform flushes the DNS cache on Linux.
//
// Commands:
// - resolvectl flush-caches: systemd-resolved 239+ (2018).
// Source: https://man7.org/linux/man-pages/man1/resolvectl.1.html
// - systemd-resolve --flush-caches: pre-239 name for the same operation.
//
// Tries resolvectl first, then falls back to systemd-resolve.
// Returns ErrNoResolver (wrapped in FlushError) if neither is available.
//
// Other resolvers (dnsmasq, unbound, nscd) are intentionally not supported here
// because flushing them reliably depends on per-site configuration (socket paths,
// service names, etc.) that we can't safely assume.
func flushDNSCachePlatform() error {
// Try resolvectl first (systemd 239+).
if _, err := exec.LookPath("resolvectl"); err == nil {
cmd := execCommandFunc("resolvectl", "flush-caches") // #nosec G204 -- hardcoded command, not user input
if err := cmd.Run(); err != nil {
return &FlushError{
Platform: runtime.GOOS,
Command: joinArgs("resolvectl", []string{"flush-caches"}),
Err: err,
}
}
return nil
}
// Fallback to systemd-resolve (older systemd).
if _, err := exec.LookPath("systemd-resolve"); err == nil {
cmd := execCommandFunc("systemd-resolve", "--flush-caches") // #nosec G204 -- hardcoded command, not user input
if err := cmd.Run(); err != nil {
return &FlushError{
Platform: runtime.GOOS,
Command: joinArgs("systemd-resolve", []string{"--flush-caches"}),
Err: err,
}
}
return nil
}
return &FlushError{
Platform: runtime.GOOS,
Command: "",
Err: ErrNoResolver,
}
}