github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/libnetwork/internal/resolvconf/resolvconf_path.go (about)

     1  //go:build go1.21
     2  
     3  package resolvconf
     4  
     5  import (
     6  	"context"
     7  	"net/netip"
     8  	"sync"
     9  
    10  	"github.com/containerd/log"
    11  )
    12  
    13  const (
    14  	// defaultPath is the default path to the resolv.conf that contains information to resolve DNS. See Path().
    15  	defaultPath = "/etc/resolv.conf"
    16  	// alternatePath is a path different from defaultPath, that may be used to resolve DNS. See Path().
    17  	alternatePath = "/run/systemd/resolve/resolv.conf"
    18  )
    19  
    20  // For Path to detect systemd (only needed for legacy networking).
    21  var (
    22  	detectSystemdResolvConfOnce sync.Once
    23  	pathAfterSystemdDetection   = defaultPath
    24  )
    25  
    26  // Path returns the path to the resolv.conf file that libnetwork should use.
    27  //
    28  // When /etc/resolv.conf contains 127.0.0.53 as the only nameserver, then
    29  // it is assumed systemd-resolved manages DNS. Because inside the container 127.0.0.53
    30  // is not a valid DNS server, Path() returns /run/systemd/resolve/resolv.conf
    31  // which is the resolv.conf that systemd-resolved generates and manages.
    32  // Otherwise Path() returns /etc/resolv.conf.
    33  //
    34  // Errors are silenced as they will inevitably resurface at future open/read calls.
    35  //
    36  // More information at https://www.freedesktop.org/software/systemd/man/systemd-resolved.service.html#/etc/resolv.conf
    37  //
    38  // TODO(robmry) - alternatePath is only needed for legacy networking ...
    39  //
    40  //	Host networking can use the host's resolv.conf as-is, and with an internal
    41  //	resolver it's also possible to use nameservers on the host's loopback
    42  //	interface. Once legacy networking is removed, this can always return
    43  //	defaultPath.
    44  func Path() string {
    45  	detectSystemdResolvConfOnce.Do(func() {
    46  		rc, err := Load(defaultPath)
    47  		if err != nil {
    48  			// silencing error as it will resurface at next calls trying to read defaultPath
    49  			return
    50  		}
    51  		ns := rc.nameServers
    52  		if len(ns) == 1 && ns[0] == netip.MustParseAddr("127.0.0.53") {
    53  			pathAfterSystemdDetection = alternatePath
    54  			log.G(context.TODO()).Infof("detected 127.0.0.53 nameserver, assuming systemd-resolved, so using resolv.conf: %s", alternatePath)
    55  		}
    56  	})
    57  	return pathAfterSystemdDetection
    58  }