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
105
106
107
108
109
110
111
112
113
114
115
//go:build linux
package hostsvc
import (
"bytes"
"errors"
"fmt"
"io/fs"
"os"
"strconv"
"strings"
"golang.org/x/sys/unix"
)
const bootIDPath = "/proc/sys/kernel/random/boot_id"
const statFieldStartTime = 22
var errProcessGone = errors.New("hostsvc: the process is gone")
type pinned interface {
FD() int
StartTicks() uint64
Close()
}
type procs interface {
Pin(pid int) (pinned, error)
BootID() (string, error)
}
type linuxProcs struct{}
type pidfd struct {
fd int
ticks uint64
}
func (p *pidfd) FD() int { return p.fd }
func (p *pidfd) StartTicks() uint64 { return p.ticks }
func (p *pidfd) Close() { unix.Close(p.fd) }
func (linuxProcs) Pin(pid int) (pinned, error) {
if pid <= 0 {
return nil, fmt.Errorf("%w: pid %d", errProcessGone, pid)
}
fd, err := unix.PidfdOpen(pid, 0)
if errors.Is(err, unix.ESRCH) {
return nil, fmt.Errorf("%w: pidfd_open(%d): %v", errProcessGone, pid, err)
}
if err != nil {
return nil, fmt.Errorf("hostsvc: pidfd_open(%d): %w", pid, err)
}
ticks, err := startTicks(pid)
if err != nil {
unix.Close(fd)
return nil, err
}
if err := unix.PidfdSendSignal(fd, 0, nil, 0); err != nil {
unix.Close(fd)
if errors.Is(err, unix.ESRCH) {
return nil, fmt.Errorf("%w: pidfd_send_signal(%d, 0): %v", errProcessGone, pid, err)
}
return nil, fmt.Errorf("hostsvc: pidfd_send_signal(%d, 0): %w", pid, err)
}
return &pidfd{fd: fd, ticks: ticks}, nil
}
func (linuxProcs) BootID() (string, error) {
b, err := os.ReadFile(bootIDPath)
if err != nil {
return "", fmt.Errorf("hostsvc: reading the boot id: %w", err)
}
return strings.TrimSpace(string(b)), nil
}
func startTicks(pid int) (uint64, error) {
b, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/stat")
if errors.Is(err, fs.ErrNotExist) {
return 0, fmt.Errorf("%w: /proc/%d/stat: %v", errProcessGone, pid, err)
}
if err != nil {
return 0, fmt.Errorf("hostsvc: reading /proc/%d/stat: %w", pid, err)
}
state, ticks, err := parseStat(b)
if err != nil {
return 0, err
}
if state == 'Z' || state == 'X' {
return 0, fmt.Errorf("%w: pid %d is in state %c", errProcessGone, pid, state)
}
return ticks, nil
}
func parseStat(b []byte) (byte, uint64, error) {
comm := bytes.LastIndexByte(b, ')')
if comm < 0 {
return 0, 0, fmt.Errorf("hostsvc: a /proc/<pid>/stat with no ) closing the comm field")
}
fields := strings.Fields(string(b[comm+1:]))
start := statFieldStartTime - 3
if len(fields) <= start || len(fields[0]) != 1 {
return 0, 0, fmt.Errorf("hostsvc: /proc/<pid>/stat carries %d fields after the comm field, too few for the start time",
len(fields))
}
n, err := strconv.ParseUint(fields[start], 10, 64)
if err != nil {
return 0, 0, fmt.Errorf("hostsvc: /proc/<pid>/stat start time %q: %w", fields[start], err)
}
return fields[0][0], n, nil
}