process.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "io"
  18. "sync"
  19. "time"
  20. "github.com/containerd/console"
  21. "github.com/pkg/errors"
  22. )
  23. // RuncRoot is the path to the root runc state directory
  24. const RuncRoot = "/run/containerd/runc"
  25. // Stdio of a process
  26. type Stdio struct {
  27. Stdin string
  28. Stdout string
  29. Stderr string
  30. Terminal bool
  31. }
  32. // IsNull returns true if the stdio is not defined
  33. func (s Stdio) IsNull() bool {
  34. return s.Stdin == "" && s.Stdout == "" && s.Stderr == ""
  35. }
  36. // Process on a linux system
  37. type Process interface {
  38. State
  39. // ID returns the id for the process
  40. ID() string
  41. // Pid returns the pid for the process
  42. Pid() int
  43. // ExitStatus returns the exit status
  44. ExitStatus() int
  45. // ExitedAt is the time the process exited
  46. ExitedAt() time.Time
  47. // Stdin returns the process STDIN
  48. Stdin() io.Closer
  49. // Stdio returns io information for the container
  50. Stdio() Stdio
  51. // Status returns the process status
  52. Status(context.Context) (string, error)
  53. // Wait blocks until the process has exited
  54. Wait()
  55. }
  56. // State of a process
  57. type State interface {
  58. // Resize resizes the process console
  59. Resize(ws console.WinSize) error
  60. // Start execution of the process
  61. Start(context.Context) error
  62. // Delete deletes the process and its resourcess
  63. Delete(context.Context) error
  64. // Kill kills the process
  65. Kill(context.Context, uint32, bool) error
  66. // SetExited sets the exit status for the process
  67. SetExited(status int)
  68. }
  69. func stateName(v interface{}) string {
  70. switch v.(type) {
  71. case *runningState, *execRunningState:
  72. return "running"
  73. case *createdState, *execCreatedState, *createdCheckpointState:
  74. return "created"
  75. case *pausedState:
  76. return "paused"
  77. case *deletedState:
  78. return "deleted"
  79. case *stoppedState:
  80. return "stopped"
  81. }
  82. panic(errors.Errorf("invalid state %v", v))
  83. }
  84. // Platform handles platform-specific behavior that may differs across
  85. // platform implementations
  86. type Platform interface {
  87. CopyConsole(ctx context.Context, console console.Console, stdin, stdout, stderr string,
  88. wg, cwg *sync.WaitGroup) (console.Console, error)
  89. ShutdownConsole(ctx context.Context, console console.Console) error
  90. Close() error
  91. }