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