2010-10-10 03:12:46 +04:00
#!/usr/bin/env bash
2010-10-02 23:43:50 +04:00
export TMPDIR="$SELFTEST_TMPDIR"
SERVERNAME="$ENVNAME"
[ -z "$SERVERNAME" ] && SERVERNAME="base"
basedir=$TMPDIR
2010-10-03 01:23:43 +04:00
[ -r $basedir/$SERVERNAME.pid ] && {
2018-10-16 16:08:25 +03:00
for i in {2..100}; do
2010-10-03 01:23:43 +04:00
if [ ! -r "$basedir/${SERVERNAME}-$i.pid" ]; then
SERVERNAME="${SERVERNAME}-$i"
break
fi
done
}
2020-01-20 23:19:40 +03:00
rm -f $basedir/$SERVERNAME.{launch,log,parent.pid,pid,status}
2010-10-03 01:23:43 +04:00
2010-10-02 23:43:50 +04:00
# set most of the environment vars we have in the screen session too
_ENV=""
printenv |
egrep -v '^TERMCAP|^WINDOW|^SHELL|^STY|^SHLVL|^SAMBA_VALGRIND|\$' |
egrep '^[A-Z]' |
sed "s/\(^[^=]*=\)\(.*\)/export \1'\2'/g" > $basedir/$SERVERNAME.vars
cat <<EOF > $basedir/$SERVERNAME.launch
2010-11-01 16:55:25 +03:00
cd $PWD
2010-10-02 23:43:50 +04:00
echo \$\$ > $basedir/$SERVERNAME.pid
. $basedir/$SERVERNAME.vars
2010-10-03 01:23:43 +04:00
echo "\$(date) starting $SERVERNAME" >> $basedir/$SERVERNAME.log
2010-10-02 23:43:50 +04:00
$@
echo \$? > $basedir/$SERVERNAME.status
read parent < $basedir/$SERVERNAME.parent.pid
kill \$parent
EOF
pid=$$
cleanup() {
2010-10-03 01:23:43 +04:00
trap "exit 1" SIGINT SIGTERM SIGPIPE
2010-10-02 23:43:50 +04:00
[ -r $basedir/$SERVERNAME.status ] && {
read status < $basedir/$SERVERNAME.status
echo "$(date) samba exited with status $status" >> $basedir/$SERVERNAME.log
exit $status
}
2020-01-20 23:22:39 +03:00
case $ENVNAME in
*.nmbd|*.smbd|*.winbindd|*.samba)
kill $(cat $basedir/../"${ENVNAME%\.*}"/pid/"${ENVNAME##*\.}".pid)
;;
esac
2010-10-02 23:43:50 +04:00
read pid < $basedir/$SERVERNAME.pid
2010-10-03 01:23:43 +04:00
echo "$(date) Killing samba pid $pid from $$" >> $basedir/$SERVERNAME.log
2010-10-02 23:43:50 +04:00
if [ "$pid" = "$$" ]; then
exit 1
fi
2010-10-03 01:23:43 +04:00
kill -9 $pid 2>&1
2010-10-02 23:43:50 +04:00
exit 1
}
echo $$ > $basedir/$SERVERNAME.parent.pid
trap cleanup SIGINT SIGTERM SIGPIPE
2014-06-05 17:07:07 +04:00
2013-12-12 14:38:22 +04:00
if [[ "$TMUX" ]]; then
2014-06-05 17:07:07 +04:00
TMUX_CMD=tmux
if [[ $TMUX = *tmate* ]]; then
TMUX_CMD=tmate
fi
$TMUX_CMD new-window -n test:$SERVERNAME "bash $basedir/$SERVERNAME.launch"
2017-06-15 18:36:58 +03:00
# tmux seems to lag a bit for new sessions. Don't create them too
# quickly one after another
sleep .1
2013-12-12 14:38:22 +04:00
else
screen -r -X screen -t test:$SERVERNAME bash $basedir/$SERVERNAME.launch
fi
2010-10-02 23:43:50 +04:00
echo "$(date) waiting in $$" >> $basedir/$SERVERNAME.log
read stdin_var
echo "$(date) EOF on stdin" >> $basedir/$SERVERNAME.log
2020-01-20 23:22:39 +03:00
case $ENVNAME in
*.nmbd|*.smbd|*.winbindd|*.samba)
kill $(cat $basedir/../"${ENVNAME%\.*}"/pid/"${ENVNAME##*\.}".pid)
;;
esac
2010-10-02 23:43:50 +04:00
read pid < $basedir/$SERVERNAME.pid
echo "$(date) killing $pid" >> $basedir/$SERVERNAME.log
kill $pid 2> /dev/null
echo "$(date) exiting" >> $basedir/$SERVERNAME.log
exit 0