Skip to content

Commit 20461b8

Browse files
authored
Merge pull request #2248 from AkihiroSuda/propagte-exec-root
allow propagating custom exec-root (e.g. "/run/docker") to libnetwork-setkey
2 parents 36d3bed + 03bbfad commit 20461b8

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type DaemonCfg struct {
3535
Debug bool
3636
Experimental bool
3737
DataDir string
38+
ExecRoot string
3839
DefaultNetwork string
3940
DefaultDriver string
4041
Labels []string
@@ -217,6 +218,7 @@ func OptionDataDir(dataDir string) Option {
217218
// OptionExecRoot function returns an option setter for exec root folder
218219
func OptionExecRoot(execRoot string) Option {
219220
return func(c *Config) {
221+
c.Daemon.ExecRoot = execRoot
220222
osl.SetBasePath(execRoot)
221223
}
222224
}

sandbox_externalkey_unix.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,30 @@ package libnetwork
44

55
import (
66
"encoding/json"
7+
"flag"
78
"fmt"
89
"io"
910
"io/ioutil"
1011
"net"
1112
"os"
13+
"path/filepath"
1214

1315
"github.com/docker/libnetwork/types"
1416
"github.com/opencontainers/runc/libcontainer/configs"
1517
"github.com/sirupsen/logrus"
1618
)
1719

18-
const udsBase = "/run/docker/libnetwork/"
19-
const success = "success"
20+
const (
21+
execSubdir = "libnetwork"
22+
defaultExecRoot = "/run/docker"
23+
success = "success"
24+
)
2025

2126
// processSetKeyReexec is a private function that must be called only on an reexec path
2227
// It expects 3 args { [0] = "libnetwork-setkey", [1] = <container-id>, [2] = <controller-id> }
2328
// It also expects configs.HookState as a json string in <stdin>
2429
// Refer to https://github.com/opencontainers/runc/pull/160/ for more information
30+
// The docker exec-root can be specified as "-exec-root" flag. The default value is "/run/docker".
2531
func processSetKeyReexec() {
2632
var err error
2733

@@ -32,12 +38,17 @@ func processSetKeyReexec() {
3238
}
3339
}()
3440

35-
// expecting 3 args {[0]="libnetwork-setkey", [1]=<container-id>, [2]=<controller-id> }
36-
if len(os.Args) < 3 {
37-
err = fmt.Errorf("Re-exec expects 3 args, received : %d", len(os.Args))
41+
execRoot := flag.String("exec-root", defaultExecRoot, "docker exec root")
42+
flag.Parse()
43+
44+
// expecting 3 os.Args {[0]="libnetwork-setkey", [1]=<container-id>, [2]=<controller-id> }
45+
// (i.e. expecting 2 flag.Args())
46+
args := flag.Args()
47+
if len(args) < 2 {
48+
err = fmt.Errorf("Re-exec expects 2 args (after parsing flags), received : %d", len(args))
3849
return
3950
}
40-
containerID := os.Args[1]
51+
containerID, controllerID := args[0], args[1]
4152

4253
// We expect configs.HookState as a json string in <stdin>
4354
stateBuf, err := ioutil.ReadAll(os.Stdin)
@@ -49,18 +60,17 @@ func processSetKeyReexec() {
4960
return
5061
}
5162

52-
controllerID := os.Args[2]
53-
54-
err = SetExternalKey(controllerID, containerID, fmt.Sprintf("/proc/%d/ns/net", state.Pid))
63+
err = SetExternalKey(controllerID, containerID, fmt.Sprintf("/proc/%d/ns/net", state.Pid), *execRoot)
5564
}
5665

5766
// SetExternalKey provides a convenient way to set an External key to a sandbox
58-
func SetExternalKey(controllerID string, containerID string, key string) error {
67+
func SetExternalKey(controllerID string, containerID string, key string, execRoot string) error {
5968
keyData := setKeyData{
6069
ContainerID: containerID,
6170
Key: key}
6271

63-
c, err := net.Dial("unix", udsBase+controllerID+".sock")
72+
uds := filepath.Join(execRoot, execSubdir, controllerID+".sock")
73+
c, err := net.Dial("unix", uds)
6474
if err != nil {
6575
return err
6676
}
@@ -102,10 +112,15 @@ func processReturn(r io.Reader) error {
102112
}
103113

104114
func (c *controller) startExternalKeyListener() error {
115+
execRoot := defaultExecRoot
116+
if v := c.Config().Daemon.ExecRoot; v != "" {
117+
execRoot = v
118+
}
119+
udsBase := filepath.Join(execRoot, execSubdir)
105120
if err := os.MkdirAll(udsBase, 0600); err != nil {
106121
return err
107122
}
108-
uds := udsBase + c.id + ".sock"
123+
uds := filepath.Join(udsBase, c.id+".sock")
109124
l, err := net.Listen("unix", uds)
110125
if err != nil {
111126
return err

0 commit comments

Comments
 (0)