Retry finding a socket's PID on linux

This commit is contained in:
Daniel
2020-06-05 14:02:37 +02:00
parent 6ffca6e411
commit 9f42e6ff93
4 changed files with 89 additions and 36 deletions

View File

@@ -1,6 +1,8 @@
package state
import (
"time"
"github.com/safing/portmaster/network/proc"
"github.com/safing/portmaster/network/socket"
)
@@ -13,15 +15,41 @@ var (
)
func checkConnectionPID(socketInfo *socket.ConnectionInfo, connInbound bool) (pid int, inbound bool, err error) {
if socketInfo.PID == proc.UnfetchedProcessID {
socketInfo.PID = proc.FindPID(socketInfo.UID, socketInfo.Inode)
for i := 0; i <= lookupRetries; i++ {
// look for PID
pid = proc.FindConnectionPID(socketInfo)
if pid != socket.UnidentifiedProcessID {
// if we found a PID, return
break
}
// every time, except for the last iteration
if i < lookupRetries {
// we found no PID, we could have been too fast, give the kernel some time to think
// back off timer: with 3ms baseWaitTime: 3, 6, 9, 12, 15, 18, 21ms - 84ms in total
time.Sleep(time.Duration(i+1) * baseWaitTime)
}
}
return socketInfo.PID, connInbound, nil
return pid, connInbound, nil
}
func checkBindPID(socketInfo *socket.BindInfo, connInbound bool) (pid int, inbound bool, err error) {
if socketInfo.PID == proc.UnfetchedProcessID {
socketInfo.PID = proc.FindPID(socketInfo.UID, socketInfo.Inode)
for i := 0; i <= lookupRetries; i++ {
// look for PID
pid = proc.FindBindPID(socketInfo)
if pid != socket.UnidentifiedProcessID {
// if we found a PID, return
break
}
// every time, except for the last iteration
if i < lookupRetries {
// we found no PID, we could have been too fast, give the kernel some time to think
// back off timer: with 3ms baseWaitTime: 3, 6, 9, 12, 15, 18, 21ms - 84ms in total
time.Sleep(time.Duration(i+1) * baseWaitTime)
}
}
return socketInfo.PID, connInbound, nil
return pid, connInbound, nil
}