fcntl.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build dragonfly || freebsd || linux || netbsd || openbsd
  5. // +build dragonfly freebsd linux netbsd openbsd
  6. package unix
  7. import "unsafe"
  8. // fcntl64Syscall is usually SYS_FCNTL, but is overridden on 32-bit Linux
  9. // systems by fcntl_linux_32bit.go to be SYS_FCNTL64.
  10. var fcntl64Syscall uintptr = SYS_FCNTL
  11. func fcntl(fd int, cmd, arg int) (int, error) {
  12. valptr, _, errno := Syscall(fcntl64Syscall, uintptr(fd), uintptr(cmd), uintptr(arg))
  13. var err error
  14. if errno != 0 {
  15. err = errno
  16. }
  17. return int(valptr), err
  18. }
  19. // FcntlInt performs a fcntl syscall on fd with the provided command and argument.
  20. func FcntlInt(fd uintptr, cmd, arg int) (int, error) {
  21. return fcntl(int(fd), cmd, arg)
  22. }
  23. // FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
  24. func FcntlFlock(fd uintptr, cmd int, lk *Flock_t) error {
  25. _, _, errno := Syscall(fcntl64Syscall, fd, uintptr(cmd), uintptr(unsafe.Pointer(lk)))
  26. if errno == 0 {
  27. return nil
  28. }
  29. return errno
  30. }