use super::super::{Handover, SheepFd};
use super::adopt_descriptors::{
adopt_channel, adopt_dog_link, adopt_fd, adopt_listener, adopt_log, adopt_pipe, adopt_pty,
adopt_stdin,
};
use crate::sys;
use std::io;
use std::os::fd::RawFd;
pub(super) fn refuse_repeated_fds(blob: &Handover) -> io::Result<()> {
let mut seen = std::collections::BTreeSet::new();
for fd in blob.carried_fds() {
if !seen.insert(fd) {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!(
"the handover blob names descriptor {fd} more than once, so adopting it \
would build two owners of one number"
),
));
}
}
Ok(())
}
#[track_caller]
pub fn dry_run(blob: &Handover) -> io::Result<()> {
let value = serde_json::to_value(blob).map_err(io::Error::other)?;
let blob = Handover::load_value(value).map_err(|error| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("a successor could not have read this blob back: {error}"),
)
})?;
refuse_repeated_fds(&blob)?;
rehearse(blob.listener_fd, "the control listener", adopt_listener)?;
for carried in &blob.sheep {
let name = &carried.name;
for (fd, slot) in carried.fds.all_kinded() {
let Some(fd) = fd else { continue };
rehearse(
fd,
&format!("sheep '{name}' {}", slot.describe()),
|dup| match slot {
SheepFd::OutPipe => adopt_pipe(Some(dup), name, "stdout").map(drop),
SheepFd::ErrPipe => adopt_pipe(Some(dup), name, "stderr").map(drop),
SheepFd::OutLog => adopt_log(Some(dup), name, "stdout").map(drop),
SheepFd::ErrLog => adopt_log(Some(dup), name, "stderr").map(drop),
SheepFd::Stdin => adopt_stdin(Some(dup), name).map(drop),
SheepFd::Channel => adopt_channel(Some(dup), name).map(drop),
SheepFd::DogLink => adopt_dog_link(Some(dup), name).map(drop),
SheepFd::Pty => adopt_pty(Some(dup), name).map(drop),
},
)?;
}
}
rehearse(blob.pidfile_fd, "the pidfile lock", |dup| {
adopt_fd(dup, "the pidfile lock").map(drop)
})?;
Ok(())
}
fn rehearse<T>(
fd: RawFd,
what: &str,
adopt_one: impl FnOnce(RawFd) -> io::Result<T>,
) -> io::Result<()> {
sys::adoptable_fd(fd)
.map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, format!("{what}: {error}")))?;
let duplicate = sys::duplicate_fd(fd)?;
adopt_one(duplicate).map(drop)
}
pub fn discard_blob(path: &std::path::Path) {
if let Err(error) = std::fs::remove_file(path) {
tracing::warn!(
path = %path.display(),
%error,
"the handover blob could not be removed after it was adopted"
);
}
}
#[cfg(test)]
mod tests {
use super::super::adopt_descriptors::adopt;
use std::os::fd::RawFd;
use super::super::super::{CarriedFds, SheepFd};
use std::os::fd::IntoRawFd as _;
use crate::handover::VERSION;
use tokio::io::AsyncWriteExt as _;
use super::super::testing::*;
use super::*;
#[tokio::test]
async fn two_instances_sharing_one_log_file_are_both_adopted() {
let dir = tempfile::tempdir().unwrap();
let socket = dir.path().join("shep.sock");
let merged = dir.path().join("web-out.log");
let zero = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&merged)
.unwrap();
let one = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&merged)
.unwrap();
let (zero_fd, one_fd) = (zero.into_raw_fd(), one.into_raw_fd());
assert_ne!(
zero_fd, one_fd,
"two `open`s on one path must yield two numbers, or the premise \
of this whole case is wrong"
);
let blob = blob_with(
&socket,
vec![
carried_slot(
0,
CarriedFds {
out_pipe: None,
err_pipe: None,
out_log: Some(zero_fd),
err_log: None,
stdin: None,
channel: None,
dog_link: None,
pty: None,
},
),
carried_slot(
1,
CarriedFds {
out_pipe: None,
err_pipe: None,
out_log: Some(one_fd),
err_log: None,
stdin: None,
channel: None,
dog_link: None,
pty: None,
},
),
],
);
let mut adopted = adopt(&blob).expect("a merged-log clustered app must be adoptable");
assert_eq!(adopted.sheep.len(), 2, "one adopted sheep per instance");
let zero = adopted.sheep[0].out_log.take().expect("slot 0's log");
let one = adopted.sheep[1].out_log.take().expect("slot 1's log");
let mut handles = [zero, one];
for (line, slot) in [
("zero-1\n", 0),
("one-1\n", 1),
("zero-2\n", 0),
("one-2\n", 1),
] {
let handle = &mut handles[slot];
handle.write_all(line.as_bytes()).await.unwrap();
handle.flush().await.unwrap();
}
assert_eq!(
std::fs::read_to_string(&merged).unwrap(),
"zero-1\none-1\nzero-2\none-2\n",
"both instances append into the one file, in the order written"
);
}
#[tokio::test]
async fn a_blob_a_successor_could_adopt_passes_the_rehearsal() {
let predecessor = Predecessor::new();
dry_run(&predecessor.blob()).expect("every descriptor here is the kind its slot wants");
}
#[tokio::test]
async fn a_blob_carrying_a_swap_in_flight_passes_the_rehearsal() {
use crate::entry::ReloadState;
use crate::supervisor::{CarriedReload, ReloadMode, ReloadPhase, ReloadSwap};
let predecessor = Predecessor::new();
let mut blob = predecessor.blob();
blob.sheep[0].reload = Some(ReloadState::Drainee { new_id: Some(9) });
blob.reloads = Some(vec![CarriedReload {
app: "web".to_owned(),
queue: vec![4, 5],
mode: ReloadMode::Overlap,
swap: ReloadSwap {
old_id: 1,
new_id: Some(9),
phase: ReloadPhase::DrainOld,
},
}]);
dry_run(&blob).expect("a flock mid-reload is one a successor can adopt");
}
#[tokio::test]
async fn a_blob_carrying_a_failed_readiness_verdict_passes_the_rehearsal() {
let predecessor = Predecessor::new();
let mut blob = predecessor.blob();
blob.sheep[0].ready_failed = Some(true);
dry_run(&blob).expect("an instance a reload gave up on is one a successor can adopt");
}
#[tokio::test]
async fn a_rehearsal_leaves_every_descriptor_it_checked_working() {
use std::io::{Read as _, Write as _};
let mut predecessor = Predecessor::new();
predecessor.out_write.write_all(b"a bleat").unwrap();
let socket = predecessor.socket();
let connecting = tokio::task::spawn_blocking(move || {
std::os::unix::net::UnixStream::connect(&socket).unwrap()
});
let client = connecting.await.unwrap();
dry_run(&predecessor.blob()).expect("the fixture is adoptable");
predecessor
.listener
.accept()
.expect("the checked listener still accepts");
drop(client);
let mut buf = [0_u8; 7];
predecessor
.out_read
.read_exact(&mut buf)
.expect("the checked read end still reads");
assert_eq!(&buf, b"a bleat");
predecessor.stdin_write.write_all(b"whisper").unwrap();
let mut buf = [0_u8; 7];
predecessor
.stdin_read
.read_exact(&mut buf)
.expect("the checked write end still writes");
assert_eq!(&buf, b"whisper");
predecessor.channel.write_all(b"ping").unwrap();
let mut buf = [0_u8; 4];
predecessor
.child_channel
.read_exact(&mut buf)
.expect("the checked channel still reaches the child");
assert_eq!(&buf, b"ping");
}
#[tokio::test]
async fn a_rehearsal_leaks_no_descriptors() {
let predecessor = Predecessor::new();
let blob = predecessor.blob();
let before = open_fd_count();
for _ in 0..100 {
dry_run(&blob).expect("the fixture is adoptable");
}
let after = open_fd_count();
assert!(
after < before + 100,
"a hundred rehearsals of a blob naming six descriptors must not grow this \
process's descriptor table: {before} -> {after}"
);
}
#[tokio::test]
async fn a_descriptor_that_is_open_but_not_a_pipe_is_refused_before_the_exec() {
use std::os::fd::AsRawFd as _;
let predecessor = Predecessor::new();
let not_a_pipe = std::fs::File::open("/dev/null").unwrap();
let mut blob = predecessor.blob();
blob.sheep[0].fds.out_pipe = Some(not_a_pipe.as_raw_fd());
crate::sys::adoptable_fd(not_a_pipe.as_raw_fd())
.expect("the number must be open, or this proves nothing");
crate::sys::OpenFd::new(not_a_pipe.as_raw_fd())
.expect("carrying it must not refuse it either");
let err = dry_run(&blob).expect_err("/dev/null is not a readable pipe");
assert!(
err.to_string().contains("web") && err.to_string().contains("stdout"),
"the refusal must name the sheep and the stream: {err}"
);
}
#[tokio::test]
async fn the_rehearsal_and_the_adoption_agree_on_every_slot() {
use std::os::fd::AsRawFd as _;
for slot in [
SheepFd::OutPipe,
SheepFd::ErrPipe,
SheepFd::OutLog,
SheepFd::ErrLog,
SheepFd::Stdin,
SheepFd::Channel,
SheepFd::DogLink,
SheepFd::Pty,
] {
let predecessor = Predecessor::new();
let wrong = std::fs::File::open("/dev/null").unwrap();
let wrong = Some(wrong.as_raw_fd());
let mut blob = predecessor.blob();
let fds = &mut blob.sheep[0].fds;
match slot {
SheepFd::OutPipe => fds.out_pipe = wrong,
SheepFd::ErrPipe => fds.err_pipe = wrong,
SheepFd::OutLog => fds.out_log = wrong,
SheepFd::ErrLog => fds.err_log = wrong,
SheepFd::Stdin => fds.stdin = wrong,
SheepFd::Channel => fds.channel = wrong,
SheepFd::DogLink => fds.dog_link = wrong,
SheepFd::Pty => fds.pty = wrong,
}
assert_eq!(
dry_run(&blob).is_err(),
adopt_a_copy(&blob).is_err(),
"the rehearsal and the adoption disagree about {slot:?}, so one of them is \
checking something the other is not"
);
}
}
#[tokio::test]
async fn a_blob_naming_one_descriptor_twice_is_refused_before_the_exec() {
let predecessor = Predecessor::new();
let mut blob = predecessor.blob();
blob.sheep[0].fds.err_log = Some(blob.pidfile_fd);
let err = dry_run(&blob).expect_err("one number cannot have two owners");
assert!(
err.to_string().contains("more than once"),
"the refusal must be `refuse_repeated_fds`'s own: {err}"
);
}
#[tokio::test]
async fn a_reserved_or_closed_number_is_refused_before_the_exec() {
let predecessor = Predecessor::new();
let mut reserved = predecessor.blob();
reserved.sheep[0].fds.out_log = Some(0);
let err = dry_run(&reserved).expect_err("stdio is owned elsewhere");
assert!(
err.to_string().contains("reserved for stdio"),
"the refusal must be the successor's own wording: {err}"
);
let mut gone = predecessor.blob();
gone.sheep[0].fds.err_log = Some(RawFd::MAX);
let err = dry_run(&gone).expect_err("a number this high names nothing");
assert!(
err.to_string().contains("not an open descriptor"),
"the refusal must be the successor's own wording: {err}"
);
}
#[tokio::test]
async fn a_blob_a_successor_could_not_read_back_is_refused_before_the_exec() {
let predecessor = Predecessor::new();
let mut blob = predecessor.blob();
blob.version = VERSION + 1;
let err = dry_run(&blob).expect_err("a version this image cannot read");
assert!(
err.to_string()
.contains("could not have read this blob back"),
"the refusal must say the parse failed, not the descriptors: {err}"
);
}
}