1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
use anyhow::bail;
use std::fs;
use std::io::Write;
use std::process::{self, Command, ExitCode};
use std::thread;
use std::time::Duration;
use sys_mount::{Mount, Unmount, UnmountFlags};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
fn start() -> anyhow::Result<()> {
let mut stdout = StandardStream::stdout(ColorChoice::Always);
stdout.set_color(ColorSpec::new().set_fg(Some(Color::Yellow)))?;
writeln!(&mut stdout, "Starting rustkrazy")?;
for service in fs::read_dir("/bin")? {
let service = service?;
let service_name = match service.file_name().into_string() {
Ok(v) => v,
Err(_) => bail!("[ ERROR ] invalid unicode in file name"),
};
if service_name == "init" {
continue;
}
match Command::new(service.path()).spawn() {
Ok(_) => {
stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
write!(&mut stdout, "[ OK ] Starting {}", service_name)?;
stdout.reset()?;
writeln!(&mut stdout)?;
}
Err(e) => {
stdout.set_color(ColorSpec::new().set_fg(Some(Color::Red)))?;
write!(&mut stdout, "[ ERROR ] Starting {}: {}", service_name, e)?;
stdout.reset()?;
writeln!(&mut stdout)?;
}
}
}
Ok(())
}
fn main() -> ExitCode {
let mut stdout = StandardStream::stdout(ColorChoice::Always);
let _mount = Mount::builder()
.fstype("vfat")
.mount("/dev/disk/by-partuuid/00000000-01", "/boot")
.expect("can't mount boot partition")
.into_unmount_drop(UnmountFlags::DETACH);
if process::id() != 1 {
match stdout.set_color(ColorSpec::new().set_fg(Some(Color::Red))) {
Ok(_) => match writeln!(&mut stdout, "Must be run as PID 1") {
Ok(_) => {}
Err(_) => println!("Must be run as PID 1"),
},
Err(_) => {
println!("Must be run as PID 1");
}
}
return ExitCode::FAILURE;
}
match start() {
Ok(_) => {}
Err(e) => match stdout.set_color(ColorSpec::new().set_fg(Some(Color::Red))) {
Ok(_) => match writeln!(&mut stdout, "[ ERROR ] {}", e) {
Ok(_) => {}
Err(_) => println!("[ ERROR ] {}", e),
},
Err(_) => {
println!("[ ERROR ] {}", e);
}
},
}
loop {
thread::sleep(Duration::MAX);
}
}
|