Documentation
¶
Overview ¶
Package lock wraps flock(2) on O_CLOEXEC files (spec 6.2, 9).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Held ¶
Held reports whether another open file description holds path's lock. A missing file is not held. It never creates the file.
func HoldersVisible ¶ added in v0.1.5
func HoldersVisible() bool
HoldersVisible reports whether Inspect can tell held from free: on Linux, whether /proc/locks can be read.
func SetProcLocksPath ¶ added in v0.1.5
func SetProcLocksPath(p string) (restore func())
SetProcLocksPath points the lock view at another file and returns the undo. It is for tests, in this package and others (Linux without a readable /proc/locks); it must not run in parallel, and it panics outside a test binary, like state.StubNetworkFS.
func TryHoldExisting ¶ added in v0.1.7
TryHoldExisting takes path's lock without blocking, on the file that is there (it never creates it), for a caller about to remove something because nobody holds it: Inspect only looks, and a look can miss a holder, so the removal is made under the lock instead. It returns:
- StateFree with the open file, now locked: the caller holds the lock through its removal, then closes the file, which releases it;
- StateFree with no file when path is not there;
- StateHeld when another open file description holds it;
- StateUnknown with the error for anything else (the caller keeps what it would remove).
Types ¶
type File ¶
type File struct {
Path string
// contains filtered or unexported fields
}
File is an open lock file. Locks belong to the open file description (flock semantics), so two Opens of one path exclude each other even within one process.
func (*File) LockWait ¶
LockWait retries TryLock every 5 ms until d passes or ctx ends (no unbounded blocking).
func (*File) Rewrite ¶
Rewrite replaces the file's content in place (pwrite + ftruncate), keeping the inode and lock.
func (*File) TryLockShared ¶ added in v0.1.6
TryLockShared takes a shared lock without blocking (compat.flock: the lock a flock -s caller takes beside other shared holders).
type State ¶ added in v0.1.5
type State int
State is what a read-only look at a lock found.
func Inspect ¶ added in v0.1.5
Inspect reports whether any process holds a flock on path without making a free lock look busy to a concurrent non-blocking caller (#55). Linux reads /proc/locks, matching the device and inode it prints; darwin asks fcntl(F_GETLK), which sees flock locks there. Neither takes the lock. A missing file is free. On Linux without a readable /proc/locks (no procfs, an LSM or seccomp denial, gVisor) a flock cannot be seen without taking it, so the answer is StateUnknown and no lock is taken (#24). Locks of processes outside this pid namespace are not listed in /proc/locks.
The kernel hands /proc/locks out a page at a time and the list shifts when other locks go, so one look can miss a held lock. Inspect looks again until two looks in a row agree, and calls a lock held when inspectLooks looks never do. A look can still be wrong both times, so a caller that removes something on "free" takes the lock itself first (TryHoldExisting).