diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2015-01-07 01:12:01 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2015-01-07 01:12:01 +0000 |
commit | e3aeb3b63b9996bd06c20861e1dac1c9421d9312 (patch) | |
tree | f777537b8db0ae5241f5b6d4a20ca39351249bc1 /src/conn-tools/s6-tcpserver4-socketbinder.c | |
parent | 80f1ebe9aaa1f5fb0ee796f005b0abda1628226d (diff) | |
download | s6-networking-e3aeb3b63b9996bd06c20861e1dac1c9421d9312.tar.xz |
Experimental decoupling of socketbinders and daemons in ucspi servers.
Rewrite of s6-ipcserver as a small wrapper.
s6-tcpserver4 and s6-tcpserver6 will follow.
Diffstat (limited to 'src/conn-tools/s6-tcpserver4-socketbinder.c')
-rw-r--r-- | src/conn-tools/s6-tcpserver4-socketbinder.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/conn-tools/s6-tcpserver4-socketbinder.c b/src/conn-tools/s6-tcpserver4-socketbinder.c new file mode 100644 index 0000000..86f5390 --- /dev/null +++ b/src/conn-tools/s6-tcpserver4-socketbinder.c @@ -0,0 +1,49 @@ +/* ISC license. */ + +#include <unistd.h> +#include <skalibs/uint16.h> +#include <skalibs/uint.h> +#include <skalibs/sgetopt.h> +#include <skalibs/fmtscan.h> +#include <skalibs/strerr2.h> +#include <skalibs/djbunix.h> +#include <skalibs/socket.h> + +#define USAGE "s6-tcpserver4-socketbinder [ -d | -D ] [ -b backlog ] ip4 port prog..." +#define dieusage() strerr_dieusage(100, USAGE) + +int main (int argc, char const *const *argv, char const *const *envp) +{ + unsigned int backlog = 20 ; + int flagreuse = 1 ; + char ip[4] ; + uint16 port ; + PROG = "s6-tcpserver4-socketbinder" ; + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + register int opt = subgetopt_r(argc, argv, "Ddb:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'D' : flagreuse = 0 ; break ; + case 'd' : flagreuse = 1 ; break ; + case 'b' : if (!uint0_scan(l.arg, &backlog)) dieusage() ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + } + if (argc < 3) dieusage() ; + if (!ip4_scan(argv[0], ip) || !uint160_scan(argv[1], &port)) dieusage() ; + close(0) ; + if (socket_tcp4()) strerr_diefu1sys(111, "create socket") ; + if ((flagreuse ? socket_bind4_reuse(0, ip, port) : socket_bind4(0, ip, port)) < 0) + strerr_diefu5sys(111, "bind to ", argv[0], ":", argv[1], " ") ; + if (socket_listen(0, backlog) < 0) + strerr_diefu5sys(111, "listen to ", argv[0], ":", argv[1], " ") ; + + pathexec_run(argv[2], argv + 2, envp) ; + strerr_dieexec(111, argv[2]) ; +} |