-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30163dc
commit fd4869c
Showing
11 changed files
with
141 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ jobs: | |
- ping | ||
- dev_random | ||
- ping_queue | ||
- redirect_stdio | ||
env: | ||
DURATION: 10s | ||
RUST_LOG: trace | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
/target | ||
.direnv/ | ||
.vscode/ | ||
result | ||
result | ||
|
||
# For the redirect_stdio example | ||
stdin | ||
stdout | ||
stderr |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "redirect_stdio" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
a653rs = { workspace = true, features = ["macros"] } | ||
a653rs-linux.workspace = true | ||
log.workspace = true | ||
nix.workspace = true | ||
anyhow.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
major_frame: 1s | ||
partitions: | ||
- id: 0 | ||
name: partition_0 | ||
duration: 1s | ||
offset: 0ms | ||
period: 1s | ||
image: ./target/x86_64-unknown-linux-musl/release/redirect_stdio | ||
mounts: | ||
- [ ./stdin, /stdin ] | ||
- [ ./stdout, /stdout ] | ||
- [ ./stderr, /stderr ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use std::fs::OpenOptions; | ||
use std::os::fd::AsRawFd; | ||
use std::path::Path; | ||
|
||
use a653rs::partition; | ||
use a653rs::prelude::PartitionExt; | ||
use a653rs_linux::partition::ApexLogger; | ||
use anyhow::Result; | ||
use log::LevelFilter; | ||
|
||
fn replace_stdio<T: AsRawFd, U: AsRef<Path>>(stdio: T, new: U, write: bool) -> Result<()> { | ||
let new = OpenOptions::new() | ||
.write(write) | ||
.read(!write) | ||
.truncate(write) | ||
.open(new)?; | ||
nix::unistd::dup2(new.as_raw_fd(), stdio.as_raw_fd())?; | ||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
replace_stdio(std::io::stdin(), "/stdin", false).unwrap(); | ||
replace_stdio(std::io::stdout(), "/stdout", true).unwrap(); | ||
replace_stdio(std::io::stderr(), "/stderr", true).unwrap(); | ||
|
||
ApexLogger::install_panic_hook(); | ||
ApexLogger::install_logger(LevelFilter::Trace).unwrap(); | ||
|
||
redirect_stdio::Partition.run() | ||
} | ||
|
||
#[partition(a653rs_linux::partition::ApexLinuxPartition)] | ||
mod redirect_stdio { | ||
use log::info; | ||
use std::io::BufRead; | ||
|
||
#[start(cold)] | ||
fn cold_start(mut ctx: start::Context) { | ||
// create and start an aperiodic process | ||
ctx.create_process_0().unwrap().start().unwrap(); | ||
} | ||
|
||
// do the same as a cold_start | ||
#[start(warm)] | ||
fn warm_start(ctx: start::Context) { | ||
cold_start(ctx); | ||
} | ||
|
||
// this aperiodic process opens /dev/random and reads some random bytes from it | ||
#[aperiodic( | ||
time_capacity = "Infinite", | ||
stack_size = "8KB", | ||
base_priority = 1, | ||
deadline = "Soft" | ||
)] | ||
fn process_0(ctx: process_0::Context) { | ||
info!("started process with redirected stdio/stdout/stderr"); | ||
|
||
info!("Reading stdin to stdout"); | ||
println!("Start reading stdin to stdout"); | ||
let stdin = std::io::stdin(); | ||
for line in stdin.lock().lines() { | ||
println!("{}", line.unwrap()) | ||
} | ||
println!("Finished reading stdin to stdout"); | ||
|
||
info!("Writing messages to stderr"); | ||
eprintln!("Error was encountered: None"); | ||
eprintln!("But it was printed to stderr"); | ||
|
||
info!("Terminating partition"); | ||
ctx.set_partition_mode(OperatingMode::Idle).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters