opensnitch/daemon/procmon/activepids.go
Gustavo Iñiguez Goia 6048b0ef64 more cache improvements
- don't clean cache by number of items.
- clean inodes from cache every 2' if the descriptor symlink doesn't exist
  anymore, or if the lastSeen time is more than 5 minutes.
- launch cache cleaners before start a new process monitoring method,
  and start it only once for the life time of the daemon.
- do not store in cache the Time objects, only the nanoseconds of
  the last updated time.
- if the inode of a connection is found in cache, reorder the
  descriptors to push the descritptor to the top of the list.
  Also add cached the inode.

It turns out that when a new connection is about to be established,
when the process resolves the domain, the same inode is used to open the
tcp connection to the target. So if it's cached we save CPU cycles.

This also occurs when we block a connection and the process retries it,
or when a connection timeouts and the process retries it
(telnet 1.1.1.1).
2021-03-24 01:16:47 +01:00

89 lines
2.3 KiB
Go

package procmon
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"sync"
"time"
"github.com/evilsocket/opensnitch/daemon/log"
)
type value struct {
Process *Process
//Starttime uniquely identifies a process, it is the 22nd value in /proc/<PID>/stat
//if another process starts with the same PID, it's Starttime will be unique
Starttime uint32
}
var (
activePids = make(map[uint32]value)
activePidsLock = sync.RWMutex{}
)
//monitorActivePids checks that each process in activePids
//is still running and if not running (or another process with the same pid is running),
//removes the pid from activePids
func monitorActivePids() {
for {
time.Sleep(time.Second)
activePidsLock.Lock()
for k, v := range activePids {
data, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/stat", k))
if err != nil {
//file does not exists, pid has quit
delete(activePids, k)
deleteProcEntry(int(k))
continue
}
startTime, err := strconv.Atoi(strings.Split(string(data), " ")[21])
if err != nil {
log.Error("Could not find or convert Starttime. This should never happen. Please report this incident to the Opensnitch developers.")
delete(activePids, k)
deleteProcEntry(int(k))
continue
}
if uint32(startTime) != v.Starttime {
//extremely unlikely: the original process has quit and another process
//was started with the same PID - all this in less than 1 second
log.Error("Same PID but different Starttime. Please report this incident to the Opensnitch developers.")
delete(activePids, k)
deleteProcEntry(int(k))
continue
}
}
activePidsLock.Unlock()
}
}
func findProcessInActivePidsCache(pid uint32) *Process {
activePidsLock.Lock()
defer activePidsLock.Unlock()
if value, ok := activePids[pid]; ok {
return value.Process
}
return nil
}
func addToActivePidsCache(pid uint32, proc *Process) {
data, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
if err != nil {
//most likely the process has quit by now
return
}
startTime, err2 := strconv.Atoi(strings.Split(string(data), " ")[21])
if err2 != nil {
log.Error("Could not find or convert Starttime. This should never happen. Please report this incident to the Opensnitch developers.")
return
}
activePidsLock.Lock()
activePids[pid] = value{
Process: proc,
Starttime: uint32(startTime),
}
activePidsLock.Unlock()
}