Move socket sending to new thread

main
Tait Hoyem 2 years ago
parent d58fb3d5c3
commit 80f4fde12d

@ -1,4 +1,8 @@
use clap::{arg, App}; use clap::{arg, App};
use config::{
Hotkey,
Modifier,
};
use rdev::{ use rdev::{
Event, Event,
EventType::{ EventType::{
@ -7,27 +11,23 @@ use rdev::{
}, },
Key, Key,
}; };
use nix::unistd::{Group, Uid};
use once_cell::sync::OnceCell;
use std::{ use std::{
cell::RefCell,
thread, thread,
sync::{ sync::{
Mutex, Mutex,
Arc,
mpsc::{ mpsc::{
sync_channel, sync_channel,
SyncSender, SyncSender,
Receiver
}, },
}, },
collections::HashMap,
env, fs, env, fs,
io::prelude::*, io::prelude::*,
os::unix::net::UnixStream, os::unix::net::UnixStream,
os::unix::net::UnixListener, os::unix::net::UnixListener,
path::Path, path::Path,
process::{exit, id}, process::{exit, id},
thread::sleep,
time::Duration,
time::SystemTime, time::SystemTime,
}; };
use sysinfo::{ProcessExt, System, SystemExt}; use sysinfo::{ProcessExt, System, SystemExt};
@ -40,70 +40,32 @@ pub struct LastHotkey {
ran_at: SystemTime, ran_at: SystemTime,
} }
static STATE_MODE: OnceCell<Mutex<String>> = OnceCell::new(); pub fn key_listener(am_mode: Arc<Mutex<Option<String>>>, tx: SyncSender<Hotkey>) {
static MODE_SOCK: &str = "/tmp/odilia-mode.sock";
fn listen_for_mode_change() -> std::io::Result<()> {
log::trace!("Mode change listener started!");
let listener = UnixListener::bind(MODE_SOCK)?;
match listener.accept() {
Ok((mut socket, _addr)) => {
let mut response = String::new();
socket.read_to_string(&mut response)?;
log::trace!("reponse from odilia-mode.sock: '{}'", response);
let mode_m = STATE_MODE.get();
match mode_m {
Some(mm) => {
let mut mode = mm.lock();
match mode {
Ok(mut m) => {
*m = response;
println!("Mode updated to '{}'", m);
log::trace!("Mode updated to '{}'", m);
},
Err(e) => log::error!("Could not lock mode."),
}
},
_ => log::error!("Could not get static STATE_MODE."),
}
},
Err(e) => log::error!("accept function failed: {:?}", e),
}
Ok(())
}
pub fn key_listener() {
let args = set_flags().get_matches(); let args = set_flags().get_matches();
env::set_var("RUST_LOG", "swhkd=warn"); env::set_var("RUST_LOG", "sohkd=warn");
if args.is_present("debug") { if args.is_present("debug") {
env::set_var("RUST_LOG", "swhkd=trace"); env::set_var("RUST_LOG", "sohkd=trace");
} }
log::trace!("Logger initialized.");
match STATE_MODE.set(Mutex::new(String::new())) {
Err(e) => log::error!("could not initialize STATE_MODE"),
_ => log::trace!("STATE_MODE initialized"),
}
let pidfile: String = String::from("/tmp/swhkd.pid"); let pidfile: String = String::from("/tmp/sohkd.pid");
if Path::new(&pidfile).exists() { if Path::new(&pidfile).exists() {
log::trace!("Reading {} file and checking for running instances.", pidfile); log::trace!("Reading {} file and checking for running instances.", pidfile);
let swhkd_pid = match fs::read_to_string(&pidfile) { let sohkd_pid = match fs::read_to_string(&pidfile) {
Ok(swhkd_pid) => swhkd_pid, Ok(sohkd_pid) => sohkd_pid,
Err(e) => { Err(e) => {
log::error!("Unable to read {} to check all running instances", e); log::error!("Unable to read {} to check all running instances", e);
exit(1); exit(1);
} }
}; };
log::debug!("Previous PID: {}", swhkd_pid); log::debug!("Previous PID: {}", sohkd_pid);
let mut sys = System::new_all(); let mut sys = System::new_all();
sys.refresh_all(); sys.refresh_all();
for (pid, process) in sys.processes() { for (pid, process) in sys.processes() {
if pid.to_string() == swhkd_pid && process.exe() == env::current_exe().unwrap() { if pid.to_string() == sohkd_pid && process.exe() == env::current_exe().unwrap() {
log::error!("Swhkd is already running!"); log::error!("sohkd is already running!");
exit(1); exit(1);
} }
} }
@ -117,8 +79,6 @@ pub fn key_listener() {
} }
} }
permission_check();
let config_file_path: std::path::PathBuf; let config_file_path: std::path::PathBuf;
if args.is_present("config") { if args.is_present("config") {
config_file_path = Path::new(args.value_of("config").unwrap()).to_path_buf(); config_file_path = Path::new(args.value_of("config").unwrap()).to_path_buf();
@ -144,77 +104,198 @@ pub fn key_listener() {
log::debug!("hotkey: {:#?}", hotkey); log::debug!("hotkey: {:#?}", hotkey);
} }
let modifiers_map: HashMap<Key, config::Modifier> = HashMap::from([ let current_keys: RefCell<Vec<Key>> = RefCell::new(Vec::new());
(Key::MetaLeft, config::Modifier::Super), let last_keys: RefCell<Vec<Key>> = RefCell::new(Vec::new());
(Key::MetaRight, config::Modifier::Super),
(Key::Alt, config::Modifier::Alt),
(Key::AltGr, config::Modifier::Alt),
(Key::ControlLeft, config::Modifier::Control),
(Key::ControlRight, config::Modifier::Control),
(Key::ShiftLeft, config::Modifier::Shift),
(Key::ShiftRight, config::Modifier::Shift),
]);
let mut possible_hotkeys: Vec<config::Hotkey> = Vec::new(); let rgrab = rdev::grab(move |ev| {
// If this is a repeated key sequence (i.e., polled 2nd+ time while same combo is held), then
if !is_new_key_event(&ev, &mut current_keys.borrow_mut(), &mut last_keys.borrow_mut()) {
return Some(ev);
}
let mode = am_mode.lock().expect("thread crashed due to not being able to lock mutex!");
let hotkey = hotkey_match(&current_keys.borrow(), &mode, &hotkeys);
match hotkey {
Some(hk) => {
// needed so the value can be used after sending it down the channel
let consume = hk.consume.clone();
match tx.send(hk) {
Ok(_) => log::trace!("Hotkey sent down channel successfully!"),
Err(e) => log::error!("Hotkey could not be sent down channel! {:?}", e),
}
if consume { return None; }
else { return Some(ev); }
},
None => {
log::trace!("hotkey not found");
return Some(ev);
}
}
});
match rgrab {
Ok(_) => log::debug!("rdev::grab has run successfully!"),
Err(e) => log::error!("rdev::grab returned an error: {:?}", e),
}
}
let default_test_modifier: Vec<config::Modifier> = vec![config::Modifier::Super]; pub fn key_to_mod(key: &Key) -> Option<Modifier> {
let mut last_hotkey = LastHotkey { match key {
// just a dummy last_hotkey so I don't need to mess with Option<T>. TODO: Change this to Option<T> Key::MetaLeft => Some(config::Modifier::Super),
hotkey: config::Hotkey::new( Key::MetaRight => Some(config::Modifier::Super),
Some("".to_string()), Key::Alt => Some(config::Modifier::Alt),
Key::KeyA, Key::AltGr => Some(config::Modifier::Alt),
default_test_modifier, Key::ControlLeft => Some(config::Modifier::Control),
String::from("notify-send \"it works\""), Key::ControlRight => Some(config::Modifier::Control),
false, Key::ShiftLeft => Some(config::Modifier::Shift),
), Key::ShiftRight => Some(config::Modifier::Shift),
ran_at: SystemTime::now(), _ => None
}; }
}
loop { pub fn get_modifiers(keys: &Vec<Key>) -> Vec<Modifier> {
// TODO keys.iter().filter_map(key_to_mod).collect()
}
pub fn is_mod(key: &Key) -> bool {
match key {
Key::MetaLeft => true,
Key::MetaRight => true,
Key::Alt => true,
Key::AltGr => true,
Key::ControlLeft => true,
Key::ControlRight => true,
Key::ShiftLeft => true,
Key::ShiftRight => true,
_ => false
}
}
pub fn get_keysym(keys: &Vec<Key>) -> Option<Key> {
for key in keys {
if !is_mod(key) { return Some(*key); }
}
None
}
pub fn hotkey_match(current_keys: &Vec<Key>, mode: &Option<String>, hotkeys: &Vec<config::Hotkey>) -> Option<config::Hotkey> {
for key in hotkeys {
log::trace!("hotkey info: {:#?}", key);
let keysym = get_keysym(&current_keys);
log::trace!("\tkeysym: {:#?}", keysym);
log::trace!("\tmods: {:#?}", get_modifiers(&current_keys));
log::trace!("\tmode: {:#?}", mode);
if key.modifiers == get_modifiers(&current_keys)
&& (keysym.is_some() && key.keysym == keysym.unwrap())
&& (key.mode.is_none() || key.mode == *mode) {
return Some(key.clone());
} }
}
None
}
pub fn is_new_key_event(event: &Event, current_keys: &mut Vec<Key>, last_keys: &mut Vec<Key>) -> bool {
match event.event_type {
KeyPress(x) => {
log::trace!("KeyPress: {:#?}", x);
*last_keys = current_keys.clone();
current_keys.push(x);
current_keys.dedup();
// if there is a new key pressed/released and it is not a repeat event
if last_keys != current_keys {
log::trace!("KEYS: {:?}", current_keys);
true
} else {
false
}
},
KeyRelease(x) => {
log::trace!("KeyRelease: {:#?}", x);
*last_keys = current_keys.clone();
// remove just released key from curent keys
current_keys.retain(|&k| k != x);
false
},
_ => false
}
}
static MODE_SOCK: &str = "/tmp/sohkctl.sock";
pub fn listen_for_mode_change(mode_mutex: Arc<Mutex<Option<String>>>) -> std::io::Result<()> {
if Path::new(MODE_SOCK).exists() {
log::trace!("Sockfile exists, attempting to remove it.");
match fs::remove_file(MODE_SOCK) {
Ok(_) => {
log::debug!("Removed old socket file");
}
Err(e) => {
log::error!("Error removing the socket file!: {}", e);
log::error!("You can manually remove the socket file: {}", MODE_SOCK);
exit(1);
}
};
}
let listener = UnixListener::bind(MODE_SOCK)?;
loop {
match listener.accept() {
Ok((mut socket, addr)) => {
let mut response = String::new();
socket.read_to_string(&mut response)?;
let mut mode = mode_mutex.lock().expect("socket thread dies because of inability to lock mode!");
log::debug!("socket: {:?} Address: {:?}, response: {:?}", socket, addr, response);
*mode = Some(response);
},
Err(e) => log::error!("accept function failed! {:?}", e),
}
}
} }
pub fn main() { pub fn main() {
env_logger::init(); env_logger::init();
let key_handler = thread::spawn(|| { log::trace!("Logger initialized.");
key_listener(); let am_mode: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));
let k_am_mode = Arc::clone(&am_mode);
let m_am_mode = Arc::clone(&am_mode);
let (tx, rx) = sync_channel::<Hotkey>(1024);
let key_receiver = thread::spawn(move || {
loop {
match rx.recv() {
Ok(hk) => {
match sock_send(&hk.command) {
Ok(_) => {
log::debug!("Successful sending of command to server process.");
}
Err(e) => {
log::debug!("Error sending a command over the socket! {:?}", e);
}
}
},
Err(e) => {
log::debug!("Error receiving key on key_sender thread. {:?}", e);
}
}
}
}); });
/*
let mode_updater = thread::spawn(|| { let key_handler_listener = thread::spawn(move || {
let res = listen_for_mode_change(); key_listener(k_am_mode, tx);
});
let mode_updater = thread::spawn(move || {
let res = listen_for_mode_change(m_am_mode);
match res { match res {
Ok(listener) => log::trace!("Finished mode changer!"), Ok(_) => log::trace!("Finished mode changer!"),
Err(e) => log::error!("Error setting up mode socket: {}", e), Err(e) => log::error!("Error setting up mode socket: {}", e),
} }
}); });
*/
log::trace!("Threads set up"); log::trace!("Threads set up");
//mode_updater.join().unwrap(); mode_updater.join().unwrap();
key_handler.join().unwrap(); key_receiver.join().unwrap();
} key_handler_listener.join().unwrap();
pub fn permission_check() {
if !Uid::current().is_root() {
let groups = nix::unistd::getgroups();
for (_, groups) in groups.iter().enumerate() {
for group in groups {
let group = Group::from_gid(*group);
if group.unwrap().unwrap().name == "input" {
log::error!("Note: INVOKING USER IS IN INPUT GROUP!!!!");
log::error!("THIS IS A HUGE SECURITY RISK!!!!");
}
}
}
log::error!("Consider using `pkexec swhkd ...`");
} else {
log::warn!("Running swhkd as root!");
}
} }
pub fn set_flags() -> App<'static> { pub fn set_flags() -> App<'static> {
let app = App::new("swhkd") let app = App::new("sohkd")
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS")) .author(env!("CARGO_PKG_AUTHORS"))
.about("Simple Wayland HotKey Daemon") .about("Simple Wayland HotKey Daemon")
@ -238,13 +319,13 @@ pub fn check_config_xdg() -> std::path::PathBuf {
let config_file_path: std::path::PathBuf; let config_file_path: std::path::PathBuf;
match env::var("XDG_CONFIG_HOME") { match env::var("XDG_CONFIG_HOME") {
Ok(val) => { Ok(val) => {
config_file_path = Path::new(&val).join("swhkd/swhkdrc"); config_file_path = Path::new(&val).join("sohkd/sohkdrc");
log::debug!("XDG_CONFIG_HOME exists: {:#?}", val); log::debug!("XDG_CONFIG_HOME exists: {:#?}", val);
return config_file_path; return config_file_path;
} }
Err(_) => { Err(_) => {
log::error!("XDG_CONFIG_HOME has not been set."); log::error!("XDG_CONFIG_HOME has not been set.");
config_file_path = Path::new("/etc/swhkd/swhkdrc").to_path_buf(); config_file_path = Path::new("/etc/sohkd/sohkdrc").to_path_buf();
log::warn!( log::warn!(
"Note: Due to the design of the application, the invoking user is always root." "Note: Due to the design of the application, the invoking user is always root."
); );
@ -258,7 +339,7 @@ pub fn check_config_xdg() -> std::path::PathBuf {
} }
fn sock_send(command: &str) -> std::io::Result<()> { fn sock_send(command: &str) -> std::io::Result<()> {
let mut stream = UnixStream::connect("/tmp/swhkd.sock")?; let mut stream = UnixStream::connect("/tmp/sohkd.sock")?;
stream.write_all(command.as_bytes())?; stream.write_all(command.as_bytes())?;
Ok(()) Ok(())
} }

@ -11,24 +11,24 @@ fn main() -> std::io::Result<()> {
env::set_var("RUST_LOG", "swhks=trace"); env::set_var("RUST_LOG", "swhks=trace");
env_logger::init(); env_logger::init();
let pidfile: String = String::from("/tmp/swhks.pid"); let pidfile: String = String::from("/tmp/sohks.pid");
let sockfile: String = String::from("/tmp/swhkd.sock"); let sockfile: String = String::from("/tmp/sohkd.sock");
if Path::new(&pidfile).exists() { if Path::new(&pidfile).exists() {
log::trace!("Reading {} file and checking for running instances.", pidfile); log::trace!("Reading {} file and checking for running instances.", pidfile);
let swhkd_pid = match fs::read_to_string(&pidfile) { let sohkd_pid = match fs::read_to_string(&pidfile) {
Ok(swhkd_pid) => swhkd_pid, Ok(sohkd_pid) => sohkd_pid,
Err(e) => { Err(e) => {
log::error!("Unable to read {} to check all running instances", e); log::error!("Unable to read {} to check all running instances", e);
exit(1); exit(1);
} }
}; };
log::debug!("Previous PID: {}", swhkd_pid); log::debug!("Previous PID: {}", sohkd_pid);
let mut sys = System::new_all(); let mut sys = System::new_all();
sys.refresh_all(); sys.refresh_all();
for (pid, process) in sys.processes() { for (pid, process) in sys.processes() {
if pid.to_string() == swhkd_pid && process.exe() == env::current_exe().unwrap() { if pid.to_string() == sohkd_pid && process.exe() == env::current_exe().unwrap() {
log::error!("Server is already running!"); log::error!("Server is already running!");
exit(1); exit(1);
} }

Loading…
Cancel
Save