github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/syscall/flock_aix.go (about)

     1  // Copyright 2018 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  
     5  package syscall
     6  
     7  import "unsafe"
     8  
     9  // On AIX, there is no flock() system call, we emulate it.
    10  // Moreover, we can't call the default fcntl syscall because the arguments
    11  // must be integer and it's not possible to transform a pointer (lk)
    12  // to a int value.
    13  // It's easier to call syscall6 than to transform fcntl for every GOOS.
    14  func fcntlFlock(fd, cmd int, lk *Flock_t) (err error) {
    15  	_, _, e1 := syscall6(uintptr(unsafe.Pointer(&libc_fcntl)), 3, uintptr(fd), uintptr(cmd), uintptr(unsafe.Pointer(lk)), 0, 0, 0)
    16  	if e1 != 0 {
    17  		err = errnoErr(e1)
    18  	}
    19  	return
    20  }
    21  
    22  func Flock(fd int, op int) (err error) {
    23  	lk := &Flock_t{}
    24  	if (op & LOCK_UN) != 0 {
    25  		lk.Type = F_UNLCK
    26  	} else if (op & LOCK_EX) != 0 {
    27  		lk.Type = F_WRLCK
    28  	} else if (op & LOCK_SH) != 0 {
    29  		lk.Type = F_RDLCK
    30  	} else {
    31  		return nil
    32  	}
    33  	if (op & LOCK_NB) != 0 {
    34  		err = fcntlFlock(fd, F_SETLK, lk)
    35  		if err != nil && (err == EAGAIN || err == EACCES) {
    36  			return EWOULDBLOCK
    37  		}
    38  		return err
    39  	}
    40  	return fcntlFlock(fd, F_SETLKW, lk)
    41  }