From a1bb1987cc78dead96ef84086020f74b7c44c700 Mon Sep 17 00:00:00 2001
From: Matthew Heon <matthew.heon@pm.me>
Date: Tue, 2 Jul 2019 18:52:55 -0400
Subject: Store Conmon's PID in our state and display in inspect

Signed-off-by: Matthew Heon <matthew.heon@pm.me>
---
 libpod/container.go          | 20 +++++++++++++++++++-
 libpod/container_inspect.go  |  2 ++
 libpod/container_internal.go |  1 +
 libpod/oci_linux.go          |  3 +++
 4 files changed, 25 insertions(+), 1 deletion(-)

(limited to 'libpod')

diff --git a/libpod/container.go b/libpod/container.go
index 713386477..bfbc47d76 100644
--- a/libpod/container.go
+++ b/libpod/container.go
@@ -168,6 +168,8 @@ type ContainerState struct {
 	OOMKilled bool `json:"oomKilled,omitempty"`
 	// PID is the PID of a running container
 	PID int `json:"pid,omitempty"`
+	// ConmonPID is the PID of the container's conmon
+	ConmonPID int `json:"conmonPid,omitempty"`
 	// ExecSessions contains active exec sessions for container
 	// Exec session ID is mapped to PID of exec process
 	ExecSessions map[string]*ExecSession `json:"execSessions,omitempty"`
@@ -849,7 +851,7 @@ func (c *Container) OOMKilled() (bool, error) {
 	return c.state.OOMKilled, nil
 }
 
-// PID returns the PID of the container
+// PID returns the PID of the container.
 // If the container is not running, a pid of 0 will be returned. No error will
 // occur.
 func (c *Container) PID() (int, error) {
@@ -865,6 +867,22 @@ func (c *Container) PID() (int, error) {
 	return c.state.PID, nil
 }
 
+// ConmonPID Returns the PID of the container's conmon process.
+// If the container is not running, a PID of 0 will be returned. No error will
+// occur.
+func (c *Container) ConmonPID() (int, error) {
+	if !c.batched {
+		c.lock.Lock()
+		defer c.lock.Unlock()
+
+		if err := c.syncContainer(); err != nil {
+			return -1, err
+		}
+	}
+
+	return c.state.ConmonPID, nil
+}
+
 // ExecSessions retrieves active exec sessions running in the container
 func (c *Container) ExecSessions() ([]string, error) {
 	if !c.batched {
diff --git a/libpod/container_inspect.go b/libpod/container_inspect.go
index 6085f1210..938a5b210 100644
--- a/libpod/container_inspect.go
+++ b/libpod/container_inspect.go
@@ -145,6 +145,7 @@ type InspectContainerState struct {
 	OOMKilled   bool               `json:"OOMKilled"`
 	Dead        bool               `json:"Dead"`
 	Pid         int                `json:"Pid"`
+	ConmonPid   int                `json:"ConmonPid,omitempty"`
 	ExitCode    int32              `json:"ExitCode"`
 	Error       string             `json:"Error"` // TODO
 	StartedAt   time.Time          `json:"StartedAt"`
@@ -261,6 +262,7 @@ func (c *Container) getContainerInspectData(size bool, driverData *driver.Data)
 			OOMKilled:  runtimeInfo.OOMKilled,
 			Dead:       runtimeInfo.State.String() == "bad state",
 			Pid:        runtimeInfo.PID,
+			ConmonPid:  runtimeInfo.ConmonPID,
 			ExitCode:   runtimeInfo.ExitCode,
 			Error:      "", // can't get yet
 			StartedAt:  runtimeInfo.StartedTime,
diff --git a/libpod/container_internal.go b/libpod/container_internal.go
index 43d2b6e61..29df233b0 100644
--- a/libpod/container_internal.go
+++ b/libpod/container_internal.go
@@ -452,6 +452,7 @@ func (c *Container) teardownStorage() error {
 // It does not save the results - assumes the database will do that for us
 func resetState(state *ContainerState) error {
 	state.PID = 0
+	state.ConmonPID = 0
 	state.Mountpoint = ""
 	state.Mounted = false
 	if state.State != define.ContainerStateExited {
diff --git a/libpod/oci_linux.go b/libpod/oci_linux.go
index 7d9f47ae2..24502ef4f 100644
--- a/libpod/oci_linux.go
+++ b/libpod/oci_linux.go
@@ -446,6 +446,9 @@ func (r *OCIRuntime) createOCIContainer(ctr *Container, cgroupParent string, res
 			return errors.Wrapf(define.ErrInternal, "container create failed")
 		}
 		ctr.state.PID = ss.si.Pid
+		if cmd.Process != nil {
+			ctr.state.ConmonPID = cmd.Process.Pid
+		}
 	case <-time.After(ContainerCreateTimeout):
 		return errors.Wrapf(define.ErrInternal, "container creation timeout")
 	}
-- 
cgit v1.2.3-54-g00ecf


From 38c6199b80c5de5d965ab1bcf05820567a36813c Mon Sep 17 00:00:00 2001
From: Matthew Heon <matthew.heon@pm.me>
Date: Tue, 2 Jul 2019 19:10:51 -0400
Subject: Wipe PID and ConmonPID in state after container stops

Matches the behavior of Docker.

Signed-off-by: Matthew Heon <matthew.heon@pm.me>
---
 libpod/container_internal.go | 2 ++
 libpod/oci.go                | 2 ++
 2 files changed, 4 insertions(+)

(limited to 'libpod')

diff --git a/libpod/container_internal.go b/libpod/container_internal.go
index 29df233b0..cb6c35049 100644
--- a/libpod/container_internal.go
+++ b/libpod/container_internal.go
@@ -1044,6 +1044,8 @@ func (c *Container) stop(timeout uint) error {
 		return err
 	}
 
+	c.state.PID = 0
+	c.state.ConmonPID = 0
 	c.state.StoppedByUser = true
 	if err := c.save(); err != nil {
 		return errors.Wrapf(err, "error saving container %s state after stopping", c.ID())
diff --git a/libpod/oci.go b/libpod/oci.go
index efb5e42cc..fdd783100 100644
--- a/libpod/oci.go
+++ b/libpod/oci.go
@@ -234,6 +234,8 @@ func (r *OCIRuntime) updateContainerStatus(ctr *Container, useRuntime bool) erro
 
 		// Alright, it exists. Transition to Stopped state.
 		ctr.state.State = define.ContainerStateStopped
+		ctr.state.PID = 0
+		ctr.state.ConmonPID = 0
 
 		// Read the exit file to get our stopped time and exit code.
 		return ctr.handleExitFile(exitFile, info)
-- 
cgit v1.2.3-54-g00ecf