restored_process.go 2.6 KB

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