blob: e9b991258c18da5e166adc8d97d5995d7080edcf [file] [log] [blame]
William Kurkianea869482019-04-09 15:16:11 -04001// 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
khenaidoo106c61a2021-08-11 18:05:46 -04005//go:build dragonfly || freebsd || linux || netbsd || openbsd
William Kurkianea869482019-04-09 15:16:11 -04006// +build dragonfly freebsd linux netbsd openbsd
7
8package unix
9
10import "unsafe"
11
12// fcntl64Syscall is usually SYS_FCNTL, but is overridden on 32-bit Linux
khenaidoo106c61a2021-08-11 18:05:46 -040013// systems by fcntl_linux_32bit.go to be SYS_FCNTL64.
William Kurkianea869482019-04-09 15:16:11 -040014var fcntl64Syscall uintptr = SYS_FCNTL
15
khenaidoo106c61a2021-08-11 18:05:46 -040016func fcntl(fd int, cmd, arg int) (int, error) {
17 valptr, _, errno := Syscall(fcntl64Syscall, uintptr(fd), uintptr(cmd), uintptr(arg))
William Kurkianea869482019-04-09 15:16:11 -040018 var err error
19 if errno != 0 {
20 err = errno
21 }
22 return int(valptr), err
23}
24
khenaidoo106c61a2021-08-11 18:05:46 -040025// FcntlInt performs a fcntl syscall on fd with the provided command and argument.
26func FcntlInt(fd uintptr, cmd, arg int) (int, error) {
27 return fcntl(int(fd), cmd, arg)
28}
29
William Kurkianea869482019-04-09 15:16:11 -040030// FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
31func 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}