github.com/portworx/docker@v1.12.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 {
    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.LiveRestore {
    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  // allocateDaemonPort ensures that there are no containers
    89  // that try to use any port allocated for the docker server.
    90  func allocateDaemonPort(addr string) error {
    91  	host, port, err := net.SplitHostPort(addr)
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	intPort, err := strconv.Atoi(port)
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	var hostIPs []net.IP
   102  	if parsedIP := net.ParseIP(host); parsedIP != nil {
   103  		hostIPs = append(hostIPs, parsedIP)
   104  	} else if hostIPs, err = net.LookupIP(host); err != nil {
   105  		return fmt.Errorf("failed to lookup %s address in host specification", host)
   106  	}
   107  
   108  	pa := portallocator.Get()
   109  	for _, hostIP := range hostIPs {
   110  		if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
   111  			return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
   112  		}
   113  	}
   114  	return nil
   115  }
   116  
   117  // notifyShutdown is called after the daemon shuts down but before the process exits.
   118  func notifyShutdown(err error) {
   119  }
   120  
   121  func wrapListeners(proto string, ls []net.Listener) []net.Listener {
   122  	switch proto {
   123  	case "unix":
   124  		ls[0] = &hack.MalformedHostHeaderOverride{ls[0]}
   125  	case "fd":
   126  		for i := range ls {
   127  			ls[i] = &hack.MalformedHostHeaderOverride{ls[i]}
   128  		}
   129  	}
   130  	return ls
   131  }