Skip to content

Commit

Permalink
Detection of interactive session fixed for systemd user services (#246)
Browse files Browse the repository at this point in the history
Co-authored-by: Pavel Bazika <pavel.bazika@icewarp.com>
  • Loading branch information
pavelbazika and Pavel Bazika committed Nov 16, 2020
1 parent 5dd9ac4 commit 19f776c
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions service_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package service

import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 19f776c

Please sign in to comment.