github.com/devdivbcp/moby@v17.12.0-ce-rc1.0.20200726071732-2d4bfdc789ad+incompatible/cmd/dockerd/daemon_unix.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"net"
     9  	"os"
    10  	"os/signal"
    11  	"path/filepath"
    12  	"strconv"
    13  	"time"
    14  
    15  	"github.com/containerd/containerd/runtime/v1/linux"
    16  	"github.com/docker/docker/cmd/dockerd/hack"
    17  	"github.com/docker/docker/daemon"
    18  	"github.com/docker/docker/daemon/config"
    19  	"github.com/docker/docker/libcontainerd/supervisor"
    20  	"github.com/docker/docker/pkg/homedir"
    21  	"github.com/docker/libnetwork/portallocator"
    22  	"github.com/pkg/errors"
    23  	"github.com/sirupsen/logrus"
    24  	"golang.org/x/sys/unix"
    25  )
    26  
    27  func getDefaultDaemonConfigDir() (string, error) {
    28  	if !honorXDG {
    29  		return "/etc/docker", nil
    30  	}
    31  	// NOTE: CLI uses ~/.docker while the daemon uses ~/.config/docker, because
    32  	// ~/.docker was not designed to store daemon configurations.
    33  	// In future, the daemon directory may be renamed to ~/.config/moby-engine (?).
    34  	configHome, err := homedir.GetConfigHome()
    35  	if err != nil {
    36  		return "", nil
    37  	}
    38  	return filepath.Join(configHome, "docker"), nil
    39  }
    40  
    41  func getDefaultDaemonConfigFile() (string, error) {
    42  	dir, err := getDefaultDaemonConfigDir()
    43  	if err != nil {
    44  		return "", err
    45  	}
    46  	return filepath.Join(dir, "daemon.json"), nil
    47  }
    48  
    49  // setDefaultUmask sets the umask to 0022 to avoid problems
    50  // caused by custom umask
    51  func setDefaultUmask() error {
    52  	desiredUmask := 0022
    53  	unix.Umask(desiredUmask)
    54  	if umask := unix.Umask(desiredUmask); umask != desiredUmask {
    55  		return fmt.Errorf("failed to set umask: expected %#o, got %#o", desiredUmask, umask)
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  func getDaemonConfDir(_ string) (string, error) {
    62  	return getDefaultDaemonConfigDir()
    63  }
    64  
    65  func (cli *DaemonCli) getPlatformContainerdDaemonOpts() ([]supervisor.DaemonOpt, error) {
    66  	opts := []supervisor.DaemonOpt{
    67  		supervisor.WithOOMScore(cli.Config.OOMScoreAdjust),
    68  		supervisor.WithPlugin("linux", &linux.Config{
    69  			Shim:        daemon.DefaultShimBinary,
    70  			Runtime:     daemon.DefaultRuntimeBinary,
    71  			RuntimeRoot: filepath.Join(cli.Config.Root, "runc"),
    72  			ShimDebug:   cli.Config.Debug,
    73  		}),
    74  	}
    75  
    76  	return opts, nil
    77  }
    78  
    79  // setupConfigReloadTrap configures the USR2 signal to reload the configuration.
    80  func (cli *DaemonCli) setupConfigReloadTrap() {
    81  	c := make(chan os.Signal, 1)
    82  	signal.Notify(c, unix.SIGHUP)
    83  	go func() {
    84  		for range c {
    85  			cli.reloadConfig()
    86  		}
    87  	}()
    88  }
    89  
    90  // getSwarmRunRoot gets the root directory for swarm to store runtime state
    91  // For example, the control socket
    92  func (cli *DaemonCli) getSwarmRunRoot() string {
    93  	return filepath.Join(cli.Config.ExecRoot, "swarm")
    94  }
    95  
    96  // allocateDaemonPort ensures that there are no containers
    97  // that try to use any port allocated for the docker server.
    98  func allocateDaemonPort(addr string) error {
    99  	host, port, err := net.SplitHostPort(addr)
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	intPort, err := strconv.Atoi(port)
   105  	if err != nil {
   106  		return err
   107  	}
   108  
   109  	var hostIPs []net.IP
   110  	if parsedIP := net.ParseIP(host); parsedIP != nil {
   111  		hostIPs = append(hostIPs, parsedIP)
   112  	} else if hostIPs, err = net.LookupIP(host); err != nil {
   113  		return fmt.Errorf("failed to lookup %s address in host specification", host)
   114  	}
   115  
   116  	pa := portallocator.Get()
   117  	for _, hostIP := range hostIPs {
   118  		if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
   119  			return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
   120  		}
   121  	}
   122  	return nil
   123  }
   124  
   125  func wrapListeners(proto string, ls []net.Listener) []net.Listener {
   126  	switch proto {
   127  	case "unix":
   128  		ls[0] = &hack.MalformedHostHeaderOverride{Listener: ls[0]}
   129  	case "fd":
   130  		for i := range ls {
   131  			ls[i] = &hack.MalformedHostHeaderOverride{Listener: ls[i]}
   132  		}
   133  	}
   134  	return ls
   135  }
   136  
   137  func newCgroupParent(config *config.Config) string {
   138  	cgroupParent := "docker"
   139  	useSystemd := daemon.UsingSystemd(config)
   140  	if useSystemd {
   141  		cgroupParent = "system.slice"
   142  	}
   143  	if config.CgroupParent != "" {
   144  		cgroupParent = config.CgroupParent
   145  	}
   146  	if useSystemd {
   147  		cgroupParent = cgroupParent + ":" + "docker" + ":"
   148  	}
   149  	return cgroupParent
   150  }
   151  
   152  func (cli *DaemonCli) initContainerD(ctx context.Context) (func(time.Duration) error, error) {
   153  	var waitForShutdown func(time.Duration) error
   154  	if cli.Config.ContainerdAddr == "" {
   155  		systemContainerdAddr, ok, err := systemContainerdRunning(honorXDG)
   156  		if err != nil {
   157  			return nil, errors.Wrap(err, "could not determine whether the system containerd is running")
   158  		}
   159  		if !ok {
   160  			logrus.Debug("Containerd not running, starting daemon managed containerd")
   161  			opts, err := cli.getContainerdDaemonOpts()
   162  			if err != nil {
   163  				return nil, errors.Wrap(err, "failed to generate containerd options")
   164  			}
   165  
   166  			r, err := supervisor.Start(ctx, filepath.Join(cli.Config.Root, "containerd"), filepath.Join(cli.Config.ExecRoot, "containerd"), opts...)
   167  			if err != nil {
   168  				return nil, errors.Wrap(err, "failed to start containerd")
   169  			}
   170  			logrus.Debug("Started daemon managed containerd")
   171  			cli.Config.ContainerdAddr = r.Address()
   172  
   173  			// Try to wait for containerd to shutdown
   174  			waitForShutdown = r.WaitTimeout
   175  		} else {
   176  			cli.Config.ContainerdAddr = systemContainerdAddr
   177  		}
   178  	}
   179  
   180  	return waitForShutdown, nil
   181  }