github.com/olljanat/moby@v1.13.1/cmd/dockerd/daemon_unix.go (about)

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