logging added, permission checks added, basic error handling

main
Shinyzenith 3 years ago
parent 328c310f16
commit c44b634cc3
No known key found for this signature in database
GPG Key ID: 6DD485917B553B7B

144
Cargo.lock generated

@ -2,6 +2,39 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "Simple-Wayland-HotKey-Daemon"
version = "0.1.0"
dependencies = [
"clap",
"env_logger",
"evdev",
"glob",
"log",
"nix",
"tokio",
]
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.0.1"
@ -44,6 +77,34 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "3.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d01c9347757e131122b19cd19a05c85805b68c2352a97b623efdc3c295290299"
dependencies = [
"atty",
"bitflags",
"indexmap",
"os_str_bytes",
"strsim",
"termcolor",
"textwrap",
]
[[package]]
name = "env_logger"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "evdev"
version = "0.11.3"
@ -67,6 +128,12 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "hermit-abi"
version = "0.1.19"
@ -76,6 +143,22 @@ dependencies = [
"libc",
]
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "indexmap"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "instant"
version = "0.1.12"
@ -184,6 +267,15 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5"
[[package]]
name = "os_str_bytes"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
dependencies = [
"memchr",
]
[[package]]
name = "parking_lot"
version = "0.11.2"
@ -248,6 +340,23 @@ dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "scopeguard"
version = "1.1.0"
@ -270,13 +379,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309"
[[package]]
name = "swhkd"
version = "0.1.0"
dependencies = [
"evdev",
"glob",
"tokio",
]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
@ -295,6 +401,21 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "termcolor"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
[[package]]
name = "tokio"
version = "1.15.0"
@ -347,6 +468,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

@ -1,11 +1,17 @@
[package]
name = "swhkd"
name = "Simple-Wayland-HotKey-Daemon"
version = "0.1.0"
edition = "2021"
[dependencies]
evdev = "0.11.3"
glob = "0.3.0"
log = "0.4.0"
env_logger = "0.8.4"
nix = "0.23.1"
clap = "3.0.4"
tokio = { version = "1.15.0", features = ["full"] }
[[bin]]

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

Loading…
Cancel
Save