144
by Dimitri John Ledkov
auto update |
1 |
#!/bin/sh
|
2 |
#
|
|
3 |
# Copyright (c) 2007 Javier Fernandez-Sanguino <jfs@debian.org>
|
|
4 |
# Modified by Andrew Shadura <bugzilla@tut.by>
|
|
5 |
#
|
|
6 |
# This is free software; you may redistribute it and/or modify
|
|
7 |
# it under the terms of the GNU General Public License as
|
|
8 |
# published by the Free Software Foundation; either version 2,
|
|
9 |
# or (at your option) any later version.
|
|
10 |
#
|
|
11 |
# This is distributed in the hope that it will be useful, but
|
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 |
# GNU General Public License for more details.
|
|
15 |
#
|
|
16 |
# You should have received a copy of the GNU General Public License with
|
|
17 |
# the Debian operating system, in /usr/share/common-licenses/GPL; if
|
|
18 |
# not, write to the Free Software Foundation, Inc., 59 Temple Place,
|
|
19 |
# Suite 330, Boston, MA 02111-1307 USA
|
|
20 |
#
|
|
21 |
### BEGIN INIT INFO
|
|
22 |
# Provides: tayga
|
|
23 |
# Required-Start: $network $local_fs $remote_fs
|
|
24 |
# Required-Stop: $remote_fs
|
|
25 |
# Should-Start:
|
|
26 |
# Should-Stop:
|
|
27 |
# Default-Start: 2 3 4 5
|
|
28 |
# Default-Stop: 0 1 6
|
|
29 |
# Short-Description: userspace NAT64
|
|
30 |
# Description: TAYGA is a stateless NAT64 userspace daemon. Using the
|
|
31 |
# in-kernel TUN network driver, TAYGA receives IPv4 and
|
|
32 |
# IPv6 packets, translates them to the other protocol,
|
|
33 |
# and sends the translated packets back using the same
|
|
34 |
# TUN interface.
|
|
35 |
### END INIT INFO
|
|
36 |
||
37 |
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
|
38 |
||
39 |
DAEMON=/usr/sbin/tayga # Introduce the server's location here |
|
40 |
NAME=tayga # Introduce the short server's name here |
|
41 |
DESC="userspace NAT64" # Introduce a short description here |
|
42 |
||
43 |
PIDFILE=/var/run/$NAME.pid |
|
44 |
||
45 |
test -x "$DAEMON" || exit 0 |
|
46 |
||
47 |
. /lib/lsb/init-functions |
|
48 |
||
49 |
# Default options, these can be overriden by the information
|
|
50 |
# at /etc/default/$NAME
|
|
51 |
DAEMON_OPTS="--nodetach" # Additional options given to the server |
|
52 |
||
53 |
DIETIME=10 # Time to wait for the server to die, in seconds |
|
54 |
# If this value is set too low you might not
|
|
55 |
# let some servers to die gracefully and
|
|
56 |
# 'restart' will not work
|
|
57 |
||
58 |
STARTTIME=2 # Time to wait for the server to start, in seconds |
|
59 |
# If this value is set each time the server is
|
|
60 |
# started (on start or restart) the script will
|
|
61 |
# stall to try to determine if it is running
|
|
62 |
# If it is not set and the server takes time
|
|
63 |
# to setup a pid file the log message might
|
|
64 |
# be a false positive (says it did not start
|
|
65 |
# when it actually did)
|
|
66 |
||
67 |
TUN_DEVICE=$(sed -rn "/^[ \t]*tun-device/s/^[ \t]*tun-device[ \t]+//p" /etc/tayga.conf) |
|
68 |
IPV6_PREFIX=$(sed -rn "/^[ \t]*prefix/s/^[ \t]*prefix[ \t]+//p" /etc/tayga.conf) |
|
69 |
DYNAMIC_POOL=$(sed -rn "/^[ \t]*dynamic-pool/s/^[ \t]*dynamic-pool[ \t]+//p" /etc/tayga.conf) |
|
70 |
CONFIGURE_IFACE="no" |
|
71 |
CONFIGURE_NAT44="no" |
|
72 |
||
73 |
# Include defaults if available
|
|
74 |
if [ -f "/etc/default/$NAME" ] ; then |
|
75 |
. "/etc/default/$NAME" |
|
76 |
fi
|
|
77 |
||
78 |
if [ "$RUN" != "yes" ] ; then |
|
79 |
log_failure_msg "$NAME disabled, please adjust the configuration to your needs " |
|
80 |
log_failure_msg "and then set RUN to 'yes' in /etc/default/$NAME to enable it." |
|
81 |
exit 0 |
|
82 |
fi
|
|
83 |
||
84 |
set -e
|
|
85 |
||
86 |
running_pid() { |
|
87 |
# Check if a given process pid's cmdline matches a given name
|
|
88 |
pid="$1" |
|
89 |
name="$2" |
|
90 |
[ -z "$pid" ] && return 1 |
|
91 |
[ ! -d "/proc/$pid" ] && return 1 |
|
92 |
return 0 |
|
93 |
}
|
|
94 |
||
95 |
running() { |
|
96 |
# Check if the process is running looking at /proc
|
|
97 |
# (works for all users)
|
|
98 |
||
99 |
# No pidfile, probably no daemon present
|
|
100 |
[ ! -f "$PIDFILE" ] && return 1 |
|
101 |
pid=$(cat $PIDFILE) |
|
102 |
running_pid "$pid" "$DAEMON" || return 1 |
|
103 |
return 0 |
|
104 |
}
|
|
105 |
||
106 |
start_server() { |
|
107 |
if [ "$CONFIGURE_IFACE" = "yes" ] ; then |
|
108 |
"$DAEMON" --mktun | logger -t "$NAME" -i |
|
109 |
ip link set "$TUN_DEVICE" up |
|
110 |
[ -n "$DYNAMIC_POOL" ] && ip route add "$DYNAMIC_POOL" dev "$TUN_DEVICE" |
|
111 |
[ -n "$IPV6_PREFIX" ] && ip route add "$IPV6_PREFIX" dev "$TUN_DEVICE" |
|
112 |
[ -n "$IPV4_TUN_ADDR" ] && ip addr add "$IPV4_TUN_ADDR" dev "$TUN_DEVICE" |
|
113 |
[ -n "$IPV6_TUN_ADDR" ] && ip addr add "$IPV6_TUN_ADDR" dev "$TUN_DEVICE" |
|
114 |
fi
|
|
115 |
[ "$CONFIGURE_NAT44" = "yes" ] && [ -n "$DYNAMIC_POOL" ] && iptables -t nat -A POSTROUTING -s "$DYNAMIC_POOL" -j MASQUERADE || true |
|
116 |
||
117 |
start-stop-daemon --start --quiet \
|
|
118 |
-b --exec "$DAEMON" -- --pidfile "$PIDFILE" $DAEMON_OPTS |
|
119 |
}
|
|
120 |
||
121 |
stop_server() { |
|
122 |
start-stop-daemon --stop --quiet --pidfile "$PIDFILE" |
|
123 |
errcode=$? |
|
124 |
if [ "$CONFIGURE_IFACE" = "yes" ] ; then |
|
125 |
ip link set "$TUN_DEVICE" down |
|
126 |
"$DAEMON" --rmtun | logger -t "$NAME" -i |
|
127 |
fi
|
|
128 |
[ "$CONFIGURE_NAT44" = "yes" ] && [ -n "$DYNAMIC_POOL" ] && iptables -t nat -D POSTROUTING -s "$DYNAMIC_POOL" -j MASQUERADE || true |
|
129 |
rm -f "$PIDFILE" |
|
130 |
||
131 |
return $errcode |
|
132 |
}
|
|
133 |
||
134 |
force_stop() { |
|
135 |
# Force the process to die killing it manually
|
|
136 |
[ ! -e "$PIDFILE" ] && return |
|
137 |
if running ; then |
|
138 |
kill -15 $pid |
|
139 |
# Is it really dead?
|
|
140 |
sleep "$DIETIME"s |
|
141 |
if running ; then |
|
142 |
kill -9 $pid |
|
143 |
sleep "$DIETIME"s |
|
144 |
if running ; then |
|
145 |
echo "Cannot kill $NAME (pid=$pid)!" |
|
146 |
exit 1 |
|
147 |
fi
|
|
148 |
fi
|
|
149 |
fi
|
|
150 |
rm -f "$PIDFILE" |
|
151 |
}
|
|
152 |
||
153 |
||
154 |
case "$1" in |
|
155 |
start)
|
|
156 |
log_daemon_msg "Starting $DESC" "$NAME" |
|
157 |
# Check if it's running first
|
|
158 |
if running ; then |
|
159 |
log_progress_msg "apparently already running"
|
|
160 |
log_end_msg 0
|
|
161 |
exit 0 |
|
162 |
fi
|
|
163 |
if start_server ; then |
|
164 |
# NOTE: Some servers might die some time after they start,
|
|
165 |
# this code will detect this issue if STARTTIME is set
|
|
166 |
# to a reasonable value
|
|
167 |
[ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time |
|
168 |
if running ; then |
|
169 |
# It's ok, the server started and is running
|
|
170 |
log_end_msg 0
|
|
171 |
else
|
|
172 |
# It is not running after we did start
|
|
173 |
log_end_msg 1
|
|
174 |
fi
|
|
175 |
else
|
|
176 |
# Either we could not start it
|
|
177 |
log_end_msg 1
|
|
178 |
fi
|
|
179 |
;;
|
|
180 |
stop)
|
|
181 |
log_daemon_msg "Stopping $DESC" "$NAME" |
|
182 |
if running ; then |
|
183 |
# Only stop the server if we see it running
|
|
184 |
errcode=0 |
|
185 |
stop_server || errcode=$? |
|
186 |
log_end_msg $errcode
|
|
187 |
else
|
|
188 |
# If it's not running don't do anything
|
|
189 |
log_progress_msg "apparently not running"
|
|
190 |
log_end_msg 0
|
|
191 |
exit 0 |
|
192 |
fi
|
|
193 |
;;
|
|
194 |
force-stop)
|
|
195 |
# First try to stop gracefully the program
|
|
196 |
$0 stop
|
|
197 |
if running; then |
|
198 |
# If it's still running try to kill it more forcefully
|
|
199 |
log_daemon_msg "Stopping (force) $DESC" "$NAME" |
|
200 |
errcode=0 |
|
201 |
force_stop || errcode=$? |
|
202 |
log_end_msg $errcode
|
|
203 |
fi
|
|
204 |
;;
|
|
205 |
restart|force-reload) |
|
206 |
log_daemon_msg "Restarting $DESC" "$NAME" |
|
207 |
errcode=0 |
|
208 |
stop_server || errcode=$? |
|
209 |
# Wait some sensible amount, some server need this
|
|
210 |
[ -n "$DIETIME" ] && sleep $DIETIME |
|
211 |
start_server || errcode=$? |
|
212 |
[ -n "$STARTTIME" ] && sleep $STARTTIME |
|
213 |
running || errcode=$? |
|
214 |
log_end_msg $errcode
|
|
215 |
;;
|
|
216 |
status)
|
|
217 |
||
218 |
log_daemon_msg "Checking status of $DESC" "$NAME" |
|
219 |
if running ; then |
|
220 |
log_progress_msg "running"
|
|
221 |
log_end_msg 0
|
|
222 |
else
|
|
223 |
log_progress_msg "apparently not running"
|
|
224 |
log_end_msg 1
|
|
225 |
exit 1 |
|
226 |
fi
|
|
227 |
;;
|
|
228 |
# Use this if the daemon cannot reload
|
|
229 |
reload)
|
|
230 |
log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon" |
|
231 |
log_warning_msg "cannot re-read the config file (use restart)."
|
|
232 |
;;
|
|
233 |
*)
|
|
234 |
N=/etc/init.d/$NAME |
|
235 |
echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2 |
|
236 |
exit 1 |
|
237 |
;;
|
|
238 |
esac
|
|
239 |
||
240 |
exit 0 |