container.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Package libcontainer provides a native Go implementation for creating containers
  2. // with namespaces, cgroups, capabilities, and filesystem access controls.
  3. // It allows you to manage the lifecycle of the container performing additional operations
  4. // after the container is created.
  5. package libcontainer
  6. import (
  7. "os"
  8. "time"
  9. "github.com/opencontainers/runc/libcontainer/configs"
  10. "github.com/opencontainers/runtime-spec/specs-go"
  11. )
  12. // Status is the status of a container.
  13. type Status int
  14. const (
  15. // Created is the status that denotes the container exists but has not been run yet.
  16. Created Status = iota
  17. // Running is the status that denotes the container exists and is running.
  18. Running
  19. // Pausing is the status that denotes the container exists, it is in the process of being paused.
  20. Pausing
  21. // Paused is the status that denotes the container exists, but all its processes are paused.
  22. Paused
  23. // Stopped is the status that denotes the container does not have a created or running process.
  24. Stopped
  25. )
  26. func (s Status) String() string {
  27. switch s {
  28. case Created:
  29. return "created"
  30. case Running:
  31. return "running"
  32. case Pausing:
  33. return "pausing"
  34. case Paused:
  35. return "paused"
  36. case Stopped:
  37. return "stopped"
  38. default:
  39. return "unknown"
  40. }
  41. }
  42. // BaseState represents the platform agnostic pieces relating to a
  43. // running container's state
  44. type BaseState struct {
  45. // ID is the container ID.
  46. ID string `json:"id"`
  47. // InitProcessPid is the init process id in the parent namespace.
  48. InitProcessPid int `json:"init_process_pid"`
  49. // InitProcessStartTime is the init process start time in clock cycles since boot time.
  50. InitProcessStartTime uint64 `json:"init_process_start"`
  51. // Created is the unix timestamp for the creation time of the container in UTC
  52. Created time.Time `json:"created"`
  53. // Config is the container's configuration.
  54. Config configs.Config `json:"config"`
  55. }
  56. // BaseContainer is a libcontainer container object.
  57. //
  58. // Each container is thread-safe within the same process. Since a container can
  59. // be destroyed by a separate process, any function may return that the container
  60. // was not found. BaseContainer includes methods that are platform agnostic.
  61. type BaseContainer interface {
  62. // Returns the ID of the container
  63. ID() string
  64. // Returns the current status of the container.
  65. //
  66. // errors:
  67. // ContainerNotExists - Container no longer exists,
  68. // Systemerror - System error.
  69. Status() (Status, error)
  70. // State returns the current container's state information.
  71. //
  72. // errors:
  73. // SystemError - System error.
  74. State() (*State, error)
  75. // OCIState returns the current container's state information.
  76. //
  77. // errors:
  78. // SystemError - System error.
  79. OCIState() (*specs.State, error)
  80. // Returns the current config of the container.
  81. Config() configs.Config
  82. // Returns the PIDs inside this container. The PIDs are in the namespace of the calling process.
  83. //
  84. // errors:
  85. // ContainerNotExists - Container no longer exists,
  86. // Systemerror - System error.
  87. //
  88. // Some of the returned PIDs may no longer refer to processes in the Container, unless
  89. // the Container state is PAUSED in which case every PID in the slice is valid.
  90. Processes() ([]int, error)
  91. // Returns statistics for the container.
  92. //
  93. // errors:
  94. // ContainerNotExists - Container no longer exists,
  95. // Systemerror - System error.
  96. Stats() (*Stats, error)
  97. // Set resources of container as configured
  98. //
  99. // We can use this to change resources when containers are running.
  100. //
  101. // errors:
  102. // SystemError - System error.
  103. Set(config configs.Config) error
  104. // Start a process inside the container. Returns error if process fails to
  105. // start. You can track process lifecycle with passed Process structure.
  106. //
  107. // errors:
  108. // ContainerNotExists - Container no longer exists,
  109. // ConfigInvalid - config is invalid,
  110. // ContainerPaused - Container is paused,
  111. // SystemError - System error.
  112. Start(process *Process) (err error)
  113. // Run immediately starts the process inside the container. Returns error if process
  114. // fails to start. It does not block waiting for the exec fifo after start returns but
  115. // opens the fifo after start returns.
  116. //
  117. // errors:
  118. // ContainerNotExists - Container no longer exists,
  119. // ConfigInvalid - config is invalid,
  120. // ContainerPaused - Container is paused,
  121. // SystemError - System error.
  122. Run(process *Process) (err error)
  123. // Destroys the container, if its in a valid state, after killing any
  124. // remaining running processes.
  125. //
  126. // Any event registrations are removed before the container is destroyed.
  127. // No error is returned if the container is already destroyed.
  128. //
  129. // Running containers must first be stopped using Signal(..).
  130. // Paused containers must first be resumed using Resume(..).
  131. //
  132. // errors:
  133. // ContainerNotStopped - Container is still running,
  134. // ContainerPaused - Container is paused,
  135. // SystemError - System error.
  136. Destroy() error
  137. // Signal sends the provided signal code to the container's initial process.
  138. //
  139. // If all is specified the signal is sent to all processes in the container
  140. // including the initial process.
  141. //
  142. // errors:
  143. // SystemError - System error.
  144. Signal(s os.Signal, all bool) error
  145. // Exec signals the container to exec the users process at the end of the init.
  146. //
  147. // errors:
  148. // SystemError - System error.
  149. Exec() error
  150. }