cad152660dc5ac315d63ea961574579b995b3b06
6 *) echo >&2 "usage: $0 {start|stop|restart|status} HOST"; exit 1 ;;
12 echo "$*" >"$file.new"
13 mv "$file.new" "$file"
16 runssh
() { ssh -T
-oControlPath
="./$host.ctrl" "$@"; }
20 ## Initial shutdown protocol.
21 writefile
"$host.state" stopping
22 if [ -f
"$host.pid" ]; then kill $
(cat "$host.pid") 2>/dev
/null ||
:; fi
25 ## Shut down an existing connection if there is one.
26 if [ -S
"$host.ctrl" ]; then
27 runssh
-Oexit
"$host" >/dev
/null
2>/dev
/null ||
:
30 ## If there's still a socket, then work out what to do.
31 if [ -e
"$host.ctrl" ]; then
33 ## If the connection's still running then we have a problem.
34 if runssh
-Ocheck
"$host" >/dev
/null
2>/dev
/null
; then
35 echo >&2 "$0: failed to kill existing connection to $host"
39 ## Remove the stale socket.
44 rm -f
"$host.state" "$host.pid"
49 ## There doesn't seem to be a better way of getting this. :-(
50 read pid
<"$host.daemonpipe"
51 rm -f
"$host.daemonpipe"
54 trap 'rm -f "$host.pid"; stopit' EXIT INT TERM
55 writefile
"$host.pid" "$pid"
60 ## Keep the connection up for as long as we can.
61 while [ -f
"$host.pid" ]; do
63 ## Maybe back off before trying another connection.
69 writefile
"$host.state" \
70 "wait until $(date -d+${delay}sec +"%Y-
%m-
%d
%H
:%M
:%S
%z
")"
73 if [ $delay -gt
120 ]; then delay
=120; fi
77 ## Start a new connection.
78 writefile
"$host.state" starting
79 if ! runssh
-MNnf
"$host" >/dev
/null
; then continue; fi
80 if ! runssh
-Ocheck
"$host" >/dev
/null
2>&1; then
81 echo "connection to $host apparently stillborn"
84 writefile
"$host.state" connected
87 ## Wait until it gets torn down. The chicanery with a pipe is because
88 ## the ssh process will continue until either it gets disconnected from
89 ## the server or stdin closes -- so we have to arrange that stdin doesn't
90 ## close. Thanks to Richard Kettlewell for the suggestion.
91 rm -f
"$host.pipe"; mkfifo -m400
"$host.pipe"
92 runssh
-N
"$host" >/dev
/null
<"$host.pipe" ||
:
94 writefile
"$host.state" disconnected
100 ## If there's already a connection then we have nothing to do.
101 if runssh
-Ocheck
"$host" >/dev
/null
2>/dev
/null
; then
102 echo >&2 "$0: already connected to $host"
106 ## Start a daemon which makes connections for us. This is remarkably
108 rm -f
"$host.daemonpipe"; mkfifo -m600
"$host.daemonpipe"
109 { daemon
& echo $
! >"$host.daemonpipe"; } 2>&1 | logger
-pdaemon.notice
&
116 restart
) stopit
; startit
;;
118 if [ -f
"$host.state" ]
119 then cat "$host.state"
124 echo >&2 "usage: $0 {start|stop|restart|status} HOST"