mini/src/main.rs

119 lines
3.3 KiB
Rust

mod processes;
mod svc;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use std::process;
use std::error::Error;
use signal_hook::{SIGINT, SIGCHLD, SIGUSR1};
use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus};
use nix::sys::reboot::{reboot, RebootMode};
use nix::sys::signal::Signal;
use nix::sys::signal::kill;
use nix::unistd::Pid;
fn sigint_handler() {
println!("Received signal SIGINT");
process::exit(0);
}
fn reap_handler() {
loop {
match waitpid(Some(Pid::from_raw(-1)), Some(WaitPidFlag::WNOHANG)) {
Ok(WaitStatus::Exited(_pid, _code)) => return,
Err(e) => {
eprintln!("could not reap, error is {}", e);
return
},
_ => continue
}
}
}
fn set_hostname() {
let path = Path::new("/proc/sys/kernel/hostname");
let display = path.display();
let mut file = match File::create(&path) {
Err(why) => panic!("couldn't create {}: {}", display, why),
Ok(file) => file,
};
match file.write_all("octogone".as_bytes()) {
Err(why) => panic!("couldn't write to {}: {}", display, why),
Ok(_) => (),
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
println!("mini, the bare minimum init by darkgallium");
println!();
println!("remount /");
processes::run("mount", ["-o", "remount", "/"].as_ref()).await;
println!("set hostname");
set_hostname();
println!("mount all partitions");
processes::run("mount", ["-a"].as_ref()).await;
processes::run("swapon", ["-a"].as_ref()).await;
println!("load udev");
processes::run("/usr/lib/systemd/systemd-udevd", ["-d"].as_ref()).await;
processes::run("udevadm", ["trigger", "--action=add", "--type=subsystems"].as_ref()).await;
processes::run("udevadm", ["trigger", "--action=add", "--type=devices"].as_ref()).await;
processes::run("udevadm", ["settle"].as_ref()).await;
println!("setting keymap");
processes::run("loadkeys", ["fr-latin9"].as_ref()).await;
println!("load dbus");
processes::run("mkdir", ["/run/dbus"].as_ref()).await;
processes::run("dbus-daemon", ["--system"].as_ref()).await; // a lot of services depend on dbus being on
println!("now loading your services");
svc::launch_services().await;
unsafe { signal_hook::register(SIGINT, sigint_handler) }?;
let sigchld_handle = unsafe { signal_hook::register(SIGCHLD, reap_handler) }?;
println!("end bootstrap, launching your terminals");
// TODO: spawn a thread by tty and join threads at end
processes::sync_run_wait("agetty", ["--noclear", "tty1"].as_ref());
signal_hook::unregister(sigchld_handle);
println!("stopping services");
svc::stop_services();
println!("killing remaining processes");
let pids = processes::get_all_pids();
for pid in pids {
let res = kill(Pid::from_raw(pid), Signal::SIGKILL);
match res {
Ok(_) => {}
Err(err) => eprintln!("error killing process {}, error is {}", pid, err)
}
}
println!("unmounting all partitions");
processes::run("swapoff", ["-a"].as_ref()).await;
processes::run("umount", ["-a", "-f"].as_ref()).await;
println!("end pre-shutdown routines");
#[allow(unused_must_use)] {
reboot(RebootMode::RB_POWER_OFF);
}
Ok(())
}