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"
|
|
|
|
|
2017-01-26 19:29:19 +00:00
|
|
|
"github.com/crosbymichael/console"
|
2017-01-24 22:45:18 +00:00
|
|
|
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
|
|
|
|
|
2017-01-26 23:09:59 +00:00
|
|
|
id int
|
2017-01-26 19:29:19 +00:00
|
|
|
console console.Console
|
2017-01-24 22:45:18 +00:00
|
|
|
io runc.IO
|
|
|
|
status int
|
|
|
|
pid int
|
|
|
|
|
|
|
|
parent *initProcess
|
|
|
|
}
|
|
|
|
|
2017-01-26 23:09:59 +00:00
|
|
|
func newExecProcess(context context.Context, r *apishim.ExecRequest, parent *initProcess, id int) (process, error) {
|
2017-01-24 22:45:18 +00:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e := &execProcess{
|
2017-01-26 23:09:59 +00:00
|
|
|
id: id,
|
2017-01-24 22:45:18 +00:00
|
|
|
parent: parent,
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
socket *runc.ConsoleSocket
|
|
|
|
io runc.IO
|
2017-01-26 23:09:59 +00:00
|
|
|
pidfile = filepath.Join(cwd, fmt.Sprintf("%d.pid", id))
|
2017-01-24 22:45:18 +00:00
|
|
|
)
|
|
|
|
if r.Terminal {
|
|
|
|
if socket, err = runc.NewConsoleSocket(filepath.Join(cwd, "pty.sock")); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-26 19:29:19 +00:00
|
|
|
defer os.Remove(socket.Path())
|
2017-01-24 22:45:18 +00:00
|
|
|
} 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,
|
|
|
|
}
|
2017-01-26 23:09:59 +00:00
|
|
|
if err := parent.runc.Exec(context, parent.id, processFromRequest(r), opts); err != nil {
|
2017-01-24 22:45:18 +00:00
|
|
|
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-26 23:09:59 +00:00
|
|
|
var user specs.User
|
|
|
|
if r.User != nil {
|
|
|
|
user.UID = r.User.Uid
|
|
|
|
user.GID = r.User.Gid
|
|
|
|
user.AdditionalGids = r.User.AdditionalGids
|
|
|
|
}
|
2017-01-24 22:45:18 +00:00
|
|
|
return specs.Process{
|
2017-01-26 23:09:59 +00:00
|
|
|
Terminal: r.Terminal,
|
|
|
|
User: user,
|
2017-01-24 22:45:18 +00:00
|
|
|
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
|
|
|
|
e.Wait()
|
2017-01-26 19:29:19 +00:00
|
|
|
if e.io != nil {
|
|
|
|
e.io.Close()
|
|
|
|
}
|
2017-01-24 22:45:18 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 23:09:59 +00:00
|
|
|
func (e *execProcess) Delete(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:29:19 +00:00
|
|
|
func (e *execProcess) Resize(ws console.WinSize) error {
|
2017-01-24 22:45:18 +00:00
|
|
|
if e.console == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e.console.Resize(ws)
|
2017-01-24 00:18:10 +00:00
|
|
|
}
|