Skip to content

Commit

Permalink
Add server wait time
Browse files Browse the repository at this point in the history
  • Loading branch information
newtoallofthis123 committed Aug 25, 2024
1 parent 8a93e99 commit 456f6b7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
23 changes: 15 additions & 8 deletions swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct Args {
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();
let default_cooldown: u64 = 650;
let default_cooldown: u64 = 5;
env::set_var("RUST_LOG", "swhkd=warn");

if args.debug {
Expand All @@ -89,7 +89,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
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
Expand Down Expand Up @@ -125,7 +125,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// 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::<String>(100);
let pairs = Arc::new(Mutex::new(env.pairs.clone()));
Expand All @@ -137,9 +137,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
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;
}
});

Expand Down Expand Up @@ -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<environ::Env, Box<dyn Error>> {
fn refresh_env(uname: &str, invoking_uid: u32, skip: bool) -> Result<environ::Env, Box<dyn Error>> {
let env = environ::Env::construct(uname, None);

let (_pid_path, sock_path) =
Expand All @@ -637,7 +637,7 @@ fn refresh_env(uname: &str, invoking_uid: u32) -> Result<environ::Env, Box<dyn E
let listener = UnixListener::bind(&sock_path)?;
fs::set_permissions(sock_path, fs::Permissions::from_mode(0o666))?;
loop {
log::warn!("Waiting for Server...");
log::trace!("Waiting for Server...");
match listener.accept() {
Ok((mut socket, _addr)) => {
let mut buf = String::new();
Expand All @@ -650,7 +650,14 @@ fn refresh_env(uname: &str, invoking_uid: u32) -> Result<environ::Env, Box<dyn E
break;
}
Err(e) => {
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...");
}
}
}
}
Expand Down
14 changes: 0 additions & 14 deletions swhkd/src/environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,6 @@ pub struct Env {
}

impl Env {
pub fn get_default_shell() -> Result<String, Box<dyn Error>> {
// 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<String, Box<dyn Error>> {
// let shell = Self::get_default_shell()?;
let cmd = Command::new("su").arg(uname).arg("-c").arg("-l").arg("env").output()?;
Expand Down
2 changes: 1 addition & 1 deletion swhkd/src/perms.rs
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
2 changes: 1 addition & 1 deletion swhks/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ fn parse_env(env: &str) -> HashMap<String, String> {
}
pairs
}

fn main() -> std::io::Result<()> {
let args = Args::parse();
if args.debug {
Expand Down Expand Up @@ -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));
}
}

Expand Down

0 comments on commit 456f6b7

Please sign in to comment.