restored_process.go 2.7 KB

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