diff --git a/swhkd/src/daemon.rs b/swhkd/src/daemon.rs index 3c84be3..d11976b 100644 --- a/swhkd/src/daemon.rs +++ b/swhkd/src/daemon.rs @@ -87,11 +87,9 @@ async fn main() -> Result<(), Box> { perms::raise_privileges(); let invoking_uid = get_uid()?; - let uname = get_uname_from_uid(invoking_uid)?; - let env = refresh_env(&uname, invoking_uid, true) - .unwrap() - .unwrap_or(environ::Env::construct(&uname, None)); + log::debug!("Wating for server to start..."); + let env = refresh_env(invoking_uid)?.unwrap_or(environ::Env::construct(None)); log::trace!("Environment Aquired"); let log_file_name = if let Some(val) = args.log { val @@ -140,7 +138,7 @@ async fn main() -> Result<(), Box> { loop { { let mut pairs = pairs_clone.lock().unwrap(); - match refresh_env(&uname, invoking_uid, false) { + match refresh_env(invoking_uid) { Ok(Some(env)) => { pairs.clone_from(&env.pairs); } @@ -612,23 +610,6 @@ fn get_uid() -> Result> { Ok(uid) } -fn get_uname_from_uid(uid: u32) -> Result> { - let passwd = fs::read_to_string("/etc/passwd").unwrap(); - let lines: Vec<&str> = passwd.split('\n').collect(); - for line in lines { - let parts: Vec<&str> = line.split(':').collect(); - if parts.len() > 2 { - let Ok(user_id) = parts[2].parse::() else { - continue; - }; - if user_id == uid { - return Ok(parts[0].to_string()); - } - } - } - Err("User not found".into()) -} - fn get_file_paths(runtime_dir: &str) -> (String, String) { let pid_file_path = format!("{}/swhks.pid", runtime_dir); let sock_file_path = format!("{}/swhkd.sock", runtime_dir); @@ -636,12 +617,8 @@ fn get_file_paths(runtime_dir: &str) -> (String, String) { (pid_file_path, sock_file_path) } -fn refresh_env( - uname: &str, - invoking_uid: u32, - skip: bool, -) -> Result, Box> { - let env = environ::Env::construct(uname, None); +fn refresh_env(invoking_uid: u32) -> Result, Box> { + let env = environ::Env::construct(None); let (_pid_path, sock_path) = get_file_paths(env.xdg_runtime_dir(invoking_uid).to_str().unwrap()); @@ -653,31 +630,14 @@ fn refresh_env( let mut result: String = String::new(); let listener = UnixListener::bind(&sock_path)?; fs::set_permissions(sock_path, fs::Permissions::from_mode(0o666))?; - loop { - log::trace!("Waiting for Server..."); - match listener.accept() { - Ok((mut socket, _addr)) => { - let mut buf = String::new(); - socket.read_to_string(&mut buf)?; - if buf.is_empty() { - return Ok(None); - } - log::info!("Server Instance found!"); - result.push_str(&buf); - break; - } - 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..."); - } - } + for mut socket in listener.incoming().flatten() { + let mut buf = String::new(); + socket.read_to_string(&mut buf)?; + if buf.is_empty() { + return Ok(None); } + log::info!("Env refreshed from server."); + result.push_str(&buf); } - log::trace!("Environment Refreshed"); - Ok(Some(environ::Env::construct(uname, Some(&result)))) + Ok(Some(environ::Env::construct(Some(&result)))) } diff --git a/swhkd/src/environ.rs b/swhkd/src/environ.rs index 4232953..4c1bac3 100644 --- a/swhkd/src/environ.rs +++ b/swhkd/src/environ.rs @@ -3,13 +3,12 @@ use std::{collections::HashMap, error::Error, path::PathBuf, process::Command}; #[derive(Debug, Clone)] pub struct Env { pub pairs: HashMap, - pub uname: String, } impl Env { - fn get_env(uname: &str) -> Result> { + fn get_env() -> Result> { // let shell = Self::get_default_shell()?; - let cmd = Command::new("su").arg(uname).arg("-c").arg("-l").arg("env").output()?; + let cmd = Command::new("env").output()?; let stdout = String::from_utf8(cmd.stdout)?; Ok(stdout) } @@ -25,17 +24,13 @@ impl Env { pairs } - pub fn refresh_env(&mut self, pairs: HashMap) { - self.pairs = pairs; - } - - pub fn construct(uname: &str, env: Option<&str>) -> Self { + pub fn construct(env: Option<&str>) -> Self { let env = match env { Some(env) => env.to_string(), - None => Self::get_env(uname).unwrap(), + None => Self::get_env().unwrap(), }; let pairs = Self::parse_env(&env); - Self { pairs, uname: uname.to_string() } + Self { pairs } } pub fn fetch_home(&self) -> Option { diff --git a/swhks/src/main.rs b/swhks/src/main.rs index 270cd0d..17b68dc 100644 --- a/swhks/src/main.rs +++ b/swhks/src/main.rs @@ -57,7 +57,7 @@ fn main() -> std::io::Result<()> { let (_pid_file_path, sock_file_path) = get_file_paths(&runtime_dir); log::info!("Started SWHKS placeholder server"); - //let _ = daemon(true, false); + let _ = daemon(true, false); setup_swhks(invoking_uid, PathBuf::from(runtime_dir)); loop { if let Ok(mut stream) = UnixStream::connect(&sock_file_path) {