1
0
Fork 0
forked from wry/wry

all: use run-on-drop crate

This commit is contained in:
Julian Orth 2026-02-13 11:09:07 +01:00
parent 346c6a7345
commit 0932ad11b5
19 changed files with 60 additions and 110 deletions

View file

@ -2,8 +2,9 @@ use {
crate::{
forker::ForkerError,
pr_caps::drop_all_pr_caps,
utils::{errorfmt::ErrorFmt, on_drop::OnDrop, process_name::set_process_name},
utils::{errorfmt::ErrorFmt, process_name::set_process_name},
},
run_on_drop::on_drop,
std::{env, mem::MaybeUninit, process, slice, str::FromStr},
uapi::{Msghdr, MsghdrMut, OwnedFd, c},
};
@ -47,7 +48,7 @@ pub fn double_fork() -> Result<Option<OwnedFd>, ForkerError> {
match fork_with_pidfd(false)? {
Forked::Parent { pid, .. } => {
drop(c);
let _wait = OnDrop(|| {
let _wait = on_drop(|| {
let _ = uapi::waitpid(pid, 0);
});
recv_pidfd(&p).map(Some)

View file

@ -1,46 +0,0 @@
use std::{mem, mem::ManuallyDrop};
pub struct OnDrop<F>(pub F)
where
F: FnMut() + Copy;
impl<F: FnMut() + Copy> OnDrop<F> {
pub fn forget(self) {
mem::forget(self);
}
}
impl<F: FnMut() + Copy> Drop for OnDrop<F> {
fn drop(&mut self) {
(self.0)();
}
}
pub struct OnDrop2<F>
where
F: FnOnce(),
{
f: ManuallyDrop<F>,
}
impl<F: FnOnce()> OnDrop2<F> {
pub fn new(f: F) -> Self {
Self {
f: ManuallyDrop::new(f),
}
}
pub fn forget(mut self) {
unsafe {
ManuallyDrop::drop(&mut self.f);
}
mem::forget(self);
}
}
impl<F: FnOnce()> Drop for OnDrop2<F> {
fn drop(&mut self) {
let f = unsafe { ManuallyDrop::take(&mut self.f) };
f();
}
}