Skip to content

Commit

Permalink
usm: Move helper function to its only usage (#31862)
Browse files Browse the repository at this point in the history
  • Loading branch information
guyarb authored Dec 8, 2024
1 parent b08c03d commit 512bca5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
28 changes: 27 additions & 1 deletion pkg/network/usm/sharedlibraries/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (w *Watcher) Start() {
return
case <-processSync.C:
processSet := w.registry.GetRegisteredProcesses()
deletedPids := monitor.FindDeletedProcesses(processSet)
deletedPids := findDeletedProcesses(processSet)
for deletedPid := range deletedPids {
_ = w.registry.Unregister(deletedPid)
}
Expand All @@ -290,3 +290,29 @@ func (w *Watcher) Start() {

utils.AddAttacher(consts.USMModuleName, "native", w)
}

// findDeletedProcesses returns the terminated PIDs from the given map.
func findDeletedProcesses[V any](pids map[uint32]V) map[uint32]struct{} {
existingPids := make(map[uint32]struct{}, len(pids))

procIter := func(pid int) error {
if _, exists := pids[uint32(pid)]; exists {
existingPids[uint32(pid)] = struct{}{}
}
return nil
}
// Scanning already running processes
if err := kernel.WithAllProcs(kernel.ProcFSRoot(), procIter); err != nil {
return nil
}

res := make(map[uint32]struct{}, len(pids)-len(existingPids))
for pid := range pids {
if _, exists := existingPids[pid]; exists {
continue
}
res[pid] = struct{}{}
}

return res
}
26 changes: 0 additions & 26 deletions pkg/process/monitor/process_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,32 +487,6 @@ func (pm *ProcessMonitor) Stop() {
pm.processExitCallbacksMutex.Unlock()
}

// FindDeletedProcesses returns the terminated PIDs from the given map.
func FindDeletedProcesses[V any](pids map[uint32]V) map[uint32]struct{} {
existingPids := make(map[uint32]struct{}, len(pids))

procIter := func(pid int) error {
if _, exists := pids[uint32(pid)]; exists {
existingPids[uint32(pid)] = struct{}{}
}
return nil
}
// Scanning already running processes
if err := kernel.WithAllProcs(kernel.ProcFSRoot(), procIter); err != nil {
return nil
}

res := make(map[uint32]struct{}, len(pids)-len(existingPids))
for pid := range pids {
if _, exists := existingPids[pid]; exists {
continue
}
res[pid] = struct{}{}
}

return res
}

// Event defines the event used by the process monitor
type Event struct {
Type model.EventType
Expand Down

0 comments on commit 512bca5

Please sign in to comment.