summaryrefslogtreecommitdiff
path: root/internal/askcli/runlock_stale_linux.go
blob: 183dfb2ea2065a3fb2834d4e5304131ccc35de3f (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
//go:build linux

package askcli

import (
	"os"
	"strconv"
	"strings"

	"golang.org/x/sys/unix"
)

func lockHolderIsStale(pid int, expectedComm string) bool {
	if pid <= 0 {
		return false
	}
	if err := unix.Kill(pid, 0); err != nil {
		return true
	}
	data, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/comm")
	if err != nil {
		return false
	}
	holder := strings.TrimSpace(string(data))
	if holder == "" {
		return false
	}
	want := expectedComm
	if len(want) > 15 {
		want = want[:15]
	}
	return holder != want
}