Browse code

Merge pull request #33330 from coolljt0725/fix_sock_is_dir

Don't create source directory while the daemon is being shutdown, fix #30348

Akihiro Suda authored on 2017/06/07 12:37:08
Showing 6 changed files
... ...
@@ -154,6 +154,8 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
154 154
 	api := apiserver.New(serverConfig)
155 155
 	cli.api = api
156 156
 
157
+	var hosts []string
158
+
157 159
 	for i := 0; i < len(cli.Config.Hosts); i++ {
158 160
 		var err error
159 161
 		if cli.Config.Hosts[i], err = dopts.ParseHost(cli.Config.TLS, cli.Config.Hosts[i]); err != nil {
... ...
@@ -185,6 +187,7 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
185 185
 			}
186 186
 		}
187 187
 		logrus.Debugf("Listener created for HTTP on %s (%s)", proto, addr)
188
+		hosts = append(hosts, protoAddrParts[1])
188 189
 		api.Accept(addr, ls...)
189 190
 	}
190 191
 
... ...
@@ -212,6 +215,8 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
212 212
 		return fmt.Errorf("Error starting daemon: %v", err)
213 213
 	}
214 214
 
215
+	d.StoreHosts(hosts)
216
+
215 217
 	// validate after NewDaemon has restored enabled plugins. Dont change order.
216 218
 	if err := validateAuthzPlugins(cli.Config.AuthorizationPlugins, pluginStore); err != nil {
217 219
 		return fmt.Errorf("Error validating authorization plugin: %v", err)
... ...
@@ -116,6 +116,17 @@ type Daemon struct {
116 116
 
117 117
 	diskUsageRunning int32
118 118
 	pruneRunning     int32
119
+	hosts            map[string]bool // hosts stores the addresses the daemon is listening on
120
+}
121
+
122
+// StoreHosts stores the addresses the daemon is listening on
123
+func (daemon *Daemon) StoreHosts(hosts []string) {
124
+	if daemon.hosts == nil {
125
+		daemon.hosts = make(map[string]bool)
126
+	}
127
+	for _, h := range hosts {
128
+		daemon.hosts[h] = true
129
+	}
119 130
 }
120 131
 
121 132
 // HasExperimental returns whether the experimental features of the daemon are enabled or not
... ...
@@ -46,7 +46,8 @@ func (daemon *Daemon) StateChanged(id string, e libcontainerd.StateInfo) error {
46 46
 		c.StreamConfig.Wait()
47 47
 		c.Reset(false)
48 48
 
49
-		restart, wait, err := c.RestartManager().ShouldRestart(e.ExitCode, c.HasBeenManuallyStopped, time.Since(c.StartedAt))
49
+		// If daemon is being shutdown, don't let the container restart
50
+		restart, wait, err := c.RestartManager().ShouldRestart(e.ExitCode, daemon.IsShuttingDown() || c.HasBeenManuallyStopped, time.Since(c.StartedAt))
50 51
 		if err == nil && restart {
51 52
 			c.RestartCount++
52 53
 			c.SetRestarting(platformConstructExitStatus(e))
... ...
@@ -6,6 +6,7 @@ package daemon
6 6
 
7 7
 import (
8 8
 	"encoding/json"
9
+	"fmt"
9 10
 	"os"
10 11
 	"path/filepath"
11 12
 	"sort"
... ...
@@ -42,8 +43,19 @@ func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, er
42 42
 		if err := daemon.lazyInitializeVolume(c.ID, m); err != nil {
43 43
 			return nil, err
44 44
 		}
45
+		// If the daemon is being shutdown, we should not let a container start if it is trying to
46
+		// mount the socket the daemon is listening on. During daemon shutdown, the socket
47
+		// (/var/run/docker.sock by default) doesn't exist anymore causing the call to m.Setup to
48
+		// create at directory instead. This in turn will prevent the daemon to restart.
49
+		checkfunc := func(m *volume.MountPoint) error {
50
+			if _, exist := daemon.hosts[m.Source]; exist && daemon.IsShuttingDown() {
51
+				return fmt.Errorf("Could not mount %q to container while the daemon is shutting down", m.Source)
52
+			}
53
+			return nil
54
+		}
55
+
45 56
 		rootUID, rootGID := daemon.GetRemappedUIDGID()
46
-		path, err := m.Setup(c.MountLabel, rootUID, rootGID)
57
+		path, err := m.Setup(c.MountLabel, rootUID, rootGID, checkfunc)
47 58
 		if err != nil {
48 59
 			return nil, err
49 60
 		}
... ...
@@ -24,7 +24,7 @@ func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, er
24 24
 		if err := daemon.lazyInitializeVolume(c.ID, mount); err != nil {
25 25
 			return nil, err
26 26
 		}
27
-		s, err := mount.Setup(c.MountLabel, 0, 0)
27
+		s, err := mount.Setup(c.MountLabel, 0, 0, nil)
28 28
 		if err != nil {
29 29
 			return nil, err
30 30
 		}
... ...
@@ -149,7 +149,9 @@ func (m *MountPoint) Cleanup() error {
149 149
 
150 150
 // Setup sets up a mount point by either mounting the volume if it is
151 151
 // configured, or creating the source directory if supplied.
152
-func (m *MountPoint) Setup(mountLabel string, rootUID, rootGID int) (path string, err error) {
152
+// The, optional, checkFun parameter allows doing additional checking
153
+// before creating the source directory on the host.
154
+func (m *MountPoint) Setup(mountLabel string, rootUID, rootGID int, checkFun func(m *MountPoint) error) (path string, err error) {
153 155
 	defer func() {
154 156
 		if err == nil {
155 157
 			if label.RelabelNeeded(m.Mode) {
... ...
@@ -184,6 +186,14 @@ func (m *MountPoint) Setup(mountLabel string, rootUID, rootGID int) (path string
184 184
 
185 185
 	// system.MkdirAll() produces an error if m.Source exists and is a file (not a directory),
186 186
 	if m.Type == mounttypes.TypeBind {
187
+		// Before creating the source directory on the host, invoke checkFun if it's not nil. One of
188
+		// the use case is to forbid creating the daemon socket as a directory if the daemon is in
189
+		// the process of shutting down.
190
+		if checkFun != nil {
191
+			if err := checkFun(m); err != nil {
192
+				return "", err
193
+			}
194
+		}
187 195
 		// idtools.MkdirAllNewAs() produces an error if m.Source exists and is a file (not a directory)
188 196
 		// also, makes sure that if the directory is created, the correct remapped rootUID/rootGID will own it
189 197
 		if err := idtools.MkdirAllNewAs(m.Source, 0755, rootUID, rootGID); err != nil {