Home > front end >  Shell prompt does not shows on socket
Shell prompt does not shows on socket

Time:12-05

I want to establish a remote shell over tcp.

I already got it to work, but there's a small issue: The prompt does not shows up on client side.

If my client input echo hi, it well prints hi on stdout (client side), and the shell process remains open until he hits ctrl-D or chooses to exit whatever way.

After some basic investigation, I figured out that bash prints its prompt on stderr (e.g. bash 2>/dev/null will not show any prompt on any terminal).

This may be a hint but before I call execve in the shell process, I do my redirections from my server to my client connection fd this way:

for (int i = 0; i < 3; i  )
{
    dup2(client->confd, i);
}
execve("/bin/bash", (char *[]){"bash", NULL}, NULL);

And as my server's stdout (1) is well redirected to my client connection fd, stderr (2) should too. I'm I right?

I don't know how to fix it, any help would be very appreciated.

More context:

  • If I input an invalid command on client side, a message well appears, meaning stderr is redirected.
  • If I redirect stderr to stdout before my redirections on server side, and then redirects stdin / stdout to my client connection fd, nothing changes.
  • I connect clients to my server this way: nc localhost 8080.
  • I close all open file descriptors that are greater than 2 before my redirections.
  • isatty(client->fd) returns 0, so maybe she shell's prompt isn't shown on non tty descriptors, but can't find any clue in readline code or wherever else.

CodePudding user response:

man bash:

An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option.

A non-interactive shell normally won't print the prompt at all.

  • Related