From a5c02a3aa8a2a023e4630d687698abc9782debfb Mon Sep 17 00:00:00 2001 From: Mark Poliakov Date: Mon, 8 Jan 2024 18:44:27 +0200 Subject: [PATCH] Add C compatability fork()/execve() --- src/path/path.rs | 22 ++++++++++++++++++++++ src/path/path_buf.rs | 4 ++++ src/process.rs | 10 ++++++++++ src/syscall.rs | 9 +++++++++ 4 files changed, 45 insertions(+) diff --git a/src/path/path.rs b/src/path/path.rs index 40c3090b..c1621e0c 100644 --- a/src/path/path.rs +++ b/src/path/path.rs @@ -1,3 +1,7 @@ +use core::ffi::CStr; + +use crate::error::Error; + #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] pub struct Path(str); @@ -15,6 +19,12 @@ impl Path { unsafe { core::mem::transmute(s) } } + pub fn from_c_str(s: &CStr) -> Result<&Self, Error> { + s.to_str() + .map_err(|_| Error::InvalidArgument) + .map(Path::from_str) + } + pub fn empty() -> &'static Self { Self::from_str("") } @@ -76,6 +86,11 @@ impl Path { pub fn as_ptr(&self) -> *const u8 { self.0.as_ptr() } + + #[inline] + pub fn as_str(&self) -> &str { + &self.0 + } } impl AsRef for Path { @@ -95,3 +110,10 @@ impl AsRef for str { Path::from_str(self) } } + +#[cfg(feature = "alloc")] +impl AsRef for alloc::string::String { + fn as_ref(&self) -> &Path { + self.as_str().as_ref() + } +} diff --git a/src/path/path_buf.rs b/src/path/path_buf.rs index 20a24fe9..d43c527f 100644 --- a/src/path/path_buf.rs +++ b/src/path/path_buf.rs @@ -21,6 +21,10 @@ impl PathBuf { todo!() } + pub fn join>(&mut self, _path: P) -> Self { + todo!() + } + pub fn display(&self) -> &str { self.0.as_str() } diff --git a/src/process.rs b/src/process.rs index 2e20f6d9..a663b7f2 100644 --- a/src/process.rs +++ b/src/process.rs @@ -68,6 +68,16 @@ pub struct SpawnOptions<'a> { pub optional: &'a [SpawnOption], } +/// Legacy execve(2) arguments +#[allow(missing_docs)] +#[derive(Clone, Debug)] +#[repr(C)] +pub struct ExecveOptions<'a> { + pub program: &'a str, + pub arguments: &'a [&'a str], + pub environment: &'a [&'a str], +} + /// Controls how threads are spawned within a process #[derive(Clone, Debug)] #[repr(C)] diff --git a/src/syscall.rs b/src/syscall.rs index 9e5464a0..0cb604b9 100644 --- a/src/syscall.rs +++ b/src/syscall.rs @@ -56,6 +56,9 @@ primitive_enum!( #[doc = "Creates a pair of pseudo-terminal master/slave"] CreatePty = 24, + #[doc = "Clones a file descriptor"] + CloneFd = 25, + #[doc = "Create and start a new process"] SpawnProcess = 40, #[doc = "Wait for a process to exit"] @@ -86,6 +89,12 @@ primitive_enum!( #[doc = "Terminate the caller process"] ExitProcess = 54, + // C compat + #[doc = "Fork a process, creating an identical copy of its address space (C compat)"] + Fork = 55, + #[doc = "Replace the process address space with a new loaded program"] + Exec = 56, + #[doc = "Mount a filesystem"] Mount = 101, #[doc = "Unmount a filesystem"]