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:
Michael Crosby
2014-06-24 16:54:50 -07:00
parent e49f5f4788
commit 81e5a3f7a7
6 changed files with 64 additions and 53 deletions
+48
View File
@@ -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"))
}