use anyhow::{Context, bail};
use nix::cmsg_space;
use nix::unistd::Pid;
use std::os::fd::RawFd;
use crate::procfs::ProcStatus;
use crate::ipc;
use crate::pty;
use crate::result::Result;
pub(crate) fn run(
child_pid: Pid,
_process_status: &ProcStatus,
socket: &ipc::Socket,
) -> Result<std::convert::Infallible> {
let mut cmsgspace = cmsg_space!([RawFd; 1]);
let (msg_buf, mut fds) = socket
.receive::<std::fs::File>(1, &mut cmsgspace)
.context("failed to receive ready signal from child")?;
if msg_buf.is_empty() || msg_buf[0] != b'R' {
bail!("child did not send ready signal");
}
if fds.is_empty() {
bail!("expected PTY fd from child, got none");
}
let pty_fd = fds.remove(0);
pty::forward_pty_and_wait(&pty_fd, child_pid)
}