From 5b3e121798dcc7e79422a7d95894acc2a0bf229f Mon Sep 17 00:00:00 2001 From: Gunter Labes Date: Thu, 24 Mar 2022 12:28:33 +0100 Subject: [PATCH] Fix parameter handling and quoting Add timestamps to some log messages and custom settings for newer server versions. --- utils/mp-server/run_server | 41 +++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/utils/mp-server/run_server b/utils/mp-server/run_server index 878962ff4e4..965749415d6 100755 --- a/utils/mp-server/run_server +++ b/utils/mp-server/run_server @@ -1,12 +1,12 @@ #!/bin/sh -die() { echo >&2 "$@"; exit 1; } +die() { printf '%s\n' "$*" >&2; exit 1; } dietail() { - echo >&2 "$@" - echo >&2 "tail $LOG:" + printf '%s\n' "$*" >&2 + printf 'tail %s:\n' "$LOG" >&2 tail "$SERVERBASE/logs/$LOG" exit 1 } -[ $# -ge 1 ] || die "Syntax: $(basename $0) [--test] []" +[ $# -ge 1 ] || die "Syntax: $(basename "$0") [--test] []" set -o nounset set -o noclobber @@ -19,15 +19,13 @@ SERVERBASE="$HOME/servers/$SERVER" shift if [ $# -ge 1 ] && [ "$1" = "--test" ]; then shift - PARAMETERS="$@" cd "$SERVERBASE"/build || exit 1 - bin/wesnothd-$SERVER -c "$SERVERBASE"/wesnothd.cfg --port 15001 $PARAMETERS + bin/wesnothd-$SERVER -c "$SERVERBASE"/wesnothd.cfg --port 15001 "$@" # remove the socket so it looks like we never ran the server rm var/run/socket exit 0 fi -PARAMETERS="$@" THREADS=4 PORT=15000 case $SERVER in @@ -41,11 +39,17 @@ case $SERVER in 1.7*) PORT=14997 THREADS=16 ;; 1.9*|1.10*) - PORT=14997 - THREADS=30 ;; + PORT=14996 + THREADS=8 ;; 1.12*) - # TODO: increase after 1.12.0 is announced - THREADS=16 ;; + THREADS=30 ;; +1.14*) + set -- --keepalive ;; +1.16*) + set -- --keepalive ;; +1.13*|trunk) + # TODO: loonycyborg wants to test this with asio builds + set -- --keepalive ;; esac ulimit -Ss 2048 @@ -53,22 +57,22 @@ ulimit -c unlimited # send the standard server message to the appropriate server when killing it with ctrl+c #trap "$HOME/bin/send_server_message $SERVER; sleep 2; echo -n 'terminated: '; date; exit 0" INT -trap "echo -n 'terminated: '; date; exit 0" INT +trap 'printf "terminated: "; date; exit' INT -while [ true ] +while true do cd "$SERVERBASE"/build || exit 1 [ -x bin/wesnothd-$SERVER ] || die "Executable 'bin/wesnothd-$SERVER' not found." DATE=$(date +"%Y%m%d-%H%M%S") - [ -r "$SERVERBASE"/redirect.cfg ] && PORT=$(sed -nre '/port=/s/[ \t]*port="?([0-9]+)"?/\1/p' "$SERVERBASE"/redirect.cfg) + [ -r "$SERVERBASE"/redirect.cfg ] && PORT=$(sed -nE '/port=/s/[[:blank:]]*port="?([0-9]+)"?/\1/p' "$SERVERBASE"/redirect.cfg) BUILDDIR=$(readlink "$SERVERBASE"/build) #REV=r$(echo $BUILDDIR | sed -nre "s,.*wesnothd-svn-([0-9:SM]+)_$SERVER$,\1,p") - REV=$(echo $BUILDDIR | sed -nre "s,.*wesnothd-$SERVER-git-(.*)$,\1,p") + REV=${BUILDDIR#*wesnothd-*-git-} LOG="wesnothd.$DATE.$REV.log" - bin/wesnothd-$SERVER -c "$SERVERBASE"/wesnothd.cfg --port $PORT --threads $THREADS $PARAMETERS > "$SERVERBASE/logs/$LOG" 2>&1 & + bin/wesnothd-$SERVER -c "$SERVERBASE"/wesnothd.cfg --port "$PORT" --threads "$THREADS" "$@" > "$SERVERBASE/logs/$LOG" 2>&1 & PID=$! - echo "started $SERVER server with command: 'wesnothd-$SERVER -c \"$SERVERBASE\"/wesnothd.cfg --port $PORT --threads $THREADS $PARAMETERS' (commit: $REV, pid: $PID) logging to: $LOG" + echo "started $SERVER server with command: 'wesnothd-$SERVER -c \"$SERVERBASE\"/wesnothd.cfg --port $PORT --threads $THREADS $*' (commit: $REV, pid: $PID) logging to: $LOG" # wait a bit in case the process dies immediately sleep 5 if ps -p $PID > /dev/null 2>&1 @@ -84,7 +88,7 @@ do # wait for the server to terminate wait $PID EXIT_CODE=$? - echo "wesnothd exited with code: $EXIT_CODE" + printf '%s wesnothd exited with code: %s\n' "$(date +'%Y%m%d %T')" "$EXIT_CODE" # need to use the recorded path since the build/ symlink might have changed mv "$SERVERBASE/$BUILDDIR/gmon.out" "$SERVERBASE/$BUILDDIR/gmon.$DATE.$REV.out" > /dev/null 2>&1 # check for return code if not zero or 98 (port in use) the server should be restarted @@ -93,6 +97,7 @@ do 98|127|128) dietail ;; #Could not bind to port #Command not found #Invalid argument to exit esac + printf '%s tail %s:\n' "$(date +'%Y%m%d %T')" "$LOG" tail "$SERVERBASE/logs/$LOG" echo done