Merge pull request #2604 from weaveworks/2577-ipv6-disabled-proc

Do not read tcp6 files if TCP version 6 isn't supported
This commit is contained in:
Alfonso Acosta
2017-06-16 15:10:14 +02:00
committed by GitHub
2 changed files with 17 additions and 7 deletions

View File

@@ -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

View File

@@ -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{