github.com/skanehira/moby@v17.12.1-ce-rc2+incompatible/cmd/dockerd/daemon_unix.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"net"
     8  	"os"
     9  	"os/signal"
    10  	"path/filepath"
    11  	"strconv"
    12  
    13  	"github.com/containerd/containerd/linux"
    14  	"github.com/docker/docker/cmd/dockerd/hack"
    15  	"github.com/docker/docker/daemon"
    16  	"github.com/docker/docker/libcontainerd"
    17  	"github.com/docker/libnetwork/portallocator"
    18  	"golang.org/x/sys/unix"
    19  )
    20  
    21  const defaultDaemonConfigFile = "/etc/docker/daemon.json"
    22  
    23  // setDefaultUmask sets the umask to 0022 to avoid problems
    24  // caused by custom umask
    25  func setDefaultUmask() error {
    26  	desiredUmask := 0022
    27  	unix.Umask(desiredUmask)
    28  	if umask := unix.Umask(desiredUmask); umask != desiredUmask {
    29  		return fmt.Errorf("failed to set umask: expected %#o, got %#o", desiredUmask, umask)
    30  	}
    31  
    32  	return nil
    33  }
    34  
    35  func getDaemonConfDir(_ string) string {
    36  	return "/etc/docker"
    37  }
    38  
    39  func (cli *DaemonCli) getPlatformRemoteOptions() ([]libcontainerd.RemoteOption, error) {
    40  	opts := []libcontainerd.RemoteOption{
    41  		libcontainerd.WithOOMScore(cli.Config.OOMScoreAdjust),
    42  		libcontainerd.WithPlugin("linux", &linux.Config{
    43  			Shim:        daemon.DefaultShimBinary,
    44  			Runtime:     daemon.DefaultRuntimeBinary,
    45  			RuntimeRoot: filepath.Join(cli.Config.Root, "runc"),
    46  			ShimDebug:   cli.Config.Debug,
    47  		}),
    48  	}
    49  	if cli.Config.Debug {
    50  		opts = append(opts, libcontainerd.WithLogLevel("debug"))
    51  	}
    52  	if cli.Config.ContainerdAddr != "" {
    53  		opts = append(opts, libcontainerd.WithRemoteAddr(cli.Config.ContainerdAddr))
    54  	} else {
    55  		opts = append(opts, libcontainerd.WithStartDaemon(true))
    56  	}
    57  
    58  	return opts, nil
    59  }
    60  
    61  // setupConfigReloadTrap configures the USR2 signal to reload the configuration.
    62  func (cli *DaemonCli) setupConfigReloadTrap() {
    63  	c := make(chan os.Signal, 1)
    64  	signal.Notify(c, unix.SIGHUP)
    65  	go func() {
    66  		for range c {
    67  			cli.reloadConfig()
    68  		}
    69  	}()
    70  }
    71  
    72  // getSwarmRunRoot gets the root directory for swarm to store runtime state
    73  // For example, the control socket
    74  func (cli *DaemonCli) getSwarmRunRoot() string {
    75  	return filepath.Join(cli.Config.ExecRoot, "swarm")
    76  }
    77  
    78  // allocateDaemonPort ensures that there are no containers
    79  // that try to use any port allocated for the docker server.
    80  func allocateDaemonPort(addr string) error {
    81  	host, port, err := net.SplitHostPort(addr)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	intPort, err := strconv.Atoi(port)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	var hostIPs []net.IP
    92  	if parsedIP := net.ParseIP(host); parsedIP != nil {
    93  		hostIPs = append(hostIPs, parsedIP)
    94  	} else if hostIPs, err = net.LookupIP(host); err != nil {
    95  		return fmt.Errorf("failed to lookup %s address in host specification", host)
    96  	}
    97  
    98  	pa := portallocator.Get()
    99  	for _, hostIP := range hostIPs {
   100  		if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
   101  			return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
   102  		}
   103  	}
   104  	return nil
   105  }
   106  
   107  // notifyShutdown is called after the daemon shuts down but before the process exits.
   108  func notifyShutdown(err error) {
   109  }
   110  
   111  func wrapListeners(proto string, ls []net.Listener) []net.Listener {
   112  	switch proto {
   113  	case "unix":
   114  		ls[0] = &hack.MalformedHostHeaderOverride{ls[0]}
   115  	case "fd":
   116  		for i := range ls {
   117  			ls[i] = &hack.MalformedHostHeaderOverride{ls[i]}
   118  		}
   119  	}
   120  	return ls
   121  }