2017-11-01 19:37:53 -04:00
|
|
|
// +build !windows
|
2015-05-15 19:34:26 -04:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-06-15 09:36:19 -04:00
|
|
|
"fmt"
|
2016-04-09 02:49:33 -04:00
|
|
|
"net"
|
2015-05-15 19:34:26 -04:00
|
|
|
"os"
|
2015-12-10 18:35:10 -05:00
|
|
|
"os/signal"
|
2016-03-24 14:42:03 -04:00
|
|
|
"path/filepath"
|
2016-04-09 02:49:33 -04:00
|
|
|
"strconv"
|
2015-05-15 19:34:26 -04:00
|
|
|
|
2018-08-17 16:08:22 -04:00
|
|
|
"github.com/containerd/containerd/runtime/v1/linux"
|
2016-04-12 11:21:20 -04:00
|
|
|
"github.com/docker/docker/cmd/dockerd/hack"
|
2015-05-15 19:34:26 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
2018-09-07 16:43:21 -04:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2018-05-23 15:15:21 -04:00
|
|
|
"github.com/docker/docker/libcontainerd/supervisor"
|
2016-04-09 02:49:33 -04:00
|
|
|
"github.com/docker/libnetwork/portallocator"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2015-05-15 19:34:26 -04:00
|
|
|
)
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
const defaultDaemonConfigFile = "/etc/docker/daemon.json"
|
|
|
|
|
2015-06-15 09:36:19 -04:00
|
|
|
// setDefaultUmask sets the umask to 0022 to avoid problems
|
|
|
|
// caused by custom umask
|
|
|
|
func setDefaultUmask() error {
|
|
|
|
desiredUmask := 0022
|
2017-05-23 10:22:32 -04:00
|
|
|
unix.Umask(desiredUmask)
|
|
|
|
if umask := unix.Umask(desiredUmask); umask != desiredUmask {
|
2015-06-15 09:36:19 -04:00
|
|
|
return fmt.Errorf("failed to set umask: expected %#o, got %#o", desiredUmask, umask)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-30 15:39:19 -04:00
|
|
|
|
2016-11-04 15:42:21 -04:00
|
|
|
func getDaemonConfDir(_ string) string {
|
2015-07-30 15:39:19 -04:00
|
|
|
return "/etc/docker"
|
|
|
|
}
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2018-05-23 15:15:21 -04:00
|
|
|
func (cli *DaemonCli) getPlatformContainerdDaemonOpts() ([]supervisor.DaemonOpt, error) {
|
|
|
|
opts := []supervisor.DaemonOpt{
|
|
|
|
supervisor.WithOOMScore(cli.Config.OOMScoreAdjust),
|
|
|
|
supervisor.WithPlugin("linux", &linux.Config{
|
2017-12-04 14:17:30 -05:00
|
|
|
Shim: daemon.DefaultShimBinary,
|
|
|
|
Runtime: daemon.DefaultRuntimeBinary,
|
|
|
|
RuntimeRoot: filepath.Join(cli.Config.Root, "runc"),
|
|
|
|
ShimDebug: cli.Config.Debug,
|
2017-09-22 09:52:41 -04:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts, nil
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
2016-03-24 14:42:03 -04:00
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
// setupConfigReloadTrap configures the USR2 signal to reload the configuration.
|
|
|
|
func (cli *DaemonCli) setupConfigReloadTrap() {
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, unix.SIGHUP)
|
|
|
|
go func() {
|
|
|
|
for range c {
|
|
|
|
cli.reloadConfig()
|
|
|
|
}
|
|
|
|
}()
|
2016-03-24 14:42:03 -04:00
|
|
|
}
|
2016-04-09 02:49:33 -04:00
|
|
|
|
2016-08-19 16:06:28 -04:00
|
|
|
// getSwarmRunRoot gets the root directory for swarm to store runtime state
|
|
|
|
// For example, the control socket
|
|
|
|
func (cli *DaemonCli) getSwarmRunRoot() string {
|
|
|
|
return filepath.Join(cli.Config.ExecRoot, "swarm")
|
|
|
|
}
|
|
|
|
|
2016-04-09 02:49:33 -04:00
|
|
|
// allocateDaemonPort ensures that there are no containers
|
|
|
|
// that try to use any port allocated for the docker server.
|
|
|
|
func allocateDaemonPort(addr string) error {
|
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
intPort, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var hostIPs []net.IP
|
|
|
|
if parsedIP := net.ParseIP(host); parsedIP != nil {
|
|
|
|
hostIPs = append(hostIPs, parsedIP)
|
|
|
|
} else if hostIPs, err = net.LookupIP(host); err != nil {
|
|
|
|
return fmt.Errorf("failed to lookup %s address in host specification", host)
|
|
|
|
}
|
|
|
|
|
|
|
|
pa := portallocator.Get()
|
|
|
|
for _, hostIP := range hostIPs {
|
|
|
|
if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
|
|
|
|
return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-22 20:16:14 -04:00
|
|
|
|
2016-04-12 11:21:20 -04:00
|
|
|
func wrapListeners(proto string, ls []net.Listener) []net.Listener {
|
2016-05-21 23:40:15 -04:00
|
|
|
switch proto {
|
|
|
|
case "unix":
|
2018-05-17 10:29:52 -04:00
|
|
|
ls[0] = &hack.MalformedHostHeaderOverride{Listener: ls[0]}
|
2016-05-21 23:40:15 -04:00
|
|
|
case "fd":
|
|
|
|
for i := range ls {
|
2018-05-17 10:29:52 -04:00
|
|
|
ls[i] = &hack.MalformedHostHeaderOverride{Listener: ls[i]}
|
2016-04-12 11:21:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ls
|
|
|
|
}
|
2018-09-07 16:43:21 -04:00
|
|
|
|
|
|
|
func newCgroupParent(config *config.Config) string {
|
|
|
|
cgroupParent := "docker"
|
|
|
|
useSystemd := daemon.UsingSystemd(config)
|
|
|
|
if useSystemd {
|
|
|
|
cgroupParent = "system.slice"
|
|
|
|
}
|
|
|
|
if config.CgroupParent != "" {
|
|
|
|
cgroupParent = config.CgroupParent
|
|
|
|
}
|
|
|
|
if useSystemd {
|
|
|
|
cgroupParent = cgroupParent + ":" + "docker" + ":"
|
|
|
|
}
|
|
|
|
return cgroupParent
|
|
|
|
}
|