aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 43f82c5368b51ebc74f0ac7a53d0e3df47b798b6 (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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, UnmountDrop, 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 mount_or_halt(part_id: u8, mount_point: &str, fs: &str) -> UnmountDrop<Mount> {
    let mut stdout = StandardStream::stdout(ColorChoice::Always);

    let mut mount = None;
    let mut mount_err = None;

    let devs = [
        format!("/dev/mmcblk0p{}", part_id),
        format!("/dev/sda{}", part_id),
        format!("/dev/vda{}", part_id),
    ];

    for dev in &devs {
        match Mount::builder().fstype(fs).mount(dev, mount_point) {
            Ok(v) => {
                mount = Some(v.into_unmount_drop(UnmountFlags::DETACH));
                break;
            }
            Err(e) => {
                mount_err = Some(e);
            }
        };
    }

    match mount {
        None => {
            if let Some(e) = mount_err {
                match stdout.set_color(ColorSpec::new().set_fg(Some(Color::Red))) {
                    Ok(_) => {
                        match writeln!(&mut stdout, "[ ERROR ] Can't mount {}: {}", mount_point, e)
                        {
                            Ok(_) => {}
                            Err(_) => println!("[ ERROR ] Can't mount {}: {}", mount_point, e),
                        }
                    }
                    Err(_) => println!("[ ERROR ] Can't mount {}: {}", mount_point, e),
                }
            } else {
                match stdout.set_color(ColorSpec::new().set_fg(Some(Color::Red))) {
                    Ok(_) => match writeln!(
                        &mut stdout,
                        "[ ERROR ] Can't mount {}: Unknown error (this shouldn't happen)",
                        mount_point
                    ) {
                        Ok(_) => {}
                        Err(_) => println!(
                            "[ ERROR ] Can't mount {}: Unknown error (this shouldn't happen)",
                            mount_point
                        ),
                    },
                    Err(_) => {
                        println!(
                            "[ ERROR ] Can't mount {}: Unknown error (this shouldn't happen)",
                            mount_point
                        )
                    }
                }
            }

            loop {
                thread::sleep(Duration::MAX);
            }
        }
        Some(handle) => handle,
    }
}

fn main() -> ExitCode {
    let mut stdout = StandardStream::stdout(ColorChoice::Always);

    let _boot_handle = mount_or_halt(1, "/boot", "vfat");
    let _data_handle = mount_or_halt(4, "/data", "ext4");
    let _tmp_handle = Mount::builder()
        .fstype("tmpfs")
        .mount("tmpfs", "/tmp")
        .expect("can't mount /tmp tmpfs");

    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");
            }
        }

        loop {
            thread::sleep(Duration::MAX);
        }
    }

    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);
    }
}