runtime: move pidfd support to yggdrasil-rt
This commit is contained in:
@@ -19,7 +19,19 @@ pub use paths::*;
|
||||
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
use abi::{error::Error, option::OptionValue};
|
||||
use abi::{error::Error, option::OptionValue, process::ProcessId};
|
||||
|
||||
pub fn read_pid_fd(fd: RawFd) -> Result<(ProcessId, i32), Error> {
|
||||
let mut buffer = [0; size_of::<u32>() + size_of::<i32>()];
|
||||
let len = unsafe { crate::sys::read(fd, &mut buffer) }?;
|
||||
assert_eq!(len, buffer.len());
|
||||
let mut word = [0; size_of::<u32>()];
|
||||
word.copy_from_slice(&buffer[0..4]);
|
||||
let pid = unsafe { ProcessId::from_raw(u32::from_ne_bytes(word)) };
|
||||
word.copy_from_slice(&buffer[4..8]);
|
||||
let status = i32::from_ne_bytes(word);
|
||||
Ok((pid, status))
|
||||
}
|
||||
|
||||
pub fn remove_file(at: Option<RawFd>, path: &str) -> Result<(), Error> {
|
||||
unsafe { crate::sys::remove(at, path, RemoveFlags::empty()) }
|
||||
|
||||
@@ -1,28 +1,37 @@
|
||||
use std::{
|
||||
io,
|
||||
os::{
|
||||
fd::{AsRawFd, RawFd},
|
||||
yggdrasil::io::pid::{PidFd as YggPidFd, ProcessId},
|
||||
},
|
||||
os::fd::{AsRawFd, FromRawFd, OwnedFd, RawFd},
|
||||
};
|
||||
|
||||
use runtime::rt::{
|
||||
process::{ProcessId, ProcessWait, WaitFlags},
|
||||
sys as syscall,
|
||||
};
|
||||
|
||||
use crate::sys::PidFd;
|
||||
|
||||
pub struct PidFdImpl(YggPidFd);
|
||||
pub struct PidFdImpl {
|
||||
fd: OwnedFd,
|
||||
}
|
||||
|
||||
impl PidFd for PidFdImpl {
|
||||
fn new(pid: u32) -> io::Result<Self> {
|
||||
let pid = unsafe { ProcessId::from_raw(pid) };
|
||||
YggPidFd::child(pid, false).map(Self)
|
||||
let target = ProcessWait::Process(pid);
|
||||
let fd = unsafe { syscall::create_pid(&target, WaitFlags::NON_BLOCKING) }
|
||||
.map_err(io::Error::from)?;
|
||||
let fd = unsafe { OwnedFd::from_raw_fd(fd) };
|
||||
Ok(Self { fd })
|
||||
}
|
||||
|
||||
fn exit_status(&self) -> io::Result<i32> {
|
||||
self.0.status()
|
||||
let (_, code) = runtime::rt::io::read_pid_fd(self.fd.as_raw_fd())?;
|
||||
Ok(code)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRawFd for PidFdImpl {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.0.as_raw_fd()
|
||||
self.fd.as_raw_fd()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user