How to make a slave the controlling terminal in linux?

63 Views Asked by At

Can someone please explain to me why ioctl fails to change set the slave as the controlling terminal in the following code

int main(int argc, char ** argv) 
{
    int master, slave, status;
    int client_socket;
    /*
        created the socket and connected to the remote server
    */

    pid_t pid = fork();

    if (pid < 0)
    {
        perror("Failed to fork process");
    }
    else if (pid == 0)
    {
        close(master);
        if (openpty(&master, &slave, slave_name, NULL,NULL) != 0)
        {
            perror("child process failed to create a pty");
        }
        if (setsid() < 0)
        {
            perror("failed to start new session");
        }
        if (ioctl(slave, TIOCSCTTY, 0) < 0)
        {
            perror("failed set to slave as controlling tty");
        }
        execl("/bin/bash", "/bin/bash", NULL);
    }
    else
    {
        close(slave);
        // PARENT 
        waitpid(pid, &status, 0);

    }
    close(client_socket);
    return 0;
}

On successful connection to the remote server I get the error

bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell

and the forked process still works.

I checked the controlling terminal before and after running the client program and got the same value dev/pty/7

Can someone explain how to fix this error?

0

There are 0 best solutions below