Skip to content

Commit 12c877f

Browse files
Merge pull request #2618 from crosbymichael/no-stdin
Don't provide IO when it's not set
2 parents 18d9e43 + 906acb1 commit 12c877f

10 files changed

Lines changed: 174 additions & 58 deletions

File tree

cio/io.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ func NewCreator(opts ...Opt) Creator {
141141
if err != nil {
142142
return nil, err
143143
}
144+
if streams.Stdin == nil {
145+
fifos.Stdin = ""
146+
}
147+
if streams.Stdout == nil {
148+
fifos.Stdout = ""
149+
}
150+
if streams.Stderr == nil {
151+
fifos.Stderr = ""
152+
}
144153
return copyIO(fifos, streams)
145154
}
146155
}

container_linux_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"context"
2424
"fmt"
2525
"io"
26+
"io/ioutil"
2627
"os/exec"
2728
"runtime"
2829
"strings"
@@ -1481,3 +1482,52 @@ func TestBindLowPortNonOpt(t *testing.T) {
14811482
t.Fatal(err)
14821483
}
14831484
}
1485+
1486+
func TestContainerNoSTDIN(t *testing.T) {
1487+
t.Parallel()
1488+
1489+
client, err := newClient(t, address)
1490+
if err != nil {
1491+
t.Fatal(err)
1492+
}
1493+
defer client.Close()
1494+
1495+
var (
1496+
image Image
1497+
ctx, cancel = testContext()
1498+
id = t.Name()
1499+
)
1500+
defer cancel()
1501+
1502+
image, err = client.GetImage(ctx, testImage)
1503+
if err != nil {
1504+
t.Fatal(err)
1505+
}
1506+
container, err := client.NewContainer(ctx, id, WithNewSpec(oci.WithImageConfig(image), withExitStatus(0)), WithNewSnapshot(id, image))
1507+
if err != nil {
1508+
t.Fatal(err)
1509+
}
1510+
defer container.Delete(ctx, WithSnapshotCleanup)
1511+
1512+
task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStreams(nil, ioutil.Discard, ioutil.Discard)))
1513+
if err != nil {
1514+
t.Fatal(err)
1515+
}
1516+
defer task.Delete(ctx)
1517+
1518+
statusC, err := task.Wait(ctx)
1519+
if err != nil {
1520+
t.Fatal(err)
1521+
}
1522+
if err := task.Start(ctx); err != nil {
1523+
t.Fatal(err)
1524+
}
1525+
status := <-statusC
1526+
code, _, err := status.Result()
1527+
if err != nil {
1528+
t.Fatal(err)
1529+
}
1530+
if code != 0 {
1531+
t.Errorf("expected status 0 from wait but received %d", code)
1532+
}
1533+
}

runtime/v1/linux/proc/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (e *execProcess) start(ctx context.Context) (err error) {
147147
return errors.Wrap(err, "creating new NULL IO")
148148
}
149149
} else {
150-
if e.io, err = runc.NewPipeIO(e.parent.IoUID, e.parent.IoGID); err != nil {
150+
if e.io, err = runc.NewPipeIO(e.parent.IoUID, e.parent.IoGID, withConditionalIO(e.stdio)); err != nil {
151151
return errors.Wrap(err, "failed to create runc io pipes")
152152
}
153153
}

runtime/v1/linux/proc/init.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (p *Init) Create(ctx context.Context, r *CreateConfig) error {
123123
return errors.Wrap(err, "creating new NULL IO")
124124
}
125125
} else {
126-
if p.io, err = runc.NewPipeIO(p.IoUID, p.IoGID); err != nil {
126+
if p.io, err = runc.NewPipeIO(p.IoUID, p.IoGID, withConditionalIO(p.stdio)); err != nil {
127127
return errors.Wrap(err, "failed to create OCI runtime io pipes")
128128
}
129129
}
@@ -399,3 +399,11 @@ func (p *Init) runtimeError(rErr error, msg string) error {
399399
return errors.Errorf("%s: %s", msg, rMsg)
400400
}
401401
}
402+
403+
func withConditionalIO(c proc.Stdio) runc.IOOpt {
404+
return func(o *runc.IOOption) {
405+
o.OpenStdin = c.Stdin != ""
406+
o.OpenStdout = c.Stdout != ""
407+
o.OpenStderr = c.Stderr != ""
408+
}
409+
}

runtime/v1/linux/proc/io.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ func copyPipes(ctx context.Context, rio runc.IO, stdin, stdout, stderr string, w
109109
i.dest(fw, fr)
110110
}
111111
if stdin == "" {
112-
rio.Stdin().Close()
113112
return nil
114113
}
115114
f, err := fifo.OpenFifo(ctx, stdin, syscall.O_RDONLY|syscall.O_NONBLOCK, 0)

vendor.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github.com/containerd/go-runc acb7c88cac264acca9b5eae187a117f4d77a1292
1+
github.com/containerd/go-runc 5a6d9f37cfa36b15efba46dc7ea349fa9b7143c3
22
github.com/containerd/console c12b1e7919c14469339a5d38f2f8ed9b64a9de23
33
github.com/containerd/cgroups 5e610833b72089b37d0e615de9a92dfc043757c2
44
github.com/containerd/typeurl a93fcdb778cd272c6e9b3028b2f42d813e785d40

vendor/github.com/containerd/go-runc/io.go

Lines changed: 46 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/go-runc/io_unix.go

Lines changed: 33 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/go-runc/io_windows.go

Lines changed: 24 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/go-runc/runc.go

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)