summaryrefslogtreecommitdiffstats
path: root/bidirtest.c
diff options
context:
space:
mode:
authorMarius Halden <marius.h@lden.org>2015-10-31 02:24:51 +0100
committerMarius Halden <marius.h@lden.org>2015-10-31 02:24:51 +0100
commit055eb04b7f5cbf1748010e220428dd6d12629d30 (patch)
treeaad001094436692f8243a6813e7b8d7b1e0eaf2f /bidirtest.c
parent11196ceba062662f59bfe2d54acf8d35b26d4332 (diff)
downloadpiper-055eb04b7f5cbf1748010e220428dd6d12629d30.tar.gz
piper-055eb04b7f5cbf1748010e220428dd6d12629d30.tar.bz2
piper-055eb04b7f5cbf1748010e220428dd6d12629d30.tar.xz
Add a test implementation of bidirectional pipe between to programs
Diffstat (limited to 'bidirtest.c')
-rw-r--r--bidirtest.c74
1 files changed, 74 insertions, 0 deletions
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;
+}