25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-20 17:27:00 +01:00

Remove bashismes from ejabberdctl

To avoid unecessary extra quoting, do not call commands with sh -c ''
and use the -- option from su to supply arguments.

Parse command line parameters is a bit tricky as the previous behavior
allows to mix options to ejabberdctl with unknown options given to
the next script (usually, the ctl). This is solved by relying on the
fact that for loop saves its argument, so we can flush its content
with set -- and re-add unknown options with set -- "$@" "$arg".

Finally, remove unecessary quotes in mnesia options and in the ping
command.
This commit is contained in:
Johan Oudinet 2016-11-21 15:25:58 +01:00
parent d00a634025
commit 404a7c3381

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/sh
# define default configuration # define default configuration
POLL=true POLL=true
@ -27,35 +27,59 @@ if [ "$INSTALLUSER" != "" ] ; then
mkdir -p "$INSTALLUSER_HOME" mkdir -p "$INSTALLUSER_HOME"
chown "$INSTALLUSER" "$INSTALLUSER_HOME" chown "$INSTALLUSER" "$INSTALLUSER_HOME"
fi fi
EXEC_CMD="su $INSTALLUSER -c" EXEC_CMD="as_install_user"
fi fi
done done
if [ `id -g` -eq `id -g $INSTALLUSER` ] ; then if [ `id -g` -eq `id -g $INSTALLUSER` ] ; then
EXEC_CMD="bash -c" EXEC_CMD="as_current_user"
fi fi
if [ "$EXEC_CMD" = "false" ] ; then if [ "$EXEC_CMD" = "false" ] ; then
echo "This command can only be run by root or the user $INSTALLUSER" >&2 echo "This command can only be run by root or the user $INSTALLUSER" >&2
exit 4 exit 4
fi fi
else else
EXEC_CMD="bash -c" EXEC_CMD="as_current_user"
fi fi
# run command either directly or via su $INSTALLUSER
exec_cmd()
{
if [ "EXEC_CMD" = as_install_user ]; then
su -c '"$0" $@"' "INSTALLUSER" -- "$@"
else
"$@"
fi
}
# parse command line parameters # parse command line parameters
declare -a ARGS=() next=init
while [ $# -ne 0 ] ; do for arg; do
PARAM="$1" # Empty argument list as it is already saved in the for buffer
shift if [ "$next" = init ]; then
case $PARAM in next=
--) break ;; set --
fi
case $next in
node) ERLANG_NODE_ARG=$arg; next=;;
config-dir) ETC_DIR=$arg; next=;;
config) EJABBERD_CONFIG_PATH=$arg; next=;;
ctl-config) EJABBERDCTL_CONFIG_PATH=$arg; next=;;
logs) LOGS_DIR=$arg; next=;;
spool) SPOOL_DIR=$arg; next=;;
"")
case $arg in
--) next=raw;;
--no-timeout) EJABBERD_NO_TIMEOUT="--no-timeout" ;; --no-timeout) EJABBERD_NO_TIMEOUT="--no-timeout" ;;
--node) ERLANG_NODE_ARG=$1 ; shift ;; --node) next=node;;
--config-dir) ETC_DIR="$1" ; shift ;; --config-dir) next=config-dir;;
--config) EJABBERD_CONFIG_PATH="$1" ; shift ;; --config) next=config;;
--ctl-config) EJABBERDCTL_CONFIG_PATH="$1" ; shift ;; --ctl-config) next=ctl-config;;
--logs) LOGS_DIR="$1" ; shift ;; --logs) next=logs;;
--spool) SPOOL_DIR="$1" ; shift ;; --spool) next=spool;;
*) ARGS=("${ARGS[@]}" "$PARAM") ;; *) set -- "$@" "$arg";; # unknown option, keep it.
esac;;
raw) # we are after --, keep options as it is.
set -- "$@" "$arg";;
esac esac
done done
@ -93,7 +117,7 @@ ERL_CRASH_DUMP=$LOGS_DIR/erl_crash_$DATETIME.dump
ERL_INETRC=$ETC_DIR/inetrc ERL_INETRC=$ETC_DIR/inetrc
# define mnesia options # define mnesia options
MNESIA_OPTS="-mnesia dir \"\\\"$SPOOL_DIR\\\"\" $MNESIA_OPTIONS" MNESIA_OPTS="-mnesia dir \"$SPOOL_DIR\" $MNESIA_OPTIONS"
# define erl parameters # define erl parameters
ERLANG_OPTS="+K $POLL -smp $SMP +P $ERL_PROCESSES $ERL_OPTIONS" ERLANG_OPTS="+K $POLL -smp $SMP +P $ERL_PROCESSES $ERL_OPTIONS"
KERNEL_OPTS="" KERNEL_OPTS=""
@ -150,40 +174,20 @@ export CONTRIB_MODULES_PATH
export CONTRIB_MODULES_CONF_DIR export CONTRIB_MODULES_CONF_DIR
export ERL_LIBS export ERL_LIBS
shell_escape_str() # TODO: Too much copy-and-paste below, factorize!
{
if test $# -eq 0; then
printf '"" '
else
shell_escape "$@"
fi
}
shell_escape()
{
local RES=()
for i in "$@"; do
if test -z "$i"; then
printf '"" '
else
printf '%q ' "$i"
fi
done
}
# start server # start server
start() start()
{ {
check_start check_start
CMD="`shell_escape \"$ERL\" \"$NAME\" \"$ERLANG_NODE\"` \ exec_cmd $ERL \
$NAME $ERLANG_NODE \
-noinput -detached \ -noinput -detached \
$MNESIA_OPTS \ $MNESIA_OPTS \
$KERNEL_OPTS \ $KERNEL_OPTS \
$EJABBERD_OPTS \ $EJABBERD_OPTS \
-s ejabberd \ -s ejabberd \
$ERLANG_OPTS \ $ERLANG_OPTS \
`shell_escape \"${ARGS[@]}\" \"$@\"`" "$@"
$EXEC_CMD "$CMD"
} }
# attach to server # attach to server
@ -191,13 +195,12 @@ debug()
{ {
debugwarning debugwarning
NID=$(uid debug) NID=$(uid debug)
CMD="`shell_escape \"$ERL\" \"$NAME\" \"$NID\"` \ exec_cmd $ERL $NAME $NID \
-remsh $ERLANG_NODE \ -remsh $ERLANG_NODE \
-hidden \ -hidden \
$KERNEL_OPTS \ $KERNEL_OPTS \
$ERLANG_OPTS \ $ERLANG_OPTS \
`shell_escape \"${ARGS[@]}\" \"$@\"`" "$@"
$EXEC_CMD "$CMD"
} }
# attach to server using Elixir # attach to server using Elixir
@ -206,27 +209,24 @@ iexdebug()
debugwarning debugwarning
# Elixir shell is hidden as default # Elixir shell is hidden as default
NID=$(uid debug) NID=$(uid debug)
CMD="`shell_escape \"$IEX\" \"$IEXNAME\" \"$NID\"` \ exec_cmd $IEX $IEXNAME $NID \
-remsh $ERLANG_NODE \ -remsh "$ERLANG_NODE" \
--erl `shell_escape \"$KERNEL_OPTS\"` \ --erl "$KERNEL_OPTS" \
--erl `shell_escape \"$ERLANG_OPTS\"` \ --erl "$ERLANG_OPTS" \
--erl `shell_escape \"${ARGS[@]}\"` \ --erl "$@"
--erl `shell_escape_str \"$@\"`"
$EXEC_CMD "ERL_PATH=\"$ERL\" $CMD"
} }
# start interactive server # start interactive server
live() live()
{ {
livewarning livewarning
CMD="`shell_escape \"$ERL\" \"$NAME\" \"${ERLANG_NODE}\"` \ exec_cmd $ERL $NAME $ERLANG_NODE \
$MNESIA_OPTS \ $MNESIA_OPTS \
$KERNEL_OPTS \ $KERNEL_OPTS \
$EJABBERD_OPTS \ $EJABBERD_OPTS \
-s ejabberd \ -s ejabberd \
$ERLANG_OPTS \ $ERLANG_OPTS \
`shell_escape \"${ARGS[@]}\" \"$@\"`" "$@"
$EXEC_CMD "$CMD"
} }
# start interactive server with Elixir # start interactive server with Elixir
@ -234,30 +234,27 @@ iexlive()
{ {
livewarning livewarning
echo $@ echo $@
CMD="`shell_escape \"$IEX\" \"$IEXNAME\" \"${ERLANG_NODE}\"` \ exec_cmd $IEX $IEXNAME $ERLANG_NODE \
--erl \"-mnesia dir \\\"$SPOOL_DIR\\\"\" \ --erl "-mnesia dir \"$SPOOL_DIR\"" \
--erl \"`shell_escape \"$KERNEL_OPTS\"`\" \ --erl "$KERNEL_OPTS" \
--erl \"`shell_escape \"$EJABBERD_OPTS\"`\" \ --erl "$EJABBERD_OPTS" \
--app ejabberd \ --app ejabberd \
--erl `shell_escape \"$ERLANG_OPTS\"` \ --erl "$ERLANG_OPTS" \
--erl `shell_escape \"${ARGS[@]}\"` \ --erl "$@"
--erl `shell_escape_str \"$@\"`"
$EXEC_CMD "ERL_PATH=\"$ERL\" $CMD"
} }
# start server in the foreground # start server in the foreground
foreground() foreground()
{ {
check_start check_start
CMD="`shell_escape \"$ERL\" \"$NAME\" \"$ERLANG_NODE\"` \ exec_cmd $ERL $NAME $ERLANG_NODE \
-noinput \ -noinput \
$MNESIA_OPTS \ $MNESIA_OPTS \
$KERNEL_OPTS \ $KERNEL_OPTS \
$EJABBERD_OPTS \ $EJABBERD_OPTS \
-s ejabberd \ -s ejabberd \
$ERLANG_OPTS \ $ERLANG_OPTS \
`shell_escape \"${ARGS[@]}\" \"$@\"`" "$@"
$EXEC_CMD "$CMD"
} }
debugwarning() debugwarning()
@ -311,9 +308,9 @@ livewarning()
etop() etop()
{ {
NID=$(uid top) NID=$(uid top)
$EXEC_CMD "$ERL \ exec_cmd $ERL \
$NAME $NID \ $NAME $NID \
-hidden -s etop -s erlang halt -output text -node $ERLANG_NODE" -hidden -s etop -s erlang halt -output text -node $ERLANG_NODE
} }
ping() ping()
@ -327,11 +324,11 @@ ping()
PING_NODE=$(hostname) PING_NODE=$(hostname)
fi fi
NID=$(uid ping ${PING_NODE}) NID=$(uid ping ${PING_NODE})
$EXEC_CMD "$ERL \ exec_cmd $ERL \
$PING_NAME $NID \ $PING_NAME $NID \
-hidden $KERNEL_OPTS $ERLANG_OPTS \ -hidden $KERNEL_OPTS $ERLANG_OPTS \
-eval 'io:format(\"~p~n\",[net_adm:ping('\"'\"'$PEER'\"'\"')])' \ -eval 'io:format("~p~n",[net_adm:ping('"$PEER"')])' \
-s erlang halt -output text -noinput" -s erlang halt -output text -noinput
} }
help() help()
@ -359,11 +356,10 @@ help()
ctl() ctl()
{ {
NID=$(uid ctl) NID=$(uid ctl)
CMD="`shell_escape \"$ERL\" \"$NAME\" \"$NID\"` \ exec_cmd $ERL $NAME $NID \
-noinput -hidden $KERNEL_OPTS -s ejabberd_ctl \ -noinput -hidden $KERNEL_OPTS -s ejabberd_ctl \
-extra `shell_escape \"$ERLANG_NODE\"` $EJABBERD_NO_TIMEOUT \ -extra $ERLANG_NODE $EJABBERD_NO_TIMEOUT \
`shell_escape \"$@\"`" "$@"
$EXEC_CMD "$CMD"
result=$? result=$?
case $result in case $result in
2) help;; 2) help;;
@ -376,7 +372,8 @@ ctl()
uid() uid()
{ {
uuid=$(uuidgen 2>/dev/null) uuid=$(uuidgen 2>/dev/null)
[ -z "$uuid" -a -f /proc/sys/kernel/random/uuid ] && uuid=$(</proc/sys/kernel/random/uuid) [ -z "$uuid" -a -f /proc/sys/kernel/random/uuid ] && \
uuid=$(cat /proc/sys/kernel/random/uuid)
[ -z "$uuid" ] && uuid=$(printf "%X" $RANDOM$(date +%M%S)$$) [ -z "$uuid" ] && uuid=$(printf "%X" $RANDOM$(date +%M%S)$$)
uuid=${uuid%%-*} uuid=${uuid%%-*}
[ $# -eq 0 ] && echo ${uuid}-${ERLANG_NODE} [ $# -eq 0 ] && echo ${uuid}-${ERLANG_NODE}
@ -431,16 +428,16 @@ wait_for_status()
} }
# main handler # main handler
case "${ARGS[0]}" in case $@ in
'start') start;; 'start') start;;
'debug') debug;; 'debug') debug;;
'iexdebug') iexdebug;; 'iexdebug') iexdebug;;
'live') live;; 'live') live;;
'iexlive') iexlive;; 'iexlive') iexlive;;
'foreground') foreground;; 'foreground') foreground;;
'ping'*) ping ${ARGS[1]};; 'ping'*) shift; ping "$@";;
'etop') etop;; 'etop') etop;;
'started') wait_for_status 0 30 2;; # wait 30x2s before timeout 'started') wait_for_status 0 30 2;; # wait 30x2s before timeout
'stopped') wait_for_status 3 30 2 && stop_epmd;; # wait 30x2s before timeout 'stopped') wait_for_status 3 30 2 && stop_epmd;; # wait 30x2s before timeout
*) ctl "${ARGS[@]}";; *) ctl "$@";;
esac esac