Repository navigation
Expand file tree
/
Copy pathsession_linux.go
More file actions
104 lines (94 loc) · 2.94 KB
/
Copy pathsession_linux.go
File metadata and controls
104 lines (94 loc) · 2.94 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//go:build linux
package main
import (
"bytes"
"os"
"path/filepath"
"strconv"
"strings"
)
// scanSessions locates live agent sessions by inspecting /proc: each process's
// working directory and the regular files it holds open. Detection is
// best-effort; an unreadable /proc reports supported == false.
func scanSessions() sessionScan {
procs, err := os.ReadDir("/proc")
if err != nil {
return sessionScan{supported: false}
}
scan := sessionScan{supported: true}
for _, p := range procs {
if _, err := strconv.Atoi(p.Name()); err != nil {
continue // not a pid directory
}
if !isAgentProcess(p.Name()) {
continue
}
if cwd := resolvedLink(filepath.Join("/proc", p.Name(), "cwd")); cwd != "" {
scan.cwds = append(scan.cwds, cwd)
}
scan.openFiles = append(scan.openFiles, openFiles(p.Name())...)
}
return scan
}
// openFiles returns the resolved paths of regular files the process holds open,
// read from /proc/<pid>/fd. Non-file descriptors (sockets, pipes, anon inodes)
// resolve to targets like "socket:[…]" that don't begin with "/" and are
// skipped, so they never match a worktree path.
func openFiles(pid string) []string {
fdDir := filepath.Join("/proc", pid, "fd")
entries, err := os.ReadDir(fdDir)
if err != nil {
return nil // process gone or fds unreadable (not ours)
}
var files []string
for _, e := range entries {
target, err := os.Readlink(filepath.Join(fdDir, e.Name()))
if err != nil || !strings.HasPrefix(target, "/") {
continue
}
if resolved, err := filepath.EvalSymlinks(target); err == nil {
target = resolved
}
files = append(files, target)
}
return files
}
// resolvedLink reads a /proc symlink and resolves it to a symlink-free path.
func resolvedLink(link string) string {
target, err := os.Readlink(link)
if err != nil || target == "" {
return ""
}
if resolved, err := filepath.EvalSymlinks(target); err == nil {
return resolved
}
return target
}
// pidIsAgent reports whether the live process pid looks like an agent session,
// so a .treetop-inuse marker carrying a recycled PID (left behind by a
// SIGKILLed hook) isn't honoured forever once the kernel reassigns that PID to
// an unrelated process. It reuses the /proc identity read the session scan does.
func pidIsAgent(pid int) bool {
return isAgentProcess(strconv.Itoa(pid))
}
// isAgentProcess reports whether pid looks like a supported agent session,
// reading /proc/<pid>/comm for the name and /proc/<pid>/cmdline for node
// disambiguation.
func isAgentProcess(pid string) bool {
base := filepath.Join("/proc", pid)
comm, err := os.ReadFile(filepath.Join(base, "comm"))
if err != nil {
return false
}
name := strings.TrimSpace(string(comm))
var cmdline string
if name == "node" {
raw, err := os.ReadFile(filepath.Join(base, "cmdline"))
if err != nil {
return false
}
// cmdline args are NUL-separated.
cmdline = string(bytes.ReplaceAll(raw, []byte{0}, []byte{' '}))
}
return agentName(name, cmdline)
}