Skip to content

Commit 8676c75

Browse files
committed
Fix the pid-file option for runc run/exec/create command
Signed-off-by: Wang Long <[email protected]>
1 parent bc462c9 commit 8676c75

File tree

7 files changed

+106
-0
lines changed

7 files changed

+106
-0
lines changed

create.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ command(s) that get executed on start, edit the args parameter of the spec. See
5454
cli.ShowCommandHelp(context, "create")
5555
return fmt.Errorf("runc: \"create\" requires exactly one argument")
5656
}
57+
if err := revisePidFile(context); err != nil {
58+
return err
59+
}
5760
spec, err := setupSpec(context)
5861
if err != nil {
5962
return err

exec.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ following will output a list of processes running in the container:
8989
if os.Geteuid() != 0 {
9090
return fmt.Errorf("runc should be run as root")
9191
}
92+
if err := revisePidFile(context); err != nil {
93+
return err
94+
}
9295
status, err := execProcess(context)
9396
if err == nil {
9497
os.Exit(status)

run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ command(s) that get executed on start, edit the args parameter of the spec. See
5959
},
6060
},
6161
Action: func(context *cli.Context) error {
62+
if err := revisePidFile(context); err != nil {
63+
return err
64+
}
6265
spec, err := setupSpec(context)
6366
if err != nil {
6467
return err

tests/integration/create.bats

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,29 @@ function teardown() {
5959

6060
testcontainer test_busybox running
6161
}
62+
63+
@test "runc create --pid-file with new CWD" {
64+
# create pid_file directory as the CWD
65+
run mkdir pid_file
66+
[ "$status" -eq 0 ]
67+
run cd pid_file
68+
[ "$status" -eq 0 ]
69+
70+
runc create --pid-file pid.txt -b $BUSYBOX_BUNDLE --console /dev/pts/ptmx test_busybox
71+
[ "$status" -eq 0 ]
72+
73+
testcontainer test_busybox created
74+
75+
# check pid.txt was generated
76+
[ -e pid.txt ]
77+
78+
run cat pid.txt
79+
[ "$status" -eq 0 ]
80+
[[ ${lines[0]} == $(__runc state test_busybox | jq '.pid') ]]
81+
82+
# start the command
83+
runc start test_busybox
84+
[ "$status" -eq 0 ]
85+
86+
testcontainer test_busybox running
87+
}

tests/integration/exec.bats

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,33 @@ function teardown() {
4545
[[ ${lines[0]} != $(__runc state test_busybox | jq '.pid') ]]
4646
}
4747

48+
@test "runc exec --pid-file with new CWD" {
49+
# create pid_file directory as the CWD
50+
run mkdir pid_file
51+
[ "$status" -eq 0 ]
52+
run cd pid_file
53+
[ "$status" -eq 0 ]
54+
55+
# run busybox detached
56+
runc run -d -b $BUSYBOX_BUNDLE --console /dev/pts/ptmx test_busybox
57+
[ "$status" -eq 0 ]
58+
59+
wait_for_container 15 1 test_busybox
60+
61+
runc exec --pid-file pid.txt test_busybox echo Hello from exec
62+
[ "$status" -eq 0 ]
63+
echo text echoed = "'""${output}""'"
64+
[[ "${output}" == *"Hello from exec"* ]]
65+
66+
# check pid.txt was generated
67+
[ -e pid.txt ]
68+
69+
run cat pid.txt
70+
[ "$status" -eq 0 ]
71+
[[ ${lines[0]} =~ [0-9]+ ]]
72+
[[ ${lines[0]} != $(__runc state test_busybox | jq '.pid') ]]
73+
}
74+
4875
@test "runc exec ls -la" {
4976
# run busybox detached
5077
runc run -d --console /dev/pts/ptmx test_busybox

tests/integration/start_detached.bats

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,27 @@ function teardown() {
5555
[ "$status" -eq 0 ]
5656
[[ ${lines[0]} == $(__runc state test_busybox | jq '.pid') ]]
5757
}
58+
59+
@test "runc run detached --pid-file with new CWD" {
60+
# create pid_file directory as the CWD
61+
run mkdir pid_file
62+
[ "$status" -eq 0 ]
63+
run cd pid_file
64+
[ "$status" -eq 0 ]
65+
66+
# run busybox detached
67+
runc run --pid-file pid.txt -d -b $BUSYBOX_BUNDLE --console /dev/pts/ptmx test_busybox
68+
[ "$status" -eq 0 ]
69+
70+
# check state
71+
wait_for_container 15 1 test_busybox
72+
73+
testcontainer test_busybox running
74+
75+
# check pid.txt was generated
76+
[ -e pid.txt ]
77+
78+
run cat pid.txt
79+
[ "$status" -eq 0 ]
80+
[[ ${lines[0]} == $(__runc state test_busybox | jq '.pid') ]]
81+
}

utils.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67

78
"github.com/Sirupsen/logrus"
89
"github.com/opencontainers/runtime-spec/specs-go"
@@ -39,3 +40,22 @@ func setupSpec(context *cli.Context) (*specs.Spec, error) {
3940
}
4041
return spec, nil
4142
}
43+
44+
func revisePidFile(context *cli.Context) error {
45+
pidFile := context.String("pid-file")
46+
if pidFile == "" {
47+
return nil
48+
}
49+
50+
// convert pid-file to an absolute path so we can write to the right
51+
// file after chdir to bundle
52+
pidFile, err := filepath.Abs(pidFile)
53+
if err != nil {
54+
return err
55+
}
56+
err = context.Set("pid-file", pidFile)
57+
if err != nil {
58+
return err
59+
}
60+
return nil
61+
}

0 commit comments

Comments
 (0)