summaryrefslogtreecommitdiffstats
path: root/src/pipe.rs
blob: eafc4d8535153b7977c0cad13772ff0bac49b47c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std::io::Read as _;
use std::os::unix::io::FromRawFd as _;
use std::os::unix::process::CommandExt as _;

const PID0: nix::unistd::Pid = nix::unistd::Pid::from_raw(0);

pub fn run() -> anyhow::Result<i32> {
    let pipeline = read_pipeline()?;
    let mut cmds: Vec<_> = pipeline
        .exes()
        .iter()
        .map(|exe| {
            let mut cmd = std::process::Command::new(exe.exe());
            cmd.args(exe.args());
            cmd
        })
        .collect();
    for i in 0..(cmds.len() - 1) {
        let (r, w) = pipe()?;
        cmds[i].stdout(w);
        cmds[i + 1].stdin(r);
    }

    let mut children = vec![];

    unsafe {
        cmds[0].pre_exec(|| {
            setpgid_child(PID0)?;
            Ok(())
        });
    }
    let leader = cmds[0].spawn()?;
    let pg_pid = id_to_pid(leader.id());
    setpgid_parent(pg_pid, PID0)?;
    set_foreground_pg(pg_pid)?;
    children.push(leader);

    for cmd in &mut cmds[1..] {
        unsafe {
            cmd.pre_exec(move || {
                setpgid_child(pg_pid)?;
                Ok(())
            });
        }
        let child = cmd.spawn()?;
        let child_pid = id_to_pid(child.id());
        children.push(child);
        setpgid_parent(child_pid, pg_pid)?;
    }
    drop(cmds);

    let last_pid = id_to_pid(children[children.len() - 1].id());
    let mut children: std::collections::HashMap<
        nix::unistd::Pid,
        std::process::Child,
    > = children
        .into_iter()
        .map(|child| (id_to_pid(child.id()), child))
        .collect();
    let mut final_code = None;
    let mut final_signal = None;
    while !children.is_empty() {
        match nix::sys::wait::waitpid(neg_pid(pg_pid), None)? {
            nix::sys::wait::WaitStatus::Exited(pid, code) => {
                if pid == last_pid {
                    final_code = Some(code);
                }
                children.remove(&pid);
            }
            nix::sys::wait::WaitStatus::Signaled(pid, signal, _) => {
                if signal == nix::sys::signal::Signal::SIGINT {
                    nix::sys::signal::raise(nix::sys::signal::SIGINT)?;
                }
                if pid == last_pid {
                    final_signal = Some(signal);
                }
                children.remove(&pid);
            }
            _ => {}
        }
    }
    if let Some(signal) = final_signal {
        nix::sys::signal::raise(signal)?;
    }
    Ok(final_code.unwrap())
}

fn read_pipeline() -> anyhow::Result<crate::parse::Pipeline> {
    // Safety: this code is only called by crate::history::run_pipeline, which
    // passes data through on fd 3, and which will not spawn this process
    // unless the pipe was successfully opened on that fd
    let mut fd3 = unsafe { std::fs::File::from_raw_fd(3) };
    let mut pipeline = String::new();
    fd3.read_to_string(&mut pipeline)?;
    let ast = crate::parse::Pipeline::parse(&pipeline)?;
    Ok(ast)
}

fn pipe() -> anyhow::Result<(std::fs::File, std::fs::File)> {
    let (r, w) = nix::unistd::pipe2(nix::fcntl::OFlag::O_CLOEXEC)?;
    // Safety: these file descriptors were just returned by pipe2 above, which
    // means they must be valid otherwise that call would have returned an
    // error
    Ok((unsafe { std::fs::File::from_raw_fd(r) }, unsafe {
        std::fs::File::from_raw_fd(w)
    }))
}

fn set_foreground_pg(pg: nix::unistd::Pid) -> anyhow::Result<()> {
    let pty = nix::fcntl::open(
        "/dev/tty",
        nix::fcntl::OFlag::empty(),
        nix::sys::stat::Mode::empty(),
    )?;
    nix::unistd::tcsetpgrp(pty, pg)?;
    nix::unistd::close(pty)?;
    nix::sys::signal::kill(neg_pid(pg), nix::sys::signal::Signal::SIGCONT)?;
    Ok(())
}

fn setpgid_child(pg: nix::unistd::Pid) -> std::io::Result<()> {
    nix::unistd::setpgid(id_to_pid(0), pg)?;
    Ok(())
}

fn setpgid_parent(
    pid: nix::unistd::Pid,
    pg: nix::unistd::Pid,
) -> anyhow::Result<()> {
    nix::unistd::setpgid(pid, pg).or_else(|e| {
        if e == nix::errno::Errno::EACCES {
            Ok(())
        } else {
            Err(e)
        }
    })?;
    Ok(())
}

fn id_to_pid(id: u32) -> nix::unistd::Pid {
    nix::unistd::Pid::from_raw(id.try_into().unwrap())
}

fn neg_pid(pid: nix::unistd::Pid) -> nix::unistd::Pid {
    nix::unistd::Pid::from_raw(-pid.as_raw())
}