Reputation: 4428
I am working to build a wrapper to test an application that occasionally results in a Segmentation Fault.
use std::io::{Write};
use std::process::{Command, Stdio};
fn main() {
let input_name = "Saqib Ali";
let input_email = "[email protected]"
let input_serial_numer = "SN103213112"
let register_app = Command::new("register_drive").stdin(Stdio::piped()).spawn().unwrap();
write!(register_app.stdin.unwrap(), "{}", input_name).unwrap();
}
How do I detect if a program resulted in Segmentation Fault when a input_name
was sent to it?
Upvotes: 0
Views: 182
Reputation: 155046
You can wait for the process to finish and use the Unix-specific ExitStatusExt
trait to extract the signal code out of the exit status and the libc
crate to identify the signal as SIGSEGV
:
use std::os::unix::process::ExitStatusExt;
let input_name = "Saqib Ali";
let mut register_app = Command::new("register_drive")
.stdin(Stdio::piped())
.spawn()
.unwrap();
write!(register_app.stdin.as_mut().unwrap(), "{}", input_name).unwrap();
let status = register_app.wait().unwrap();
match status.signal() {
Some(signal) if signal == libc::SIGSEGV => {
println!("the process resulted in segmentation fault");
}
_ => println!("the process resulted in {:?}", status),
}
Note that you might want to use writeln!()
to write to the program's stdin, otherwise the line with the input name won't be terminated and might get merged with subsequent input.
Upvotes: 3