I'm trying to Impliment a pipe trick for my function, but I'm kind of at a loss of how.
Here's some example code I'm working with - you'll get the idea as it is psuedo code.
int do_command_exec (fd_t rfd, command_t *command)
pid_t pid;
fd_t fd;
pid = fork();
if (pid == 0) {
dup2(rfd, STDIN_FILENO);
for (fd = 0; fd < NOFILE; fd++) {
if (fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO) {
close(fd);
}
}
}
memset(cmd, 0, sizeof(cmd));
memcpy(cmd, command->data, command->len);
argv[0] = SHELL_CMD;
argv[1] = "-c";
argv[2] = cmd;
argv[3] = NULL;
execvp(argv[0], argv);
exit(1);
else
.. parent...
Now, when I try and implement it using rfd for a pipe, as you expect, my program does not function correctly with STDIN (ie: no logs)
int pipes[2];
pipe(pipes);
pid = fork();
if (pid == 0) /* Child */
close(pipes[1]);
dup2(pipes[0], STDIN_FILENO);
for (fd = 0; fd < NOFILE; fd++) {
if (fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO) {
close(fd);
}
}
}
memset(cmd, 0, sizeof(cmd));
memcpy(cmd, command->data, command->len);
argv[0] = SHELL_CMD;
argv[1] = "-c";
argv[2] = cmd;
argv[3] = NULL;
execvp(argv[0], argv);
exit(1);
else /* Parent */
close(pipes[0]);
Now the good news is, my program is now existing like I would like (ie: EOF is seen, and the children quit when the parent process dies for any reason, but anything that was using STDIN no longer works).