diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2017-03-12 19:39:01 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2017-03-12 19:39:01 +0000 |
commit | 53091e3bce487ee82e2805a0231e780551561717 (patch) | |
tree | f0fa36ff8eadaf1f01d4510597b5e3a310764dc7 /src/conn-tools/s6-tcpserver4d.c | |
parent | f85b8a70f3b44510a5cf3895bf7357ae90655f65 (diff) | |
download | s6-networking-53091e3bce487ee82e2805a0231e780551561717.tar.xz |
Adapt to skalibs-2.5.0.0
Diffstat (limited to 'src/conn-tools/s6-tcpserver4d.c')
-rw-r--r-- | src/conn-tools/s6-tcpserver4d.c | 75 |
1 files changed, 38 insertions, 37 deletions
diff --git a/src/conn-tools/s6-tcpserver4d.c b/src/conn-tools/s6-tcpserver4d.c index e176ee5..7579ea2 100644 --- a/src/conn-tools/s6-tcpserver4d.c +++ b/src/conn-tools/s6-tcpserver4d.c @@ -1,6 +1,6 @@ /* ISC license. */ -#include <sys/types.h> +#include <string.h> #include <stdint.h> #include <sys/stat.h> #include <sys/wait.h> @@ -10,10 +10,7 @@ #include <signal.h> #include <skalibs/gccattributes.h> #include <skalibs/allreadwrite.h> -#include <skalibs/uint16.h> -#include <skalibs/uint32.h> -#include <skalibs/uint.h> -#include <skalibs/bytestr.h> +#include <skalibs/types.h> #include <skalibs/sgetopt.h> #include <skalibs/strerr2.h> #include <skalibs/fmtscan.h> @@ -29,11 +26,18 @@ #define USAGE "s6-tcpserver4d [ -v verbosity ] [ -1 ] [ -c maxconn ] [ -C localmaxconn ] prog..." +typedef struct pidip_s pidip_t, *pidip_t_ref ; +struct pidip_s +{ + pid_t left ; + uint32_t right ; +} ; + static unsigned int maxconn = 40 ; static unsigned int localmaxconn = 40 ; static unsigned int verbosity = 1 ; static int cont = 1 ; -static diuint32 *pidip = 0 ; +static pidip_t *pidip = 0 ; static unsigned int numconn = 0 ; static diuint32 *ipnum = 0 ; static unsigned int iplen = 0 ; @@ -57,22 +61,18 @@ static inline void X (void) /* Lookup primitives */ -static unsigned int lookup_diuint32 (diuint32 const *, unsigned int, unsigned int) gccattr_pure ; -static unsigned int lookup_diuint32 (diuint32 const *tab, unsigned int tablen, unsigned int key) +static inline unsigned int lookup_pid (pid_t pid) { - register unsigned int i = 0 ; - for (; i < tablen ; i++) if (key == tab[i].left) break ; + unsigned int i = 0 ; + for (; i < numconn ; i++) if (pid == pidip[i].left) break ; return i ; } -static inline unsigned int lookup_pid (uint32 pid) -{ - return lookup_diuint32(pidip, numconn, pid) ; -} - -static inline unsigned int lookup_ip (uint32 ip) +static inline unsigned int lookup_ip (uint32_t ip) { - return lookup_diuint32(ipnum, iplen, ip) ; + unsigned int i = 0 ; + for (; i < iplen ; i++) if (ip == ipnum[i].left) break ; + return i ; } @@ -106,27 +106,27 @@ static void log_deny (uint32_t ip, uint16_t port, unsigned int num) strerr_warni7sys("deny ", fmtip, ":", fmtport, " count ", fmtnum, fmtlocalmaxconn) ; } -static void log_accept (uint32_t pid, uint32_t ip, uint16_t port, unsigned int num) +static void log_accept (pid_t pid, uint32_t ip, uint16_t port, unsigned int num) { char fmtipport[IP4_FMT + UINT16_FMT + 1] ; - char fmtpid[UINT32_FMT] ; + char fmtpid[PID_FMT] ; char fmtnum[UINT_FMT] ; - register size_t n ; + size_t n ; n = ip4_fmtu32(fmtipport, ip) ; fmtipport[n++] = ':' ; n += uint16_fmt(fmtipport + n, port) ; fmtipport[n] = 0 ; fmtnum[uint_fmt(fmtnum, num)] = 0 ; - fmtpid[uint32_fmt(fmtpid, pid)] = 0 ; + fmtpid[pid_fmt(fmtpid, pid)] = 0 ; strerr_warni7x("allow ", fmtipport, " pid ", fmtpid, " count ", fmtnum, fmtlocalmaxconn) ; } -static void log_close (uint32_t pid, uint32_t ip, int w) +static void log_close (pid_t pid, uint32_t ip, int w) { - char fmtpid[UINT32_FMT] ; + char fmtpid[PID_FMT] ; char fmtip[IP4_FMT] = "?" ; char fmtw[UINT_FMT] ; - fmtpid[uint32_fmt(fmtpid, pid)] = 0 ; + fmtpid[pid_fmt(fmtpid, pid)] = 0 ; fmtip[ip4_fmtu32(fmtip, ip)] = 0 ; fmtw[uint_fmt(fmtw, WIFSIGNALED(w) ? WTERMSIG(w) : WEXITSTATUS(w))] = 0 ; strerr_warni6x("end pid ", fmtpid, " ip ", fmtip, WIFSIGNALED(w) ? " signal " : " exitcode ", fmtw) ; @@ -137,7 +137,7 @@ static void log_close (uint32_t pid, uint32_t ip, int w) static void killthem (int sig) { - register unsigned int i = 0 ; + unsigned int i = 0 ; for (; i < numconn ; i++) kill(pidip[i].left, sig) ; } @@ -147,7 +147,7 @@ static void wait_children (void) { unsigned int i ; int w ; - register pid_t pid = wait_nohang(&w) ; + pid_t pid = wait_nohang(&w) ; if (pid < 0) if (errno != ECHILD) strerr_diefu1sys(111, "wait_nohang") ; else break ; @@ -156,7 +156,7 @@ static void wait_children (void) if (i < numconn) /* it's one of ours ! */ { uint32_t ip = pidip[i].right ; - register unsigned int j = lookup_ip(ip) ; + unsigned int j = lookup_ip(ip) ; if (j >= iplen) X() ; if (!--ipnum[j].right) ipnum[j] = ipnum[--iplen] ; pidip[i] = pidip[--numconn] ; @@ -223,11 +223,11 @@ static void run_child (int s, uint32_t ip, uint16_t port, unsigned int num, char PROG = "s6-tcpserver (child)" ; if ((fd_move(0, s) < 0) || (fd_copy(1, 0) < 0)) strerr_diefu1sys(111, "move fds") ; - byte_copy(fmt+n, 22, "PROTO=TCP\0TCPREMOTEIP=") ; n += 22 ; + memcpy(fmt+n, "PROTO=TCP\0TCPREMOTEIP=", 22) ; n += 22 ; n += ip4_fmtu32(fmt+n, ip) ; fmt[n++] = 0 ; - byte_copy(fmt+n, 14, "TCPREMOTEPORT=") ; n += 14 ; + memcpy(fmt+n, "TCPREMOTEPORT=", 14) ; n += 14 ; n += uint16_fmt(fmt+n, port) ; fmt[n++] = 0 ; - byte_copy(fmt+n, 11, "TCPCONNNUM=") ; n += 11 ; + memcpy(fmt+n, "TCPCONNNUM=", 11) ; n += 11 ; n += uint_fmt(fmt+n, num) ; fmt[n++] = 0 ; pathexec_r(argv, envp, env_len(envp), fmt, n) ; strerr_dieexec(111, argv[0]) ; @@ -237,7 +237,7 @@ static void new_connection (int s, uint32_t ip, uint16_t port, char const *const { unsigned int i = lookup_ip(ip) ; unsigned int num = (i < iplen) ? ipnum[i].right : 0 ; - register pid_t pid ; + pid_t pid ; if (num >= localmaxconn) { log_deny(ip, port, num) ; @@ -261,11 +261,11 @@ static void new_connection (int s, uint32_t ip, uint16_t port, char const *const ipnum[iplen].left = ip ; ipnum[iplen++].right = 1 ; } - pidip[numconn].left = (uint32_t)pid ; + pidip[numconn].left = pid ; pidip[numconn++].right = ip ; if (verbosity >= 2) { - log_accept((uint32_t)pid, ip, port, ipnum[i].right) ; + log_accept(pid, ip, port, ipnum[i].right) ; log_status() ; } } @@ -282,7 +282,7 @@ int main (int argc, char const *const *argv, char const *const *envp) int flag1 = 0 ; for (;;) { - register int opt = subgetopt_r(argc, argv, "1c:C:v:", &l) ; + int opt = subgetopt_r(argc, argv, "1c:C:v:", &l) ; if (opt == -1) break ; switch (opt) { @@ -339,8 +339,9 @@ int main (int argc, char const *const *argv, char const *const *envp) } { - diuint32 inyostack[maxconn<<1] ; - pidip = inyostack ; ipnum = inyostack + maxconn ; + pidip_t pidip_inyostack[maxconn] ; + diuint32 ipnum_inyostack[maxconn] ; + pidip = pidip_inyostack ; ipnum = ipnum_inyostack ; while (cont) { if (iopause_g(x, 1 + (numconn < maxconn), 0) < 0) @@ -355,7 +356,7 @@ int main (int argc, char const *const *argv, char const *const *envp) { char packedip[4] ; uint16_t port ; - register int fd = socket_accept4(x[1].fd, packedip, &port) ; + int fd = socket_accept4(x[1].fd, packedip, &port) ; if (fd < 0) { if (verbosity) strerr_warnwu1sys("accept") ; |