forkpty - socket

Posted by Alexxx on Stack Overflow See other posts from Stack Overflow or by Alexxx
Published on 2010-03-23T13:46:02Z Indexed on 2010/03/23 15:43 UTC
Read the original article Hit count: 422

Filed under:
|
|
|

Hi,

I'm trying to develop a simple "telnet/server" daemon which have to run a program on a new socket connection. This part working fine.

But I have to associate my new process to a pty, because this process have some terminal capabilities (like a readline).

The code I've developped is (where socketfd is the new socket file descriptor for the new input connection) :

int masterfd, pid;
const char *prgName = "...";
char *arguments[10] = ....;

if ((pid = forkpty(&masterfd, NULL, NULL, NULL)) < 0)
    perror("FORK");
else if (pid)
    return pid;
else
{
    close(STDOUT_FILENO);
    dup2(socketfd, STDOUT_FILENO);

    close(STDIN_FILENO);
    dup2(socketfd, STDIN_FILENO);

    close(STDERR_FILENO);
    dup2(socketfd, STDERR_FILENO);

    if (execvp(prgName, arguments) < 0)
    {
        perror("execvp");
        exit(2);
    }
}

With that code, the stdin / stdout / stderr file descriptor of my "prgName" are associated to the socket (when looking with ls -la /proc/PID/fd), and so, the terminal capabilities of this process doesn't work.

A test with a connection via ssh/sshd on the remote device, and executing "localy" (under the ssh connection) prgName, show that the stdin/stdout/stderr fd of this process "prgName" are associated to a pty (and so the terminal capabilities of this process are working fine).

What I am doing wrong? How to associate my socketfd with the pty (created by forkpty) ?

Thank

Alex

© Stack Overflow or respective owner

Related posts about c

    Related posts about linux