From d01976c9695a84e94b9e290d81f448cf3af34ee4 Mon Sep 17 00:00:00 2001 From: Alessandro Boch Date: Mon, 29 Jun 2015 20:18:09 -0700 Subject: [PATCH] Endpoint interface stats read fails when invoked from docker - When invoked from docker, endpoint.Statistics() returns the statistics of the host's interfaces. Issue is tracked down to ioutil.ReadFile(). For some reason even if invoked from inside the sandbox netns, it ends up reading the stats file from the default netns, when invoked from docker. If same operation is run from inside a dedicated binary, it works as expected. - Replacing it with exec.Command("cat", ) solves the issue Signed-off-by: Alessandro Boch --- sandbox/interface_linux.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sandbox/interface_linux.go b/sandbox/interface_linux.go index 4bc6de5..7fc8c70 100644 --- a/sandbox/interface_linux.go +++ b/sandbox/interface_linux.go @@ -2,8 +2,8 @@ package sandbox import ( "fmt" - "io/ioutil" "net" + "os/exec" "regexp" "sync" @@ -168,9 +168,12 @@ func (i *nwIface) Statistics() (*InterfaceStatistics, error) { s := &InterfaceStatistics{} err := nsInvoke(path, func(nsFD int) error { return nil }, func(callerFD int) error { - data, err := ioutil.ReadFile(netStatsFile) + // For some reason ioutil.ReadFile(netStatsFile) reads the file in + // the default netns when this code is invoked from docker. + // Executing "cat " works as expected. + data, err := exec.Command("cat", netStatsFile).Output() if err != nil { - return fmt.Errorf("failed to open %s: %v", netStatsFile, err) + return fmt.Errorf("failure opening %s: %v", netStatsFile, err) } return scanInterfaceStats(string(data), i.DstName(), s) })