From 896c77df1ce2645c6dd7898b59ea802e204dc7d9 Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Sun, 1 Jul 2018 19:58:02 +1000 Subject: [PATCH] ctdb-tests: Avoid use of non-portable getopt in stubs getopt is being used with non-portable options. In most cases use simpler, POSIX-compliant getopts instead. In the case of the ctdb test stub command, options can appear after other arguments, so this requires an additional nested loop. In the case of smnotify, there are no short options, so handle the long options manually. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13520 Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- ctdb/tests/eventscripts/stubs/ctdb | 38 +++++++++++++++----------- ctdb/tests/eventscripts/stubs/rpcinfo | 32 +++++++++------------- ctdb/tests/eventscripts/stubs/smnotify | 17 ++++++------ ctdb/tests/eventscripts/stubs/ss | 26 ++++++++---------- 4 files changed, 54 insertions(+), 59 deletions(-) diff --git a/ctdb/tests/eventscripts/stubs/ctdb b/ctdb/tests/eventscripts/stubs/ctdb index c5a6ceecce0..c3ea0d50550 100755 --- a/ctdb/tests/eventscripts/stubs/ctdb +++ b/ctdb/tests/eventscripts/stubs/ctdb @@ -27,29 +27,35 @@ not_implemented () exit $not_implemented_exit_code } -# Don't set $POSIXLY_CORRECT here. -_temp=$(getopt -n "$prog" -o "Xvhn:" -l help -- "$@") || \ - usage - -eval set -- "$_temp" - verbose=false machine_readable=false nodespec="" -args="$*" +args="" + +# Options and command argument can appear in any order, so when +# getopts thinks it is done, process any non-option arguments and go +# around again. +while [ $# -gt 0 ] ; do + while getopts "Xvhn:?" opt ; do + case "$opt" in + X) machine_readable=true ;; + v) verbose=true ;; + n) nodespec="$OPTARG" ;; + \?|*) usage ;; + esac + done + shift $((OPTIND - 1)) -while true ; do - case "$1" in - -X) machine_readable=true ; shift ;; - -v) verbose=true ; shift ;; - -n) nodespec="$2" ; shift 2 ;; - --) shift ; break ;; - -h|--help|*) usage ;; # * shouldn't happen, so this is reasonable. - esac + # Anything left over must be a non-option arg + if [ $# -gt 0 ] ; then + args="${args}${args:+ }${1}" + shift + fi done -[ $# -ge 1 ] || usage +[ -n "$args" ] || usage +set -- $args setup_tickles () { diff --git a/ctdb/tests/eventscripts/stubs/rpcinfo b/ctdb/tests/eventscripts/stubs/rpcinfo index 1866b59560e..bf21197a9d4 100755 --- a/ctdb/tests/eventscripts/stubs/rpcinfo +++ b/ctdb/tests/eventscripts/stubs/rpcinfo @@ -16,29 +16,23 @@ EOF parse_options () { - _temp=$(getopt -n "$prog" -o "T:h" -- "$@") + while getopts "T:h?" opt ; do + case "$opt" in + T) netid="$OPTARG" ;; + \?|h) usage ;; + esac + done + shift $((OPTIND - 1)) - [ $? != 0 ] && usage + [ "$netid" = "tcp" ] || usage - eval set -- "$_temp" + host="$1" ; shift + [ "$host" = "localhost" -o "$host" = "127.0.0.1" ] || usage - while true ; do - case "$1" in - -T) netid="$2"; shift 2 ;; - --) shift ; break ;; - -h|*) usage ;; # * shouldn't happen, so this is reasonable. - esac - done + [ 1 -le $# -a $# -le 2 ] || usage - [ "$netid" = "tcp" ] || usage - - host="$1" ; shift - [ "$host" = "localhost" -o "$host" = "127.0.0.1" ] || usage - - [ 1 -le $# -a $# -le 2 ] || usage - - p="$1" - v="$2" + p="$1" + v="$2" } parse_options "$@" diff --git a/ctdb/tests/eventscripts/stubs/smnotify b/ctdb/tests/eventscripts/stubs/smnotify index 2bace779df0..78710346998 100755 --- a/ctdb/tests/eventscripts/stubs/smnotify +++ b/ctdb/tests/eventscripts/stubs/smnotify @@ -9,28 +9,27 @@ EOF exit 1 } -temp=$(getopt -n "smnotify" -o "h" -l client:,ip:,server:,stateval: -- "$@") -if [ $? != 0 ] ; then - usage -fi - -eval set -- "$temp" - cip="" sip="" mon_name="" state="" -while : ; do +while [ $# -gt 0 ] ; do case "$1" in --client) cip="$2" ; shift 2 ;; + --client=*) cip="${1#*=}" ; shift ;; --ip) sip="$2" ; shift 2 ;; + --ip=*) sip="${1#*=}" ; shift ;; --server) mon_name="$2" ; shift 2 ;; + --server=*) mon_name="${1#*=}" ; shift ;; --stateval) state="$2" ; shift 2 ;; + --stateval=*) state="${1#*=}" ; shift ;; --) shift ; break ;; - *) usage ;; + -*) usage ;; + *) break ;; esac done +[ $# -eq 0 ] || usage if [ -z "$cip" -o -z "$sip" -o -z "$mon_name" -o -z "$state" ] ; then usage diff --git a/ctdb/tests/eventscripts/stubs/ss b/ctdb/tests/eventscripts/stubs/ss index bb291b2b091..30f9b89556b 100755 --- a/ctdb/tests/eventscripts/stubs/ss +++ b/ctdb/tests/eventscripts/stubs/ss @@ -152,23 +152,19 @@ listen=false header=true orig="$*" -temp=$(getopt -n "$prog" -o "txnalHh" -l tcp -l unix -l help -- "$@") -[ $? -eq 0 ] || usage - -eval set -- "$temp" - -while true ; do - case "$1" in - --tcp|-t) tcp=true ; shift ;; - --unix|-x) unix=true ; shift ;; - -l) listen=true ; shift ;; - -a) all=true ; shift ;; - -H) header=false ; shift ;; - -n) shift ;; - --) shift ; break ;; - -h|--help|*) usage ;; + +while getopts "txnalHh?" opt ; do + case "$opt" in + t) tcp=true ;; + x) unix=true ;; + l) listen=true ;; + a) all=true ;; + H) header=false ;; + n) : ;; + \?|h) usage ;; esac done +shift $((OPTIND - 1)) $tcp || $unix || not_supported "$*" -- 2.47.3