Add C compatability fork()/execve()

This commit is contained in:
Mark Poliakov 2024-01-08 18:44:27 +02:00
parent fba21b5743
commit a5c02a3aa8
4 changed files with 45 additions and 0 deletions

View File

@ -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<str> for Path {
@ -95,3 +110,10 @@ impl AsRef<Path> for str {
Path::from_str(self)
}
}
#[cfg(feature = "alloc")]
impl AsRef<Path> for alloc::string::String {
fn as_ref(&self) -> &Path {
self.as_str().as_ref()
}
}

View File

@ -21,6 +21,10 @@ impl PathBuf {
todo!()
}
pub fn join<P: AsRef<Path>>(&mut self, _path: P) -> Self {
todo!()
}
pub fn display(&self) -> &str {
self.0.as_str()
}

View File

@ -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)]

View File

@ -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"]