diff --git a/swhkd/src/daemon.rs b/swhkd/src/daemon.rs index 8ea0842..59f22b8 100644 --- a/swhkd/src/daemon.rs +++ b/swhkd/src/daemon.rs @@ -75,7 +75,7 @@ struct Args { #[tokio::main] async fn main() -> Result<(), Box> { let args = Args::parse(); - let default_cooldown: u64 = 650; + let default_cooldown: u64 = 5; env::set_var("RUST_LOG", "swhkd=warn"); if args.debug { @@ -89,7 +89,7 @@ async fn main() -> Result<(), Box> { let invoking_uid = get_uid()?; let uname = get_uname_from_uid(invoking_uid)?; - let env = refresh_env(&uname, invoking_uid).unwrap(); + let env = refresh_env(&uname, invoking_uid, true).unwrap(); log::trace!("Environment Aquired"); let log_file_name = if let Some(val) = args.log { val @@ -125,7 +125,7 @@ async fn main() -> Result<(), Box> { // The server cool down is set to 650ms by default // which is calculated based on the default repeat cooldown // along with it, an additional 120ms is added to it, just to be safe. - let server_cooldown = args.refresh.unwrap_or(default_cooldown + 1024); + let server_cooldown = args.refresh.unwrap_or(default_cooldown); let (tx, mut rx) = tokio::sync::mpsc::channel::(100); let pairs = Arc::new(Mutex::new(env.pairs.clone())); @@ -137,9 +137,9 @@ async fn main() -> Result<(), Box> { loop { { let mut pairs = pairs_clone.lock().unwrap(); - pairs.clone_from(&refresh_env(&uname, invoking_uid).unwrap().pairs); + pairs.clone_from(&refresh_env(&uname, invoking_uid, false).unwrap().pairs); } - sleep(Duration::from_millis(server_cooldown)).await; + sleep(Duration::from_secs(server_cooldown)).await; } }); @@ -623,7 +623,7 @@ fn get_file_paths(runtime_dir: &str) -> (String, String) { (pid_file_path, sock_file_path) } -fn refresh_env(uname: &str, invoking_uid: u32) -> Result> { +fn refresh_env(uname: &str, invoking_uid: u32, skip: bool) -> Result> { let env = environ::Env::construct(uname, None); let (_pid_path, sock_path) = @@ -637,7 +637,7 @@ fn refresh_env(uname: &str, invoking_uid: u32) -> Result { let mut buf = String::new(); @@ -650,7 +650,14 @@ fn refresh_env(uname: &str, invoking_uid: u32) -> Result { - log::info!("Sock Err: {}", e); + if skip { + log::warn!("Sock Err: {}", e); + log::warn!("Unable to connect to server which is needed for environment"); + exit(1); + } else { + log::info!("Sock Err: {}", e); + log::info!("Server not found, skipping..."); + } } } } diff --git a/swhkd/src/environ.rs b/swhkd/src/environ.rs index 9ed0057..73d361b 100644 --- a/swhkd/src/environ.rs +++ b/swhkd/src/environ.rs @@ -7,20 +7,6 @@ pub struct Env { } impl Env { - pub fn get_default_shell() -> Result> { - // read from /etc/passwd - let passwd = std::fs::read_to_string("/etc/passwd")?; - let lines: Vec<&str> = passwd.split('\n').collect(); - for line in lines { - let parts: Vec<&str> = line.split(':').collect(); - if parts.len() > 2 { - let user_shell = parts[6]; - return Ok(user_shell.to_string()); - } - } - Err("User shell not found".into()) - } - fn get_env(uname: &str) -> Result> { // let shell = Self::get_default_shell()?; let cmd = Command::new("su").arg(uname).arg("-c").arg("-l").arg("env").output()?; diff --git a/swhkd/src/perms.rs b/swhkd/src/perms.rs index cd5a80b..3b17c60 100644 --- a/swhkd/src/perms.rs +++ b/swhkd/src/perms.rs @@ -1,7 +1,7 @@ use nix::unistd::{Gid, Uid, User}; use std::process::exit; -pub fn drop_privileges(user_uid: u32) { +pub fn _drop_privileges(user_uid: u32) { let user_uid = Uid::from_raw(user_uid); let user = User::from_uid(user_uid).unwrap().unwrap(); diff --git a/swhks/src/main.rs b/swhks/src/main.rs index 8744141..55c8a3f 100644 --- a/swhks/src/main.rs +++ b/swhks/src/main.rs @@ -49,7 +49,6 @@ fn parse_env(env: &str) -> HashMap { } pairs } - fn main() -> std::io::Result<()> { let args = Args::parse(); if args.debug { @@ -80,6 +79,7 @@ fn main() -> std::io::Result<()> { if let Ok(mut stream) = UnixStream::connect(&sock_file_path) { let _ = stream.write_all(env_raw.as_bytes()); }; + std::thread::sleep(std::time::Duration::from_secs(3)); } }