github.com/go/docker@v1.12.0-rc2/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  	}
    65  	if cli.Config.ContainerdAddr != "" {
    66  		opts = append(opts, libcontainerd.WithRemoteAddr(cli.Config.ContainerdAddr))
    67  	} else {
    68  		opts = append(opts, libcontainerd.WithStartDaemon(true))
    69  	}
    70  	if daemon.UsingSystemd(cli.Config) {
    71  		args := []string{"--systemd-cgroup=true"}
    72  		opts = append(opts, libcontainerd.WithRuntimeArgs(args))
    73  	}
    74  	if cli.Config.LiveRestore {
    75  		opts = append(opts, libcontainerd.WithLiveRestore(true))
    76  	}
    77  	opts = append(opts, libcontainerd.WithRuntimePath(daemon.DefaultRuntimeBinary))
    78  	return opts
    79  }
    80  
    81  // getLibcontainerdRoot gets the root directory for libcontainerd/containerd to
    82  // store their state.
    83  func (cli *DaemonCli) getLibcontainerdRoot() string {
    84  	return filepath.Join(cli.Config.ExecRoot, "libcontainerd")
    85  }
    86  
    87  // allocateDaemonPort ensures that there are no containers
    88  // that try to use any port allocated for the docker server.
    89  func allocateDaemonPort(addr string) error {
    90  	host, port, err := net.SplitHostPort(addr)
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	intPort, err := strconv.Atoi(port)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	var hostIPs []net.IP
   101  	if parsedIP := net.ParseIP(host); parsedIP != nil {
   102  		hostIPs = append(hostIPs, parsedIP)
   103  	} else if hostIPs, err = net.LookupIP(host); err != nil {
   104  		return fmt.Errorf("failed to lookup %s address in host specification", host)
   105  	}
   106  
   107  	pa := portallocator.Get()
   108  	for _, hostIP := range hostIPs {
   109  		if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
   110  			return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
   111  		}
   112  	}
   113  	return nil
   114  }
   115  
   116  // notifyShutdown is called after the daemon shuts down but before the process exits.
   117  func notifyShutdown(err error) {
   118  }
   119  
   120  func wrapListeners(proto string, ls []net.Listener) []net.Listener {
   121  	switch proto {
   122  	case "unix":
   123  		ls[0] = &hack.MalformedHostHeaderOverride{ls[0]}
   124  	case "fd":
   125  		for i := range ls {
   126  			ls[i] = &hack.MalformedHostHeaderOverride{ls[i]}
   127  		}
   128  	}
   129  	return ls
   130  }