[PATCH] [s6-networking] add silent option to s6-tcpclient

From: John Regan <john_at_jrjrtech.com>
Date: Thu, 12 Apr 2018 09:53:01 -0400

If the user passes "-s" to s6-tcpclient, all warning and
error messages will be supressed, making s6-tcpclient
completely transparent.
---
 doc/s6-tcpclient.html         |  3 +-
 src/conn-tools/s6-tcpclient.c | 73 +++++++++++++++++++++++--------------------
 2 files changed, 41 insertions(+), 35 deletions(-)
diff --git a/doc/s6-tcpclient.html b/doc/s6-tcpclient.html
index 79b66c7..842d659 100644
--- a/doc/s6-tcpclient.html
+++ b/doc/s6-tcpclient.html
_at_@ -28,7 +28,7 @@ then executes into a program.
 <h2> Interface </h2>
 
 <pre>
-     s6-tcpclient [ -q | -Q | -v ] [ -4 | -6 ] [ -d | -D ] [ -r | -R ] [ -h | -H ] [ -n | -N ] [ -t <em>timeout</em> ] [ -l <em>localname</em> ] [ -T <em>timeoutconn</em> ] [ -i <em>localip</em> ] [ -p <em>localport</em> ] <em>host</em> <em>port</em> <em>prog...</em>
+     s6-tcpclient [ -q | -Q | -v | -s ] [ -4 | -6 ] [ -d | -D ] [ -r | -R ] [ -h | -H ] [ -n | -N ] [ -t <em>timeout</em> ] [ -l <em>localname</em> ] [ -T <em>timeoutconn</em> ] [ -i <em>localip</em> ] [ -p <em>localport</em> ] <em>host</em> <em>port</em> <em>prog...</em>
 </pre>
 
 <ul>
_at_@ -80,6 +80,7 @@ the current connection (very unreliable). Else unset. </li>
  <li> <tt>-q</tt>&nbsp;: be quiet. </li>
  <li> <tt>-Q</tt>&nbsp;: be normally verbose. This is the default. </li>
  <li> <tt>-v</tt>&nbsp;: be verbose. </li>
+ <li> <tt>-s</tt>&nbsp;: be absolutely silent, even for system errors. </li>
  <li> <tt>-4</tt>&nbsp;: (only valid if the underlying skalibs has
 IPv6 support) Interpret <em>host</em> as an IPv4 address or make A
 queries to determine its addresses. </li>
diff --git a/src/conn-tools/s6-tcpclient.c b/src/conn-tools/s6-tcpclient.c
index 5fdd7f0..53056b9 100644
--- a/src/conn-tools/s6-tcpclient.c
+++ b/src/conn-tools/s6-tcpclient.c
_at_@ -3,6 +3,7 @@
 #include <string.h>
 #include <stdint.h>
 #include <errno.h>
+#include <unistd.h>
 #include <skalibs/types.h>
 #include <skalibs/sgetopt.h>
 #include <skalibs/fmtscan.h>
_at_@ -18,17 +19,19 @@
 #include <s6-networking/ident.h>
 
 #ifdef SKALIBS_IPV6_ENABLED
-# define USAGE "s6-tcpclient [ -q | -Q | -v ] [ -4 | -6 ] [ -d | -D ] [ -r | -R ] [ -h | -H ] [ -n | -N ] [ -t timeoutinfo ] [ -l localname ] [ -T timeoutconn ] [ -i localip ] [ -p localport ] host port prog..."
-# define TFLAGS_DEFAULT { 0, 0, { 2, 58 }, IP46_ZERO, 0, 1, 0, 0, 1, 0, 1, 1 }
-# define OPTSTRING "qQv46dDrRhHnNt:l:T:i:p:"
+# define USAGE "s6-tcpclient [ -q | -Q | -v | -s ] [ -4 | -6 ] [ -d | -D ] [ -r | -R ] [ -h | -H ] [ -n | -N ] [ -t timeoutinfo ] [ -l localname ] [ -T timeoutconn ] [ -i localip ] [ -p localport ] host port prog..."
+# define TFLAGS_DEFAULT { 0, 0, { 2, 58 }, IP46_ZERO, 0, 1, 0, 0, 0, 1, 0, 1, 1 }
+# define OPTSTRING "qQvs46dDrRhHnNt:l:T:i:p:"
 #else
-# define USAGE "s6-tcpclient [ -q | -Q | -v ] [ -d | -D ] [ -r | -R ] [ -h | -H ] [ -n | -N ] [ -t timeoutinfo ] [ -l localname ] [ -T timeoutconn ] [ -i localip ] [ -p localport ] host port prog..."
-# define TFLAGS_DEFAULT { 0, 0, { 2, 58 }, IP46_ZERO, 0, 1, 1, 0, 1, 1 }
-# define OPTSTRING "qQvdDrRhHnNt:l:T:i:p:"
+# define USAGE "s6-tcpclient [ -q | -Q | -v | -s ] [ -d | -D ] [ -r | -R ] [ -h | -H ] [ -n | -N ] [ -t timeoutinfo ] [ -l localname ] [ -T timeoutconn ] [ -i localip ] [ -p localport ] host port prog..."
+# define TFLAGS_DEFAULT { 0, 0, { 2, 58 }, IP46_ZERO, 0, 1, 0, 1, 0, 1, 1 }
+# define OPTSTRING "qQvsdDrRhHnNt:l:T:i:p:"
 #endif
 
 #define usage() strerr_dieusage(100, USAGE)
+#define usagesilent() _exit(100)
 #define dienomem() strerr_diefu1sys(111, "allocate")
+#define diesilent(e) _exit(e)
 
 #define MAXIP 16
 
_at_@ -41,6 +44,7 @@ struct tflags_s
   ip46_t localip ;
   uint16_t localport ;
   unsigned int verbosity : 2 ;
+  unsigned int silent : 1 ;
 #ifdef SKALIBS_IPV6_ENABLED
   unsigned int ip4 : 1 ;
   unsigned int ip6 : 1 ;
_at_@ -71,6 +75,7 @@ int main (int argc, char const *const *argv)
         case 'q' : if (flags.verbosity) flags.verbosity-- ; break ;
         case 'Q' : flags.verbosity = 1 ; break ;
         case 'v' : flags.verbosity++ ; break ;
+        case 's' : flags.silent = 1 ; break ;
 #ifdef SKALIBS_IPV6_ENABLED
         case '4' : flags.ip4 = 1 ; break ;
         case '6' : flags.ip6 = 1 ; break ;
_at_@ -100,7 +105,7 @@ int main (int argc, char const *const *argv)
         }
         case 'i' : if (!ip46_scan(l.arg, &flags.localip)) usage() ; localip = 1 ; break ;
         case 'p' : if (!uint160_scan(l.arg, &flags.localport)) usage() ; break ;
-        default : usage() ;
+        default : flags.silent ? usagesilent() : usage() ;
       }
     }
     argc -= l.ind ; argv += l.ind ;
_at_@ -110,11 +115,11 @@ int main (int argc, char const *const *argv)
   if (!flags.ip6) flags.ip4 = 1 ;
 #endif
   if (!uint160_scan(argv[1], &remoteport))
-    strerr_dief2x(100, "invalid port number: ", argv[1]) ;
+    flags.silent ? diesilent(100) : strerr_dief2x(100, "invalid port number: ", argv[1]) ;
   tain_now_g() ;
   if (flags.timeout) tain_addsec_g(&deadline, flags.timeout) ;
   else tain_add_g(&deadline, &tain_infinite_relative) ;
-  if (!s6dns_init()) strerr_diefu1sys(111, "init DNS") ; 
+  if (!s6dns_init()) flags.silent ? diesilent(111) : strerr_diefu1sys(111, "init DNS") ; 
   {
     ip46_t ip[2][MAXIP] ;
     unsigned int j = 0 ;
_at_@ -159,7 +164,7 @@ int main (int argc, char const *const *argv)
             genalloc ips = STRALLOC_ZERO ;
             size_t i = 0 ;
             if (s6dns_resolve_aaaaa_g(&ips, argv[0], strlen(argv[0]), flags.qualif, &deadline) <= 0)
-              strerr_diefu4x(111, "resolve ", argv[0], ": ", s6dns_constants_error_str(errno)) ;
+              flags.silent ? diesilent(111) : strerr_diefu4x(111, "resolve ", argv[0], ": ", s6dns_constants_error_str(errno)) ;
             n[0] = genalloc_len(ip46_t, &ips) ;
             if (n[0] >= MAXIP) n[0] = MAXIP ;
             for (; i < n[0] ; i++) ip[0][i] = genalloc_s(ip46_t, &ips)[i] ;
_at_@ -179,7 +184,7 @@ int main (int argc, char const *const *argv)
             stralloc ip6s = STRALLOC_ZERO ;
             size_t i = 0 ;
             if (s6dns_resolve_aaaa_g(&ip6s, argv[0], strlen(argv[0]), flags.qualif, &deadline) <= 0)
-              strerr_diefu4x(111, "resolve ", argv[0], ": ", s6dns_constants_error_str(errno)) ;
+              flags.silent ? diesilent(111) : strerr_diefu4x(111, "resolve ", argv[0], ": ", s6dns_constants_error_str(errno)) ;
             n[0] = ip6s.len >> 4 ;
             if (n[0] >= MAXIP) n[0] = MAXIP ;
             for (; i < n[0] ; i++) ip46_from_ip6(&ip[0][i], ip6s.s + (i << 4)) ;
_at_@ -200,7 +205,7 @@ int main (int argc, char const *const *argv)
             stralloc ip4s = STRALLOC_ZERO ;
             size_t i = 0 ;
             if (s6dns_resolve_a_g(&ip4s, argv[0], strlen(argv[0]), flags.qualif, &deadline) <= 0)
-              strerr_diefu4x(111, "resolve ", argv[0], ": ", s6dns_constants_error_str(errno)) ;
+              flags.silent ? diesilent(111) : strerr_diefu4x(111, "resolve ", argv[0], ": ", s6dns_constants_error_str(errno)) ;
             n[0] = ip4s.len >> 2 ;
             if (n[0] >= MAXIP) n[0] = MAXIP ;
             for (; i < n[0] ; i++) ip46_from_ip4(&ip[0][i], ip4s.s + (i << 2)) ;
_at_@ -208,7 +213,7 @@ int main (int argc, char const *const *argv)
           }
         } 
       }
-      if (!n[0]) strerr_dief2x(100, "no IP address for ", argv[0]) ;
+      if (!n[0]) flags.silent ? diesilent(100) : strerr_dief2x(100, "no IP address for ", argv[0]) ;
     }
 
     if (n[0] == 1)
_at_@ -227,9 +232,9 @@ int main (int argc, char const *const *argv)
         if(!localip) flags.localip.is6 = ip46_is6(&ip[j][i]);
 #endif
         s = socket_tcp46(ip46_is6(&flags.localip));
-        if (s < 0) strerr_diefu1sys(111, "create socket") ;
+        if (s < 0) flags.silent ? diesilent(111) : strerr_diefu1sys(111, "create socket") ;
         if (socket_bind46(s, &flags.localip, flags.localport) < 0)
-          strerr_diefu1sys(111, "bind socket") ;
+          flags.silent ? diesilent(111) : strerr_diefu1sys(111, "bind socket") ;
         tain_addsec_g(&localdeadline, flags.timeoutconn[j]) ;
         if (tain_less(&deadline, &localdeadline)) localdeadline = deadline ;
         if (socket_deadlineconnstamp46_g(s, &ip[j][i], remoteport, &localdeadline)) goto connected ;
_at_@ -241,20 +246,20 @@ int main (int argc, char const *const *argv)
           char fmtport[UINT16_FMT] ;
           fmtip[ip46_fmt(fmtip, &ip[j][i])] = 0 ;
           fmtport[uint16_fmt(fmtport, remoteport)] = 0 ;
-          strerr_warnwu4sys("connect to ", fmtip, " port ", fmtport) ;
+          if(!flags.silent) strerr_warnwu4sys("connect to ", fmtip, " port ", fmtport) ;
         }
       }
     }
-    strerr_diefu2x(111, "connect to ", "a suitable IP address") ;
+    flags.silent ? diesilent(111) : strerr_diefu2x(111, "connect to ", "a suitable IP address") ;
   }
 
  connected:
 
   if (ndelay_off(s) == -1)
-    strerr_diefu1sys(111, "ndelay_off") ;
+    flags.silent ? diesilent(111) : strerr_diefu1sys(111, "ndelay_off") ;
   if (!flags.delay) socket_tcpnodelay(s) ;
   if (socket_local46(s, &flags.localip, &flags.localport) == -1)
-    strerr_diefu2sys(111, "get local", " address and port") ;
+    flags.silent ? diesilent(111) : strerr_diefu2sys(111, "get local", " address and port") ;
 
   {
     ip46_t remoteip ;
_at_@ -262,23 +267,23 @@ int main (int argc, char const *const *argv)
     char fmtport[UINT16_FMT] ;
 
     if (socket_remote46(s, &remoteip, &remoteport) == -1)
-      strerr_diefu2sys(111, "get remote", " address and port") ;
+      flags.silent ? diesilent(111) : strerr_diefu2sys(111, "get remote", " address and port") ;
     fmtip[ip46_fmt(fmtip, &remoteip)] = 0 ;
     fmtport[uint16_fmt(fmtport, remoteport)] = 0 ;
-    if (flags.verbosity >= 2)
+    if (flags.verbosity >= 2 && !flags.silent)
       strerr_warni4x("connected to ", fmtip, " port ", fmtport) ;
     if (!pathexec_env("PROTO", "TCP")
      || !pathexec_env("TCPREMOTEIP", fmtip)
-     || !pathexec_env("TCPREMOTEPORT", fmtport)) dienomem() ;
+     || !pathexec_env("TCPREMOTEPORT", fmtport)) flags.silent ? diesilent(111) : dienomem() ;
 
     fmtip[ip46_fmt(fmtip, &flags.localip)] = 0 ;
     fmtport[uint16_fmt(fmtport, flags.localport)] = 0 ;
     if (!pathexec_env("TCPLOCALIP", fmtip)
-     || !pathexec_env("TCPLOCALPORT", fmtport)) dienomem() ;
+     || !pathexec_env("TCPLOCALPORT", fmtport)) flags.silent ? diesilent(111) : dienomem() ;
 
     if (flags.localname)
     {
-      if (!pathexec_env("TCPLOCALHOST", flags.localname)) dienomem() ;
+      if (!pathexec_env("TCPLOCALHOST", flags.localname)) flags.silent ? diesilent(111) : dienomem() ;
     }
 
  /* DNS resolution for TCPLOCALHOST and TCPREMOTEHOST */
_at_@ -312,13 +317,13 @@ int main (int argc, char const *const *argv)
       {
         tain_t infinite = TAIN_INFINITE ;
         if (!s6dns_resolven_parse_g(blob + !!flags.localname, !flags.localname + !!flags.remotehost, &infinite))
-          strerr_diefu2x(111, "resolve IP addresses: ", s6dns_constants_error_str(errno)) ;
+          flags.silent ? diesilent(111) : strerr_diefu2x(111, "resolve IP addresses: ", s6dns_constants_error_str(errno)) ;
       }
       if (!flags.localname)
       {
         if (blob[0].status)
         {
-          if (!pathexec_env("TCPLOCALHOST", 0)) dienomem() ;
+          if (!pathexec_env("TCPLOCALHOST", 0)) flags.silent ? diesilent(111) : dienomem() ;
         }
         else
         {
_at_@ -328,14 +333,14 @@ int main (int argc, char const *const *argv)
            len = s6dns_domain_tostring(s, 255, genalloc_s(s6dns_domain_t, &data[0].ds)) ;
           genalloc_free(s6dns_domain_t, &data[0].ds) ;
           s[len] = 0 ;
-          if (!pathexec_env("TCPLOCALHOST", s)) dienomem() ;
+          if (!pathexec_env("TCPLOCALHOST", s)) flags.silent ? diesilent(111) : dienomem() ;
         }
       }
       if (flags.remotehost)
       {
         if (blob[1].status)
         {
-          if (!pathexec_env("TCPREMOTEHOST", 0)) dienomem() ;
+          if (!pathexec_env("TCPREMOTEHOST", 0)) flags.silent ? diesilent(111) : dienomem() ;
         }
         else
         {
_at_@ -345,7 +350,7 @@ int main (int argc, char const *const *argv)
            len = s6dns_domain_tostring(s, 255, genalloc_s(s6dns_domain_t, &data[1].ds)) ;
           genalloc_free(s6dns_domain_t, &data[1].ds) ;
           s[len] = 0 ;
-          if (!pathexec_env("TCPREMOTEHOST", s)) dienomem() ;
+          if (!pathexec_env("TCPREMOTEHOST", s)) flags.silent ? diesilent(111) : dienomem() ;
         }
       }
     }
_at_@ -364,19 +369,19 @@ int main (int argc, char const *const *argv)
         ssize_t r = s6net_ident_client_g(idbuf, S6NET_IDENT_ID_SIZE, &remoteip, remoteport, &flags.localip, flags.localport, &deadline) ;
         if (r <= 0)
         {
-          if (flags.verbosity)
+          if (flags.verbosity && !flags.silent)
           {
             if (r < 0) strerr_warnwu1sys("s6net_ident_client") ;
             else strerr_warnw2x("ident server replied: ", s6net_ident_error_str(errno)) ;
           }
-          if (!pathexec_env("TCPREMOTEINFO", "")) dienomem() ;
+          if (!pathexec_env("TCPREMOTEINFO", "")) flags.silent ? diesilent(111) : dienomem() ;
         }
-        else if (!pathexec_env("TCPREMOTEINFO", idbuf)) dienomem() ;
+        else if (!pathexec_env("TCPREMOTEINFO", idbuf)) flags.silent ? diesilent(111) : dienomem() ;
       }
     }
   }
 
-  if (fd_move(6, s) < 0) strerr_diefu2sys(111, "set up fd ", "6") ;
-  if (fd_copy(7, 6) < 0) strerr_diefu2sys(111, "set up fd ", "7") ;
+  if (fd_move(6, s) < 0) flags.silent ? diesilent(111) : strerr_diefu2sys(111, "set up fd ", "6") ;
+  if (fd_copy(7, 6) < 0) flags.silent ? diesilent(111) : strerr_diefu2sys(111, "set up fd ", "7") ;
   xpathexec(argv+2) ;
 }
-- 
2.7.4
Received on Thu Apr 12 2018 - 13:53:01 UTC

This archive was generated by hypermail 2.3.0 : Sun May 09 2021 - 19:38:49 UTC