Skip to content

Commit bd0e776

Browse files
committed
guest/server: pass exit code back to pid1
libkrun's built-in init supports reporting the exit code back to the host from the program it launches. Let's not break the chain here! Signed-off-by: Val Packett <[email protected]>
1 parent 5b35e56 commit bd0e776

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

crates/muvm/src/guest/server.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub async fn server_main(command: PathBuf, command_args: Vec<String>) -> Result<
4343
let mut state_rx = WatchStream::new(state_rx);
4444

4545
let mut server_died = false;
46-
let mut command_exited = false;
46+
let mut command_exited_code = None;
4747

4848
loop {
4949
tokio::select! {
@@ -59,14 +59,16 @@ pub async fn server_main(command: PathBuf, command_args: Vec<String>) -> Result<
5959
server_died = true;
6060
}
6161
},
62-
res = &mut command_status, if !command_exited => {
62+
res = &mut command_status, if command_exited_code.is_none() => {
63+
command_exited_code = Some(1);
6364
match res {
6465
Ok(status) => {
6566
if !status.success() {
6667
if let Some(code) = status.code() {
6768
eprintln!(
6869
"{command:?} process exited with status code: {code}"
6970
);
71+
command_exited_code = Some(code);
7072
} else {
7173
eprintln!(
7274
"{:?} process terminated by signal: {}",
@@ -76,6 +78,8 @@ pub async fn server_main(command: PathBuf, command_args: Vec<String>) -> Result<
7678
.expect("either one of status code or signal should be set")
7779
);
7880
}
81+
} else {
82+
command_exited_code = Some(0);
7983
}
8084
},
8185
Err(err) => {
@@ -84,14 +88,13 @@ pub async fn server_main(command: PathBuf, command_args: Vec<String>) -> Result<
8488
);
8589
},
8690
}
87-
command_exited = true;
8891
},
89-
Some(state) = state_rx.next(), if command_exited => {
92+
Some(state) = state_rx.next(), if command_exited_code.is_some() => {
9093
if state.connection_idle() && state.child_processes() == 0 {
9194
// Server is idle (not currently handling an accepted
9295
// incoming connection) and no more child processes.
9396
// We're done.
94-
return Ok(());
97+
std::process::exit(command_exited_code.unwrap());
9598
}
9699
println!(
97100
"Waiting for {} other commands launched through this muvm server to exit...",

0 commit comments

Comments
 (0)