samt1903
samt1903

Reputation: 181

Child process not reading pipe

I am trying to write a program where a parent process will accept incoming TCP connections, fork() a child process to handle them and read/write data to the child process using a pipe. When the child receives TCP data it will write it to the parent process over a pipe and when the parent wants to send TCP data it will write it to the pipe and the child will then write it over the socket. The child is using poll() to read from the pipe and the socket without blocking.

My problem is that the child does not seem to be receiving data over the pipe. I expect the output to look like below:

server: got connection from 192.168.111.10
Child: 1 fds are ready to read
Child: socket received 3 bytes
Parent received 3 bytes
0xaa, 0xbb, 0xcc, 
Parent: sending bytes to child over pipe
Child: pipe received 6 bytes
Child: sending bytes to socket

So the parent sends data back to the child and then the child receives it and forwards it. But the child doesn't seem to be reading from the pipe correctly so the actual output is like below:

server: got connection from 192.168.111.10
Child: 1 fds are ready to read
Child: socket received 3 bytes
Parent received 3 bytes
0xaa, 0xbb, 0xcc, 
Parent: sending bytes to child over pipe
Child: poll timeout occured
Child: poll timeout occured
//Continues forever

My code is below, I marked off with a comment where I think the important part is.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/poll.h>


#define PORT "3490"  // the port users will be connecting to

#define BACKLOG 10     // how many pending connections queue will hold

void sigchld_handler(int s)
{
    // waitpid() might overwrite errno, so we save and restore it:
    int saved_errno = errno;

    while(waitpid(-1, NULL, WNOHANG) > 0);

    errno = saved_errno;
}


// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
    if (sa->sa_family == AF_INET) {
        return &(((struct sockaddr_in*)sa)->sin_addr);
    }

    return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int main(void)
{
    int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd
    struct addrinfo hints, *servinfo, *p;
    struct sockaddr_storage their_addr; // connector's address information
    socklen_t sin_size;
    struct sigaction sa;
    int yes=1;
    char s[INET6_ADDRSTRLEN];
    int rv;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE; // use my IP

    if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    // loop through all the results and bind to the first we can
    for(p = servinfo; p != NULL; p = p->ai_next) {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }

        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }

        if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sockfd);
            perror("server: bind");
            continue;
        }

        break;
    }

    freeaddrinfo(servinfo); // all done with this structure

    if (p == NULL)  {
        fprintf(stderr, "server: failed to bind\n");
        exit(1);
    }

    if (listen(sockfd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }

    sa.sa_handler = sigchld_handler; // reap all dead processes
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    if (sigaction(SIGCHLD, &sa, NULL) == -1) {
        perror("sigaction");
        exit(1);
    }

    printf("server: waiting for connections...\n");
////////////////// RELEVANT PART STARTS HERE//////////////////////
    int child_pipe[2], parent_pipe[2];
    while(1) {  // main accept() loop
        sin_size = sizeof their_addr;
        new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
        if (new_fd == -1) {
            perror("accept");
            continue;
        }

        inet_ntop(their_addr.ss_family,
            get_in_addr((struct sockaddr *)&their_addr),
            s, sizeof s);
        printf("server: got connection from %s\n", s);

        //Pipe for child to send data to parent
        if(pipe(child_pipe) == -1) {
            perror("pipe");
        }

        //Pipe for parent to send data to child
        if(pipe(parent_pipe) == -1) {
            perror("pipe");
        }

        if (!fork()) { // this is the child process
            close(sockfd); // child doesn't need the listener
            close(child_pipe[0]); //close input side of pipe
            close(parent_pipe[1]); //close output side of pipe

            struct pollfd ufds[2];
            ufds[0].fd = new_fd;
            ufds[0].events = POLLIN;
            ufds[1].fd = parent_pipe[0];
            ufds[2].events = POLLIN;

            while(1) {
                const int rv = poll(ufds, 2, 3000);
                if(rv < 0) { //Error
                    perror("poll");
                    break;
                }
                else if(rv == 0) { //Timeout
                    printf("Child: poll timeout occured\n");
                }
                else { //Ready to read
                    printf("Child: %d fds are ready to read\n", rv);

                    if(ufds[0].revents & POLLIN) { //Received TCP data
                        uint8_t temp_buf[50] = {0};
                        const int recv_ret = recv(new_fd, temp_buf, sizeof(temp_buf), 0);
                        if(recv_ret == 0) { //Connection was closed
                            printf("Child: Connection closed\n");
                            break;
                        }
                        else if(recv_ret < 0) { //Error in recv
                            perror("recv");
                            //TODO: Make check errno 22 specifically to make sure this was cause of error
                            break;
                        }
                        else {
                            printf("Child: socket received %d bytes\n", recv_ret);
                            write(child_pipe[1], temp_buf, recv_ret);
                        }
                    }

                    if(ufds[1].revents & POLLIN) { //Received pipe data from parent
                        uint8_t pipe_buf[50] = {0};
                        const int pipe_ret = read(parent_pipe[0], pipe_buf, sizeof(pipe_buf));
                        printf("Child: pipe received %d bytes\n", pipe_ret);
                        printf("Child: sending bytes to socket\n");
                        if (send(new_fd, pipe_buf, pipe_ret, 0) == -1)
                            perror("send");
                    }
                }
            }

            printf("Child: exiting\n");
            close(new_fd);
            exit(0);
        }
        else { //This is the parent process
            close(new_fd);  // parent doesn't need this
            close(child_pipe[1]); //close output side of pipe
            close(parent_pipe[0]); //close input side of pipe
            uint8_t read_buf[50] = {0};
            const int nbytes = read(child_pipe[0], read_buf, sizeof(read_buf));
            printf("Parent received %d bytes\n", nbytes);
            for(int j = 0; j < nbytes; j++) {
                printf("0x%02x, ", read_buf[j]);
            }
            printf("\n");

            //Write a message back
            char msg_buf[] = {'h', 'e', 'l', 'l', 'o', '\0'};
            printf("Parent: sending bytes to child over pipe\n");
            write(parent_pipe[1], msg_buf, sizeof(msg_buf));
        }

    }

    return 0;
}

Upvotes: 1

Views: 656

Answers (1)

Orest Hera
Orest Hera

Reputation: 6776

There is wrong array index:

        struct pollfd ufds[2];
        ufds[2].events = POLLIN; /* <== BUG */

        /* correct index */
        ufds[1].events = POLLIN;

Upvotes: 1

Related Questions