0

I'm working on a remote shell program and I have a pty created with forkpty(). Writing from the follower-end to the leader-end works as expected, but I get strange output on the leader side when I start trying to read on the follower end.

I get this:

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

I thought it might have something to do with the debug output, so I disabled it, but:

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@^@^@^@^@^@^@^@^@

It looks like it's repeating, but I can't figure out what it is.

Follower's code

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

Leader's code

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);
}

When I remove the fgets from the follower end my output becomes:

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

Why on earth would a read on the follower side garble the read on the leader side?

JRotelli
  • 45
  • 6

0 Answers0