2015-02-07 13:12:27 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-03-28 01:50:32 +08:00
|
|
|
"errors"
|
2015-02-07 13:12:27 +08:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-03-19 11:22:21 +08:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2015-02-07 13:12:27 +08:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/docker/libcontainer/cgroups"
|
|
|
|
"github.com/docker/libcontainer/system"
|
|
|
|
)
|
|
|
|
|
|
|
|
type parentProcess interface {
|
|
|
|
// pid returns the pid for the running process.
|
|
|
|
pid() int
|
|
|
|
|
|
|
|
// start starts the process execution.
|
|
|
|
start() error
|
|
|
|
|
|
|
|
// send a SIGKILL to the process and wait for the exit.
|
|
|
|
terminate() error
|
|
|
|
|
|
|
|
// wait waits on the process returning the process state.
|
|
|
|
wait() (*os.ProcessState, error)
|
|
|
|
|
|
|
|
// startTime return's the process start time.
|
|
|
|
startTime() (string, error)
|
2015-02-07 14:33:10 +08:00
|
|
|
|
|
|
|
signal(os.Signal) error
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type setnsProcess struct {
|
2015-02-23 17:26:43 +08:00
|
|
|
cmd *exec.Cmd
|
|
|
|
parentPipe *os.File
|
|
|
|
childPipe *os.File
|
|
|
|
cgroupPaths map[string]string
|
|
|
|
config *initConfig
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *setnsProcess) startTime() (string, error) {
|
|
|
|
return system.GetProcessStartTime(p.pid())
|
|
|
|
}
|
|
|
|
|
2015-03-28 01:50:32 +08:00
|
|
|
func (p *setnsProcess) signal(sig os.Signal) error {
|
|
|
|
s, ok := sig.(syscall.Signal)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("os: unsupported signal type")
|
|
|
|
}
|
|
|
|
return syscall.Kill(p.cmd.Process.Pid, s)
|
2015-02-07 14:33:10 +08:00
|
|
|
}
|
|
|
|
|
2015-02-07 13:12:27 +08:00
|
|
|
func (p *setnsProcess) start() (err error) {
|
|
|
|
defer p.parentPipe.Close()
|
2015-02-23 17:26:43 +08:00
|
|
|
if err = p.execSetns(); err != nil {
|
2015-02-12 09:42:58 +08:00
|
|
|
return newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
if len(p.cgroupPaths) > 0 {
|
2015-02-23 17:26:43 +08:00
|
|
|
if err := cgroups.EnterPid(p.cgroupPaths, p.cmd.Process.Pid); err != nil {
|
2015-02-12 09:42:58 +08:00
|
|
|
return newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := json.NewEncoder(p.parentPipe).Encode(p.config); err != nil {
|
2015-02-12 09:42:58 +08:00
|
|
|
return newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
2015-02-28 07:55:53 +08:00
|
|
|
if err := syscall.Shutdown(int(p.parentPipe.Fd()), syscall.SHUT_WR); err != nil {
|
|
|
|
return newSystemError(err)
|
|
|
|
}
|
|
|
|
// wait for the child process to fully complete and receive an error message
|
|
|
|
// if one was encoutered
|
|
|
|
var ierr *genericError
|
|
|
|
if err := json.NewDecoder(p.parentPipe).Decode(&ierr); err != nil && err != io.EOF {
|
|
|
|
return newSystemError(err)
|
|
|
|
}
|
|
|
|
if ierr != nil {
|
|
|
|
return newSystemError(ierr)
|
|
|
|
}
|
|
|
|
|
2015-02-07 13:12:27 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// execSetns runs the process that executes C code to perform the setns calls
|
|
|
|
// because setns support requires the C process to fork off a child and perform the setns
|
|
|
|
// before the go runtime boots, we wait on the process to die and receive the child's pid
|
|
|
|
// over the provided pipe.
|
2015-02-23 17:26:43 +08:00
|
|
|
func (p *setnsProcess) execSetns() error {
|
2015-02-07 13:12:27 +08:00
|
|
|
err := p.cmd.Start()
|
|
|
|
p.childPipe.Close()
|
|
|
|
if err != nil {
|
2015-02-23 17:26:43 +08:00
|
|
|
return newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
status, err := p.cmd.Process.Wait()
|
|
|
|
if err != nil {
|
2015-02-23 17:26:43 +08:00
|
|
|
p.cmd.Wait()
|
|
|
|
return newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
if !status.Success() {
|
2015-02-23 17:26:43 +08:00
|
|
|
p.cmd.Wait()
|
|
|
|
return newSystemError(&exec.ExitError{ProcessState: status})
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
var pid *pid
|
|
|
|
if err := json.NewDecoder(p.parentPipe).Decode(&pid); err != nil {
|
2015-02-23 17:26:43 +08:00
|
|
|
p.cmd.Wait()
|
|
|
|
return newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
|
|
|
|
process, err := os.FindProcess(pid.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.cmd.Process = process
|
|
|
|
return nil
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// terminate sends a SIGKILL to the forked process for the setns routine then waits to
|
|
|
|
// avoid the process becomming a zombie.
|
|
|
|
func (p *setnsProcess) terminate() error {
|
2015-04-23 02:30:42 +08:00
|
|
|
if p.cmd.Process == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
err := p.cmd.Process.Kill()
|
2015-02-07 13:12:27 +08:00
|
|
|
if _, werr := p.wait(); err == nil {
|
|
|
|
err = werr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *setnsProcess) wait() (*os.ProcessState, error) {
|
2015-02-23 17:26:43 +08:00
|
|
|
err := p.cmd.Wait()
|
|
|
|
if err != nil {
|
2015-02-26 03:45:53 +08:00
|
|
|
return p.cmd.ProcessState, err
|
2015-02-23 17:26:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return p.cmd.ProcessState, nil
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *setnsProcess) pid() int {
|
2015-02-23 17:26:43 +08:00
|
|
|
return p.cmd.Process.Pid
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type initProcess struct {
|
|
|
|
cmd *exec.Cmd
|
|
|
|
parentPipe *os.File
|
|
|
|
childPipe *os.File
|
|
|
|
config *initConfig
|
|
|
|
manager cgroups.Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *initProcess) pid() int {
|
|
|
|
return p.cmd.Process.Pid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *initProcess) start() error {
|
|
|
|
defer p.parentPipe.Close()
|
|
|
|
err := p.cmd.Start()
|
|
|
|
p.childPipe.Close()
|
|
|
|
if err != nil {
|
2015-02-12 09:42:58 +08:00
|
|
|
return newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
2015-03-19 11:22:21 +08:00
|
|
|
// Save the standard descriptor names before the container process
|
|
|
|
// can potentially move them (e.g., via dup2()). If we don't do this now,
|
|
|
|
// we won't know at checkpoint time which file descriptor to look up.
|
|
|
|
if err = p.saveStdPipes(); err != nil {
|
|
|
|
return newSystemError(err)
|
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
// Do this before syncing with child so that no children
|
|
|
|
// can escape the cgroup
|
|
|
|
if err := p.manager.Apply(p.pid()); err != nil {
|
2015-02-12 09:42:58 +08:00
|
|
|
return newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
// TODO: should not be the responsibility to call here
|
|
|
|
p.manager.Destroy()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err := p.createNetworkInterfaces(); err != nil {
|
2015-02-12 09:42:58 +08:00
|
|
|
return newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
if err := p.sendConfig(); err != nil {
|
2015-02-12 09:42:58 +08:00
|
|
|
return newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
// wait for the child process to fully complete and receive an error message
|
|
|
|
// if one was encoutered
|
2015-02-18 13:50:43 +08:00
|
|
|
var ierr *genericError
|
2015-02-07 13:12:27 +08:00
|
|
|
if err := json.NewDecoder(p.parentPipe).Decode(&ierr); err != nil && err != io.EOF {
|
2015-02-12 09:42:58 +08:00
|
|
|
return newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
if ierr != nil {
|
2015-02-12 09:42:58 +08:00
|
|
|
return newSystemError(ierr)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *initProcess) wait() (*os.ProcessState, error) {
|
2015-02-23 17:26:43 +08:00
|
|
|
err := p.cmd.Wait()
|
2015-02-07 13:12:27 +08:00
|
|
|
if err != nil {
|
2015-02-26 03:45:53 +08:00
|
|
|
return p.cmd.ProcessState, err
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
// we should kill all processes in cgroup when init is died if we use host PID namespace
|
|
|
|
if p.cmd.SysProcAttr.Cloneflags&syscall.CLONE_NEWPID == 0 {
|
2015-02-12 08:45:23 +08:00
|
|
|
killCgroupProcesses(p.manager)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
return p.cmd.ProcessState, nil
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *initProcess) terminate() error {
|
|
|
|
if p.cmd.Process == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := p.cmd.Process.Kill()
|
|
|
|
if _, werr := p.wait(); err == nil {
|
|
|
|
err = werr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *initProcess) startTime() (string, error) {
|
|
|
|
return system.GetProcessStartTime(p.pid())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *initProcess) sendConfig() error {
|
|
|
|
// send the state to the container's init process then shutdown writes for the parent
|
|
|
|
if err := json.NewEncoder(p.parentPipe).Encode(p.config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// shutdown writes for the parent side of the pipe
|
|
|
|
return syscall.Shutdown(int(p.parentPipe.Fd()), syscall.SHUT_WR)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *initProcess) createNetworkInterfaces() error {
|
|
|
|
for _, config := range p.config.Config.Networks {
|
2015-02-10 07:16:27 +08:00
|
|
|
strategy, err := getStrategy(config.Type)
|
2015-02-07 13:12:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-11 03:51:45 +08:00
|
|
|
n := &network{
|
|
|
|
Network: *config,
|
|
|
|
}
|
|
|
|
if err := strategy.create(n, p.pid()); err != nil {
|
2015-02-07 13:12:27 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-02-11 03:51:45 +08:00
|
|
|
p.config.Networks = append(p.config.Networks, n)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-28 01:50:32 +08:00
|
|
|
func (p *initProcess) signal(sig os.Signal) error {
|
|
|
|
s, ok := sig.(syscall.Signal)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("os: unsupported signal type")
|
|
|
|
}
|
|
|
|
return syscall.Kill(p.cmd.Process.Pid, s)
|
2015-02-07 14:33:10 +08:00
|
|
|
}
|
2015-03-19 11:22:21 +08:00
|
|
|
|
|
|
|
// Save process's std{in,out,err} file names as these will be
|
|
|
|
// removed if/when the container is checkpointed. We will need
|
|
|
|
// this info to restore the container.
|
|
|
|
func (p *initProcess) saveStdPipes() error {
|
|
|
|
dirPath := filepath.Join("/proc", strconv.Itoa(p.pid()), "/fd")
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
f := filepath.Join(dirPath, strconv.Itoa(i))
|
|
|
|
target, err := os.Readlink(f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.config.Config.StdFds[i] = target
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|