0704ae53909f9767e660e1338e6bf088f72a5bf1
[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 # set most of the environment vars we have in the screen session too
10 _ENV=""
11 vars=$(mktemp)
12 printenv |
13   egrep -v '^TERMCAP|^WINDOW|^SHELL|^STY|^SHLVL|^SAMBA_VALGRIND|\$' |
14   egrep '^[A-Z]' |
15   sed "s/\(^[^=]*=\)\(.*\)/export \1'\2'/g" > $basedir/$SERVERNAME.vars
16
17 cat <<EOF > $basedir/$SERVERNAME.launch
18  echo \$\$ > $basedir/$SERVERNAME.pid
19  . $basedir/$SERVERNAME.vars
20  echo "\$(date) starting $@" >> $basedir/$SERVERNAME.log
21  $@
22  echo \$? > $basedir/$SERVERNAME.status
23  read parent < $basedir/$SERVERNAME.parent.pid
24  kill \$parent
25 EOF
26 pid=$$
27
28 cleanup() {
29     [ -r $basedir/$SERVERNAME.status ] && {
30         read status < $basedir/$SERVERNAME.status
31         echo "$(date) samba exited with status $status" >> $basedir/$SERVERNAME.log
32         exit $status
33     }
34     read pid < $basedir/$SERVERNAME.pid
35     echo "$(date) Killing samba pid $pid" >> $basedir/$SERVERNAME.log
36     if [ "$pid" = "$$" ]; then
37         exit 1
38     fi
39     kill $pid 2>&1
40     exit 1
41 }
42
43 rm -f $basedir/$SERVERNAME.status $basedir/$SERVERNAME.log
44 echo $$ > $basedir/$SERVERNAME.parent.pid
45 trap cleanup SIGINT SIGTERM SIGPIPE
46 screen -r -X screen -t test:$SERVERNAME bash $basedir/$SERVERNAME.launch
47 echo "$(date) waiting in $$" >> $basedir/$SERVERNAME.log
48 read stdin_var
49 echo "$(date) EOF on stdin" >> $basedir/$SERVERNAME.log
50 read pid < $basedir/$SERVERNAME.pid
51 echo "$(date) killing $pid" >> $basedir/$SERVERNAME.log
52 kill $pid 2> /dev/null
53 echo "$(date) exiting" >> $basedir/$SERVERNAME.log
54 exit 0