gpt4 book ai didi

c - 通过标准输入发送 SIGINT

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:09:04 25 4
gpt4 key购买 nike

我目前正在编写一个允许用户通过 telnet 访问远程计算机的 bash shell 的应用程序。该应用程序是在远程计算机上运行的 telnet 服务器。我将 telnet 流的输出通过管道传输到我生成的 bash 进程中,如下所示:

char *execArgs[] = {"/bin/bash", "-i", 0};
execv(execArgs[0], execArgs);

我几乎可以做任何你在普通 shell 中能做的事情;键入命令、退格等。但是,我在使用 CTRL+C 终止任务时遇到问题。当我从 telnet 端收到一个 CTRL+C 序列时,它以 [0xFF][0xF8] 的形式出现。 FF 将 telnet 置于 IAC 模式(允许它解释命令),然后 F8 告诉我的程序发送一个 ETX (0x03):

char tmpchar = 3;
retval = write(outfd[1], &tmpchar, 1);
fflush(fstdin);

其中 outfd 是进入 bash 进程的流。但是,我认为 0x03 不会被解释为 SIGINT 信号,或者至少它不会终止当前正在运行的任务。为什么会这样?

最佳答案

因为你没有终端,你不能指望任何终端功能。因此,如果您将 ^C 发送到管道中,则 ^C 将从管道中出来。您可以使用 kill 向进程发送 SIGINT

但是您最好使用终端与 bash 对话!外壳期望与终端交互,而不是管道。将 man pty 打入您的系统,开始学习如何正确执行此操作。

来自 pty(7) 手册页:

A pseudoterminal (sometimes abbreviated "pty") is a pair of virtual character devices that provide a bidirectional communication channel. One end of the channel is called the master; the other end is called the slave. The slave end of the pseudoterminal provides an interface that behaves exactly like a classical terminal. A process that expects to be connected to a terminal, can open the slave end of a pseudoterminal and then be driven by a program that has opened the master end.

Anything that is written on the master end is provided to the process on the slave end as though it was input typed on a terminal. For example, writing the interrupt character (usually control-C) to the master device would cause an interrupt signal (SIGINT) to be generated for the foreground process group that is connected to the slave. Conversely, anything that is written to the slave end of the pseudoterminal can be read by the process that is connected to the master end. Pseudoterminals are used by applications such as network login services (ssh(1), rlogin(1), telnet(1)), terminal emulators, script(1), screen(1), and expect(1).

这就是你想要的。

关于c - 通过标准输入发送 SIGINT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40750790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com