summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@gmail.com>2018-02-27 22:52:28 -0500
committerAtomic Bot <atomic-devel@projectatomic.io>2018-03-01 21:17:51 +0000
commitfa5f99effa7dd2ef9fbd5287b6225590b28713d1 (patch)
tree240a4696de8a499ed7489b67fcbdf4674ca87d19
parent83d7ae6506f7aaf1f7d543412a148b4bf4cd6657 (diff)
downloadpodman-fa5f99effa7dd2ef9fbd5287b6225590b28713d1.tar.gz
podman-fa5f99effa7dd2ef9fbd5287b6225590b28713d1.tar.bz2
podman-fa5f99effa7dd2ef9fbd5287b6225590b28713d1.zip
Convert exec session tracking to use a dedicated struct
This will behave better if we need to add anything to it at a later date - we can add fields to the struct without breaking existing BoltDB databases. Signed-off-by: Matthew Heon <matthew.heon@gmail.com> Closes: #412 Approved by: baude
-rw-r--r--libpod/boltdb_state.go2
-rw-r--r--libpod/container.go45
-rw-r--r--libpod/container_api.go8
-rw-r--r--libpod/container_ffjson.go400
-rw-r--r--libpod/oci.go3
-rw-r--r--libpod/sql_state.go2
-rw-r--r--libpod/sql_state_internal.go2
-rw-r--r--libpod/test_common.go25
8 files changed, 439 insertions, 48 deletions
diff --git a/libpod/boltdb_state.go b/libpod/boltdb_state.go
index f5d0afc4a..d4a6ea4cb 100644
--- a/libpod/boltdb_state.go
+++ b/libpod/boltdb_state.go
@@ -142,7 +142,7 @@ func (s *BoltState) Refresh() error {
state.State = ContainerStateConfigured
state.IPAddress = ""
state.SubnetMask = ""
- state.ExecSessions = make(map[string]int)
+ state.ExecSessions = make(map[string]*ExecSession)
newStateBytes, err := json.Marshal(state)
if err != nil {
diff --git a/libpod/container.go b/libpod/container.go
index 83a45d379..8dee0ae2e 100644
--- a/libpod/container.go
+++ b/libpod/container.go
@@ -143,7 +143,14 @@ type containerState struct {
SubnetMask string `json:"subnetMask"`
// ExecSessions contains active exec sessions for container
// Exec session ID is mapped to PID of exec process
- ExecSessions map[string]int `json:"execSessions,omitempty"`
+ ExecSessions map[string]*ExecSession `json:"execSessions,omitempty"`
+}
+
+// ExecSession contains information on an active exec session
+type ExecSession struct {
+ ID string `json:"id"`
+ Command []string `json:"command"`
+ PID int `json:"pid"`
}
// ContainerConfig contains all information that was used to create the
@@ -593,8 +600,27 @@ func (c *Container) PID() (int, error) {
}
// ExecSessions retrieves active exec sessions running in the container
-// The result is a map from session ID to the PID of the exec process
-func (c *Container) ExecSessions() (map[string]int, error) {
+func (c *Container) ExecSessions() ([]string, error) {
+ if !c.locked {
+ c.lock.Lock()
+ defer c.lock.Unlock()
+
+ if err := c.syncContainer(); err != nil {
+ return nil, err
+ }
+ }
+
+ ids := make([]string, 0, len(c.state.ExecSessions))
+ for id := range c.state.ExecSessions {
+ ids = append(ids, id)
+ }
+
+ return ids, nil
+}
+
+// ExecSession retrieves detailed information on a single active exec session in
+// a container
+func (c *Container) ExecSession(id string) (*ExecSession, error) {
if !c.locked {
c.lock.Lock()
defer c.lock.Unlock()
@@ -604,12 +630,17 @@ func (c *Container) ExecSessions() (map[string]int, error) {
}
}
- returnMap := make(map[string]int, len(c.state.ExecSessions))
- for k, v := range c.state.ExecSessions {
- returnMap[k] = v
+ session, ok := c.state.ExecSessions[id]
+ if !ok {
+ return nil, errors.Wrapf(ErrNoSuchCtr, "no exec session with ID %s found in container %s", id, c.ID())
}
- return returnMap, nil
+ returnSession := new(ExecSession)
+ returnSession.ID = session.ID
+ returnSession.Command = session.Command
+ returnSession.PID = session.PID
+
+ return returnSession, nil
}
// Misc Accessors
diff --git a/libpod/container_api.go b/libpod/container_api.go
index e8185ae19..4203e8d44 100644
--- a/libpod/container_api.go
+++ b/libpod/container_api.go
@@ -307,9 +307,13 @@ func (c *Container) Exec(tty, privileged bool, env, cmd []string, user string) e
// We have the PID, add it to state
if c.state.ExecSessions == nil {
- c.state.ExecSessions = make(map[string]int)
+ c.state.ExecSessions = make(map[string]*ExecSession)
}
- c.state.ExecSessions[sessionID] = int(pid)
+ session := new(ExecSession)
+ session.ID = sessionID
+ session.Command = cmd
+ session.PID = int(pid)
+ c.state.ExecSessions[sessionID] = session
if err := c.save(); err != nil {
// Now we have a PID but we can't save it in the DB
// TODO handle this better
diff --git a/libpod/container_ffjson.go b/libpod/container_ffjson.go
index 077e0a635..c34472c1f 100644
--- a/libpod/container_ffjson.go
+++ b/libpod/container_ffjson.go
@@ -2416,6 +2416,361 @@ done:
}
// MarshalJSON marshal bytes to json - template
+func (j *ExecSession) MarshalJSON() ([]byte, error) {
+ var buf fflib.Buffer
+ if j == nil {
+ buf.WriteString("null")
+ return buf.Bytes(), nil
+ }
+ err := j.MarshalJSONBuf(&buf)
+ if err != nil {
+ return nil, err
+ }
+ return buf.Bytes(), nil
+}
+
+// MarshalJSONBuf marshal buff to json - template
+func (j *ExecSession) MarshalJSONBuf(buf fflib.EncodingBuffer) error {
+ if j == nil {
+ buf.WriteString("null")
+ return nil
+ }
+ var err error
+ var obj []byte
+ _ = obj
+ _ = err
+ buf.WriteString(`{"id":`)
+ fflib.WriteJsonString(buf, string(j.ID))
+ buf.WriteString(`,"command":`)
+ if j.Command != nil {
+ buf.WriteString(`[`)
+ for i, v := range j.Command {
+ if i != 0 {
+ buf.WriteString(`,`)
+ }
+ fflib.WriteJsonString(buf, string(v))
+ }
+ buf.WriteString(`]`)
+ } else {
+ buf.WriteString(`null`)
+ }
+ buf.WriteString(`,"pid":`)
+ fflib.FormatBits2(buf, uint64(j.PID), 10, j.PID < 0)
+ buf.WriteByte('}')
+ return nil
+}
+
+const (
+ ffjtExecSessionbase = iota
+ ffjtExecSessionnosuchkey
+
+ ffjtExecSessionID
+
+ ffjtExecSessionCommand
+
+ ffjtExecSessionPID
+)
+
+var ffjKeyExecSessionID = []byte("id")
+
+var ffjKeyExecSessionCommand = []byte("command")
+
+var ffjKeyExecSessionPID = []byte("pid")
+
+// UnmarshalJSON umarshall json - template of ffjson
+func (j *ExecSession) UnmarshalJSON(input []byte) error {
+ fs := fflib.NewFFLexer(input)
+ return j.UnmarshalJSONFFLexer(fs, fflib.FFParse_map_start)
+}
+
+// UnmarshalJSONFFLexer fast json unmarshall - template ffjson
+func (j *ExecSession) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error {
+ var err error
+ currentKey := ffjtExecSessionbase
+ _ = currentKey
+ tok := fflib.FFTok_init
+ wantedTok := fflib.FFTok_init
+
+mainparse:
+ for {
+ tok = fs.Scan()
+ // println(fmt.Sprintf("debug: tok: %v state: %v", tok, state))
+ if tok == fflib.FFTok_error {
+ goto tokerror
+ }
+
+ switch state {
+
+ case fflib.FFParse_map_start:
+ if tok != fflib.FFTok_left_bracket {
+ wantedTok = fflib.FFTok_left_bracket
+ goto wrongtokenerror
+ }
+ state = fflib.FFParse_want_key
+ continue
+
+ case fflib.FFParse_after_value:
+ if tok == fflib.FFTok_comma {
+ state = fflib.FFParse_want_key
+ } else if tok == fflib.FFTok_right_bracket {
+ goto done
+ } else {
+ wantedTok = fflib.FFTok_comma
+ goto wrongtokenerror
+ }
+
+ case fflib.FFParse_want_key:
+ // json {} ended. goto exit. woo.
+ if tok == fflib.FFTok_right_bracket {
+ goto done
+ }
+ if tok != fflib.FFTok_string {
+ wantedTok = fflib.FFTok_string
+ goto wrongtokenerror
+ }
+
+ kn := fs.Output.Bytes()
+ if len(kn) <= 0 {
+ // "" case. hrm.
+ currentKey = ffjtExecSessionnosuchkey
+ state = fflib.FFParse_want_colon
+ goto mainparse
+ } else {
+ switch kn[0] {
+
+ case 'c':
+
+ if bytes.Equal(ffjKeyExecSessionCommand, kn) {
+ currentKey = ffjtExecSessionCommand
+ state = fflib.FFParse_want_colon
+ goto mainparse
+ }
+
+ case 'i':
+
+ if bytes.Equal(ffjKeyExecSessionID, kn) {
+ currentKey = ffjtExecSessionID
+ state = fflib.FFParse_want_colon
+ goto mainparse
+ }
+
+ case 'p':
+
+ if bytes.Equal(ffjKeyExecSessionPID, kn) {
+ currentKey = ffjtExecSessionPID
+ state = fflib.FFParse_want_colon
+ goto mainparse
+ }
+
+ }
+
+ if fflib.SimpleLetterEqualFold(ffjKeyExecSessionPID, kn) {
+ currentKey = ffjtExecSessionPID
+ state = fflib.FFParse_want_colon
+ goto mainparse
+ }
+
+ if fflib.SimpleLetterEqualFold(ffjKeyExecSessionCommand, kn) {
+ currentKey = ffjtExecSessionCommand
+ state = fflib.FFParse_want_colon
+ goto mainparse
+ }
+
+ if fflib.SimpleLetterEqualFold(ffjKeyExecSessionID, kn) {
+ currentKey = ffjtExecSessionID
+ state = fflib.FFParse_want_colon
+ goto mainparse
+ }
+
+ currentKey = ffjtExecSessionnosuchkey
+ state = fflib.FFParse_want_colon
+ goto mainparse
+ }
+
+ case fflib.FFParse_want_colon:
+ if tok != fflib.FFTok_colon {
+ wantedTok = fflib.FFTok_colon
+ goto wrongtokenerror
+ }
+ state = fflib.FFParse_want_value
+ continue
+ case fflib.FFParse_want_value:
+
+ if tok == fflib.FFTok_left_brace || tok == fflib.FFTok_left_bracket || tok == fflib.FFTok_integer || tok == fflib.FFTok_double || tok == fflib.FFTok_string || tok == fflib.FFTok_bool || tok == fflib.FFTok_null {
+ switch currentKey {
+
+ case ffjtExecSessionID:
+ goto handle_ID
+
+ case ffjtExecSessionCommand:
+ goto handle_Command
+
+ case ffjtExecSessionPID:
+ goto handle_PID
+
+ case ffjtExecSessionnosuchkey:
+ err = fs.SkipField(tok)
+ if err != nil {
+ return fs.WrapErr(err)
+ }
+ state = fflib.FFParse_after_value
+ goto mainparse
+ }
+ } else {
+ goto wantedvalue
+ }
+ }
+ }
+
+handle_ID:
+
+ /* handler: j.ID type=string kind=string quoted=false*/
+
+ {
+
+ {
+ if tok != fflib.FFTok_string && tok != fflib.FFTok_null {
+ return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok))
+ }
+ }
+
+ if tok == fflib.FFTok_null {
+
+ } else {
+
+ outBuf := fs.Output.Bytes()
+
+ j.ID = string(string(outBuf))
+
+ }
+ }
+
+ state = fflib.FFParse_after_value
+ goto mainparse
+
+handle_Command:
+
+ /* handler: j.Command type=[]string kind=slice quoted=false*/
+
+ {
+
+ {
+ if tok != fflib.FFTok_left_brace && tok != fflib.FFTok_null {
+ return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for ", tok))
+ }
+ }
+
+ if tok == fflib.FFTok_null {
+ j.Command = nil
+ } else {
+
+ j.Command = []string{}
+
+ wantVal := true
+
+ for {
+
+ var tmpJCommand string
+
+ tok = fs.Scan()
+ if tok == fflib.FFTok_error {
+ goto tokerror
+ }
+ if tok == fflib.FFTok_right_brace {
+ break
+ }
+
+ if tok == fflib.FFTok_comma {
+ if wantVal == true {
+ // TODO(pquerna): this isn't an ideal error message, this handles
+ // things like [,,,] as an array value.
+ return fs.WrapErr(fmt.Errorf("wanted value token, but got token: %v", tok))
+ }
+ continue
+ } else {
+ wantVal = true
+ }
+
+ /* handler: tmpJCommand type=string kind=string quoted=false*/
+
+ {
+
+ {
+ if tok != fflib.FFTok_string && tok != fflib.FFTok_null {
+ return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok))
+ }
+ }
+
+ if tok == fflib.FFTok_null {
+
+ } else {
+
+ outBuf := fs.Output.Bytes()
+
+ tmpJCommand = string(string(outBuf))
+
+ }
+ }
+
+ j.Command = append(j.Command, tmpJCommand)
+
+ wantVal = false
+ }
+ }
+ }
+
+ state = fflib.FFParse_after_value
+ goto mainparse
+
+handle_PID:
+
+ /* handler: j.PID type=int kind=int quoted=false*/
+
+ {
+ if tok != fflib.FFTok_integer && tok != fflib.FFTok_null {
+ return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for int", tok))
+ }
+ }
+
+ {
+
+ if tok == fflib.FFTok_null {
+
+ } else {
+
+ tval, err := fflib.ParseInt(fs.Output.Bytes(), 10, 64)
+
+ if err != nil {
+ return fs.WrapErr(err)
+ }
+
+ j.PID = int(tval)
+
+ }
+ }
+
+ state = fflib.FFParse_after_value
+ goto mainparse
+
+wantedvalue:
+ return fs.WrapErr(fmt.Errorf("wanted value token, but got token: %v", tok))
+wrongtokenerror:
+ return fs.WrapErr(fmt.Errorf("ffjson: wanted token: %v, but got token: %v output=%s", wantedTok, tok, fs.Output.String()))
+tokerror:
+ if fs.BigError != nil {
+ return fs.WrapErr(fs.BigError)
+ }
+ err = fs.Error.ToError()
+ if err != nil {
+ return fs.WrapErr(err)
+ }
+ panic("ffjson-generated: unreachable, please report bug.")
+done:
+
+ return nil
+}
+
+// MarshalJSON marshal bytes to json - template
func (j *containerState) MarshalJSON() ([]byte, error) {
var buf fflib.Buffer
if j == nil {
@@ -2517,18 +2872,11 @@ func (j *containerState) MarshalJSONBuf(buf fflib.EncodingBuffer) error {
fflib.WriteJsonString(buf, string(j.SubnetMask))
buf.WriteByte(',')
if len(j.ExecSessions) != 0 {
- if j.ExecSessions == nil {
- buf.WriteString(`"execSessions":null`)
- } else {
- buf.WriteString(`"execSessions":{ `)
- for key, value := range j.ExecSessions {
- fflib.WriteJsonString(buf, key)
- buf.WriteString(`:`)
- fflib.FormatBits2(buf, uint64(value), 10, value < 0)
- buf.WriteByte(',')
- }
- buf.Rewind(1)
- buf.WriteByte('}')
+ buf.WriteString(`"execSessions":`)
+ /* Falling back. type=map[string]*libpod.ExecSession kind=map */
+ err = buf.Encode(j.ExecSessions)
+ if err != nil {
+ return err
}
buf.WriteByte(',')
}
@@ -3239,7 +3587,7 @@ handle_SubnetMask:
handle_ExecSessions:
- /* handler: j.ExecSessions type=map[string]int kind=map quoted=false*/
+ /* handler: j.ExecSessions type=map[string]*libpod.ExecSession kind=map quoted=false*/
{
@@ -3253,7 +3601,7 @@ handle_ExecSessions:
j.ExecSessions = nil
} else {
- j.ExecSessions = make(map[string]int, 0)
+ j.ExecSessions = make(map[string]*ExecSession, 0)
wantVal := true
@@ -3261,7 +3609,7 @@ handle_ExecSessions:
var k string
- var tmpJExecSessions int
+ var tmpJExecSessions *ExecSession
tok = fs.Scan()
if tok == fflib.FFTok_error {
@@ -3310,29 +3658,25 @@ handle_ExecSessions:
}
tok = fs.Scan()
- /* handler: tmpJExecSessions type=int kind=int quoted=false*/
-
- {
- if tok != fflib.FFTok_integer && tok != fflib.FFTok_null {
- return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for int", tok))
- }
- }
+ /* handler: tmpJExecSessions type=*libpod.ExecSession kind=ptr quoted=false*/
{
-
if tok == fflib.FFTok_null {
+ tmpJExecSessions = nil
+
} else {
- tval, err := fflib.ParseInt(fs.Output.Bytes(), 10, 64)
+ if tmpJExecSessions == nil {
+ tmpJExecSessions = new(ExecSession)
+ }
+ err = tmpJExecSessions.UnmarshalJSONFFLexer(fs, fflib.FFParse_want_key)
if err != nil {
- return fs.WrapErr(err)
+ return err
}
-
- tmpJExecSessions = int(tval)
-
}
+ state = fflib.FFParse_after_value
}
j.ExecSessions[k] = tmpJExecSessions
diff --git a/libpod/oci.go b/libpod/oci.go
index 63aff9487..ef32079f9 100644
--- a/libpod/oci.go
+++ b/libpod/oci.go
@@ -571,7 +571,8 @@ func (r *OCIRuntime) execStopContainer(ctr *Container, timeout uint) error {
// Get a list of active exec sessions
execSessions := []int{}
- for _, pid := range ctr.state.ExecSessions {
+ for _, session := range ctr.state.ExecSessions {
+ pid := session.PID
// Ping the PID with signal 0 to see if it still exists
if err := unix.Kill(pid, 0); err == unix.ESRCH {
continue
diff --git a/libpod/sql_state.go b/libpod/sql_state.go
index b3e531ba1..20aa7c0ac 100644
--- a/libpod/sql_state.go
+++ b/libpod/sql_state.go
@@ -338,7 +338,7 @@ func (s *SQLState) UpdateContainer(ctr *Container) error {
newState.IPAddress = ipAddress
newState.SubnetMask = subnetMask
- newState.ExecSessions = make(map[string]int)
+ newState.ExecSessions = make(map[string]*ExecSession)
if err := json.Unmarshal([]byte(execSessions), &newState.ExecSessions); err != nil {
return errors.Wrapf(err, "error parsing container %s exec sessions", ctr.ID())
}
diff --git a/libpod/sql_state_internal.go b/libpod/sql_state_internal.go
index 6f0e2284c..5fd6e23a3 100644
--- a/libpod/sql_state_internal.go
+++ b/libpod/sql_state_internal.go
@@ -620,7 +620,7 @@ func (s *SQLState) ctrFromScannable(row scannable) (*Container, error) {
return nil, errors.Wrapf(err, "error parsing container %s DNS server JSON", id)
}
- ctr.state.ExecSessions = make(map[string]int)
+ ctr.state.ExecSessions = make(map[string]*ExecSession)
if err := json.Unmarshal([]byte(execSessions), &ctr.state.ExecSessions); err != nil {
return nil, errors.Wrapf(err, "error parsing container %s exec sessions JSON", id)
}
diff --git a/libpod/test_common.go b/libpod/test_common.go
index 8b8248188..75d9516b1 100644
--- a/libpod/test_common.go
+++ b/libpod/test_common.go
@@ -49,13 +49,24 @@ func getTestContainer(id, name, locksDir string) (*Container, error) {
},
},
state: &containerState{
- State: ContainerStateRunning,
- ConfigPath: "/does/not/exist/specs/" + id,
- RunDir: "/does/not/exist/tmp/",
- Mounted: true,
- Mountpoint: "/does/not/exist/tmp/" + id,
- PID: 1234,
- ExecSessions: map[string]int{"abcd": 101, "ef01": 202},
+ State: ContainerStateRunning,
+ ConfigPath: "/does/not/exist/specs/" + id,
+ RunDir: "/does/not/exist/tmp/",
+ Mounted: true,
+ Mountpoint: "/does/not/exist/tmp/" + id,
+ PID: 1234,
+ ExecSessions: map[string]*ExecSession{
+ "abcd": {
+ ID: "1",
+ Command: []string{"2", "3"},
+ PID: 9876,
+ },
+ "ef01": {
+ ID: "5",
+ Command: []string{"hello", "world"},
+ PID: 46765,
+ },
+ },
},
valid: true,
}