From 3d59a7fe8bcc39d968783f6d380d8cc53f842ea3 Mon Sep 17 00:00:00 2001 From: boucher Date: Tue, 28 Apr 2015 13:54:03 -0700 Subject: [PATCH] Refactor saving fds slightly, set the restored process fds correctly. Docker-DCO-1.1-Signed-off-by: Ross Boucher (github: boucher) --- container_linux.go | 10 +++++++++- process_linux.go | 41 +++++++++++++++++++++++++++++++---------- restored_process.go | 17 ++++++++++++++--- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/container_linux.go b/container_linux.go index c99de565..c70663e5 100644 --- a/container_linux.go +++ b/container_linux.go @@ -558,6 +558,14 @@ func (c *linuxContainer) criuSwrk(process *Process, req *criurpc.CriuReq, opts * log.Warn(st.String()) }() + if process != nil { + fds, err := getPipeFds(cmd.Process.Pid) + if err != nil { + return err + } + c.initProcess.setStdFds(fds) + } + data, err := proto.Marshal(req) if err != nil { return err @@ -692,7 +700,7 @@ func (c *linuxContainer) criuNotifications(resp *criurpc.CriuResp, process *Proc case notify.GetScript() == "post-restore": pid := notify.GetPid() - r, err := newRestoredProcess(int(pid)) + r, err := newRestoredProcess(int(pid), c.initProcess.stdFds()) if err != nil { return err } diff --git a/process_linux.go b/process_linux.go index 5924161f..b37e8830 100644 --- a/process_linux.go +++ b/process_linux.go @@ -35,6 +35,8 @@ type parentProcess interface { signal(os.Signal) error stdFds() [3]string + + setStdFds(fds [3]string) } type setnsProcess struct { @@ -43,6 +45,7 @@ type setnsProcess struct { childPipe *os.File cgroupPaths map[string]string config *initConfig + fds [3]string } func (p *setnsProcess) startTime() (string, error) { @@ -147,7 +150,11 @@ func (p *setnsProcess) pid() int { } func (p *setnsProcess) stdFds() [3]string { - return [3]string{"", "", ""} + return p.fds +} + +func (p *setnsProcess) setStdFds(newFds [3]string) { + p.fds = newFds } type initProcess struct { @@ -178,16 +185,11 @@ func (p *initProcess) start() error { // 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. - // we need this info to restore the container - 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 newSystemError(err) - } - p.fds[i] = target + fds, err := getPipeFds(p.pid()) + if err != nil { + return newSystemError(err) } + p.setStdFds(fds); // Do this before syncing with child so that no children // can escape the cgroup @@ -278,3 +280,22 @@ func (p *initProcess) signal(sig os.Signal) error { } return syscall.Kill(p.cmd.Process.Pid, s) } + +func (p *initProcess) setStdFds(newFds [3]string) { + p.fds = newFds +} + +func getPipeFds(pid int) ([3]string, error) { + var fds [3]string; + + dirPath := filepath.Join("/proc", strconv.Itoa(pid), "/fd") + for i := 0; i < 3; i++ { + f := filepath.Join(dirPath, strconv.Itoa(i)) + target, err := os.Readlink(f) + if err != nil { + return fds, err + } + fds[i] = target + } + return fds, nil +} diff --git a/restored_process.go b/restored_process.go index 12408d77..ebf96d9f 100644 --- a/restored_process.go +++ b/restored_process.go @@ -9,7 +9,7 @@ import ( "github.com/docker/libcontainer/system" ) -func newRestoredProcess(pid int) (*restoredProcess, error) { +func newRestoredProcess(pid int, fds [3]string) (*restoredProcess, error) { var ( err error ) @@ -24,12 +24,14 @@ func newRestoredProcess(pid int) (*restoredProcess, error) { return &restoredProcess{ proc: proc, processStartTime: started, + fds: fds, }, nil } type restoredProcess struct { proc *os.Process processStartTime string + fds [3]string } func (p *restoredProcess) start() error { @@ -67,7 +69,11 @@ func (p *restoredProcess) signal(s os.Signal) error { } func (p *restoredProcess) stdFds() [3]string { - return [3]string{"", "", ""} + return p.fds +} + +func (p *restoredProcess) setStdFds(newFds [3]string) { + p.fds = newFds } // nonChildProcess represents a process where the calling process is not @@ -76,6 +82,7 @@ func (p *restoredProcess) stdFds() [3]string { type nonChildProcess struct { processPid int processStartTime string + fds [3]string } func (p *nonChildProcess) start() error { @@ -103,5 +110,9 @@ func (p *nonChildProcess) signal(s os.Signal) error { } func (p *nonChildProcess) stdFds() [3]string { - return [3]string{"", "", ""} + return p.fds +} + +func (p *nonChildProcess) setStdFds(newFds [3]string) { + p.fds = newFds }