ioctl.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // +build linux
  2. package loopback // import "github.com/docker/docker/pkg/loopback"
  3. import (
  4. "unsafe"
  5. "golang.org/x/sys/unix"
  6. )
  7. func ioctlLoopCtlGetFree(fd uintptr) (int, error) {
  8. // The ioctl interface for /dev/loop-control (since Linux 3.1) is a bit
  9. // off compared to what you'd expect: instead of writing an integer to a
  10. // parameter pointer like unix.IoctlGetInt() expects, it returns the first
  11. // available loop device index directly.
  12. ioctlReturn, _, err := unix.Syscall(unix.SYS_IOCTL, fd, LoopCtlGetFree, 0)
  13. if err != 0 {
  14. return 0, err
  15. }
  16. return int(ioctlReturn), nil
  17. }
  18. func ioctlLoopSetFd(loopFd, sparseFd uintptr) error {
  19. return unix.IoctlSetInt(int(loopFd), unix.LOOP_SET_FD, int(sparseFd))
  20. }
  21. func ioctlLoopSetStatus64(loopFd uintptr, loopInfo *unix.LoopInfo64) error {
  22. if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, unix.LOOP_SET_STATUS64, uintptr(unsafe.Pointer(loopInfo))); err != 0 {
  23. return err
  24. }
  25. return nil
  26. }
  27. func ioctlLoopClrFd(loopFd uintptr) error {
  28. if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, unix.LOOP_CLR_FD, 0); err != 0 {
  29. return err
  30. }
  31. return nil
  32. }
  33. func ioctlLoopGetStatus64(loopFd uintptr) (*unix.LoopInfo64, error) {
  34. loopInfo := &unix.LoopInfo64{}
  35. if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, unix.LOOP_GET_STATUS64, uintptr(unsafe.Pointer(loopInfo))); err != 0 {
  36. return nil, err
  37. }
  38. return loopInfo, nil
  39. }
  40. func ioctlLoopSetCapacity(loopFd uintptr, value int) error {
  41. return unix.IoctlSetInt(int(loopFd), unix.LOOP_SET_CAPACITY, value)
  42. }