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