github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/flock_windows.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 //go:build windows 5 // +build windows 6 7 package libkb 8 9 import ( 10 "fmt" 11 "os" 12 "syscall" 13 ) 14 15 // Open the file with exclusive access (locked to other processes) 16 // When the process exits, the lock will be released. 17 func (f *LockPIDFile) Lock() (err error) { 18 pathp, err := syscall.UTF16PtrFromString(f.name) 19 if err != nil { 20 return PIDFileLockError{f.name} 21 } 22 access := uint32(syscall.GENERIC_READ | syscall.GENERIC_WRITE) 23 createmode := uint32(syscall.OPEN_ALWAYS) 24 sharemode := uint32(syscall.FILE_SHARE_READ) // os.Open always uses FILE_SHARE_READ | FILE_SHARE_WRITE 25 var sa *syscall.SecurityAttributes 26 r, err := syscall.CreateFile(pathp, access, sharemode, sa, createmode, syscall.FILE_ATTRIBUTE_NORMAL, 0) 27 if err != nil { 28 return PIDFileLockError{f.name} 29 } 30 // this is what os.openFile does 31 f.file = os.NewFile(uintptr(r), f.name) 32 33 pid := os.Getpid() 34 fmt.Fprintf(f.file, "%d", pid) 35 f.file.Sync() 36 37 f.G().Log.Debug("Locked pidfile %s for pid=%d", f.name, pid) 38 39 return nil 40 }