Skip to content

Commit

Permalink
refactor: merging executables to save disk space (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
llenotre committed Jul 2, 2024
1 parent 64dc394 commit 50a303f
Show file tree
Hide file tree
Showing 23 changed files with 149 additions and 254 deletions.
117 changes: 13 additions & 104 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 19 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
[workspace]
[package]
name = "mutils"
version = "0.1.0"
edition = "2021"

resolver = "2"
[lib]
name = "utils"
path = "src/utils/lib.rs"

members = [
"dmesg",
"fdisk",
"insmod",
"login",
"lsmod",
"mkfs",
"mount",
"nologin",
"powerctl",
"ps",
"rmmod",
"su",
"umount",
"usrgrp",
"utils",
]
[[bin]]
name = "mutils"
path = "src/main.rs"

[[bin]]
name = "mutils-suid"
path = "src/main-suid.rs"

[dependencies]
argon2 = { version = "0.5.3", features = ["password-hash"] }
libc = "0.2.155"
rand_core = { version = "0.6.4", features = ["getrandom"] }

[profile.release]
panic = "abort"
Expand Down
6 changes: 0 additions & 6 deletions dmesg/Cargo.toml

This file was deleted.

6 changes: 0 additions & 6 deletions lsmod/Cargo.toml

This file was deleted.

7 changes: 0 additions & 7 deletions mount/Cargo.toml

This file was deleted.

6 changes: 0 additions & 6 deletions nologin/Cargo.toml

This file was deleted.

9 changes: 0 additions & 9 deletions ps/Cargo.toml

This file was deleted.

7 changes: 1 addition & 6 deletions dmesg/src/main.rs → src/dmesg.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
//! The `dmesg` command allows to print the kernel's logs.
use std::env;

/// The path to the kmsg device file.
const KMSG_PATH: &str = "/dev/kmsg";

fn main() {
let _args: Vec<String> = env::args().collect();
// TODO parse arguments

pub fn main() {
// TODO read non blocking from file
// TODO for each line:
// - split once with `;`
Expand Down
28 changes: 10 additions & 18 deletions lsmod/src/main.rs → src/lsmod.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
//! The `lsmod` command allows to list loaded kernel modules.
use crate::error;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::process::exit;

/// The path to the modules file.
const MODULES_PATH: &str = "/proc/modules";

fn main() {
let file = match File::open(MODULES_PATH) {
Ok(f) => f,
Err(e) => {
eprintln!("lsmod: cannot open `{MODULES_PATH}`: {e}");
exit(1);
}
};

pub fn main() {
let file = File::open(MODULES_PATH).unwrap_or_else(|e| {
error("lsmod", format_args!("cannot open `{MODULES_PATH}`: {e}"));
});
let reader = BufReader::new(file);

println!("Name\tSize\tUsed by");

for line in reader.lines() {
// TODO handle error
let mut split = line.as_ref().unwrap().split(' ');

let line = line.unwrap_or_else(|e| {
error("lsmod", e);
});
let mut split = line.split(' ');
let name = split.next().unwrap();
let size = split.next().unwrap();
let use_count = split.next().unwrap();
let used_by_list = split.next().unwrap();

// TODO padding
println!("{name}\t{size}\t{use_count}\t{used_by_list}");
println!("{name} {size} {use_count} {used_by_list}");
}
}
5 changes: 5 additions & 0 deletions src/main-suid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! Main of all commands that **require** the SUID flag.
fn main() {
// TODO
}
57 changes: 57 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! Main of all commands that **do not require** the SUID flag.
#![feature(option_get_or_insert_default)]

mod dmesg;
mod lsmod;
mod mount;
mod nologin;
mod ps;

use std::process::exit;
use std::{env, fmt};

/// Writes an error to stderr, then exits.
fn error<M: fmt::Display>(bin: &str, msg: M) -> ! {
eprintln!("{bin}: error: {msg}");
exit(1);
}

fn main() {
let mut args = env::args_os();
let bin = args
.next()
.and_then(|s| s.into_string().ok())
.unwrap_or_else(|| {
error("mutils", "missing binary name");
});
match bin.as_str() {
"dmesg" => dmesg::main(),
"fdisk" => todo!(),
"insmod" => todo!(),
"lsmod" => lsmod::main(),
"rmmod" => todo!(),
bin @ ("mkfs" | "mkfs.ext2") => {
// TODO change default fs to `ext4` when implemented
let fs_name = bin.find(".").map(|i| &bin[(i + 1)..]).unwrap_or("ext2");
todo!()
}
"mount" => mount::main(args),
"umount" => todo!(),
"nologin" => nologin::main(),
"powerctl" => todo!(),
"halt" => todo!(),
"poweroff" => todo!(),
"reboot" => todo!(),
"shutdown" => todo!(),
"suspend" => todo!(),
"ps" => ps::main(),
"useradd" => todo!(),
"usermod" => todo!(),
"userdel" => todo!(),
"groupadd" => todo!(),
"groupmod" => todo!(),
"groupdel" => todo!(),
_ => error("mutils", "invalid binary name"),
}
}
Loading

0 comments on commit 50a303f

Please sign in to comment.