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