CleanProcessStorage cleans the storage from old processes.
(activePIDs map[int]struct{})
| 127 | |
| 128 | // CleanProcessStorage cleans the storage from old processes. |
| 129 | func CleanProcessStorage(activePIDs map[int]struct{}) { |
| 130 | // add system table of processes |
| 131 | pids, err := processInfo.Pids() |
| 132 | if err != nil { |
| 133 | log.Warningf("process: failed to get list of active PIDs: %s", err) |
| 134 | } else { |
| 135 | for _, pid := range pids { |
| 136 | activePIDs[int(pid)] = struct{}{} |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | processesCopy := All() |
| 141 | threshold := time.Now().Add(-deleteProcessesThreshold).Unix() |
| 142 | |
| 143 | // clean primary processes |
| 144 | for _, p := range processesCopy { |
| 145 | // The PID of a process does not change. |
| 146 | |
| 147 | // Check if this is a special process. |
| 148 | switch p.Pid { |
| 149 | case UnidentifiedProcessID, UnsolicitedProcessID, SystemProcessID: |
| 150 | p.profile.MarkStillActive() |
| 151 | continue |
| 152 | } |
| 153 | |
| 154 | // Check if process is active. |
| 155 | _, active := activePIDs[p.Pid] |
| 156 | if active { |
| 157 | p.profile.MarkStillActive() |
| 158 | continue |
| 159 | } |
| 160 | |
| 161 | // Process is inactive, start deletion process |
| 162 | lastSeen := p.GetLastSeen() |
| 163 | switch { |
| 164 | case lastSeen == 0: |
| 165 | // add last seen timestamp |
| 166 | p.SetLastSeen(time.Now().Unix()) |
| 167 | case lastSeen > threshold: |
| 168 | // within keep period |
| 169 | default: |
| 170 | // delete now |
| 171 | p.Delete() |
| 172 | log.Tracef("process: cleaned %s", p.DatabaseKey()) |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // SetDBController sets the database controller and allows the package to push database updates on a save. It must be set by the package that registers the "network" database. |
| 178 | func SetDBController(controller *database.Controller) { |
no test coverage detected