2015-03-13 12:45:43 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/system"
|
2015-03-13 12:45:43 +08:00
|
|
|
)
|
|
|
|
|
2015-04-29 19:52:17 +08:00
|
|
|
func newRestoredProcess(pid int, fds []string) (*restoredProcess, error) {
|
2015-03-13 12:45:43 +08:00
|
|
|
var (
|
2015-03-26 19:20:59 +08:00
|
|
|
err error
|
2015-03-13 12:45:43 +08:00
|
|
|
)
|
|
|
|
proc, err := os.FindProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-15 06:38:45 +08:00
|
|
|
stat, err := system.Stat(pid)
|
2015-03-13 12:45:43 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &restoredProcess{
|
|
|
|
proc: proc,
|
2017-06-15 06:38:45 +08:00
|
|
|
processStartTime: stat.StartTime,
|
2015-04-29 04:54:03 +08:00
|
|
|
fds: fds,
|
2015-03-13 12:45:43 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type restoredProcess struct {
|
|
|
|
proc *os.Process
|
2017-06-15 06:38:45 +08:00
|
|
|
processStartTime uint64
|
2015-04-29 19:52:17 +08:00
|
|
|
fds []string
|
2015-03-13 12:45:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *restoredProcess) start() error {
|
|
|
|
return newGenericError(fmt.Errorf("restored process cannot be started"), SystemError)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *restoredProcess) pid() int {
|
|
|
|
return p.proc.Pid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *restoredProcess) terminate() error {
|
|
|
|
err := p.proc.Kill()
|
|
|
|
if _, werr := p.wait(); err == nil {
|
|
|
|
err = werr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *restoredProcess) wait() (*os.ProcessState, error) {
|
|
|
|
// TODO: how do we wait on the actual process?
|
|
|
|
// maybe use --exec-cmd in criu
|
2015-03-26 19:17:26 +08:00
|
|
|
st, err := p.proc.Wait()
|
|
|
|
if err != nil {
|
2015-03-13 12:45:43 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-26 19:17:26 +08:00
|
|
|
return st, nil
|
2015-03-13 12:45:43 +08:00
|
|
|
}
|
|
|
|
|
2017-06-15 06:38:45 +08:00
|
|
|
func (p *restoredProcess) startTime() (uint64, error) {
|
2015-03-13 12:45:43 +08:00
|
|
|
return p.processStartTime, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *restoredProcess) signal(s os.Signal) error {
|
|
|
|
return p.proc.Signal(s)
|
|
|
|
}
|
|
|
|
|
2015-04-29 19:52:17 +08:00
|
|
|
func (p *restoredProcess) externalDescriptors() []string {
|
2015-04-29 04:54:03 +08:00
|
|
|
return p.fds
|
|
|
|
}
|
|
|
|
|
2015-04-29 19:52:17 +08:00
|
|
|
func (p *restoredProcess) setExternalDescriptors(newFds []string) {
|
2015-04-29 04:54:03 +08:00
|
|
|
p.fds = newFds
|
2015-04-29 03:13:57 +08:00
|
|
|
}
|
|
|
|
|
2015-03-13 12:45:43 +08:00
|
|
|
// nonChildProcess represents a process where the calling process is not
|
|
|
|
// the parent process. This process is created when a factory loads a container from
|
|
|
|
// a persisted state.
|
|
|
|
type nonChildProcess struct {
|
|
|
|
processPid int
|
2017-06-15 06:38:45 +08:00
|
|
|
processStartTime uint64
|
2015-04-29 19:52:17 +08:00
|
|
|
fds []string
|
2015-03-13 12:45:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *nonChildProcess) start() error {
|
|
|
|
return newGenericError(fmt.Errorf("restored process cannot be started"), SystemError)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *nonChildProcess) pid() int {
|
|
|
|
return p.processPid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *nonChildProcess) terminate() error {
|
|
|
|
return newGenericError(fmt.Errorf("restored process cannot be terminated"), SystemError)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *nonChildProcess) wait() (*os.ProcessState, error) {
|
|
|
|
return nil, newGenericError(fmt.Errorf("restored process cannot be waited on"), SystemError)
|
|
|
|
}
|
|
|
|
|
2017-06-15 06:38:45 +08:00
|
|
|
func (p *nonChildProcess) startTime() (uint64, error) {
|
2015-03-13 12:45:43 +08:00
|
|
|
return p.processStartTime, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *nonChildProcess) signal(s os.Signal) error {
|
2015-08-04 07:48:19 +08:00
|
|
|
proc, err := os.FindProcess(p.processPid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return proc.Signal(s)
|
2015-03-13 12:45:43 +08:00
|
|
|
}
|
2015-04-29 03:13:57 +08:00
|
|
|
|
2015-04-29 19:52:17 +08:00
|
|
|
func (p *nonChildProcess) externalDescriptors() []string {
|
2015-04-29 04:54:03 +08:00
|
|
|
return p.fds
|
|
|
|
}
|
|
|
|
|
2015-04-29 19:52:17 +08:00
|
|
|
func (p *nonChildProcess) setExternalDescriptors(newFds []string) {
|
2015-04-29 04:54:03 +08:00
|
|
|
p.fds = newFds
|
2015-04-29 03:13:57 +08:00
|
|
|
}
|