From 1eec073e8a997ff43b710189461ccef28872b039 Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Sat, 2 Sep 2017 20:57:56 +1000 Subject: [PATCH] ctdb-tests: Enhance ss stub to check for listening Unix domain sockets Generalise command-line parsing, taking hints from old netstat stub, and use FAKE_NETSTAT_UNIX_LISTEN to specify listening Unix domain sockets. Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- ctdb/tests/eventscripts/stubs/ss | 177 +++++++++++++++++++++++++++++++-------- 1 file changed, 141 insertions(+), 36 deletions(-) diff --git a/ctdb/tests/eventscripts/stubs/ss b/ctdb/tests/eventscripts/stubs/ss index 1a3db0d0467..bb291b2b091 100755 --- a/ctdb/tests/eventscripts/stubs/ss +++ b/ctdb/tests/eventscripts/stubs/ss @@ -4,23 +4,73 @@ prog="ss" usage () { - cat >&2 <&2 <&2 + usage +} -shift 3 +############################################################ + +# +parse_filter () +{ + # Very limited implementation: + # We only expect to find || inside parentheses + # We don't expect to see && - it is implied by juxtaposition + # Operator for port comparison is ignored and assumed to be == + + # Build lists of source ports and source IP addresses where + # each entry is surrounded by '|' characters. These lists can + # be easily "searched" using the POSIX prefix and suffix + # removal operators. + in_parens=false + sports="|" + srcs="|" + + while [ -n "$1" ] ; do + case "$1" in + \() + in_parens=true + shift + ;; + \)) + in_parens=false + shift + ;; + \|\|) + if ! $in_parens ; then + not_supported "|| in parentheses" + fi + shift + ;; + sport) + p="${3#:}" ; sports="${sports}${p}|" + shift 3 + ;; + src) + ip="${2#\[}" ; ip="${ip%\]}" ; srcs="${srcs}${ip}|" + shift 2 + ;; + *) + usage + ;; + esac + done +} # Check if socket has matches in both ok_ips and ok_ports filter_socket () @@ -44,31 +94,11 @@ filter_socket () ss_tcp_established () { - echo "Recv-Q Send-Q Local Address:Port Peer Address:Port" - - # Very limited implementation: - # We only expect to find || inside parentheses - # We don't expect to see && - it is implied by juxtaposition - # Operator for port comparison is ignored and assumed to be == - - # Build lists of source ports and source IP addresses where each - # entry is surrounded by '|' characters. These lists can be - # easily "searched" using the POSIX prefix and suffix removal - # operators. - in_parens=false - sports="|" - srcs="|" - - while [ -n "$1" ] ; do - case "$1" in - \() in_parens=true ; shift ;; - \)) in_parens=false ; shift ;; - \|\|) if ! $in_parens ; then usage ; fi ; shift ;; - sport) p="${3#:}" ; sports="${sports}${p}|" ; shift 3 ;; - src) ip="${2#\[}" ; ip="${ip%\]}" ; srcs="${srcs}${ip}|" ; shift 2 ;; - *) usage ;; - esac - done + if $header ; then + echo "Recv-Q Send-Q Local Address:Port Peer Address:Port" + fi + + parse_filter $* for i in $FAKE_NETSTAT_TCP_ESTABLISHED ; do src="${i%|*}" @@ -88,5 +118,80 @@ ss_tcp_established () done <"$FAKE_NETSTAT_TCP_ESTABLISHED_FILE" } -# Yes, lose the quoting so we can do a hacky parsing job -ss_tcp_established $* +############################################################ + +unix_listen () +{ + if $header ; then + cat <