summaryrefslogtreecommitdiff
path: root/selftest/in_screen
blob: 0704ae53909f9767e660e1338e6bf088f72a5bf1 (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
#!/bin/bash

export TMPDIR="$SELFTEST_TMPDIR"

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

# set most of the environment vars we have in the screen session too
_ENV=""
vars=$(mktemp)
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
 echo \$\$ > $basedir/$SERVERNAME.pid
 . $basedir/$SERVERNAME.vars
 echo "\$(date) starting $@" >> $basedir/$SERVERNAME.log
 $@
 echo \$? > $basedir/$SERVERNAME.status
 read parent < $basedir/$SERVERNAME.parent.pid
 kill \$parent
EOF
pid=$$

cleanup() {
    [ -r $basedir/$SERVERNAME.status ] && {
	read status < $basedir/$SERVERNAME.status
	echo "$(date) samba exited with status $status" >> $basedir/$SERVERNAME.log
	exit $status
    }
    read pid < $basedir/$SERVERNAME.pid
    echo "$(date) Killing samba pid $pid" >> $basedir/$SERVERNAME.log
    if [ "$pid" = "$$" ]; then
	exit 1
    fi
    kill $pid 2>&1
    exit 1
}

rm -f $basedir/$SERVERNAME.status $basedir/$SERVERNAME.log
echo $$ > $basedir/$SERVERNAME.parent.pid
trap cleanup SIGINT SIGTERM SIGPIPE
screen -r -X screen -t test:$SERVERNAME bash $basedir/$SERVERNAME.launch
echo "$(date) waiting in $$" >> $basedir/$SERVERNAME.log
read stdin_var
echo "$(date) EOF on stdin" >> $basedir/$SERVERNAME.log
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