safa_abi/raw/
processes.rs

1use core::ops::BitOr;
2
3use super::{Optional, RawSlice, RawSliceMut};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6#[repr(C)]
7pub struct TaskMetadata {
8    pub stdout: Optional<usize>,
9    pub stdin: Optional<usize>,
10    pub stderr: Optional<usize>,
11}
12
13impl TaskMetadata {
14    pub fn new(stdout: Option<usize>, stdin: Option<usize>, stderr: Option<usize>) -> Self {
15        Self {
16            stdout: stdout.into(),
17            stdin: stdin.into(),
18            stderr: stderr.into(),
19        }
20    }
21}
22
23#[derive(Debug, Clone, Copy)]
24#[repr(C)]
25/// Flags for the [crate::syscalls::SyscallTable::SysPSpawn] syscall
26pub struct SpawnFlags(u8);
27impl SpawnFlags {
28    pub const CLONE_RESOURCES: Self = Self(1 << 0);
29    pub const CLONE_CWD: Self = Self(1 << 1);
30    pub const EMPTY: Self = Self(0);
31}
32
33impl BitOr for SpawnFlags {
34    type Output = Self;
35    fn bitor(self, rhs: Self) -> Self::Output {
36        Self(self.0 | rhs.0)
37    }
38}
39
40/// configuration for the spawn syscall
41#[repr(C)]
42pub struct SpawnConfig {
43    /// config version for compatibility
44    /// added in kernel version 0.2.1 and therfore breaking compatibility with any program compiled for version below 0.2.1
45    pub version: u8,
46    pub name: RawSlice<u8>,
47    pub argv: RawSliceMut<RawSlice<u8>>,
48    pub flags: SpawnFlags,
49    pub metadata: *const TaskMetadata,
50    pub env: RawSliceMut<RawSlice<u8>>,
51}
52
53impl SpawnConfig {
54    pub fn new(
55        name: &str,
56        argv: *mut [&[u8]],
57        env: *mut [&[u8]],
58        flags: SpawnFlags,
59        metadata: Option<&TaskMetadata>,
60    ) -> Self {
61        let name = unsafe { RawSlice::from_slice(name.as_bytes()) };
62        let argv = unsafe { RawSliceMut::from_slices(argv) };
63        let env = unsafe { RawSliceMut::from_slices(env) };
64
65        Self {
66            version: 1,
67            name,
68            argv,
69            env,
70            flags,
71            metadata: metadata
72                .map(|x| x as *const TaskMetadata)
73                .unwrap_or(core::ptr::null()),
74        }
75    }
76}