use std::cell::Cell;
use super::dir::WalkStep;
type Hook = Box<dyn FnOnce()>;
thread_local! {
static FORCE_CHILD_PROC_DIR_FAILURE: Cell<bool> = const { Cell::new(false) };
static BETWEEN_CHECK_AND_KILL: std::cell::RefCell<Option<Box<dyn FnOnce()>>> = std::cell::RefCell::new(None);
static ON_TAKE_PLACEMENT: crate::oneshot_hook::OneShotHook = const { crate::oneshot_hook::OneShotHook::new() };
static BEFORE_EXIT_WAIT: crate::oneshot_hook::OneShotHook = const { crate::oneshot_hook::OneShotHook::new() };
static SIGNALLED_BY_PID: Cell<usize> = const { Cell::new(0) };
static HOOK_GATE: Cell<Option<std::os::fd::RawFd>> = const { Cell::new(None) };
static FORCE_CHILD_KILL_DENIED: Cell<bool> = const { Cell::new(false) };
static BACKGROUND_REAP_NOTIFY: std::cell::RefCell<Option<std::sync::mpsc::Sender<()>>> = const { std::cell::RefCell::new(None) };
static AFTER_SHUT_READ: std::cell::RefCell<Option<Box<dyn FnOnce()>>> = std::cell::RefCell::new(None);
static WAIT_POLLING: std::cell::RefCell<Option<std::sync::mpsc::Sender<()>>> = const { std::cell::RefCell::new(None) };
static ON_WAIT: crate::oneshot_hook::OneShotHook = const { crate::oneshot_hook::OneShotHook::new() };
static BEFORE_STATE_READ: crate::oneshot_hook::OneShotHook = const { crate::oneshot_hook::OneShotHook::new() };
static WAITED_PIDFD: Cell<Option<std::os::fd::RawFd>> = const { Cell::new(None) };
static FORCE_CHILD_PIDFD_FAILURE: Cell<bool> = const { Cell::new(false) };
static REAPED_ORPHANS: std::cell::RefCell<Vec<(u32, Option<i32>)>> = const { std::cell::RefCell::new(Vec::new()) };
static FORCE_KILL_SUPPORTED: Cell<bool> = const { Cell::new(false) };
static FORCE_REPORT_CHANNEL_FAILURE: Cell<bool> = const { Cell::new(false) };
static FORCE_PLACEMENT_WRITE_RESULT: Cell<Option<isize>> = const { Cell::new(None) };
static FORCE_OCCUPY_BEFORE_UNWIND: Cell<bool> = const { Cell::new(false) };
static DRAIN_BLOCKING: std::cell::RefCell<Option<std::sync::mpsc::Sender<()>>> = const { std::cell::RefCell::new(None) };
static DRAIN_ZERO_REMAINING: std::cell::RefCell<Option<std::sync::mpsc::Sender<()>>> = const { std::cell::RefCell::new(None) };
static WAIT_SITE_PARK: std::cell::RefCell<Option<std::sync::mpsc::Sender<WaitSitePark>>> = const { std::cell::RefCell::new(None) };
static WAIT_DEADLINE_ARG: std::cell::RefCell<Option<std::sync::mpsc::Sender<std::time::Instant>>> = const { std::cell::RefCell::new(None) };
static LEAF_STEPS: std::cell::RefCell<Option<Vec<String>>> = const { std::cell::RefCell::new(None) };
static FORCE_INOTIFY_FAILURE: Cell<bool> = const { Cell::new(false) };
static FORCE_KILL_CHECK_ERRNO: Cell<Option<i32>> = const { Cell::new(None) };
static FORCE_LEAF_OPEN_FAILURE: Cell<bool> = const { Cell::new(false) };
static FAIL_KILL_WRITES: Cell<bool> = const { Cell::new(false) };
static FAIL_NEXT_KILL_WRITE: Cell<bool> = const { Cell::new(false) };
static PIDFD_INFO_MISSING: Cell<bool> = const { Cell::new(false) };
static PROC_HIDDEN: Cell<bool> = const { Cell::new(false) };
static BEFORE_KILL_WRITE: crate::oneshot_hook::OneShotHook = const { crate::oneshot_hook::OneShotHook::new() };
static CHILD_DIES: Cell<Option<ChildDeath>> = const { Cell::new(None) };
static FORCED_PIDFD_CGROUP_ID: Cell<Option<u64>> = const { Cell::new(None) };
static BEFORE_WALK_OPEN: std::cell::RefCell<Option<WalkHook>> = const { std::cell::RefCell::new(None) };
static BEFORE_DRAIN_BLOCK: crate::oneshot_hook::OneShotHook = const { crate::oneshot_hook::OneShotHook::new() };
static WALK_FAILURE: std::cell::RefCell<Option<WalkFailure>> = const { std::cell::RefCell::new(None) };
static PROC_HIDDEN_AS: Cell<Option<i32>> = const { Cell::new(None) };
static PIDFD_INFO_FAILS: Cell<bool> = const { Cell::new(false) };
static PROBE_DUMPABLE: Cell<bool> = const { Cell::new(false) };
static CGROUP_ID_FAILS: Cell<Option<i32>> = const { Cell::new(None) };
static LIVENESS_READ_FAILS: Cell<Option<i32>> = const { Cell::new(None) };
static SUBTREE_FAILS: Cell<Option<i32>> = const { Cell::new(None) };
static RMDIR_HOOK: std::cell::RefCell<Option<RmdirHook>> = std::cell::RefCell::new(None);
static FORCE_FORK_RUNNING_PIDFD_FAILURE: Cell<bool> = const { Cell::new(false) };
static FORK_RUNNING_PIDFD_FAILURE_PROBE: std::cell::RefCell<Option<std::os::fd::OwnedFd>> =
const { std::cell::RefCell::new(None) };
static FORCE_FORK_RUNNING_PROBE_PIDFD_FAILURE: Cell<bool> = const { Cell::new(false) };
static FORCE_KILL_ON_DROP_WAITID_EINTR: Cell<bool> = const { Cell::new(false) };
static FORCE_KILL_ON_DROP_KILL_FAILURE: Cell<bool> = const { Cell::new(false) };
static AFTER_FORK_STILL_LOCKED: std::cell::RefCell<Option<Hook>> = std::cell::RefCell::new(None);
static ON_FORK_RUNNING_LOCK_CONTENDED: std::cell::RefCell<Option<Hook>> = std::cell::RefCell::new(None);
static ON_FORK_RUNNING_CLEANUP: std::cell::RefCell<Option<Hook>> = std::cell::RefCell::new(None);
static FORK_RUNNING_LOCK_HELD_REPORT_FD: Cell<Option<std::os::fd::RawFd>> = const { Cell::new(None) };
}
type RmdirHook = Box<dyn FnMut(&std::path::Path) -> std::io::Result<()>>;
pub(crate) fn fail_kill_writes() -> FailKillWrites {
FAIL_KILL_WRITES.with(|f| f.set(true));
FailKillWrites(())
}
#[must_use = "the writes succeed again as soon as the guard is dropped"]
pub(crate) struct FailKillWrites(());
impl Drop for FailKillWrites {
fn drop(&mut self) {
FAIL_KILL_WRITES.with(|f| f.set(false));
}
}
pub(crate) fn kill_writes_fail() -> bool {
FAIL_KILL_WRITES.with(Cell::get)
}
pub(crate) fn fail_next_kill_write() -> FailNextKillWrite {
FAIL_NEXT_KILL_WRITE.with(|f| f.set(true));
FailNextKillWrite(())
}
#[must_use = "the failure is cleared as soon as the guard is dropped"]
pub(crate) struct FailNextKillWrite(());
impl Drop for FailNextKillWrite {
fn drop(&mut self) {
FAIL_NEXT_KILL_WRITE.with(|f| f.set(false));
}
}
pub(crate) fn take_next_kill_write_failure() -> bool {
FAIL_NEXT_KILL_WRITE.with(|f| f.replace(false))
}
pub(crate) fn miss_pidfd_info() -> MissPidfdInfo {
PIDFD_INFO_MISSING.with(|f| f.set(true));
MissPidfdInfo(())
}
#[must_use = "PIDFD_GET_INFO is back as soon as the guard is dropped"]
pub(crate) struct MissPidfdInfo(());
impl Drop for MissPidfdInfo {
fn drop(&mut self) {
PIDFD_INFO_MISSING.with(|f| f.set(false));
}
}
pub(crate) fn pidfd_info_missing() -> bool {
PIDFD_INFO_MISSING.with(Cell::get)
}
pub(crate) fn hide_proc() -> HideProc {
PROC_HIDDEN.with(|f| f.set(true));
HideProc(())
}
#[must_use = "/proc is visible again as soon as the guard is dropped"]
pub(crate) struct HideProc(());
impl Drop for HideProc {
fn drop(&mut self) {
PROC_HIDDEN.with(|f| f.set(false));
}
}
pub(crate) fn hide_proc_as(errno: i32) -> HideProcAs {
PROC_HIDDEN_AS.with(|f| f.set(Some(errno)));
HideProcAs(())
}
#[must_use = "/proc is visible again as soon as the guard is dropped"]
pub(crate) struct HideProcAs(());
impl Drop for HideProcAs {
fn drop(&mut self) {
PROC_HIDDEN_AS.with(|f| f.set(None));
}
}
pub(crate) fn proc_hidden_as() -> Option<i32> {
PROC_HIDDEN_AS
.with(Cell::get)
.or_else(|| PROC_HIDDEN.with(Cell::get).then_some(libc::ENOENT))
}
pub(crate) fn fail_pidfd_info() -> FailPidfdInfo {
PIDFD_INFO_FAILS.with(|f| f.set(true));
FailPidfdInfo(())
}
#[must_use = "PIDFD_GET_INFO answers again as soon as the guard is dropped"]
pub(crate) struct FailPidfdInfo(());
impl Drop for FailPidfdInfo {
fn drop(&mut self) {
PIDFD_INFO_FAILS.with(|f| f.set(false));
}
}
pub(crate) fn pidfd_info_fails() -> bool {
PIDFD_INFO_FAILS.with(Cell::get)
}
pub(crate) fn keep_probe_dumpable() -> KeepProbeDumpable {
PROBE_DUMPABLE.with(|f| f.set(true));
KeepProbeDumpable(())
}
#[must_use = "the probe's child is made non-dumpable again as soon as the guard is dropped"]
pub(crate) struct KeepProbeDumpable(());
impl Drop for KeepProbeDumpable {
fn drop(&mut self) {
PROBE_DUMPABLE.with(|f| f.set(false));
}
}
pub(crate) fn probe_kept_dumpable() -> bool {
PROBE_DUMPABLE.with(Cell::get)
}
pub(crate) fn set_before_kill_write(hook: impl FnOnce() + 'static) -> crate::oneshot_hook::Armed {
crate::oneshot_hook::arm(&BEFORE_KILL_WRITE, hook)
}
pub(crate) fn run_before_kill_write() {
crate::oneshot_hook::fire(&BEFORE_KILL_WRITE);
}
pub(crate) fn set_force_leaf_open_failure(on: bool) {
FORCE_LEAF_OPEN_FAILURE.with(|f| f.set(on));
}
pub(crate) fn take_force_leaf_open_failure() -> bool {
FORCE_LEAF_OPEN_FAILURE.with(|f| f.replace(false))
}
pub(crate) fn set_force_kill_check_errno(errno: i32) {
FORCE_KILL_CHECK_ERRNO.with(|f| f.set(Some(errno)));
}
pub(crate) fn take_force_kill_check_errno() -> Option<i32> {
FORCE_KILL_CHECK_ERRNO.with(|f| f.take())
}
pub(crate) fn set_force_inotify_failure(on: bool) {
FORCE_INOTIFY_FAILURE.with(|f| f.set(on));
}
pub(crate) fn take_force_inotify_failure() -> bool {
FORCE_INOTIFY_FAILURE.with(|f| f.replace(false))
}
pub(crate) fn set_rmdir_hook(hook: impl FnMut(&std::path::Path) -> std::io::Result<()> + 'static) {
RMDIR_HOOK.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
}
pub(crate) fn take_rmdir_hook() {
RMDIR_HOOK.with(|h| h.borrow_mut().take());
}
pub(crate) fn run_rmdir_hook(path: &std::path::Path) -> Option<std::io::Result<()>> {
let mut hook = RMDIR_HOOK.with(|h| h.borrow_mut().take())?;
let result = hook(path);
RMDIR_HOOK.with(|h| *h.borrow_mut() = Some(hook));
Some(result)
}
pub(crate) fn set_drain_blocking_notifier(notify: std::sync::mpsc::Sender<()>) {
DRAIN_BLOCKING.with(|d| *d.borrow_mut() = Some(notify));
}
pub(crate) fn take_drain_blocking_notifier() {
DRAIN_BLOCKING.with(|d| d.borrow_mut().take());
}
#[cfg(feature = "tokio")]
pub(crate) fn set_before_drain_block(hook: impl FnOnce() + 'static) -> crate::oneshot_hook::Armed {
crate::oneshot_hook::arm(&BEFORE_DRAIN_BLOCK, hook)
}
pub(crate) fn notify_drain_blocking() {
crate::oneshot_hook::fire(&BEFORE_DRAIN_BLOCK);
DRAIN_BLOCKING.with(|d| {
if let Some(notify) = d.borrow().as_ref() {
let sent = notify.send(());
debug_assert!(sent.is_ok(), "drain-blocking notifier's receiver was dropped");
}
});
}
pub(crate) fn set_drain_zero_remaining_notifier(notify: std::sync::mpsc::Sender<()>) {
DRAIN_ZERO_REMAINING.with(|d| *d.borrow_mut() = Some(notify));
}
pub(crate) fn take_drain_zero_remaining_notifier() {
DRAIN_ZERO_REMAINING.with(|d| d.borrow_mut().take());
}
pub(crate) fn notify_drain_zero_remaining() {
DRAIN_ZERO_REMAINING.with(|d| {
if let Some(notify) = d.borrow().as_ref() {
let sent = notify.send(());
debug_assert!(sent.is_ok(), "zero-remaining notifier's receiver was dropped");
}
});
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct WaitSitePark {
pub(crate) deadline: Option<std::time::Instant>,
pub(crate) woken: bool,
}
pub(crate) fn set_wait_site_park_notifier(notify: std::sync::mpsc::Sender<WaitSitePark>) {
WAIT_SITE_PARK.with(|p| *p.borrow_mut() = Some(notify));
}
pub(crate) fn take_wait_site_park_notifier() {
WAIT_SITE_PARK.with(|p| p.borrow_mut().take());
}
pub(crate) fn notify_wait_site_park(park: WaitSitePark) {
WAIT_SITE_PARK.with(|p| {
if let Some(notify) = p.borrow().as_ref() {
let sent = notify.send(park);
debug_assert!(sent.is_ok(), "wait-site-park notifier's receiver was dropped");
}
});
}
pub(crate) fn set_wait_deadline_arg_notifier(notify: std::sync::mpsc::Sender<std::time::Instant>) {
WAIT_DEADLINE_ARG.with(|p| *p.borrow_mut() = Some(notify));
}
pub(crate) fn take_wait_deadline_arg_notifier() {
WAIT_DEADLINE_ARG.with(|p| p.borrow_mut().take());
}
pub(crate) fn notify_wait_deadline_arg(at: std::time::Instant) {
WAIT_DEADLINE_ARG.with(|p| {
if let Some(notify) = p.borrow().as_ref() {
let sent = notify.send(at);
debug_assert!(sent.is_ok(), "wait-deadline-arg notifier's receiver was dropped");
}
});
}
pub(crate) fn record_leaf_steps() {
LEAF_STEPS.with(|s| *s.borrow_mut() = Some(Vec::new()));
}
#[cfg(feature = "tokio")]
pub(crate) fn leaf_steps_so_far() -> Vec<String> {
LEAF_STEPS.with(|s| s.borrow().clone()).unwrap_or_default()
}
pub(crate) fn take_leaf_steps() -> Vec<String> {
LEAF_STEPS.with(|s| s.borrow_mut().take()).unwrap_or_default()
}
pub(crate) fn record_leaf_step(step: impl FnOnce() -> String) {
LEAF_STEPS.with(|s| {
if let Some(steps) = s.borrow_mut().as_mut() {
steps.push(step());
}
});
}
pub(crate) fn set_force_kill_supported(on: bool) {
FORCE_KILL_SUPPORTED.with(|f| f.set(on));
}
pub(crate) fn take_force_kill_supported() -> bool {
FORCE_KILL_SUPPORTED.with(|f| f.replace(false))
}
pub(crate) fn kill_supported_armed() -> bool {
FORCE_KILL_SUPPORTED.with(|f| f.get())
}
pub(crate) fn set_force_report_channel_failure(on: bool) {
FORCE_REPORT_CHANNEL_FAILURE.with(|f| f.set(on));
}
pub(crate) fn take_force_report_channel_failure() -> bool {
FORCE_REPORT_CHANNEL_FAILURE.with(|f| f.replace(false))
}
pub(crate) fn report_channel_failure_armed() -> bool {
FORCE_REPORT_CHANNEL_FAILURE.with(|f| f.get())
}
pub(crate) fn set_force_placement_write_result(ret: isize) {
FORCE_PLACEMENT_WRITE_RESULT.with(|f| f.set(Some(ret)));
}
pub(crate) fn take_force_placement_write_result() -> Option<isize> {
FORCE_PLACEMENT_WRITE_RESULT.with(|f| f.take())
}
pub(crate) fn set_force_occupy_before_unwind(on: bool) {
FORCE_OCCUPY_BEFORE_UNWIND.with(|f| f.set(on));
}
pub(crate) fn take_force_occupy_before_unwind() -> bool {
FORCE_OCCUPY_BEFORE_UNWIND.with(|f| f.replace(false))
}
pub(crate) fn occupy_before_unwind_armed() -> bool {
FORCE_OCCUPY_BEFORE_UNWIND.with(|f| f.get())
}
pub(crate) fn take_reaped_orphans() -> Vec<(u32, Option<i32>)> {
REAPED_ORPHANS.with(|r| std::mem::take(&mut *r.borrow_mut()))
}
pub(crate) fn record_reaped_orphan(pid: u32, signal: Option<i32>) {
REAPED_ORPHANS.with(|r| r.borrow_mut().push((pid, signal)));
}
pub(crate) fn set_wait_polling_notifier(notify: std::sync::mpsc::Sender<()>) {
WAIT_POLLING.with(|w| *w.borrow_mut() = Some(notify));
}
pub(crate) fn notify_wait_polling() {
if let Some(notify) = WAIT_POLLING.with(|w| w.borrow_mut().take()) {
_ = notify.send(());
}
}
pub(crate) fn set_on_wait(hook: impl FnOnce() + 'static) -> crate::oneshot_hook::Armed {
crate::oneshot_hook::arm(&ON_WAIT, hook)
}
pub(crate) fn notify_wait_entry(pidfd: std::os::fd::RawFd) {
WAITED_PIDFD.with(|p| p.set(Some(pidfd)));
crate::oneshot_hook::fire(&ON_WAIT);
}
pub(crate) fn waited_pidfd() -> Option<std::os::fd::RawFd> {
WAITED_PIDFD.with(|p| p.get())
}
pub(crate) fn set_force_child_pidfd_failure(on: bool) {
FORCE_CHILD_PIDFD_FAILURE.with(|f| f.set(on));
}
pub(crate) fn take_force_child_pidfd_failure() -> bool {
FORCE_CHILD_PIDFD_FAILURE.with(|f| f.replace(false))
}
pub(crate) fn set_after_shut_read(hook: impl FnOnce() + 'static) {
AFTER_SHUT_READ.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
}
pub(crate) fn run_after_shut_read() {
if let Some(hook) = AFTER_SHUT_READ.with(|h| h.borrow_mut().take()) {
hook();
}
}
pub(crate) fn set_force_child_kill_denied(on: bool) {
FORCE_CHILD_KILL_DENIED.with(|f| f.set(on));
}
pub(crate) fn take_force_child_kill_denied() -> bool {
FORCE_CHILD_KILL_DENIED.with(|f| f.replace(false))
}
pub(crate) fn set_background_reap_notifier(notify: std::sync::mpsc::Sender<()>) {
BACKGROUND_REAP_NOTIFY.with(|n| *n.borrow_mut() = Some(notify));
}
pub(crate) fn take_background_reap_notifier() -> Option<std::sync::mpsc::Sender<()>> {
BACKGROUND_REAP_NOTIFY.with(|n| n.borrow_mut().take())
}
#[cfg(feature = "tokio")]
pub(crate) fn set_hook_gate(gate: std::os::fd::RawFd) {
HOOK_GATE.with(|g| g.set(Some(gate)));
}
pub(crate) fn take_hook_gate() -> Option<std::os::fd::RawFd> {
HOOK_GATE.with(|g| g.take())
}
pub(crate) fn set_force_child_proc_dir_failure(on: bool) {
FORCE_CHILD_PROC_DIR_FAILURE.with(|f| f.set(on));
}
pub(crate) fn take_force_child_proc_dir_failure() -> bool {
FORCE_CHILD_PROC_DIR_FAILURE.with(|f| f.replace(false))
}
pub(crate) fn set_between_check_and_kill(hook: impl FnOnce() + 'static) {
BETWEEN_CHECK_AND_KILL.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
}
pub(crate) fn run_between_check_and_kill() {
if let Some(hook) = BETWEEN_CHECK_AND_KILL.with(|h| h.borrow_mut().take()) {
hook();
}
}
pub(crate) fn set_before_exit_wait(hook: impl FnOnce() + 'static) -> BeforeExitWaitGuard {
crate::oneshot_hook::arm(&BEFORE_EXIT_WAIT, hook)
}
pub(crate) type BeforeExitWaitGuard = crate::oneshot_hook::Armed;
pub(crate) fn run_before_exit_wait() {
crate::oneshot_hook::fire(&BEFORE_EXIT_WAIT);
}
#[must_use = "dropping this immediately clears the armed hook; bind it for the scope that needs it"]
pub(crate) struct AfterForkStillLockedGuard(());
impl Drop for AfterForkStillLockedGuard {
fn drop(&mut self) {
AFTER_FORK_STILL_LOCKED.with(|h| *h.borrow_mut() = None);
}
}
pub(crate) fn set_after_fork_still_locked(hook: impl FnOnce() + 'static) -> AfterForkStillLockedGuard {
AFTER_FORK_STILL_LOCKED.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
AfterForkStillLockedGuard(())
}
pub(crate) fn run_after_fork_still_locked() {
if let Some(hook) = AFTER_FORK_STILL_LOCKED.with(|h| h.borrow_mut().take()) {
hook();
}
}
#[must_use = "dropping this immediately clears the armed hook; bind it for the scope that needs it"]
pub(crate) struct ForkRunningLockContendedGuard(());
impl Drop for ForkRunningLockContendedGuard {
fn drop(&mut self) {
ON_FORK_RUNNING_LOCK_CONTENDED.with(|h| *h.borrow_mut() = None);
}
}
pub(crate) fn set_fork_running_lock_contended(hook: impl FnOnce() + 'static) -> ForkRunningLockContendedGuard {
ON_FORK_RUNNING_LOCK_CONTENDED.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
ForkRunningLockContendedGuard(())
}
pub(crate) fn run_fork_running_lock_contended() {
if let Some(hook) = ON_FORK_RUNNING_LOCK_CONTENDED.with(|h| h.borrow_mut().take()) {
hook();
}
}
#[must_use = "dropping this immediately clears the armed hook; bind it for the scope that needs it"]
pub(crate) struct ForkRunningCleanupGuard(());
impl Drop for ForkRunningCleanupGuard {
fn drop(&mut self) {
ON_FORK_RUNNING_CLEANUP.with(|h| *h.borrow_mut() = None);
}
}
pub(crate) fn set_fork_running_cleanup(hook: impl FnOnce() + 'static) -> ForkRunningCleanupGuard {
ON_FORK_RUNNING_CLEANUP.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
ForkRunningCleanupGuard(())
}
pub(crate) fn run_fork_running_cleanup() {
if let Some(hook) = ON_FORK_RUNNING_CLEANUP.with(|h| h.borrow_mut().take()) {
hook();
}
}
#[must_use = "dropping this immediately clears the registered fd; bind it for the scope that needs it"]
pub(crate) struct ForkRunningLockHeldReportFdGuard(());
impl Drop for ForkRunningLockHeldReportFdGuard {
fn drop(&mut self) {
FORK_RUNNING_LOCK_HELD_REPORT_FD.with(|f| f.set(None));
}
}
pub(crate) fn set_fork_running_lock_held_report_fd(fd: std::os::fd::RawFd) -> ForkRunningLockHeldReportFdGuard {
FORK_RUNNING_LOCK_HELD_REPORT_FD.with(|f| f.set(Some(fd)));
ForkRunningLockHeldReportFdGuard(())
}
pub(crate) fn take_fork_running_lock_held_report_fd() -> Option<std::os::fd::RawFd> {
FORK_RUNNING_LOCK_HELD_REPORT_FD.with(|f| f.take())
}
pub(crate) fn record_signalled_by_pid() {
SIGNALLED_BY_PID.with(|c| c.set(c.get() + 1));
}
pub(crate) fn take_signalled_by_pid() -> usize {
SIGNALLED_BY_PID.with(|c| c.replace(0))
}
static PUMPS: std::sync::Mutex<Vec<(std::ffi::OsString, bool)>> = std::sync::Mutex::new(Vec::new());
pub(crate) fn record_pump(name: &std::ffi::OsStr, ended: bool) {
PUMPS
.lock()
.unwrap_or_else(|e| e.into_inner())
.push((name.to_os_string(), ended));
}
pub(crate) fn pumps_of(name: &str) -> (usize, usize) {
let pumps = PUMPS.lock().unwrap_or_else(|e| e.into_inner());
let of = |ended| {
pumps
.iter()
.filter(|(n, e)| n.as_os_str() == name && *e == ended)
.count()
};
(of(false), of(true))
}
static ARMS: std::sync::Mutex<Vec<std::ffi::OsString>> = std::sync::Mutex::new(Vec::new());
pub(crate) fn record_arm(name: &std::ffi::OsStr) {
ARMS.lock().unwrap_or_else(|e| e.into_inner()).push(name.to_os_string());
}
#[cfg(feature = "tokio")]
pub(crate) fn arms_of(name: &str) -> usize {
ARMS.lock()
.unwrap_or_else(|e| e.into_inner())
.iter()
.filter(|n| n.as_os_str() == name)
.count()
}
static PUMP_SEAMS: std::sync::Mutex<Vec<(std::ffi::OsString, PumpSeam)>> = std::sync::Mutex::new(Vec::new());
enum PumpSeam {
Fail,
Batches(std::sync::mpsc::Sender<bool>),
}
pub(crate) fn set_force_pump_failure(name: &str) {
PUMP_SEAMS
.lock()
.unwrap_or_else(|e| e.into_inner())
.push((name.into(), PumpSeam::Fail));
}
pub(crate) fn take_force_pump_failure(name: &std::ffi::OsStr) -> bool {
let mut seams = PUMP_SEAMS.lock().unwrap_or_else(|e| e.into_inner());
let at = seams
.iter()
.position(|(n, s)| n.as_os_str() == name && matches!(s, PumpSeam::Fail));
at.map(|at| seams.remove(at)).is_some()
}
pub(crate) fn set_pump_batch_notifier(name: &str, notify: std::sync::mpsc::Sender<bool>) {
PUMP_SEAMS
.lock()
.unwrap_or_else(|e| e.into_inner())
.push((name.into(), PumpSeam::Batches(notify)));
}
pub(crate) fn notify_pump_batch(name: &std::ffi::OsStr, notified: bool) {
for (n, seam) in PUMP_SEAMS.lock().unwrap_or_else(|e| e.into_inner()).iter() {
if let (true, PumpSeam::Batches(notify)) = (n.as_os_str() == name, seam) {
_ = notify.send(notified);
}
}
}
pub(crate) fn set_force_fork_running_pidfd_failure(on: bool) {
FORCE_FORK_RUNNING_PIDFD_FAILURE.with(|f| f.set(on));
}
pub(crate) fn take_force_fork_running_pidfd_failure() -> bool {
FORCE_FORK_RUNNING_PIDFD_FAILURE.with(|f| f.replace(false))
}
pub(crate) fn take_fork_running_pidfd_failure_probe() -> Option<std::os::fd::OwnedFd> {
FORK_RUNNING_PIDFD_FAILURE_PROBE.with(|p| p.borrow_mut().take())
}
pub(crate) fn record_fork_running_pidfd_failure_probe(probe: std::os::fd::OwnedFd) {
FORK_RUNNING_PIDFD_FAILURE_PROBE.with(|p| *p.borrow_mut() = Some(probe));
}
pub(crate) fn set_force_fork_running_probe_pidfd_failure(on: bool) {
FORCE_FORK_RUNNING_PROBE_PIDFD_FAILURE.with(|f| f.set(on));
}
pub(crate) fn take_force_fork_running_probe_pidfd_failure() -> bool {
FORCE_FORK_RUNNING_PROBE_PIDFD_FAILURE.with(|f| f.replace(false))
}
pub(crate) fn set_force_kill_on_drop_waitid_eintr(on: bool) {
FORCE_KILL_ON_DROP_WAITID_EINTR.with(|f| f.set(on));
}
pub(crate) fn take_force_kill_on_drop_waitid_eintr() -> bool {
FORCE_KILL_ON_DROP_WAITID_EINTR.with(|f| f.replace(false))
}
pub(crate) fn set_force_kill_on_drop_kill_failure(on: bool) {
FORCE_KILL_ON_DROP_KILL_FAILURE.with(|f| f.set(on));
}
pub(crate) fn take_force_kill_on_drop_kill_failure() -> bool {
FORCE_KILL_ON_DROP_KILL_FAILURE.with(|f| f.replace(false))
}
pub(crate) fn set_on_take_placement(hook: impl FnOnce() + 'static) -> crate::oneshot_hook::Armed {
crate::oneshot_hook::arm(&ON_TAKE_PLACEMENT, hook)
}
pub(crate) fn run_on_take_placement() {
crate::oneshot_hook::fire(&ON_TAKE_PLACEMENT);
}
pub(crate) fn set_before_state_read(hook: impl FnOnce() + 'static) -> crate::oneshot_hook::Armed {
crate::oneshot_hook::arm(&BEFORE_STATE_READ, hook)
}
pub(crate) fn run_before_state_read() {
crate::oneshot_hook::fire(&BEFORE_STATE_READ);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ChildDeath {
BeforeIntent,
BeforeReport,
}
#[cfg(feature = "tokio")]
pub(crate) fn kill_child_at(at: ChildDeath) -> ChildDies {
CHILD_DIES.with(|c| c.set(Some(at)));
ChildDies(())
}
#[cfg(feature = "tokio")]
#[must_use = "children live again as soon as the guard is dropped"]
pub(crate) struct ChildDies(());
#[cfg(feature = "tokio")]
impl Drop for ChildDies {
fn drop(&mut self) {
CHILD_DIES.with(|c| c.set(None));
}
}
pub(crate) fn die_if_at(at: ChildDeath) {
if CHILD_DIES.with(Cell::get) == Some(at) {
unsafe { libc::kill(libc::getpid(), libc::SIGKILL) };
}
}
pub(crate) fn force_pidfd_cgroup_id(id: u64) -> ForcedPidfdCgroupId {
FORCED_PIDFD_CGROUP_ID.with(|f| f.set(Some(id)));
ForcedPidfdCgroupId(())
}
#[must_use = "the id is read again as soon as the guard is dropped"]
pub(crate) struct ForcedPidfdCgroupId(());
impl Drop for ForcedPidfdCgroupId {
fn drop(&mut self) {
FORCED_PIDFD_CGROUP_ID.with(|f| f.set(None));
}
}
pub(crate) fn forced_pidfd_cgroup_id() -> Option<u64> {
FORCED_PIDFD_CGROUP_ID.with(Cell::get)
}
pub(crate) fn fail_subtree(errno: i32) -> FailSubtree {
SUBTREE_FAILS.with(|f| f.set(Some(errno)));
FailSubtree(())
}
#[must_use = "the subtree is captured again as soon as the guard is dropped"]
pub(crate) struct FailSubtree(());
impl Drop for FailSubtree {
fn drop(&mut self) {
SUBTREE_FAILS.with(|f| f.set(None));
}
}
pub(crate) fn subtree_fails() -> Option<i32> {
SUBTREE_FAILS.with(Cell::get)
}
type WalkHook = Box<dyn FnMut(&std::path::Path)>;
pub(crate) fn set_before_walk_open(hook: impl FnMut(&std::path::Path) + 'static) -> BeforeWalkOpen {
BEFORE_WALK_OPEN.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
BeforeWalkOpen(())
}
#[must_use = "the hook is cleared as soon as the guard is dropped"]
pub(crate) struct BeforeWalkOpen(());
impl Drop for BeforeWalkOpen {
fn drop(&mut self) {
BEFORE_WALK_OPEN.with(|h| h.borrow_mut().take());
}
}
pub(crate) fn run_before_walk_open(path: &std::path::Path) {
let hook = BEFORE_WALK_OPEN.with(|h| h.borrow_mut().take());
if let Some(mut hook) = hook {
hook(path);
BEFORE_WALK_OPEN.with(|h| {
h.borrow_mut().get_or_insert(hook);
});
}
}
type WalkFailure = (std::path::PathBuf, WalkStep, i32);
pub(crate) fn fail_walk_step(path: &str, step: WalkStep, errno: i32) -> FailWalkStep {
WALK_FAILURE.with(|f| *f.borrow_mut() = Some((path.into(), step, errno)));
FailWalkStep(())
}
#[must_use = "the step succeeds again as soon as the guard is dropped"]
pub(crate) struct FailWalkStep(());
impl Drop for FailWalkStep {
fn drop(&mut self) {
WALK_FAILURE.with(|f| f.borrow_mut().take());
}
}
pub(crate) fn walk_step_failure(path: &std::path::Path, step: WalkStep) -> Option<i32> {
WALK_FAILURE.with(|f| match &*f.borrow() {
Some((at, failing, errno)) if at == path && *failing == step => Some(*errno),
_ => None,
})
}
pub(crate) fn fail_cgroup_id(errno: i32) -> FailCgroupId {
CGROUP_ID_FAILS.with(|f| f.set(Some(errno)));
FailCgroupId(())
}
#[must_use = "the id reads again as soon as the guard is dropped"]
pub(crate) struct FailCgroupId(());
impl Drop for FailCgroupId {
fn drop(&mut self) {
CGROUP_ID_FAILS.with(|f| f.set(None));
}
}
pub(crate) fn cgroup_id_fails() -> Option<i32> {
CGROUP_ID_FAILS.with(Cell::get)
}
pub(crate) fn fail_liveness_read(errno: i32) -> FailLivenessRead {
LIVENESS_READ_FAILS.with(|f| f.set(Some(errno)));
FailLivenessRead(())
}
#[must_use = "the liveness reads again as soon as the guard is dropped"]
pub(crate) struct FailLivenessRead(());
impl Drop for FailLivenessRead {
fn drop(&mut self) {
LIVENESS_READ_FAILS.with(|f| f.set(None));
}
}
pub(crate) fn liveness_read_fails() -> Option<i32> {
LIVENESS_READ_FAILS.with(Cell::get)
}