/
taskinfo.go
420 lines (366 loc) · 10.3 KB
/
taskinfo.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package proctree
import (
"sync"
"time"
ch "github.com/aquasecurity/tracee/pkg/changelog"
"github.com/aquasecurity/tracee/pkg/utils"
)
// TaskInfoFeed allows external packages to set/get multiple values of a task at once.
type TaskInfoFeed struct {
Name string
Tid int
Pid int
PPid int
NsTid int
NsPid int
NsPPid int
Uid int
Gid int
StartTimeNS uint64
ExitTimeNS uint64
}
//
// Task Info
//
// TaskInfo represents a task.
type TaskInfo struct {
name *ch.Changelog[string] // variable (process name can be changed)
tid int // immutable
pid int // immutable
pPid *ch.Changelog[int] // variable (process can be reparented)
nsTid int // immutable
nsPid int // immutable
nsPPid *ch.Changelog[int] // variable (process can be reparented)
uid *ch.Changelog[int] // variable (process uid can be changed)
gid *ch.Changelog[int] // variable (process gid can be changed)
startTimeNS uint64 // this is a duration, in ns, since boot (immutable)
exitTimeNS uint64 // this is a duration, in ns, since boot (immutable)
mutex *sync.RWMutex
}
// NewTaskInfo creates a new task.
func NewTaskInfo() *TaskInfo {
return &TaskInfo{
name: ch.NewChangelog[string](),
pPid: ch.NewChangelog[int](),
nsPPid: ch.NewChangelog[int](),
uid: ch.NewChangelog[int](),
gid: ch.NewChangelog[int](),
mutex: &sync.RWMutex{},
}
}
// NewTaskInfoFromFeed creates a new task with values from the given feed.
func NewTaskInfoFromFeed(feed TaskInfoFeed) *TaskInfo {
new := NewTaskInfo()
new.SetFeed(feed)
return new
}
// Feed: Multiple values at once.
// SetFeed sets the values of the task from the given feed.
func (ti *TaskInfo) SetFeed(feed TaskInfoFeed) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.setFeedAt(feed, time.Now()) // set current values
}
// SetFeedAt sets the values of the task from the given feed at the given time.
func (ti *TaskInfo) SetFeedAt(feed TaskInfoFeed, targetTime time.Time) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.setFeedAt(feed, targetTime) // set values at the given time
}
func (ti *TaskInfo) setFeedAt(feed TaskInfoFeed, targetTime time.Time) {
if feed.Name != "" {
ti.name.Set(feed.Name, targetTime)
}
if feed.Tid >= 0 {
ti.tid = feed.Tid
}
if feed.Pid >= 0 {
ti.pid = feed.Pid
}
if feed.PPid >= 0 {
ti.pPid.Set(feed.PPid, targetTime)
}
if feed.NsTid >= 0 {
ti.nsTid = feed.NsTid
}
if feed.NsPid >= 0 {
ti.nsPid = feed.NsPid
}
if feed.NsPid >= 0 {
ti.nsPPid.Set(feed.NsPid, targetTime)
}
if feed.Uid >= 0 {
ti.uid.Set(feed.Uid, targetTime)
}
if feed.Gid >= 0 {
ti.gid.Set(feed.Gid, targetTime)
}
if feed.StartTimeNS != 0 {
ti.startTimeNS = feed.StartTimeNS
}
if feed.ExitTimeNS != 0 {
ti.exitTimeNS = feed.ExitTimeNS
}
}
// GetFeed returns the values of the task as a feed.
func (ti *TaskInfo) GetFeed() TaskInfoFeed {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.getFeedAt(time.Now()) // return current values
}
// GetFeedAt returns the values of the task as a feed at the given time.
func (ti *TaskInfo) GetFeedAt(targetTime time.Time) TaskInfoFeed {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.getFeedAt(targetTime) // return values at the given time
}
func (ti *TaskInfo) getFeedAt(targetTime time.Time) TaskInfoFeed {
return TaskInfoFeed{
Name: ti.name.Get(targetTime),
Tid: ti.tid,
Pid: ti.pid,
PPid: ti.pPid.Get(targetTime),
NsTid: ti.nsTid,
NsPid: ti.nsPid,
NsPPid: ti.nsPPid.Get(targetTime),
Uid: ti.uid.Get(targetTime),
Gid: ti.gid.Get(targetTime),
StartTimeNS: ti.startTimeNS,
ExitTimeNS: ti.exitTimeNS,
}
}
// Setters
// SetName sets the name of the task.
func (ti *TaskInfo) SetName(name string) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.name.Set(name, time.Now())
}
// SetNameAt sets the name of the task at the given time.
func (ti *TaskInfo) SetNameAt(name string, targetTime time.Time) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.name.Set(name, targetTime)
}
// SetTid sets the tid of the task.
func (ti *TaskInfo) SetTid(tid int) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.tid = tid
}
// SetPid sets the pid of the task.
func (ti *TaskInfo) SetPid(pid int) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.pid = pid
}
// SetNsTid sets the nsTid of the task.
func (ti *TaskInfo) SetNsTid(nsTid int) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.nsTid = nsTid
}
// SetNsPid sets the nsPid of the task.
func (ti *TaskInfo) SetNsPid(nsPid int) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.nsPid = nsPid
}
// SetStartTimeNS sets the startTimeNS of the task.
func (ti *TaskInfo) SetStartTimeNS(startTimeNS uint64) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.startTimeNS = startTimeNS
}
// SetExitTime sets the exitTime of the task.
func (ti *TaskInfo) SetExitTime(exitTime uint64) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.exitTimeNS = exitTime
}
// SetPPid sets the ppid of the task.
func (ti *TaskInfo) SetPPid(pPid int) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.pPid.Set(pPid, time.Now())
}
// SetPPidAt sets the ppid of the task at the given time.
func (ti *TaskInfo) SetPPidAt(pPid int, targetTime time.Time) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.pPid.Set(pPid, targetTime)
}
// SetNsPPid sets the nsppid of the task.
func (ti *TaskInfo) SetNsPPid(nsPPid int) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.nsPPid.Set(nsPPid, time.Now())
}
// SetNsPPidAt sets the nsppid of the task at the given time.
func (ti *TaskInfo) SetNsPPidAt(nsPPid int, targetTime time.Time) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.nsPPid.Set(nsPPid, targetTime)
}
// SetUid sets the uid of the task.
func (ti *TaskInfo) SetUid(uid int) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.uid.Set(uid, time.Now())
}
// SetUidAt sets the uid of the task at the given time.
func (ti *TaskInfo) SetUidAt(uid int, targetTime time.Time) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.uid.Set(uid, targetTime)
}
// SetGid sets the gid of the task.
func (ti *TaskInfo) SetGid(gid int) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.gid.Set(gid, time.Now())
}
// SetGidAt sets the gid of the task at the given time.
func (ti *TaskInfo) SetGidAt(gid int, targetTime time.Time) {
ti.mutex.Lock()
defer ti.mutex.Unlock()
ti.gid.Set(gid, targetTime)
}
// Getters
// GetName returns the name of the task.
func (ti *TaskInfo) GetName() string {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.name.GetCurrent()
}
// GetNameAt returns the name of the task at the given time.
func (ti *TaskInfo) GetNameAt(targetTime time.Time) string {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.name.Get(targetTime)
}
// GetTid returns the tid of the task.
func (ti *TaskInfo) GetTid() int {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.tid
}
// GetPid returns the pid of the task.
func (ti *TaskInfo) GetPid() int {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.pid
}
// GetNsTid returns the nsTid of the task.
func (ti *TaskInfo) GetNsTid() int {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.nsTid
}
// GetNsPid returns the nsPid of the task.
func (ti *TaskInfo) GetNsPid() int {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.nsPid
}
// GetPPid returns the ppid of the task.
func (ti *TaskInfo) GetPPid() int {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.pPid.GetCurrent()
}
// GetPPidAt returns the ppid of the task at the given time.
func (ti *TaskInfo) GetPPidAt(targetTime time.Time) int {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.pPid.Get(targetTime)
}
// GetNsPPid returns the nsPPid of the task.
func (ti *TaskInfo) GetNsPPid() int {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.nsPPid.GetCurrent()
}
// GetNsPPidAt returns the nsPPid of the task at the given time.
func (ti *TaskInfo) GetNsPPidAt(targetTime time.Time) int {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.nsPPid.Get(targetTime)
}
// GetUid returns the uid of the task.
func (ti *TaskInfo) GetUid() int {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.uid.GetCurrent()
}
// GetUidAt returns the uid of the task at the given time.
func (ti *TaskInfo) GetUidAt(targetTime time.Time) int {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.uid.Get(targetTime)
}
// GetGid returns the gid of the task.
func (ti *TaskInfo) GetGid() int {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.gid.GetCurrent()
}
// GetGidAt returns the gid of the task at the given time.
func (ti *TaskInfo) GetGidAt(targetTime time.Time) int {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.gid.Get(targetTime)
}
// GetStartTimeNS returns the startTimeNS of the task.
func (ti *TaskInfo) GetStartTimeNS() uint64 {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.startTimeNS
}
// GetStartTime returns the "real" start time of the task.
func (ti *TaskInfo) GetStartTime() time.Time {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
// Returns a real "time" to be used for the task.
duration := time.Duration(int64(ti.startTimeNS))
bootTime := utils.GetBootTime()
return bootTime.Add(duration)
}
// GetExitTimeNS returns the exitTime of the task.
func (ti *TaskInfo) GetExitTimeNS() uint64 {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.exitTimeNS
}
// GetExitTime returns the "real" exit time of the task.
func (ti *TaskInfo) GetExitTime() time.Time {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
// Returns a real "time" to be used for the task.
duration := time.Duration(int64(ti.exitTimeNS))
bootTime := utils.GetBootTime()
return bootTime.Add(duration)
}
// IsAlive returns true if the task has exited.
func (ti *TaskInfo) IsAlive() bool {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
return ti.exitTimeNS == 0
}
// IsAliveAt return whether the task is alive in the given time, either because it didn't start
// yet or it has exited.
func (ti *TaskInfo) IsAliveAt(targetTime time.Time) bool {
ti.mutex.RLock()
defer ti.mutex.RUnlock()
if ti.exitTimeNS != 0 {
if targetTime.After(utils.NsSinceBootTimeToTime(ti.exitTimeNS)) {
return false
}
}
// If start time is not initialized it will count as 0 ns, meaning it will be before any
// query time given.
if targetTime.Before(utils.NsSinceBootTimeToTime(ti.startTimeNS)) {
return false
}
return true
}