diff --git a/pkg/fileutils/fileutils_linux.go b/pkg/fileutils/fileutils_linux.go index 954ea48344..1988fef4d8 100644 --- a/pkg/fileutils/fileutils_linux.go +++ b/pkg/fileutils/fileutils_linux.go @@ -2,6 +2,7 @@ package fileutils import ( "fmt" + "io" "os" "github.com/sirupsen/logrus" @@ -10,10 +11,24 @@ import ( // GetTotalUsedFds Returns the number of used File Descriptors by // reading it via /proc filesystem. func GetTotalUsedFds() int { - if fds, err := os.ReadDir(fmt.Sprintf("/proc/%d/fd", os.Getpid())); err != nil { - logrus.Errorf("Error opening /proc/%d/fd: %s", os.Getpid(), err) - } else { - return len(fds) + name := fmt.Sprintf("/proc/%d/fd", os.Getpid()) + f, err := os.Open(name) + if err != nil { + logrus.WithError(err).Error("Error listing file descriptors") + return -1 } - return -1 + defer f.Close() + + var fdCount int + for { + names, err := f.Readdirnames(100) + fdCount += len(names) + if err == io.EOF { + break + } else if err != nil { + logrus.WithError(err).Error("Error listing file descriptors") + return -1 + } + } + return fdCount }