I'm trying to execute a process, read from its stdout, send a response based on that to its stdin, and then send the parent process's stdin to the child's stdin and the child's stdout to the parent's stdout.
So, for example, say the process I want to run is /bin/sh - I would want to send a command, read the response, and then connect the shell to stdin/stdout so that it could be used just as if you had run sh instead of my program.
Here's the code I currently have:
Code:
Right now, executing the process, reading from its stdin, and responding to its stdout all work fine, but I haven't been able to figure out how to redirect the parent's stdin/out to the child afterward.
I originally tried a loop that read from stdin and then wrote to outpipefd[1], read from inpipefd[0], and then wrote that to stdout, which obviously didn't work particularly well.
I also tried redirecting outpipefd[1] to stdout using dup2, but that's the read half of the pipe. If I understand correctly, I would have to redirect outpipefd[0] instead, which is now controlled by the sh process.
Google was surprisingly unhelpful for this, so I would appreciate it if anyone could give me any pointers on how to do this, or let me know if I'm approaching this in the wrong way entirely.
So, for example, say the process I want to run is /bin/sh - I would want to send a command, read the response, and then connect the shell to stdin/stdout so that it could be used just as if you had run sh instead of my program.
Here's the code I currently have:
Code:
pid_t pid = 0;
int inpipefd[2];
int outpipefd[2];
pipe(inpipefd);
pipe(outpipefd);
pid = fork();
if (pid == 0) {
dup2(outpipefd[0], STDIN_FILENO);
dup2(inpipefd[1], STDOUT_FILENO);
//dup2(inpipefd[1], STDERR_FILENO);
prctl(PR_SET_PDEATHSIG, SIGTERM);
// for example
execl("/bin/sh", "sh", (char*) NULL);
exit(1);
}
close(outpipefd[0]);
close(inpipefd[1]);
// some working code that reads something from inpipefd[0] and writes something to outpipefd[1]
// todo: pass the parent's stdin to the child's and the child's stdout to the parent
Right now, executing the process, reading from its stdin, and responding to its stdout all work fine, but I haven't been able to figure out how to redirect the parent's stdin/out to the child afterward.
I originally tried a loop that read from stdin and then wrote to outpipefd[1], read from inpipefd[0], and then wrote that to stdout, which obviously didn't work particularly well.
I also tried redirecting outpipefd[1] to stdout using dup2, but that's the read half of the pipe. If I understand correctly, I would have to redirect outpipefd[0] instead, which is now controlled by the sh process.
Google was surprisingly unhelpful for this, so I would appreciate it if anyone could give me any pointers on how to do this, or let me know if I'm approaching this in the wrong way entirely.