From 61d6d01901738104de8de5c7c19db4c2e1f02c35 Mon Sep 17 00:00:00 2001 From: Peter Hommel Date: Tue, 14 May 2019 17:36:39 +0200 Subject: [PATCH] Linux systemd: two small fixes (#171) * Linux systemd: fixed bug in service configuration template. Dependencies should now work for Linux systemd. * Linux systemd: enabled detection of systemd inside chroot environments * Linux systemd: enabled detection of systemd inside chroot environments fix --- service_systemd_linux.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/service_systemd_linux.go b/service_systemd_linux.go index da3ecce..252637e 100644 --- a/service_systemd_linux.go +++ b/service_systemd_linux.go @@ -5,6 +5,7 @@ package service import ( + "bytes" "errors" "fmt" "os" @@ -20,6 +21,21 @@ func isSystemd() bool { if _, err := os.Stat("/run/systemd/system"); err == nil { return true } + if _, err := os.Stat("/proc/1/comm"); err == nil { + filerc, err := os.Open("/proc/1/comm") + if err != nil { + return false + } + defer filerc.Close() + + buf := new(bytes.Buffer) + buf.ReadFrom(filerc) + contents := buf.String() + + if strings.Trim(contents, " \r\n") == "systemd" { + return true + } + } return false } @@ -228,9 +244,8 @@ func (s *systemd) Restart() error { const systemdScript = `[Unit] Description={{.Description}} ConditionFileIsExecutable={{.Path|cmdEscape}} -{{range .Dependencies}} -{{.}} -{{end}} +{{range $i, $dep := .Dependencies}} +{{$dep}} {{end}} [Service] StartLimitInterval=5