Retry finding a socket's PID on linux
This commit is contained in:
@@ -16,21 +16,48 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
// pidsByUserLock is also used for locking the socketInfo.PID on all socket.*Info structs.
|
||||
pidsByUserLock sync.Mutex
|
||||
pidsByUser = make(map[int][]int)
|
||||
)
|
||||
|
||||
// FindPID returns the pid of the given uid and socket inode.
|
||||
func FindPID(uid, inode int) (pid int) { //nolint:gocognit // TODO
|
||||
// FindConnectionPID returns the pid of the given socket info.
|
||||
func FindConnectionPID(socketInfo *socket.ConnectionInfo) (pid int) {
|
||||
pidsByUserLock.Lock()
|
||||
defer pidsByUserLock.Unlock()
|
||||
|
||||
if socketInfo.PID != socket.UnidentifiedProcessID {
|
||||
return socket.UnidentifiedProcessID
|
||||
}
|
||||
|
||||
pid = findPID(socketInfo.UID, socketInfo.Inode)
|
||||
socketInfo.PID = pid
|
||||
return pid
|
||||
}
|
||||
|
||||
// FindBindPID returns the pid of the given socket info.
|
||||
func FindBindPID(socketInfo *socket.BindInfo) (pid int) {
|
||||
pidsByUserLock.Lock()
|
||||
defer pidsByUserLock.Unlock()
|
||||
|
||||
if socketInfo.PID != socket.UnidentifiedProcessID {
|
||||
return socket.UnidentifiedProcessID
|
||||
}
|
||||
|
||||
pid = findPID(socketInfo.UID, socketInfo.Inode)
|
||||
socketInfo.PID = pid
|
||||
return pid
|
||||
}
|
||||
|
||||
// findPID returns the pid of the given uid and socket inode.
|
||||
func findPID(uid, inode int) (pid int) { //nolint:gocognit // TODO
|
||||
|
||||
pidsUpdated := false
|
||||
|
||||
// get pids of user, update if missing
|
||||
pids, ok := pidsByUser[uid]
|
||||
if !ok {
|
||||
// log.Trace("process: no processes of user, updating table")
|
||||
// log.Trace("proc: no processes of user, updating table")
|
||||
updatePids()
|
||||
pidsUpdated = true
|
||||
pids, ok = pidsByUser[uid]
|
||||
@@ -46,7 +73,7 @@ func FindPID(uid, inode int) (pid int) { //nolint:gocognit // TODO
|
||||
}
|
||||
// if we fail on the first run and have not updated, update and check the ones we haven't tried so far.
|
||||
if !pidsUpdated {
|
||||
// log.Trace("process: socket not found in any process of user, updating table")
|
||||
// log.Trace("proc: socket not found in any process of user, updating table")
|
||||
// update
|
||||
updatePids()
|
||||
// sort for faster search
|
||||
@@ -67,7 +94,7 @@ func FindPID(uid, inode int) (pid int) { //nolint:gocognit // TODO
|
||||
}
|
||||
|
||||
// check all other pids
|
||||
// log.Trace("process: socket not found in any process of user, checking all pids")
|
||||
// log.Trace("proc: socket not found in any process of user, checking all pids")
|
||||
// TODO: find best order for pidsByUser for best performance
|
||||
for possibleUID, pids := range pidsByUser {
|
||||
if possibleUID != uid {
|
||||
@@ -93,7 +120,7 @@ func findSocketFromPid(pid, inode int) bool {
|
||||
link, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/%s", pid, entry))
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
log.Warningf("process: failed to read link /proc/%d/fd/%s: %s", pid, entry, err)
|
||||
log.Warningf("proc: failed to read link /proc/%d/fd/%s: %s", pid, entry, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -122,12 +149,12 @@ entryLoop:
|
||||
|
||||
statData, err := os.Stat(fmt.Sprintf("/proc/%d", pid))
|
||||
if err != nil {
|
||||
log.Warningf("process: could not stat /proc/%d: %s", pid, err)
|
||||
log.Warningf("proc: could not stat /proc/%d: %s", pid, err)
|
||||
continue entryLoop
|
||||
}
|
||||
sys, ok := statData.Sys().(*syscall.Stat_t)
|
||||
if !ok {
|
||||
log.Warningf("process: unable to parse /proc/%d: wrong type", pid)
|
||||
log.Warningf("proc: unable to parse /proc/%d: wrong type", pid)
|
||||
continue entryLoop
|
||||
}
|
||||
|
||||
@@ -152,14 +179,14 @@ func readDirNames(dir string) (names []string) {
|
||||
file, err := os.Open(dir)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
log.Warningf("process: could not open directory %s: %s", dir, err)
|
||||
log.Warningf("proc: could not open directory %s: %s", dir, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
names, err = file.Readdirnames(0)
|
||||
if err != nil {
|
||||
log.Warningf("process: could not get entries from directory %s: %s", dir, err)
|
||||
log.Warningf("proc: could not get entries from directory %s: %s", dir, err)
|
||||
return []string{}
|
||||
}
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user