summaryrefslogtreecommitdiff
path: root/pkg/systemd/activation.go
blob: c8b2389dc0c7404ed33d5fe29386c3fc35906b7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package systemd

import (
	"os"
	"strconv"
	"strings"
)

// SocketActivated determine if podman is running under the socket activation protocol
func SocketActivated() bool {
	pid, pid_found := os.LookupEnv("LISTEN_PID")
	fds, fds_found := os.LookupEnv("LISTEN_FDS")
	fdnames, fdnames_found := os.LookupEnv("LISTEN_FDNAMES")

	if !(pid_found && fds_found && fdnames_found) {
		return false
	}

	p, err := strconv.Atoi(pid)
	if err != nil || p != os.Getpid() {
		return false
	}

	nfds, err := strconv.Atoi(fds)
	if err != nil || nfds < 1 {
		return false
	}

	// First available file descriptor is always 3.
	if nfds > 1 {
		names := strings.Split(fdnames, ":")
		for _, n := range names {
			if strings.Contains(n, "podman") {
				return true
			}
		}
	}

	return true
}