Simpler IPC implementation using std::os::unix::net instead of using interprocess crate.
main
Aakash Sen Sharma 2 years ago committed by GitHub
commit cdac47494d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

307
Cargo.lock generated

@ -9,7 +9,6 @@ dependencies = [
"clap",
"env_logger",
"evdev",
"interprocess",
"log",
"nix",
"sysinfo",
@ -24,29 +23,6 @@ dependencies = [
"memchr",
]
[[package]]
name = "async-channel"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2114d64672151c0c5eaa5e131ec84a74f06e1e559830dabba01ca30605d66319"
dependencies = [
"concurrent-queue",
"event-listener",
"futures-core",
]
[[package]]
name = "async-task"
version = "4.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0"
[[package]]
name = "atomic-waker"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a"
[[package]]
name = "atty"
version = "0.2.14"
@ -82,26 +58,6 @@ dependencies = [
"wyz",
]
[[package]]
name = "blocking"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "046e47d4b2d391b1f6f8b407b1deb8dee56c1852ccd868becf2710f601b5f427"
dependencies = [
"async-channel",
"async-task",
"atomic-waker",
"fastrand",
"futures-lite",
"once_cell",
]
[[package]]
name = "cache-padded"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1db59621ec70f09c5e9b597b220c7a2b43611f4710dc03ceb8748637775692c"
[[package]]
name = "cc"
version = "1.0.72"
@ -129,15 +85,6 @@ dependencies = [
"textwrap",
]
[[package]]
name = "concurrent-queue"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3"
dependencies = [
"cache-padded",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.3"
@ -218,131 +165,12 @@ dependencies = [
"nix",
]
[[package]]
name = "event-listener"
version = "2.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77f3309417938f28bf8228fcff79a4a37103981e3e186d2ccd19c74b38f4eb71"
[[package]]
name = "fastrand"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3fcf0cee53519c866c09b5de1f6c56ff9d647101f81c1964fa632e148896cdf"
dependencies = [
"instant",
]
[[package]]
name = "funty"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]]
name = "futures"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28560757fe2bb34e79f907794bb6b22ae8b0e5c669b638a1132f2592b19035b4"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba3dda0b6588335f360afc675d0564c17a77a2bda81ca178a4b6081bd86c7f0b"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
name = "futures-core"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0c8ff0461b82559810cdccfde3215c3f373807f5e5232b71479bff7bb2583d7"
[[package]]
name = "futures-executor"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29d6d2ff5bb10fb95c85b8ce46538a2e5f5e7fdc755623a7d4529ab8a4ed9d2a"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f9d34af5a1aac6fb380f735fe510746c38067c5bf16c7fd250280503c971b2"
[[package]]
name = "futures-lite"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7694489acd39452c77daa48516b894c153f192c3578d5a839b62c58099fcbf48"
dependencies = [
"fastrand",
"futures-core",
"futures-io",
"memchr",
"parking",
"pin-project-lite",
"waker-fn",
]
[[package]]
name = "futures-macro"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dbd947adfffb0efc70599b3ddcf7b5597bb5fa9e245eb99f62b3a5f7bb8bd3c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3055baccb68d74ff6480350f8d6eb8fcfa3aa11bdc1a1ae3afdd0514617d508"
[[package]]
name = "futures-task"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ee7c6485c30167ce4dfb83ac568a849fe53274c831081476ee13e0dce1aad72"
[[package]]
name = "futures-util"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b5cf40b47a271f77a8b1bec03ca09044d99d2372c0de244e66430761127164"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
name = "hashbrown"
version = "0.11.2"
@ -374,38 +202,6 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "instant"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
dependencies = [
"cfg-if",
]
[[package]]
name = "interprocess"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c58ec7fbda1df9a93f587b780659db3c99f61f4be27f9c82c9b37684ffd0366"
dependencies = [
"blocking",
"cfg-if",
"futures",
"intmap",
"libc",
"once_cell",
"spinning",
"thiserror",
"winapi",
]
[[package]]
name = "intmap"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae52f28f45ac2bc96edb7714de995cffc174a395fb0abf5bff453587c980d7b9"
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -418,15 +214,6 @@ version = "0.2.112"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
[[package]]
name = "lock_api"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712a4d093c9976e24e7dbca41db895dabcbac38eb5f4045393d17a95bdfb1109"
dependencies = [
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.14"
@ -498,42 +285,6 @@ dependencies = [
"memchr",
]
[[package]]
name = "parking"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72"
[[package]]
name = "pin-project-lite"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "proc-macro2"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145"
dependencies = [
"proc-macro2",
]
[[package]]
name = "radium"
version = "0.7.0"
@ -588,38 +339,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "slab"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5"
[[package]]
name = "spinning"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d4f0e86297cad2658d92a707320d87bf4e6ae1050287f51d19b67ef3f153a7b"
dependencies = [
"lock_api",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "sysinfo"
version = "0.23.0"
@ -656,38 +381,6 @@ version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
[[package]]
name = "thiserror"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "waker-fn"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca"
[[package]]
name = "winapi"
version = "0.3.9"

@ -8,7 +8,6 @@ authors = ["Shinyzenith <aakashsensharma@gmail.com>"]
clap = "3.0.4"
env_logger = "0.8.4"
evdev = "0.11.3"
interprocess = "1.1.1"
log = "0.4.0"
nix = "0.23.1"
sysinfo = "0.23.0"

@ -51,7 +51,7 @@ clean:
@cargo clean
setup:
@mkdir bin
@mkdir -p ./bin
@rustup install stable
@rustup default stable
@rustup target add x86_64-unknown-linux-musl

@ -1,8 +1,7 @@
use clap::{arg, App};
use evdev::{Device, Key};
use interprocess::local_socket::LocalSocketStream;
use nix::unistd;
use std::{env, io::prelude::*, path::Path, process::exit};
use std::{env, io::prelude::*, os::unix::net::UnixStream, path::Path, process::exit};
pub fn main() {
let args = set_flags().get_matches();
@ -32,40 +31,28 @@ pub fn main() {
log::trace!("Attempting to find all keyboard file descriptors.");
let mut keyboard_devices: Vec<Device> = Vec::new();
for (_, device) in evdev::enumerate().enumerate() {
if check_keyboard(&device) == true {
if check_keyboard(&device) {
keyboard_devices.push(device);
}
}
if keyboard_devices.len() == 0 {
if keyboard_devices.is_empty() {
log::error!("No valid keyboard device was detected!");
exit(1);
}
log::debug!("{} Keyboard device(s) detected.", keyboard_devices.len());
//TODO: IMPLEMENT KEYBOARD EVENT GRAB
let mut conn = match LocalSocketStream::connect("/tmp/swhkd.sock") {
Ok(conn) => conn,
match sock_send("notify-send hello world") {
Err(e) => {
log::error!("Unable to connect to hotkey server, is swhks running??");
log::error!("Error: {}", e);
exit(1);
}
};
match conn.write_all(args.value_of("shell").unwrap().as_bytes()) {
Ok(_) => {}
Err(e) => {
log::error!("Unable to send command to hotkey server, is swhks running??");
log::error!("Error: {}", e);
exit(1);
log::error!("Failed to send command over IPC.");
log::error!("Is swhks running?");
log::error!("{:#?}", e)
}
_ => {}
};
}
pub fn permission_check() {
if unistd::Uid::current().is_root() == false {
if !unistd::Uid::current().is_root() {
let groups = unistd::getgroups();
for (_, groups) in groups.iter().enumerate() {
for group in groups {
@ -86,10 +73,10 @@ pub fn permission_check() {
pub fn check_keyboard(device: &Device) -> bool {
if device.supported_keys().map_or(false, |keys| keys.contains(Key::KEY_ENTER)) {
log::debug!("{} is a keyboard.", device.name().unwrap(),);
return true;
true
} else {
log::trace!("{} is not a keyboard.", device.name().unwrap(),);
return false;
false
}
}
@ -103,13 +90,8 @@ pub fn set_flags() -> App<'static> {
.required(false)
.help("Set a custom config file path"),
)
.arg(arg!(-d - -debug).required(false).help("Enable debug mode"))
.arg(
arg!(-s - -shell <SHELL_COMMAND>)
.required(true)
.help("Shell command to run on success"),
);
return app;
.arg(arg!(-d - -debug).required(false).help("Enable debug mode"));
app
}
pub fn check_config_xdg() -> std::path::PathBuf {
@ -132,5 +114,11 @@ pub fn check_config_xdg() -> std::path::PathBuf {
log::warn!("The following issue may be addressed in the future, but it is certainly not a priority right now.");
}
}
return config_file_path;
config_file_path
}
fn sock_send(command: &str) -> std::io::Result<()> {
let mut stream = UnixStream::connect("/tmp/swhkd.sock")?;
stream.write_all(command.as_bytes())?;
Ok(())
}

@ -1,17 +1,17 @@
use interprocess::local_socket::{LocalSocketListener, LocalSocketStream};
use std::io::prelude::*;
use std::os::unix::net::UnixListener;
use std::{
env, fs,
io::{self, prelude::*, BufReader},
path::Path,
process::{exit, id, Command, Stdio},
};
use sysinfo::{ProcessExt, System, SystemExt};
fn main() {
fn main() -> std::io::Result<()> {
env::set_var("RUST_LOG", "swhks=trace");
env_logger::init();
let pidfile: String = String::from("/tmp/swhkc.pid");
let pidfile: String = String::from("/tmp/swhks.pid");
let sockfile: String = String::from("/tmp/swhkd.sock");
if Path::new(&pidfile).exists() {
@ -59,10 +59,6 @@ fn main() {
}
}
fn handle_error(connection: io::Result<LocalSocketStream>) -> Option<LocalSocketStream> {
connection.map_err(|error| log::error!("Incoming connection failed: {}", error)).ok()
}
fn run_system_command(command: &String) -> () {
match Command::new("sh")
.arg("-c")
@ -80,26 +76,16 @@ fn main() {
}
}
let listener = match LocalSocketListener::bind(sockfile.clone()) {
Ok(listener) => listener,
Err(e) => {
log::error!("Failed to connect to {}", sockfile);
log::error!("Error: {}", e);
exit(1);
}
};
for conn in listener.incoming().filter_map(handle_error) {
let mut conn = BufReader::new(conn);
let mut buffer = String::new();
match conn.read_line(&mut buffer) {
Ok(_) => {}
Err(e) => {
log::error!("Failed to read incoming command from client.");
log::error!("Error: {}", e);
exit(1);
let listener = UnixListener::bind(sockfile)?;
loop {
match listener.accept() {
Ok((mut socket, addr)) => {
let mut response = String::new();
socket.read_to_string(&mut response)?;
run_system_command(&response);
log::debug!("Socket: {:?} Address: {:?} Response: {}", socket, addr, response);
}
};
log::debug!("Recieved command : {}", buffer);
run_system_command(&buffer);
Err(e) => log::error!("accept function failed: {:?}", e),
}
}
}

Loading…
Cancel
Save