wifi: Fix extract_wifi_trace

Without passing -o, the extraction happens to a predictable file instead of
stdout. This just has it overwrite the dropped tempfile instead.
This commit is contained in:
Vicki Pfau 2025-01-21 18:55:40 -08:00
parent 6596176836
commit 7ba4075810
2 changed files with 13 additions and 48 deletions

View file

@ -7,18 +7,12 @@
use anyhow::{anyhow, Result};
use std::ffi::OsStr;
use tokio::process::ChildStdout;
#[cfg(not(test))]
use std::process::Stdio;
#[cfg(not(test))]
use tokio::process::Command;
#[cfg(test)]
use nix::{fcntl::OFlag, unistd::pipe2};
#[cfg(test)]
use std::{fs::File, io::Write};
#[cfg(not(test))]
pub async fn script_exit_code(
executable: impl AsRef<OsStr>,
@ -80,35 +74,6 @@ pub async fn script_output(
cb(executable.as_ref(), args.as_ref()).map(|(_, res)| res)
}
#[cfg(not(test))]
pub async fn script_pipe_output(
executable: impl AsRef<OsStr>,
args: &[impl AsRef<OsStr>],
) -> Result<ChildStdout> {
// Run given command and return the output given, as an fd instead of a String
let child = Command::new(executable)
.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()?;
child.stdout.ok_or(anyhow!("Failed to get stdout"))
}
#[cfg(test)]
pub async fn script_pipe_output(
executable: impl AsRef<OsStr>,
args: &[impl AsRef<OsStr>],
) -> Result<ChildStdout> {
let test = crate::testing::current();
let args: Vec<&OsStr> = args.iter().map(std::convert::AsRef::as_ref).collect();
let cb = test.process_cb.get();
let string = cb(executable.as_ref(), args.as_ref()).map(|(_, res)| res)?;
let (rx, tx) = pipe2(OFlag::O_CLOEXEC)?;
File::from(tx).write(string.as_bytes())?;
Ok(ChildStdout::from_std(std::process::ChildStdout::from(rx))?)
}
#[cfg(test)]
pub(crate) mod test {
use super::*;

View file

@ -25,7 +25,7 @@ use tracing::error;
use udev::{Event, EventType};
use zbus::Connection;
use crate::process::{run_script, script_output, script_pipe_output};
use crate::process::{run_script, script_output};
use crate::systemd::{daemon_reload, SystemdUnit};
use crate::udev::single_poll;
use crate::{path, read_config_directory};
@ -151,16 +151,13 @@ fn make_tempfile(prefix: &str) -> Result<(fs::File, PathBuf)> {
}
pub async fn extract_wifi_trace() -> Result<PathBuf> {
let (mut output, path) = make_tempfile("wifi-trace-")?;
let mut pipe = script_pipe_output("trace-cmd", &["extract"]).await?;
let mut buf = [0; 4096];
loop {
let read = pipe.read(&mut buf).await?;
if read == 0 {
break Ok(path);
}
output.write_all(&buf[..read]).await?;
}
let (_, path) = make_tempfile("wifi-trace-")?;
run_script(
"trace-cmd",
&[OsStr::new("extract"), OsStr::new("-o"), path.as_os_str()],
)
.await?;
Ok(path)
}
pub(crate) async fn set_wifi_debug_mode(
@ -568,8 +565,11 @@ mod test {
async fn trace_extract() {
let h = testing::start();
fn process_output(_: &OsStr, _: &[&OsStr]) -> Result<(i32, String)> {
Ok((0, String::from("output")))
fn process_output(_: &OsStr, args: &[&OsStr]) -> Result<(i32, String)> {
assert_eq!(args[0], OsStr::new("extract"));
assert_eq!(args[1], OsStr::new("-o"));
std::fs::write(args[2], b"output").unwrap();
Ok((0, String::new()))
}
h.test.process_cb.set(process_output);