diff --git a/libpod/container.go b/libpod/container.go index acfd5031fc..525f31f7a8 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -128,10 +128,6 @@ type Container struct { // This is true if a container is restored from a checkpoint. restoreFromCheckpoint bool - // Used to query the NOTIFY_SOCKET once along with setting up - // mounts etc. - notifySocket string - slirp4netnsSubnet *net.IPNet } diff --git a/libpod/container_internal_common.go b/libpod/container_internal_common.go index 98c92bb302..aebde344dc 100644 --- a/libpod/container_internal_common.go +++ b/libpod/container_internal_common.go @@ -751,11 +751,10 @@ func lookupHostUser(name string) (*runcuser.ExecUser, error) { return &execUser, nil } -// mountNotifySocket mounts the NOTIFY_SOCKET into the container if it's set -// and if the sdnotify mode is set to container. It also sets c.notifySocket -// to avoid redundantly looking up the env variable. +// mountNotifySocket mounts the NOTIFY_SOCKET into the container if the host +// notify socket path is stored in config and sdnotify mode is container. func (c *Container) mountNotifySocket(g generate.Generator) error { - if c.notifySocket == "" { + if c.config.SdNotifySocket == "" { return nil } if c.config.SdNotifyMode != define.SdNotifyModeContainer { diff --git a/libpod/options.go b/libpod/options.go index d4b6997e3e..ced9132eda 100644 --- a/libpod/options.go +++ b/libpod/options.go @@ -626,6 +626,19 @@ func WithSdNotifyMode(mode string) CtrCreateOption { } } +// WithSdNotifySocket sets the host path to the systemd notify socket (e.g. from +// NOTIFY_SOCKET) when using sd-notify container mode. +func WithSdNotifySocket(socket string) CtrCreateOption { + return func(ctr *Container) error { + if ctr.valid { + return define.ErrCtrFinalized + } + + ctr.config.SdNotifySocket = socket + return nil + } +} + // WithShmSize sets the size of /dev/shm tmpfs mount. func WithShmSize(size int64) CtrCreateOption { return func(ctr *Container) error { diff --git a/pkg/specgen/generate/container_create.go b/pkg/specgen/generate/container_create.go index f1e5f9a219..f7412e19f3 100644 --- a/pkg/specgen/generate/container_create.go +++ b/pkg/specgen/generate/container_create.go @@ -7,6 +7,7 @@ import ( "encoding/json" "errors" "fmt" + "os" "path/filepath" "strings" @@ -361,6 +362,11 @@ func createContainerOptions(rt *libpod.Runtime, s *specgen.SpecGenerator, pod *l if len(s.SdNotifyMode) > 0 { options = append(options, libpod.WithSdNotifyMode(s.SdNotifyMode)) } + if notifySocket, ok := os.LookupEnv("NOTIFY_SOCKET"); ok && notifySocket != "" { + if strings.EqualFold(s.SdNotifyMode, define.SdNotifyModeContainer) { + options = append(options, libpod.WithSdNotifySocket(notifySocket)) + } + } if pod != nil { logrus.Debugf("adding container to pod %s", pod.Name()) options = append(options, rt.WithPod(pod))