diff --git a/create.go b/create.go index 77663133..9d8a52b3 100644 --- a/create.go +++ b/create.go @@ -54,6 +54,9 @@ command(s) that get executed on start, edit the args parameter of the spec. See cli.ShowCommandHelp(context, "create") return fmt.Errorf("runc: \"create\" requires exactly one argument") } + if err := revisePidFile(context); err != nil { + return err + } spec, err := setupSpec(context) if err != nil { return err diff --git a/exec.go b/exec.go index a0b4f4f4..e49e904b 100644 --- a/exec.go +++ b/exec.go @@ -89,6 +89,9 @@ following will output a list of processes running in the container: if os.Geteuid() != 0 { return fmt.Errorf("runc should be run as root") } + if err := revisePidFile(context); err != nil { + return err + } status, err := execProcess(context) if err == nil { os.Exit(status) diff --git a/run.go b/run.go index 3eb63de9..64b986bd 100644 --- a/run.go +++ b/run.go @@ -59,6 +59,9 @@ command(s) that get executed on start, edit the args parameter of the spec. See }, }, Action: func(context *cli.Context) error { + if err := revisePidFile(context); err != nil { + return err + } spec, err := setupSpec(context) if err != nil { return err diff --git a/tests/integration/create.bats b/tests/integration/create.bats index cac2e6de..c3527955 100644 --- a/tests/integration/create.bats +++ b/tests/integration/create.bats @@ -59,3 +59,29 @@ function teardown() { testcontainer test_busybox running } + +@test "runc create --pid-file with new CWD" { + # create pid_file directory as the CWD + run mkdir pid_file + [ "$status" -eq 0 ] + run cd pid_file + [ "$status" -eq 0 ] + + runc create --pid-file pid.txt -b $BUSYBOX_BUNDLE --console /dev/pts/ptmx test_busybox + [ "$status" -eq 0 ] + + testcontainer test_busybox created + + # check pid.txt was generated + [ -e pid.txt ] + + run cat pid.txt + [ "$status" -eq 0 ] + [[ ${lines[0]} == $(__runc state test_busybox | jq '.pid') ]] + + # start the command + runc start test_busybox + [ "$status" -eq 0 ] + + testcontainer test_busybox running +} diff --git a/tests/integration/exec.bats b/tests/integration/exec.bats index 61b62e45..23d5c24f 100644 --- a/tests/integration/exec.bats +++ b/tests/integration/exec.bats @@ -45,6 +45,33 @@ function teardown() { [[ ${lines[0]} != $(__runc state test_busybox | jq '.pid') ]] } +@test "runc exec --pid-file with new CWD" { + # create pid_file directory as the CWD + run mkdir pid_file + [ "$status" -eq 0 ] + run cd pid_file + [ "$status" -eq 0 ] + + # run busybox detached + runc run -d -b $BUSYBOX_BUNDLE --console /dev/pts/ptmx test_busybox + [ "$status" -eq 0 ] + + wait_for_container 15 1 test_busybox + + runc exec --pid-file pid.txt test_busybox echo Hello from exec + [ "$status" -eq 0 ] + echo text echoed = "'""${output}""'" + [[ "${output}" == *"Hello from exec"* ]] + + # check pid.txt was generated + [ -e pid.txt ] + + run cat pid.txt + [ "$status" -eq 0 ] + [[ ${lines[0]} =~ [0-9]+ ]] + [[ ${lines[0]} != $(__runc state test_busybox | jq '.pid') ]] +} + @test "runc exec ls -la" { # run busybox detached runc run -d --console /dev/pts/ptmx test_busybox diff --git a/tests/integration/start_detached.bats b/tests/integration/start_detached.bats index 48d99957..02ca90d1 100644 --- a/tests/integration/start_detached.bats +++ b/tests/integration/start_detached.bats @@ -55,3 +55,27 @@ function teardown() { [ "$status" -eq 0 ] [[ ${lines[0]} == $(__runc state test_busybox | jq '.pid') ]] } + +@test "runc run detached --pid-file with new CWD" { + # create pid_file directory as the CWD + run mkdir pid_file + [ "$status" -eq 0 ] + run cd pid_file + [ "$status" -eq 0 ] + + # run busybox detached + runc run --pid-file pid.txt -d -b $BUSYBOX_BUNDLE --console /dev/pts/ptmx test_busybox + [ "$status" -eq 0 ] + + # check state + wait_for_container 15 1 test_busybox + + testcontainer test_busybox running + + # check pid.txt was generated + [ -e pid.txt ] + + run cat pid.txt + [ "$status" -eq 0 ] + [[ ${lines[0]} == $(__runc state test_busybox | jq '.pid') ]] +} diff --git a/utils.go b/utils.go index b3de0065..adf312f7 100644 --- a/utils.go +++ b/utils.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "path/filepath" "github.com/Sirupsen/logrus" "github.com/opencontainers/runtime-spec/specs-go" @@ -39,3 +40,22 @@ func setupSpec(context *cli.Context) (*specs.Spec, error) { } return spec, nil } + +func revisePidFile(context *cli.Context) error { + pidFile := context.String("pid-file") + if pidFile == "" { + return nil + } + + // convert pid-file to an absolute path so we can write to the right + // file after chdir to bundle + pidFile, err := filepath.Abs(pidFile) + if err != nil { + return err + } + err = context.Set("pid-file", pidFile) + if err != nil { + return err + } + return nil +}