My objective is to make an IPC between a child and parent through a FIFO. The child should run
execl ("/bin/cat", "cat", "/etc/passwd", (char *)0);
redirect its output to the parents input and the parent should run this command:
cut -l : -f 1
and output this to command line.
Right now, I've successfully linked my FIFO and redirected the output of my child process to the input of the parent. I've done multiple tests and that connection is working properly. The problem is with the execl for the cut, which should look something like this:
execlp("/bin/cut", "cut", "-l:", "-f", "1", NULL);
but I'm pretty sure it isn't.
int cut(){
//
int myfifo;
char buf[MAX_BUF];
printf("\nCut opening FIFO");
if((myfifo = open("/tmp/myfifo", O_RDONLY | O_TRUNC))<0){
perror("open FIFO at cut");
quit(EXIT_FAILURE);}
else{printf("\nCut has FIFO opened and is reading\n");}
//read(myfifo, buf, MAX_BUF); outputting buf goes as supposed to
if( dup2(myfifo, 0) < 0 ){
perror("dup2 at cut");
quit(EXIT_FAILURE);}
//read(STDIN_FILENO, buf, MAX_BUF);
close(myfifo);
execlp("/bin/cut", "cut", "-l:", "-f", "1", NULL);
//this is for testing buf, but i guess the program shouldn't even get here
printf("\nCut has received: %s\nAnd is closing FIFO", buf);
return -1;
}
int cat(){
int myfifo;
//OPEN FIFO
printf("\nCat opening FIFO");
if( (myfifo = open("/tmp/myfifo", O_WRONLY | O_TRUNC) )<0){
perror("open FIFO at cat");
quit(EXIT_FAILURE);
}
else{
printf("\nCat has opened FIFO");
//WRITE OUTPUT OF "cat \etc\passwd" TO FIFO
dup2(myfifo, 1);
execl ("/bin/cat", "cat", "/etc/passwd", (char *)0);
}
close(myfifo);
return 0;
}
The main currently only creates the fifo (mkfifo), forks() and calls the function. My problem could with the stdin of the parent (running cut), but I don't think so, or maybe I'm assuming execl() reads directly from the stdin and It doesn't. I think it's really bcause I'm not writing the "cut" through execl() properly.
Any corrections to the code, or even the way I expressed some ideas can indicate that I don't understand something properly, would be very appreciated. Thank you for helping