Comment 5 for bug 1898000

Revision history for this message
Christian Ehrhardt  (paelzer) wrote :

As mentioned we use getopt ignoring all but -x to select.

opstring so far was ":x"
1. leading ":" => silence errors
2. look for -x

We had hoped that we don't have to "keep track" of the official chronyd arguments that way.

But what happens is that the "1" as argument to -F with an optstring that misses "F:" will make getopt consider it the end of the options.
Therefore the getopt loop ends and won't reach the -x :-/

For some hardening I wonder if for our purpose "is a -x set" I wonder if we should switch away from getopt entirely or improve on it.

The following is already much more robust ignoring any bad args and odd arguments in between
for idx in $(seq 1 $#); do
    OPTIND=$idx
    echo PROCESS OPTIND $OPTIND
    if getopts ":x" opt; then
        echo OPT is $opt
        if [ "$opt" = "x" ]; then
            echo -x is set
        fi
    fi
done

But it would still fails at combined short args like -qdx to recognize -x out of these.

The following is even better, but depends on bash
for arg in $@; do
    echo ARG = $arg
    if [[ "$arg" =~ ^-[a-zA-Z0-9]*x ]]; then
        X_SET=1
    fi
done

Or we could do the same with grep

for arg in $@; do
    echo ARG = $arg
    if echo "$arg" | grep -q -e '^-[a-zA-Z0-9]*x'; then
         X_SET=1
    fi
done

The last two versions catch:
a) -F -1 -x
b) -F 1 -x
c) -dx
d) not confused by --longopts (non existing in chronyd today, but who knows)
e) any combination of a/b/c/d

bash and grep both already have
  Essential: yes
So we'd not need a dependency on it when introducing this change.

Generally people like grep more than bash regex, so I'm going with that.