summaryrefslogtreecommitdiff
path: root/src/libs6/s6_svc_main.c
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/libs6/s6_svc_main.c
downloads6-90b12bd71bb9fc79a4640b9112c13ef529d0196a.tar.xz
Initial commit
Diffstat (limited to 'src/libs6/s6_svc_main.c')
-rw-r--r--src/libs6/s6_svc_main.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/libs6/s6_svc_main.c b/src/libs6/s6_svc_main.c
new file mode 100644
index 0000000..5d14904
--- /dev/null
+++ b/src/libs6/s6_svc_main.c
@@ -0,0 +1,40 @@
+/* ISC license. */
+
+#include <skalibs/bytestr.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/skamisc.h>
+#include <s6/s6-supervise.h>
+
+#define DATASIZE 256
+
+int s6_svc_main (int argc, char const *const *argv, char const *optstring, char const *usage, char const *controldir)
+{
+ char data[DATASIZE] ;
+ unsigned int datalen = 0 ;
+ register int r ;
+ for (;;)
+ {
+ register int opt = subgetopt(argc, argv, optstring) ;
+ if (opt == -1) break ;
+ if (opt == '?') strerr_dieusage(100, usage) ;
+ if (datalen >= DATASIZE) strerr_dief1x(100, "too many commands") ;
+ data[datalen++] = opt ;
+ }
+ argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ;
+ if (!argc) strerr_dieusage(100, usage) ;
+
+ {
+ unsigned int arglen = str_len(*argv) ;
+ unsigned int cdirlen = str_len(controldir) ;
+ char tmp[arglen + cdirlen + 10] ;
+ byte_copy(tmp, arglen, *argv) ;
+ tmp[arglen] = '/' ;
+ byte_copy(tmp + arglen + 1, cdirlen, controldir) ;
+ byte_copy(tmp + arglen + 1 + cdirlen, 9, "/control") ;
+ r = s6_svc_write(tmp, data, datalen) ;
+ }
+ if (r < 0) strerr_diefu2sys(111, "control ", *argv) ;
+ else if (!r) strerr_diefu3x(100, "control ", *argv, ": supervisor not listening") ;
+ return 0 ;
+}