fn get_env()

in http-common/src/connector.rs [648:683]


fn get_env(env: &str) -> Result<Option<String>, String> {
    // Check that the LISTEN_* environment variable is for this process.
    let listen_pid = {
        let listen_pid = match std::env::var("LISTEN_PID") {
            Ok(listen_pid) => listen_pid,
            Err(std::env::VarError::NotPresent) => return Ok(None),
            Err(err @ std::env::VarError::NotUnicode(_)) => {
                return Err(format!("could not read LISTEN_PID env var: {err}"))
            }
        };

        let listen_pid = listen_pid
            .parse()
            .map_err(|err| format!("could not read LISTEN_PID env var: {err}"))?;

        nix::unistd::Pid::from_raw(listen_pid)
    };

    let current_pid = nix::unistd::Pid::this();
    if listen_pid != current_pid {
        // The env vars are not for us. Perhaps we're being spawned by another socket-activated service and we inherited these env vars from it.
        //
        // Either way, this is the same as if the env var wasn't set at all. That is, the caller wants us to find a socket-activated fd,
        // but we weren't started via socket activation.
        return Ok(None);
    }

    // Get the requested environment variable.
    match std::env::var(env) {
        Ok(value) => Ok(Some(value)),
        Err(std::env::VarError::NotPresent) => Ok(None),
        Err(err @ std::env::VarError::NotUnicode(_)) => {
            Err(format!("could not read {env} env var: {err}"))
        }
    }
}