@@ -37,7 +37,8 @@ type ProcessEvent struct {
37
37
38
38
// ExecEventItem represents an item of the cache
39
39
type ExecEventItem struct {
40
- Proc Process
40
+ sync.RWMutex
41
+ Proc * Process
41
42
LastSeen int64
42
43
TTL int32
43
44
}
@@ -56,7 +57,6 @@ type EventsStore struct {
56
57
eventByPath map [string ]* ExecEventItem
57
58
checksums map [string ]uint
58
59
mu * sync.RWMutex
59
- hashed uint64
60
60
checksumsEnabled bool
61
61
}
62
62
@@ -69,54 +69,50 @@ func NewEventsStore() *EventsStore {
69
69
70
70
return & EventsStore {
71
71
mu : & sync.RWMutex {},
72
- checksums : make (map [string ]uint ),
73
- eventByPID : make (map [int ]* ExecEventItem ),
74
- eventByPath : make (map [string ]* ExecEventItem ),
72
+ checksums : make (map [string ]uint , 5000 ),
73
+ eventByPID : make (map [int ]* ExecEventItem , 5000 ),
74
+ eventByPath : make (map [string ]* ExecEventItem , 5000 ),
75
75
}
76
76
}
77
77
78
78
// Add adds a new process to cache.
79
79
// If computing checksums is enabled, new checksums will be computed if needed,
80
80
// or reused existing ones otherwise.
81
- func (e * EventsStore ) Add (proc Process ) {
81
+ func (e * EventsStore ) Add (proc * Process ) {
82
82
log .Debug ("[cache] EventsStore.Add() %d, %s" , proc .ID , proc .Path )
83
+ // add the item to cache ASAP
84
+ // then calculate the checksums if needed.
85
+ e .UpdateItem (proc )
83
86
if e .GetComputeChecksums () {
84
- e .ComputeChecksums (& proc )
87
+ e .ComputeChecksums (proc )
88
+ e .UpdateItem (proc )
85
89
}
86
-
87
- e .updateItem (& proc )
88
90
}
89
91
90
- func (e * EventsStore ) updateItem (proc * Process ) {
91
- log .Debug ("[cache] updateItem() adding to events store (total: %d, hashed:%d), pid: %d, paths: %s" , e .Len (), e .hashed , proc .ID , proc .Path )
92
+ // UpdateItem updates a cache item
93
+ func (e * EventsStore ) UpdateItem (proc * Process ) {
94
+ log .Debug ("[cache] updateItem() adding to events store (total: %d), pid: %d, paths: %s" , e .Len (), proc .ID , proc .Path )
92
95
if proc .Path == "" {
93
96
return
94
97
}
95
-
96
- e .mu .Lock ()
97
- defer e .mu .Unlock ()
98
-
99
98
ev := & ExecEventItem {
100
- Proc : * proc ,
99
+ Proc : proc ,
101
100
LastSeen : time .Now ().UnixNano (),
102
101
}
102
+ e .mu .Lock ()
103
103
e .eventByPID [proc .ID ] = ev
104
104
e .eventByPath [proc .Path ] = ev
105
+ e .mu .Unlock ()
105
106
}
106
107
107
108
// IsInStore checks if a PID is in the store.
108
109
// If the PID is in cache, we may need to update it if the PID
109
110
// is reusing the PID of the parent.
110
- func (e * EventsStore ) IsInStore (key int , proc * Process ) (item * ExecEventItem , needsHashUpdate bool , found bool ) {
111
- //fmt.Printf("IsInStore()\n")
111
+ func (e * EventsStore ) IsInStore (key int , proc * Process ) (item * ExecEventItem , needsUpdate bool , found bool ) {
112
112
item , found = e .IsInStoreByPID (key )
113
113
if ! found {
114
114
return
115
115
}
116
- /*if e.checksumsEnabled && len(item.Proc.Checksums) == 0 {
117
- log.Info("RECALCULATING STORED item: %s", item.Proc.Path)
118
- item.Proc.ComputeChecksums(e.checksums)
119
- }*/
120
116
log .Debug ("[cache] Event found by PID: %d, %s" , key , item .Proc .Path )
121
117
122
118
// check if this PID has replaced the PPID:
@@ -126,9 +122,8 @@ func (e *EventsStore) IsInStore(key int, proc *Process) (item *ExecEventItem, ne
126
122
// The previous pid+path will still exist as parent of the new child, in proc.Parent
127
123
if proc != nil && proc .Path != "" && item .Proc .Path != proc .Path {
128
124
log .Debug ("[event inCache, replacement] new: %d, %s -> inCache: %d -> %s" , proc .ID , proc .Path , item .Proc .ID , item .Proc .Path )
129
- //e.ComputeChecksums(proc)
130
- e .updateItem (proc )
131
- needsHashUpdate = true
125
+ //e.UpdateItem(proc)
126
+ needsUpdate = true
132
127
}
133
128
134
129
return
@@ -137,19 +132,19 @@ func (e *EventsStore) IsInStore(key int, proc *Process) (item *ExecEventItem, ne
137
132
// IsInStoreByPID checks if a pid exists in cache.
138
133
func (e * EventsStore ) IsInStoreByPID (key int ) (item * ExecEventItem , found bool ) {
139
134
e .mu .RLock ()
140
- defer e .mu .RUnlock ()
141
135
item , found = e .eventByPID [key ]
136
+ e .mu .RUnlock ()
142
137
return
143
138
}
144
139
145
140
// IsInStoreByPath checks if a process exists in cache by path.
146
141
func (e * EventsStore ) IsInStoreByPath (path string ) (item * ExecEventItem , found bool ) {
147
- e .mu .RLock ()
148
- defer e .mu .RUnlock ()
149
142
if path == "" || path == KernelConnection {
150
143
return
151
144
}
145
+ e .mu .RLock ()
152
146
item , found = e .eventByPath [path ]
147
+ e .mu .RUnlock ()
153
148
if found {
154
149
log .Debug ("[cache] event found by path: %s" , path )
155
150
}
@@ -159,14 +154,14 @@ func (e *EventsStore) IsInStoreByPath(path string) (item *ExecEventItem, found b
159
154
// Delete an item from cache
160
155
func (e * EventsStore ) Delete (key int ) {
161
156
e .mu .Lock ()
162
- defer e .mu .Unlock ()
163
157
delete (e .eventByPID , key )
158
+ e .mu .Unlock ()
164
159
}
165
160
166
161
// Len returns the number of items in cache.
167
162
func (e * EventsStore ) Len () int {
168
163
e .mu .RLock ()
169
- e .mu .RUnlock ()
164
+ defer e .mu .RUnlock ()
170
165
return len (e .eventByPID )
171
166
}
172
167
@@ -190,6 +185,14 @@ func (e *EventsStore) DeleteOldItems() {
190
185
}
191
186
}
192
187
188
+ func (e * EventsStore ) UpdateItemDetails (proc * Process ) {
189
+ proc .GetParent ()
190
+ proc .GetTree ()
191
+ proc .ReadCwd ()
192
+ proc .ReadEnv ()
193
+ e .UpdateItem (proc )
194
+ }
195
+
193
196
// -------------------------------------------------------------------------
194
197
// TODO: Move to its own package.
195
198
// A hashing service than runs in background, and accepts paths to hash
@@ -210,13 +213,12 @@ func (e *EventsStore) ComputeChecksums(proc *Process) {
210
213
// and because of this sometimes we don't receive the event of the parent.
211
214
item , _ , found := e .IsInStore (proc .ID , proc )
212
215
if ! found {
213
- // log.Debug("cache.reuseChecksums() %d not inCache, %s", proc.ID, proc.Path)
216
+ log .Debug ("cache.reuseChecksums() %d not inCache, %s" , proc .ID , proc .Path )
214
217
215
218
// if parent path and current path are equal, and the parent is alive, see if we have the hash of the parent path
216
219
if ! proc .IsChild () {
217
- log .Debug ("[cache] reuseChecksums() pid not in cache, not child of parent: %d, %s - %d" , proc .ID , proc .Path , proc .Starttime )
218
220
proc .ComputeChecksums (e .checksums )
219
- e . hashed ++
221
+ log . Debug ( "[cache] reuseChecksums() pid not in cache, not child of parent: %d, %s - %d - %v" , proc . ID , proc . Path , proc . Starttime , proc . Checksums )
220
222
return
221
223
}
222
224
@@ -244,14 +246,16 @@ func (e *EventsStore) ComputeChecksums(proc *Process) {
244
246
// pid found in cache
245
247
// we should check other parameters to see if the pid is really the same process
246
248
// proc/<pid>/maps
247
- if len (item .Proc .Checksums ) > 0 && (item .Proc .IsAlive () && item .Proc .Path == proc .Path ) {
248
- log .Debug ("[cache] reuseChecksums() cached PID alive, already hashed:%v, %s new: %s" , item .Proc .Checksums , item .Proc .Path , proc .Path )
249
+ item .RLock ()
250
+ checksumsNum := len (item .Proc .Checksums )
251
+ item .RUnlock ()
252
+ if checksumsNum > 0 && (item .Proc .IsAlive () && item .Proc .Path == proc .Path ) {
253
+ log .Debug ("[cache] reuseChecksums() cached PID alive, already hashed: %v, %s new: %s" , item .Proc .Checksums , item .Proc .Path , proc .Path )
249
254
proc .Checksums = item .Proc .Checksums
250
255
return
251
256
}
252
257
log .Debug ("[cache] reuseChecksums() PID found inCache, computing hashes: %s new: %s - hashes: |%v<>%v|" , item .Proc .Path , proc .Path , item .Proc .Checksums , proc .Checksums )
253
258
proc .ComputeChecksums (e .checksums )
254
- e .hashed ++
255
259
}
256
260
257
261
// AddChecksumHash adds a new hash algorithm to compute checksums
@@ -270,10 +274,10 @@ func (e *EventsStore) DelChecksumHash(hash string) {
270
274
e .mu .Unlock ()
271
275
}
272
276
273
- // SetComputeChecksums configures if we compute checksums of processes
274
- // They can be disabled for example if there's no rule that requires checksums.
277
+ // SetComputeChecksums configures if we compute checksums of processes.
278
+ // They will be disabled if there's no rule that requires checksums.
275
279
// When enabling this functionality, some already stored process may don't have
276
- // the checksums computed, so when enabling compute them.
280
+ // the checksums computed yet , so when enabling compute them.
277
281
func (e * EventsStore ) SetComputeChecksums (compute bool ) {
278
282
e .mu .Lock ()
279
283
defer e .mu .Unlock ()
@@ -293,7 +297,7 @@ func (e *EventsStore) SetComputeChecksums(compute bool) {
293
297
}
294
298
}
295
299
296
- // DisableChecksums disables computing checksums functionality
300
+ // DisableChecksums disables computing checksums functionality.
297
301
func (e * EventsStore ) DisableChecksums () {
298
302
e .mu .Lock ()
299
303
defer e .mu .Unlock ()
@@ -302,10 +306,10 @@ func (e *EventsStore) DisableChecksums() {
302
306
}
303
307
304
308
// GetComputeChecksums returns if computing checksums is enabled or not.
305
- // Disabled -> if there're no rules with checksum field
309
+ // Disabled -> if there're no rules with checksum field.
306
310
// Disabled -> if events monitors are not available.
307
- // TODO: Disabled -> if there were n rules with checksums, but the user delete them, or
308
- // unchecked checksums.
311
+ // Disabled -> if the user disables it globally.
312
+ // TODO: Disabled -> if there were n rules with checksums, but the user delete them .
309
313
func (e * EventsStore ) GetComputeChecksums () bool {
310
314
e .mu .RLock ()
311
315
defer e .mu .RUnlock ()
0 commit comments