fileutils_darwin.go 758 B

12345678910111213141516171819202122232425
  1. package fileutils // import "github.com/docker/docker/pkg/fileutils"
  2. import (
  3. "bytes"
  4. "os"
  5. "os/exec"
  6. "strconv"
  7. )
  8. // GetTotalUsedFds returns the number of used File Descriptors by executing
  9. // "lsof -lnP -Ff -p PID".
  10. //
  11. // It uses the "-F" option to only print file-descriptors (f), and the "-l",
  12. // "-n", and "-P" options to omit looking up user-names, host-names, and port-
  13. // names. See [LSOF(8)].
  14. //
  15. // [LSOF(8)]: https://opensource.apple.com/source/lsof/lsof-49/lsof/lsof.man.auto.html
  16. func GetTotalUsedFds() int {
  17. output, err := exec.Command("lsof", "-lnP", "-Ff", "-p", strconv.Itoa(os.Getpid())).CombinedOutput()
  18. if err != nil {
  19. return -1
  20. }
  21. return bytes.Count(output, []byte("\nf")) // Count number of file descriptor fields in output.
  22. }