blob: 6200876fb28cc49c25e68739d8aac01b5df40a34 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001// 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
Abhay Kumar40252eb2025-10-13 13:25:53 +00005//go:build dragonfly || freebsd || linux || netbsd
Scott Baker2c1c4822019-10-16 11:02:41 -07006
7package unix
8
9import "unsafe"
10
11// fcntl64Syscall is usually SYS_FCNTL, but is overridden on 32-bit Linux
khenaidoo26721882021-08-11 17:42:52 -040012// systems by fcntl_linux_32bit.go to be SYS_FCNTL64.
Scott Baker2c1c4822019-10-16 11:02:41 -070013var fcntl64Syscall uintptr = SYS_FCNTL
14
khenaidoo26721882021-08-11 17:42:52 -040015func fcntl(fd int, cmd, arg int) (int, error) {
16 valptr, _, errno := Syscall(fcntl64Syscall, uintptr(fd), uintptr(cmd), uintptr(arg))
Scott Baker2c1c4822019-10-16 11:02:41 -070017 var err error
18 if errno != 0 {
19 err = errno
20 }
21 return int(valptr), err
22}
23
khenaidoo26721882021-08-11 17:42:52 -040024// FcntlInt performs a fcntl syscall on fd with the provided command and argument.
25func FcntlInt(fd uintptr, cmd, arg int) (int, error) {
26 return fcntl(int(fd), cmd, arg)
27}
28
Scott Baker2c1c4822019-10-16 11:02:41 -070029// FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
30func FcntlFlock(fd uintptr, cmd int, lk *Flock_t) error {
31 _, _, errno := Syscall(fcntl64Syscall, fd, uintptr(cmd), uintptr(unsafe.Pointer(lk)))
32 if errno == 0 {
33 return nil
34 }
35 return errno
36}