use std::{io, os::unix::fs::PermissionsExt, path::Path};
mod desktop;
pub(crate) use desktop::open_desktop;
pub(crate) const USES_PATHEXT: bool = false;
const EXECUTE_BITS: u32 = 0o111;
pub(crate) fn is_executable(path: &Path) -> bool {
path.metadata().is_ok_and(|metadata| {
metadata.is_file() && metadata.permissions().mode() & EXECUTE_BITS != 0
})
}
pub(crate) struct Tree {
pub(crate) child: tokio::process::Child,
group: Option<GroupId>,
}
struct GroupId(rustix::process::Pid);
const CHILD_PID_AS_GROUP: i32 = 0;
pub(crate) fn spawn_tree(command: &mut tokio::process::Command) -> io::Result<Tree> {
command.process_group(CHILD_PID_AS_GROUP);
let child = command.spawn()?;
let group = child.id().map(|pid| {
let pid = i32::try_from(pid).expect("Unix process IDs fit pid_t");
GroupId(rustix::process::Pid::from_raw(pid).expect("a child's process ID is positive"))
});
Ok(Tree { child, group })
}
impl Tree {
pub(crate) fn kill(&mut self) -> std::io::Result<()> {
if let Some(group) = self.group.take() {
match rustix::process::kill_process_group(group.0, rustix::process::Signal::KILL) {
Ok(()) | Err(rustix::io::Errno::SRCH) => {}
Err(error) => return Err(error.into()),
}
}
Ok(())
}
}
impl Drop for Tree {
fn drop(&mut self) {
let _ = self.kill();
}
}