Skip to content

Commit ab1e3c2

Browse files
committed
nss/passwd: Do the pre-check test once per process
The PID of the library won't change during the execution, so perform the test and save the information for later use, instead of repeating the same check for each request
1 parent 884735f commit ab1e3c2

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

nss/src/passwd/mod.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use libc::uid_t;
33
use libnss::interop::Response;
44
use libnss::passwd::{Passwd, PasswdHooks};
55
use std::path::PathBuf;
6+
use std::sync::OnceLock;
67
use tokio::runtime::Builder;
78
use tonic::Request;
89

@@ -178,13 +179,18 @@ fn is_proc_matching(pid: u32, name: &str) -> bool {
178179
/// should_pre_check returns true if the current process sshd or a child of sshd.
179180
#[allow(unreachable_code)] // This function body is overridden in integration tests, so we need to ignore the warning.
180181
fn should_pre_check() -> bool {
181-
#[cfg(feature = "should_pre_check_env")]
182-
return std::env::var("AUTHD_NSS_SHOULD_PRE_CHECK").is_ok();
182+
static SHOULD_PRE_CHECK: OnceLock<bool> = OnceLock::new();
183183

184-
let pid = std::process::id();
185-
if is_proc_matching(pid, SSHD_BINARY_PATH) {
186-
return true;
187-
}
184+
*SHOULD_PRE_CHECK.get_or_init(|| {
185+
#[cfg(feature = "should_pre_check_env")]
186+
return std::env::var("AUTHD_NSS_SHOULD_PRE_CHECK").is_ok();
187+
188+
let pid = std::process::id();
188189

189-
is_proc_matching(std::os::unix::process::parent_id(), SSHD_BINARY_PATH)
190+
if is_proc_matching(pid, SSHD_BINARY_PATH) {
191+
return true;
192+
}
193+
194+
is_proc_matching(std::os::unix::process::parent_id(), SSHD_BINARY_PATH)
195+
})
190196
}

0 commit comments

Comments
 (0)