8a3cc5c601b43c60e5a4c12be76e5b73c61ceb05
[samba.git] / selftest / in_screen
1 #!/bin/bash
2
3 export TMPDIR="$SELFTEST_TMPDIR"
4
5 SERVERNAME="$ENVNAME"
6 [ -z "$SERVERNAME" ] && SERVERNAME="base"
7 basedir=$TMPDIR
8
9 [ -r $basedir/$SERVERNAME.pid ] && {
10     for i in $(seq 2 100); do
11         if [ ! -r "$basedir/${SERVERNAME}-$i.pid" ]; then
12             SERVERNAME="${SERVERNAME}-$i"
13             break
14         fi
15     done
16 }
17
18 rm -f $basedir/$SERVERNAME.*
19
20 # set most of the environment vars we have in the screen session too
21 _ENV=""
22 vars=$(mktemp)
23 printenv |
24   egrep -v '^TERMCAP|^WINDOW|^SHELL|^STY|^SHLVL|^SAMBA_VALGRIND|\$' |
25   egrep '^[A-Z]' |
26   sed "s/\(^[^=]*=\)\(.*\)/export \1'\2'/g" > $basedir/$SERVERNAME.vars
27
28 cat <<EOF > $basedir/$SERVERNAME.launch
29  echo \$\$ > $basedir/$SERVERNAME.pid
30  . $basedir/$SERVERNAME.vars
31  echo "\$(date) starting $SERVERNAME" >> $basedir/$SERVERNAME.log
32  $@
33  echo \$? > $basedir/$SERVERNAME.status
34  read parent < $basedir/$SERVERNAME.parent.pid
35  kill \$parent
36 EOF
37 pid=$$
38
39 cleanup() {
40     trap "exit 1" SIGINT SIGTERM SIGPIPE
41     [ -r $basedir/$SERVERNAME.status ] && {
42         read status < $basedir/$SERVERNAME.status
43         echo "$(date) samba exited with status $status" >> $basedir/$SERVERNAME.log
44         exit $status
45     }
46     read pid < $basedir/$SERVERNAME.pid
47     echo "$(date) Killing samba pid $pid from $$" >> $basedir/$SERVERNAME.log
48     if [ "$pid" = "$$" ]; then
49         exit 1
50     fi
51     kill -9 $pid 2>&1
52     exit 1
53 }
54
55 rm -f $basedir/$SERVERNAME.status $basedir/$SERVERNAME.log
56 echo $$ > $basedir/$SERVERNAME.parent.pid
57 trap cleanup SIGINT SIGTERM SIGPIPE
58 screen -r -X screen -t test:$SERVERNAME bash $basedir/$SERVERNAME.launch
59 echo "$(date) waiting in $$" >> $basedir/$SERVERNAME.log
60 read stdin_var
61 echo "$(date) EOF on stdin" >> $basedir/$SERVERNAME.log
62 read pid < $basedir/$SERVERNAME.pid
63 echo "$(date) killing $pid" >> $basedir/$SERVERNAME.log
64 kill $pid 2> /dev/null
65 echo "$(date) exiting" >> $basedir/$SERVERNAME.log
66 exit 0