Skip to content

Commit

Permalink
refactor: move insmod and rmmod to the common executable
Browse files Browse the repository at this point in the history
  • Loading branch information
llenotre committed Jul 3, 2024
1 parent 50a303f commit cc0f660
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 52 deletions.
8 changes: 0 additions & 8 deletions insmod/Cargo.toml

This file was deleted.

8 changes: 0 additions & 8 deletions rmmod/Cargo.toml

This file was deleted.

36 changes: 18 additions & 18 deletions insmod/src/main.rs → src/insmod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! The `insmod` command loads a module from a file.
use std::env;
use crate::error;
use std::env::ArgsOs;
use std::ffi::c_long;
use std::fs::File;
use std::io::Error;
use std::os::fd::AsRawFd;
use std::path::PathBuf;
use std::process::exit;
use std::ptr::null;

Expand All @@ -20,28 +20,28 @@ fn print_usage() {
println!("Loads a kernel module from the given file");
}

fn main() {
let args: Vec<String> = env::args().collect();

if args.len() < 2 {
pub fn main(args: ArgsOs) {
let args: Vec<_> = args.collect();
let [path] = args.as_slice() else {
print_usage();
exit(1);
}

let filepath = PathBuf::from(&args[1]);
let file = File::open(&filepath).unwrap_or_else(|e| {
eprintln!("insmod: cannot open file `{}`: {}", filepath.display(), e);
exit(1);
};
let file = File::open(path).unwrap_or_else(|e| {
error(
"insmod",
format_args!("cannot open file `{}`: {e}", path.display()),
);
});

// TODO handle parameters
let ret = unsafe { libc::syscall(FINIT_MODULE_ID, file.as_raw_fd(), null::<u8>(), 0) };
if ret < 0 {
eprintln!(
"insmod: cannot load module `{}`: {}",
filepath.display(),
Error::last_os_error()
error(
"insmod",
format_args!(
"insmod: cannot load module `{}`: {}",
path.display(),
Error::last_os_error()
),
);
exit(1);
}
}
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
//! Main of all commands that **do not require** the SUID flag.
#![feature(option_get_or_insert_default)]
#![feature(os_str_display)]

mod dmesg;
mod insmod;
mod lsmod;
mod mount;
mod nologin;
mod ps;
mod rmmod;

use std::process::exit;
use std::{env, fmt};
Expand All @@ -28,9 +31,9 @@ fn main() {
match bin.as_str() {
"dmesg" => dmesg::main(),
"fdisk" => todo!(),
"insmod" => todo!(),
"insmod" => insmod::main(args),
"lsmod" => lsmod::main(),
"rmmod" => todo!(),
"rmmod" => rmmod::main(args),
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");
Expand Down
28 changes: 12 additions & 16 deletions rmmod/src/main.rs → src/rmmod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! The `rmmod` command unloads a module.
use std::env;
use crate::error;
use std::env::ArgsOs;
use std::ffi::c_long;
use std::ffi::CString;
use std::io::Error;
use std::os::unix::ffi::OsStrExt;
use std::process::exit;

/// The ID of the `delete_module` system call.
Expand All @@ -17,24 +19,18 @@ fn print_usage() {
println!("Unloads a kernel module");
}

fn main() {
let args: Vec<String> = env::args().collect();

if args.len() != 2 {
pub fn main(args: ArgsOs) {
let args: Vec<_> = args.collect();
let [name] = args.as_slice() else {
print_usage();
exit(1);
}

let name = &args[1];
let c_name = CString::new(name.as_bytes()).unwrap(); // TODO handle error

let ret = unsafe { libc::syscall(DELETE_MODULE_ID, c_name.as_ptr(), 0) };
};
let name = CString::new(name.as_bytes()).unwrap(); // TODO handle error
let ret = unsafe { libc::syscall(DELETE_MODULE_ID, name.as_ptr(), 0) };
if ret < 0 {
eprintln!(
"rmmod: cannot unload module `{}`: {}",
name,
Error::last_os_error()
error(
"rmmod: cannot unload module `{name}`: {}",
Error::last_os_error(),
);
exit(1);
}
}

0 comments on commit cc0f660

Please sign in to comment.