summaryrefslogtreecommitdiff
path: root/src/pipe-tools
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2014-12-05 22:26:11 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2014-12-05 22:26:11 +0000
commit90b12bd71bb9fc79a4640b9112c13ef529d0196a (patch)
tree523b3f4ee2969e7a729bab2ba749c4b924ae62af /src/pipe-tools
downloads6-90b12bd71bb9fc79a4640b9112c13ef529d0196a.tar.xz
Initial commit
Diffstat (limited to 'src/pipe-tools')
-rw-r--r--src/pipe-tools/deps-exe/s6-cleanfifodir2
-rw-r--r--src/pipe-tools/deps-exe/s6-ftrig-listen4
-rw-r--r--src/pipe-tools/deps-exe/s6-ftrig-listen13
-rw-r--r--src/pipe-tools/deps-exe/s6-ftrig-notify2
-rw-r--r--src/pipe-tools/deps-exe/s6-ftrig-wait3
-rw-r--r--src/pipe-tools/deps-exe/s6-mkfifodir2
-rw-r--r--src/pipe-tools/s6-cleanfifodir.c15
-rw-r--r--src/pipe-tools/s6-ftrig-listen.c122
-rw-r--r--src/pipe-tools/s6-ftrig-listen1.c101
-rw-r--r--src/pipe-tools/s6-ftrig-notify.c20
-rw-r--r--src/pipe-tools/s6-ftrig-wait.c48
-rw-r--r--src/pipe-tools/s6-mkfifodir.c39
12 files changed, 361 insertions, 0 deletions
diff --git a/src/pipe-tools/deps-exe/s6-cleanfifodir b/src/pipe-tools/deps-exe/s6-cleanfifodir
new file mode 100644
index 0000000..83cec1e
--- /dev/null
+++ b/src/pipe-tools/deps-exe/s6-cleanfifodir
@@ -0,0 +1,2 @@
+-ls6
+-lskarnet
diff --git a/src/pipe-tools/deps-exe/s6-ftrig-listen b/src/pipe-tools/deps-exe/s6-ftrig-listen
new file mode 100644
index 0000000..38a1f7a
--- /dev/null
+++ b/src/pipe-tools/deps-exe/s6-ftrig-listen
@@ -0,0 +1,4 @@
+-ls6
+-lexecline
+-lskarnet
+${TAINNOW_LIB}
diff --git a/src/pipe-tools/deps-exe/s6-ftrig-listen1 b/src/pipe-tools/deps-exe/s6-ftrig-listen1
new file mode 100644
index 0000000..58a34e0
--- /dev/null
+++ b/src/pipe-tools/deps-exe/s6-ftrig-listen1
@@ -0,0 +1,3 @@
+-ls6
+-lskarnet
+${TAINNOW_LIB}
diff --git a/src/pipe-tools/deps-exe/s6-ftrig-notify b/src/pipe-tools/deps-exe/s6-ftrig-notify
new file mode 100644
index 0000000..83cec1e
--- /dev/null
+++ b/src/pipe-tools/deps-exe/s6-ftrig-notify
@@ -0,0 +1,2 @@
+-ls6
+-lskarnet
diff --git a/src/pipe-tools/deps-exe/s6-ftrig-wait b/src/pipe-tools/deps-exe/s6-ftrig-wait
new file mode 100644
index 0000000..58a34e0
--- /dev/null
+++ b/src/pipe-tools/deps-exe/s6-ftrig-wait
@@ -0,0 +1,3 @@
+-ls6
+-lskarnet
+${TAINNOW_LIB}
diff --git a/src/pipe-tools/deps-exe/s6-mkfifodir b/src/pipe-tools/deps-exe/s6-mkfifodir
new file mode 100644
index 0000000..83cec1e
--- /dev/null
+++ b/src/pipe-tools/deps-exe/s6-mkfifodir
@@ -0,0 +1,2 @@
+-ls6
+-lskarnet
diff --git a/src/pipe-tools/s6-cleanfifodir.c b/src/pipe-tools/s6-cleanfifodir.c
new file mode 100644
index 0000000..4af38e1
--- /dev/null
+++ b/src/pipe-tools/s6-cleanfifodir.c
@@ -0,0 +1,15 @@
+/* ISC license. */
+
+#include <skalibs/strerr2.h>
+#include <s6/ftrigw.h>
+
+#define USAGE "s6-cleanfifodir fifodir"
+
+int main (int argc, char const *const *argv)
+{
+ PROG = "s6-cleanfifodir" ;
+ if (argc < 2) strerr_dieusage(100, USAGE) ;
+ if (!ftrigw_clean(argv[1]))
+ strerr_diefu2sys(111, "clean up fifodir at ", argv[1]) ;
+ return 0 ;
+}
diff --git a/src/pipe-tools/s6-ftrig-listen.c b/src/pipe-tools/s6-ftrig-listen.c
new file mode 100644
index 0000000..2f6e82b
--- /dev/null
+++ b/src/pipe-tools/s6-ftrig-listen.c
@@ -0,0 +1,122 @@
+/* ISC license. */
+
+#include <errno.h>
+#include <signal.h>
+#include <unistd.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/uint.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/tai.h>
+#include <skalibs/iopause.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/sig.h>
+#include <skalibs/selfpipe.h>
+#include <execline/execline.h>
+#include <s6/ftrigr.h>
+
+#define USAGE "s6-ftrig-listen [ -a | -o ] [ -t timeout ] ~fifodir1 ~regexp1 ... ; prog..."
+#define dieusage() strerr_dieusage(100, USAGE)
+
+static void handle_signals (void)
+{
+ for (;;) switch (selfpipe_read())
+ {
+ case -1 : strerr_diefu1sys(111, "selfpipe_read") ;
+ case 0 : return ;
+ case SIGCHLD : wait_reap() ; break ;
+ default : strerr_dief1x(101, "unexpected data in selfpipe") ;
+ }
+}
+
+int main (int argc, char const **argv, char const *const *envp)
+{
+ iopause_fd x[2] = { { -1, IOPAUSE_READ, 0 }, { -1, IOPAUSE_READ, 0 } } ;
+ tain_t deadline, tto ;
+ ftrigr_t a = FTRIGR_ZERO ;
+ int argc1 ;
+ unsigned int i = 0 ;
+ char or = 0 ;
+ PROG = "s6-ftrig-listen" ;
+ {
+ unsigned int t = 0 ;
+ for (;;)
+ {
+ register int opt = subgetopt(argc, argv, "aot:") ;
+ if (opt == -1) break ;
+ switch (opt)
+ {
+ case 'a' : or = 0 ; break ;
+ case 'o' : or = 1 ; break ;
+ case 't' : if (uint0_scan(subgetopt_here.arg, &t)) break ;
+ default : dieusage() ;
+ }
+ }
+ if (t) tain_from_millisecs(&tto, t) ; else tto = tain_infinite_relative ;
+ argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ;
+ }
+ if (argc < 2) dieusage() ;
+ argc1 = el_semicolon(argv) ;
+ if (!argc1 || (argc1 & 1) || (argc == argc1 + 1)) dieusage() ;
+ if (argc1 >= argc) strerr_dief1x(100, "unterminated fifodir+regex block") ;
+ tain_now_g() ;
+ tain_add_g(&deadline, &tto) ;
+ x[0].fd = selfpipe_init() ;
+ if (x[0].fd < 0) strerr_diefu1sys(111, "selfpipe_init") ;
+ if (selfpipe_trap(SIGCHLD) < 0) strerr_diefu1sys(111, "selfpipe_trap") ;
+ if (sig_ignore(SIGPIPE) < 0) strerr_diefu1sys(111, "sig_ignore") ;
+
+ if (!ftrigr_startf_g(&a, &deadline)) strerr_diefu1sys(111, "ftrigr_startf") ;
+ x[1].fd = ftrigr_fd(&a) ;
+
+ {
+ int pid = 0 ;
+ unsigned int idlen = argc1 >> 1 ;
+ uint16 ids[idlen] ;
+ for (; i < idlen ; i++)
+ {
+ ids[i] = ftrigr_subscribe_g(&a, argv[i<<1], argv[(i<<1)+1], 0, &deadline) ;
+ if (!ids[i]) strerr_diefu4sys(111, "subscribe to ", argv[i<<1], " with regexp ", argv[(i<<1)+1]) ;
+ }
+
+ pid = fork() ;
+ switch (pid)
+ {
+ case -1 : strerr_diefu1sys(111, "fork") ;
+ case 0 :
+ {
+ PROG = "s6-ftrig-listen (child)" ;
+ pathexec_run(argv[argc1 + 1], argv + argc1 + 1, envp) ;
+ strerr_dieexec(111, argv[argc1 + 1]) ;
+ }
+ }
+
+ for (;;)
+ {
+ register int r ;
+ i = 0 ;
+ while (i < idlen)
+ {
+ char dummy ;
+ r = ftrigr_check(&a, ids[i], &dummy) ;
+ if (r < 0) strerr_diefu1sys(111, "ftrigr_check") ;
+ else if (!r) i++ ;
+ else if (or) idlen = 0 ;
+ else ids[i] = ids[--idlen] ;
+ }
+ if (!idlen) break ;
+ r = iopause_g(x, 2, &deadline) ;
+ if (r < 0) strerr_diefu1sys(111, "iopause") ;
+ else if (!r)
+ {
+ errno = ETIMEDOUT ;
+ strerr_diefu1sys(1, "get expected event") ;
+ }
+ if (x[0].revents & IOPAUSE_READ) handle_signals() ;
+ if (x[1].revents & IOPAUSE_READ)
+ {
+ if (ftrigr_update(&a) < 0) strerr_diefu1sys(111, "ftrigr_update") ;
+ }
+ }
+ }
+ return 0 ;
+}
diff --git a/src/pipe-tools/s6-ftrig-listen1.c b/src/pipe-tools/s6-ftrig-listen1.c
new file mode 100644
index 0000000..1354a64
--- /dev/null
+++ b/src/pipe-tools/s6-ftrig-listen1.c
@@ -0,0 +1,101 @@
+/* ISC license. */
+
+#include <errno.h>
+#include <signal.h>
+#include <unistd.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/uint.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/tai.h>
+#include <skalibs/iopause.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/sig.h>
+#include <skalibs/selfpipe.h>
+#include <s6/ftrigr.h>
+
+#define USAGE "s6-ftrig-listen1 [ -t timeout ] fifodir regexp prog..."
+
+static void handle_signals (void)
+{
+ for (;;) switch (selfpipe_read())
+ {
+ case -1 : strerr_diefu1sys(111, "selfpipe_read") ;
+ case 0 : return ;
+ case SIGCHLD : wait_reap() ; break ;
+ default : strerr_dief1x(101, "unexpected data in selfpipe") ;
+ }
+}
+
+int main (int argc, char const *const *argv, char const *const *envp)
+{
+ iopause_fd x[2] = { { -1, IOPAUSE_READ, 0 }, { -1, IOPAUSE_READ, 0 } } ;
+ tain_t deadline, tto ;
+ ftrigr_t a = FTRIGR_ZERO ;
+ int pid ;
+ uint16 id ;
+ PROG = "s6-ftrig-listen1" ;
+ {
+ unsigned int t = 0 ;
+ for (;;)
+ {
+ register int opt = subgetopt(argc, argv, "t:") ;
+ if (opt == -1) break ;
+ switch (opt)
+ {
+ case 't' : if (uint0_scan(subgetopt_here.arg, &t)) break ;
+ default : strerr_dieusage(100, USAGE) ;
+ }
+ }
+ if (t) tain_from_millisecs(&tto, t) ;
+ else tto = tain_infinite_relative ;
+ argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ;
+ }
+ if (argc < 3) strerr_dieusage(100, USAGE) ;
+
+ tain_now_g() ;
+ tain_add_g(&deadline, &tto) ;
+
+ if (!ftrigr_startf_g(&a, &deadline)) strerr_diefu1sys(111, "ftrigr_startf") ;
+ id = ftrigr_subscribe_g(&a, argv[0], argv[1], 0, &deadline) ;
+ if (!id) strerr_diefu4sys(111, "subscribe to ", argv[0], " with regexp ", argv[1]) ;
+
+ x[0].fd = selfpipe_init() ;
+ if (x[0].fd < 0) strerr_diefu1sys(111, "selfpipe_init") ;
+ if (selfpipe_trap(SIGCHLD) < 0) strerr_diefu1sys(111, "selfpipe_trap") ;
+ if (sig_ignore(SIGPIPE) < 0) strerr_diefu1sys(111, "sig_ignore") ;
+ x[1].fd = ftrigr_fd(&a) ;
+
+ pid = fork() ;
+ switch (pid)
+ {
+ case -1 : strerr_diefu1sys(111, "fork") ;
+ case 0 :
+ {
+ PROG = "s6-ftrig-listen1 (child)" ;
+ pathexec_run(argv[2], argv+2, envp) ;
+ strerr_dieexec(111, argv[2]) ;
+ }
+ }
+
+ for (;;)
+ {
+ char dummy ;
+ register int r = ftrigr_check(&a, id, &dummy) ;
+ if (r < 0) strerr_diefu1sys(111, "ftrigr_check") ;
+ if (r) break ;
+ r = iopause_g(x, 2, &deadline) ;
+ if (r < 0) strerr_diefu1sys(111, "iopause") ;
+ else if (!r)
+ {
+ errno = ETIMEDOUT ;
+ strerr_diefu1sys(1, "get expected event") ;
+ }
+ if (x[0].revents & IOPAUSE_READ) handle_signals() ;
+ if (x[1].revents & IOPAUSE_READ)
+ {
+ if (ftrigr_update(&a) < 0) strerr_diefu1sys(111, "ftrigr_update") ;
+ }
+ }
+
+ return 0 ;
+}
diff --git a/src/pipe-tools/s6-ftrig-notify.c b/src/pipe-tools/s6-ftrig-notify.c
new file mode 100644
index 0000000..1216a6a
--- /dev/null
+++ b/src/pipe-tools/s6-ftrig-notify.c
@@ -0,0 +1,20 @@
+/* ISC license. */
+
+#include <skalibs/strerr2.h>
+#include <s6/ftrigw.h>
+
+#define USAGE "s6-ftrig-notify fifodir message"
+
+int main (int argc, char const *const *argv)
+{
+ char const *p ;
+ PROG = "s6-ftrig-notify" ;
+ if (argc < 3) strerr_dieusage(100, USAGE) ;
+ p = argv[2] ;
+ for (; *p ; p++)
+ {
+ if (ftrigw_notify(argv[1], *p) == -1)
+ strerr_diefu2sys(111, "notify ", argv[1]) ;
+ }
+ return 0 ;
+}
diff --git a/src/pipe-tools/s6-ftrig-wait.c b/src/pipe-tools/s6-ftrig-wait.c
new file mode 100644
index 0000000..772ce86
--- /dev/null
+++ b/src/pipe-tools/s6-ftrig-wait.c
@@ -0,0 +1,48 @@
+/* ISC license. */
+
+#include <errno.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/uint.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/tai.h>
+#include <s6/ftrigr.h>
+
+#define USAGE "s6-ftrig-wait [ -t timeout ] fifodir regexp"
+
+int main (int argc, char const *const *argv)
+{
+ tain_t deadline, tto ;
+ ftrigr_t a = FTRIGR_ZERO ;
+ uint16 id ;
+ char pack[2] = " \n" ;
+ PROG = "s6-ftrig-wait" ;
+ {
+ unsigned int t = 0 ;
+ for (;;)
+ {
+ register int opt = subgetopt(argc, argv, "t:") ;
+ if (opt == -1) break ;
+ switch (opt)
+ {
+ case 't' : if (uint0_scan(subgetopt_here.arg, &t)) break ;
+ default : strerr_dieusage(100, USAGE) ;
+ }
+ }
+ if (t) tain_from_millisecs(&tto, t) ;
+ else tto = tain_infinite_relative ;
+ argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ;
+ }
+ if (argc < 2) strerr_dieusage(100, USAGE) ;
+
+ tain_now_g() ;
+ tain_add_g(&deadline, &tto) ;
+
+ if (!ftrigr_startf_g(&a, &deadline)) strerr_diefu1sys(111, "ftrigr_startf") ;
+ id = ftrigr_subscribe_g(&a, argv[0], argv[1], 0, &deadline) ;
+ if (!id) strerr_diefu4sys(111, "subscribe to ", argv[0], " with regexp ", argv[1]) ;
+ if (ftrigr_wait_or_g(&a, &id, 1, &deadline, &pack[0]) == -1)
+ strerr_diefu2sys((errno == ETIMEDOUT) ? 1 : 111, "match regexp on ", argv[1]) ;
+ if (allwrite(1, pack, 2) < 2) strerr_diefu1sys(111, "write to stdout") ;
+ return 0 ;
+}
diff --git a/src/pipe-tools/s6-mkfifodir.c b/src/pipe-tools/s6-mkfifodir.c
new file mode 100644
index 0000000..4f5151a
--- /dev/null
+++ b/src/pipe-tools/s6-mkfifodir.c
@@ -0,0 +1,39 @@
+/* ISC license. */
+
+#include <skalibs/sgetopt.h>
+#include <skalibs/uint.h>
+#include <skalibs/strerr2.h>
+#include <s6/ftrigw.h>
+
+#define USAGE "s6-mkfifodir [ -f ] [ -g gid ] fifodir"
+
+int main (int argc, char const *const *argv)
+{
+ subgetopt_t l = SUBGETOPT_ZERO ;
+ int gid = -1 ;
+ int force = 0 ;
+ PROG = "s6-mkfifodir" ;
+ for (;;)
+ {
+ register int opt = subgetopt_r(argc, argv, "fg:", &l) ;
+ if (opt == -1) break ;
+ switch (opt)
+ {
+ case 'f' : force = 1 ; break ;
+ case 'g' :
+ {
+ unsigned int g ;
+ if (!uint0_scan(l.arg, &g)) strerr_dieusage(100, USAGE) ;
+ gid = (int)g ;
+ break ;
+ }
+ default : strerr_dieusage(100, USAGE) ;
+ }
+ }
+ argc -= l.ind ; argv += l.ind ;
+ if (argc < 1) strerr_dieusage(100, USAGE) ;
+
+ if (!ftrigw_fifodir_make(*argv, gid, force))
+ strerr_diefu2sys(111, "create fifodir at ", *argv) ;
+ return 0 ;
+}