cosca 0.5.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! Waiting for a cgroup leaf to drain: its `populated` reading 0, or the leaf being removed.
//!
//! A `cgroup.events` notification alone is not enough. The kernel rate-limits them: one that comes
//! within 10 ms of the previous is postponed on a timer (`cgroup_file_notify`), and removing the
//! cgroup cancels that timer (`cgroup_rm_file`'s `del_timer_sync`) without notifying. Removing the
//! file wakes no `poll` waiter either (`kernfs_drain_open_files`). So a leaf that drains and is
//! removed at once, by any party, can leave a waiter on `cgroup.events` blocked for good. What a
//! removal does deliver is `IN_DELETE` to its parent directory's watchers, since every cgroup
//! `rmdir` goes through `vfs_rmdir`, which ends in `d_delete_notify` → `fsnotify_delete`. (Kernel
//! v6.12: `kernel/cgroup/cgroup.c`, `fs/kernfs/file.c`, `fs/namei.c`, `include/linux/fsnotify.h`.)
//!
//! So the watch is one inotify instance holding two watches, both armed before the first read:
//! `IN_MODIFY` on `cgroup.events`, which the kernel's notification work delivers once it runs
//! (`kernfs_notify_workfn`; the watch keeps the file's inode cached, which that work needs), and
//! `IN_DELETE` on the parent, for the leaf's own name. Both bind to the held inodes, through
//! `/proc/self/fd`, never to a path a mount could redirect.
//!
//! Each leaf arms one watch, at creation, read by the leaf's pump for every wait on the leaf
//! (see `watcher.rs`), and by the leaf's own teardown once the pump is stopped.
//!
//! An inotify instance counts against `fs.inotify.max_user_instances` (128 per user by default),
//! and each watch against `fs.inotify.max_user_watches`. Arming one can therefore fail, and a
//! failure is returned, never replaced by a weaker wait.
//!
//! A leaf's watch is held for the leaf's life, and its parent watch queues an event for every
//! sibling removed meanwhile, up to `fs.inotify.max_queued_events` (16384 by default) per
//! instance, kernel memory the user is charged for. A full queue loses events after an
//! `IN_Q_OVERFLOW`, which is harmless here: the pump empties the queue as events arrive, takes
//! an overflow as a change (so every wait reads `cgroup.events` again), and a removed leaf reads
//! as drained (`ENODEV`). A sibling's removal is taken in and ignored.

use std::ffi::OsString;
use std::fs::File;
use std::io;
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd};

use rustix::fs::inotify;

use super::{above_stdio, fd_path, read_populated, removed_after_drain, LeafDir};
use crate::error::Error;

/// A watch on one leaf's drain. See the module docs.
pub(crate) struct DrainWatch {
    /// The leaf's `cgroup.events`, read for `populated`.
    events: File,
    /// The inotify instance: `IN_MODIFY` on `cgroup.events`, `IN_DELETE` on the parent.
    fd: OwnedFd,
    /// The parent's watch.
    parent: i32,
    events_watch: i32,
    /// The leaf's name in its parent.
    name: OsString,
    /// Whether the leaf was seen removed.
    gone: bool,
    buf: String,
}

impl DrainWatch {
    /// Arm a watch on the leaf `dir`, before anything is read. `None`: the leaf is already gone.
    pub(crate) fn arm(dir: &LeafDir) -> io::Result<Option<DrainWatch>> {
        let events = match dir.open("cgroup.events", rustix::fs::OFlags::RDONLY) {
            Ok(fd) => File::from(above_stdio(fd)?),
            Err(e) if removed_after_drain(&e) => return Ok(None),
            Err(e) => return Err(e),
        };
        #[cfg(test)]
        super::fault::record_arm(dir.name());
        #[cfg(test)]
        if super::fault::take_force_inotify_failure() {
            return Err(io::Error::from_raw_os_error(libc::EMFILE));
        }
        let fd = above_stdio(inotify::init(
            inotify::CreateFlags::CLOEXEC | inotify::CreateFlags::NONBLOCK,
        )?)?;
        let parent = inotify::add_watch(
            &fd,
            fd_path(dir.parent()),
            inotify::WatchFlags::DELETE | inotify::WatchFlags::ONLYDIR,
        )?;
        let events_watch = inotify::add_watch(&fd, fd_path(events.as_fd()), inotify::WatchFlags::MODIFY)?;
        // A removal between the open and the parent's watch is still seen: reads through `events`
        // then fail with `ENODEV`.
        Ok(Some(DrainWatch {
            events,
            fd,
            parent,
            events_watch,
            name: dir.name().to_os_string(),
            gone: false,
            buf: String::new(),
        }))
    }

    /// Whether an event taken in so far said the leaf was removed.
    pub(crate) fn saw_removal(&self) -> bool {
        self.gone
    }

    /// Whether the leaf still has a member. A removed leaf has none.
    pub(crate) fn populated(&mut self) -> Result<bool, Error> {
        if self.gone {
            return Ok(false);
        }
        read_populated(&mut self.events, &mut self.buf)
    }

    /// Take in what made the watch readable, without blocking. `true` if any of it bears on the
    /// leaf: a write to `cgroup.events`, the leaf's own removal, a watch the kernel dropped, or an
    /// overflowed queue, which may have lost any of these. A sibling's removal does not.
    pub(crate) fn consume(&mut self) -> Result<bool, Error> {
        use inotify::ReadFlags;

        let mut changed = false;
        let mut buf = [std::mem::MaybeUninit::<u8>::uninit(); 4096];
        let mut reader = inotify::Reader::new(&self.fd, &mut buf);
        loop {
            match reader.next() {
                Ok(event) => {
                    let flags = event.events();
                    let named_us = event
                        .file_name()
                        .is_some_and(|n| n.to_bytes() == self.name.as_encoded_bytes());
                    if event.wd() == self.parent && flags.contains(ReadFlags::DELETE) && named_us {
                        self.gone = true;
                        changed = true;
                    }
                    if flags.intersects(ReadFlags::QUEUE_OVERFLOW | ReadFlags::IGNORED)
                        || (event.wd() == self.events_watch && flags.contains(ReadFlags::MODIFY))
                    {
                        changed = true;
                    }
                }
                Err(rustix::io::Errno::AGAIN) => return Ok(changed),
                Err(rustix::io::Errno::INTR) => {}
                Err(e) => return Err(Error::Io(e.into())),
            }
        }
    }

    /// Block until the leaf drains — `populated` reads 0, or the leaf is removed. Unbounded.
    pub(crate) fn wait(&mut self) -> Result<(), Error> {
        use rustix::event::{poll, PollFd, PollFlags};

        loop {
            if !self.populated()? {
                return Ok(());
            }
            #[cfg(test)]
            super::fault::notify_drain_blocking();
            let mut fds = [PollFd::from_borrowed_fd(self.fd.as_fd(), PollFlags::IN)];
            match poll(&mut fds, None) {
                Ok(_) => drop(self.consume()?),
                Err(rustix::io::Errno::INTR) => {}
                Err(e) => return Err(Error::Io(e.into())),
            }
        }
    }
}

impl AsRawFd for DrainWatch {
    fn as_raw_fd(&self) -> RawFd {
        self.fd.as_raw_fd()
    }
}

impl AsFd for DrainWatch {
    fn as_fd(&self) -> BorrowedFd<'_> {
        self.fd.as_fd()
    }
}

#[cfg(test)]
#[path = "drain_tests.rs"]
mod drain_tests;