-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logging added, permission checks added, basic error handling
- Loading branch information
1 parent
328c310
commit c44b634
Showing
3 changed files
with
190 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,69 @@ | ||
use evdev::Device; | ||
use evdev::Key; | ||
use glob::glob; | ||
use nix::unistd; | ||
use std::process::exit; | ||
|
||
pub fn main() { | ||
std::env::set_var("RUST_LOG", "trace"); | ||
env_logger::init(); | ||
log::trace!("Logger initialized."); | ||
|
||
if unistd::Uid::current().is_root() { | ||
log::error!("Refusing to run swhkd as root."); | ||
exit(1); | ||
} | ||
|
||
log::trace!("Checking if invoking user is in input group or not."); | ||
let groups = unistd::getgroups(); | ||
let mut is_in_input_group: bool = false; | ||
'init_loop: for (_, groups) in groups.iter().enumerate() { | ||
for group in groups { | ||
let group = unistd::Group::from_gid(*group); | ||
if group.unwrap().unwrap().name == "input" { | ||
log::debug!("Invoking user is in input group."); | ||
is_in_input_group = true; | ||
break 'init_loop; | ||
} | ||
} | ||
} | ||
|
||
if is_in_input_group == false { | ||
log::error!("Invoking user is NOT in input group."); | ||
exit(1); | ||
} | ||
|
||
log::trace!("Attempting to find all keyboard file descriptors."); | ||
for entry in glob("/dev/input/event*").expect("Failed to read /dev/input/event*.") { | ||
match entry { | ||
Ok(path) => { | ||
check_keyboard(path.into_os_string().into_string().unwrap()); | ||
} | ||
Err(_) => continue, | ||
Err(error) => log::error!("Unexpected error occured: {}", error), | ||
} | ||
} | ||
} | ||
|
||
fn check_keyboard(input_path: String) -> bool { | ||
let device = Device::open(&input_path).expect("Failed to open device."); | ||
pub fn check_keyboard(input_path: String) -> bool { | ||
/* Try to open the device fd. */ | ||
let device = Device::open(&input_path); | ||
match &device { | ||
Ok(_) => (), | ||
Err(error) => { | ||
log::error!("Failed to open device {}: {}", input_path, error); | ||
} | ||
} | ||
|
||
/* Check for the presence of enter key. */ | ||
if device | ||
.unwrap() | ||
.supported_keys() | ||
.map_or(false, |keys| keys.contains(Key::KEY_ENTER)) | ||
{ | ||
println!("{} is a keyboard.", input_path); | ||
log::debug!("{} is a keyboard.", input_path); | ||
return true; | ||
} else { | ||
println!("{} is not a keyboard.", input_path); | ||
log::debug!("{} is not a keyboard.", input_path); | ||
return false; | ||
} | ||
} |