summaryrefslogtreecommitdiffstats
path: root/selftest/in_screen
blob: d7d1b53d9201e0489ebc420f7b54c138b4b3b159 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bash

export TMPDIR="$SELFTEST_TMPDIR"

SERVERNAME="$ENVNAME"
[ -z "$SERVERNAME" ] && SERVERNAME="base"
basedir=$TMPDIR

[ -r $basedir/$SERVERNAME.pid ] && {
	for i in {2..100}; do
		if [ ! -r "$basedir/${SERVERNAME}-$i.pid" ]; then
			SERVERNAME="${SERVERNAME}-$i"
			break
		fi
	done
}

rm -f $basedir/$SERVERNAME.{launch,log,parent.pid,pid,status}

# 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
cd $PWD
 echo \$\$ > $basedir/$SERVERNAME.pid
 . $basedir/$SERVERNAME.vars
 echo "\$(date) starting $SERVERNAME" >> $basedir/$SERVERNAME.log
 $@
 echo \$? > $basedir/$SERVERNAME.status
 read parent < $basedir/$SERVERNAME.parent.pid
 kill \$parent
EOF
pid=$$

cleanup()
{
	trap "exit 1" SIGINT SIGTERM SIGPIPE
	[ -r $basedir/$SERVERNAME.status ] && {
		read status <$basedir/$SERVERNAME.status
		echo "$(date) samba exited with status $status" >>$basedir/$SERVERNAME.log
		exit $status
	}

	case $ENVNAME in
	*.nmbd | *.smbd | *.winbindd | *.samba | *.samba_dcerpcd)
		kill $(cat $basedir/../"${ENVNAME%\.*}"/pid/"${ENVNAME##*\.}".pid)
		;;
	esac

	read pid <$basedir/$SERVERNAME.pid
	echo "$(date) Killing samba pid $pid from $$" >>$basedir/$SERVERNAME.log
	if [ "$pid" = "$$" ]; then
		exit 1
	fi
	kill -9 $pid 2>&1
	exit 1
}

echo $$ >$basedir/$SERVERNAME.parent.pid
trap cleanup SIGINT SIGTERM SIGPIPE

if [[ "$TMUX" ]]; then
	TMUX_CMD=tmux
	if [[ $TMUX = *tmate* ]]; then
		TMUX_CMD=tmate
	fi

	$TMUX_CMD new-window -n test:$SERVERNAME "bash $basedir/$SERVERNAME.launch"

	# tmux seems to lag a bit for new sessions. Don't create them too
	# quickly one after another
	sleep .1
else
	screen -r -X screen -t test:$SERVERNAME bash $basedir/$SERVERNAME.launch
fi
echo "$(date) waiting in $$" >>$basedir/$SERVERNAME.log
read stdin_var
echo "$(date) EOF on stdin" >>$basedir/$SERVERNAME.log

case $ENVNAME in
*.nmbd | *.smbd | *.winbindd | *.samba | *.samba_dcerpcd)
	kill $(cat $basedir/../"${ENVNAME%\.*}"/pid/"${ENVNAME##*\.}".pid)
	;;
esac

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