2020-06-13 23:02:16 +00:00
|
|
|
mod processes;
|
|
|
|
mod svc;
|
|
|
|
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::process;
|
|
|
|
use std::error::Error;
|
2020-06-20 13:27:21 +00:00
|
|
|
use signal_hook::{SIGINT, SIGCHLD, SIGUSR1};
|
2020-06-13 23:02:16 +00:00
|
|
|
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;
|
2020-06-20 13:36:50 +00:00
|
|
|
use tokio;
|
2020-06-13 23:02:16 +00:00
|
|
|
|
2020-06-20 17:43:19 +00:00
|
|
|
fn shutdown_handler() {
|
|
|
|
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::sync_run_wait("swapoff", ["-a"].as_ref());
|
|
|
|
processes::sync_run_wait("umount", ["-a", "-f"].as_ref());
|
|
|
|
|
|
|
|
println!("syncing disks");
|
|
|
|
processes::sync_run_wait("sync", [].as_ref());
|
|
|
|
|
|
|
|
println!("end pre-shutdown routines");
|
|
|
|
|
|
|
|
#[allow(unused_must_use)] {
|
|
|
|
reboot(RebootMode::RB_POWER_OFF);
|
|
|
|
}
|
|
|
|
|
2020-06-13 23:02:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 17:43:19 +00:00
|
|
|
|
|
|
|
|
2020-06-13 23:02:16 +00:00
|
|
|
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>> {
|
|
|
|
|
2020-06-20 17:43:19 +00:00
|
|
|
if process::id() == 1 {
|
|
|
|
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;
|
|
|
|
|
|
|
|
// misc stuff in another thread
|
|
|
|
tokio::spawn(async {
|
|
|
|
println!("setting keymap");
|
|
|
|
processes::run("loadkeys", ["fr-latin9"].as_ref()).await;
|
|
|
|
|
|
|
|
println!("mounting pty");
|
|
|
|
processes::run("mkdir", ["/dev/pts"].as_ref()).await;
|
|
|
|
processes::run("mount", ["devpts", "/dev/pts", "-t", "devpts"].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
|
|
|
|
|
|
|
|
let sigchld_handle = unsafe { signal_hook::register(SIGCHLD, reap_handler) }?;
|
|
|
|
let sigchld_handle_c = sigchld_handle.clone();
|
|
|
|
|
|
|
|
unsafe { signal_hook::register(SIGINT, move || {
|
|
|
|
signal_hook::unregister(sigchld_handle);
|
|
|
|
shutdown_handler();
|
|
|
|
}) }?;
|
|
|
|
|
|
|
|
|
|
|
|
println!("loading services");
|
|
|
|
svc::launch_services().await;
|
|
|
|
|
|
|
|
println!("end bootstrap, launching your terminals");
|
|
|
|
|
|
|
|
tokio::spawn(async {
|
|
|
|
processes::run("agetty", ["--noclear", "tty1"].as_ref()).await;
|
|
|
|
}).await;
|
|
|
|
|
|
|
|
signal_hook::unregister(sigchld_handle_c);
|
|
|
|
shutdown_handler();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2020-06-13 23:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|