summaryrefslogtreecommitdiff
path: root/libpod/container.go
diff options
context:
space:
mode:
Diffstat (limited to 'libpod/container.go')
-rw-r--r--libpod/container.go120
1 files changed, 39 insertions, 81 deletions
diff --git a/libpod/container.go b/libpod/container.go
index 3a0f60fd9..a9b512de9 100644
--- a/libpod/container.go
+++ b/libpod/container.go
@@ -11,39 +11,16 @@ import (
"github.com/containernetworking/cni/pkg/types"
cnitypes "github.com/containernetworking/cni/pkg/types/current"
"github.com/containers/image/manifest"
+ "github.com/containers/libpod/libpod/define"
"github.com/containers/libpod/libpod/lock"
"github.com/containers/libpod/pkg/namespaces"
+ "github.com/containers/libpod/pkg/rootless"
"github.com/containers/storage"
"github.com/cri-o/ocicni/pkg/ocicni"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
-// ContainerStatus represents the current state of a container
-type ContainerStatus int
-
-const (
- // ContainerStateUnknown indicates that the container is in an error
- // state where information about it cannot be retrieved
- ContainerStateUnknown ContainerStatus = iota
- // ContainerStateConfigured indicates that the container has had its
- // storage configured but it has not been created in the OCI runtime
- ContainerStateConfigured ContainerStatus = iota
- // ContainerStateCreated indicates the container has been created in
- // the OCI runtime but not started
- ContainerStateCreated ContainerStatus = iota
- // ContainerStateRunning indicates the container is currently executing
- ContainerStateRunning ContainerStatus = iota
- // ContainerStateStopped indicates that the container was running but has
- // exited
- ContainerStateStopped ContainerStatus = iota
- // ContainerStatePaused indicates that the container has been paused
- ContainerStatePaused ContainerStatus = iota
- // ContainerStateExited indicates the the container has stopped and been
- // cleaned up
- ContainerStateExited ContainerStatus = iota
-)
-
// CgroupfsDefaultCgroupParent is the cgroup parent for CGroupFS in libpod
const CgroupfsDefaultCgroupParent = "/libpod_parent"
@@ -51,6 +28,10 @@ const CgroupfsDefaultCgroupParent = "/libpod_parent"
// manager in libpod
const SystemdDefaultCgroupParent = "machine.slice"
+// SystemdDefaultRootlessCgroupParent is the cgroup parent for the systemd cgroup
+// manager in libpod when running as rootless
+const SystemdDefaultRootlessCgroupParent = "user.slice"
+
// JournaldLogging is the string conmon expects to specify journald logging
const JournaldLogging = "journald"
@@ -166,7 +147,7 @@ type Container struct {
// It is stored on disk in a tmpfs and recreated on reboot
type ContainerState struct {
// The current state of the running container
- State ContainerStatus `json:"state"`
+ State define.ContainerStatus `json:"state"`
// The path to the JSON OCI runtime spec for this container
ConfigPath string `json:"configPath,omitempty"`
// RunDir is a per-boot directory for container content
@@ -190,6 +171,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"`
@@ -425,51 +408,6 @@ type ContainerNamedVolume struct {
Options []string `json:"options,omitempty"`
}
-// ContainerStatus returns a string representation for users
-// of a container state
-func (t ContainerStatus) String() string {
- switch t {
- case ContainerStateUnknown:
- return "unknown"
- case ContainerStateConfigured:
- return "configured"
- case ContainerStateCreated:
- return "created"
- case ContainerStateRunning:
- return "running"
- case ContainerStateStopped:
- return "stopped"
- case ContainerStatePaused:
- return "paused"
- case ContainerStateExited:
- return "exited"
- }
- return "bad state"
-}
-
-// StringToContainerStatus converts a string representation of a containers
-// status into an actual container status type
-func StringToContainerStatus(status string) (ContainerStatus, error) {
- switch status {
- case ContainerStateUnknown.String():
- return ContainerStateUnknown, nil
- case ContainerStateConfigured.String():
- return ContainerStateConfigured, nil
- case ContainerStateCreated.String():
- return ContainerStateCreated, nil
- case ContainerStateRunning.String():
- return ContainerStateRunning, nil
- case ContainerStateStopped.String():
- return ContainerStateStopped, nil
- case ContainerStatePaused.String():
- return ContainerStatePaused, nil
- case ContainerStateExited.String():
- return ContainerStateExited, nil
- default:
- return ContainerStateUnknown, errors.Wrapf(ErrInvalidArg, "unknown container state: %s", status)
- }
-}
-
// Config accessors
// Unlocked
@@ -820,13 +758,13 @@ func (c *Container) WorkingDir() string {
// Require locking
// State returns the current state of the container
-func (c *Container) State() (ContainerStatus, error) {
+func (c *Container) State() (define.ContainerStatus, error) {
if !c.batched {
c.lock.Lock()
defer c.lock.Unlock()
if err := c.syncContainer(); err != nil {
- return ContainerStateUnknown, err
+ return define.ContainerStateUnknown, err
}
}
return c.state.State, nil
@@ -916,7 +854,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) {
@@ -932,6 +870,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 {
@@ -965,7 +919,7 @@ func (c *Container) ExecSession(id string) (*ExecSession, error) {
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 nil, errors.Wrapf(define.ErrNoSuchCtr, "no exec session with ID %s found in container %s", id, c.ID())
}
returnSession := new(ExecSession)
@@ -990,7 +944,7 @@ func (c *Container) IPs() ([]net.IPNet, error) {
}
if !c.config.CreateNetNS {
- return nil, errors.Wrapf(ErrInvalidArg, "container %s network namespace is not managed by libpod", c.ID())
+ return nil, errors.Wrapf(define.ErrInvalidArg, "container %s network namespace is not managed by libpod", c.ID())
}
ips := make([]net.IPNet, 0)
@@ -1018,7 +972,7 @@ func (c *Container) Routes() ([]types.Route, error) {
}
if !c.config.CreateNetNS {
- return nil, errors.Wrapf(ErrInvalidArg, "container %s network namespace is not managed by libpod", c.ID())
+ return nil, errors.Wrapf(define.ErrInvalidArg, "container %s network namespace is not managed by libpod", c.ID())
}
routes := make([]types.Route, 0)
@@ -1094,12 +1048,12 @@ func (c *Container) NamespacePath(ns LinuxNS) (string, error) {
}
}
- if c.state.State != ContainerStateRunning && c.state.State != ContainerStatePaused {
- return "", errors.Wrapf(ErrCtrStopped, "cannot get namespace path unless container %s is running", c.ID())
+ if c.state.State != define.ContainerStateRunning && c.state.State != define.ContainerStatePaused {
+ return "", errors.Wrapf(define.ErrCtrStopped, "cannot get namespace path unless container %s is running", c.ID())
}
if ns == InvalidNS {
- return "", errors.Wrapf(ErrInvalidArg, "invalid namespace requested from container %s", c.ID())
+ return "", errors.Wrapf(define.ErrInvalidArg, "invalid namespace requested from container %s", c.ID())
}
return fmt.Sprintf("/proc/%d/ns/%s", c.state.PID, ns.String()), nil
@@ -1111,9 +1065,13 @@ func (c *Container) CGroupPath() (string, error) {
case CgroupfsCgroupsManager:
return filepath.Join(c.config.CgroupParent, fmt.Sprintf("libpod-%s", c.ID())), nil
case SystemdCgroupsManager:
+ if rootless.IsRootless() {
+ uid := rootless.GetRootlessUID()
+ return filepath.Join(c.config.CgroupParent, fmt.Sprintf("user-%d.slice/user@%d.service/user.slice", uid, uid), createUnitName("libpod", c.ID())), nil
+ }
return filepath.Join(c.config.CgroupParent, createUnitName("libpod", c.ID())), nil
default:
- return "", errors.Wrapf(ErrInvalidArg, "unsupported CGroup manager %s in use", c.runtime.config.CgroupManager)
+ return "", errors.Wrapf(define.ErrInvalidArg, "unsupported CGroup manager %s in use", c.runtime.config.CgroupManager)
}
}