Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <string.h>
- #include <sys/mman.h>
- #include <sys/wait.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <stdbool.h>
- int
- main(int argc, char *argv[])
- {
- int pip1[2];
- pipe2(pip1, O_CLOEXEC);
- if (!fork()) {
- dup2(pip1[1], 1);
- execlp(argv[1], argv[1], NULL);
- }
- int pip2[2];
- pipe2(pip2, O_CLOEXEC);
- if (!fork()) {
- dup2(pip1[0], 0);
- dup2(pip2[1], 1);
- close(pip1[0]);
- close(pip1[1]);
- close(pip2[0]);
- close(pip2[1]);
- int cmd2_pid = fork();
- if (cmd2_pid == 0) {
- int file = open(argv[5], O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
- dup2(file, 2);
- execlp(argv[2], argv[2], NULL);
- }
- int st;
- waitpid(cmd2_pid, &st, 0);
- if (!WIFEXITED(st) || WEXITSTATUS(st) != 0) {
- _exit(0);
- }
- if (!fork()) {
- execlp(argv[3], argv[3], NULL);
- }
- wait(NULL);
- _exit(0);
- }
- if (!fork()) {
- dup2(pip2[0], 0);
- execlp(argv[4], argv[4], NULL);
- }
- wait(NULL);
- wait(NULL);
- wait(NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement