exec_state.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // +build !windows
  2. /*
  3. Copyright The containerd Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package proc
  15. import (
  16. "context"
  17. "github.com/containerd/console"
  18. "github.com/pkg/errors"
  19. )
  20. type execCreatedState struct {
  21. p *execProcess
  22. }
  23. func (s *execCreatedState) transition(name string) error {
  24. switch name {
  25. case "running":
  26. s.p.State = &execRunningState{p: s.p}
  27. case "stopped":
  28. s.p.State = &execStoppedState{p: s.p}
  29. case "deleted":
  30. s.p.State = &deletedState{}
  31. default:
  32. return errors.Errorf("invalid state transition %q to %q", stateName(s), name)
  33. }
  34. return nil
  35. }
  36. func (s *execCreatedState) Resize(ws console.WinSize) error {
  37. s.p.mu.Lock()
  38. defer s.p.mu.Unlock()
  39. return s.p.resize(ws)
  40. }
  41. func (s *execCreatedState) Start(ctx context.Context) error {
  42. s.p.mu.Lock()
  43. defer s.p.mu.Unlock()
  44. if err := s.p.start(ctx); err != nil {
  45. return err
  46. }
  47. return s.transition("running")
  48. }
  49. func (s *execCreatedState) Delete(ctx context.Context) error {
  50. s.p.mu.Lock()
  51. defer s.p.mu.Unlock()
  52. if err := s.p.delete(ctx); err != nil {
  53. return err
  54. }
  55. return s.transition("deleted")
  56. }
  57. func (s *execCreatedState) Kill(ctx context.Context, sig uint32, all bool) error {
  58. s.p.mu.Lock()
  59. defer s.p.mu.Unlock()
  60. return s.p.kill(ctx, sig, all)
  61. }
  62. func (s *execCreatedState) SetExited(status int) {
  63. s.p.mu.Lock()
  64. defer s.p.mu.Unlock()
  65. s.p.setExited(status)
  66. if err := s.transition("stopped"); err != nil {
  67. panic(err)
  68. }
  69. }
  70. type execRunningState struct {
  71. p *execProcess
  72. }
  73. func (s *execRunningState) transition(name string) error {
  74. switch name {
  75. case "stopped":
  76. s.p.State = &execStoppedState{p: s.p}
  77. default:
  78. return errors.Errorf("invalid state transition %q to %q", stateName(s), name)
  79. }
  80. return nil
  81. }
  82. func (s *execRunningState) Resize(ws console.WinSize) error {
  83. s.p.mu.Lock()
  84. defer s.p.mu.Unlock()
  85. return s.p.resize(ws)
  86. }
  87. func (s *execRunningState) Start(ctx context.Context) error {
  88. s.p.mu.Lock()
  89. defer s.p.mu.Unlock()
  90. return errors.Errorf("cannot start a running process")
  91. }
  92. func (s *execRunningState) Delete(ctx context.Context) error {
  93. s.p.mu.Lock()
  94. defer s.p.mu.Unlock()
  95. return errors.Errorf("cannot delete a running process")
  96. }
  97. func (s *execRunningState) Kill(ctx context.Context, sig uint32, all bool) error {
  98. s.p.mu.Lock()
  99. defer s.p.mu.Unlock()
  100. return s.p.kill(ctx, sig, all)
  101. }
  102. func (s *execRunningState) SetExited(status int) {
  103. s.p.mu.Lock()
  104. defer s.p.mu.Unlock()
  105. s.p.setExited(status)
  106. if err := s.transition("stopped"); err != nil {
  107. panic(err)
  108. }
  109. }
  110. type execStoppedState struct {
  111. p *execProcess
  112. }
  113. func (s *execStoppedState) transition(name string) error {
  114. switch name {
  115. case "deleted":
  116. s.p.State = &deletedState{}
  117. default:
  118. return errors.Errorf("invalid state transition %q to %q", stateName(s), name)
  119. }
  120. return nil
  121. }
  122. func (s *execStoppedState) Resize(ws console.WinSize) error {
  123. s.p.mu.Lock()
  124. defer s.p.mu.Unlock()
  125. return errors.Errorf("cannot resize a stopped container")
  126. }
  127. func (s *execStoppedState) Start(ctx context.Context) error {
  128. s.p.mu.Lock()
  129. defer s.p.mu.Unlock()
  130. return errors.Errorf("cannot start a stopped process")
  131. }
  132. func (s *execStoppedState) Delete(ctx context.Context) error {
  133. s.p.mu.Lock()
  134. defer s.p.mu.Unlock()
  135. if err := s.p.delete(ctx); err != nil {
  136. return err
  137. }
  138. return s.transition("deleted")
  139. }
  140. func (s *execStoppedState) Kill(ctx context.Context, sig uint32, all bool) error {
  141. s.p.mu.Lock()
  142. defer s.p.mu.Unlock()
  143. return s.p.kill(ctx, sig, all)
  144. }
  145. func (s *execStoppedState) SetExited(status int) {
  146. // no op
  147. }