Skip to content

Commit

Permalink
feat: redirect std{out,err} to file
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenautumns committed Jun 17, 2024
1 parent bef69b8 commit 2b78ef7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 6 additions & 0 deletions hypervisor/src/hypervisor/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ pub struct Partition {

#[serde(default)]
pub sockets: Vec<PosixSocket>,

#[serde(default)]
pub stdout: Option<PathBuf>,

#[serde(default)]
pub stderr: Option<PathBuf>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down
30 changes: 28 additions & 2 deletions hypervisor/src/hypervisor/partition.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::fs::OpenOptions;
use std::net::{TcpStream, UdpSocket};
use std::os::unix::prelude::{AsRawFd, FromRawFd, OwnedFd, PermissionsExt, RawFd};
use std::os::unix::process::CommandExt;
Expand Down Expand Up @@ -164,6 +165,27 @@ impl Run {
keep.push(udp_io_rx.as_raw_fd());
keep.push(tcp_io_rx.as_raw_fd());

let mut stdout = None;
if let Some(path) = &base.stdout {
let out = OpenOptions::new()
.create(true)
.append(true)
.open(path)
.unwrap();
keep.push(out.as_raw_fd());
stdout = Some(out);
}
let mut stderr = None;
if let Some(path) = &base.stderr {
let err = OpenOptions::new()
.create(true)
.append(true)
.open(path)
.unwrap();
keep.push(err.as_raw_fd());
stderr = Some(err);
}

Partition::release_fds(&keep).unwrap();

let ipc_path_inner: PathBuf = PartitionConstants::IPC_SENDER[1..].into();
Expand Down Expand Up @@ -243,9 +265,9 @@ impl Run {
// Run binary
let mut command = Command::new("/bin");
let mut command = command
.stdout(Stdio::null())
.stdout(stdout.map(|s| s.into()).unwrap_or_else(Stdio::null))
.stderr(stderr.map(|s| s.into()).unwrap_or_else(Stdio::null))
.stdin(Stdio::null())
.stderr(Stdio::null())
// Set Partition Name Env
.env(
PartitionConstants::PARTITION_CONSTANTS_FD,
Expand Down Expand Up @@ -480,6 +502,8 @@ pub(crate) struct Base {
period: Duration,
working_dir: TempDir,
sockets: Vec<PosixSocket>,
stdout: Option<PathBuf>,
stderr: Option<PathBuf>,
}

impl Base {
Expand Down Expand Up @@ -559,6 +583,8 @@ impl Partition {
sampling_channel,
sockets: config.sockets,
queuing_channel,
stdout: config.stdout,
stderr: config.stderr,
};
// TODO use StartCondition::HmModuleRestart in case of a ModuleRestart!!
let run =
Expand Down

0 comments on commit 2b78ef7

Please sign in to comment.