amjad_os_user_std/
process.rs

1use core::ffi::{c_char, CStr};
2
3pub use kernel_user_link::process::SpawnFileMapping;
4use kernel_user_link::{
5    call_syscall,
6    syscalls::{SyscallError, SYS_EXIT, SYS_SPAWN, SYS_WAIT_PID},
7};
8
9/// # Safety
10/// No guarantees are made about the state of the system after this function returns.
11pub unsafe fn exit(code: i32) -> ! {
12    unsafe {
13        call_syscall!(
14            SYS_EXIT,
15            code as u64, // code
16        )
17        .unwrap();
18    }
19    unreachable!("exit syscall should not return")
20}
21
22/// # Safety
23/// path must be a valid C string.
24/// argv must be a valid C string array. ending with a null pointer.
25/// File mappings must be valid and present file mappings.
26/// The fds used in the file mappings must never be used again by the caller, as the ownership is
27/// transferred to the child process.
28pub unsafe fn spawn(
29    path: &CStr,
30    argv: &[*const c_char],
31    file_mappings: &[SpawnFileMapping],
32) -> Result<u64, SyscallError> {
33    unsafe {
34        call_syscall!(
35            SYS_SPAWN,
36            path.as_ptr() as u64,          // path
37            argv.as_ptr() as u64,          // argv
38            file_mappings.as_ptr() as u64, // file_mappings
39            file_mappings.len() as u64     // file_mappings_len
40        )
41    }
42}
43
44/// # Safety
45/// This is generally safe, it will return error if the pid is not valid, but it might wait for a long
46/// time depending on the process we are waiting for.
47pub unsafe fn wait_for_pid(pid: u64, block: bool) -> Result<i32, SyscallError> {
48    unsafe {
49        call_syscall!(
50            SYS_WAIT_PID,
51            pid,          // pid
52            block as u64  // block
53        )
54        .map(|x| x as i32)
55    }
56}