From e7b7133fdc63facb2faf27aadc78db237e9bfeb3 Mon Sep 17 00:00:00 2001 From: david-soto-m Date: Mon, 11 Apr 2022 10:11:00 +0200 Subject: [PATCH 1/3] Change bool to result, clippy, fmt --- src/main.rs | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index edb3c4c..8f2ee2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -267,15 +267,25 @@ impl EventObserver for KeyLogger { } } -fn check_uinput_loaded() -> bool { - let mut kernel_version = std::fs::read_to_string("/proc/version").unwrap(); - kernel_version = kernel_version.to_string().split(' ').nth(2).unwrap().to_string(); - let built_in_modules = std::fs::read_to_string(String::from("/lib/modules/") + &kernel_version + &String::from("/modules.builtin")).unwrap(); +fn check_uinput_loaded() -> std::result::Result<(),&'static str> { + let mut kernel_version = std::fs::read_to_string("/proc/version").unwrap(); + kernel_version = kernel_version.split(' ') + .nth(2) + .unwrap() + .to_string(); + + let built_in_modules = std::fs::read_to_string( + String::from("/lib/modules/") + &kernel_version + &String::from("/modules.builtin"), + ) + .unwrap(); for line in built_in_modules.lines() { if line.eq("kernel/drivers/input/misc/uinput.ko") { - info!("'uinput' is built into running kernel version: {}", kernel_version); - return true; + info!( + "'uinput' is built into running kernel version: {}", + kernel_version + ); + return Ok(()); } } @@ -283,20 +293,19 @@ fn check_uinput_loaded() -> bool { for line in modules.lines() { if line.starts_with("uinput ") { info!("'uinput' module is loaded"); - return true; + return Ok(()); } } - return false; + Err("'uinput' module must be loaded OR built into the kernel") } fn start_mapper_from_file_conf(config_file: String) -> Result<()> { - if !check_uinput_loaded() { - panic!("'uinput' module must be loaded OR built into the kernel"); - } + check_uinput_loaded().unwrap(); let config = serde_yaml::from_str( &*std::fs::read_to_string(config_file.as_str()) - .expect(&format!("Could not open file {}", config_file))) - .expect("Could not parse the configuration yaml file"); + .expect(&format!("Could not open file {}", config_file)), + ) + .expect("Could not parse the configuration yaml file"); start_mapper(config) } From 3871f7acfc4621dbf4c78d15e5cad47dcc98cb88 Mon Sep 17 00:00:00 2001 From: david-soto-m Date: Mon, 11 Apr 2022 10:35:54 +0200 Subject: [PATCH 2/3] kbcterror --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8f2ee2b..d403ee3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -268,7 +268,7 @@ impl EventObserver for KeyLogger { } -fn check_uinput_loaded() -> std::result::Result<(),&'static str> { +fn check_uinput_loaded() -> Result<()> { let mut kernel_version = std::fs::read_to_string("/proc/version").unwrap(); kernel_version = kernel_version.split(' ') .nth(2) @@ -296,7 +296,7 @@ fn check_uinput_loaded() -> std::result::Result<(),&'static str> { return Ok(()); } } - Err("'uinput' module must be loaded OR built into the kernel") + Err(KbctError::Error("'uinput' module must be loaded OR built into the kernel".into())) } fn start_mapper_from_file_conf(config_file: String) -> Result<()> { From b31985961cfa19f18526a63c8ca03961a7941381 Mon Sep 17 00:00:00 2001 From: david-soto-m Date: Sat, 16 Apr 2022 10:33:27 +0200 Subject: [PATCH 3/3] don't panic --- src/main.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index d403ee3..a938891 100644 --- a/src/main.rs +++ b/src/main.rs @@ -267,18 +267,12 @@ impl EventObserver for KeyLogger { } } - fn check_uinput_loaded() -> Result<()> { - let mut kernel_version = std::fs::read_to_string("/proc/version").unwrap(); - kernel_version = kernel_version.split(' ') - .nth(2) - .unwrap() - .to_string(); - - let built_in_modules = std::fs::read_to_string( - String::from("/lib/modules/") + &kernel_version + &String::from("/modules.builtin"), - ) - .unwrap(); + let mut kernel_version = std::fs::read_to_string("/proc/version")?; + kernel_version = kernel_version.split(' ').nth(2).unwrap().to_string(); + let built_in_modules_file = + String::from("/lib/modules/") + &kernel_version + &String::from("/modules.builtin"); + let built_in_modules = std::fs::read_to_string(&built_in_modules_file)?; for line in built_in_modules.lines() { if line.eq("kernel/drivers/input/misc/uinput.ko") { info!( @@ -289,14 +283,16 @@ fn check_uinput_loaded() -> Result<()> { } } - let modules = std::fs::read_to_string("/proc/modules").unwrap(); + let modules = std::fs::read_to_string("/proc/modules")?; for line in modules.lines() { if line.starts_with("uinput ") { info!("'uinput' module is loaded"); return Ok(()); } } - Err(KbctError::Error("'uinput' module must be loaded OR built into the kernel".into())) + Err(KbctError::Error( + "'uinput' module must be loaded OR built into the kernel".into(), + )) } fn start_mapper_from_file_conf(config_file: String) -> Result<()> {