-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
process.go
137 lines (114 loc) · 3.16 KB
/
process.go
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build linux
// Package ptracer holds the start command of CWS injector
package ptracer
import (
"slices"
"github.com/DataDog/datadog-agent/pkg/security/proto/ebpfless"
)
type fileHandleKey struct {
handleBytes uint32
handleType int32
}
type fileHandleVal struct {
pathName string
}
// Resources defines shared process resources
type Resources struct {
Fd map[int32]string
Cwd string
FileHandleCache map[fileHandleKey]*fileHandleVal
}
// Process represents a process context
type Process struct {
Pid int
Tgid int
Nr map[int]*ebpfless.SyscallMsg
Res *Resources
}
// NewProcess returns a new process
func NewProcess(pid int) *Process {
return &Process{
Pid: pid,
Tgid: pid,
Nr: make(map[int]*ebpfless.SyscallMsg),
Res: &Resources{
Fd: make(map[int32]string),
FileHandleCache: make(map[fileHandleKey]*fileHandleVal),
},
}
}
// ProcessCache defines a thread cache
type ProcessCache struct {
pid2Process map[int]*Process
tgid2Pid map[int][]int
tgid2Span map[int]*SpanTLS
}
// NewProcessCache returns a new thread cache
func NewProcessCache() *ProcessCache {
return &ProcessCache{
pid2Process: make(map[int]*Process),
tgid2Pid: make(map[int][]int),
tgid2Span: make(map[int]*SpanTLS),
}
}
// Add a process
func (tc *ProcessCache) Add(pid int, process *Process) {
tc.pid2Process[pid] = process
if process.Pid != process.Tgid {
tc.tgid2Pid[process.Tgid] = append(tc.tgid2Pid[process.Tgid], process.Pid)
}
}
// SetAsThreadOf set the process as thread of the given tgid
func (tc *ProcessCache) SetAsThreadOf(process *Process, ppid int) {
parent := tc.pid2Process[ppid]
if parent == nil {
// this shouldn't happen
return
}
// share resources, parent should never be nil
process.Tgid = parent.Tgid
process.Res = parent.Res
// re-add to update the caches
tc.Add(process.Pid, process)
}
// Remove a pid
func (tc *ProcessCache) Remove(process *Process) {
delete(tc.pid2Process, process.Pid)
if process.Pid == process.Tgid {
pids, ok := tc.tgid2Pid[process.Pid]
if !ok {
return
}
delete(tc.tgid2Pid, process.Pid)
for pid := range pids {
delete(tc.pid2Process, pid)
}
} else {
tc.tgid2Pid[process.Tgid] = slices.DeleteFunc(tc.tgid2Pid[process.Tgid], func(pid int) bool {
return pid == process.Pid
})
if len(tc.tgid2Pid[process.Tgid]) == 0 {
delete(tc.tgid2Pid, process.Tgid)
}
}
}
// Get return the process entry for the given pid
func (tc *ProcessCache) Get(pid int) *Process {
return tc.pid2Process[pid]
}
// GetSpan returns the span TLS entry for the given pid
func (tc *ProcessCache) GetSpan(tgid int) *SpanTLS {
return tc.tgid2Span[tgid]
}
// SetSpanTLS sets the span TLS entry for the given pid
func (tc *ProcessCache) SetSpanTLS(tgid int, span *SpanTLS) {
tc.tgid2Span[tgid] = span
}
// UnsetSpan unsets the span TLS entry for the given pid
func (tc *ProcessCache) UnsetSpan(tgid int) {
delete(tc.tgid2Span, tgid)
}