From df79231bdf80b2ac09a4ae0acfa8d3c4b794ffab Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Wed, 28 Sep 2016 15:27:24 -0700 Subject: [PATCH] Add exec to example Signed-off-by: Michael Crosby --- .gitignore | 1 + container.go | 12 +++++++----- example/main.go | 38 ++++++++++++++++++++++++++++++++++++++ runc/runc.go | 5 ++++- 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index b76083b5f..0bf5a0dd4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.exe +main /containerd/containerd /containerd-shim/containerd-shim /bin/ diff --git a/container.go b/container.go index 4dc26de88..6a5662631 100644 --- a/container.go +++ b/container.go @@ -100,9 +100,10 @@ func (c *Container) NewProcess(spec *specs.Process) (*Process, error) { c.mu.Lock() defer c.mu.Unlock() process := &Process{ - s: spec, - c: c, - exec: true, + s: spec, + c: c, + exec: true, + driver: c.driver, } c.processes = append(c.processes, process) return process, nil @@ -123,8 +124,9 @@ func (c *Container) Pid() int { // Wait will perform a blocking wait on the init process of the container func (c *Container) Wait() (uint32, error) { c.mu.Lock() - defer c.mu.Unlock() - return c.init.Wait() + proc := c.init + c.mu.Unlock() + return proc.Wait() } // Signal will send the provided signal to the init process of the container diff --git a/example/main.go b/example/main.go index 2ecdf7d39..ab7bf8e58 100644 --- a/example/main.go +++ b/example/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "path/filepath" "runtime" @@ -65,6 +66,43 @@ func runTest() error { return err } + for i := 0; i < 10; i++ { + process, err := container.NewProcess(&specs.Process{ + Args: []string{ + "echo", fmt.Sprintf("sup from itteration %d", i), + }, + Env: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}, + Terminal: false, + Cwd: "/", + NoNewPrivileges: true, + Capabilities: []string{ + "CAP_AUDIT_WRITE", + "CAP_KILL", + "CAP_FOWNER", + "CAP_CHOWN", + "CAP_MKNOD", + "CAP_FSETID", + "CAP_DAC_OVERRIDE", + "CAP_SETFCAP", + "CAP_SETPCAP", + "CAP_SETGID", + "CAP_SETUID", + "CAP_NET_BIND_SERVICE", + }, + }) + process.Stdin = os.Stdin + process.Stdout = os.Stdout + process.Stderr = os.Stderr + if err := process.Start(); err != nil { + return err + } + procStatus, err := process.Wait() + if err != nil { + return err + } + logrus.Infof("process %d returned with %d", i, procStatus) + } + // wait for it to exit and get the exit status logrus.Info("wait container") status, err := container.Wait() diff --git a/runc/runc.go b/runc/runc.go index 0ef44241e..ecd3b1e0b 100644 --- a/runc/runc.go +++ b/runc/runc.go @@ -66,8 +66,11 @@ func (r *Runc) Exec(c *containerkit.Container, p *containerkit.Process) (contain if err != nil { return nil, err } - cmd := r.command("exec", "--process", path, "--pid-file", pidFile, c.ID()) + cmd := r.command("exec", "--detach", "--process", path, "--pid-file", pidFile, c.ID()) cmd.Stdin, cmd.Stdout, cmd.Stderr = p.Stdin, p.Stdout, p.Stderr + if err := cmd.Run(); err != nil { + return nil, err + } data, err := ioutil.ReadFile(pidFile) if err != nil { return nil, err