github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/states/statemgr/filesystem_lock_unix.go (about)

     1  // +build !windows
     2  
     3  package statemgr
     4  
     5  import (
     6  	"log"
     7  	"os"
     8  	"syscall"
     9  )
    10  
    11  // use fcntl POSIX locks for the most consistent behavior across platforms, and
    12  // hopefully some campatibility over NFS and CIFS.
    13  func (s *Filesystem) lock() error {
    14  	log.Printf("[TRACE] statemgr.Filesystem: locking %s using fcntl flock", s.path)
    15  	flock := &syscall.Flock_t{
    16  		Type:   syscall.F_RDLCK | syscall.F_WRLCK,
    17  		Whence: int16(os.SEEK_SET),
    18  		Start:  0,
    19  		Len:    0,
    20  	}
    21  
    22  	fd := s.stateFileOut.Fd()
    23  	return syscall.FcntlFlock(fd, syscall.F_SETLK, flock)
    24  }
    25  
    26  func (s *Filesystem) unlock() error {
    27  	log.Printf("[TRACE] statemgr.Filesystem: unlocking %s using fcntl flock", s.path)
    28  	flock := &syscall.Flock_t{
    29  		Type:   syscall.F_UNLCK,
    30  		Whence: int16(os.SEEK_SET),
    31  		Start:  0,
    32  		Len:    0,
    33  	}
    34  
    35  	fd := s.stateFileOut.Fd()
    36  	return syscall.FcntlFlock(fd, syscall.F_SETLK, flock)
    37  }