Debian Codegraph @

staprun/guest/stapshd.in

raw
1 #!/bin/bash
2 #
3 # chkconfig: - 90 10
4 # description: Starts up stapshd instances for each SystemTap virtio-serial \
5 # port discovered.
6 #
7 # Should end up in init dir
8
9 if [ -f /etc/rc.d/init.d/functions ]; then
10 . /etc/rc.d/init.d/functions
11 fi
12
13 STAPSHD=@libexecdir@/systemtap/stapsh-daemon
14 STAPSH=@bindir@/stapsh
15
16 PORT_BASENAME="/dev/virtio-ports/org.systemtap.stapsh"
17
18 # Prints paths to available SystemTap ports and returns the number of ports
19 ports() {
20 count=0
21 for port in $PORT_BASENAME.*; do
22 if [[ $port =~ $PORT_BASENAME.[0-9]+ ]]; then
23 echo "$port"
24 count=$(expr $count + 1)
25 fi
26 done
27 return $count
28 }
29
30 count_missing() {
31 missing=0
32 for port in $(ports); do
33 if [ ! -f "/run/stapshd.$(basename $port).pid" ]; then
34 missing=$(expr $missing + 1)
35 fi
36 done
37 return $missing
38 }
39
40 # Start stapshd instances
41 start() {
42 prts=$(ports)
43 if [ $? -eq 0 ]; then
44 echo "no ports detected" >&2
45 # This shouldn't be an error
46 return 0
47 fi
48 count_missing
49 if [ $? -eq 0 ]; then
50 echo "already running" >&2
51 return 0
52 fi
53 for port in $prts; do
54 pidfile="/run/stapshd.$(basename $port).pid"
55 if [ ! -f "$pidfile" ]; then
56 $STAPSHD "$port" &>/dev/null &
57 echo $! > $pidfile
58 fi
59 done
60 return 0
61 }
62
63 # First stops stapshd instances and then the stapsh instances
64 stop() {
65 for pidfile in /run/stapshd.$(basename $PORT_BASENAME).*.pid; do
66 if [ -f "$pidfile" ]; then
67 killproc -p $pidfile stapshd
68 fi
69 # Delete PID file if killproc didn't do it
70 if [ -f "$pidfile" ]; then
71 rm $pidfile
72 fi
73 done
74 for pidfile in /run/stapsh.$(basename $PORT_BASENAME).*.pid; do
75 if [ -f "$pidfile" ]; then
76 killproc -p $pidfile stapsh
77 fi
78 # Delete PID file if killproc didn't do it
79 if [ -f "$pidfile" ]; then
80 rm $pidfile
81 fi
82 done
83 return 0
84 }
85
86 # Only restart if we're running
87 condrestart() {
88 if [ -f "/run/stapshd.$(basename $PORT_BASENAME).*.pid" ]; then
89 stop
90 start
91 fi
92 return 0
93 }
94
95 # Check if there's a stapshd instance for each port
96 status() {
97 prts=$(ports)
98 if [ $? -eq 0 ]; then
99 echo "not running" >&2
100 return 3
101 fi
102 count_missing
103 if [ $? -eq 0 ]; then
104 echo "running"
105 return 0
106 else
107 echo "missing $missing stapshd instances"
108 return 3
109 fi
110 }
111
112 # Stops any stapshd instances that are still running for a non-existent port
113 # and starts stapshd instances for any port that doesn't have one
114 reload() {
115 for pidfile in /run/stapshd.$(basename $PORT_BASENAME).*.pid; do
116 chardev=$(basename $pidfile .pid) # trim /run and .pid
117 chardev=${chardev#stapshd.} # trim leading 'stapshd.'
118 if [ -f "$pidfile" -a ! -c "/dev/virtio-ports/$chardev" ]; then
119 killproc -p $pidfile stapshd
120 # Delete PID file if killproc didn't do it
121 if [ -f "$pidfile" ]; then
122 rm $pidfile
123 fi
124 fi
125 done
126 for pidfile in /run/stapsh.$(basename $PORT_BASENAME).*.pid; do
127 chardev=$(basename $pidfile .pid) # trim /run and .pid
128 chardev=${chardev#stapsh.} # trim leading 'stapsh.'
129 if [ -f "$pidfile" -a ! -c "/dev/virtio-ports/$chardev" ]; then
130 killproc -p $pidfile stapsh
131 # Delete PID file if killproc didn't do it
132 if [ -f "$pidfile" ]; then
133 rm $pidfile
134 fi
135 fi
136 done
137 start
138 }
139
140 # Main logic
141 case "$1" in
142 start)
143 start
144 ;;
145 stop)
146 stop
147 ;;
148 status)
149 status
150 ;;
151 restart)
152 stop
153 start
154 ;;
155 condrestart|try-restart)
156 condrestart
157 ;;
158 reload)
159 reload
160 ;;
161 *)
162 echo "Usage: $0 {start|stop|restart|reload|condrestart|status}"
163 exit 1
164 esac
165 exit $?