Debian Codegraph @

staprun/guest/stapsh-daemon.in

raw
1 #!/bin/bash
2 # Keeps respawning stapsh on the given port
3 # External programs used: basename, date, expr (all in coreutils)
4 # Must be in libexec dir
5
6 STAPSH=@bindir@/stapsh
7
8 # Make sure we were given a port
9 if [ ! -n "$1" ]; then
10 echo "Usage: `basename $0` <port>" >&2
11 exit 1
12 fi
13
14 STAPSH_PID=/run/stapsh.$(basename $1).pid
15 STAPSHD_PID=/run/stapshd.$(basename $1).pid
16
17 # Make sure the port exists
18 if [ ! -c "$1" ]; then
19 echo "Argument $1 is not a character device" >&2
20 if [ -f "$STAPSHD_PID" ]; then
21 rm "$STAPSHD_PID"
22 fi
23 exit 2
24 fi
25
26 # Returns the time in seconds
27 now() {
28 echo $(date "+%s")
29 }
30
31 # Returns the time elapsed in seconds since arg
32 elapsed() {
33 echo $(expr $(now) - $1)
34 }
35
36 # If stapsh is restarted more than 5 times in less than 1 second, we abort
37 last=$(now)
38 respawns=0
39 while [ true ]; do
40 if [ "$(elapsed $last)" -gt 1 ]; then
41 last=$(now)
42 respawns=0
43 elif [ "$respawns" -gt 5 ]; then
44 echo "Too many respawns... aborting" >&2
45 rm "$STAPSH_PID"
46 if [ -f "$STAPSHD_PID" ]; then
47 rm "$STAPSHD_PID"
48 fi
49 exit 3
50 elif [ ! -c "$1" ]; then
51 echo "Port no longer available" >&2
52 if [ -f "$STAPSHD_PID" ]; then
53 rm "$STAPSHD_PID"
54 fi
55 exit 4
56 fi
57 $STAPSH -l "$1" &>/dev/null &
58 pid=$!
59 echo $pid > $STAPSH_PID
60 wait $pid
61 respawns=$(expr $respawns + 1)
62 done
63