integration-cli/docker_cli_exec_test.go
ecdbc1a0
 // +build !test_no_exec
 
42dafe4b
 package main
 
 import (
 	"bufio"
9462dbb2
 	"fmt"
72b75cd4
 	"net/http"
16562406
 	"os"
42dafe4b
 	"os/exec"
43d1c201
 	"path/filepath"
9462dbb2
 	"reflect"
 	"sort"
42dafe4b
 	"strings"
9462dbb2
 	"sync"
42dafe4b
 	"time"
dc944ea7
 
9077c896
 	"github.com/docker/docker/pkg/integration/checker"
dc944ea7
 	"github.com/go-check/check"
42dafe4b
 )
 
dc944ea7
 func (s *DockerSuite) TestExec(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && top")
70407ce4
 
668e2369
 	out, _ := dockerCmd(c, "exec", "testing", "cat", "/tmp/file")
42dafe4b
 	out = strings.Trim(out, "\r\n")
668e2369
 	if out != "test" {
 		c.Errorf("container exec should've printed test but printed %q", out)
42dafe4b
 	}
 
 }
 
dc944ea7
 func (s *DockerSuite) TestExecInteractive(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && top")
42dafe4b
 
 	execCmd := exec.Command(dockerBinary, "exec", "-i", "testing", "sh")
 	stdin, err := execCmd.StdinPipe()
 	if err != nil {
dc944ea7
 		c.Fatal(err)
42dafe4b
 	}
 	stdout, err := execCmd.StdoutPipe()
 	if err != nil {
dc944ea7
 		c.Fatal(err)
42dafe4b
 	}
 
 	if err := execCmd.Start(); err != nil {
dc944ea7
 		c.Fatal(err)
42dafe4b
 	}
 	if _, err := stdin.Write([]byte("cat /tmp/file\n")); err != nil {
dc944ea7
 		c.Fatal(err)
42dafe4b
 	}
 
 	r := bufio.NewReader(stdout)
 	line, err := r.ReadString('\n')
 	if err != nil {
dc944ea7
 		c.Fatal(err)
42dafe4b
 	}
 	line = strings.TrimSpace(line)
 	if line != "test" {
dc944ea7
 		c.Fatalf("Output should be 'test', got '%q'", line)
42dafe4b
 	}
 	if err := stdin.Close(); err != nil {
dc944ea7
 		c.Fatal(err)
42dafe4b
 	}
4203230c
 	errChan := make(chan error)
42dafe4b
 	go func() {
4203230c
 		errChan <- execCmd.Wait()
 		close(errChan)
42dafe4b
 	}()
 	select {
4203230c
 	case err := <-errChan:
 		c.Assert(err, check.IsNil)
42dafe4b
 	case <-time.After(1 * time.Second):
dc944ea7
 		c.Fatal("docker exec failed to exit on stdin close")
42dafe4b
 	}
 
 }
 
dc944ea7
 func (s *DockerSuite) TestExecAfterContainerRestart(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
668e2369
 	dockerCmd(c, "restart", cleanedContainerID)
42dafe4b
 
668e2369
 	out, _ = dockerCmd(c, "exec", cleanedContainerID, "echo", "hello")
42dafe4b
 	outStr := strings.TrimSpace(out)
 	if outStr != "hello" {
dc944ea7
 		c.Errorf("container should've printed hello, instead printed %q", outStr)
42dafe4b
 	}
 }
 
57464c32
 func (s *DockerDaemonSuite) TestExecAfterDaemonRestart(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
dc944ea7
 	testRequires(c, SameHostDaemon)
70407ce4
 
57464c32
 	if err := s.d.StartWithBusybox(); err != nil {
dc944ea7
 		c.Fatalf("Could not start daemon with busybox: %v", err)
42dafe4b
 	}
 
57464c32
 	if out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
dc944ea7
 		c.Fatalf("Could not run top: err=%v\n%s", err, out)
42dafe4b
 	}
 
57464c32
 	if err := s.d.Restart(); err != nil {
dc944ea7
 		c.Fatalf("Could not restart daemon: %v", err)
42dafe4b
 	}
 
57464c32
 	if out, err := s.d.Cmd("start", "top"); err != nil {
dc944ea7
 		c.Fatalf("Could not start top after daemon restart: err=%v\n%s", err, out)
42dafe4b
 	}
 
57464c32
 	out, err := s.d.Cmd("exec", "top", "echo", "hello")
42dafe4b
 	if err != nil {
dc944ea7
 		c.Fatalf("Could not exec on container top: err=%v\n%s", err, out)
42dafe4b
 	}
 
 	outStr := strings.TrimSpace(string(out))
 	if outStr != "hello" {
dc944ea7
 		c.Errorf("container should've printed hello, instead printed %q", outStr)
42dafe4b
 	}
 }
2bceaae4
 
906974b1
 // Regression test for #9155, #9044
dc944ea7
 func (s *DockerSuite) TestExecEnv(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	dockerCmd(c, "run", "-e", "LALA=value1", "-e", "LALA=value2",
2bceaae4
 		"-d", "--name", "testing", "busybox", "top")
 
668e2369
 	out, _ := dockerCmd(c, "exec", "testing", "env")
2bceaae4
 	if strings.Contains(out, "LALA=value1") ||
 		!strings.Contains(out, "LALA=value2") ||
 		!strings.Contains(out, "HOME=/root") {
dc944ea7
 		c.Errorf("exec env(%q), expect %q, %q", out, "LALA=value2", "HOME=/root")
2bceaae4
 	}
 }
90928eb1
 
dc944ea7
 func (s *DockerSuite) TestExecExitStatus(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top")
90928eb1
 
 	// Test normal (non-detached) case first
 	cmd := exec.Command(dockerBinary, "exec", "top", "sh", "-c", "exit 23")
 	ec, _ := runCommand(cmd)
 	if ec != 23 {
dc944ea7
 		c.Fatalf("Should have had an ExitCode of 23, not: %d", ec)
90928eb1
 	}
 }
1bb02117
 
dc944ea7
 func (s *DockerSuite) TestExecPausedContainer(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
1bb02117
 	defer unpauseAllContainers()
 
668e2369
 	out, _ := dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
475c6531
 	ContainerID := strings.TrimSpace(out)
1bb02117
 
668e2369
 	dockerCmd(c, "pause", "testing")
693ba98c
 	out, _, err := dockerCmdWithError("exec", "-i", "-t", ContainerID, "echo", "hello")
1bb02117
 	if err == nil {
dc944ea7
 		c.Fatal("container should fail to exec new command if it is paused")
1bb02117
 	}
 
 	expected := ContainerID + " is paused, unpause the container before exec"
 	if !strings.Contains(out, expected) {
dc944ea7
 		c.Fatal("container should not exec new command if it is paused")
1bb02117
 	}
 }
243a640d
 
 // regression test for #9476
dc944ea7
 func (s *DockerSuite) TestExecTtyCloseStdin(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	dockerCmd(c, "run", "-d", "-it", "--name", "exec_tty_stdin", "busybox")
243a640d
 
668e2369
 	cmd := exec.Command(dockerBinary, "exec", "-i", "exec_tty_stdin", "cat")
243a640d
 	stdinRw, err := cmd.StdinPipe()
 	if err != nil {
dc944ea7
 		c.Fatal(err)
243a640d
 	}
 
 	stdinRw.Write([]byte("test"))
 	stdinRw.Close()
 
 	if out, _, err := runCommandWithOutput(cmd); err != nil {
dc944ea7
 		c.Fatal(out, err)
243a640d
 	}
 
668e2369
 	out, _ := dockerCmd(c, "top", "exec_tty_stdin")
243a640d
 	outArr := strings.Split(out, "\n")
 	if len(outArr) > 3 || strings.Contains(out, "nsenter-exec") {
dc944ea7
 		c.Fatalf("exec process left running\n\t %s", out)
243a640d
 	}
 }
67e3ddb7
 
dc944ea7
 func (s *DockerSuite) TestExecTtyWithoutStdin(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
67e3ddb7
 	id := strings.TrimSpace(out)
799d9605
 	c.Assert(waitRun(id), check.IsNil)
67e3ddb7
 
4203230c
 	errChan := make(chan error)
67e3ddb7
 	go func() {
4203230c
 		defer close(errChan)
67e3ddb7
 
 		cmd := exec.Command(dockerBinary, "exec", "-ti", id, "true")
 		if _, err := cmd.StdinPipe(); err != nil {
4203230c
 			errChan <- err
 			return
67e3ddb7
 		}
 
 		expected := "cannot enable tty mode"
 		if out, _, err := runCommandWithOutput(cmd); err == nil {
4203230c
 			errChan <- fmt.Errorf("exec should have failed")
 			return
67e3ddb7
 		} else if !strings.Contains(out, expected) {
4203230c
 			errChan <- fmt.Errorf("exec failed with error %q: expected %q", out, expected)
 			return
67e3ddb7
 		}
 	}()
 
 	select {
4203230c
 	case err := <-errChan:
 		c.Assert(err, check.IsNil)
67e3ddb7
 	case <-time.After(3 * time.Second):
dc944ea7
 		c.Fatal("exec is running but should have failed")
67e3ddb7
 	}
 }
7fdbd90f
 
dc944ea7
 func (s *DockerSuite) TestExecParseError(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top")
7fdbd90f
 
 	// Test normal (non-detached) case first
 	cmd := exec.Command(dockerBinary, "exec", "top")
1db92711
 	if _, stderr, code, err := runCommandWithStdoutStderr(cmd); err == nil || !strings.Contains(stderr, "See '"+dockerBinary+" exec --help'") || code == 0 {
dc944ea7
 		c.Fatalf("Should have thrown error & point to help: %s", stderr)
7fdbd90f
 	}
 }
eda92e88
 
dc944ea7
 func (s *DockerSuite) TestExecStopNotHanging(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
eda92e88
 
 	if err := exec.Command(dockerBinary, "exec", "testing", "top").Start(); err != nil {
dc944ea7
 		c.Fatal(err)
eda92e88
 	}
 
4203230c
 	type dstop struct {
 		out []byte
 		err error
 	}
 
 	ch := make(chan dstop)
eda92e88
 	go func() {
4203230c
 		out, err := exec.Command(dockerBinary, "stop", "testing").CombinedOutput()
 		ch <- dstop{out, err}
 		close(ch)
eda92e88
 	}()
 	select {
 	case <-time.After(3 * time.Second):
dc944ea7
 		c.Fatal("Container stop timed out")
4203230c
 	case s := <-ch:
 		c.Assert(s.err, check.IsNil)
eda92e88
 	}
 }
9462dbb2
 
dc944ea7
 func (s *DockerSuite) TestExecCgroup(c *check.C) {
ea3afdad
 	testRequires(c, NotUserNamespace)
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
9462dbb2
 
668e2369
 	out, _ := dockerCmd(c, "exec", "testing", "cat", "/proc/1/cgroup")
 	containerCgroups := sort.StringSlice(strings.Split(out, "\n"))
9462dbb2
 
 	var wg sync.WaitGroup
dc944ea7
 	var mu sync.Mutex
9462dbb2
 	execCgroups := []sort.StringSlice{}
4203230c
 	errChan := make(chan error)
9462dbb2
 	// exec a few times concurrently to get consistent failure
 	for i := 0; i < 5; i++ {
 		wg.Add(1)
 		go func() {
693ba98c
 			out, _, err := dockerCmdWithError("exec", "testing", "cat", "/proc/self/cgroup")
9462dbb2
 			if err != nil {
4203230c
 				errChan <- err
 				return
9462dbb2
 			}
668e2369
 			cg := sort.StringSlice(strings.Split(out, "\n"))
9462dbb2
 
dc944ea7
 			mu.Lock()
9462dbb2
 			execCgroups = append(execCgroups, cg)
dc944ea7
 			mu.Unlock()
9462dbb2
 			wg.Done()
 		}()
 	}
 	wg.Wait()
4203230c
 	close(errChan)
 
 	for err := range errChan {
 		c.Assert(err, check.IsNil)
 	}
9462dbb2
 
 	for _, cg := range execCgroups {
 		if !reflect.DeepEqual(cg, containerCgroups) {
 			fmt.Println("exec cgroups:")
 			for _, name := range cg {
 				fmt.Printf(" %s\n", name)
 			}
 
 			fmt.Println("container cgroups:")
 			for _, name := range containerCgroups {
 				fmt.Printf(" %s\n", name)
 			}
dc944ea7
 			c.Fatal("cgroups mismatched")
9462dbb2
 		}
 	}
 }
957cbdbf
 
dc944ea7
 func (s *DockerSuite) TestInspectExecID(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
957cbdbf
 	id := strings.TrimSuffix(out, "\n")
 
668e2369
 	out, err := inspectField(id, "ExecIDs")
957cbdbf
 	if err != nil {
dc944ea7
 		c.Fatalf("failed to inspect container: %s, %v", out, err)
957cbdbf
 	}
231d362d
 	if out != "[]" {
dc944ea7
 		c.Fatalf("ExecIDs should be empty, got: %s", out)
957cbdbf
 	}
 
fe6a7c8e
 	// Start an exec, have it block waiting so we can do some checking
 	cmd := exec.Command(dockerBinary, "exec", id, "sh", "-c",
 		"while ! test -e /tmp/execid1; do sleep 1; done")
72b75cd4
 
 	if err = cmd.Start(); err != nil {
 		c.Fatalf("failed to start the exec cmd: %q", err)
 	}
 
 	// Give the exec 10 chances/seconds to start then give up and stop the test
 	tries := 10
 	for i := 0; i < tries; i++ {
f06620ec
 		// Since its still running we should see exec as part of the container
 		out, err = inspectField(id, "ExecIDs")
 		if err != nil {
 			c.Fatalf("failed to inspect container: %s, %v", out, err)
 		}
 
72b75cd4
 		out = strings.TrimSuffix(out, "\n")
 		if out != "[]" && out != "<no value>" {
 			break
 		}
c5c98c31
 		if i+1 == tries {
72b75cd4
 			c.Fatalf("ExecIDs should not be empty, got: %s", out)
 		}
 		time.Sleep(1 * time.Second)
 	}
 
 	// Save execID for later
 	execID, err := inspectFilter(id, "index .ExecIDs 0")
 	if err != nil {
97c5f640
 		c.Fatalf("failed to get the exec id: %v", err)
957cbdbf
 	}
 
fe6a7c8e
 	// End the exec by creating the missing file
 	err = exec.Command(dockerBinary, "exec", id,
 		"sh", "-c", "touch /tmp/execid1").Run()
 
 	if err != nil {
 		c.Fatalf("failed to run the 2nd exec cmd: %q", err)
 	}
 
 	// Wait for 1st exec to complete
72b75cd4
 	cmd.Wait()
 
 	// All execs for the container should be gone now
957cbdbf
 	out, err = inspectField(id, "ExecIDs")
 	if err != nil {
dc944ea7
 		c.Fatalf("failed to inspect container: %s, %v", out, err)
957cbdbf
 	}
 
 	out = strings.TrimSuffix(out, "\n")
72b75cd4
 	if out != "[]" && out != "<no value>" {
 		c.Fatalf("ExecIDs should be empty, got: %s", out)
 	}
 
 	// But we should still be able to query the execID
 	sc, body, err := sockRequest("GET", "/exec/"+execID+"/json", nil)
 	if sc != http.StatusOK {
26ce3f4c
 		c.Fatalf("received status != 200 OK: %d\n%s", sc, body)
957cbdbf
 	}
d841b779
 
 	// Now delete the container and then an 'inspect' on the exec should
 	// result in a 404 (not 'container not running')
 	out, ec := dockerCmd(c, "rm", "-f", id)
 	if ec != 0 {
 		c.Fatalf("error removing container: %s", out)
 	}
 	sc, body, err = sockRequest("GET", "/exec/"+execID+"/json", nil)
 	if sc != http.StatusNotFound {
5b870d7f
 		c.Fatalf("received status != 404: %d\n%s", sc, body)
d841b779
 	}
957cbdbf
 }
43d1c201
 
dc944ea7
 func (s *DockerSuite) TestLinksPingLinkedContainersOnRename(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
43d1c201
 	var out string
dc944ea7
 	out, _ = dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
475c6531
 	idA := strings.TrimSpace(out)
43d1c201
 	if idA == "" {
dc944ea7
 		c.Fatal(out, "id should not be nil")
43d1c201
 	}
dc944ea7
 	out, _ = dockerCmd(c, "run", "-d", "--link", "container1:alias1", "--name", "container2", "busybox", "top")
475c6531
 	idB := strings.TrimSpace(out)
43d1c201
 	if idB == "" {
dc944ea7
 		c.Fatal(out, "id should not be nil")
43d1c201
 	}
 
668e2369
 	dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
dc944ea7
 	dockerCmd(c, "rename", "container1", "container_new")
668e2369
 	dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
43d1c201
 }
 
dc944ea7
 func (s *DockerSuite) TestRunExecDir(c *check.C) {
 	testRequires(c, SameHostDaemon)
668e2369
 
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
43d1c201
 	id := strings.TrimSpace(out)
 	execDir := filepath.Join(execDriverPath, id)
 	stateFile := filepath.Join(execDir, "state.json")
 
 	{
 		fi, err := os.Stat(execDir)
 		if err != nil {
dc944ea7
 			c.Fatal(err)
43d1c201
 		}
 		if !fi.IsDir() {
dc944ea7
 			c.Fatalf("%q must be a directory", execDir)
43d1c201
 		}
 		fi, err = os.Stat(stateFile)
 		if err != nil {
dc944ea7
 			c.Fatal(err)
43d1c201
 		}
 	}
 
668e2369
 	dockerCmd(c, "stop", id)
43d1c201
 	{
68ba5f0b
 		_, err := os.Stat(execDir)
43d1c201
 		if err == nil {
dc944ea7
 			c.Fatal(err)
43d1c201
 		}
 		if err == nil {
dc944ea7
 			c.Fatalf("Exec directory %q exists for removed container!", execDir)
43d1c201
 		}
 		if !os.IsNotExist(err) {
dc944ea7
 			c.Fatalf("Error should be about non-existing, got %s", err)
43d1c201
 		}
 	}
668e2369
 	dockerCmd(c, "start", id)
43d1c201
 	{
 		fi, err := os.Stat(execDir)
 		if err != nil {
dc944ea7
 			c.Fatal(err)
43d1c201
 		}
 		if !fi.IsDir() {
dc944ea7
 			c.Fatalf("%q must be a directory", execDir)
43d1c201
 		}
 		fi, err = os.Stat(stateFile)
 		if err != nil {
dc944ea7
 			c.Fatal(err)
43d1c201
 		}
 	}
668e2369
 	dockerCmd(c, "rm", "-f", id)
43d1c201
 	{
 		_, err := os.Stat(execDir)
 		if err == nil {
dc944ea7
 			c.Fatal(err)
43d1c201
 		}
 		if err == nil {
dc944ea7
 			c.Fatalf("Exec directory %q is exists for removed container!", execDir)
43d1c201
 		}
 		if !os.IsNotExist(err) {
dc944ea7
 			c.Fatalf("Error should be about non-existing, got %s", err)
43d1c201
 		}
 	}
 }
6a2c6e97
 
dc944ea7
 func (s *DockerSuite) TestRunMutableNetworkFiles(c *check.C) {
 	testRequires(c, SameHostDaemon)
6a2c6e97
 
 	for _, fn := range []string{"resolv.conf", "hosts"} {
 		deleteAllContainers()
 
 		content, err := runCommandAndReadContainerFile(fn, exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn)))
 		if err != nil {
dc944ea7
 			c.Fatal(err)
6a2c6e97
 		}
 
 		if strings.TrimSpace(string(content)) != "success" {
dc944ea7
 			c.Fatal("Content was not what was modified in the container", string(content))
6a2c6e97
 		}
 
668e2369
 		out, _ := dockerCmd(c, "run", "-d", "--name", "c2", "busybox", "top")
6a2c6e97
 		contID := strings.TrimSpace(out)
 		netFilePath := containerStorageFile(contID, fn)
 
 		f, err := os.OpenFile(netFilePath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644)
 		if err != nil {
dc944ea7
 			c.Fatal(err)
6a2c6e97
 		}
 
 		if _, err := f.Seek(0, 0); err != nil {
 			f.Close()
dc944ea7
 			c.Fatal(err)
6a2c6e97
 		}
 
 		if err := f.Truncate(0); err != nil {
 			f.Close()
dc944ea7
 			c.Fatal(err)
6a2c6e97
 		}
 
 		if _, err := f.Write([]byte("success2\n")); err != nil {
 			f.Close()
dc944ea7
 			c.Fatal(err)
6a2c6e97
 		}
 		f.Close()
 
668e2369
 		res, _ := dockerCmd(c, "exec", contID, "cat", "/etc/"+fn)
 		if res != "success2\n" {
dc944ea7
 			c.Fatalf("Expected content of %s: %q, got: %q", fn, "success2\n", res)
6a2c6e97
 		}
 	}
 }
2cce4791
 
dc944ea7
 func (s *DockerSuite) TestExecWithUser(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
2cce4791
 
668e2369
 	out, _ := dockerCmd(c, "exec", "-u", "1", "parent", "id")
2cce4791
 	if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
dc944ea7
 		c.Fatalf("exec with user by id expected daemon user got %s", out)
2cce4791
 	}
 
668e2369
 	out, _ = dockerCmd(c, "exec", "-u", "root", "parent", "id")
2cce4791
 	if !strings.Contains(out, "uid=0(root) gid=0(root)") {
dc944ea7
 		c.Fatalf("exec with user by root expected root user got %s", out)
2cce4791
 	}
 }
0faa4518
 
03f65b3d
 func (s *DockerSuite) TestExecWithPrivileged(c *check.C) {
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
90326939
 	// Start main loop which attempts mknod repeatedly
 	dockerCmd(c, "run", "-d", "--name", "parent", "--cap-drop=ALL", "busybox", "sh", "-c", `while (true); do if [ -e /exec_priv ]; then cat /exec_priv && mknod /tmp/sda b 8 0 && echo "Success"; else echo "Privileged exec has not run yet"; fi; usleep 10000; done`)
03f65b3d
 
90326939
 	// Check exec mknod doesn't work
 	cmd := exec.Command(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdb b 8 16")
03f65b3d
 	out, _, err := runCommandWithOutput(cmd)
 	if err == nil || !strings.Contains(out, "Operation not permitted") {
90326939
 		c.Fatalf("exec mknod in --cap-drop=ALL container without --privileged should fail")
03f65b3d
 	}
 
90326939
 	// Check exec mknod does work with --privileged
 	cmd = exec.Command(dockerBinary, "exec", "--privileged", "parent", "sh", "-c", `echo "Running exec --privileged" > /exec_priv && mknod /tmp/sdb b 8 16 && usleep 50000 && echo "Finished exec --privileged" > /exec_priv && echo ok`)
03f65b3d
 	out, _, err = runCommandWithOutput(cmd)
 	if err != nil {
 		c.Fatal(err, out)
 	}
 
 	if actual := strings.TrimSpace(out); actual != "ok" {
 		c.Fatalf("exec mknod in --cap-drop=ALL container with --privileged failed: %v, output: %q", err, out)
 	}
 
90326939
 	// Check subsequent unprivileged exec cannot mknod
 	cmd = exec.Command(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdc b 8 32")
 	out, _, err = runCommandWithOutput(cmd)
 	if err == nil || !strings.Contains(out, "Operation not permitted") {
 		c.Fatalf("repeating exec mknod in --cap-drop=ALL container after --privileged without --privileged should fail")
 	}
 
 	// Confirm at no point was mknod allowed
 	logCmd := exec.Command(dockerBinary, "logs", "parent")
 	if out, _, err := runCommandWithOutput(logCmd); err != nil || strings.Contains(out, "Success") {
 		c.Fatal(out, err)
 	}
 
03f65b3d
 }
 
0faa4518
 func (s *DockerSuite) TestExecWithImageUser(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
0faa4518
 	name := "testbuilduser"
 	_, err := buildImage(name,
 		`FROM busybox
 		RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
 		USER dockerio`,
 		true)
 	if err != nil {
 		c.Fatalf("Could not build image %s: %v", name, err)
 	}
 
 	dockerCmd(c, "run", "-d", "--name", "dockerioexec", name, "top")
 
 	out, _ := dockerCmd(c, "exec", "dockerioexec", "whoami")
 	if !strings.Contains(out, "dockerio") {
 		c.Fatalf("exec with user by id expected dockerio user got %s", out)
 	}
 }
bfc51cf6
 
 func (s *DockerSuite) TestExecOnReadonlyContainer(c *check.C) {
ea3afdad
 	// --read-only + userns has remount issues
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
bfc51cf6
 	dockerCmd(c, "run", "-d", "--read-only", "--name", "parent", "busybox", "top")
 	if _, status := dockerCmd(c, "exec", "parent", "true"); status != 0 {
 		c.Fatalf("exec into a read-only container failed with exit status %d", status)
 	}
 }
fcf9daad
 
 // #15750
 func (s *DockerSuite) TestExecStartFails(c *check.C) {
d9f5f195
 	testRequires(c, DaemonIsLinux)
fcf9daad
 	name := "exec-15750"
 	dockerCmd(c, "run", "-d", "--name", name, "busybox", "top")
9077c896
 	c.Assert(waitRun(name), check.IsNil)
fcf9daad
 
9077c896
 	out, _, err := dockerCmdWithError("exec", name, "no-such-cmd")
 	c.Assert(err, check.NotNil, check.Commentf(out))
 	c.Assert(out, checker.Contains, "executable file not found")
fcf9daad
 }