brunoais
brunoais

Reputation: 6846

Pass data through an anonymous pipe to another program

This is my code I'm trying to use to pass data to the other program.:

static int callWithFile(char* buff) {

    int myPipes[2];


    if( pipe( myPipes ) < 0 ){
        perror("Can't pipe through \n");
        exit(13);
    }

    int pid = fork();
    switch(pid){
        case 0:
            {
                if(verbose_flag) printf("pid is %d; pipe fds are.... %d & %d\n", getpid(), myPipes[PIPE_READ], myPipes[PIPE_WRITE]);

                //close (myPipes[PIPE_READ]);
                write (myPipes[PIPE_WRITE], buff, strlen(buff) + 1); 
                close (myPipes[PIPE_WRITE]);

                char* pipeArg;
                if(verbose_flag){
                    asprintf (&pipeArg, "/proc/%d/fd/%d", getpid(), myPipes[PIPE_READ]);
                    printf("\n%s\n", pipeArg);
                } 

                asprintf (&pipeArg, "/dev/fd/%d", myPipes[PIPE_READ]);


                char* progArgv[] = {
                    "prog",
                    "--new_settings",
                    pipeArg,
                    //"/dev/fd/0",
                    NULL
                };


                // This works just fine

                // FILE* fp = fopen(pipeArg, "r");
                // if (fp == NULL) {
                //     perror("Can't open fd pipe file \n");
                //     exit(14);
                // }

                // fread(buff, sizeof(char), strlen(buff) + 1, fp);

                // printf("buff: %s", buff);


                execvp(prog_path, progArgv);
                perror("execvp screwed up");
                exit(15);
            }

        case -1:
            perror("fork screwed up ");
            exit(16);
    }
    
    close (myPipes[PIPE_READ]);
    close (myPipes[PIPE_WRITE]);
    wait(NULL);

    puts("done");
}

In all aspects, the code appears to be correct and providing the file descriptor for the other program to read from. However, for some reason, the other program tells it can't open and read the file.

This is the program that reads the data: https://github.com/tuxedocomputers/tuxedo-control-center/blob/master/src/common/classes/ConfigHandler.ts#L87 It complains: Error on read option --new_settings with path: /dev/fd/4

I already confirmed that it is correct JSON, so that shouldn't be the problem.

As for debugging it, I can't make it run on my machine for some reason. Cannot launch program because corresponding JavaScript cannot be found..

My objective is to have the equivalent of this in bash: program <(echo $buff) Where $buff is the contents of the buff function argument.

Upvotes: 0

Views: 158

Answers (1)

brunoais
brunoais

Reputation: 6846

Everything in your code is correct except this:

write (myPipes[PIPE_WRITE], buff, strlen(buff) + 1); 

See that + 1? That's the failure. You are sending a null byte (AKA character 0 or '\0') to the program when its JSON parser doesn't expect it.

Try this instead (without the + 1):

write (myPipes[PIPE_WRITE], buff, strlen(buff));

Upvotes: 1

Related Questions