33: systemd-vaultd-update-secrets: get service name from MAINPID r=Mic92 a=Mic92



Co-authored-by: Jörg Thalheim <joerg@thalheim.io>
main
bors[bot] 2 years ago committed by GitHub
commit 2ec510274b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,6 +6,7 @@ import (
"log"
"os"
"path"
"strings"
"syscall"
"time"
)
@ -85,14 +86,31 @@ func updateSecrets(serviceName, target string) error {
return nil
}
func getSystemdServiceName() (string, error) {
mainPid := os.Getenv("MAINPID")
if mainPid == "" {
return "", fmt.Errorf("MAINPID not set")
}
p := fmt.Sprintf("/proc/%s/cgroup", mainPid)
content, err := os.ReadFile(p)
if err != nil {
return "", fmt.Errorf("failed to read cgroup file %s: %w", p, err)
}
line := strings.SplitN(string(content), "\n", 2)[0]
if !strings.HasSuffix(line, ".service") {
return "", fmt.Errorf("cgroup file %s does not end with .service", p)
}
return path.Base(line), nil
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: systemd-vaultd-update-secrets <target>")
os.Exit(1)
}
serviceName := os.Getenv("SYSTEMD_ACTIVATION_UNIT")
if serviceName == "" {
fmt.Println("SYSTEMD_ACTIVATION_UNIT not set")
serviceName, err := getSystemdServiceName()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

Loading…
Cancel
Save