restored_process.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. stat, err := system.Stat(pid)
  17. if err != nil {
  18. return nil, err
  19. }
  20. return &restoredProcess{
  21. proc: proc,
  22. processStartTime: stat.StartTime,
  23. fds: fds,
  24. }, nil
  25. }
  26. type restoredProcess struct {
  27. proc *os.Process
  28. processStartTime uint64
  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() (uint64, 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. func (p *restoredProcess) forwardChildLogs() {
  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 newGenericError(fmt.Errorf("restored process cannot be started"), SystemError)
  77. }
  78. func (p *nonChildProcess) pid() int {
  79. return p.processPid
  80. }
  81. func (p *nonChildProcess) terminate() error {
  82. return newGenericError(fmt.Errorf("restored process cannot be terminated"), SystemError)
  83. }
  84. func (p *nonChildProcess) wait() (*os.ProcessState, error) {
  85. return nil, newGenericError(fmt.Errorf("restored process cannot be waited on"), SystemError)
  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() {
  104. }