MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / install_bsd_kqueue_watcher

Function install_bsd_kqueue_watcher

crates/openshell-driver-vm/src/procguard.rs:132–198  ·  view source on GitHub ↗
(cleanup: F)

Source from the content-addressed store, hash-verified

130 target_os = "dragonfly",
131))]
132fn install_bsd_kqueue_watcher<F>(cleanup: F) -> Result<(), String>
133where
134 F: FnOnce() + Send + 'static,
135{
136 use nix::unistd::getppid;
137 use polling::os::kqueue::{PollerKqueueExt, Process, ProcessOps};
138 use polling::{Events, PollMode, Poller};
139
140 let parent_pid = getppid();
141 if parent_pid == nix::unistd::Pid::from_raw(1) {
142 return Err("process was already orphaned before procguard armed".to_string());
143 }
144 let parent_pid_nz = std::num::NonZeroI32::new(parent_pid.as_raw())
145 .ok_or_else(|| "getppid returned 0 unexpectedly".to_string())?;
146
147 // Build the poller on the caller's thread so any setup error
148 // surfaces synchronously. `EVFILT_PROC | NOTE_EXIT` is a one-shot
149 // filter, so `PollMode::Oneshot` matches the kernel semantics.
150 //
151 // SAFETY: `Process::from_pid` requires the PID to "be tied to an
152 // actual child process". Our parent is alive at this point — we
153 // re-check `getppid()` immediately after registration to close the
154 // race where the parent dies between the read above and the
155 // `add_filter` call. The BSD kqueue implementation accepts any
156 // live PID, not just our own children; the "child" wording in the
157 // polling docs is carried over from historical terminology in the
158 // kqueue(2) manpage. The kernel guarantees NOTE_EXIT fires if the
159 // PID is valid at registration.
160 let poller = Poller::new().map_err(|err| format!("polling: Poller::new failed: {err}"))?;
161 let key = 1;
162 #[allow(unsafe_code)]
163 // SAFETY requirement is documented on the enclosing function: the
164 // PID was just read from `getppid()` and re-checked below, so it
165 // points at a live process. `Process::from_pid` is an
166 // entry-in-the-kernel-table registration — the kernel validates
167 // the PID when the filter is added.
168 let filter = unsafe { Process::from_pid(parent_pid_nz, ProcessOps::Exit) };
169 poller
170 .add_filter(filter, key, PollMode::Oneshot)
171 .map_err(|err| format!("polling: add_filter(NOTE_EXIT, {parent_pid_nz}) failed: {err}"))?;
172
173 // Between getppid() and the registered filter the parent may
174 // already have died. Detect that and abort so the caller can bail.
175 if getppid() != parent_pid {
176 return Err("parent exited before procguard could arm".to_string());
177 }
178
179 // Hand off to a dedicated OS thread. Block in `poller.wait()`
180 // until the single NOTE_EXIT event fires, run the cleanup, then
181 // exit. We prefer `exit(1)` over `kill(getpid, SIGKILL)` so the
182 // callback gets to complete — SIGKILL would race it. Our children
183 // have their own procguards armed and will notice `getppid() ==
184 // 1` shortly after, so we do not need Linux-semantics exactness.
185 std::thread::Builder::new()
186 .name("procguard".to_string())
187 .spawn(move || {
188 let mut events = Events::new();
189 // Block indefinitely; the filter is Oneshot so we expect

Callers 1

die_with_parent_cleanupFunction · 0.85

Calls 3

waitMethod · 0.80
spawnMethod · 0.45
nameMethod · 0.45

Tested by

no test coverage detected