summaryrefslogtreecommitdiff
path: root/src/conn-tools/s6-tcpserver.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/conn-tools/s6-tcpserver.c')
-rw-r--r--src/conn-tools/s6-tcpserver.c129
1 files changed, 97 insertions, 32 deletions
diff --git a/src/conn-tools/s6-tcpserver.c b/src/conn-tools/s6-tcpserver.c
index fcbf4f2..751b2b8 100644
--- a/src/conn-tools/s6-tcpserver.c
+++ b/src/conn-tools/s6-tcpserver.c
@@ -1,71 +1,136 @@
/* ISC license. */
+#include <sys/types.h>
+#include <limits.h>
#include <skalibs/uint.h>
+#include <skalibs/gidstuff.h>
#include <skalibs/sgetopt.h>
#include <skalibs/strerr2.h>
#include <skalibs/djbunix.h>
#include <skalibs/ip46.h>
+#include <s6/config.h>
#include <s6-networking/config.h>
-#define USAGE "s6-tcpserver [ -q | -Q | -v ] [ -1 ] [ -4 | -6 ] [ -c maxconn ] [ -C localmaxconn ] [ -b backlog ] [ -G gid,gid,... ] [ -g gid ] [ -u uid ] [ -U ] ip port prog..."
+#define USAGE "s6-tcpserver [ -q | -Q | -v ] [ -4 | -6 ] [ -d | -D ] [ -1 ] [ -c maxconn ] [ -C localmaxconn ] [ -b backlog ] [ -G gid,gid,... ] [ -g gid ] [ -u uid ] [ -U ] ip port prog..."
#define dieusage() strerr_dieusage(100, USAGE)
int main (int argc, char const *const *argv, char const *const *envp)
{
- char const *newargv[2 + (argc << 1)] ;
- char const *path ;
- unsigned int m = 3 ;
- int what = 0 ;
unsigned int verbosity = 1 ;
- char fmtv[UINT_FMT] ;
+ int flag1 = 0 ;
+ int flagU = 0 ;
+ int flagreuse = 1 ;
+ unsigned int uid = 0, gid = 0 ;
+ gid_t gids[NGROUPS_MAX] ;
+ unsigned int gidn = (unsigned int)-1 ;
+ unsigned int maxconn = 0 ;
+ unsigned int localmaxconn = 0 ;
+ unsigned int backlog = (unsigned int)-1 ;
+ unsigned int what = 0 ;
PROG = "s6-tcpserver" ;
- newargv[1] = "-v" ;
- newargv[2] = fmtv ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
- register int opt = subgetopt_r(argc, argv, "qQv146Uc:C:b:u:g:G:", &l) ;
+ register int opt = subgetopt_r(argc, argv, "qQvDd1U46c:C:b:u:g:G:", &l) ;
if (opt == -1) break ;
switch (opt)
{
- case '4' : if (what) dieusage() ; what = 4 ; break ;
- case '6' : if (what) dieusage() ; what = 6 ; break ;
case 'q' : verbosity = 0 ; break ;
case 'Q' : verbosity = 1 ; break ;
case 'v' : verbosity = 2 ; break ;
- case '1' : newargv[m++] = "-1" ; break ;
- case 'U' : newargv[m++] = "-U" ; break ;
- case 'c' : newargv[m++] = "-c" ; newargv[m++] = l.arg ; break ;
- case 'C' : newargv[m++] = "-C" ; newargv[m++] = l.arg ; break ;
- case 'b' : newargv[m++] = "-b" ; newargv[m++] = l.arg ; break ;
- case 'u' : newargv[m++] = "-u" ; newargv[m++] = l.arg ; break ;
- case 'g' : newargv[m++] = "-g" ; newargv[m++] = l.arg ; break ;
- case 'G' : newargv[m++] = "-G" ; newargv[m++] = l.arg ; break ;
+ case 'D' : flagreuse = 0 ; break ;
+ case 'd' : flagreuse = 1 ; break ;
+ case '4' : what = 4 ; break ;
+ case '6' : what = 6 ; break ;
+ case 'c' : if (!uint0_scan(l.arg, &maxconn)) dieusage() ; if (!maxconn) maxconn = 1 ; break ;
+ case 'C' : if (!uint0_scan(l.arg, &localmaxconn)) dieusage() ; if (!localmaxconn) localmaxconn = 1 ; break ;
+ case 'b' : if (!uint0_scan(l.arg, &backlog)) dieusage() ; break ;
+ case 'u' : if (!uint0_scan(l.arg, &uid)) dieusage() ; break ;
+ case 'g' : if (!uint0_scan(l.arg, &gid)) dieusage() ; break ;
+ case 'G' : if (!gid_scanlist(gids, NGROUPS_MAX, l.arg, &gidn) && *l.arg) dieusage() ; break ;
+ case '1' : flag1 = 1 ; break ;
+ case 'U' : flagU = 1 ; uid = 0 ; gid = 0 ; gidn = (unsigned int)-1 ; break ;
default : dieusage() ;
}
}
argc -= l.ind ; argv += l.ind ;
+ if (argc < 3) dieusage() ;
}
- if (argc < 3) dieusage() ;
- fmtv[uint_fmt(fmtv, verbosity)] = 0 ;
- newargv[m++] = 0 ;
+
if (!what)
{
ip46_t ip ;
if (!ip46_scan(argv[0], &ip)) dieusage() ;
what = ip46_is6(&ip) ? 6 : 4 ;
}
- if (what == 6)
- {
- newargv[0] = "s6-tcpserver6" ;
- path = S6_NETWORKING_BINPREFIX "s6-tcpserver6" ;
- }
- else
+
{
- newargv[0] = "s6-tcpserver4" ;
- path = S6_NETWORKING_BINPREFIX "s6-tcpserver4" ;
+ unsigned int m = 0 ;
+ unsigned int pos = 0 ;
+ char fmt[UINT_FMT * 5 + GID_FMT * NGROUPS_MAX] ;
+ char const *newargv[23 + argc] ;
+ newargv[m++] = what == 6 ? S6_NETWORKING_BINPREFIX "s6-tcpserver6-socketbinder" : S6_NETWORKING_BINPREFIX "s6-tcpserver4-socketbinder" ;
+ if (!flagreuse) newargv[m++] = "-D" ;
+ if (backlog != (unsigned int)-1)
+ {
+ newargv[m++] = "-b" ;
+ newargv[m++] = fmt + pos ;
+ pos += uint_fmt(fmt + pos, backlog) ;
+ fmt[pos++] = 0 ;
+ }
+ newargv[m++] = "--" ;
+ newargv[m++] = *argv++ ;
+ newargv[m++] = *argv++ ;
+ if (flagU || uid || gid || gidn != (unsigned int)-1)
+ {
+ newargv[m++] = S6_EXTBINPREFIX "s6-applyuidgid" ;
+ if (flagU) newargv[m++] = "-Uz" ;
+ if (uid)
+ {
+ newargv[m++] = "-u" ;
+ newargv[m++] = fmt + pos ;
+ pos += uint_fmt(fmt + pos, uid) ;
+ fmt[pos++] = 0 ;
+ }
+ if (gid)
+ {
+ newargv[m++] = "-g" ;
+ newargv[m++] = fmt + pos ;
+ pos += uint_fmt(fmt + pos, gid) ;
+ fmt[pos++] = 0 ;
+ }
+ if (gidn != (unsigned int)-1)
+ {
+ newargv[m++] = "-G" ;
+ newargv[m++] = fmt + pos ;
+ pos += gid_fmtlist(fmt + pos, gids, gidn) ;
+ fmt[pos++] = 0 ;
+ }
+ newargv[m++] = "--" ;
+ }
+ newargv[m++] = what == 6 ? S6_NETWORKING_BINPREFIX "s6-tcpserver6d" : S6_NETWORKING_BINPREFIX "s6-tcpserver4d" ;
+ if (!verbosity) newargv[m++] = "-v0" ;
+ else if (verbosity == 2) newargv[m++] = "-v2" ;
+ if (flag1) newargv[m++] = "-1" ;
+ if (maxconn)
+ {
+ newargv[m++] = "-c" ;
+ newargv[m++] = fmt + pos ;
+ pos += uint_fmt(fmt + pos, maxconn) ;
+ fmt[pos++] = 0 ;
+ }
+ if (localmaxconn)
+ {
+ newargv[m++] = "-C" ;
+ newargv[m++] = fmt + pos ;
+ pos += uint_fmt(fmt + pos, localmaxconn) ;
+ fmt[pos++] = 0 ;
+ }
+ newargv[m++] = "--" ;
+ while (*argv) newargv[m++] = *argv++ ;
+ newargv[m++] = 0 ;
+ pathexec_run(newargv[0], newargv, envp) ;
+ strerr_dieexec(111, newargv[0]) ;
}
- pathexec_run(path, newargv, envp) ;
- strerr_dieexec(111, path) ;
}