Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 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 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 5 | //go:build dragonfly || freebsd || linux || netbsd || openbsd |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 6 | // +build dragonfly freebsd linux netbsd openbsd |
| 7 | |
| 8 | package unix |
| 9 | |
| 10 | import "unsafe" |
| 11 | |
| 12 | // fcntl64Syscall is usually SYS_FCNTL, but is overridden on 32-bit Linux |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 13 | // systems by fcntl_linux_32bit.go to be SYS_FCNTL64. |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 14 | var fcntl64Syscall uintptr = SYS_FCNTL |
| 15 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 16 | func fcntl(fd int, cmd, arg int) (int, error) { |
| 17 | valptr, _, errno := Syscall(fcntl64Syscall, uintptr(fd), uintptr(cmd), uintptr(arg)) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 18 | var err error |
| 19 | if errno != 0 { |
| 20 | err = errno |
| 21 | } |
| 22 | return int(valptr), err |
| 23 | } |
| 24 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 25 | // FcntlInt performs a fcntl syscall on fd with the provided command and argument. |
| 26 | func FcntlInt(fd uintptr, cmd, arg int) (int, error) { |
| 27 | return fcntl(int(fd), cmd, arg) |
| 28 | } |
| 29 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 30 | // FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command. |
| 31 | func FcntlFlock(fd uintptr, cmd int, lk *Flock_t) error { |
| 32 | _, _, errno := Syscall(fcntl64Syscall, fd, uintptr(cmd), uintptr(unsafe.Pointer(lk))) |
| 33 | if errno == 0 { |
| 34 | return nil |
| 35 | } |
| 36 | return errno |
| 37 | } |