Files
podman/pkg/systemd/activation.go
Tycho Andersen 364e8a26da pkg/systemd: don't require LISTEN_FDNAMES for socket activation
LISTEN_FDNAMES is optional, the docs for sd_listen_fds() says:

    This information is read from the $LISTEN_FDNAMES variable, which
    **may** contain a colon-separated list of names.

emphasis mine (indeed, the cited coreos code also suggests it is optional).

This actually results in bug, since the default
/contrib/systemd/system/podman.socket file doesn't set a
FileDescriptorName=. podman when run with this systemd configuration
*always* starts in unix socket mode since SocketActivated() will return
false because the name is missing.

The bug is a race with a very small window: between when podman does the
unlink() and when it re-binds the socket later in the code, requests made
during this time will fail since nothing is listening. There's another
small race when the service stops and systemd realizes it and starts
listening again.

However, small this window we managed to hit it :).

Let's fix this by ignoring LISTEN_FDNAMES. Since the code in
cmd/podman/system/service_abi.go:restService() ignores this value anyway
when setting up the socket activated stuff, there's no real loss here.

Signed-off-by: Tycho Andersen <tycho@tycho.pizza>
2021-06-24 09:01:39 -06:00

30 lines
579 B
Go

package systemd
import (
"os"
"strconv"
)
// SocketActivated determine if podman is running under the socket activation protocol
// Criteria is based on the expectations of "github.com/coreos/go-systemd/v22/activation"
func SocketActivated() bool {
pid, found := os.LookupEnv("LISTEN_PID")
if !found {
return false
}
p, err := strconv.Atoi(pid)
if err != nil || p != os.Getpid() {
return false
}
fds, found := os.LookupEnv("LISTEN_FDS")
if !found {
return false
}
nfds, err := strconv.Atoi(fds)
if err != nil || nfds == 0 {
return false
}
return true
}