devela/sys/os/linux/fns/
mod.rs

1// devela::sys::os::linux::fns
2//
3//! Linux related functions.
4//
5
6use super::{consts::LINUX_FILENO, structs::LinuxTimespec};
7use core::{cmp::Ordering, time::Duration};
8
9mod rand;
10mod read;
11mod signal;
12mod syscalls;
13mod write;
14pub use rand::{
15    linux_random_bytes, linux_random_u128, linux_random_u16, linux_random_u32, linux_random_u64,
16    linux_random_u8,
17};
18pub use read::{
19    linux_get_byte, linux_get_char, linux_get_dirty_char, linux_get_line, linux_get_str,
20    linux_get_utf8_bytes, linux_pause_until_char, linux_prompt,
21};
22pub use signal::linux_sig_handler_no_return;
23pub use syscalls::*;
24pub use write::{linux_eprint, linux_eprintln, linux_print, linux_print_bytes, linux_println};
25
26/// Suspends execution of calling thread for `duration`.
27///
28/// This function makes use of the [`linux_sys_nanosleep`] syscall.
29pub fn linux_sleep(duration: Duration) {
30    let mut req = LinuxTimespec::with(duration);
31    let mut rem = LinuxTimespec::default();
32
33    loop {
34        let n = unsafe { linux_sys_nanosleep(req.as_ptr(), rem.as_mut_ptr()) };
35        match n.cmp(&0) {
36            Ordering::Less => {
37                linux_print("nanosleep failed");
38                unsafe { linux_sys_exit(13) };
39            }
40            Ordering::Equal => break,
41            Ordering::Greater => req = rem,
42        }
43    }
44}
45
46/// Returns the current process number.
47///
48/// This function makes use of the [`linux_sys_getpid`] syscall.
49pub fn linux_getpid() -> i32 {
50    unsafe { linux_sys_getpid() }
51}