Spawn, WaitProcess syscalls

This commit is contained in:
Mark Poliakov 2023-07-25 10:49:26 +03:00
parent d371dabecd
commit 91a4a93569
6 changed files with 149 additions and 1 deletions

View File

@ -114,3 +114,32 @@ impl SyscallResult for RawFd {
self.0 as usize
}
}
impl SyscallResult for u64 {
fn from_syscall_result(value: usize) -> Result<Self, Error> {
if (value as isize) < 0 {
Err(Error::from_syscall_error(value))
} else {
Ok(value as _)
}
}
fn into_syscall_result(self) -> usize {
assert_eq!(self >> 63, 0);
self as _
}
}
impl SyscallResult for u32 {
fn from_syscall_result(value: usize) -> Result<Self, Error> {
if (value as isize) < 0 {
Err(Error::from_syscall_error(value))
} else {
Ok(value as _)
}
}
fn into_syscall_result(self) -> usize {
self as _
}
}

View File

@ -75,6 +75,17 @@ pub struct FileAttr {
pub mode: FileMode,
}
/// Describes a seek operation for a regular file
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SeekFrom {
/// Argument is an offset from the start of the file
Start(u64),
/// Argument is an offset from the end of the file
End(i64),
/// Argument is an offset from the current position
Current(i64),
}
impl FileMode {
/// Default mode for a regular file
pub const fn default_file() -> Self {
@ -86,3 +97,74 @@ impl FileMode {
Self(0o777)
}
}
impl From<SeekFrom> for u64 {
fn from(value: SeekFrom) -> Self {
match value {
SeekFrom::Start(value) => value << 2,
SeekFrom::End(value) => (value << 2) as u64 | 1,
SeekFrom::Current(value) => (value << 2) as u64 | 2,
}
}
}
impl From<u64> for SeekFrom {
fn from(value: u64) -> Self {
let dir = value & 0x3;
match dir {
0 => Self::Start((value as u64) >> 2),
1 => Self::End((value as i64) >> 2),
2 => Self::Current((value as i64) >> 2),
_ => panic!(),
}
}
}
#[cfg(test)]
mod tests {
use crate::io::SeekFrom;
#[test]
fn test_seek_from_from() {
assert_eq!(SeekFrom::from(0), SeekFrom::Start(0));
assert_eq!(SeekFrom::from(1), SeekFrom::End(0));
assert_eq!(SeekFrom::from(2), SeekFrom::Current(0));
assert_eq!(
SeekFrom::from(0 | 0xF000000000000000),
SeekFrom::Start(0xF000000000000000u64 >> 2)
);
assert_eq!(
SeekFrom::from(1 | (-1234i64 << 2) as u64),
SeekFrom::End(-1234)
);
assert_eq!(
SeekFrom::from(2 | (-4321i64 << 2) as u64),
SeekFrom::Current(-4321)
);
}
#[test]
fn test_seek_from_into() {
assert_eq!(u64::from(SeekFrom::Start(0)), 0);
assert_eq!(u64::from(SeekFrom::End(-1)), (-1i64 << 2) as u64 | 1);
}
#[test]
fn test_seek_from_inversible() {
for value in &[
SeekFrom::Start(0),
SeekFrom::Start(1234),
SeekFrom::End(-1234),
SeekFrom::End(0),
SeekFrom::Current(1234),
SeekFrom::Current(-5678),
] {
let to_u64 = u64::from(*value);
let from_u64 = SeekFrom::from(to_u64);
assert_eq!(*value, from_u64);
}
}
}

View File

@ -3,7 +3,7 @@
mod file;
mod terminal;
pub use file::{FileAttr, FileMode, FileType, OpenOptions};
pub use file::{FileAttr, FileMode, FileType, OpenOptions, SeekFrom};
pub use terminal::{
TerminalInputOptions, TerminalLineOptions, TerminalOptions, TerminalOutputOptions,
};

View File

@ -8,4 +8,5 @@ pub(crate) mod macros;
pub mod error;
pub mod io;
pub mod path;
pub mod process;
pub mod syscall;

27
src/process.rs Normal file
View File

@ -0,0 +1,27 @@
//! Data structures for process management
use crate::io::RawFd;
/// Defines an optional argument for controlling process creation
#[derive(Debug)]
pub enum SpawnOption {
/// Indicates a new process should inherit a file descriptor from its creator
InheritFile {
/// FD on the creator side
source: RawFd,
/// What FD number should be used in the child
child: RawFd,
},
}
/// Controls how processes are created
#[derive(Clone, Debug)]
#[repr(C)]
pub struct SpawnOptions<'a> {
/// Creator-relative path to the binary to execute
pub program: &'a str,
/// Arguments to the child process
pub arguments: &'a [&'a str],
/// Optional arguments to specify details of the creation
pub optional: &'a [SpawnOption],
}

View File

@ -30,6 +30,15 @@ primitive_enum!(
Remove = 12,
#[doc = "Retrieves metadata for a filesystem entry"]
GetMetadata = 13,
#[doc = "Seek a file descriptor to the specified position"]
Seek = 14,
#[doc = "Create and start a new process"]
Spawn = 15,
#[doc = "Wait for a process to exit"]
WaitProcess = 16,
#[doc = "Send a signal to a process"]
SendSignal = 17,
#[doc = "Mount a filesystem"]
Mount = 101,