[delete] Remove all IPC code.

main
Shinyzenith 2 years ago
parent 54234ac9e8
commit b5a7422c89
No known key found for this signature in database
GPG Key ID: A3DFCDC66E9E2950

@ -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, path::Path, process::exit};
pub fn main() {
let args = set_flags().get_matches();
@ -42,26 +41,6 @@ pub fn main() {
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,
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);
}
};
}
pub fn permission_check() {
@ -103,12 +82,7 @@ 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"),
);
.arg(arg!(-d - -debug).required(false).help("Enable debug mode"));
return app;
}

@ -1,105 +0,0 @@
use interprocess::local_socket::{LocalSocketListener, LocalSocketStream};
use std::{
env, fs,
io::{self, prelude::*, BufReader},
path::Path,
process::{exit, id, Command, Stdio},
};
use sysinfo::{ProcessExt, System, SystemExt};
fn main() {
env::set_var("RUST_LOG", "swhks=trace");
env_logger::init();
let pidfile: String = String::from("/tmp/swhkc.pid");
let sockfile: String = String::from("/tmp/swhkd.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,
Err(e) => {
log::error!("Unable to read {} to check all running instances", e);
exit(1);
}
};
log::debug!("Previous PID: {}", swhkd_pid);
let mut sys = System::new_all();
sys.refresh_all();
for (pid, process) in sys.processes() {
if pid.to_string() == swhkd_pid {
if process.exe() == env::current_exe().unwrap() {
log::error!("Server is already running!");
exit(1);
}
}
}
}
if Path::new(&sockfile).exists() {
log::trace!("Sockfile exists, attempting to remove it.");
match fs::remove_file(&sockfile) {
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: {}", sockfile);
exit(1);
}
};
}
match fs::write(&pidfile, id().to_string()) {
Ok(_) => {}
Err(e) => {
log::error!("Unable to write to {}: {}", pidfile, e);
exit(1);
}
}
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")
.arg(command)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
{
Ok(_) => {}
Err(e) => {
log::error!("Failed to execute {}", command);
log::error!("Error, {}", e);
}
}
}
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);
}
};
log::debug!("Recieved command : {}", buffer);
run_system_command(&buffer);
}
}

@ -0,0 +1,2 @@
#!/bin/sh
pkexec /usr/local/bin/swhkd
Loading…
Cancel
Save