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 config::{
Hotkey,
Modifier,
};
use rdev::{
Event,
EventType::{
@ -7,27 +11,23 @@ use rdev::{
},
Key,
};
use nix::unistd::{Group, Uid};
use once_cell::sync::OnceCell;
use std::{
cell::RefCell,
thread,
sync::{
Mutex,
Arc,
mpsc::{
sync_channel,
SyncSender,
Receiver
},
},
collections::HashMap,
env, fs,
io::prelude::*,
os::unix::net::UnixStream,
os::unix::net::UnixListener,
path::Path,
process::{exit, id},
thread::sleep,
time::Duration,
time::SystemTime,
};
use sysinfo::{ProcessExt, System, SystemExt};
@ -40,70 +40,32 @@ pub struct LastHotkey {
ran_at: SystemTime,
}
static STATE_MODE: OnceCell<Mutex<String>> = OnceCell::new();
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() {
pub fn key_listener(am_mode: Arc<Mutex<Option<String>>>, tx: SyncSender<Hotkey>) {
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") {
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() {
log::trace!("Reading {} file and checking for running instances.", pidfile);
let swhkd_pid = match fs::read_to_string(&pidfile) {
Ok(swhkd_pid) => swhkd_pid,
let sohkd_pid = match fs::read_to_string(&pidfile) {
Ok(sohkd_pid) => sohkd_pid,
Err(e) => {
log::error!("Unable to read {} to check all running instances", e);
exit(1);
}
};
log::debug!("Previous PID: {}", swhkd_pid);
log::debug!("Previous PID: {}", sohkd_pid);
let mut sys = System::new_all();
sys.refresh_all();
for (pid, process) in sys.processes() {
if pid.to_string() == swhkd_pid && process.exe() == env::current_exe().unwrap() {
log::error!("Swhkd is already running!");
if pid.to_string() == sohkd_pid && process.exe() == env::current_exe().unwrap() {
log::error!("sohkd is already running!");
exit(1);
}
}
@ -117,8 +79,6 @@ pub fn key_listener() {
}
}
permission_check();
let config_file_path: std::path::PathBuf;
if args.is_present("config") {
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);
}
let modifiers_map: HashMap<Key, config::Modifier> = HashMap::from([
(Key::MetaLeft, config::Modifier::Super),
(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 current_keys: RefCell<Vec<Key>> = RefCell::new(Vec::new());
let last_keys: RefCell<Vec<Key>> = RefCell::new(Vec::new());
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];
let mut last_hotkey = LastHotkey {
// just a dummy last_hotkey so I don't need to mess with Option<T>. TODO: Change this to Option<T>
hotkey: config::Hotkey::new(
Some("".to_string()),
Key::KeyA,
default_test_modifier,
String::from("notify-send \"it works\""),
false,
),
ran_at: SystemTime::now(),
};
pub fn key_to_mod(key: &Key) -> Option<Modifier> {
match key {
Key::MetaLeft => Some(config::Modifier::Super),
Key::MetaRight => Some(config::Modifier::Super),
Key::Alt => Some(config::Modifier::Alt),
Key::AltGr => Some(config::Modifier::Alt),
Key::ControlLeft => Some(config::Modifier::Control),
Key::ControlRight => Some(config::Modifier::Control),
Key::ShiftLeft => Some(config::Modifier::Shift),
Key::ShiftRight => Some(config::Modifier::Shift),
_ => None
}
}
loop {
// TODO
pub fn get_modifiers(keys: &Vec<Key>) -> Vec<Modifier> {
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() {
env_logger::init();
let key_handler = thread::spawn(|| {
key_listener();
log::trace!("Logger initialized.");
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 res = listen_for_mode_change();
let key_handler_listener = thread::spawn(move || {
key_listener(k_am_mode, tx);
});
let mode_updater = thread::spawn(move || {
let res = listen_for_mode_change(m_am_mode);
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),
}
});
*/
log::trace!("Threads set up");
//mode_updater.join().unwrap();
key_handler.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!");
}
mode_updater.join().unwrap();
key_receiver.join().unwrap();
key_handler_listener.join().unwrap();
}
pub fn set_flags() -> App<'static> {
let app = App::new("swhkd")
let app = App::new("sohkd")
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
.about("Simple Wayland HotKey Daemon")
@ -238,13 +319,13 @@ pub fn check_config_xdg() -> std::path::PathBuf {
let config_file_path: std::path::PathBuf;
match env::var("XDG_CONFIG_HOME") {
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);
return config_file_path;
}
Err(_) => {
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!(
"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<()> {
let mut stream = UnixStream::connect("/tmp/swhkd.sock")?;
let mut stream = UnixStream::connect("/tmp/sohkd.sock")?;
stream.write_all(command.as_bytes())?;
Ok(())
}

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

Loading…
Cancel
Save