lock

package
v1.131.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2026 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultLockFile = "installer.lock"

DefaultLockFile is the recommended path for the installer lock file. Callers may override (e.g., tests use t.TempDir() + this filename).

Variables

This section is empty.

Functions

This section is empty.

Types

type Lock

type Lock struct {
	// contains filtered or unexported fields
}

Lock represents an acquired installer lock. Callers MUST call Release() when done; the recommended pattern is `defer lk.Release()` immediately after a successful Acquire.

func Acquire

func Acquire(path string) (*Lock, error)

Acquire takes an exclusive non-blocking flock on path. Returns a *Lock on success or an error describing why acquisition failed.

If another process holds the lock AND the recorded PID corresponds to a live nftban-installer process, the returned error message includes that PID for operator diagnosis ("another installer is running (pid N)").

The lock file's parent directory is created (mkdir -p) if missing — on fresh installs the state-dir may not exist yet because FHS bootstrap happens in phasePrepare downstream.

Stale-PID safety: the recorded PID in the lock file is informational only. The authoritative lock source is the kernel's flock state. If the previous holder exited (cleanly or via crash/kill), the kernel releases the flock automatically and the next Acquire succeeds — the stale PID is overwritten with the new holder's PID.

func (*Lock) Path

func (l *Lock) Path() string

Path returns the lock file path. Useful in error messages and logs.

func (*Lock) Release

func (l *Lock) Release() error

Release releases the flock and closes the file descriptor. Idempotent and safe to call on a nil *Lock. The lock file itself is intentionally NOT deleted — leaving it preserves the recorded PID for post-mortem inspection and avoids a delete-race window.

Note: on os.Exit() Go skips deferred calls, so callers that use os.Exit (e.g., cmd/nftban-installer/main.go does this with the computed exit code) MUST call Release() explicitly before os.Exit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL