chiark / gitweb /
init/tripe-init.in, contrib/tripe-upstart.in: Reformat startup rune.
[tripe] / contrib / tripe-upstart.in
CommitLineData
2e35470f
MW
1### -*-conf-*-
2###
3### Upstart control script for tripe.
4
5###--------------------------------------------------------------------------
6### Identification.
7
8description "TrIPE virtual private network server"
9author "Mark Wooding <mdw@distorted.org.uk>"
10
11###--------------------------------------------------------------------------
12### Trigger conditions.
13
14start on stopped pymaemo-optify
15stop on starting shutdown
16
17respawn
18
19###--------------------------------------------------------------------------
20### Process environment.
21
22console none
23
24###--------------------------------------------------------------------------
25### Pre-flight check.
26
27pre-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
127end script
128
129###--------------------------------------------------------------------------
130### Main startup.
131
132script
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 \
2e35470f 147 -P$pidfile \
85b239ad
MW
148 -f${logfile-@logfile@} \
149 ${keytag+-S-t$keytag} \
150 ${addr+-S-b$addr} ${port+-S-p$port} \
151 ${user+-U$user} ${group+-G$group} \
152 ${sockmode+-S-m$sockmode} \
153 ${trace+-S-T$trace} \
154 ${tunnel+-S-n$tunnel} \
155 $miscopts
2e35470f
MW
156
157end script
158
159post-start script
160
161 [ -f @initconfig@ ] && . @initconfig@
162
163 : ${prefix=@prefix@} ${exec_prefix=@exec_prefix@}
164 : ${bindir=@bindir@} ${sbindir=@sbindir@}
165 : ${TRIPEDIR=@configdir@} ${TRIPESOCK=@socketdir@/tripesock}
166 : ${pidfile=@pidfile@}
167 : ${tripe=$sbindir/tripe} ${tripectl=$bindir/tripectl}
168
169 PATH=/usr/bin:/usr/sbin:/bin:/sbin:$bindir
170 export PATH TRIPEDIR TRIPESOCK TRIPE_SLIPIF
171
172 ## Wait for the server to start up. This doesn't usually take long.
173 for i in 1 2 3 4 give-up; do
174 $tripectl help >/dev/null 2>/dev/null && break
175 sleep 1
176 done
177
178 if [ $i = give-up ]; then
179 echo >&2 "Tripe server wouldn't start"
180 exit 1
181 fi
182
183 ## Start up the ancillary services.
184 [ -d $TRIPEDIR/services ] && for i in $TRIPEDIR/services/*; do
185 [ -x $i ] || continue
186 name=`basename $i`
187 case $name in *~|\#*) continue;; esac
188 $i --daemon --startup || :
189 done
190
191 ## Start up the statically configured peers.
192 [ -d $TRIPEDIR/peers ] && for i in $TRIPEDIR/peers/*; do
193 [ -x $i ] || continue
194 name=`basename $i`
195 case $name in *~|\#*) continue;; esac
196 $i || :
197 done
198
199end script
200
201###--------------------------------------------------------------------------
202### Shutdown.
203
204pre-stop script
205
206 [ -f /etc/default/tripe ] && . /etc/default/tripe
207
208 : ${prefix=/usr} ${exec_prefix=/usr}
209 : ${bindir=/usr/bin} ${sbindir=/usr/sbin}
210 : ${TRIPEDIR=/etc/tripe} ${TRIPESOCK=/var/run/tripesock}
211 : ${pidfile=/var/run/tripectl.pid}
212 : ${tripe=$sbindir/tripe} ${tripectl=$bindir/tripectl}
213
214 PATH=/usr/bin:/usr/sbin:/bin:/sbin:$bindir
215 export PATH TRIPEDIR TRIPESOCK TRIPE_SLIPIF
216
217 ## If there's no socket, it must have quit (probably nonviolently).
218 if test ! -S $TRIPESOCK; then
219 :
220
221 ## Ask it to die nicely.
222 elif $tripectl quit >/dev/null 2>&1; then
223 :
224
225 ## If there's no pidfile then tripectl presumably deleted it.
226 elif test ! -f $pidfile; then
227 rm -f $TRIPESOCK
228
229 ## Otherwise kill the process unpleasantly.
230 elif kill `cat $pidfile`; then
231 :
232 else
233 exit 1
234 fi
235
236end script
237
238###------ That's all, folks -------------------------------------------------