safa_api/
process.rs

1use crate::syscalls::{self, define_syscall};
2use safa_abi::{errors::ErrorStatus, raw::processes::TaskMetadata};
3
4use crate::{syscalls::err_from_u16, syscalls::SyscallNum, Lazy};
5
6static META: Lazy<TaskMetadata> =
7    Lazy::new(|| meta_take().expect("failed to take ownership of the task metadata"));
8
9// PEAK design
10
11static STDIN: Lazy<usize> = Lazy::new(|| {
12    let stdin: Option<usize> = META.stdin.into();
13    if let Some(stdin) = stdin {
14        stdin
15    } else {
16        syscalls::open("dev:/tty").expect("failed to fall back to `dev:/tty` for stdin")
17    }
18});
19
20static STDOUT: Lazy<usize> = Lazy::new(|| {
21    let stdout: Option<usize> = META.stdout.into();
22    if let Some(stdout) = stdout {
23        stdout
24    } else {
25        syscalls::open("dev:/tty").expect("failed to fall back to `dev:/tty` for stdout")
26    }
27});
28
29use syscalls::types::SyscallResult;
30static STDERR: Lazy<usize> = Lazy::new(|| {
31    let stderr: Option<usize> = META.stderr.into();
32    if let Some(stderr) = stderr {
33        stderr
34    } else {
35        syscalls::open("dev:/tty").expect("failed to fall back to `dev:/tty` for stderr")
36    }
37});
38
39define_syscall!(SyscallNum::SysMetaTake => {
40    /// Takes ownership of the task metadata
41    /// the task metadata is used to store the stdin, stdout, and stderr file descriptors
42    /// this syscall can only be called once otherwise it will return [`ErrorStatus::Generic`] (1)
43    sysmeta_take(dest_task: *mut TaskMetadata)
44});
45
46#[inline]
47pub fn meta_take() -> Result<TaskMetadata, ErrorStatus> {
48    let mut dest_meta: TaskMetadata = unsafe { core::mem::zeroed() };
49    err_from_u16!(sysmeta_take(&raw mut dest_meta), dest_meta)
50}
51
52/// Returns the resource id of the stdout file descriptor
53#[cfg_attr(
54    not(any(feature = "std", feature = "rustc-dep-of-std")),
55    unsafe(no_mangle)
56)]
57#[inline(always)]
58pub extern "C" fn sysmeta_stdout() -> usize {
59    **STDOUT
60}
61
62/// Returns the resource id of the stderr file descriptor
63#[cfg_attr(
64    not(any(feature = "std", feature = "rustc-dep-of-std")),
65    unsafe(no_mangle)
66)]
67#[inline(always)]
68pub extern "C" fn sysmeta_stderr() -> usize {
69    **STDERR
70}
71
72/// Returns the resource id of the stdin file descriptor
73#[cfg_attr(
74    not(any(feature = "std", feature = "rustc-dep-of-std")),
75    unsafe(no_mangle)
76)]
77#[inline(always)]
78pub extern "C" fn sysmeta_stdin() -> usize {
79    **STDIN
80}