通过将ctrl-c发送到stdin将SIGINT发送到进程

时间:2017-05-04 17:08:18

标签: process rust signals pty

我正在寻找一种方法来模仿终端进行一些自动化测试:即启动一个进程,然后通过向stdin发送数据和从stdout读取来与之交互。例如。向stdin发送一些输入行,包括ctrl-cctrl-\,这将导致向进程发送信号。

使用std::process::Commannd我可以将输入发送到例如cat我也在stdout上看到了它的输出,但是发送ctrl-c(作为I understand that is 3)不会导致SIGINT发送到shell。例如。该程序应终止:

use std::process::{Command, Stdio};
use std::io::Write;

fn main() {
    let mut child = Command::new("sh")
        .arg("-c").arg("-i").arg("cat")
        .stdin(Stdio::piped())
        .spawn().unwrap();
    let mut stdin = child.stdin.take().unwrap();
    stdin.write(&[3]).expect("cannot send ctrl-c");
    child.wait();
}

我怀疑问题是发送ctrl-c需要一些tty,而sh -i只需要“互动模式”。

我是否需要完全成熟并使用例如termionncurses

更新:我在原始问题中混淆了shell和终端。我现在清理了这个。我还提到ssh应该是sh

3 个答案:

答案 0 :(得分:0)

尝试添加-t选项TWICE以强制伪tty分配。即。

klar (16:14) ~>echo foo | ssh user@host.ssh.com tty
not a tty
klar (16:14) ~>echo foo | ssh -t -t user@host.ssh.com tty
/dev/pts/0

当你有一个伪tty时,我认为它应该按照你想要的那样将它转换为SIGINT。

在您的简单示例中,您也可以在写入后关闭stdin,在这种情况下服务器应该退出。对于这种特殊情况,它会更优雅,也可能更可靠。

答案 1 :(得分:0)

经过大量的研究,我发现自己做pty fork并不是太多的工作。有pty-rs,但它有错误,似乎没有维护。

以下代码需要pty module of nix尚未在crates.io上,所以Cargo.toml现在需要这个:

[dependencies]
nix = {git = "https://github.com/nix-rust/nix.git"}

以下代码在tty中运行cat,然后从中写入/读取并发送Ctrl-C(3):

extern crate nix;

use std::path::Path;
use nix::pty::{posix_openpt, grantpt, unlockpt, ptsname};
use nix::fcntl::{O_RDWR, open};
use nix::sys::stat;
use nix::unistd::{fork, ForkResult, setsid, dup2};
use nix::libc::{STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO};
use std::os::unix::io::{AsRawFd, FromRawFd};
use std::io::prelude::*;
use std::io::{BufReader, LineWriter};


fn run() -> std::io::Result<()> {
    // Open a new PTY master
    let master_fd = posix_openpt(O_RDWR)?;

    // Allow a slave to be generated for it
    grantpt(&master_fd)?;
    unlockpt(&master_fd)?;

    // Get the name of the slave
    let slave_name = ptsname(&master_fd)?;

    match fork() {
        Ok(ForkResult::Child) => {
            setsid()?; // create new session with child as session leader
            let slave_fd = open(Path::new(&slave_name), O_RDWR, stat::Mode::empty())?;

            // assign stdin, stdout, stderr to the tty, just like a terminal does
            dup2(slave_fd, STDIN_FILENO)?;
            dup2(slave_fd, STDOUT_FILENO)?;
            dup2(slave_fd, STDERR_FILENO)?;
            std::process::Command::new("cat").status()?;
        }
        Ok(ForkResult::Parent { child: _ }) => {
            let f = unsafe { std::fs::File::from_raw_fd(master_fd.as_raw_fd()) };
            let mut reader = BufReader::new(&f);
            let mut writer = LineWriter::new(&f);

            writer.write_all(b"hello world\n")?;
            let mut s = String::new();
            reader.read_line(&mut s)?; // what we just wrote in
            reader.read_line(&mut s)?; // what cat wrote out
            writer.write(&[3])?; // send ^C
            writer.flush()?;
            let mut buf = [0; 2]; // needs bytewise read as ^C has no newline
            reader.read(&mut buf)?;
            s += &String::from_utf8_lossy(&buf).to_string();
            println!("{}", s);
            println!("cat exit code: {:?}", wait::wait()?); // make sure cat really exited
        }
        Err(_) => println!("error"),
    }
    Ok(())
}

fn main() {
    run().expect("could not execute command");
}

输出:

hello world
hello world
^C
cat exit code: Signaled(2906, SIGINT, false)

答案 2 :(得分:0)

最简单的方法是直接将SIGINT信号发送到子进程。使用nixsignal::kill函数可以轻松完成此操作:

// add `nix = "0.15.0"` to your Cargo.toml
use std::process::{Command, Stdio};
use std::io::Write;

fn main() {
    // spawn child process
    let mut child = Command::new("cat")
        .stdin(Stdio::piped())
        .spawn().unwrap();

    // send "echo\n" to child's stdin
    let mut stdin = child.stdin.take().unwrap();
    writeln!(stdin, "echo");

    // sleep a bit so that child can process the input
    std::thread::sleep(std::time::Duration::from_millis(500));

    // send SIGINT to the child
    nix::sys::signal::kill(
        nix::unistd::Pid::from_raw(child.id() as i32), 
        nix::sys::signal::Signal::SIGINT
    ).expect("cannot send ctrl-c");

    // wait for child to terminate
    child.wait().unwrap();
}

您应该能够使用此方法发送各种信号。对于更高级的“交互性”(例如查询终端大小的vi之类的子程序),您需要像@hansaplast在其解决方案中那样创建伪终端。

相关问题