Skip to content

Commit df79231

Browse files
committed
Add exec to example
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
1 parent b3ab74f commit df79231

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.exe
2+
main
23
/containerd/containerd
34
/containerd-shim/containerd-shim
45
/bin/

container.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ func (c *Container) NewProcess(spec *specs.Process) (*Process, error) {
100100
c.mu.Lock()
101101
defer c.mu.Unlock()
102102
process := &Process{
103-
s: spec,
104-
c: c,
105-
exec: true,
103+
s: spec,
104+
c: c,
105+
exec: true,
106+
driver: c.driver,
106107
}
107108
c.processes = append(c.processes, process)
108109
return process, nil
@@ -123,8 +124,9 @@ func (c *Container) Pid() int {
123124
// Wait will perform a blocking wait on the init process of the container
124125
func (c *Container) Wait() (uint32, error) {
125126
c.mu.Lock()
126-
defer c.mu.Unlock()
127-
return c.init.Wait()
127+
proc := c.init
128+
c.mu.Unlock()
129+
return proc.Wait()
128130
}
129131

130132
// Signal will send the provided signal to the init process of the container

example/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67
"runtime"
@@ -65,6 +66,43 @@ func runTest() error {
6566
return err
6667
}
6768

69+
for i := 0; i < 10; i++ {
70+
process, err := container.NewProcess(&specs.Process{
71+
Args: []string{
72+
"echo", fmt.Sprintf("sup from itteration %d", i),
73+
},
74+
Env: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"},
75+
Terminal: false,
76+
Cwd: "/",
77+
NoNewPrivileges: true,
78+
Capabilities: []string{
79+
"CAP_AUDIT_WRITE",
80+
"CAP_KILL",
81+
"CAP_FOWNER",
82+
"CAP_CHOWN",
83+
"CAP_MKNOD",
84+
"CAP_FSETID",
85+
"CAP_DAC_OVERRIDE",
86+
"CAP_SETFCAP",
87+
"CAP_SETPCAP",
88+
"CAP_SETGID",
89+
"CAP_SETUID",
90+
"CAP_NET_BIND_SERVICE",
91+
},
92+
})
93+
process.Stdin = os.Stdin
94+
process.Stdout = os.Stdout
95+
process.Stderr = os.Stderr
96+
if err := process.Start(); err != nil {
97+
return err
98+
}
99+
procStatus, err := process.Wait()
100+
if err != nil {
101+
return err
102+
}
103+
logrus.Infof("process %d returned with %d", i, procStatus)
104+
}
105+
68106
// wait for it to exit and get the exit status
69107
logrus.Info("wait container")
70108
status, err := container.Wait()

runc/runc.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ func (r *Runc) Exec(c *containerkit.Container, p *containerkit.Process) (contain
6666
if err != nil {
6767
return nil, err
6868
}
69-
cmd := r.command("exec", "--process", path, "--pid-file", pidFile, c.ID())
69+
cmd := r.command("exec", "--detach", "--process", path, "--pid-file", pidFile, c.ID())
7070
cmd.Stdin, cmd.Stdout, cmd.Stderr = p.Stdin, p.Stdout, p.Stderr
71+
if err := cmd.Run(); err != nil {
72+
return nil, err
73+
}
7174
data, err := ioutil.ReadFile(pidFile)
7275
if err != nil {
7376
return nil, err

0 commit comments

Comments
 (0)