2016-03-25 19:38:00 -04:00
|
|
|
// +build !windows,!solaris
|
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
|
|
|
|
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"
|
2016-03-18 14:50:19 -04:00
|
|
|
"github.com/docker/docker/libcontainerd"
|
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
|
|
|
|
|
|
|
// setupConfigReloadTrap configures the USR2 signal to reload the configuration.
|
2016-04-22 20:16:14 -04:00
|
|
|
func (cli *DaemonCli) setupConfigReloadTrap() {
|
2015-12-10 18:35:10 -05:00
|
|
|
c := make(chan os.Signal, 1)
|
2017-05-23 10:22:32 -04:00
|
|
|
signal.Notify(c, unix.SIGHUP)
|
2015-12-10 18:35:10 -05:00
|
|
|
go func() {
|
|
|
|
for range c {
|
2016-04-22 20:16:14 -04:00
|
|
|
cli.reloadConfig()
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
|
|
|
|
func (cli *DaemonCli) getPlatformRemoteOptions() []libcontainerd.RemoteOption {
|
|
|
|
opts := []libcontainerd.RemoteOption{
|
|
|
|
libcontainerd.WithDebugLog(cli.Config.Debug),
|
2016-07-11 18:26:23 -04:00
|
|
|
libcontainerd.WithOOMScore(cli.Config.OOMScoreAdjust),
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
if cli.Config.ContainerdAddr != "" {
|
|
|
|
opts = append(opts, libcontainerd.WithRemoteAddr(cli.Config.ContainerdAddr))
|
|
|
|
} else {
|
|
|
|
opts = append(opts, libcontainerd.WithStartDaemon(true))
|
|
|
|
}
|
2016-03-24 12:18:03 -04:00
|
|
|
if daemon.UsingSystemd(cli.Config) {
|
|
|
|
args := []string{"--systemd-cgroup=true"}
|
|
|
|
opts = append(opts, libcontainerd.WithRuntimeArgs(args))
|
|
|
|
}
|
2016-07-27 11:30:15 -04:00
|
|
|
if cli.Config.LiveRestoreEnabled {
|
2016-06-02 14:10:55 -04:00
|
|
|
opts = append(opts, libcontainerd.WithLiveRestore(true))
|
|
|
|
}
|
2016-05-23 17:49:50 -04:00
|
|
|
opts = append(opts, libcontainerd.WithRuntimePath(daemon.DefaultRuntimeBinary))
|
2016-03-18 14:50:19 -04:00
|
|
|
return opts
|
|
|
|
}
|
2016-03-24 14:42:03 -04:00
|
|
|
|
|
|
|
// getLibcontainerdRoot gets the root directory for libcontainerd/containerd to
|
|
|
|
// store their state.
|
|
|
|
func (cli *DaemonCli) getLibcontainerdRoot() string {
|
|
|
|
return filepath.Join(cli.Config.ExecRoot, "libcontainerd")
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// notifyShutdown is called after the daemon shuts down but before the process exits.
|
|
|
|
func notifyShutdown(err error) {
|
|
|
|
}
|
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":
|
|
|
|
ls[0] = &hack.MalformedHostHeaderOverride{ls[0]}
|
|
|
|
case "fd":
|
|
|
|
for i := range ls {
|
|
|
|
ls[i] = &hack.MalformedHostHeaderOverride{ls[i]}
|
2016-04-12 11:21:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ls
|
|
|
|
}
|