nit/src/main.rs

109 lines
3.4 KiB
Rust

mod config;
mod platform;
mod result;
mod system;
use platform::Platform;
use result::Result;
fn main() {
if let Err(e) = init() {
eprintln!("Error: {e}");
let mut opt = Some(&e as &dyn std::error::Error);
while let Some(current_source) = opt {
eprintln!("Caused by: {current_source}");
opt = current_source.source();
}
}
}
extern "C" fn handle_sigchld(_sig: i32) {
// wait on all dead processes
while system::syscall::waitpid(-1, libc::WNOHANG).is_ok() {
// pass
}
}
fn init_rootfs() {
use libc::{MS_NOSUID, MS_NOEXEC, MS_NODEV };
let no_dse = MS_NODEV | MS_NOSUID | MS_NOEXEC;
let no_se = MS_NOSUID | MS_NOEXEC;
let args = [
("devtmpfs", "/dev", "devtmpfs", no_se, "mode=0755"),
("devtmpfs", "/dev", "devtmpfs", no_se, "mode=0755"),
("devpts", "/dev/pts", "devpts", no_se, ""),
("shm", "/dev/shm", "tmpfs", no_dse, "mode=0755"),
("proc", "/proc", "proc", no_dse, "hidepid=2"),
("tmpfs", "/run", "tmpfs", no_dse, "mode=0755"),
("tmpfs", "/tmp", "tmpfs", no_dse, ""),
("sysfs", "/sys", "sysfs", no_dse, ""),
("cgroup_root", "/sys/fs/cgroup", "tmpfs", no_dse, "mode=0755"),
];
for (src, target, fstype, flags, data) in args {
match system::syscall::mount(src, target, fstype, flags, Some(data)) {
Ok(())=> eprintln!("Mounted {target}"),
Err(e)=> {
eprintln!("Error: {e}");
let mut opt = Some(&e as &dyn std::error::Error);
while let Some(current_source) = opt {
eprintln!("Caused by: {current_source}");
opt = current_source.source();
}
},
}
}
}
fn init() -> Result<()> {
// let config = config::get_config()?;
init_rootfs();
init_console()?;
platform::aws::Aws.init().unwrap();
std::thread::sleep(std::time::Duration::from_secs(500));
/*
if let Some(platform) = config.platform.as_deref() {
platform::init(platform)?;
} else if let Some(platform) = platform::get_current_platform(None)?.as_deref() {
platform::init(platform)?;
}
*/
/*
let command = &config.target;
match config.mode {
config::Mode::Spawn => {
// set up a process reaper. any time a child process dies, a SIGCHLD will be fired, and
// the signal handler will reap the processes
eprintln!("installing signal handler");
system::syscall::signal(libc::SIGCHLD, handle_sigchld)?;
loop {
eprintln!("spawning {command}");
if let Err(e) = std::process::Command::new(command).spawn() {
eprintln!("Encountered error running {command}: {e}");
}
}
}
config::Mode::Exec => {
eprintln!("pivoting to {command}");
system::syscall::execv(command, &[])?;
}
}
*/
Ok(())
}
fn init_console() -> Result<(), result::CtxError> {
Ok(for (filename, mode, fd) in [
("/dev/console", "r", 0),
("/dev/console", "w", 1),
("/dev/console", "w", 2),
] {
system::syscall::freopen(filename, mode, &fd)?;
})
}