/
process.go
173 lines (149 loc) · 3.89 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
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
package proctree
import (
"sync"
)
//
// Process
//
// Process represents a process.
type Process struct {
processHash uint32 // hash of process
parentHash uint32 // hash of parent
info *TaskInfo // task info
executable *FileInfo // executable info
interpreter *FileInfo // interpreter info (binary format interpreter/loader)
interp *FileInfo // interpreter (scripting language interpreter)
children map[uint32]struct{} // hash of childrens
threads map[uint32]struct{} // hash of threads
// Control fields
mutex *sync.RWMutex // mutex to protect the process
}
// NewProcess creates a new process.
func NewProcess(hash uint32) *Process {
return &Process{
processHash: hash,
parentHash: 0,
info: NewTaskInfo(),
executable: NewFileInfo(),
interpreter: NewFileInfo(),
interp: NewFileInfo(),
children: make(map[uint32]struct{}),
threads: make(map[uint32]struct{}),
mutex: &sync.RWMutex{},
}
}
// Getters
// GetHash returns the hash of the process.
func (p *Process) GetHash() uint32 {
p.mutex.RLock()
defer p.mutex.RUnlock()
return p.processHash
}
// GetParentHash returns the hash of the parent.
func (p *Process) GetParentHash() uint32 {
p.mutex.RLock()
defer p.mutex.RUnlock()
return p.parentHash
}
// GetInfo returns a instanced task info.
func (p *Process) GetInfo() *TaskInfo {
p.mutex.RLock()
defer p.mutex.RUnlock()
return p.info
}
// GetExecutable returns a instanced executable info.
func (p *Process) GetExecutable() *FileInfo {
p.mutex.RLock()
defer p.mutex.RUnlock()
return p.executable
}
// GetInterpreter returns a instanced interpreter info.
func (p *Process) GetInterpreter() *FileInfo {
p.mutex.RLock()
defer p.mutex.RUnlock()
return p.interpreter
}
// GetInterp returns a instanced interpreter.
func (p *Process) GetInterp() *FileInfo {
p.mutex.RLock()
defer p.mutex.RUnlock()
return p.interp
}
// Setters
// SetParentHash sets the hash of the parent.
func (p *Process) SetParentHash(parentHash uint32) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.parentHash = parentHash
}
//
// Children and Threads
//
// AddChild adds a child to the process.
func (p *Process) AddChild(childHash uint32) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.addChild(childHash)
}
// AddThread adds a thread to the process.
func (p *Process) AddThread(threadHash uint32) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.addThread(threadHash)
}
// GetChildren returns the children of the process.
func (p *Process) GetChildren() []uint32 {
p.mutex.RLock()
defer p.mutex.RUnlock()
children := make([]uint32, len(p.children))
i := 0
for child := range p.children {
children[i] = child
i++
}
return children
}
// GetThreads returns the threads of the process.
func (p *Process) GetThreads() []uint32 {
p.mutex.RLock()
defer p.mutex.RUnlock()
threads := make([]uint32, len(p.threads))
i := 0
for thread := range p.threads {
threads[i] = thread
i++
}
return threads
}
// DelChild deletes a child from the process.
func (p *Process) DelChild(childHash uint32) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.delChild(childHash)
}
// DelThread deletes a thread from the process.
func (p *Process) DelThread(threadHash uint32) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.delThread(threadHash)
}
// addChild adds a child to the process.
func (p *Process) addChild(childHash uint32) {
if _, ok := p.children[childHash]; !ok {
p.children[childHash] = struct{}{}
}
}
// addThread adds a thread to the process.
func (p *Process) addThread(threadHash uint32) {
if _, ok := p.threads[threadHash]; !ok {
p.threads[threadHash] = struct{}{}
}
}
// delChild deletes a child from the process.
func (p *Process) delChild(childHash uint32) {
delete(p.children, childHash)
}
// delThread deletes a thread from the process.
func (p *Process) delThread(threadHash uint32) {
delete(p.threads, threadHash)
}