github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/runc/libcontainer/restored_process.go (about) 1 // +build linux 2 3 package libcontainer 4 5 import ( 6 "fmt" 7 "os" 8 9 "github.com/opencontainers/runc/libcontainer/system" 10 ) 11 12 func newRestoredProcess(pid int, fds []string) (*restoredProcess, error) { 13 var ( 14 err error 15 ) 16 proc, err := os.FindProcess(pid) 17 if err != nil { 18 return nil, err 19 } 20 started, err := system.GetProcessStartTime(pid) 21 if err != nil { 22 return nil, err 23 } 24 return &restoredProcess{ 25 proc: proc, 26 processStartTime: started, 27 fds: fds, 28 }, nil 29 } 30 31 type restoredProcess struct { 32 proc *os.Process 33 processStartTime string 34 fds []string 35 } 36 37 func (p *restoredProcess) start() error { 38 return newGenericError(fmt.Errorf("restored process cannot be started"), SystemError) 39 } 40 41 func (p *restoredProcess) pid() int { 42 return p.proc.Pid 43 } 44 45 func (p *restoredProcess) terminate() error { 46 err := p.proc.Kill() 47 if _, werr := p.wait(); err == nil { 48 err = werr 49 } 50 return err 51 } 52 53 func (p *restoredProcess) wait() (*os.ProcessState, error) { 54 // TODO: how do we wait on the actual process? 55 // maybe use --exec-cmd in criu 56 st, err := p.proc.Wait() 57 if err != nil { 58 return nil, err 59 } 60 return st, nil 61 } 62 63 func (p *restoredProcess) startTime() (string, error) { 64 return p.processStartTime, nil 65 } 66 67 func (p *restoredProcess) signal(s os.Signal) error { 68 return p.proc.Signal(s) 69 } 70 71 func (p *restoredProcess) externalDescriptors() []string { 72 return p.fds 73 } 74 75 func (p *restoredProcess) setExternalDescriptors(newFds []string) { 76 p.fds = newFds 77 } 78 79 // nonChildProcess represents a process where the calling process is not 80 // the parent process. This process is created when a factory loads a container from 81 // a persisted state. 82 type nonChildProcess struct { 83 processPid int 84 processStartTime string 85 fds []string 86 } 87 88 func (p *nonChildProcess) start() error { 89 return newGenericError(fmt.Errorf("restored process cannot be started"), SystemError) 90 } 91 92 func (p *nonChildProcess) pid() int { 93 return p.processPid 94 } 95 96 func (p *nonChildProcess) terminate() error { 97 return newGenericError(fmt.Errorf("restored process cannot be terminated"), SystemError) 98 } 99 100 func (p *nonChildProcess) wait() (*os.ProcessState, error) { 101 return nil, newGenericError(fmt.Errorf("restored process cannot be waited on"), SystemError) 102 } 103 104 func (p *nonChildProcess) startTime() (string, error) { 105 return p.processStartTime, nil 106 } 107 108 func (p *nonChildProcess) signal(s os.Signal) error { 109 proc, err := os.FindProcess(p.processPid) 110 if err != nil { 111 return err 112 } 113 return proc.Signal(s) 114 } 115 116 func (p *nonChildProcess) externalDescriptors() []string { 117 return p.fds 118 } 119 120 func (p *nonChildProcess) setExternalDescriptors(newFds []string) { 121 p.fds = newFds 122 }