DMH
DMH

Reputation: 192

Process with output file descriptor redirected to pipe is writing on screen instead of pipe

So this is my code, socket, nameExecutable and commandLineArgs are all parameters received by the function.

My issue is that the new process isn't writing to its pipe and is instead writing to the terminal.

The goal of this code is to have the child process write its output to the parent and then the parent sends it through the socket, when content is received through the socket the parent sends it to the child process for processing.

char bufferWriteToClient[BUFFER_WRITE_SIZE],bufferReadFromClient[BUFFER_READ_SIZE];
pid_t pid;
fd_set readSet;
FD_ZERO(&readSet);
struct timeval timeVal;
int pipeDad[2],pipeSon[2],returnState;
if (pipe(pipeDad) < 0)
    return -1;
if (pipe(pipeSon) < 0){
    close(pipeDad[0]);
    close(pipeDad[1]);
    return -1;
} // End of pipe initialization


switch ((pid = fork())){
case -1:
    close(pipeDad[0]);
    close(pipeDad[1]);
    close(pipeSon[0]);
    close(pipeSon[1]);
    return -1;
case 0:
    // Duplicate new process stdin and stdout to be the parents pipe received end and childs pipe writing end

    dup2(STDIN_FILENO,pipeDad[0]);
    dup2(STDOUT_FILENO,pipeSon[1]);

    // Close the ends we don't need
    close(pipeSon[0]);
    close(pipeDad[1]);
    execv(nameExecutable,commandLineArgs);
    break;
default:
    // Close the ends attributed to the child process
    close(pipeDad[0]);
    close(pipeSon[1]);
    // --------------------------------
    while (!waitpid(pid,&returnState,WNOHANG)){
        int retValSelect;
        FD_SET(pipeSon[0],&readSet);
        FD_SET(socket,&readSet);
        timeVal.tv_sec = 3;
        timeVal.tv_usec = 0;
        retValSelect = select(pipeSon[0] > socket ? pipeSon[0]+1:socket+1,&readSet,NULL,NULL,&timeVal);
        if (retValSelect == -1){
            return -1;
        }
        if (FD_ISSET(pipeSon[0],&readSet)){
            int receivedBytes = readUpToData(socket,bufferWriteToClient,sizeof(bufferWriteToClient)-1);
            if (receivedBytes == -1)
                return -1;
            if (bufferWriteToClient[receivedBytes] != '\0')
                bufferWriteToClient[receivedBytes+1] = '\0';
            sendData(socket,bufferWriteToClient,strlen(bufferWriteToClient)+1);
        }
        if (FD_ISSET(socket,&readSet)){
            int receivedBytes = readUpToData(socket,bufferReadFromClient,sizeof(bufferReadFromClient)-1);
            if (receivedBytes == -1)
                return -1;
            if (bufferReadFromClient[receivedBytes] != '\0')
                bufferReadFromClient[receivedBytes+1] = '\0';
            sendData(pipeDad[1],bufferReadFromClient,strlen(bufferReadFromClient)+1);
        }
    }
    break;
}

So before I've even gotten to the default case in the switch the child process has already written to the terminal, can anyone tell me what I'm doing wrong?

Upvotes: 1

Views: 45

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118445

Looks like you reversed the arguments to dup2(). From the dup(2) man page:

NAME dup, dup2, dup3 - duplicate a file descriptor

SYNOPSIS

   #include <unistd.h>

   int dup(int oldfd);
   int dup2(int oldfd, int newfd);

Upvotes: 1

Related Questions