mountinfo_freebsd.go 921 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package mount
  2. /*
  3. #include <sys/param.h>
  4. #include <sys/ucred.h>
  5. #include <sys/mount.h>
  6. */
  7. import "C"
  8. import (
  9. "fmt"
  10. "reflect"
  11. "unsafe"
  12. )
  13. // Parse /proc/self/mountinfo because comparing Dev and ino does not work from
  14. // bind mounts.
  15. func parseMountTable() ([]*Info, error) {
  16. var rawEntries *C.struct_statfs
  17. count := int(C.getmntinfo(&rawEntries, C.MNT_WAIT))
  18. if count == 0 {
  19. return nil, fmt.Errorf("Failed to call getmntinfo")
  20. }
  21. var entries []C.struct_statfs
  22. header := (*reflect.SliceHeader)(unsafe.Pointer(&entries))
  23. header.Cap = count
  24. header.Len = count
  25. header.Data = uintptr(unsafe.Pointer(rawEntries))
  26. var out []*Info
  27. for _, entry := range entries {
  28. var mountinfo Info
  29. mountinfo.Mountpoint = C.GoString(&entry.f_mntonname[0])
  30. mountinfo.Source = C.GoString(&entry.f_mntfromname[0])
  31. mountinfo.Fstype = C.GoString(&entry.f_fstypename[0])
  32. out = append(out, &mountinfo)
  33. }
  34. return out, nil
  35. }