Detection of interactive session fixed for systemd user services (#246)

Co-authored-by: Pavel Bazika <pavel.bazika@icewarp.com>
This commit is contained in:
pavelbazika
2020-11-16 15:45:35 +01:00
committed by GitHub
parent 5dd9ac4ef7
commit 19f776cc5f
+28 -2
View File
@@ -6,6 +6,8 @@ package service
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
)
@@ -63,13 +65,37 @@ func init() {
)
}
func binaryName(pid int) (string, error) {
statPath := fmt.Sprintf("/proc/%d/stat", pid)
dataBytes, err := ioutil.ReadFile(statPath)
if err != nil {
return "", err
}
// First, parse out the image name
data := string(dataBytes)
binStart := strings.IndexRune(data, '(') + 1
binEnd := strings.IndexRune(data[binStart:], ')')
return data[binStart : binStart+binEnd], nil
}
func isInteractive() (bool, error) {
// TODO: This is not true for user services.
inContainer, err := isInContainer(cgroupFile)
if err != nil {
return false, err
}
return os.Getppid() != 1 || inContainer, nil
if inContainer {
return true, nil
}
ppid := os.Getppid()
if ppid == 1 {
return false, nil
}
binary, _ := binaryName(ppid)
return binary != "systemd", nil
}
// isInContainer checks if the service is being executed in docker or lxc