2014-06-25 07:54:50 +08:00
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-06-25 07:43:00 +08:00
|
|
|
|
|
|
|
"github.com/docker/libcontainer/network"
|
2014-06-25 07:54:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// State represents a running container's state
|
|
|
|
type State struct {
|
2014-06-26 02:42:08 +08:00
|
|
|
// InitPid is the init process id in the parent namespace
|
|
|
|
InitPid int `json:"init_pid,omitempty"`
|
2014-07-09 01:17:05 +08:00
|
|
|
|
2014-06-26 02:42:08 +08:00
|
|
|
// InitStartTime is the init process start time
|
|
|
|
InitStartTime string `json:"init_start_time,omitempty"`
|
2014-07-09 01:17:05 +08:00
|
|
|
|
2014-06-25 07:43:00 +08:00
|
|
|
// Network runtime state.
|
|
|
|
NetworkState network.NetworkState `json:"network_state,omitempty"`
|
2014-08-13 14:18:55 +08:00
|
|
|
|
2014-08-14 07:25:18 +08:00
|
|
|
// Path to all the cgroups setup for a container. Key is cgroup subsystem name.
|
|
|
|
CgroupPaths map[string]string `json:"cgroup_paths,omitempty"`
|
2014-06-25 07:54:50 +08:00
|
|
|
}
|
|
|
|
|
2014-07-09 01:17:05 +08:00
|
|
|
// The running state of the container.
|
|
|
|
type RunState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// The name of the runtime state file
|
|
|
|
stateFile = "state.json"
|
|
|
|
|
|
|
|
// The container exists and is running.
|
|
|
|
Running RunState = iota
|
|
|
|
|
|
|
|
// The container exists, it is in the process of being paused.
|
|
|
|
Pausing
|
|
|
|
|
|
|
|
// The container exists, but all its processes are paused.
|
|
|
|
Paused
|
|
|
|
|
|
|
|
// The container does not exist.
|
|
|
|
Destroyed
|
|
|
|
)
|
2014-06-25 07:43:00 +08:00
|
|
|
|
2014-06-25 08:17:41 +08:00
|
|
|
// SaveState writes the container's runtime state to a state.json file
|
2014-06-25 07:54:50 +08:00
|
|
|
// in the specified path
|
2014-06-25 08:17:41 +08:00
|
|
|
func SaveState(basePath string, state *State) error {
|
2014-06-25 07:43:00 +08:00
|
|
|
f, err := os.Create(filepath.Join(basePath, stateFile))
|
2014-06-25 07:54:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return json.NewEncoder(f).Encode(state)
|
|
|
|
}
|
|
|
|
|
2014-06-25 08:17:41 +08:00
|
|
|
// GetState reads the state.json file for a running container
|
|
|
|
func GetState(basePath string) (*State, error) {
|
2014-06-25 07:43:00 +08:00
|
|
|
f, err := os.Open(filepath.Join(basePath, stateFile))
|
2014-06-25 07:54:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var state *State
|
|
|
|
if err := json.NewDecoder(f).Decode(&state); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteState deletes the state.json file
|
|
|
|
func DeleteState(basePath string) error {
|
2014-06-25 07:43:00 +08:00
|
|
|
return os.Remove(filepath.Join(basePath, stateFile))
|
2014-06-25 07:54:50 +08:00
|
|
|
}
|