updated on Thu Jan 19 12:17:07 UTC 2012
[aur-mirror.git] / chaosvpn / chaosvpn.rc.d
blob9a5726ad5f4910e309d855cfd30b2d8932e04e08
1 #!/bin/bash
3 . /etc/rc.conf
4 . /etc/rc.d/functions
6 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
8 DAEMON=/usr/sbin/chaosvpn # Introduce the server's location here
9 TINCCTL=/usr/sbin/tincctl
10 NAME=chaosvpn # Introduce the short server's name here
11 DESC="Agora Link" # Introduce a short description here
13 PIDFILE="overridden later"
15 test -x $DAEMON || exit 0
17 # Default options, these can be overriden by the information
18 # at /etc/conf.d/$NAME
19 DAEMON_OPTS="-a" # Additional options given to the server
20 # -a == do not run as daemon, onetime fetch+update of config and tincd
21 # restart
23 DIETIME=10 # Time to wait for the server to die, in seconds
24 # If this value is set too low you might not
25 # let some servers to die gracefully and
26 # 'restart' will not work
28 STARTTIME=1 # Time to wait for the server to start, in seconds
29 # If this value is set each time the server is
30 # started (on start or restart) the script will
31 # stall to try to determine if it is running
32 # If it is not set and the server takes time
33 # to setup a pid file the log message might
34 # be a false positive (says it did not start
35 # when it actually did)
37 DAEMONUSER="" # Users to run the daemons as. If this value
38 # is set start-stop-daemon will chuid the server
40 CONFIGS="chaosvpn.conf" # which network config to startup
43 # Include defaults if available
44 if [ -f /etc/conf.d/$NAME ] ; then
45 . /etc/conf.d/$NAME
48 # Use this if you want the user to explicitly set 'RUN' in
49 # /etc/conf.d/
50 if [ "x$RUN" != "xyes" ] ; then
51 echo "$NAME disabled, please adjust the configuration to your needs "
52 echo "and then set RUN to 'yes' in /etc/conf.d/$NAME to enable it."
53 exit 0
56 # Check that the user exists (if we set a user)
57 # Does the user exist?
58 if [ -n "$DAEMONUSER" ] ; then
59 if getent passwd | grep -q "^$DAEMONUSER:"; then
60 # Obtain the uid and gid
61 DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'`
62 DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'`
63 else
64 echo "The user $DAEMONUSER, required to run $NAME does not exist."
65 exit 0
70 running_pid() {
71 # Check if a given process pid's cmdline matches a given name
72 local pid=$1
73 local name=$2
74 [ -z "$pid" ] && return 1
75 [ ! -d /proc/$pid ] && return 1
76 local cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
77 # Is this the expected server
78 [ "$cmd" != "$name" ] && return 1
79 return 0
82 start_server() {
83 local errcode
84 # Start the process using the wrapper
85 if [ -z "$DAEMONUSER" ] ; then
86 $DAEMON $DAEMON_OPTS >/dev/null
87 errcode=$?
88 else
89 # if we are using a daemonuser then change the user id
90 su -s '/bin/sh' $DAEMONUSER -c "$DAEMON $DAEMON_OPTS"
91 errcode=$?
93 return $errcode
96 extract_configinfos() {
97 local config="$1"
98 PIDFILE=""
99 DAEMON_OPTS="--help"
100 networkname=""
102 [ -f "/etc/tinc/$config" ] || return 1
104 # TODO: replace the following with something smaller like sed/busybox sed
105 networkname=$( perl -ne 's/^.*?\$networkname\s*=\s*\"(.*?)\".*$/$1/ && print $_' <"/etc/tinc/$config" )
106 [ -z "$networkname" ] && return 1
108 PIDFILE="/var/run/tinc.$networkname.pid"
109 DAEMON_OPTS="-a -c /etc/tinc/$config"
110 return 0;
113 start_configs() {
114 local mode="$1"
115 local errcode
116 local config
117 for config in $CONFIGS ; do
118 if [ "$mode" = "reload" ] ; then
119 stat_busy "Reloading $DESC: $config"
120 else
121 stat_busy "Starting $DESC: $config"
124 if extract_configinfos "$config" ; then
125 start_server && stat_done || stat_fail
127 done
129 add_daemon $NAME
132 stop_configs() {
133 local config
134 for config in $CONFIGS ; do
135 stat_busy "Stopping $DESC " "$config"
136 extract_configinfos "$config"
137 if [ -x "$TINCCTL" ] ; then
138 # tincctl exists since tinc 1.1-git
139 "$TINCCTL" -n "$networkname" stop 2>/dev/null && stat_done || stat_fail
140 else
141 if [ -f "$PIDFILE" ] ; then
142 kill `cat $PIDFILE` && stat_done || stat_fail
143 else
144 stat_append "apparently not running"
145 stat_fail
148 done
150 rm_daemon $NAME
152 return 0
156 case "$1" in
157 start)
158 start_configs
160 stop)
161 stop_configs
163 force-stop)
164 stop_configs
166 restart|force-reload)
167 stop_configs && start_configs
169 # status)
170 # echo "Checking status of $DESC" "$NAME"
171 # if running ; then
172 # log_progress_msg "running"
173 # log_end_msg 0
174 # else
175 # log_progress_msg "apparently not running"
176 # log_end_msg 1
177 # exit 0
178 # fi
179 # ;;
180 reload)
181 #log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
182 #log_warning_msg "cannot re-read the config file (use restart)."
183 # Reloading is the same as starting for us
184 start_configs "reload"
187 N=/etc/init.d/$NAME
188 echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
189 exit 1
191 esac
193 exit 0