Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: swhkd binary should never read environment #286

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Without this request, the environmental variables responsible for the reading for the config
// file will not be available.
// Thus, it is important to wait for the server to start before proceeding.
let mut env = environ::Env::construct(None);
let mut env_hash = 0;
let env;
let mut env_hash;
loop {
match refresh_env(invoking_uid, 0) {
Ok((Some(new_env), hash)) => {
env_hash = hash;
env = new_env;
break;
}
Ok((None, hash)) => {
env_hash = hash;
Ok((None, _)) => {
log::debug!("Waiting for env...");
continue;
}
Expand Down
10 changes: 2 additions & 8 deletions swhkd/src/environ.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
use std::{collections::HashMap, error::Error, path::PathBuf, process::Command};
use std::{collections::HashMap, path::PathBuf};

#[derive(Debug, Clone)]
pub struct Env {
pub pairs: HashMap<String, String>,
}

impl Env {
fn get_env() -> Result<String, Box<dyn Error>> {
let cmd = Command::new("env").output()?;
let stdout = String::from_utf8(cmd.stdout)?;
Ok(stdout)
}

fn parse_env(env: &str) -> HashMap<String, String> {
let mut pairs = HashMap::new();
for line in env.lines() {
Expand All @@ -27,7 +21,7 @@ impl Env {
pub fn construct(env: Option<&str>) -> Self {
let env = match env {
Some(env) => env.to_string(),
None => Self::get_env().unwrap(),
None => "".to_string(),
};
let pairs = Self::parse_env(&env);
Self { pairs }
Expand Down
Loading