chiark / gitweb /
contrib/knock.in: Login script for establishing dynamic associations.
[tripe] / contrib / tripe-upstart.in
1 ### -*-conf-*-
2 ###
3 ### Upstart control script for tripe.
4
5 ###--------------------------------------------------------------------------
6 ### Identification.
7
8 description "TrIPE virtual private network server"
9 author "Mark Wooding <mdw@distorted.org.uk>"
10
11 ###--------------------------------------------------------------------------
12 ### Trigger conditions.
13
14 start on stopped pymaemo-optify
15 stop on starting shutdown
16
17 respawn
18
19 ###--------------------------------------------------------------------------
20 ### Process environment.
21
22 console none
23
24 ###--------------------------------------------------------------------------
25 ### Pre-flight check.
26
27 pre-start script
28
29         [ -f @initconfig@ ] && . @initconfig@
30
31         : ${prefix=@prefix@} ${exec_prefix=@exec_prefix@}
32         : ${bindir=@bindir@} ${sbindir=@sbindir@}
33         : ${TRIPEDIR=@configdir@} ${TRIPESOCK=@socketdir@/tripesock}
34         : ${pidfile=@pidfile@}
35         : ${tripe=$sbindir/tripe} ${tripectl=$bindir/tripectl}
36
37         PATH=/usr/bin:/usr/sbin:/bin:/sbin:$bindir
38         export PATH TRIPEDIR TRIPESOCK TRIPE_SLIPIF
39
40         ## Give up if there's no binary.
41         if test ! -x "$tripe" || test ! -x "$tripectl"; then
42           echo >&2 "Not starting/stopping TrIPE: binary files missing"
43           exit 1
44         fi
45
46         ## Give up if there's no key.
47         if test ! -f "$TRIPEDIR/keyring" ||
48            test ! -f "$TRIPEDIR/keyring.pub"; then
49           echo >&2 "Not starting/stopping TrIPE: keyring files missing"
50           exit 1
51         fi
52
53         ## Check it will work, or at least stands a fighting chance.
54         ##
55         ## (Having loads of different tunnel types doesn't help any.)
56         case ${tunnel-`$tripe --tunnels | head -1`},`uname -s` in
57
58           ## Linux TUN/TAP.
59           linux,Linux)
60             if { test -f /proc/misc && grep -q tun /proc/misc; } ||
61                modprobe -q tun; then
62               : good
63             else
64               echo >&2 "$tripe needs the Linux TUN/TAP driver to run."
65               exit 1
66             fi
67             if test -c /dev/net/tun; then
68               : good
69             else
70               echo >&2 "$tripe needs /dev/net/tun, which is missing."
71               exit 1
72             fi
73             ;;
74
75           ## Linux Unet (obsolete).
76           unet,Linux)
77             if { test -f /proc/devices && grep -q unet /proc/devices; } ||
78                modprobe -q unet; then
79               : good
80             else
81               echo >&2 "$tripe needs the Linux UNET driver to run."
82               exit 1
83             fi
84             if test -c /dev/unet; then
85               : good
86             else
87               echo >&2 "$tripe needs /dev/unet, which is missing."
88               exit 1
89             fi
90             ;;
91
92           ## BSD tun.
93           bsd,*BSD)
94             ## Don't know how to check the device is working.  Check the
95             ## device file exists and hope for the best.
96             if test -c /dev/tun0; then
97               : good
98             else
99               echo >&2 "$tripe needs /dev/tun0, which is missing."
100               exit 1
101             fi
102             ;;
103
104           ## SLIP.
105           slip,*)
106             if test "$TRIPE_SLIPIF" = ""; then
107               echo >&2 "$tripe needs SLIP interfaces set up!"
108               exit 1
109             fi
110             ;;
111
112           ## Various kinds of misconfiguration.
113           linux,* | unet,*)
114             echo >&2 "CONFIGURATION ERROR"
115             echo >&2 "  $tripe is compiled to use a Linux tunnel device, but"
116             echo >&2 "  this system is `uname -s`"
117             exit 1
118             ;;
119           bsd,*)
120             echo >&2 "CONFIGURATION ERROR"
121             echo >&2 "  $tripe is compiled to use a BSD tunnel device, but"
122             echo >&2 "  this system is `uname -s`"
123             exit 1
124             ;;
125         esac
126
127 end script
128
129 ###--------------------------------------------------------------------------
130 ### Main startup.
131
132 script
133
134         [ -f @initconfig@ ] && . @initconfig@
135
136         : ${prefix=@prefix@} ${exec_prefix=@exec_prefix@}
137         : ${bindir=@bindir@} ${sbindir=@sbindir@}
138         : ${TRIPEDIR=@configdir@} ${TRIPESOCK=@socketdir@/tripesock}
139         : ${pidfile=@pidfile@}
140         : ${tripe=$sbindir/tripe} ${tripectl=$bindir/tripectl}
141
142         PATH=/usr/bin:/usr/sbin:/bin:/sbin:$bindir
143         export PATH TRIPEDIR TRIPESOCK TRIPE_SLIPIF
144
145         ## Start the server, passing lots of arguments.
146         $tripectl -s -p$tripe \
147           -f${logfile-/var/log/tripe.log} \
148           -P$pidfile \
149           ${keytag+-S-t}$keytag \
150           ${addr+-S-b}$addr \
151           ${port+-S-p}${port} \
152           ${user+-U}${user} \
153           ${group+-G}${group} \
154           ${trace+-S-T}${trace} \
155           ${tunnel+-S-n}${tunnel} \
156           ${miscopts}
157
158 end script
159
160 post-start script
161
162         [ -f @initconfig@ ] && . @initconfig@
163
164         : ${prefix=@prefix@} ${exec_prefix=@exec_prefix@}
165         : ${bindir=@bindir@} ${sbindir=@sbindir@}
166         : ${TRIPEDIR=@configdir@} ${TRIPESOCK=@socketdir@/tripesock}
167         : ${pidfile=@pidfile@}
168         : ${tripe=$sbindir/tripe} ${tripectl=$bindir/tripectl}
169
170         PATH=/usr/bin:/usr/sbin:/bin:/sbin:$bindir
171         export PATH TRIPEDIR TRIPESOCK TRIPE_SLIPIF
172
173         ## Wait for the server to start up.  This doesn't usually take long.
174         for i in 1 2 3 4 give-up; do
175           $tripectl help >/dev/null 2>/dev/null && break
176           sleep 1
177         done
178
179         if [ $i = give-up ]; then
180           echo >&2 "Tripe server wouldn't start"
181           exit 1
182         fi
183
184         ## Start up the ancillary services.
185         [ -d $TRIPEDIR/services ] && for i in $TRIPEDIR/services/*; do
186           [ -x $i ] || continue
187           name=`basename $i`
188           case $name in *~|\#*) continue;; esac
189           $i --daemon --startup || :
190         done
191
192         ## Start up the statically configured peers.
193         [ -d $TRIPEDIR/peers ] && for i in $TRIPEDIR/peers/*; do
194           [ -x $i ] || continue
195           name=`basename $i`
196           case $name in *~|\#*) continue;; esac
197           $i || :
198         done
199
200 end script
201
202 ###--------------------------------------------------------------------------
203 ### Shutdown.
204
205 pre-stop script
206
207         [ -f /etc/default/tripe ] && . /etc/default/tripe
208
209         : ${prefix=/usr} ${exec_prefix=/usr}
210         : ${bindir=/usr/bin} ${sbindir=/usr/sbin}
211         : ${TRIPEDIR=/etc/tripe} ${TRIPESOCK=/var/run/tripesock}
212         : ${pidfile=/var/run/tripectl.pid}
213         : ${tripe=$sbindir/tripe} ${tripectl=$bindir/tripectl}
214
215         PATH=/usr/bin:/usr/sbin:/bin:/sbin:$bindir
216         export PATH TRIPEDIR TRIPESOCK TRIPE_SLIPIF
217
218         ## If there's no socket, it must have quit (probably nonviolently).
219         if test ! -S $TRIPESOCK; then
220           :
221
222         ## Ask it to die nicely.
223         elif $tripectl quit >/dev/null 2>&1; then
224           :
225
226         ## If there's no pidfile then tripectl presumably deleted it.
227         elif test ! -f $pidfile; then
228           rm -f $TRIPESOCK
229
230         ## Otherwise kill the process unpleasantly.
231         elif kill `cat $pidfile`; then
232           :
233         else
234           exit 1
235         fi
236
237 end script
238
239 ###------ That's all, folks -------------------------------------------------