chiark / gitweb /
doc/.gitignore: Make the patterns more general.
[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         logopt=
147         case ${syslogp-nil} in nil) ;; *) logopt="$logopt -l" ;; esac
148         case ${logfile+t},${syslogp-nil} in
149           t,*) logopt="$logopt -f$logfile" ;;
150           ,nil) logopt="$logopt -f@logfile@" ;;
151         esac
152         $tripectl -s -p$tripe \
153           -P$pidfile \
154           $logopt \
155           ${keytag+-S-t$keytag} \
156           ${addr+-S-b$addr} ${port+-S-p$port} \
157           ${user+-U$user} ${group+-G$group} \
158           ${sockmode+-S-m$sockmode} \
159           ${trace+-S-T$trace} \
160           ${tunnel+-S-n$tunnel} \
161           $miscopts
162
163 end script
164
165 post-start script
166
167         [ -f @initconfig@ ] && . @initconfig@
168
169         : ${prefix=@prefix@} ${exec_prefix=@exec_prefix@}
170         : ${bindir=@bindir@} ${sbindir=@sbindir@}
171         : ${TRIPEDIR=@configdir@} ${TRIPESOCK=@socketdir@/tripesock}
172         : ${pidfile=@pidfile@}
173         : ${tripe=$sbindir/tripe} ${tripectl=$bindir/tripectl}
174
175         PATH=/usr/bin:/usr/sbin:/bin:/sbin:$bindir
176         export PATH TRIPEDIR TRIPESOCK TRIPE_SLIPIF
177
178         ## Wait for the server to start up.  This doesn't usually take long.
179         for i in 1 2 3 4 give-up; do
180           $tripectl help >/dev/null 2>/dev/null && break
181           sleep 1
182         done
183
184         if [ $i = give-up ]; then
185           echo >&2 "Tripe server wouldn't start"
186           exit 1
187         fi
188
189         ## Start up the ancillary services.
190         [ -d $TRIPEDIR/services ] && for i in $TRIPEDIR/services/*; do
191           [ -x $i ] || continue
192           name=`basename $i`
193           case $name in *~|\#*) continue;; esac
194           $i --daemon --startup || :
195         done
196
197         ## Start up the statically configured peers.
198         [ -d $TRIPEDIR/peers ] && for i in $TRIPEDIR/peers/*; do
199           [ -x $i ] || continue
200           name=`basename $i`
201           case $name in *~|\#*) continue;; esac
202           $i || :
203         done
204
205 end script
206
207 ###--------------------------------------------------------------------------
208 ### Shutdown.
209
210 pre-stop script
211
212         [ -f /etc/default/tripe ] && . /etc/default/tripe
213
214         : ${prefix=/usr} ${exec_prefix=/usr}
215         : ${bindir=/usr/bin} ${sbindir=/usr/sbin}
216         : ${TRIPEDIR=/etc/tripe} ${TRIPESOCK=/var/run/tripesock}
217         : ${pidfile=/var/run/tripectl.pid}
218         : ${tripe=$sbindir/tripe} ${tripectl=$bindir/tripectl}
219
220         PATH=/usr/bin:/usr/sbin:/bin:/sbin:$bindir
221         export PATH TRIPEDIR TRIPESOCK TRIPE_SLIPIF
222
223         ## If there's no socket, it must have quit (probably nonviolently).
224         if test ! -S $TRIPESOCK; then
225           :
226
227         ## Ask it to die nicely.
228         elif $tripectl quit >/dev/null 2>&1; then
229           :
230
231         ## If there's no pidfile then tripectl presumably deleted it.
232         elif test ! -f $pidfile; then
233           rm -f $TRIPESOCK
234
235         ## Otherwise kill the process unpleasantly.
236         elif kill `cat $pidfile`; then
237           :
238         else
239           exit 1
240         fi
241
242 end script
243
244 ###------ That's all, folks -------------------------------------------------