diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | bidirtest.c | 74 |
3 files changed, 74 insertions, 11 deletions
diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 6e8de4a..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -piper -*.o diff --git a/Makefile b/Makefile deleted file mode 100644 index a586703..0000000 --- a/Makefile +++ /dev/null @@ -1,9 +0,0 @@ - - -piper: piper.c - ${CC} -o piper -Wall -Werror piper.c - -.PHONY: clean - -clean: - rm -f piper diff --git a/bidirtest.c b/bidirtest.c new file mode 100644 index 0000000..995b222 --- /dev/null +++ b/bidirtest.c @@ -0,0 +1,74 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> +#include <wait.h> +#include <string.h> + +volatile sig_atomic_t sig = 0; + +char *cmd1[] = {"nc", "-l", "12345", NULL}; +char *cmd2[] = {"nc", "-l", "12346", NULL}; + +void +handle_sig(int _sig, siginfo_t *siginfo, void *ucontext) +{ + sig = _sig; +} + +int +main(int argc, char **argv) +{ + int p1[2], p2[2]; + pid_t pid1, pid2; + + struct sigaction act; + memset(&act, 0, sizeof(act)); + + act.sa_sigaction = &handle_sig; + act.sa_flags = SA_SIGINFO; + + sigaction(SIGINT, &act, NULL); + sigaction(SIGTERM, &act, NULL); + sigaction(SIGHUP, &act, NULL); + + pipe(p1); + pipe(p2); + + switch ((pid1 = fork())) { + case 0: + close(0); + close(1); + dup2(p1[0], 0); + dup2(p2[1], 1); + + execvp(cmd1[0], cmd1); + break; + default: + break; + } + + switch ((pid2 = fork())) { + case 0: + close(0); + close(1); + dup2(p2[0], 0); + dup2(p1[1], 1); + + execvp(cmd2[0], cmd2); + break; + default: + break; + } + + while (wait(NULL) != -1 || errno == EINTR) { + if (sig != 0) { + kill(pid1, sig); + kill(pid2, sig); + + sig = 0; + } + } + + return 0; +} |