alnyan/yggdrasil: better pipes

This commit is contained in:
Mark Poliakov 2024-11-06 19:40:52 +02:00
parent de2085ff0a
commit f076438d64
4 changed files with 49 additions and 4 deletions

View File

@ -6,6 +6,7 @@ pub mod device;
pub mod mapping;
pub mod message_channel;
pub mod net;
pub mod pid;
pub mod pipe;
pub mod poll;
pub mod shared_memory;
@ -14,7 +15,6 @@ pub mod timer;
#[unstable(feature = "yggdrasil_os", issue = "none")]
pub use net::raw_socket;
#[unstable(feature = "yggdrasil_os", issue = "none")]
pub use yggdrasil_rt::io::{FileMetadataUpdate, FileMetadataUpdateMode, FileMode as RawFileMode};

View File

@ -0,0 +1,34 @@
use yggdrasil_rt::process::ProcessId as OsProcessId;
use yggdrasil_rt::sys as syscall;
use crate::io;
use crate::os::fd::{AsRawFd, FromRawFd, RawFd};
use crate::sys::cvt_io;
use crate::sys::fd::FileDesc;
pub type ProcessId = OsProcessId;
pub struct PidFd {
inner: FileDesc,
}
impl PidFd {
pub fn new<P: Into<ProcessId>>(pid: P) -> io::Result<Self> {
let fd = cvt_io(unsafe { syscall::create_pid(pid.into()) })?;
let fd = unsafe { FileDesc::from_raw_fd(fd) };
Ok(Self { inner: fd })
}
pub fn status(&self) -> io::Result<i32> {
let mut buf = [0; size_of::<i32>()];
let len = cvt_io(unsafe { syscall::read(self.inner.as_raw_fd(), &mut buf) })?;
assert_eq!(len, size_of::<i32>());
Ok(i32::from_le_bytes(buf))
}
}
impl AsRawFd for PidFd {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}

View File

@ -1,5 +1,6 @@
#![unstable(feature = "yggdrasil_os", issue = "none")]
use yggdrasil_rt::io::PipeOptions;
use yggdrasil_rt::sys as syscall;
use crate::mem::MaybeUninit;
@ -7,9 +8,19 @@ use crate::os::fd::{FromRawFd, OwnedFd};
use crate::sys::cvt_io;
#[unstable(feature = "yggdrasil_os", issue = "none")]
pub fn create_pipe_pair() -> crate::io::Result<(OwnedFd, OwnedFd)> {
pub fn create_pipe_pair(
read_nonblocking: bool,
write_nonblocking: bool,
) -> crate::io::Result<(OwnedFd, OwnedFd)> {
let mut buffer = MaybeUninit::uninit_array();
cvt_io(unsafe { syscall::create_pipe(&mut buffer) })?;
let mut options = PipeOptions::empty();
if read_nonblocking {
options |= PipeOptions::READ_NONBLOCKING;
}
if write_nonblocking {
options |= PipeOptions::WRITE_NONBLOCKING;
}
cvt_io(unsafe { syscall::create_pipe(&mut buffer, options) })?;
let read = unsafe { OwnedFd::from_raw_fd(buffer[0].assume_init()) };
let write = unsafe { OwnedFd::from_raw_fd(buffer[1].assume_init()) };
Ok((read, write))

View File

@ -95,7 +95,7 @@ impl Stdio {
Stdio::Inherit => Ok((ChildStdio::Inherit, None)),
Stdio::Null => Ok((ChildStdio::Null, None)),
Stdio::MakePipe => {
let (read, write) = os_pipe::create_pipe_pair()?;
let (read, write) = os_pipe::create_pipe_pair(false, false)?;
if readable {
let write = AnonPipe::from_inner(write);