Skip to main content

relibc/header/unistd/sysconf/
redox.rs

1use core::convert::TryInto;
2
3use crate::{
4    error::Errno,
5    fs::File,
6    header::{errno, fcntl, limits, sys_statvfs, unistd::sysconf::constants::*},
7    io::Read,
8    out::Out,
9    platform::{
10        self, Pal, Sys,
11        types::{c_int, c_long},
12    },
13};
14
15pub(super) fn sysconf_impl(name: c_int) -> c_long {
16    //TODO: Real values
17    match name {
18        _SC_ARG_MAX => 4096,
19        _SC_CHILD_MAX => 65536,
20        _SC_CLK_TCK => 100,
21        _SC_NGROUPS_MAX => limits::NGROUPS_MAX as c_long,
22        _SC_OPEN_MAX => 1024,
23        _SC_STREAM_MAX => 16,
24        _SC_TZNAME_MAX => -1,
25        _SC_VERSION => 200809,
26        _SC_PAGESIZE => Sys::getpagesize().try_into().unwrap_or(-1),
27        _SC_RE_DUP_MAX => 32767,
28        _SC_GETGR_R_SIZE_MAX => -1,
29        _SC_GETPW_R_SIZE_MAX => -1,
30        _SC_LOGIN_NAME_MAX => 256,
31        _SC_TTY_NAME_MAX => 32,
32        _SC_MONOTONIC_CLOCK => 200112,
33        _SC_SEMAPHORES => 200112,
34        _SC_BARRIERS => 202405,
35        _SC_SHELL => 1,
36        _SC_SHARED_MEMORY_OBJECTS => 200112,
37        _SC_THREADS => 202405,
38        _SC_THREAD_ATTR_STACKADDR => 202405,
39        _SC_THREAD_ATTR_STACKSIZE => 202405,
40        _SC_TIMEOUTS => 202405,
41        _SC_TIMERS => 202405,
42        _SC_SYMLOOP_MAX => -1,
43        _SC_HOST_NAME_MAX => limits::HOST_NAME_MAX.try_into().unwrap_or(-1),
44        _SC_NPROCESSORS_CONF => get_cpu_count().unwrap_or(None).unwrap_or(1),
45        _SC_NPROCESSORS_ONLN => get_cpu_count().unwrap_or(None).unwrap_or(1),
46        _SC_PHYS_PAGES => get_mem_stat().map(|s| s.f_blocks as c_long).unwrap_or(-1),
47        _SC_AVPHYS_PAGES => get_mem_stat().map(|s| s.f_bfree as c_long).unwrap_or(-1),
48        _SC_SIGQUEUE_MAX => 32,
49        _SC_REALTIME_SIGNALS => 202405,
50        _ => {
51            platform::ERRNO.set(errno::EINVAL);
52            -1
53        }
54    }
55}
56
57pub fn get_cpu_count() -> Result<Option<c_long>, Errno> {
58    // As long as CPUs: entry is in the first line, this is safe
59    let mut buffer = [0u8; 128];
60    let mut file = File::open(c"/scheme/sys/cpu".into(), fcntl::O_RDONLY)?;
61
62    let bytes_read = file
63        .read(&mut buffer)
64        .map_err(|_| Errno(errno::EIO).sync())?;
65
66    let contents =
67        str::from_utf8(&buffer[..bytes_read]).map_err(|_| Errno(errno::EINVAL).sync())?;
68    Ok(contents
69        .lines()
70        .find(|line| line.starts_with("CPUs:"))
71        .and_then(|line| line.split(':').nth(1))
72        .and_then(|num_str| num_str.trim().parse::<c_long>().ok()))
73}
74
75pub fn get_mem_stat() -> Result<sys_statvfs::statvfs, Errno> {
76    let fd = Sys::open(c"/scheme/memory".into(), fcntl::O_PATH, 0)?;
77    let mut buf = sys_statvfs::statvfs::default();
78    let res = Sys::fstatvfs(fd, Out::from_mut(&mut buf));
79    if let Ok(()) = Sys::close(fd) {}; // TODO handle error
80    let _ = res?;
81    return Ok(buf);
82}