Skip to content

Commit

Permalink
podman.service: use sdnotiy
Browse files Browse the repository at this point in the history
Commit 2b6dd3f set the killmode of the podman.service to the
systemd default which ultimately lead to the problem that systemd
will kill *all* processes inside the unit's cgroup and hence kill
all containers whenever the service is stopped.

Fix it by setting the type to sdnotify and the killmode to process.
`podman system service` will send the necessary notify messages
when the NOTIFY_SOCKET is set and unset it right after to prevent
the backend and container runtimes from jumping in between and send
messages as well.

Fixes: containers#7294
Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg authored and mheon committed Aug 17, 2020
1 parent 422d035 commit f5b3903
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 2 deletions.
3 changes: 2 additions & 1 deletion contrib/systemd/system/podman.service
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ Documentation=man:podman-system-service(1)
StartLimitIntervalSec=0

[Service]
Type=simple
Type=notify
KillMode=process
ExecStart=/usr/bin/podman system service
27 changes: 26 additions & 1 deletion pkg/api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"fmt"
"log"
"net"
"net/http"
Expand All @@ -17,6 +18,7 @@ import (
"github.com/containers/libpod/v2/pkg/api/handlers"
"github.com/containers/libpod/v2/pkg/api/server/idletracker"
"github.com/coreos/go-systemd/v22/activation"
"github.com/coreos/go-systemd/v22/daemon"
"github.com/gorilla/mux"
"github.com/gorilla/schema"
"github.com/pkg/errors"
Expand Down Expand Up @@ -147,8 +149,31 @@ func newServer(runtime *libpod.Runtime, duration time.Duration, listener *net.Li
return &server, nil
}

// Serve starts responding to HTTP requests
// If the NOTIFY_SOCKET is set, communicate the PID and readiness, and
// further unset NOTIFY_SOCKET to prevent containers from sending
// messages and unset INVOCATION_ID so conmon and containers are in
// the correct cgroup.
func setupSystemd() {
if len(os.Getenv("NOTIFY_SOCKET")) == 0 {
return
}
payload := fmt.Sprintf("MAINPID=%d", os.Getpid())
payload += "\n"
payload += daemon.SdNotifyReady
if sent, err := daemon.SdNotify(true, payload); err != nil {
logrus.Errorf("Error notifying systemd of Conmon PID: %s", err.Error())
} else if sent {
logrus.Debugf("Notify sent successfully")
}

if err := os.Unsetenv("INVOCATION_ID"); err != nil {
logrus.Errorf("Error unsetting INVOCATION_ID: %s", err.Error())
}
}

// Serve starts responding to HTTP requests.
func (s *APIServer) Serve() error {
setupSystemd()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
errChan := make(chan error, 1)
Expand Down
84 changes: 84 additions & 0 deletions vendor/github.com/coreos/go-systemd/v22/daemon/sdnotify.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions vendor/github.com/coreos/go-systemd/v22/daemon/watchdog.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ github.com/containers/storage/pkg/unshare
github.com/coreos/go-iptables/iptables
# github.com/coreos/go-systemd/v22 v22.1.0
github.com/coreos/go-systemd/v22/activation
github.com/coreos/go-systemd/v22/daemon
github.com/coreos/go-systemd/v22/dbus
github.com/coreos/go-systemd/v22/internal/dlopen
github.com/coreos/go-systemd/v22/journal
Expand Down

0 comments on commit f5b3903

Please sign in to comment.