2017-01-24 23:55:32 +00:00
|
|
|
package shim
|
2017-01-24 00:18:10 +00:00
|
|
|
|
2017-01-24 22:45:18 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
runc "github.com/crosbymichael/go-runc"
|
2017-01-24 23:55:32 +00:00
|
|
|
apishim "github.com/docker/containerd/api/shim"
|
2017-01-24 22:45:18 +00:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type execProcess struct {
|
|
|
|
sync.WaitGroup
|
|
|
|
|
|
|
|
id string
|
|
|
|
console *runc.Console
|
|
|
|
io runc.IO
|
|
|
|
status int
|
|
|
|
pid int
|
|
|
|
|
|
|
|
parent *initProcess
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func newExecProcess(context context.Context, r *apishim.ExecRequest, parent *initProcess) (process, error) {
|
2017-01-24 22:45:18 +00:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e := &execProcess{
|
|
|
|
id: r.ID,
|
|
|
|
parent: parent,
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
socket *runc.ConsoleSocket
|
|
|
|
io runc.IO
|
|
|
|
pidfile = filepath.Join(cwd, fmt.Sprintf("%s.pid", r.ID))
|
|
|
|
)
|
|
|
|
if r.Terminal {
|
|
|
|
if socket, err = runc.NewConsoleSocket(filepath.Join(cwd, "pty.sock")); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: get uid/gid
|
|
|
|
if io, err = runc.NewPipeIO(0, 0); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e.io = io
|
|
|
|
}
|
|
|
|
opts := &runc.ExecOpts{
|
|
|
|
PidFile: pidfile,
|
|
|
|
ConsoleSocket: socket,
|
|
|
|
IO: io,
|
|
|
|
Detach: true,
|
|
|
|
Tty: socket != nil,
|
|
|
|
}
|
|
|
|
if err := parent.runc.Exec(context, r.ID, processFromRequest(r), opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pid, err := runc.ReadPidFile(opts.PidFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e.pid = pid
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func processFromRequest(r *apishim.ExecRequest) specs.Process {
|
2017-01-24 22:45:18 +00:00
|
|
|
return specs.Process{
|
|
|
|
Terminal: r.Terminal,
|
|
|
|
User: specs.User{
|
|
|
|
UID: r.User.Uid,
|
|
|
|
GID: r.User.Gid,
|
|
|
|
AdditionalGids: r.User.AdditionalGids,
|
|
|
|
},
|
|
|
|
Rlimits: rlimits(r.Rlimits),
|
|
|
|
Args: r.Args,
|
|
|
|
Env: r.Env,
|
|
|
|
Cwd: r.Cwd,
|
|
|
|
Capabilities: r.Capabilities,
|
|
|
|
NoNewPrivileges: r.NoNewPrivileges,
|
|
|
|
ApparmorProfile: r.ApparmorProfile,
|
|
|
|
SelinuxLabel: r.SelinuxLabel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:55:32 +00:00
|
|
|
func rlimits(rr []*apishim.Rlimit) (o []specs.LinuxRlimit) {
|
2017-01-24 22:45:18 +00:00
|
|
|
for _, r := range rr {
|
|
|
|
o = append(o, specs.LinuxRlimit{
|
|
|
|
Type: r.Type,
|
|
|
|
Hard: r.Hard,
|
|
|
|
Soft: r.Soft,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execProcess) Pid() int {
|
|
|
|
return e.pid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execProcess) Status() int {
|
|
|
|
return e.status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execProcess) Exited(status int) {
|
|
|
|
e.status = status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execProcess) Start(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execProcess) Delete(context context.Context) error {
|
|
|
|
e.Wait()
|
|
|
|
e.io.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execProcess) Resize(ws runc.WinSize) error {
|
|
|
|
if e.console == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e.console.Resize(ws)
|
2017-01-24 00:18:10 +00:00
|
|
|
}
|