diff --git a/probe/endpoint/procspy/proc_linux.go b/probe/endpoint/procspy/proc_linux.go index 12c44fd0a..ec676072c 100644 --- a/probe/endpoint/procspy/proc_linux.go +++ b/probe/endpoint/procspy/proc_linux.go @@ -22,8 +22,19 @@ var ( procRoot = "/proc" namespaceKey = []string{"procspy", "namespaces"} netNamespacePathSuffix = "" + ipv6IsSupported = tcp6FileExists() ) +func tcp6FileExists() bool { + filename := filepath.Join(procRoot, "self/net/tcp6") + f, err := fs.Open(filename) + if err != nil { + return false + } + f.Close() + return true +} + type pidWalker struct { walker process.Walker tickc <-chan time.Time // Rate-limit clock. Sets the pace when traversing namespaces and /proc/PID/fd/* files. @@ -41,11 +52,6 @@ func newPidWalker(walker process.Walker, tickc <-chan time.Time, fdBlockSize uin return w } -// SetProcRoot sets the location of the proc filesystem. -func SetProcRoot(root string) { - procRoot = root -} - func getKernelVersion() (major, minor int, err error) { var u syscall.Utsname if err = syscall.Uname(&u); err != nil { @@ -102,7 +108,9 @@ func ReadTCPFiles(pid int, buf *bytes.Buffer) (int64, error) { dirName := strconv.Itoa(pid) read, errRead = readFile(filepath.Join(procRoot, dirName, "/net/tcp"), buf) - read6, errRead6 = readFile(filepath.Join(procRoot, dirName, "/net/tcp6"), buf) + if ipv6IsSupported { + read6, errRead6 = readFile(filepath.Join(procRoot, dirName, "/net/tcp6"), buf) + } if errRead != nil { return read + read6, errRead diff --git a/probe/endpoint/procspy/spy_linux.go b/probe/endpoint/procspy/spy_linux.go index 08d4258c5..2b83b806a 100644 --- a/probe/endpoint/procspy/spy_linux.go +++ b/probe/endpoint/procspy/spy_linux.go @@ -69,7 +69,9 @@ func (s *linuxScanner) Connections() (ConnIter, error) { if buf.Len() == 0 { readFile(procRoot+"/net/tcp", buf) - readFile(procRoot+"/net/tcp6", buf) + if ipv6IsSupported { + readFile(procRoot+"/net/tcp6", buf) + } } return &pnConnIter{