use core::convert::Infallible;
use std::ffi::CString;
use std::fs;
use std::io;
use std::os::fd::RawFd;
use std::os::unix::ffi::{OsStrExt, OsStringExt};
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use shep_core::paths::ShepPaths;
use super::blob::Handover;
use super::fds;
static LAUNCH_PATH: OnceLock<Option<PathBuf>> = OnceLock::new();
pub fn record_launch_path() {
let _ = LAUNCH_PATH.set(launch_path_from_argv());
}
pub fn exec_target() -> io::Result<PathBuf> {
let recorded = LAUNCH_PATH.get().cloned().flatten();
let current = std::env::current_exe();
resolve_target(
[recorded, current.as_deref().ok().map(Path::to_path_buf)],
current.as_ref().err(),
)
}
pub(crate) fn resolve_target(
candidates: [Option<PathBuf>; 2],
current_exe_error: Option<&io::Error>,
) -> io::Result<PathBuf> {
let mut refusals = Vec::new();
for candidate in candidates {
let Some(candidate) = candidate else { continue };
match check_target(&candidate) {
Ok(()) => return Ok(candidate),
Err(problem) => refusals.push(format!("{} ({problem})", candidate.display())),
}
}
if let Some(e) = current_exe_error {
refusals.push(format!("this process's own image ({e})"));
}
if refusals.is_empty() {
refusals.push("no candidate at all".to_owned());
}
Err(io::Error::new(
io::ErrorKind::NotFound,
format!("no binary to exec: {}", refusals.join("; ")),
))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TargetProblem {
DeletedInode,
Missing,
NotAFile,
}
impl core::fmt::Display for TargetProblem {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let text = match self {
Self::DeletedInode => "names a deleted inode, not a file",
Self::Missing => "is not on disk",
Self::NotAFile => "is not a file",
};
f.write_str(text)
}
}
fn check_target(candidate: &Path) -> Result<(), TargetProblem> {
if candidate.to_string_lossy().contains(" (deleted)") {
return Err(TargetProblem::DeletedInode);
}
match std::fs::metadata(candidate) {
Ok(meta) if meta.is_file() => Ok(()),
Ok(_) => Err(TargetProblem::NotAFile),
Err(_) => Err(TargetProblem::Missing),
}
}
fn launch_path_from_argv() -> Option<PathBuf> {
let argv0 = PathBuf::from(std::env::args_os().next()?);
if argv0.as_os_str().is_empty() {
return None;
}
if argv0.is_absolute() {
return Some(argv0);
}
let has_separator = argv0
.parent()
.is_some_and(|dir| !dir.as_os_str().is_empty());
has_separator.then(|| std::env::current_dir().ok().map(|cwd| cwd.join(&argv0)))?
}
pub const HANDOVER_ENV: &str = "SHEP_HANDOVER";
pub fn hand_over(blob: &Handover, paths: &ShepPaths) -> io::Result<Infallible> {
exec_into(&exec_target()?, blob, paths)
}
fn exec_into(target: &Path, blob: &Handover, paths: &ShepPaths) -> io::Result<Infallible> {
let written = blob.write(paths)?;
let failure = match exec_with_blob(target, blob, &written) {
Ok(never) => match never {},
Err(err) => err,
};
match fs::remove_file(&written) {
Ok(()) | Err(_) => Err(failure),
}
}
fn exec_with_blob(target: &Path, blob: &Handover, written: &Path) -> io::Result<Infallible> {
let mut cleared = Vec::new();
let failure = match keep_and_exec(target, blob, written, &mut cleared) {
Ok(never) => match never {},
Err(err) => err,
};
for fd in cleared {
let _ = fds::close_raw_after_exec(fd);
}
Err(failure)
}
fn keep_and_exec(
target: &Path,
blob: &Handover,
written: &Path,
cleared: &mut Vec<RawFd>,
) -> io::Result<Infallible> {
for fd in blob.named_fds() {
fds::keep_raw_across_exec(fd)?;
cleared.push(fd);
}
let path = c_string(target.as_os_str().as_bytes())?;
let argv = std::env::args_os()
.map(|arg| c_string(arg.as_bytes()))
.collect::<io::Result<Vec<_>>>()?;
let env = successor_env(written)?;
nix::unistd::execve(&path, &argv, &env).map_err(io::Error::from)
}
fn successor_env(written: &Path) -> io::Result<Vec<CString>> {
let mut env = std::env::vars_os()
.filter(|(name, _)| name != HANDOVER_ENV)
.map(|(name, value)| {
let mut entry = name.into_vec();
entry.push(b'=');
entry.extend(value.into_vec());
c_string(&entry)
})
.collect::<io::Result<Vec<_>>>()?;
let mut marker = HANDOVER_ENV.as_bytes().to_vec();
marker.push(b'=');
marker.extend(written.as_os_str().as_bytes());
env.push(c_string(&marker)?);
Ok(env)
}
fn c_string(bytes: &[u8]) -> io::Result<CString> {
CString::new(bytes).map_err(io::Error::other)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::entry::ProcessEntry;
use crate::handover::fixtures::entry_fixture;
use crate::handover::{CarriedFds, CarriedSheep, VERSION};
#[test]
fn the_exec_target_exists_and_is_a_file() {
let p = exec_target().unwrap();
assert!(p.is_file(), "{}", p.display());
}
#[cfg(target_os = "linux")]
#[test]
fn a_deleted_inode_path_is_never_returned() {
let p = exec_target().unwrap();
assert!(
!p.to_string_lossy().contains("(deleted)"),
"exec target resolved to a deleted inode: {}",
p.display()
);
}
#[test]
fn resolve_target_refuses_a_synthetic_deleted_inode_candidate() {
let deleted = PathBuf::from("/opt/shep/shep (deleted)");
let err = resolve_target([None, Some(deleted)], None).unwrap_err();
assert_eq!(
err.to_string(),
"no binary to exec: /opt/shep/shep (deleted) (names a deleted inode, not a file)"
);
}
#[test]
fn a_deleted_inode_candidate_is_refused_on_every_platform() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("shep (deleted)");
std::fs::write(&path, "an exec target that really is on disk").unwrap();
assert_eq!(
check_target(&path),
Err(TargetProblem::DeletedInode),
"existing on disk must not excuse the suffix"
);
}
#[test]
fn a_candidate_that_is_not_on_disk_is_refused() {
let dir = tempfile::tempdir().unwrap();
assert_eq!(
check_target(&dir.path().join("never-written")),
Err(TargetProblem::Missing)
);
}
#[test]
fn a_directory_is_refused() {
let dir = tempfile::tempdir().unwrap();
assert_eq!(check_target(dir.path()), Err(TargetProblem::NotAFile));
}
#[test]
fn a_real_binary_passes_the_check() {
assert_eq!(check_target(&std::env::current_exe().unwrap()), Ok(()));
}
#[test]
fn argv0_resolves_against_the_startup_directory() {
let p = launch_path_from_argv().expect("argv[0] names a path");
assert!(p.is_file(), "{}", p.display());
}
const SELFTEST_HOME: &str = "SHEP_HANDOVER_SELFTEST";
const SELFTEST_NAME: &str =
"handover::exec::tests::an_exec_replaces_the_image_and_keeps_a_descriptor";
fn handover_with_fds(
entry: &ProcessEntry,
listener_fd: RawFd,
pidfile_fd: RawFd,
fds: CarriedFds,
) -> Handover {
Handover {
version: VERSION,
sheep: vec![CarriedSheep::from_entry(
entry, 7, fds, false, None, false, None,
)],
listener_fd,
pidfile_fd,
next_id: 9,
next_deadline: 5,
next_action_stamp: 2,
reloads: Some(Vec::new()),
}
}
fn selftest_paths(home: &Path) -> ShepPaths {
let home = home.display().to_string();
let paths = ShepPaths::resolve(
&|key| (key == "SHEP_HOME").then(|| home.clone()),
Path::new("/nonexistent"),
);
std::fs::create_dir_all(&paths.run).unwrap();
paths
}
#[test]
fn an_exec_replaces_the_image_and_keeps_a_descriptor() {
if let Some(blob) = std::env::var_os(HANDOVER_ENV) {
successor_stage(Path::new(&blob));
}
if let Some(home) = std::env::var_os(SELFTEST_HOME) {
exec_stage(Path::new(&home));
}
let dir = tempfile::tempdir().unwrap();
let out = std::process::Command::new(std::env::current_exe().unwrap())
.arg(SELFTEST_NAME)
.arg("--exact")
.arg("--nocapture")
.env(SELFTEST_HOME, dir.path())
.env_remove(HANDOVER_ENV)
.output()
.unwrap();
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
out.status.success(),
"{stdout}{}",
String::from_utf8_lossy(&out.stderr)
);
assert!(
stdout.contains("running 1 test"),
"the child ran no test, so `{SELFTEST_NAME}` is not where this test lives any more; `--exact` needs the full path and nothing updates it automatically: {stdout}"
);
assert!(stdout.contains("adopted: hello"), "{stdout}");
}
fn exec_stage(home: &Path) -> ! {
use std::io::Write as _;
use std::os::fd::AsRawFd as _;
let paths = selftest_paths(home);
let (reader, mut writer) = std::io::pipe().unwrap();
writer.write_all(b"hello").unwrap();
drop(writer);
let listener = tempfile::tempfile().unwrap();
let pidfile = tempfile::tempfile().unwrap();
let blob = handover_with_fds(
&entry_fixture(|_| {}),
listener.as_raw_fd(),
pidfile.as_raw_fd(),
CarriedFds {
out_pipe: Some(reader.as_raw_fd()),
err_pipe: None,
out_log: None,
err_log: None,
stdin: None,
channel: None,
},
);
let err = hand_over(&blob, &paths).unwrap_err();
panic!("the exec should not have returned: {err}");
}
fn successor_stage(blob_path: &Path) -> ! {
let blob = Handover::read(blob_path).expect("the successor's blob");
let fd = blob.sheep[0].fds.out_pipe.expect("a carried stdout pipe");
let mut buf = [0_u8; 16];
let read = nix::unistd::read(fd, &mut buf).expect("the carried descriptor is open");
println!("adopted: {}", String::from_utf8_lossy(&buf[..read]));
std::process::exit(0);
}
#[test]
fn a_failed_exec_leaves_no_blob_behind() {
use std::os::fd::AsRawFd as _;
let dir = tempfile::tempdir().unwrap();
let paths = selftest_paths(dir.path());
let target = dir.path().join("not-a-binary");
std::fs::write(&target, "this will never execute").unwrap();
let listener = tempfile::tempfile().unwrap();
let pidfile = tempfile::tempfile().unwrap();
let blob = handover_with_fds(
&entry_fixture(|_| {}),
listener.as_raw_fd(),
pidfile.as_raw_fd(),
CarriedFds {
out_pipe: None,
err_pipe: None,
out_log: None,
err_log: None,
stdin: None,
channel: None,
},
);
let err = exec_into(&target, &blob, &paths).unwrap_err();
assert!(
!Handover::path(&paths).exists(),
"a failed exec left a blob behind: {err}"
);
}
#[test]
fn a_failed_exec_makes_every_descriptor_close_on_exec_again() {
use std::os::fd::{AsFd as _, AsRawFd as _};
let dir = tempfile::tempdir().unwrap();
let paths = selftest_paths(dir.path());
let target = dir.path().join("not-a-binary");
std::fs::write(&target, "this will never execute").unwrap();
let listener = tempfile::tempfile().unwrap();
let pidfile = tempfile::tempfile().unwrap();
let out_log = tempfile::tempfile().unwrap();
let (_child_end, stdin) = std::io::pipe().unwrap();
let (channel, _child_channel) = std::os::unix::net::UnixStream::pair().unwrap();
let blob = handover_with_fds(
&entry_fixture(|_| {}),
listener.as_raw_fd(),
pidfile.as_raw_fd(),
CarriedFds {
out_pipe: None,
err_pipe: None,
out_log: Some(out_log.as_raw_fd()),
err_log: None,
stdin: Some(stdin.as_raw_fd()),
channel: Some(channel.as_raw_fd()),
},
);
for fd in [
listener.as_fd(),
pidfile.as_fd(),
out_log.as_fd(),
stdin.as_fd(),
channel.as_fd(),
] {
assert!(
!fds::is_kept(fd).unwrap(),
"the daemon opens everything close-on-exec, so this starts set"
);
}
let mut cleared = Vec::new();
let written = Handover::path(&paths);
let _ = keep_and_exec(&target, &blob, &written, &mut cleared);
cleared.sort_unstable();
let mut expected = vec![
listener.as_raw_fd(),
pidfile.as_raw_fd(),
out_log.as_raw_fd(),
stdin.as_raw_fd(),
channel.as_raw_fd(),
];
expected.sort_unstable();
assert_eq!(
cleared, expected,
"every named descriptor must be cleared, from both of the two \
places `named_fds` draws them"
);
let err = exec_into(&target, &blob, &paths).unwrap_err();
for fd in [
listener.as_fd(),
pidfile.as_fd(),
out_log.as_fd(),
stdin.as_fd(),
channel.as_fd(),
] {
assert!(
!fds::is_kept(fd).unwrap(),
"a failed exec left a descriptor exec-inheritable: {err}"
);
}
}
}