-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_resolver.go
39 lines (32 loc) · 1.09 KB
/
host_resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package socket
import (
"fmt"
"os/exec"
"strconv"
"strings"
)
const hostProcPath = "/host/proc/"
// hostResolver uses the procfs of the host to resolve PIDs. With this the
// connection tracker can work when running in a container. As the ebpf
// program is not aware of the PID namespace that the processes running in, we
// need to find the PIDs of the host processes from the ones in the container.
type hostResolver struct{}
func (h hostResolver) Resolve(pid uint32) uint32 {
p, err := findHostPid(hostProcPath, pid)
if err != nil {
return pid
}
return p
}
// findHostPid greps through the procfs to find the host pid of the supplied
// namespaced pid. It's very ugly but it works well enough for testing with
// Kind.
func findHostPid(procPath string, nsPid uint32) (uint32, error) {
out, err := exec.Command("bash", "-c", fmt.Sprintf("grep %d -ril %s*/status | head -n 1", nsPid, procPath)).Output()
if err != nil {
return 0, err
}
strPid := strings.TrimSuffix(strings.TrimPrefix(string(out), procPath), "/status\n")
pid, err := strconv.ParseUint(strPid, 10, 32)
return uint32(pid), err
}