Reading from pipe via stdin in binary

⚓ Rust    📅 2025-08-16    👤 surdeus    👁️ 6      

surdeus

Implementing a responder for Apache mod_fcgi.

The responder reads binary records from stdin, and writes binary records to stdout. Stdin and stdout are UNIX pipes. I'm getting an EINVAL at the first read.

So I stripped the program down to this:

pub fn main() {
    logger();   // start logging
    let mut outio = std::io::stdout();
    let inio = std::io::stdin();
    let mut instream = inio.lock(); // Lock the stdin for reading.
    // ***TEMP TEST***
    let mut header_bytes:[u8;8] = Default::default();
    use std::io::Read;
    let stat = instream.read_exact(&mut header_bytes);
    log::debug!("Stat: {:?} Bytes: {:?}", stat, header_bytes);
    std::process::exit(0);
    // ***END TEMP***
}

/// Debug logging
fn logger() {
    //  Log file is openly visible as a web page.
    //  Only for debug tests.
    const LOG_FILE_NAME: &str = "logs/echolog.txt";
    let _ = simplelog::CombinedLogger::init(vec![
            simplelog::WriteLogger::new(
                LevelFilter::Debug,
                simplelog::Config::default(),
                std::fs::File::create(LOG_FILE_NAME).expect("Unable to create log file"),
            ),
        ]);
    log::warn!("Logging to {:?}", LOG_FILE_NAME); // where the log is going
}

which gets me, when run under mod_fcgid under Apache on a Linux server,

19:14:55 [WARN] Logging to "logs/echolog.txt"
19:14:55 [DEBUG] (1) echo: Stat: Err(Os { code: 22, kind: InvalidInput, message: "Invalid argument" }) Bytes: [0, 0, 0, 0, 0, 0, 0, 0]

Now, according to Github Copilot, this can occur when non UTF-8 characters come in via stdin. read_exact is a binary read, though. This should work.

Do I need to do something to put "stdin" in "raw mode" here?

3 posts - 2 participants

Read full topic

🏷️ Rust_feed