pub struct PidfileLock { /* private fields */ }Expand description
RAII handle holding an exclusive OFD-level flock on sqryd.lock.
Construct via acquire_pidfile_lock (normal start) or
PidfileLock::adopt (grandchild after --detach FD inheritance).
§Drop behaviour
PidfileOwnership | Unlinks pidfile? | Calls unlock()? |
|---|---|---|
WriteOwner | yes | yes |
Handoff | no | no (M-2 fix) |
Adopted | yes | yes |
Handoff closes the parent’s File handle (decrementing the OFD
reference count) WITHOUT calling flock(LOCK_UN). This is intentional:
LOCK_UN on a shared OFD releases the lock for ALL processes sharing that
OFD — including the grandchild — which would break the singleton guarantee.
Drop MUST NOT panic — all cleanup uses let _ = ....
Implementations§
Source§impl PidfileLock
impl PidfileLock
Sourcepub fn pidfile_path(&self) -> &PathBuf
pub fn pidfile_path(&self) -> &PathBuf
Returns the path to the pidfile (sqryd.pid).
Sourcepub fn lockfile_path(&self) -> &PathBuf
pub fn lockfile_path(&self) -> &PathBuf
Returns the path to the lockfile (sqryd.lock).
Sourcepub fn ownership(&self) -> PidfileOwnership
pub fn ownership(&self) -> PidfileOwnership
Returns the current ownership state.
Sourcepub fn as_raw_fd(&self) -> RawFd
pub fn as_raw_fd(&self) -> RawFd
Returns the raw OS file descriptor for the underlying lock file.
This is used by the --detach parent path in
sqry_daemon::entrypoint to obtain the FD that must be passed to
the grandchild process via SQRYD_LOCK_FD so the grandchild can call
PidfileLock::adopt.
The returned FD is owned by this PidfileLock; the caller MUST NOT
close it and MUST NOT use it after this lock is dropped.
Sourcepub fn hand_off_to_adopter(&mut self)
pub fn hand_off_to_adopter(&mut self)
Transition from WriteOwner to Handoff.
Called by the parent process in the --detach path after the grandchild
has signalled ready via the self-pipe. After this call the parent’s
Drop will release the flock but not unlink the pidfile — the
grandchild’s Adopted drop owns that responsibility.
Panics if the current state is not WriteOwner — calling this on a
Handoff or Adopted handle is always a programming error and
must be caught in both debug and release builds to prevent incorrect
drop behaviour (skipping required pidfile unlink).
Sourcepub fn write_pid(&self, pid: u32) -> DaemonResult<()>
pub fn write_pid(&self, pid: u32) -> DaemonResult<()>
Write the current process’s PID (decimal + newline) to the pidfile atomically (tmp + rename).
This is called by:
- The foreground /
run_startpath immediately afteracquire_pidfile_locksucceeds. - The grandchild in the
--detachpath afterPidfileLock::adoptto overwrite the parent’s PID with the grandchild’s PID.
The pidfile is created with mode 0644 (world-readable so that
diagnostic tools can read it without elevated privileges).
Sourcepub unsafe fn adopt(fd: RawFd, pidfile: PathBuf, lockfile: PathBuf) -> Self
pub unsafe fn adopt(fd: RawFd, pidfile: PathBuf, lockfile: PathBuf) -> Self
Wrap an already-locked FD inherited across fork+exec into a
PidfileLock with ownership = Adopted.
§Safety
The caller MUST ensure:
fdis a valid, open file descriptor in the current process.fdis the target of an active OFD-level exclusive flock (typically inherited acrossfork+execwithFD_CLOEXECcleared).- No other
PidfileLockin this process ownsfd. - The caller MUST NOT close
fdseparately — dropping the returnedPidfileLockwill close it (and release the flock) via theFiledestructor.
Violating any of these invariants is undefined behaviour (double-free, double-close, or stale-lock ABA).
Trait Implementations§
Source§impl Debug for PidfileLock
impl Debug for PidfileLock
Source§impl Drop for PidfileLock
impl Drop for PidfileLock
Source§fn drop(&mut self)
fn drop(&mut self)
Release the flock and, when appropriate, unlink the pidfile.
Drop MUST NOT panic — every operation is wrapped in let _ = ....
§Handoff and OFD-level flock invariant (M-2 fix)
In the --detach double-fork path the parent transitions to Handoff
after the grandchild signals ready. The grandchild inherits the same
OFD (open file description) via FD inheritance across fork+exec.
flock(LOCK_UN) operates at the OFD level: it releases the lock for
every FD in every process that shares the same OFD. Calling unlock()
in the Handoff drop would therefore release the grandchild’s lock too,
breaking the singleton guarantee.
The correct behaviour is to close the parent’s FD without explicitly
unlocking it. Closing a single FD that shares an OFD with another
process’s FD is safe: the kernel decrements the OFD reference count but
keeps the lock alive as long as any process still holds an open FD on
that OFD. The lock remains held until the grandchild closes its
inherited FD (which happens in Adopted drop).
Auto Trait Implementations§
impl Freeze for PidfileLock
impl RefUnwindSafe for PidfileLock
impl Send for PidfileLock
impl Sync for PidfileLock
impl Unpin for PidfileLock
impl UnsafeUnpin for PidfileLock
impl UnwindSafe for PidfileLock
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more