首页 > 解决方案 > pty主端的奇怪输出

问题描述

我正在开发一个远程 shell 程序,并且我有一个使用forkpty(). 从跟随端写入领导端按预期工作,但是当我开始尝试在跟随端读取时,领导端出现奇怪的输出。

我明白了:

$
socket->pty: interesting
pty->socket: ^@^@^@^@^@^@^@^@^@^@▒▒ɖ^BE<^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^X▒߰^@^@^@^@^@^@^@^@^@^@^@^@^@^@ ^@^@^@^@^@^@^@^@^@^@^@^@^C^@^@▒^E^@^@▒     ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒^@^@▒   ^@^@^@^@^@^@^@^@^@^@^@^A^@^@@^@^@^@^@^@^@^@@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@@^@@^@^@^@^@^@^K^@^@^@^@^@^@^@▒▒▒^@^@^@^@^@▒^@^@^@^@^@^@^@▒▒԰^@^@=L@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^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^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@^@^@^@^@^@^@^@^@^@^A^@^@@^@^@^@^@^@^@^@@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@@^@@^@^@^@^@^@^K^@^@^@^@^@^@^@▒▒▒^@^@^@^@^@▒^@^@^@^@^@^@^@▒▒԰^@^@=L@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@

我认为它可能与调试输出有关,所以我禁用了它,但是:

pty->socket: s^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A▒x▒^@^@@`P▒^@^@^A▒▒▒▒▒▒▒^H▒x▒^@^@▒▒x▒^@^@^@^@^@^@^@^@^@^@▒     ^_▒^AM<^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^X"W▒^@^@^@^@^@^@^@^@^@^@^@^@^@^@ ^@^@^@^@^@^@^C^@^@▒^E^@^@▒        ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@^@^@^@^@^@^@^@^@^@^A^@^@@^@^@^@^@^@^@^@@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@@^@@^@^@^@^@^@^K^@^@^@^@^@^@^@▒▒▒^@^@^@^@^@▒^@^@^@^@^@^@^@▒▒C▒^@^@▒L@^@^@^@^@^@^@^@^@
socket->pty: ?
pty->socket: s^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A▒x▒^@^@@`P▒^@^@^A▒▒▒▒▒▒▒^H▒x▒^@^@▒▒x▒^@^@^@^@^@^@^@^@^@^@▒     ^_▒^AM<^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^X"W▒^@^@^@^@^@^@^@^@^@^@^@^@^@^@ ^@^@^@^@^@^@^@^C^@^@▒^E^@^@▒      ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@^@^@^@^@^@^@^@^@^@^A^@^@@^@^@^@^@^@^@^@@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@@^@@^@^@^@^@^@^K^@^@^@^@^@^@^@▒▒▒^@^@^@^@^@▒^@^@^@^@^@^@^@▒▒C▒^@^@▒L@^@^@^@^@^@^@^@^@
socket->pty: what
pty->socket: s^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A▒x▒^@^@@`P▒^@^@^A▒▒▒▒▒▒▒^H▒x▒^@^@▒▒x▒^@^@^@^@^@^@^@^@^@^@▒     ^_▒^AM<^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^X"W▒^@^@^@^@^@^@^@^@^@^@^@^@^@^@ ^@^@^@^@^@^@^@^C^@^@▒^E^@^@▒      ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@^@^@^@^@^@^@^@^@^@^A^@^@@^@^@^@^@^@^@^@@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@@^@@^@^@^@^@^@^K^@^@^@^@^@^@^@▒▒▒^@^@^@^@^@▒^@^@^@^@^@^@^@▒▒C▒^@^@▒L@^@^@^@^@^@^@^@^@

它似乎在重复,但我无法弄清楚它是什么。

追随者代码

fprintf(stdout, "$");
fgets(input_buff, buff_len, stdin);

领导者代码

chars_read = read(leader_fd, &buff, buff_len - 1);
if (chars_read > 0) {
    buff[chars_read] = '\0';
    printf("pty->socket: %s\n", buff);
    write(socketfd, buff, chars_read);
}

chars_read = read(socketfd, &buff, buff_len - 1);
if (chars_read > 0) {
    buff[chars_read] = '\0';
    printf("socket->pty: %s\n" buff);
    write(leader_fd, buff, chars_read);
}

当我fgets从追随者端删除时,我的输出变为:

socket->pty: what
pty->socket: .txt: No such file or directory
$56152 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56153 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56154 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56155 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56156 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56157 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56158 (st
socket->pty: why?
pty->socket: arted): cat hello.txt
cat: hello.txt: No such file or directory
$56159 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56160 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56161 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56162 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56163 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56164 (started): cat hello.txt

为什么追随者一侧的阅读会混淆领导者一侧的阅读?

标签: clinuxptyjob-control

解决方案


推荐阅读