mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-10 21:53:57 +08:00
Replace pid and started file with State type
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@docker.com> (github: crosbymichael)
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package libcontainer
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// State represents a running container's state
|
||||
type State struct {
|
||||
// Pid1 is the process id for the container's pid 1 in it's parent namespace
|
||||
Pid1 int `json:"pid1,omitempty"`
|
||||
// Pid1StartTime is the process start time for the container's pid 1
|
||||
Pid1StartTime string `json:"pid1_start_time,omitempty"`
|
||||
}
|
||||
|
||||
// WriteState writes the container's runtime state to a state.json file
|
||||
// in the specified path
|
||||
func WriteState(basePath string, state *State) error {
|
||||
f, err := os.Create(filepath.Join(basePath, "state.json"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return json.NewEncoder(f).Encode(state)
|
||||
}
|
||||
|
||||
// LoadState reads the state.json file for a running container
|
||||
func LoadState(basePath string) (*State, error) {
|
||||
f, err := os.Open(filepath.Join(basePath, "state.json"))
|
||||
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 {
|
||||
return os.Remove(filepath.Join(basePath, "state.json"))
|
||||
}
|
||||
Reference in New Issue
Block a user